First attempt on implementing support for desktop session management. eric7

Tue, 25 May 2021 17:12:48 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Tue, 25 May 2021 17:12:48 +0200
branch
eric7
changeset 8374
82151410efbb
parent 8373
4f81e18e4d81
child 8375
057f17f82bee

First attempt on implementing support for desktop session management.

eric7/UI/UserInterface.py file | annotate | diff | comparison | revisions
--- a/eric7/UI/UserInterface.py	Mon May 24 11:21:24 2021 +0200
+++ b/eric7/UI/UserInterface.py	Tue May 25 17:12:48 2021 +0200
@@ -22,7 +22,9 @@
     QIODevice, qVersion, QProcess, QSize, QUrl, QObject, Qt, QUuid, QThread,
     QUrlQuery
 )
-from PyQt6.QtGui import QAction, QKeySequence, QDesktopServices
+from PyQt6.QtGui import (
+    QAction, QKeySequence, QDesktopServices, QSessionManager
+)
 from PyQt6.QtWidgets import (
     QSizePolicy, QWidget, QWhatsThis, QToolBar, QDialog, QSplitter,
     QApplication, QMenu, QVBoxLayout, QDockWidget, QLabel
@@ -132,7 +134,6 @@
         self.__nWrite(self.__bufferedWrite())
 
 
-# TODO: add support for QSessionManager to save changed files
 class UserInterface(EricMainWindow):
     """
     Class implementing the main user interface.
@@ -750,6 +751,11 @@
         if interval > 0:
             QApplication.setKeyboardInputInterval(interval)
         
+        # connect to the desktop environment session manager
+        app.commitDataRequest.connect(self.__commitData,
+                                      Qt.ConnectionType.DirectConnection)
+        app.saveStateRequest.connect(self.__saveState)
+        
     def networkAccessManager(self):
         """
         Public method to get a reference to the network access manager object.
@@ -7487,9 +7493,9 @@
         """
         self.__activateIRC()
     
-    ###############################################
-    ## Support for Code Documentation Viewer  below
-    ###############################################
+    ##############################################
+    ## Support for Code Documentation Viewer below
+    ##############################################
     
     def documentationViewer(self):
         """
@@ -7499,3 +7505,57 @@
         @rtype CodeDocumentationViewer
         """
         return self.codeDocumentationViewer
+    
+    ###############################################
+    ## Support for Desktop session management below
+    ###############################################
+    
+    def __commitData(self, manager: QSessionManager):
+        """
+        Private slot to commit unsaved data when instructed by the desktop
+        session manager.
+        
+        @param manager reference to the desktop session manager
+        @type QSessionManager
+        """
+        if self.viewmanager.hasDirtyEditor():
+            if manager.allowsInteraction():
+                res = EricMessageBox.warning(
+                    self,
+                    self.tr("Unsaved Data Detected"),
+                    self.tr("Some editors contain unsaved data. Shall these"
+                            " be saved?"),
+                    EricMessageBox.Abort |
+                    EricMessageBox.Discard |
+                    EricMessageBox.Save |
+                    EricMessageBox.SaveAll,
+                    EricMessageBox.SaveAll)
+                if res == EricMessageBox.SaveAll:
+                    manager.release()
+                    self.viewmanager.saveAllEditors()
+                elif res == EricMessageBox.Save:
+                    manager.release()
+                    ok = self.viewmanager.checkAllDirty()
+                    if not ok:
+                        manager.cancel()
+                elif res == EricMessageBox.Discard:
+                    # nothing to do
+                    pass
+                else:
+                    # default action is to abort shutdown
+                    manager.cancel()
+            else:
+                # We did not get permission to interact, play it safe and
+                # save all data.
+                self.viewmanager.saveAllEditors()
+    
+    def __saveState(self, manager: QSessionManager):
+        """
+        Private slot to save the state when instructed by the desktop session
+        manager.
+        
+        @param manager reference to the desktop session manager
+        @type QSessionManager
+        """
+        # TODO: implement saving the state
+        print("__saveState() call by session manager")

eric ide

mercurial