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