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