1
# -*- coding: utf-8 -*-
2
# Copyright (c) 2009 - Luca Invernizzi <invernizzi.l@gmail.com>
3
# - Paulo Cabido <paulo.cabido@gmail.com> (example file)
5
# This program is free software: you can redistribute it and/or modify it under
6
# the terms of the GNU General Public License as published by the Free Software
7
# Foundation, either version 3 of the License, or (at your option) any later
10
# This program is distributed in the hope that it will be useful, but WITHOUT
11
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
15
# You should have received a copy of the GNU General Public License along with
16
# this program. If not, see <http://www.gnu.org/licenses/>.
20
from threading import Thread
24
from GTG.plugins.rtm_sync.syncEngine import SyncEngine
32
progressbar_percent =0
36
def purgeDialog(self):
39
def showDialog(self, dialog = None):
40
if hasattr(self, 'dialog') and self.dialog != None:
46
self.plugin_path = os.path.dirname(os.path.abspath(__file__))
47
icons_subpath = "icons/hicolor/16x16/rtm_image.png"
48
rtm_image_path = os.path.join(self.plugin_path, icons_subpath)
49
pixbug_rtm_toolbar = gtk.gdk.\
50
pixbuf_new_from_file_at_size(rtm_image_path, 16, 16)
51
pixbug_rtm_menu = gtk.gdk.\
52
pixbuf_new_from_file_at_size(rtm_image_path, 16, 16)
53
rtm_toolbar_image = gtk.Image()
54
rtm_menu_image = gtk.Image()
55
rtm_toolbar_image.set_from_pixbuf(pixbug_rtm_toolbar)
56
rtm_menu_image.set_from_pixbuf(pixbug_rtm_menu)
57
rtm_toolbar_image.show()
59
ui_file = os.path.join(self.plugin_path, "rtm.ui")
60
self.builder = gtk.Builder()
61
self.builder.add_from_file(ui_file)
62
self.callback = self.close_dialog
64
"on_dialogsync_delete_event":
66
"on_btn_ok_s_clicked":
68
"on_dialogtoken_delete_event":
70
"on_btn_ok_t_clicked":
73
self.builder.connect_signals(dic)
75
self.menu_item = gtk.ImageMenuItem(_("Synchronize with RTM"))
76
self.menu_item.connect('activate', self.onTesteMenu)
77
self.menu_item.set_image(rtm_menu_image)
79
self.tb_button = gtk.ToolButton(rtm_toolbar_image)
80
self.tb_button.set_label(_("Synchronize with RTM"))
81
self.tb_button.connect('clicked', self.onTbButton)
82
self.tb_button.set_tooltip_text("Synchronize with Remember the Milk")
83
self.separator = gtk.SeparatorToolItem()
85
def activate(self, plugin_api):
86
self.plugin_api = plugin_api
87
self.sync_engine = SyncEngine(self)
88
# add a menu item to the menu bar
89
plugin_api.add_menu_item(self.menu_item)
90
# saves the separator's index to later remove it
91
plugin_api.add_toolbar_item(self.separator)
92
# add a item(button) to the ToolBar
93
plugin_api.add_toolbar_item(self.tb_button)
95
def deactivate(self, plugin_api):
96
plugin_api.remove_menu_item(self.menu_item)
97
plugin_api.remove_toolbar_item(self.tb_button)
98
plugin_api.remove_toolbar_item(self.separator)
100
#load a dialog with a String
101
def loadDialogToken(self, msg):
102
self.showDialog(self.builder.get_object("dialogtoken"))
103
self.btn_ok = self.builder.get_object("btn_ok_t")
104
self.lbl_dialog = self.builder.get_object("lbl_dialog_t")
105
self.lbl_dialog.set_markup(msg)
106
self.dialog.show_all()
108
def loadDialogSync(self, msg):
109
self.showDialog(self.builder.get_object("dialogsync"))
110
self.btn_ok = self.builder.get_object("btn_ok_s")
111
self.btn_ok.set_sensitive(False)
112
self.lbl_dialog = self.builder.get_object("lbl_dialog_s")
113
self.lbl_dialog.set_text(msg)
114
self.progressbar = self.builder.get_object("progressbar")
115
self.dialog.show_all()
117
def loadDialogNotification(self, msg):
118
self.showDialog(self.builder.get_object("notification"))
119
self.lbl_dialog = self.builder.get_object("lbl_dialog")
120
self.lbl_dialog.set_text(msg)
121
self.dialog.show_all()
123
def close_dialog(self, widget, data=None):
124
self.showDialog(None)
126
def set_progressbar(self):
127
self.progressbar.set_fraction(self.progressbar_percent)
128
if self.progressbar_percent == 1.0:
129
self.btn_ok.set_sensitive(True)
131
def set_status(self):
132
self.lbl_dialog.set_text(self.status)
134
def set_substatus(self):
135
self.progressbar.set_text(self.substatus)
137
def onTesteMenu(self, widget):
138
self.onTbButton(widget)
140
def lauchSynchronization(self):
141
self.loadDialogSync(_("Synchronization started"))
142
self.worker_thread = Thread(target = \
143
self.sync_engine.synchronize).start()
145
def onTbButton(self, widget):
148
def checkLoginBtn(self, widget):
149
self.showDialog(None)
150
self.checkLogin(False)
152
def checkLogin(self, firstime = True):
153
self.firstime = firstime
154
self.loadDialogNotification(_("Trying to access, please stand by..."))
155
Thread(target = self.checkLoginThreadWatchdog).start()
157
def checkLoginThreadWatchdog(self):
158
login_thread = Thread(target = self.checkLoginThread)
161
login_thread.join(10)
164
if login_thread.isAlive():
165
#Can't connect to RTM server
166
gobject.idle_add(self.loginHasFailed)
168
gobject.idle_add(self.checkHasLogon)
170
def loginHasFailed(self):
171
self.callback = self.close_dialog
172
self.loadDialogToken(_("Couldn't connect to Remember The Milk"))
174
def checkLoginThread(self):
176
self.sync_engine.rtmLogin()
180
def checkHasLogon(self):
181
login = self.sync_engine.rtmHasLogon()
182
self.showDialog(None)
184
if not self.firstime:
185
self.callback = self.close_dialog
186
self.loadDialogToken("<b>%s</b> %s" % (
187
_("Authentication failed."), _("Please retry.")))
189
self.callback = self.close_dialog
190
self.callback = self.checkLoginBtn
191
self.loadDialogToken(_("Please authenticate to Remember \
192
The Milk in the browser that is being opened now. \
193
When done, press OK"))
195
self.lauchSynchronization()
197
def onTaskOpened(self, plugin_api):