src/eric7/EricWidgets/EricPathPicker.py

branch
server
changeset 10605
b6f5e27daeb5
parent 10439
21c28b0f9e41
child 10610
bb0149571d94
--- a/src/eric7/EricWidgets/EricPathPicker.py	Fri Feb 23 16:50:50 2024 +0100
+++ b/src/eric7/EricWidgets/EricPathPicker.py	Fri Feb 23 16:52:01 2024 +0100
@@ -22,8 +22,11 @@
 )
 
 from eric7.EricGui import EricPixmapCache
+from eric7.RemoteServerInterface import EricServerFileDialog
+from eric7.SystemUtilities import FileSystemUtilities
 
 from . import EricFileDialog
+from .EricApplication import ericApp
 from .EricCompleters import EricDirCompleter, EricFileCompleter
 
 
@@ -83,6 +86,8 @@
 
         self.__mode = EricPathPicker.DefaultMode
         self.__editorEnabled = True
+        self.__remote = False
+        self.__remotefsInterface = None
 
         self._completer = None
         self.__filters = ""
@@ -187,6 +192,30 @@
         """
         return self.__mode
 
+    def setRemote(self, remote):
+        """
+        Public method to set the remote mode of the path picker.
+
+        @param remote flag indicating the remote mode
+        @type bool
+        """
+        self.__remote = remote
+        if remote:
+            self.__remotefsInterface = (
+                ericApp().getObject("EricServer").getServiceInterface("FileSystem")
+            )
+        else:
+            self.__remotefsInterface = None
+
+    def isRemote(self):
+        """
+        Public method to get the path picker remote mode.
+
+        @return flag indicating the remote mode
+        @rtype bool
+        """
+        return self.__remote
+
     def setPickerEnabled(self, enable):
         """
         Public method to set the enabled state of the file dialog button.
@@ -290,10 +319,15 @@
             else:
                 return self._editorText()
         else:
-            if toNative:
-                return os.path.expanduser(QDir.toNativeSeparators(self._editorText()))
+            if self.__remote:
+                return self.__remotefsInterface.expanduser(self._editorText())
             else:
-                return os.path.expanduser(self._editorText())
+                if toNative:
+                    return os.path.expanduser(
+                        QDir.toNativeSeparators(self._editorText())
+                    )
+                else:
+                    return os.path.expanduser(self._editorText())
 
     def setEditText(self, fpath, toNative=True):
         """
@@ -370,6 +404,39 @@
         """
         return self.paths()[-1]
 
