~ubuntu-branches/ubuntu/raring/mnemosyne/raring

« back to all changes in this revision

Viewing changes to mnemosyne/pyqt_ui/qt_sync_server.py

  • Committer: Package Import Robot
  • Author(s): Julian Taylor
  • Date: 2013-04-18 20:43:08 UTC
  • mfrom: (1.1.11)
  • Revision ID: package-import@ubuntu.com-20130418204308-tiwzzs0n1n79qhyt
Tags: 2.2.1-0ubuntu1
New upstream bugfix release (LP: #1169634)

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
# either the sync server thread or the main thread is doing database
19
19
# operations.
20
20
 
 
21
answer = None
21
22
mutex = QtCore.QMutex()
 
23
dialog_closed = QtCore.QWaitCondition()
22
24
database_released = QtCore.QWaitCondition()
23
25
 
 
26
 
 
27
 
24
28
class ServerThread(QtCore.QThread, SyncServer):
25
29
 
26
30
    """When a sync request comes in, the main thread will release the
49
53
    sync_ended_signal = QtCore.pyqtSignal()
50
54
    information_signal = QtCore.pyqtSignal(QtCore.QString)
51
55
    error_signal = QtCore.pyqtSignal(QtCore.QString)
 
56
    question_signal = QtCore.pyqtSignal(QtCore.QString, QtCore.QString,
 
57
        QtCore.QString, QtCore.QString)
52
58
    set_progress_text_signal = QtCore.pyqtSignal(QtCore.QString)
53
59
    set_progress_range_signal = QtCore.pyqtSignal(int)
54
60
    set_progress_update_interval_signal = QtCore.pyqtSignal(int)
82
88
            self.terminate_all_sessions() # Does its own locking.
83
89
            self.database().release_connection()
84
90
            self.server_has_connection = False
 
91
            if self in self.component_manager.components[None]["main_widget"]:
 
92
                self.component_manager.components[None]["main_widget"].pop()
85
93
        database_released.wakeAll()
86
94
 
87
95
    def load_database(self, database_name):
88
96
        mutex.lock()
 
97
        # Libmnemosyne itself could also generate dialog messages, so
 
98
        # we temporarily override the main_widget with the threaded
 
99
        # routines in this class.
 
100
        self.component_manager.components[None]["main_widget"].append(self)
89
101
        self.sync_started_signal.emit()
90
102
        if not self.server_has_connection:
91
103
            database_released.wait(mutex)
100
112
            self.database().release_connection()
101
113
            self.server_has_connection = False
102
114
            database_released.wakeAll()
 
115
        if self in self.component_manager.components[None]["main_widget"]:
 
116
            self.component_manager.components[None]["main_widget"].pop()
103
117
        mutex.unlock()
104
118
        self.sync_ended_signal.emit()
105
119
 
111
125
        self.server_has_connection = True
112
126
        mutex.unlock()
113
127
 
114
 
    def show_information(self, information):
115
 
        self.information_signal.emit(information)
 
128
    def show_information(self, message):
 
129
        global answer
 
130
        mutex.lock()
 
131
        answer = None
 
132
        self.information_signal.emit(message)
 
133
        if not answer:
 
134
            dialog_closed.wait(mutex)
 
135
        mutex.unlock()
116
136
 
117
137
    def show_error(self, error):
 
138
        global answer
 
139
        mutex.lock()
 
140
        answer = None
118
141
        self.error_signal.emit(error)
 
142
        if not answer:
 
143
            dialog_closed.wait(mutex)
 
144
        mutex.unlock()
 
145
 
 
146
    def show_question(self, question, option0, option1, option2):
 
147
        global answer
 
148
        mutex.lock()
 
149
        answer = None
 
150
        self.question_signal.emit(question, option0, option1, option2)
 
151
        if not answer:
 
152
            dialog_closed.wait(mutex)
 
153
        mutex.unlock()
 
154
        return answer
119
155
 
120
156
    def set_progress_text(self, text):
121
157
        self.set_progress_text_signal.emit(text)
148
184
        Component.__init__(self, component_manager)
149
185
        QtCore.QObject.__init__(self)
150
186
        self.thread = None
 
187
        # Since we will overwrite the true main widget in the thread, we need
 
188
        # to save it here.
 
189
        self.true_main_widget = self.main_widget()
151
190
 
152
191
    def activate(self):
153
192
        if self.config()["run_sync_server"]:
176
215
            self.thread.sync_ended_signal.connect(\
177
216
                self.load_database)
178
217
            self.thread.information_signal.connect(\
179
 
                self.main_widget().show_information)
 
218
                self.threaded_show_information)
180
219
            self.thread.error_signal.connect(\
181
 
                self.main_widget().show_error)
 
220
                self.threaded_show_error)
 
221
            self.thread.question_signal.connect(\
 
222
                self.threaded_show_question)
182
223
            self.thread.set_progress_text_signal.connect(\
183
 
                self.main_widget().set_progress_text)
 
224
                self.true_main_widget.set_progress_text)
184
225
            self.thread.set_progress_range_signal.connect(\
185
 
                self.main_widget().set_progress_range)
 
226
                self.true_main_widget.set_progress_range)
186
227
            self.thread.set_progress_update_interval_signal.connect(\
187
 
                self.main_widget().set_progress_update_interval)
 
228
                self.true_main_widget.set_progress_update_interval)
188
229
            self.thread.increase_progress_signal.connect(\
189
 
                self.main_widget().increase_progress)
 
230
                self.true_main_widget.increase_progress)
190
231
            self.thread.set_progress_value_signal.connect(\
191
 
                self.main_widget().set_progress_value)
 
232
                self.true_main_widget.set_progress_value)
192
233
            self.thread.close_progress_signal.connect(\
193
 
                self.main_widget().close_progress)
 
234
                self.true_main_widget.close_progress)
194
235
            self.thread.start()
195
236
 
196
237
    def unload_database(self):
247
288
        self.thread.wait()
248
289
        self.thread = None
249
290
 
250
 
 
 
291
    def threaded_show_information(self, message):
 
292
        global answer
 
293
        mutex.lock()
 
294
        self.true_main_widget.show_information(message)
 
295
        answer = True
 
296
        dialog_closed.wakeAll()
 
297
        mutex.unlock()
 
298
 
 
299
    def threaded_show_error(self, error):
 
300
        global answer
 
301
        mutex.lock()
 
302
        self.true_main_widget.show_error(error)
 
303
        answer = True
 
304
        dialog_closed.wakeAll()
 
305
        mutex.unlock()
 
306
 
 
307
    def threaded_show_question(self, question, option0, option1, option2):
 
308
        global answer
 
309
        mutex.lock()
 
310
        answer = self.true_main_widget.show_question(question, option0,
 
311
            option1, option2)
 
312
        dialog_closed.wakeAll()
 
313
        mutex.unlock()