|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2012 - 2022 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a synchronization handler using a shared directory. |
|
8 """ |
|
9 |
|
10 import os |
|
11 import pathlib |
|
12 |
|
13 from PyQt6.QtCore import pyqtSignal, QByteArray, QCoreApplication |
|
14 |
|
15 from .SyncHandler import SyncHandler |
|
16 |
|
17 from WebBrowser.WebBrowserWindow import WebBrowserWindow |
|
18 |
|
19 import Preferences |
|
20 |
|
21 |
|
22 class DirectorySyncHandler(SyncHandler): |
|
23 """ |
|
24 Class implementing a synchronization handler using a shared directory. |
|
25 |
|
26 @signal syncStatus(type_, message) emitted to indicate the synchronization |
|
27 status (string one of "bookmarks", "history", "passwords", |
|
28 "useragents" or "speeddial", string) |
|
29 @signal syncError(message) emitted for a general error with the error |
|
30 message (string) |
|
31 @signal syncMessage(message) emitted to send a message about |
|
32 synchronization (string) |
|
33 @signal syncFinished(type_, done, download) emitted after a |
|
34 synchronization has finished (string one of "bookmarks", "history", |
|
35 "passwords", "useragents" or "speeddial", boolean, boolean) |
|
36 """ |
|
37 syncStatus = pyqtSignal(str, str) |
|
38 syncError = pyqtSignal(str) |
|
39 syncMessage = pyqtSignal(str) |
|
40 syncFinished = pyqtSignal(str, bool, bool) |
|
41 |
|
42 def __init__(self, parent=None): |
|
43 """ |
|
44 Constructor |
|
45 |
|
46 @param parent reference to the parent object (QObject) |
|
47 """ |
|
48 super().__init__(parent) |
|
49 self.__forceUpload = False |
|
50 |
|
51 self.__remoteFilesFound = [] |
|
52 |
|
53 def initialLoadAndCheck(self, forceUpload): |
|
54 """ |
|
55 Public method to do the initial check. |
|
56 |
|
57 @param forceUpload flag indicating a forced upload of the files |
|
58 (boolean) |
|
59 """ |
|
60 if not Preferences.getWebBrowser("SyncEnabled"): |
|
61 return |
|
62 |
|
63 self.__forceUpload = forceUpload |
|
64 |
|
65 self.__remoteFilesFound = [] |
|
66 |
|
67 # check the existence of the shared directory; create it, if it is |
|
68 # not there |
|
69 if not os.path.exists(Preferences.getWebBrowser("SyncDirectoryPath")): |
|
70 try: |
|
71 os.makedirs(Preferences.getWebBrowser("SyncDirectoryPath")) |
|
72 except OSError as err: |
|
73 self.syncError.emit( |
|
74 self.tr("Error creating the shared directory.\n{0}") |
|
75 .format(str(err))) |
|
76 return |
|
77 |
|
78 self.__initialSync() |
|
79 |
|
80 def __downloadFile(self, type_, fileName, timestamp): |
|
81 """ |
|
82 Private method to downlaod the given file. |
|
83 |
|
84 @param type_ type of the synchronization event (string one |
|
85 of "bookmarks", "history", "passwords", "useragents" or |
|
86 "speeddial") |
|
87 @param fileName name of the file to be downloaded (string) |
|
88 @param timestamp time stamp in seconds of the file to be downloaded |
|
89 (integer) |
|
90 """ |
|
91 self.syncStatus.emit(type_, self._messages[type_]["RemoteExists"]) |
|
92 try: |
|
93 with open( |
|
94 os.path.join(Preferences.getWebBrowser("SyncDirectoryPath"), |
|
95 self._remoteFiles[type_]), "rb" |
|
96 ) as f: |
|
97 data = f.read() |
|
98 except OSError as err: |
|
99 self.syncStatus.emit( |
|
100 type_, |
|
101 self.tr("Cannot read remote file.\n{0}").format(str(err))) |
|
102 self.syncFinished.emit(type_, False, True) |
|
103 return |
|
104 |
|
105 QCoreApplication.processEvents() |
|
106 ok, error = self.writeFile(QByteArray(data), fileName, type_, |
|
107 timestamp) |
|
108 if not ok: |
|
109 self.syncStatus.emit(type_, error) |
|
110 self.syncFinished.emit(type_, ok, True) |
|
111 |
|
112 def __uploadFile(self, type_, fileName): |
|
113 """ |
|
114 Private method to upload the given file. |
|
115 |
|
116 @param type_ type of the synchronization event (string one |
|
117 of "bookmarks", "history", "passwords", "useragents" or |
|
118 "speeddial") |
|
119 @param fileName name of the file to be uploaded (string) |
|
120 """ |
|
121 QCoreApplication.processEvents() |
|
122 data = self.readFile(fileName, type_) |
|
123 if data.isEmpty(): |
|
124 self.syncStatus.emit(type_, self._messages[type_]["LocalMissing"]) |
|
125 self.syncFinished.emit(type_, False, False) |
|
126 return |
|
127 else: |
|
128 try: |
|
129 with open( |
|
130 os.path.join( |
|
131 Preferences.getWebBrowser("SyncDirectoryPath"), |
|
132 self._remoteFiles[type_]), "wb" |
|
133 ) as f: |
|
134 f.write(bytes(data)) |
|
135 f.close() |
|
136 except OSError as err: |
|
137 self.syncStatus.emit( |
|
138 type_, |
|
139 self.tr("Cannot write remote file.\n{0}").format( |
|
140 str(err))) |
|
141 self.syncFinished.emit(type_, False, False) |
|
142 return |
|
143 |
|
144 self.syncFinished.emit(type_, True, False) |
|
145 |
|
146 def __initialSyncFile(self, type_, fileName): |
|
147 """ |
|
148 Private method to do the initial synchronization of the given file. |
|
149 |
|
150 @param type_ type of the synchronization event (string one |
|
151 of "bookmarks", "history", "passwords", "useragents" or |
|
152 "speeddial") |
|
153 @param fileName name of the file to be synchronized (string) |
|
154 """ |
|
155 if ( |
|
156 not self.__forceUpload and |
|
157 os.path.exists(os.path.join( |
|
158 Preferences.getWebBrowser("SyncDirectoryPath"), |
|
159 self._remoteFiles[type_])) and |
|
160 pathlib.Path(fileName).stat().st_mtime <= pathlib.Path( |
|
161 os.path.join(Preferences.getWebBrowser("SyncDirectoryPath"), |
|
162 self._remoteFiles[type_])).stat().st_mtime |
|
163 ): |
|
164 self.__downloadFile( |
|
165 type_, fileName, |
|
166 int(pathlib.Path(os.path.join( |
|
167 Preferences.getWebBrowser("SyncDirectoryPath"), |
|
168 self._remoteFiles[type_])).stat().st_mtime) |
|
169 ) |
|
170 else: |
|
171 if not os.path.exists(os.path.join( |
|
172 Preferences.getWebBrowser("SyncDirectoryPath"), |
|
173 self._remoteFiles[type_])): |
|
174 self.syncStatus.emit( |
|
175 type_, self._messages[type_]["RemoteMissing"]) |
|
176 else: |
|
177 self.syncStatus.emit( |
|
178 type_, self._messages[type_]["LocalNewer"]) |
|
179 self.__uploadFile(type_, fileName) |
|
180 |
|
181 def __initialSync(self): |
|
182 """ |
|
183 Private slot to do the initial synchronization. |
|
184 """ |
|
185 QCoreApplication.processEvents() |
|
186 # Bookmarks |
|
187 if Preferences.getWebBrowser("SyncBookmarks"): |
|
188 self.__initialSyncFile( |
|
189 "bookmarks", |
|
190 WebBrowserWindow.bookmarksManager().getFileName()) |
|
191 |
|
192 QCoreApplication.processEvents() |
|
193 # History |
|
194 if Preferences.getWebBrowser("SyncHistory"): |
|
195 self.__initialSyncFile( |
|
196 "history", |
|
197 WebBrowserWindow.historyManager().getFileName()) |
|
198 |
|
199 QCoreApplication.processEvents() |
|
200 # Passwords |
|
201 if Preferences.getWebBrowser("SyncPasswords"): |
|
202 self.__initialSyncFile( |
|
203 "passwords", |
|
204 WebBrowserWindow.passwordManager().getFileName()) |
|
205 |
|
206 QCoreApplication.processEvents() |
|
207 # User Agent Settings |
|
208 if Preferences.getWebBrowser("SyncUserAgents"): |
|
209 self.__initialSyncFile( |
|
210 "useragents", |
|
211 WebBrowserWindow.userAgentsManager().getFileName()) |
|
212 |
|
213 QCoreApplication.processEvents() |
|
214 # Speed Dial Settings |
|
215 if Preferences.getWebBrowser("SyncSpeedDial"): |
|
216 self.__initialSyncFile( |
|
217 "speeddial", |
|
218 WebBrowserWindow.speedDial().getFileName()) |
|
219 |
|
220 self.__forceUpload = False |
|
221 self.syncMessage.emit(self.tr("Synchronization finished")) |
|
222 |
|
223 def __syncFile(self, type_, fileName): |
|
224 """ |
|
225 Private method to synchronize the given file. |
|
226 |
|
227 @param type_ type of the synchronization event (string one |
|
228 of "bookmarks", "history", "passwords", "useragents" or |
|
229 "speeddial") |
|
230 @param fileName name of the file to be synchronized (string) |
|
231 """ |
|
232 self.syncStatus.emit(type_, self._messages[type_]["Uploading"]) |
|
233 self.__uploadFile(type_, fileName) |
|
234 |
|
235 def syncBookmarks(self): |
|
236 """ |
|
237 Public method to synchronize the bookmarks. |
|
238 """ |
|
239 self.__syncFile( |
|
240 "bookmarks", |
|
241 WebBrowserWindow.bookmarksManager().getFileName()) |
|
242 |
|
243 def syncHistory(self): |
|
244 """ |
|
245 Public method to synchronize the history. |
|
246 """ |
|
247 self.__syncFile( |
|
248 "history", |
|
249 WebBrowserWindow.historyManager().getFileName()) |
|
250 |
|
251 def syncPasswords(self): |
|
252 """ |
|
253 Public method to synchronize the passwords. |
|
254 """ |
|
255 self.__syncFile( |
|
256 "passwords", |
|
257 WebBrowserWindow.passwordManager().getFileName()) |
|
258 |
|
259 def syncUserAgents(self): |
|
260 """ |
|
261 Public method to synchronize the user agents. |
|
262 """ |
|
263 self.__syncFile( |
|
264 "useragents", |
|
265 WebBrowserWindow.userAgentsManager().getFileName()) |
|
266 |
|
267 def syncSpeedDial(self): |
|
268 """ |
|
269 Public method to synchronize the speed dial data. |
|
270 """ |
|
271 self.__syncFile( |
|
272 "speeddial", |
|
273 WebBrowserWindow.speedDial().getFileName()) |
|
274 |
|
275 def shutdown(self): |
|
276 """ |
|
277 Public method to shut down the handler. |
|
278 """ |
|
279 # nothing to do |
|
280 return |