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