|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2012 - 2022 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing the synchronization manager class. |
|
8 """ |
|
9 |
|
10 import contextlib |
|
11 |
|
12 from PyQt6.QtCore import QObject, pyqtSignal |
|
13 |
|
14 import Preferences |
|
15 |
|
16 from WebBrowser.WebBrowserWindow import WebBrowserWindow |
|
17 |
|
18 |
|
19 class SyncManager(QObject): |
|
20 """ |
|
21 Class implementing the synchronization manager. |
|
22 |
|
23 @signal syncError(message) emitted for a general error with the error |
|
24 message (string) |
|
25 @signal syncMessage(message) emitted to give status info about the sync |
|
26 process (string) |
|
27 @signal syncStatus(type_, message) emitted to indicate the synchronization |
|
28 status (string one of "bookmarks", "history", "passwords", |
|
29 "useragents" or "speeddial", string) |
|
30 @signal syncFinished(type_, done, download) emitted after a |
|
31 synchronization has finished (string one of "bookmarks", "history", |
|
32 "passwords", "useragents" or "speeddial", boolean, boolean) |
|
33 """ |
|
34 syncError = pyqtSignal(str) |
|
35 syncMessage = pyqtSignal(str) |
|
36 syncStatus = pyqtSignal(str, str) |
|
37 syncFinished = pyqtSignal(str, bool, bool) |
|
38 |
|
39 def __init__(self, parent=None): |
|
40 """ |
|
41 Constructor |
|
42 |
|
43 @param parent reference to the parent object (QObject) |
|
44 """ |
|
45 super().__init__(parent) |
|
46 |
|
47 self.__handler = None |
|
48 |
|
49 def handler(self): |
|
50 """ |
|
51 Public method to get a reference to the sync handler object. |
|
52 |
|
53 @return reference to the sync handler object (SyncHandler) |
|
54 """ |
|
55 return self.__handler |
|
56 |
|
57 def showSyncDialog(self): |
|
58 """ |
|
59 Public method to show the synchronization dialog. |
|
60 """ |
|
61 from .SyncAssistantDialog import SyncAssistantDialog |
|
62 dlg = SyncAssistantDialog() |
|
63 dlg.exec() |
|
64 |
|
65 def loadSettings(self, forceUpload=False): |
|
66 """ |
|
67 Public method to load the settings. |
|
68 |
|
69 @param forceUpload flag indicating a forced upload of the files |
|
70 (boolean) |
|
71 """ |
|
72 if self.__handler is not None: |
|
73 self.__handler.syncError.disconnect(self.__syncError) |
|
74 self.__handler.syncFinished.disconnect(self.__syncFinished) |
|
75 self.__handler.syncStatus.disconnect(self.__syncStatus) |
|
76 self.__handler.syncMessage.disconnect(self.syncMessage) |
|
77 self.__handler.shutdown() |
|
78 |
|
79 if self.syncEnabled(): |
|
80 from . import SyncGlobals |
|
81 if ( |
|
82 Preferences.getWebBrowser("SyncType") == |
|
83 SyncGlobals.SyncTypeFtp |
|
84 ): |
|
85 from .FtpSyncHandler import FtpSyncHandler |
|
86 self.__handler = FtpSyncHandler(self) |
|
87 elif ( |
|
88 Preferences.getWebBrowser("SyncType") == |
|
89 SyncGlobals.SyncTypeDirectory |
|
90 ): |
|
91 from .DirectorySyncHandler import DirectorySyncHandler |
|
92 self.__handler = DirectorySyncHandler(self) |
|
93 self.__handler.syncError.connect(self.__syncError) |
|
94 self.__handler.syncFinished.connect(self.__syncFinished) |
|
95 self.__handler.syncStatus.connect(self.__syncStatus) |
|
96 self.__handler.syncMessage.connect(self.syncMessage) |
|
97 |
|
98 self.__handler.initialLoadAndCheck(forceUpload=forceUpload) |
|
99 |
|
100 # connect sync manager to bookmarks manager |
|
101 if Preferences.getWebBrowser("SyncBookmarks"): |
|
102 ( |
|
103 WebBrowserWindow.bookmarksManager() |
|
104 .bookmarksSaved.connect(self.__syncBookmarks) |
|
105 ) |
|
106 else: |
|
107 with contextlib.suppress(TypeError): |
|
108 ( |
|
109 WebBrowserWindow.bookmarksManager() |
|
110 .bookmarksSaved.disconnect(self.__syncBookmarks) |
|
111 ) |
|
112 |
|
113 # connect sync manager to history manager |
|
114 if Preferences.getWebBrowser("SyncHistory"): |
|
115 ( |
|
116 WebBrowserWindow.historyManager().historySaved |
|
117 .connect(self.__syncHistory) |
|
118 ) |
|
119 else: |
|
120 with contextlib.suppress(TypeError): |
|
121 ( |
|
122 WebBrowserWindow.historyManager() |
|
123 .historySaved.disconnect(self.__syncHistory) |
|
124 ) |
|
125 |
|
126 # connect sync manager to passwords manager |
|
127 if Preferences.getWebBrowser("SyncPasswords"): |
|
128 ( |
|
129 WebBrowserWindow.passwordManager() |
|
130 .passwordsSaved.connect(self.__syncPasswords) |
|
131 ) |
|
132 else: |
|
133 with contextlib.suppress(TypeError): |
|
134 ( |
|
135 WebBrowserWindow.passwordManager() |
|
136 .passwordsSaved.disconnect(self.__syncPasswords) |
|
137 ) |
|
138 |
|
139 # connect sync manager to user agent manager |
|
140 if Preferences.getWebBrowser("SyncUserAgents"): |
|
141 ( |
|
142 WebBrowserWindow.userAgentsManager() |
|
143 .userAgentSettingsSaved.connect(self.__syncUserAgents) |
|
144 ) |
|
145 else: |
|
146 with contextlib.suppress(TypeError): |
|
147 ( |
|
148 WebBrowserWindow.userAgentsManager() |
|
149 .userAgentSettingsSaved.disconnect( |
|
150 self.__syncUserAgents) |
|
151 ) |
|
152 |
|
153 # connect sync manager to speed dial |
|
154 if Preferences.getWebBrowser("SyncSpeedDial"): |
|
155 ( |
|
156 WebBrowserWindow.speedDial() |
|
157 .speedDialSaved.connect(self.__syncSpeedDial) |
|
158 ) |
|
159 else: |
|
160 with contextlib.suppress(TypeError): |
|
161 ( |
|
162 WebBrowserWindow.speedDial() |
|
163 .speedDialSaved.disconnect(self.__syncSpeedDial) |
|
164 ) |
|
165 else: |
|
166 self.__handler = None |
|
167 |
|
168 with contextlib.suppress(TypeError): |
|
169 ( |
|
170 WebBrowserWindow.bookmarksManager() |
|
171 .bookmarksSaved.disconnect(self.__syncBookmarks) |
|
172 ) |
|
173 with contextlib.suppress(TypeError): |
|
174 ( |
|
175 WebBrowserWindow.historyManager().historySaved |
|
176 .disconnect(self.__syncHistory) |
|
177 ) |
|
178 with contextlib.suppress(TypeError): |
|
179 ( |
|
180 WebBrowserWindow.passwordManager() |
|
181 .passwordsSaved.disconnect(self.__syncPasswords) |
|
182 ) |
|
183 with contextlib.suppress(TypeError): |
|
184 ( |
|
185 WebBrowserWindow.userAgentsManager() |
|
186 .userAgentSettingsSaved.disconnect(self.__syncUserAgents) |
|
187 ) |
|
188 with contextlib.suppress(TypeError): |
|
189 WebBrowserWindow.speedDial().speedDialSaved.disconnect( |
|
190 self.__syncSpeedDial) |
|
191 |
|
192 def syncEnabled(self): |
|
193 """ |
|
194 Public method to check, if synchronization is enabled. |
|
195 |
|
196 @return flag indicating enabled synchronization |
|
197 """ |
|
198 from . import SyncGlobals |
|
199 return ( |
|
200 Preferences.getWebBrowser("SyncEnabled") and |
|
201 Preferences.getWebBrowser("SyncType") != SyncGlobals.SyncTypeNone |
|
202 ) |
|
203 |
|
204 def __syncBookmarks(self): |
|
205 """ |
|
206 Private slot to synchronize the bookmarks. |
|
207 """ |
|
208 if self.__handler is not None: |
|
209 self.__handler.syncBookmarks() |
|
210 |
|
211 def __syncHistory(self): |
|
212 """ |
|
213 Private slot to synchronize the history. |
|
214 """ |
|
215 if self.__handler is not None: |
|
216 self.__handler.syncHistory() |
|
217 |
|
218 def __syncPasswords(self): |
|
219 """ |
|
220 Private slot to synchronize the passwords. |
|
221 """ |
|
222 if self.__handler is not None: |
|
223 self.__handler.syncPasswords() |
|
224 |
|
225 def __syncUserAgents(self): |
|
226 """ |
|
227 Private slot to synchronize the user agent settings. |
|
228 """ |
|
229 if self.__handler is not None: |
|
230 self.__handler.syncUserAgents() |
|
231 |
|
232 def __syncSpeedDial(self): |
|
233 """ |
|
234 Private slot to synchronize the speed dial settings. |
|
235 """ |
|
236 if self.__handler is not None: |
|
237 self.__handler.syncSpeedDial() |
|
238 |
|
239 def __syncError(self, message): |
|
240 """ |
|
241 Private slot to handle general synchronization issues. |
|
242 |
|
243 @param message error message (string) |
|
244 """ |
|
245 self.syncError.emit(message) |
|
246 |
|
247 def __syncFinished(self, type_, status, download): |
|
248 """ |
|
249 Private slot to handle a finished synchronization event. |
|
250 |
|
251 @param type_ type of the synchronization event (string one |
|
252 of "bookmarks", "history", "passwords", "useragents" or |
|
253 "speeddial") |
|
254 @param status flag indicating success (boolean) |
|
255 @param download flag indicating a download of a file (boolean) |
|
256 """ |
|
257 if status and download: |
|
258 if type_ == "bookmarks": |
|
259 WebBrowserWindow.bookmarksManager().reload() |
|
260 elif type_ == "history": |
|
261 WebBrowserWindow.historyManager().reload() |
|
262 elif type_ == "passwords": |
|
263 WebBrowserWindow.passwordManager().reload() |
|
264 elif type_ == "useragents": |
|
265 WebBrowserWindow.userAgentsManager().reload() |
|
266 elif type_ == "speeddial": |
|
267 WebBrowserWindow.speedDial().reload() |
|
268 self.syncFinished.emit(type_, status, download) |
|
269 |
|
270 def __syncStatus(self, type_, message): |
|
271 """ |
|
272 Private slot to handle a status update of a synchronization event. |
|
273 |
|
274 @param type_ type of the synchronization event (string one |
|
275 of "bookmarks", "history", "passwords", "useragents" or |
|
276 "speeddial") |
|
277 @param message status message for the event (string) |
|
278 """ |
|
279 self.syncMessage.emit(message) |
|
280 self.syncStatus.emit(type_, message) |
|
281 |
|
282 def close(self): |
|
283 """ |
|
284 Public slot to shut down the synchronization manager. |
|
285 """ |
|
286 if not self.syncEnabled(): |
|
287 return |
|
288 |
|
289 if self.__handler is not None: |
|
290 self.__handler.shutdown() |