Merge branch 'upstream' into experimental
[debian/pino.git] / src / identica_create_dialog.vala
1 using Gtk;
2 using Rest;
3
4 namespace Identica {
5
6 public class CreateDialog : CreateDialogGeneric {
7         
8         protected ProxyCall call;
9         protected ProxyCallAsyncCallback callback;
10         
11         private Entry login;
12         private Entry password;
13         private Button check_btn;
14         private Spinner cprogress;
15         
16         InfoBar acc_info;
17         
18         public string s_password;
19         
20         public User user;
21         
22         private weak Thread thread = null;
23         
24         public CreateDialog(Window parent) {
25                 base(parent, _("Create Identica account"),  Config.SERVICE_IDENTICA_ICON);
26                 
27                 login = new Entry();
28                 password = new Entry();
29                 
30                 check_btn = new Button();
31                 
32                 HBox pbox = new HBox(false, 0);
33                 Image cimg = new Image.from_stock(Gtk.STOCK_DIALOG_AUTHENTICATION,
34                         IconSize.MENU);
35                 Label check_label = new Label("");
36                 check_label.set_markup(_("<i>%s</i>".printf("Verify credentials")));
37                 cprogress = new Spinner();
38                 pbox.pack_start(cimg, false, false, 0);
39                 pbox.pack_start(check_label, false, false, 0);
40                 pbox.pack_end(cprogress, false, true, 0);
41                 
42                 check_btn.add(pbox);
43                 check_btn.clicked.connect(verify_credentials);
44                 
45                 acc_info = new InfoBar();
46                 acc_img = new Image();
47                 acc_name = new Label(null);
48                 
49                 acc_info.pack_start(acc_img, false, false, 0);
50                 acc_info.pack_start(acc_name, true, true, 5);
51                 acc_info.reorder_child(acc_img, 0);
52                 acc_info.reorder_child(acc_name, 1);
53                 
54                 HigTable auth_table = new HigTable(_("Authentication"));
55                 auth_table.add_two_widgets(new Label(_("Login")), login);
56                 auth_table.add_two_widgets(new Label(_("Password")), password);
57                 auth_table.add_widget_wide(check_btn);
58                 auth_table.add_widget_wide(acc_info);
59                 
60                 vbox.pack_start(auth_table, false, false, 5);
61                 vbox.show_all();
62                 
63                 cprogress.hide();
64                 acc_info.hide();
65                 
66                 Rest.Proxy proxy = new Rest.Proxy("http://identi.ca/api/", false);
67                 callback = get_response;
68                 
69                 call = proxy.new_call();
70                 call.set_function("account/verify_credentials.xml");
71         }
72         
73         private void verify_credentials() {
74                 cprogress.show();
75                 cprogress.start();
76                 check_btn.set_sensitive(false);
77                 
78                 uchar[] b_chars = Utils.string_to_uchar_array("%s:%s".printf(login.get_text(),
79                         password.get_text()));
80                 string http_auth = GLib.Base64.encode(b_chars);
81                 call.remove_header("Authorization");
82                 call.add_header("Authorization", "Basic %s".printf(http_auth));
83                 
84                 call.run_async(callback, this);
85         }
86         
87         private void get_response() {
88                 cprogress.hide();
89                 cprogress.stop();
90                 check_btn.set_sensitive(true);
91                 
92                 if(call.get_status_code() != 200) { //if we got some error
93                         debug(call.get_status_message()); //TODO
94                         return;
95                 }
96                 debug(call.get_payload());
97                 
98                 /*try {
99                         thread = Thread.create(load_userpic, true);
100                 } catch(ThreadError e) {
101                         debug(e.message);
102                         return;
103                 }*/
104                 
105                 user = Twitter.Parser.get_single_user(call.get_payload());
106                 
107                 s_password = password.get_text();
108                 
109                 acc_info.show_all();
110                 acc_name.set_markup("Hello, <b>%s</b>!\nPress 'OK' to add this account".printf(user.name));
111                 
112                 string? path = img_cache.download(user.pic);
113                 if(path != null)
114                         acc_img.set_from_file(path);
115                 
116                 debug("%s, %s", user.name, user.pic);
117                 
118                 ok_btn.set_sensitive(true);
119         }
120         
121         private void* load_userpic() {
122                 string? path = img_cache.download(user.pic);
123                 if(path == null)
124                         return null;
125                 
126                 Idle.add(() => {
127                         acc_img.set_from_file(path);
128                         return false;
129                 });
130                 
131                 return null;
132         }
133 }
134
135 }