10 from __future__ import unicode_literals |
10 from __future__ import unicode_literals |
11 |
11 |
12 import os |
12 import os |
13 |
13 |
14 from PyQt5.QtCore import pyqtSignal, pyqtSlot, Qt, QObject, QTimer, QFile, \ |
14 from PyQt5.QtCore import pyqtSignal, pyqtSlot, Qt, QObject, QTimer, QFile, \ |
15 QDir, QSettings, QMetaObject, QUrl, Q_ARG |
15 QFileInfo, QDir, QSettings, QMetaObject, QUrl, Q_ARG, QCoreApplication |
|
16 from PyQt5.QtWidgets import QDialog |
|
17 |
|
18 from E5Gui import E5MessageBox |
16 |
19 |
17 import Utilities |
20 import Utilities |
18 import Preferences |
21 import Preferences |
19 |
22 |
20 from WebBrowser.WebBrowserWindow import WebBrowserWindow |
23 from WebBrowser.WebBrowserWindow import WebBrowserWindow |
85 |
88 |
86 @param url URL to download script from |
89 @param url URL to download script from |
87 @type QUrl |
90 @type QUrl |
88 """ |
91 """ |
89 from .GreaseMonkeyDownloader import GreaseMonkeyDownloader |
92 from .GreaseMonkeyDownloader import GreaseMonkeyDownloader |
90 downloader = GreaseMonkeyDownloader(url, self) |
93 downloader = GreaseMonkeyDownloader( |
|
94 url, self, GreaseMonkeyDownloader.DownloadMainScript) |
91 downloader.finished.connect(self.__downloaderFinished) |
95 downloader.finished.connect(self.__downloaderFinished) |
92 self.__downloaders.append(downloader) |
96 self.__downloaders.append(downloader) |
93 |
97 |
94 def __downloaderFinished(self): |
98 def __downloaderFinished(self, fileName): |
95 """ |
99 """ |
96 Private slot to handle the completion of a script download. |
100 Private slot to handle the completion of a script download. |
|
101 |
|
102 @param fileName name of the downloaded script |
|
103 @type str |
97 """ |
104 """ |
98 downloader = self.sender() |
105 downloader = self.sender() |
99 if downloader is None or downloader not in self.__downloaders: |
106 if downloader is None or downloader not in self.__downloaders: |
100 return |
107 return |
101 |
108 |
102 self.__downloaders.remove(downloader) |
109 self.__downloaders.remove(downloader) |
|
110 |
|
111 deleteScript = True |
|
112 from .GreaseMonkeyScript import GreaseMonkeyScript |
|
113 script = GreaseMonkeyScript(self, fileName) |
|
114 if script.isValid(): |
|
115 if not self.containsScript(script.fullName()): |
|
116 from .GreaseMonkeyAddScriptDialog import \ |
|
117 GreaseMonkeyAddScriptDialog |
|
118 dlg = GreaseMonkeyAddScriptDialog(self, script) |
|
119 deleteScript = dlg.exec_() != QDialog.Accepted |
|
120 else: |
|
121 E5MessageBox.information( |
|
122 None, |
|
123 QCoreApplication.translate( |
|
124 "GreaseMonkeyManager", |
|
125 "Install GreaseMonkey Script"), |
|
126 QCoreApplication.translate( |
|
127 "GreaseMonkeyManager", |
|
128 """'{0}' is already installed.""").format( |
|
129 script.fullName()) |
|
130 ) |
|
131 |
|
132 if deleteScript: |
|
133 try: |
|
134 os.remove(fileName) |
|
135 except (IOError, OSError): |
|
136 # ignore |
|
137 pass |
103 |
138 |
104 def scriptsDirectory(self): |
139 def scriptsDirectory(self): |
105 """ |
140 """ |
106 Public method to get the path of the scripts directory. |
141 Public method to get the path of the scripts directory. |
107 |
142 |
136 QSettings.IniFormat) |
171 QSettings.IniFormat) |
137 settings.beginGroup("Files") |
172 settings.beginGroup("Files") |
138 for url in urlList: |
173 for url in urlList: |
139 if settings.contains(url): |
174 if settings.contains(url): |
140 fileName = settings.value(url) |
175 fileName = settings.value(url) |
|
176 if not QFileInfo(fileName).isAbsolute(): |
|
177 fileName = os.path.join(self.requireScriptsDirectory(), |
|
178 fileName) |
141 try: |
179 try: |
142 f = open(fileName, "r", encoding="utf-8") |
180 f = open(fileName, "r", encoding="utf-8") |
143 source = f.read() |
181 source = f.read().strip() |
144 f.close() |
182 f.close() |
145 except (IOError, OSError): |
183 except (IOError, OSError): |
146 source = "" |
184 source = "" |
147 script += source.strip() + "\n" |
185 if source: |
|
186 script += source + "\n" |
148 |
187 |
149 return script |
188 return script |
150 |
189 |
151 def saveConfiguration(self): |
190 def saveConfiguration(self): |
152 """ |
191 """ |