Helpviewer/Sync/FtpSyncHandler.py

changeset 1695
7b115f986d48
parent 1681
34c774fb57b5
child 1700
40c911b8c0dd
equal deleted inserted replaced
1694:648466a9451b 1695:7b115f986d48
5 5
6 """ 6 """
7 Module implementing a synchronization handler using FTP. 7 Module implementing a synchronization handler using FTP.
8 """ 8 """
9 9
10 from PyQt4.QtCore import pyqtSignal, QUrl, QIODevice, QTime, QThread, QTimer, QBuffer 10 from PyQt4.QtCore import pyqtSignal, QUrl, QIODevice, QTime, QThread, QTimer, QBuffer, \
11 QFileInfo
11 from PyQt4.QtNetwork import QFtp, QNetworkProxyQuery, QNetworkProxy, QNetworkProxyFactory 12 from PyQt4.QtNetwork import QFtp, QNetworkProxyQuery, QNetworkProxy, QNetworkProxyFactory
12 13
13 from .SyncHandler import SyncHandler 14 from .SyncHandler import SyncHandler
14 15
15 import Helpviewer.HelpWindow 16 import Helpviewer.HelpWindow
44 super().__init__(parent) 45 super().__init__(parent)
45 46
46 self.__state = "idle" 47 self.__state = "idle"
47 self.__forceUpload = False 48 self.__forceUpload = False
48 49
49 self.__remoteFiles = { 50 self.__remoteFilesFound = {}
50 "bookmarks": "Bookmarks",
51 "history": "History",
52 "passwords": "Logins",
53 "useragents": "UserAgentSettings"
54 }
55 self.__remoteFilesFound = []
56
57 self.__messages = {
58 "bookmarks": {
59 "RemoteExists": self.trUtf8(
60 "Remote bookmarks file exists! Syncing local copy..."),
61 "RemoteMissing": self.trUtf8(
62 "Remote bookmarks file does NOT exists. Exporting local copy..."),
63 "LocalMissing": self.trUtf8(
64 "Local bookmarks file does NOT exist. Skipping synchronization!"),
65 "Uploading": self.trUtf8("Uploading local bookmarks file..."),
66 },
67 "history": {
68 "RemoteExists": self.trUtf8(
69 "Remote history file exists! Syncing local copy..."),
70 "RemoteMissing": self.trUtf8(
71 "Remote history file does NOT exists. Exporting local copy..."),
72 "LocalMissing": self.trUtf8(
73 "Local history file does NOT exist. Skipping synchronization!"),
74 "Uploading": self.trUtf8("Uploading local history file..."),
75 },
76 "passwords": {
77 "RemoteExists": self.trUtf8(
78 "Remote logins file exists! Syncing local copy..."),
79 "RemoteMissing": self.trUtf8(
80 "Remote logins file does NOT exists. Exporting local copy..."),
81 "LocalMissing": self.trUtf8(
82 "Local logins file does NOT exist. Skipping synchronization!"),
83 "Uploading": self.trUtf8("Uploading local logins file..."),
84 },
85 "useragents": {
86 "RemoteExists": self.trUtf8(
87 "Remote user agent settings file exists! Syncing local copy..."),
88 "RemoteMissing": self.trUtf8(
89 "Remote user agent settings file does NOT exists."
90 " Exporting local copy..."),
91 "LocalMissing": self.trUtf8(
92 "Local user agent settings file does NOT exist."
93 " Skipping synchronization!"),
94 "Uploading": self.trUtf8("Uploading local user agent settings file..."),
95 },
96 }
97 51
98 def initialLoadAndCheck(self, forceUpload): 52 def initialLoadAndCheck(self, forceUpload):
99 """ 53 """
100 Public method to do the initial check. 54 Public method to do the initial check.
101 55
105 return 59 return
106 60
107 self.__state = "initializing" 61 self.__state = "initializing"
108 self.__forceUpload = forceUpload 62 self.__forceUpload = forceUpload
109 63
110 self.__remoteFilesFound = [] 64 self.__remoteFilesFound = {}
111 self.__syncIDs = {} 65 self.__syncIDs = {}
112 66
113 self.__idleTimer = QTimer(self) 67 self.__idleTimer = QTimer(self)
114 self.__idleTimer.setInterval(Preferences.getHelp("SyncFtpIdleTimeout") * 1000) 68 self.__idleTimer.setInterval(Preferences.getHelp("SyncFtpIdleTimeout") * 1000)
115 self.__idleTimer.timeout.connect(self.__idleTimeout) 69 self.__idleTimer.timeout.connect(self.__idleTimeout)
194 else: 148 else:
195 if id in self.__syncIDs: 149 if id in self.__syncIDs:
196 if self.__ftp.currentCommand() == QFtp.Get: 150 if self.__ftp.currentCommand() == QFtp.Get:
197 self.__syncIDs[id][1].close() 151 self.__syncIDs[id][1].close()
198 ok, error = self.writeFile(self.__syncIDs[id][1].buffer(), 152 ok, error = self.writeFile(self.__syncIDs[id][1].buffer(),
199 self.__syncIDs[id][3]) 153 self.__syncIDs[id][3],
154 self.__syncIDs[id][4])
200 if ok: 155 if ok:
201 self.syncStatus.emit(self.__syncIDs[id][0], True, 156 self.syncStatus.emit(self.__syncIDs[id][0], True,
202 self.__messages[self.__syncIDs[id][0]]["RemoteExists"]) 157 self._messages[self.__syncIDs[id][0]]["RemoteExists"])
203 else: 158 else:
204 self.syncStatus.emit(self.__syncIDs[id][0], False, 159 self.syncStatus.emit(self.__syncIDs[id][0], False,
205 error) 160 error)
206 self.syncFinished.emit(self.__syncIDs[id][0], True, 161 self.syncFinished.emit(self.__syncIDs[id][0], True,
207 self.__syncIDs[id][2]) 162 self.__syncIDs[id][2])
223 Private slot called for each entry sent by the FTP list command. 178 Private slot called for each entry sent by the FTP list command.
224 179
225 @param info info about the entry (QUrlInfo) 180 @param info info about the entry (QUrlInfo)
226 """ 181 """
227 if info.isValid() and info.isFile(): 182 if info.isValid() and info.isFile():
228 if info.name() in self.__remoteFiles.values(): 183 if info.name() in self._remoteFiles.values():
229 self.__remoteFilesFound.append(info.name()) 184 self.__remoteFilesFound[info.name()] = info.lastModified()
230 185
231 def __downloadFile(self, type_, fileName): 186 def __downloadFile(self, type_, fileName, timestamp):
232 """ 187 """
233 Private method to downlaod the given file. 188 Private method to downlaod the given file.
234 189
235 @param type_ type of the synchronization event (string one 190 @param type_ type of the synchronization event (string one
236 of "bookmarks", "history", "passwords" or "useragents") 191 of "bookmarks", "history", "passwords" or "useragents")
237 @param fileName name of the file to be downloaded (string) 192 @param fileName name of the file to be downloaded (string)
193 @param timestamp time stamp in seconds of the file to be downloaded (int)
238 """ 194 """
239 buffer = QBuffer(self) 195 buffer = QBuffer(self)
240 buffer.open(QIODevice.WriteOnly) 196 buffer.open(QIODevice.WriteOnly)
241 id = self.__ftp.get(self.__remoteFiles[type_], buffer) 197 id = self.__ftp.get(self._remoteFiles[type_], buffer)
242 self.__syncIDs[id] = (type_, buffer, True, fileName) 198 self.__syncIDs[id] = (type_, buffer, True, fileName, timestamp)
243 199
244 def __uploadFile(self, type_, fileName): 200 def __uploadFile(self, type_, fileName):
245 """ 201 """
246 Private method to upload the given file. 202 Private method to upload the given file.
247 203
250 @param fileName name of the file to be uploaded (string) 206 @param fileName name of the file to be uploaded (string)
251 """ 207 """
252 data = self.readFile(fileName) 208 data = self.readFile(fileName)
253 if data.isEmpty(): 209 if data.isEmpty():
254 self.syncStatus.emit(type_, True, 210 self.syncStatus.emit(type_, True,
255 self.__messages[type_]["LocalMissing"]) 211 self._messages[type_]["LocalMissing"])
256 else: 212 else:
257 id = self.__ftp.put(data, self.__remoteFiles[type_]) 213 id = self.__ftp.put(data, self._remoteFiles[type_])
258 self.__syncIDs[id] = (type_, data, False) 214 self.__syncIDs[id] = (type_, data, False)
259 215
260 def __initialSyncFile(self, type_, fileName): 216 def __initialSyncFile(self, type_, fileName):
261 """ 217 """
262 Private method to do the initial synchronization of the given file. 218 Private method to do the initial synchronization of the given file.
264 @param type_ type of the synchronization event (string one 220 @param type_ type of the synchronization event (string one
265 of "bookmarks", "history", "passwords" or "useragents") 221 of "bookmarks", "history", "passwords" or "useragents")
266 @param fileName name of the file to be synchronized (string) 222 @param fileName name of the file to be synchronized (string)
267 """ 223 """
268 if not self.__forceUpload and \ 224 if not self.__forceUpload and \
269 self.__remoteFiles[type_] in self.__remoteFilesFound: 225 self._remoteFiles[type_] in self.__remoteFilesFound and \
270 self.__downloadFile(type_, fileName) 226 QFileInfo(fileName).lastModified() <= \
227 self.__remoteFilesFound[self._remoteFiles[type_]]:
228 self.__downloadFile(type_, fileName,
229 self.__remoteFilesFound[self._remoteFiles[type_]].toTime_t())
271 else: 230 else:
272 self.syncStatus.emit(type_, True, 231 if self._remoteFiles[type_] not in self.__remoteFilesFound:
273 self.__messages[type_]["RemoteMissing"]) 232 self.syncStatus.emit(type_, True,
233 self._messages[type_]["RemoteMissing"])
234 else:
235 self.syncStatus.emit(type_, True,
236 self._messages[type_]["LocalNewer"])
274 self.__uploadFile(type_, fileName) 237 self.__uploadFile(type_, fileName)
275 238
276 def __initialSync(self): 239 def __initialSync(self):
277 """ 240 """
278 Private slot to do the initial synchronization. 241 Private slot to do the initial synchronization.
309 """ 272 """
310 if self.__state == "initializing": 273 if self.__state == "initializing":
311 return 274 return
312 275
313 self.__state = "uploading" 276 self.__state = "uploading"
314 self.syncStatus.emit(type_, True, self.__messages[type_]["Uploading"]) 277 self.syncStatus.emit(type_, True, self._messages[type_]["Uploading"])
315 self.__uploadFile(type_, fileName) 278 self.__uploadFile(type_, fileName)
316 279
317 def syncBookmarks(self): 280 def syncBookmarks(self):
318 """ 281 """
319 Public method to synchronize the bookmarks. 282 Public method to synchronize the bookmarks.

eric ide

mercurial