+    def strPaths(self):
+        """
+        Public method to get the list of entered paths as strings.
+
+        @return entered paths
+        @rtype list of str
+        """
+        if self.__mode in (
+            EricPathPickerModes.OPEN_FILES_MODE,
+            EricPathPickerModes.OPEN_FILES_AND_DIRS_MODE,
+        ):
+            return self.text().split(";")
+        else:
+            return [self.text()]
+
+    def firstStrPath(self):
+        """
+        Public method to get the first path of a list of entered paths as a string.
+
+        @return first path
+        @rtype pathlib.Path
+        """
+        return self.strPaths()[0]
+
+    def lastStrPath(self):
+        """
+        Public method to get the last path of a list of entered paths as a string.
+
+        @return last path
+        @rtype pathlib.Path
+        """
+        return self.strPaths()[-1]
+
     def setEditorEnabled(self, enable):
         """
         Public method to set the path editor's enabled state.
@@ -534,76 +601,138 @@
         directory = self._editorText()
         if not directory and self.__defaultDirectory:
             directory = self.__defaultDirectory
-        directory = (
-            os.path.expanduser(directory.split(";")[0])
-            if self.__mode
-            in (
-                EricPathPickerModes.OPEN_FILES_MODE,
-                EricPathPickerModes.OPEN_FILES_AND_DIRS_MODE,
+        if self.__remote:
+            directory = (
+                self.__remotefsInterface.expanduser(directory.split(";")[0])
+                if self.__mode == EricPathPickerModes.OPEN_FILES_MODE
+                else self.__remotefsInterface.expanduser(directory)
             )
-            else os.path.expanduser(directory)
-        )
-        if not os.path.isabs(directory) and self.__defaultDirectory:
-            directory = os.path.join(self.__defaultDirectory, directory)
-        directory = QDir.fromNativeSeparators(directory)
+        else:
+            directory = (
+                os.path.expanduser(directory.split(";")[0])
+                if self.__mode
+                in (
+                    EricPathPickerModes.OPEN_FILES_MODE,
+                    EricPathPickerModes.OPEN_FILES_AND_DIRS_MODE,
+                )
+                else os.path.expanduser(directory)
+            )
+            if not os.path.isabs(directory) and self.__defaultDirectory:
+                directory = os.path.join(self.__defaultDirectory, directory)
+            directory = QDir.fromNativeSeparators(directory)
 
         if self.__mode == EricPathPickerModes.OPEN_FILE_MODE:
-            fpath = EricFileDialog.getOpenFileName(
-                self, windowTitle, directory, self.__filters
-            )
-            fpath = QDir.toNativeSeparators(fpath)
+            if self.__remote:
+                fpath = EricServerFileDialog.getOpenFileName(
+                    self, windowTitle, directory, self.__filters
+                )
+            else:
+                fpath = EricFileDialog.getOpenFileName(
+                    self, windowTitle, directory, self.__filters
+                )
+                fpath = QDir.toNativeSeparators(fpath)
         elif self.__mode == EricPathPickerModes.OPEN_FILES_MODE:
-            fpaths = EricFileDialog.getOpenFileNames(
-                self, windowTitle, directory, self.__filters
-            )
-            fpath = ";".join([QDir.toNativeSeparators(fpath) for fpath in fpaths])
+            if self.__remote:
+                fpaths = EricServerFileDialog.getOpenFileNames(
+                    self, windowTitle, directory, self.__filters
+                )
+                fpath = ";".join(fpaths)
+            else:
+                fpaths = EricFileDialog.getOpenFileNames(
+                    self, windowTitle, directory, self.__filters
+                )
+                fpath = ";".join([QDir.toNativeSeparators(fpath) for fpath in fpaths])
         elif self.__mode == EricPathPickerModes.OPEN_FILES_AND_DIRS_MODE:
+            # that is not supported for 'remote' pickers
             fpaths = EricFileDialog.getOpenFileAndDirNames(
                 self, windowTitle, directory, self.__filters
             )
             fpath = ";".join([QDir.toNativeSeparators(fpath) for fpath in fpaths])
         elif self.__mode == EricPathPickerModes.SAVE_FILE_MODE:
-            fpath = EricFileDialog.getSaveFileName(
-                self,
-                windowTitle,
-                directory,
-                self.__filters,
-                EricFileDialog.DontConfirmOverwrite,
-            )
-            fpath = QDir.toNativeSeparators(fpath)
+            if self.__remote:
+                fpath = EricServerFileDialog.getSaveFileName(
+                    self,
+                    windowTitle,
+                    directory,
+                    self.__filters,
+                )
+            else:
+                fpath = EricFileDialog.getSaveFileName(
+                    self,
+                    windowTitle,
+                    directory,
+                    self.__filters,
+                    EricFileDialog.DontConfirmOverwrite,
+                )
+                fpath = QDir.toNativeSeparators(fpath)
         elif self.__mode == EricPathPickerModes.SAVE_FILE_ENSURE_EXTENSION_MODE:
-            fpath, selectedFilter = EricFileDialog.getSaveFileNameAndFilter(
-                self,
-                windowTitle,
-                directory,
-                self.__filters,
-                None,
-                EricFileDialog.DontConfirmOverwrite,
-            )
-            fpath = pathlib.Path(fpath)
-            if not fpath.suffix:
-                ex = selectedFilter.split("(*")[1].split(")")[0].split()[0]
-                if ex:
-                    fpath = fpath.with_suffix(ex)
+            if self.__remote:
+                fpath, selectedFilter = EricServerFileDialog.getSaveFileNameAndFilter(
+                    self,
+                    windowTitle,
+                    directory,
+                    self.__filters,
+                )
+                fn, ext = self.__remotefsInterface.splitext(fpath)
+                if not ext:
+                    ex = selectedFilter.split("(*")[1].split(")")[0].split()[0]
+                    if ex:
+                        fpath = f"{fn}{ex}"
+            else:
+                fpath, selectedFilter = EricFileDialog.getSaveFileNameAndFilter(
+                    self,
+                    windowTitle,
+                    directory,
+                    self.__filters,
+                    None,
+                    EricFileDialog.DontConfirmOverwrite,
+                )
+                fpath = pathlib.Path(fpath)
+                if not fpath.suffix:
+                    ex = selectedFilter.split("(*")[1].split(")")[0].split()[0]
+                    if ex:
+                        fpath = fpath.with_suffix(ex)
         elif self.__mode == EricPathPickerModes.SAVE_FILE_OVERWRITE_MODE:
-            fpath = EricFileDialog.getSaveFileName(
-                self, windowTitle, directory, self.__filters
-            )
-            fpath = QDir.toNativeSeparators(fpath)
+            if self.__remote:
+                fpath = EricServerFileDialog.getSaveFileName(
+                    self,
+                    windowTitle,
+                    directory,
+                    self.__filters,
+                )
+            else:
+                fpath = EricFileDialog.getSaveFileName(
+                    self, windowTitle, directory, self.__filters
+                )
+                fpath = QDir.toNativeSeparators(fpath)
         elif self.__mode == EricPathPickerModes.DIRECTORY_MODE:
-            fpath = EricFileDialog.getExistingDirectory(
-                self, windowTitle, directory, EricFileDialog.ShowDirsOnly
-            )
-            fpath = QDir.toNativeSeparators(fpath)
-            while fpath.endswith(os.sep):
-                fpath = fpath[:-1]
+            if self.__remote:
+                fpath = EricServerFileDialog.getExistingDirectory(
+                    self, windowTitle, directory, True
+                )
+                while fpath.endswith(self.__remotefsInterface.separator()):
+                    fpath = fpath[:-1]
+            else:
+                fpath = EricFileDialog.getExistingDirectory(
+                    self, windowTitle, directory, EricFileDialog.ShowDirsOnly
+                )
+                fpath = QDir.toNativeSeparators(fpath)
+                while fpath.endswith(os.sep):
+                    fpath = fpath[:-1]
         elif self.__mode == EricPathPickerModes.DIRECTORY_SHOW_FILES_MODE:
-            fpath = EricFileDialog.getExistingDirectory(
-                self, windowTitle, directory, EricFileDialog.DontUseNativeDialog
-            )
-            fpath = QDir.toNativeSeparators(fpath)
-            while fpath.endswith(os.sep):
-                fpath = fpath[:-1]
+            if self.__remote:
+                fpath = EricServerFileDialog.getExistingDirectory(
+                    self, windowTitle, directory, False
+                )
+                while fpath.endswith(self.__remotefsInterface):
+                    fpath = fpath[:-1]
+            else:
+                fpath = EricFileDialog.getExistingDirectory(
+                    self, windowTitle, directory, EricFileDialog.DontUseNativeDialog
+                )
+                fpath = QDir.toNativeSeparators(fpath)
+                while fpath.endswith(os.sep):
+                    fpath = fpath[:-1]
 
         if fpath:
             self._setEditorText(str(fpath))
@@ -766,3 +895,17 @@
         for index in range(self._editor.count()):
             paths.append(pathlib.Path(self._editor.itemText(index)))
         return paths
+
+    def getRemotePathItems(self):
+        """
+        Public method to get the list of remembered remote paths.
+
+        @return list of remembered paths
+        @rtype list of str
+        """
+        paths = []
+        for index in range(self._editor.count()):
+            paths.append(
+                FileSystemUtilities.remoteFileName(self._editor.itemText(index))
+            )
+        return paths

eric ide

mercurial