1# -*- coding: utf-8 -*-
2#
3# PhotoFilmStrip - Creates movies out of your pictures.
4#
5# Copyright (C) 2008 Jens Goepfert
6#
7
8import io
9import sys
10import traceback
11import urllib.parse
12import urllib.request
13
14import wx
15from wx.lib.wordwrap import wordwrap
16
17from photofilmstrip import Constants
18
19
20class DlgBugReport(wx.Dialog):
21
22    PARENT = None
23
24    @classmethod
25    def Initialize(cls, parent):
26        cls.PARENT = parent
27
28        def excepthook(etype, value, tb):
29            if not getattr(sys, 'frozen', False):
30                traceback.print_exception(etype, value, tb)
31            output = io.StringIO()
32            traceback.print_exception(etype, value, tb, file=output)
33            dlg = DlgBugReport(cls.PARENT, output.getvalue())
34            dlg.ShowModal()
35            dlg.Destroy()
36
37        sys.excepthook = excepthook
38
39    def __init__(self, parent, msg):
40        wx.Dialog.__init__(self, parent, -1,
41                           _(u"An unexpected error occured"),
42                           name=u'DlgBugReport')
43
44        text = _(u"An unexpected error occured. Do you want to send this bug report to the developers of %s?") % Constants.APP_NAME
45
46        stBmp = wx.StaticBitmap(
47            self, -1,
48            wx.ArtProvider.GetBitmap(wx.ART_ERROR, wx.ART_OTHER, (32, 32)))
49        stMsg = wx.StaticText(self, -1, wordwrap(text, 300, wx.ClientDC(self)))
50
51        szTop = wx.BoxSizer(wx.HORIZONTAL)
52        szTop.Add(stBmp, 0, wx.ALIGN_CENTER_VERTICAL | wx.ALL, 8)
53        szTop.Add(stMsg, 0, wx.ALL, 8)
54
55        self.tcMsg = wx.TextCtrl(
56            self, -1, msg,
57            style=wx.TE_MULTILINE | wx.TE_READONLY | wx.TE_DONTWRAP)
58
59        szCmd = self.CreateSeparatedButtonSizer(wx.YES | wx.NO)
60
61        szMain = wx.BoxSizer(wx.VERTICAL)
62        szMain.Add(szTop, 0)
63        szMain.Add(self.tcMsg, 1, wx.EXPAND | wx.ALL, 4)
64        szMain.Add(szCmd, 0, wx.EXPAND | wx.ALL, 4)
65
66        self.SetSizer(szMain)
67
68        self.Bind(wx.EVT_BUTTON, self.OnNo, id=wx.ID_NO)
69        self.Bind(wx.EVT_BUTTON, self.OnYes, id=wx.ID_YES)
70
71        self.SetAffirmativeId(wx.ID_YES)
72        self.SetEscapeId(wx.ID_NO)
73        self.SetInitialSize(self.GetEffectiveMinSize())
74        self.CenterOnParent()
75        self.SetFocus()
76
77    def OnYes(self, event):  # pylint: disable=unused-argument
78        info = "\n".join([sys.platform,
79                          sys.getdefaultencoding(),
80                          sys.getfilesystemencoding(),
81                          str(getattr(sys, 'frozen', False))])
82        params = urllib.parse.urlencode(
83            {'bugreport': "%s-%s\n\n%s\n%s\n" % (Constants.APP_NAME,
84                                                 Constants.APP_VERSION_EX,
85                                                 self.tcMsg.GetValue(),
86                                                 info)})
87        params = params.encode('utf_8')
88        try:
89            fd = urllib.request.urlopen(
90                "http://www.photofilmstrip.org/bugreport.php", params)
91            result = fd.read()
92            result = result.decode("utf-8")
93        except IOError:
94            result = None
95
96        if result and result.find("Result 1") != -1:
97            dlg = wx.MessageDialog(
98                self,
99                _(u"Bug-Report send. Thank you for your support."),
100                _(u"Information"),
101                wx.OK | wx.ICON_INFORMATION)
102            dlg.ShowModal()
103            dlg.Destroy()
104        else:
105            dlg = wx.MessageDialog(self,
106                                   _(u"Sorry, this function is temporary not available.."),
107                                   _(u"Error"),
108                                   wx.OK | wx.ICON_ERROR)
109            dlg.ShowModal()
110            dlg.Destroy()
111
112        self.EndModal(wx.ID_YES)
113
114    def OnNo(self, event):
115        self.EndModal(wx.ID_NO)
116        event.Skip()
117