Example #1
0
class KhtEditor:
    def __init__(self):
        self.window_list = []
        self.version = __version__

        self.app = QApplication(sys.argv)
        self.app.setOrganizationName("Khertan Software")
        self.app.setOrganizationDomain("khertan.net")
        self.app.setApplicationName("KhtEditor")

        install_excepthook(self.app.applicationName(), self.version)

        self.last_know_path = '/home/user/MyDocs'
        self.run()

    def crash_report(self):
        if os.path.isfile(
                os.path.join(
                    os.path.join(os.path.expanduser("~"),
                                 '.khteditor_crash_report'))):
            import urllib2
            import urllib
            import pickle
            if ((QMessageBox.question(
                    None, "KhtEditor Crash Report",
                    "An error occur on KhtEditor in the previous launch. Report this bug on the bug tracker ?",
                    QMessageBox.Yes | QMessageBox.Close)) == QMessageBox.Yes):
                url = 'http://khertan.net/report.php'  # write ur URL here
                try:
                    filename = os.path.join(
                        os.path.join(os.path.expanduser("~"),
                                     '.khteditor_crash_report'))
                    output = open(filename, 'rb')
                    error = pickle.load(output)
                    output.close()

                    values = {
                        'project': 'khteditor',
                        'version': __version__,
                        'description': error,
                    }

                    data = urllib.urlencode(values)
                    req = urllib2.Request(url, data)
                    response = urllib2.urlopen(req)
                    the_page = response.read()
                except Exception as detail:
                    print(detail)
                    QMessageBox.question(
                        None, "KhtEditor Crash Report",
                        "An error occur during the report : %s" % detail,
                        QMessageBox.Close)
                    return False

                if 'Your report have been successfully stored' in the_page:
                    QMessageBox.question(None, "KhtEditor Crash Report",
                                         "%s" % the_page, QMessageBox.Close)
                    return True
                else:
                    print('page:', the_page)
                    QMessageBox.question(None, "KhtEditor Crash Report",
                                         "%s" % the_page, QMessageBox.Close)
                    return False
            try:
                os.remove(
                    os.path.join(
                        os.path.join(os.path.expanduser("~"),
                                     '.khteditor_crash_report')))
            except:
                pass

    def run(self):
        """
            Run method
        """

        window = WelcomeWindow(self)
        window.show()
        self.crash_report()

        for arg in self.app.argv()[1:]:
            path = os.path.abspath(arg)
            if os.path.isfile(unicode(path)):
                editor_win = EditorWindow(self)
                self.window_list.append(editor_win)
                editor_win.loadFile(unicode(path))
                editor_win.show()
                self.last_know_path = os.path.dirname(unicode(path))
                RecentFiles().append(unicode(path))

        sys.exit(self.app.exec_())

    def about(self, widget):
        self.aboutWin = KhtEditorAbout(widget)

    def newFile(self):
        """
            Create a new editor window
        """

        editor_win = EditorWindow(self)
        editor_win.show()
        self.window_list.append(editor_win)

    def openFile(self):
        """
            Create a new editor window and open selected file
        """
        editor_win = EditorWindow(self)
        editor_win.show()
        filename = editor_win.openFile(self.last_know_path)
        if not (filename == ''):
            self.window_list.append(editor_win)
            RecentFiles().append(filename)
            self.last_know_path = os.path.dirname(unicode(filename))


#        else:
#          editor_win.destroy()

    def openRecentFile(self, path):
        """
            Create a new editor window and open a recent file
        """
        editor_win = EditorWindow(self)
        self.window_list.append(editor_win)
        editor_win.show()
        try:
            editor_win.loadFile(path.valueText())
            RecentFiles().append(path.valueText())
            self.last_know_path = os.path.dirname(unicode(path.valueText()))
        except:
            editor_win.loadFile(path.text())
            RecentFiles().append(path.text())
            self.last_know_path = os.path.dirname(unicode(path.text()))

    def showPrefs(self, win):
        """
            Instantiate the prefs window and show it
        """

        self.khtsettings = settings.KhtSettings(win)
        self.khtsettings.show()