src/eric7/WebBrowser/Sync/DirectorySyncHandler.py

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

eric ide

mercurial