Some changelist stuff
[doneit] / src / rtm_view.py
1 """
2 @todo Remove blocking operations from UI thread
3 """
4
5 import webbrowser
6 import base64
7
8 import common_view
9
10 import gtk_toolbox
11 import rtm_backend
12 import cache_backend
13 import rtm_api
14
15
16 def get_token(username, apiKey, secret):
17         token = None
18         rtm = rtm_api.RTMapi(username, apiKey, secret, token)
19
20         authURL = rtm.getAuthURL()
21         webbrowser.open(authURL)
22         mb = gtk_toolbox.MessageBox2("You need to authorize DoneIt with\nRemember The Milk.\nClick OK after you authorize.")
23         mb.run()
24
25         token = rtm.getToken()
26         return token
27
28
29 def get_credentials(credentialsDialog):
30         # @todo Pass in parent window
31         username, password = credentialsDialog.request_credentials()
32         token = get_token(username, rtm_backend.RtmBackend.API_KEY, rtm_backend.RtmBackend.SECRET)
33         return username, password, token
34
35
36 class RtmView(common_view.CommonView):
37
38         def __init__(self, widgetTree, errorDisplay):
39                 super(RtmView, self).__init__(widgetTree, errorDisplay)
40                 self._credentials = "", "", ""
41                 self._credentialsDialog = gtk_toolbox.LoginWindow(widgetTree)
42
43         @staticmethod
44         def name():
45                 return "Remember The Milk"
46
47         def load_settings(self, config):
48                 """
49                 @note Thread Agnostic
50                 """
51                 blobs = (
52                         config.get(self.name(), "bin_blob_%i" % i)
53                         for i in xrange(len(self._credentials))
54                 )
55                 creds = (
56                         base64.b64decode(blob)
57                         for blob in blobs
58                 )
59                 self._credentials = tuple(creds)
60
61         def save_settings(self, config):
62                 """
63                 @note Thread Agnostic
64                 """
65                 config.add_section(self.name())
66                 for i, value in enumerate(self._credentials):
67                         blob = base64.b64encode(value)
68                         config.set(self.name(), "bin_blob_%i" % i, blob)
69
70         def login(self):
71                 """
72                 @note UI Thread
73                 """
74                 if self._manager is not None:
75                         return
76
77                 credentials = self._credentials
78                 while True:
79                         try:
80                                 self._manager = rtm_backend.RtmBackend(*credentials)
81                                 self._manager = cache_backend.LazyCacheBackend(self._manager)
82                                 self._credentials = credentials
83                                 return # Login succeeded
84                         except rtm_api.AuthStateMachine.NoData:
85                                 # Login failed, grab new credentials
86                                 credentials = get_credentials(self._credentialsDialog)
87
88         def logout(self):
89                 """
90                 @note Thread Agnostic
91                 """
92                 self._credentials = "", "", ""
93                 self._manager = None