|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2012 - 2021 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 |
|
7 """ |
|
8 Module implementing a dialog for adding GreaseMonkey scripts.. |
|
9 """ |
|
10 |
|
11 import os |
|
12 |
|
13 from PyQt5.QtCore import pyqtSlot, QDir, QFile |
|
14 from PyQt5.QtWidgets import QDialog |
|
15 |
|
16 from .Ui_GreaseMonkeyAddScriptDialog import Ui_GreaseMonkeyAddScriptDialog |
|
17 |
|
18 import UI.PixmapCache |
|
19 from UI.NotificationWidget import NotificationTypes |
|
20 |
|
21 |
|
22 class GreaseMonkeyAddScriptDialog(QDialog, Ui_GreaseMonkeyAddScriptDialog): |
|
23 """ |
|
24 Class implementing a dialog for adding GreaseMonkey scripts.. |
|
25 """ |
|
26 def __init__(self, manager, script, parent=None): |
|
27 """ |
|
28 Constructor |
|
29 |
|
30 @param manager reference to the GreaseMonkey manager |
|
31 (GreaseMonkeyManager) |
|
32 @param script GreaseMonkey script to be added (GreaseMonkeyScript) |
|
33 @param parent reference to the parent widget (QWidget) |
|
34 """ |
|
35 super().__init__(parent) |
|
36 self.setupUi(self) |
|
37 |
|
38 self.iconLabel.setPixmap( |
|
39 UI.PixmapCache.getPixmap("greaseMonkey48")) |
|
40 |
|
41 self.__manager = manager |
|
42 self.__script = script |
|
43 |
|
44 runsAt = "" |
|
45 doesNotRunAt = "" |
|
46 |
|
47 include = script.include() |
|
48 exclude = script.exclude() |
|
49 |
|
50 if include: |
|
51 runsAt = self.tr("<p>runs at:<br/><i>{0}</i></p>").format( |
|
52 "<br/>".join(include)) |
|
53 |
|
54 if exclude: |
|
55 doesNotRunAt = self.tr( |
|
56 "<p>does not run at:<br/><i>{0}</i></p>").format( |
|
57 "<br/>".join(exclude)) |
|
58 |
|
59 scriptInfoTxt = "<p><b>{0}</b> {1}<br/>{2}</p>{3}{4}".format( |
|
60 script.name(), script.version(), script.description(), runsAt, |
|
61 doesNotRunAt) |
|
62 self.scriptInfo.setHtml(scriptInfoTxt) |
|
63 |
|
64 self.accepted.connect(self.__accepted) |
|
65 |
|
66 @pyqtSlot() |
|
67 def on_showScriptSourceButton_clicked(self): |
|
68 """ |
|
69 Private slot to show an editor window with the source code. |
|
70 """ |
|
71 from WebBrowser.Tools import WebBrowserTools |
|
72 |
|
73 tmpFileName = WebBrowserTools.ensureUniqueFilename( |
|
74 os.path.join(QDir.tempPath(), "tmp-userscript.js")) |
|
75 if QFile.copy(self.__script.fileName(), tmpFileName): |
|
76 from QScintilla.MiniEditor import MiniEditor |
|
77 editor = MiniEditor(tmpFileName, "JavaScript", self) |
|
78 editor.show() |
|
79 |
|
80 def __accepted(self): |
|
81 """ |
|
82 Private slot handling the accepted signal. |
|
83 """ |
|
84 if self.__manager.addScript(self.__script): |
|
85 msg = self.tr( |
|
86 "<p><b>{0}</b> installed successfully.</p>").format( |
|
87 self.__script.name()) |
|
88 success = True |
|
89 else: |
|
90 msg = self.tr("<p>Cannot install script.</p>") |
|
91 success = False |
|
92 |
|
93 from WebBrowser.WebBrowserWindow import WebBrowserWindow |
|
94 if success: |
|
95 WebBrowserWindow.showNotification( |
|
96 UI.PixmapCache.getPixmap("greaseMonkey48"), |
|
97 self.tr("GreaseMonkey Script Installation"), |
|
98 msg) |
|
99 else: |
|
100 WebBrowserWindow.showNotification( |
|
101 UI.PixmapCache.getPixmap("greaseMonkey48"), |
|
102 self.tr("GreaseMonkey Script Installation"), |
|
103 msg, |
|
104 kind=NotificationTypes.CRITICAL, |
|
105 timeout=0) |