WebBrowser/GreaseMonkey/GreaseMonkeyManager.py

changeset 5715
cbcca230679f
parent 5389
9b1c800daff3
child 5716
27a7c7064686
--- a/WebBrowser/GreaseMonkey/GreaseMonkeyManager.py	Tue Apr 25 18:40:46 2017 +0200
+++ b/WebBrowser/GreaseMonkey/GreaseMonkeyManager.py	Tue Apr 25 19:20:18 2017 +0200
@@ -12,7 +12,10 @@
 import os
 
 from PyQt5.QtCore import pyqtSignal, pyqtSlot, Qt, QObject, QTimer, QFile, \
-    QDir, QSettings, QMetaObject, QUrl, Q_ARG
+    QFileInfo, QDir, QSettings, QMetaObject, QUrl, Q_ARG, QCoreApplication
+from PyQt5.QtWidgets import QDialog
+
+from E5Gui import E5MessageBox
 
 import Utilities
 import Preferences
@@ -87,19 +90,51 @@
         @type QUrl
         """
         from .GreaseMonkeyDownloader import GreaseMonkeyDownloader
-        downloader = GreaseMonkeyDownloader(url, self)
+        downloader = GreaseMonkeyDownloader(
+            url, self, GreaseMonkeyDownloader.DownloadMainScript)
         downloader.finished.connect(self.__downloaderFinished)
         self.__downloaders.append(downloader)
     
-    def __downloaderFinished(self):
+    def __downloaderFinished(self, fileName):
         """
         Private slot to handle the completion of a script download.
+        
+        @param fileName name of the downloaded script
+        @type str
         """
         downloader = self.sender()
         if downloader is None or downloader not in self.__downloaders:
             return
         
         self.__downloaders.remove(downloader)
+        
+        deleteScript = True
+        from .GreaseMonkeyScript import GreaseMonkeyScript
+        script = GreaseMonkeyScript(self, fileName)
+        if script.isValid():
+            if not self.containsScript(script.fullName()):
+                from .GreaseMonkeyAddScriptDialog import \
+                    GreaseMonkeyAddScriptDialog
+                dlg = GreaseMonkeyAddScriptDialog(self, script)
+                deleteScript = dlg.exec_() != QDialog.Accepted
+            else:
+                E5MessageBox.information(
+                    None,
+                    QCoreApplication.translate(
+                        "GreaseMonkeyManager",
+                        "Install GreaseMonkey Script"),
+                    QCoreApplication.translate(
+                        "GreaseMonkeyManager",
+                        """'{0}' is already installed.""").format(
+                        script.fullName())
+                )
+        
+        if deleteScript:
+            try:
+                os.remove(fileName)
+            except (IOError, OSError):
+                # ignore
+                pass
     
     def scriptsDirectory(self):
         """
@@ -138,13 +173,17 @@
         for url in urlList:
             if settings.contains(url):
                 fileName = settings.value(url)
+                if not QFileInfo(fileName).isAbsolute():
+                    fileName = os.path.join(self.requireScriptsDirectory(),
+                                            fileName)
                 try:
                     f = open(fileName, "r", encoding="utf-8")
-                    source = f.read()
+                    source = f.read().strip()
                     f.close()
                 except (IOError, OSError):
                     source = ""
-                script += source.strip() + "\n"
+                if source:
+                    script += source + "\n"
         
         return script
     

eric ide

mercurial