5 |
5 |
6 """ |
6 """ |
7 Module containing a base class for synchronization handlers. |
7 Module containing a base class for synchronization handlers. |
8 """ |
8 """ |
9 |
9 |
10 from PyQt4.QtCore import QObject, pyqtSignal |
10 import os |
|
11 |
|
12 from PyQt4.QtCore import QObject, pyqtSignal, QByteArray |
|
13 |
|
14 import Preferences |
|
15 |
|
16 from Utilities.crypto import dataEncrypt, dataDecrypt |
11 |
17 |
12 |
18 |
13 class SyncHandler(QObject): |
19 class SyncHandler(QObject): |
14 """ |
20 """ |
15 Base class for synchronization handlers. |
21 Base class for synchronization handlers. |
16 |
22 |
17 @signal syncStatus(type_, done, message) emitted to indicate the synchronization |
23 @signal syncStatus(type_, done, message) emitted to indicate the synchronization |
18 status (string one of "bookmarks", "history", "passwords" or "useragents", |
24 status (string one of "bookmarks", "history", "passwords" or "useragents", |
19 boolean, string) |
25 boolean, string) |
20 @signal syncError(message) emitted for a general error with the error message (string) |
26 @signal syncError(message) emitted for a general error with the error message (string) |
|
27 @signal syncMessage(message) emitted to send a message about synchronization (string) |
21 @signal syncFinished(type_, done, download) emitted after a synchronization has |
28 @signal syncFinished(type_, done, download) emitted after a synchronization has |
22 finished (string one of "bookmarks", "history", "passwords" or "useragents", |
29 finished (string one of "bookmarks", "history", "passwords" or "useragents", |
23 boolean, boolean) |
30 boolean, boolean) |
24 """ |
31 """ |
25 syncStatus = pyqtSignal(str, bool, str) |
32 syncStatus = pyqtSignal(str, bool, str) |
26 syncError = pyqtSignal(str) |
33 syncError = pyqtSignal(str) |
|
34 syncMessage = pyqtSignal(str) |
27 syncFinished = pyqtSignal(str, bool, bool) |
35 syncFinished = pyqtSignal(str, bool, bool) |
28 |
36 |
29 def __init__(self, parent=None): |
37 def __init__(self, parent=None): |
30 """ |
38 """ |
31 Constructor |
39 Constructor |
58 """ |
66 """ |
59 Public method to synchronize the user agents. |
67 Public method to synchronize the user agents. |
60 """ |
68 """ |
61 raise NotImplementedError |
69 raise NotImplementedError |
62 |
70 |
63 def initialLoadAndCheck(self): |
71 def initialLoadAndCheck(self, forceUpload): |
64 """ |
72 """ |
65 Public method to do the initial check. |
73 Public method to do the initial check. |
|
74 |
|
75 @keyparam forceUpload flag indicating a forced upload of the files (boolean) |
66 """ |
76 """ |
67 raise NotImplementedError |
77 raise NotImplementedError |
68 |
78 |
69 def shutdown(self): |
79 def shutdown(self): |
70 """ |
80 """ |
71 Public method to shut down the handler. |
81 Public method to shut down the handler. |
72 """ |
82 """ |
73 raise NotImplementedError |
83 raise NotImplementedError |
|
84 |
|
85 def readFile(self, fileName): |
|
86 """ |
|
87 Public method to read a file. |
|
88 |
|
89 If encrypted synchronization is enabled, the data will be encrypted using |
|
90 the relevant encryption key. |
|
91 |
|
92 @param fileName name of the file to be read (string) |
|
93 @return data of the file, optionally encrypted (QByteArray) |
|
94 """ |
|
95 if os.path.exists(fileName): |
|
96 try: |
|
97 inputFile = open(fileName, "rb") |
|
98 data = inputFile.read() |
|
99 inputFile.close() |
|
100 except IOError: |
|
101 return QByteArray() |
|
102 |
|
103 if Preferences.getHelp("SyncEncryptData"): |
|
104 key = Preferences.getHelp("SyncEncryptionKey") |
|
105 if not key: |
|
106 return QByteArray() |
|
107 |
|
108 data, ok = dataEncrypt(data, key) |
|
109 if not ok: |
|
110 return QByteArray() |
|
111 |
|
112 return QByteArray(data) |
|
113 |
|
114 return QByteArray() |
|
115 |
|
116 def writeFile(self, data, fileName): |
|
117 """ |
|
118 Public method to write the data to a file. |
|
119 |
|
120 If encrypted synchronization is enabled, the data will be decrypted using |
|
121 the relevant encryption key. |
|
122 |
|
123 @param data data to be written and optionally decrypted (QByteArray) |
|
124 @param fileName name of the file the data is to be written to (string) |
|
125 @return tuple giving a success flag and an error string (boolean, string) |
|
126 """ |
|
127 data = bytes(data) |
|
128 |
|
129 if Preferences.getHelp("SyncEncryptData"): |
|
130 key = Preferences.getHelp("SyncEncryptionKey") |
|
131 if not key: |
|
132 return False, self.trUtf8("Invalid encryption key given.") |
|
133 |
|
134 data, ok = dataDecrypt(data, key) |
|
135 if not ok: |
|
136 return False, self.trUtf8("Data cannot be decrypted.") |
|
137 |
|
138 try: |
|
139 outputFile = open(fileName, "wb") |
|
140 outputFile.write(data) |
|
141 outputFile.close() |
|
142 return True, "" |
|
143 except IOError as error: |
|
144 return False, str(error) |