OSDN Git Service

Support setup.py install
[gwit/gwit.git] / gwitlib / setupwizard.py
1 #-*- coding: utf-8 -*-
2
3 '''Setup Wizard
4 '''
5
6 ################################################################################
7 #
8 # Copyright (c) 2010 University of Tsukuba Linux User Group
9 #
10 # This file is part of "gwit".
11 #
12 # "gwit" is free software: you can redistribute it and/or modify
13 # it under the terms of the GNU General Public License as published by
14 # the Free Software Foundation, either version 3 of the License, or
15 # (at your option) any later version.
16 #
17 # "gwit" is distributed in the hope that it will be useful,
18 # but WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 # GNU General Public License for more details.
21 #
22 # You should have received a copy of the GNU General Public License
23 # along with "gwit".  If not, see <http://www.gnu.org/licenses/>.
24 #
25 ################################################################################
26
27
28 import os
29
30 import pygtk
31 pygtk.require('2.0')
32 import gtk
33
34 import twoauth
35
36 class SetupWizard:
37     ok = False
38     keys = ["Q0xJLVQOVvPp0IZysugbug",
39             "6Irwgx5pzZ8RsqGqw3OcC7Ba5pc9wYCH0m1nDj5sc"]
40     
41     def __init__(self):
42         setupglade = os.path.join(
43             os.path.dirname(__file__), "ui/setupwizard.ui")
44         
45         builder = gtk.Builder()
46         self.builder = builder
47         builder.add_from_file(setupglade)
48         builder.connect_signals(self)
49         
50         self.oauth = twoauth.oauth(*self.keys)
51         self.rtoken = self.oauth.request_token()
52         self.authurl = self.oauth.authorize_url(self.rtoken)
53         
54         # Unmask lbutt once clicked
55         lbutt = gtk.LinkButton(self.authurl, "Please Allow This Application")
56         lbutt.connect("clicked", self.show_and_enable_pin)
57         
58         self.get("table1").attach(lbutt, 1, 2, 0, 1)
59     
60     def main(self):
61         self.get("window1").show_all()
62         gtk.main()
63     
64     def close(self, widget):
65         gtk.main_quit()
66
67     def get(self, name):
68         return self.builder.get_object(name)
69
70     def show_and_enable_pin(self, widget):
71         urldlg = gtk.MessageDialog(buttons = gtk.BUTTONS_OK, message_format = self.authurl)
72         urldlg.connect("response", self.show_and_enable_pin_close)
73         urldlg.run()
74         self.get("entry1").set_sensitive(True)
75     
76     def show_and_enable_pin_close(self, dialog, response_id):
77         dialog.destroy()
78         
79     def on_button1_clicked(self, widget):
80         pin = int(self.get("entry1").get_text())
81         
82         try:
83             token = self.oauth.access_token(self.rtoken, pin)
84         except Exception, e:
85             print "[Error] %s" % e
86             return
87         
88         self.keys.append(token["oauth_token"])
89         self.keys.append(token["oauth_token_secret"])
90         
91         self.screen_name = unicode(token["screen_name"])
92         
93         lbl = gtk.Label()
94         lbl.set_markup("<b>%s</b>" % self.screen_name)
95         
96         self.get("table1").attach(lbl, 1, 2, 2, 3)
97         self.get("table1").show_all()
98         
99         self.get("button1").set_sensitive(False)
100         self.get("entry1").set_sensitive(False)
101         self.get("button3").set_sensitive(True)
102     
103     def on_button3_clicked(self, widget):
104         self.ok = True
105         self.get("window1").destroy()
106         gtk.main_quit()
107     
108     def on_entry1_changed(self, widget):
109         pin = widget.get_text()
110         if len(pin) == 7 and pin.isdigit():
111             self.get("button1").set_sensitive(True)
112         else:
113             self.get("button1").set_sensitive(False)