|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2012 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing the synchronization manager class. |
|
8 """ |
|
9 |
|
10 from PyQt4.QtCore import QObject, pyqtSignal |
|
11 |
|
12 from .FtpSyncHandler import FtpSyncHandler |
|
13 from .SyncAssistantDialog import SyncAssistantDialog |
|
14 |
|
15 import Preferences |
|
16 |
|
17 import Helpviewer.HelpWindow |
|
18 |
|
19 |
|
20 class SyncManager(QObject): |
|
21 """ |
|
22 Class implementing the synchronization manager. |
|
23 |
|
24 @signal syncError(message) emitted for a general error with the error message (string) |
|
25 """ |
|
26 syncError = pyqtSignal(str) |
|
27 |
|
28 def __init__(self, parent=None): |
|
29 """ |
|
30 Constructor |
|
31 |
|
32 @param parent reference to the parent object (QObject) |
|
33 """ |
|
34 super().__init__(parent) |
|
35 |
|
36 self.__handler = None |
|
37 |
|
38 self.loadSettings() |
|
39 |
|
40 def handler(self): |
|
41 """ |
|
42 Public method to get a reference to the sync handler object. |
|
43 |
|
44 @return reference to the sync handler object (SyncHandler) |
|
45 """ |
|
46 return self.__handler |
|
47 |
|
48 def showSyncDialog(self): |
|
49 """ |
|
50 Public method to show the synchronization dialog. |
|
51 """ |
|
52 dlg = SyncAssistantDialog() |
|
53 dlg.exec_() |
|
54 |
|
55 def loadSettings(self): |
|
56 """ |
|
57 Public method to load the settings. |
|
58 """ |
|
59 if self.syncEnabled(): |
|
60 if Preferences.getHelp("SyncType") == 0: |
|
61 if self.__handler is not None: |
|
62 self.__handler.syncError.disconnect(self.__syncError) |
|
63 self.__handler.syncFinished.disconnect(self.__syncFinished) |
|
64 self.__handler = FtpSyncHandler(self) |
|
65 self.__handler.syncError.connect(self.__syncError) |
|
66 self.__handler.syncFinished.connect(self.__syncFinished) |
|
67 |
|
68 self.__handler.initialLoadAndCheck() |
|
69 |
|
70 # connect sync manager to bookmarks manager |
|
71 if Preferences.getHelp("SyncBookmarks"): |
|
72 Helpviewer.HelpWindow.HelpWindow.bookmarksManager().bookmarksSaved\ |
|
73 .connect(self.__syncBookmarks) |
|
74 else: |
|
75 try: |
|
76 Helpviewer.HelpWindow.HelpWindow.bookmarksManager().bookmarksSaved\ |
|
77 .disconnect(self.__syncBookmarks) |
|
78 except TypeError: |
|
79 pass |
|
80 |
|
81 # connect sync manager to history manager |
|
82 if Preferences.getHelp("SyncHistory"): |
|
83 Helpviewer.HelpWindow.HelpWindow.historyManager().historySaved\ |
|
84 .connect(self.__syncHistory) |
|
85 else: |
|
86 try: |
|
87 Helpviewer.HelpWindow.HelpWindow.historyManager().historySaved\ |
|
88 .disconnect(self.__syncHistory) |
|
89 except TypeError: |
|
90 pass |
|
91 |
|
92 # connect sync manager to passwords manager |
|
93 if Preferences.getHelp("SyncPasswords"): |
|
94 Helpviewer.HelpWindow.HelpWindow.passwordManager().passwordsSaved\ |
|
95 .connect(self.__syncPasswords) |
|
96 else: |
|
97 try: |
|
98 Helpviewer.HelpWindow.HelpWindow.passwordManager().passwordsSaved\ |
|
99 .disconnect(self.__syncPasswords) |
|
100 except TypeError: |
|
101 pass |
|
102 |
|
103 # connect sync manager to user agent manager |
|
104 if Preferences.getHelp("SyncUserAgents"): |
|
105 Helpviewer.HelpWindow.HelpWindow.userAgentsManager()\ |
|
106 .userAgentSettingsSaved.connect(self.__syncUserAgents) |
|
107 else: |
|
108 try: |
|
109 Helpviewer.HelpWindow.HelpWindow.userAgentsManager()\ |
|
110 .userAgentSettingsSaved.disconnect(self.__syncUserAgents) |
|
111 except TypeError: |
|
112 pass |
|
113 else: |
|
114 if self.__handler is not None: |
|
115 self.__handler.syncError.disconnect(self.__syncError) |
|
116 self.__handler.syncFinished.disconnect(self.__syncFinished) |
|
117 self.__handler = None |
|
118 |
|
119 try: |
|
120 Helpviewer.HelpWindow.HelpWindow.bookmarksManager().bookmarksSaved\ |
|
121 .disconnect(self.__syncBookmarks) |
|
122 except TypeError: |
|
123 pass |
|
124 try: |
|
125 Helpviewer.HelpWindow.HelpWindow.historyManager().historySaved\ |
|
126 .disconnect(self.__syncHistory) |
|
127 except TypeError: |
|
128 pass |
|
129 try: |
|
130 Helpviewer.HelpWindow.HelpWindow.passwordManager().passwordsSaved\ |
|
131 .disconnect(self.__syncPasswords) |
|
132 except TypeError: |
|
133 pass |
|
134 try: |
|
135 Helpviewer.HelpWindow.HelpWindow.userAgentsManager()\ |
|
136 .userAgentSettingsSaved.disconnect(self.__syncUserAgents) |
|
137 except TypeError: |
|
138 pass |
|
139 |
|
140 def syncEnabled(self): |
|
141 """ |
|
142 Public method to check, if synchronization is enabled. |
|
143 |
|
144 @return flag indicating enabled synchronization |
|
145 """ |
|
146 return Preferences.getHelp("SyncEnabled") and \ |
|
147 Preferences.getHelp("SyncType") > -1 |
|
148 |
|
149 def __syncBookmarks(self): |
|
150 """ |
|
151 Private slot to synchronize the bookmarks. |
|
152 """ |
|
153 if self.__handler is not None: |
|
154 self.__handler.syncBookmarks() |
|
155 |
|
156 def __syncHistory(self): |
|
157 """ |
|
158 Private slot to synchronize the history. |
|
159 """ |
|
160 if self.__handler is not None: |
|
161 self.__handler.syncHistory() |
|
162 |
|
163 def __syncPasswords(self): |
|
164 """ |
|
165 Private slot to synchronize the passwords. |
|
166 """ |
|
167 if self.__handler is not None: |
|
168 self.__handler.syncPasswords() |
|
169 |
|
170 def __syncUserAgents(self): |
|
171 """ |
|
172 Private slot to synchronize the user agent settings. |
|
173 """ |
|
174 if self.__handler is not None: |
|
175 self.__handler.syncUserAgents() |
|
176 |
|
177 def __syncError(self, message): |
|
178 """ |
|
179 Private slot to handle general synchronization issues. |
|
180 |
|
181 @param message error message (string) |
|
182 """ |
|
183 self.syncError.emit(message) |
|
184 |
|
185 def __syncFinished(self, type_, status, download): |
|
186 """ |
|
187 Private slot to handle a finished synchronization event. |
|
188 |
|
189 @param type_ type of the synchronization event (string one |
|
190 of "bookmarks", "history", "passwords" or "useragents") |
|
191 @param status flag indicating success (boolean) |
|
192 @param download flag indicating a download of a file (boolean) |
|
193 """ |
|
194 if status and download: |
|
195 if type_ == "bookmarks": |
|
196 Helpviewer.HelpWindow.HelpWindow.bookmarksManager().reload() |
|
197 elif type_ == "history": |
|
198 Helpviewer.HelpWindow.HelpWindow.historyManager().reload() |
|
199 elif type_ == "passwords": |
|
200 Helpviewer.HelpWindow.HelpWindow.passwordManager().reload() |
|
201 elif type_ == "useragents": |
|
202 Helpviewer.HelpWindow.HelpWindow.userAgentsManager().reload() |
|
203 |
|
204 def close(self): |
|
205 """ |
|
206 Public slot to shut down the synchronization manager. |
|
207 """ |
|
208 if not self.syncEnabled(): |
|
209 return |
|
210 |
|
211 if self.__handler is not None: |
|
212 self.__handler.shutdown() |