eric6/UI/UserInterface.py

branch
jsonfiles
changeset 8009
29818ac4853c
parent 7973
e836d196e888
child 8011
630a173cb137
diff -r c4110b8b5931 -r 29818ac4853c eric6/UI/UserInterface.py
--- a/eric6/UI/UserInterface.py	Mon Jan 25 20:07:51 2021 +0100
+++ b/eric6/UI/UserInterface.py	Wed Jan 27 15:09:20 2021 +0100
@@ -51,6 +51,8 @@
 
 import UI.PixmapCache
 
+from Sessions.SessionFile import SessionFile
+
 from E5Network.E5NetworkProxyFactory import (
     E5NetworkProxyFactory, proxyAuthenticationRequired
 )
@@ -610,6 +612,9 @@
         self.__inVersionCheck = False
         self.__versionCheckProgress = None
         
+        # create the various JSON file interfaces
+        self.__sessionFile = SessionFile(True)
+        
         # Initialize the actions, menus, toolbars and statusbar
         splash.showMessage(self.tr("Initializing Actions..."))
         self.__initActions()
@@ -6516,7 +6521,7 @@
         
     def __writeSession(self, filename="", crashSession=False):
         """
-        Private slot to write the session data to an XML file (.e5s).
+        Private slot to write the session data to a JSON file (.esj).
         
         @param filename name of a session file to write
         @type str
@@ -6525,33 +6530,38 @@
         @return flag indicating success
         @rtype bool
         """
-        res = False
         if filename:
             fn = filename
         elif crashSession:
             fn = os.path.join(Utilities.getConfigDir(),
-                              "eric6_crash_session.e5s")
+                              "eric6_crash_session.esj")
         else:
             fn = os.path.join(Utilities.getConfigDir(),
-                              "eric6session.e5s")
-        f = QFile(fn)
-        if f.open(QIODevice.WriteOnly):
-            from E5XML.SessionWriter import SessionWriter
-            SessionWriter(f, None).writeXML()
-            f.close()
-            res = True
+                              "eric6session.esj")
+        
+        if fn.endswith(".esj"):
+            res = self.__sessionFile.writeFile(fn)
         else:
-            E5MessageBox.critical(
-                self,
-                self.tr("Save session"),
-                self.tr("<p>The session file <b>{0}</b> could not be"
-                        " written.</p>")
-                .format(fn))
+            f = QFile(fn)
+            if f.open(QIODevice.WriteOnly):
+                from E5XML.SessionWriter import SessionWriter
+                SessionWriter(f, None).writeXML()
+                f.close()
+                res = True
+            else:
+                E5MessageBox.critical(
+                    self,
+                    self.tr("Save Session"),
+                    self.tr("<p>The session file <b>{0}</b> could not be"
+                            " written.</p>")
+                    .format(fn))
+                res = False
+        
         return res
-        
+    
     def __readSession(self, filename=""):
         """
-        Private slot to read in the session file (.e5s or .e4s).
+        Private slot to read in the session file (.esj or .e5s).
         
         @param filename name of a session file to read
         @type str
@@ -6562,14 +6572,14 @@
             fn = filename
         else:
             fn = os.path.join(Utilities.getConfigDir(),
-                              "eric6session.e5s")
+                              "eric6session.esj")
             if not os.path.exists(fn):
                 fn = os.path.join(Utilities.getConfigDir(),
-                                  "eric6session.e4s")
+                                  "eric6session.e5s")
                 if not os.path.exists(fn):
                     E5MessageBox.critical(
                         self,
-                        self.tr("Read session"),
+                        self.tr("Read Session"),
                         self.tr("<p>The session file <b>{0}</b> could not"
                                 " be read.</p>")
                         .format(fn))
@@ -6577,22 +6587,29 @@
         
         res = False
         if fn:
-            f = QFile(fn)
-            if f.open(QIODevice.ReadOnly):
-                from E5XML.SessionReader import SessionReader
+            if fn.endswith(".esj"):
+                # new JSON based format
                 self.__readingSession = True
-                reader = SessionReader(f, True)
-                reader.readXML()
+                res = self.__sessionFile.readFile(fn)
                 self.__readingSession = False
-                f.close()
-                res = True
             else:
-                E5MessageBox.critical(
-                    self,
-                    self.tr("Read session"),
-                    self.tr("<p>The session file <b>{0}</b> could not be"
-                            " read.</p>")
-                    .format(fn))
+                # old XML based format
+                f = QFile(fn)
+                if f.open(QIODevice.ReadOnly):
+                    from E5XML.SessionReader import SessionReader
+                    self.__readingSession = True
+                    reader = SessionReader(f, True)
+                    reader.readXML()
+                    self.__readingSession = False
+                    f.close()
+                    res = True
+                else:
+                    E5MessageBox.critical(
+                        self,
+                        self.tr("Read session"),
+                        self.tr("<p>The session file <b>{0}</b> could not be"
+                                " read.</p>")
+                        .format(fn))
         
         # Write a crash session after a session was read.
         self.__writeCrashSession()
@@ -6605,9 +6622,10 @@
         """
         sessionFile, selectedFilter = E5FileDialog.getSaveFileNameAndFilter(
             self,
-            self.tr("Save session"),
+            self.tr("Save Session"),
             Utilities.getHomeDir(),
-            self.tr("eric Session Files (*.e5s)"),
+            self.tr("eric Session Files (*.esj);;"
+                    "eric XML Session Files (*.e5s)"),
             "")
         
         if not sessionFile:
@@ -6629,7 +6647,8 @@
             self,
             self.tr("Load session"),
             Utilities.getHomeDir(),
-            self.tr("eric Session Files (*.e5s)"))
+            self.tr("eric Session Files (*.esj);;"
+                    "eric XML Session Files (*.e5s)"))
         
         if not sessionFile:
             return
@@ -6640,14 +6659,15 @@
         """
         Private slot to delete the crash session file.
         """
-        fn = os.path.join(Utilities.getConfigDir(),
-                          "eric6_crash_session.e5s")
-        if os.path.exists(fn):
-            try:
-                os.remove(fn)
-            except OSError:
-                # ignore it silently
-                pass
+        for ext in (".esj", ".e5s"):
+            fn = os.path.join(Utilities.getConfigDir(),
+                              f"eric6_crash_session{ext}")
+            if os.path.exists(fn):
+                try:
+                    os.remove(fn)
+                except OSError:
+                    # ignore it silently
+                    pass
     
     def __writeCrashSession(self):
         """
@@ -6674,7 +6694,7 @@
             Preferences.getUI("OpenCrashSessionOnStartup")
         ):
             fn = os.path.join(Utilities.getConfigDir(),
-                              "eric6_crash_session.e5s")
+                              "eric6_crash_session.esj")
             if os.path.exists(fn):
                 yes = E5MessageBox.yesNo(
                     self,

eric ide

mercurial