Add description to the previously committed patch
[debian/pino.git] / src / edit_account.vala
1 /* edit_account.vala
2  *
3  * Copyright (C) 2009-2010  troorl
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  *
18  * Author:
19  *      troorl <troorl@gmail.com>
20  */
21
22 using Gtk;
23 using Auth;
24
25 public class EditAccount : Dialog {
26         
27         private Window parent;
28         public Account acc;
29         
30         public Entry login;
31         public Entry password;
32         public ComboBox service;
33         public Entry proxy;
34         
35         private bool edit_mode;
36         public bool ok = false;
37         
38         //private Regex re_proxy;
39         
40         public EditAccount(Window _parent) {
41                 acc = new Account();
42                 parent = _parent;
43                 set_transient_for(parent);
44                 
45                 //re_proxy = new Regex();
46                 
47                 edit_mode = false;
48                 gui_setup();
49                 
50                 show_all();
51         }
52         
53         public EditAccount.with_acc(Window _parent, Account _acc) {
54                 parent = _parent;
55                 acc = _acc;
56                 set_transient_for(parent);
57                 
58                 edit_mode = true;
59                 gui_setup();
60                 
61                 show_all();
62                 data_setup();
63         }
64         
65         private void gui_setup() {
66                 modal = true;
67                 has_separator = false;
68                 
69                 if(edit_mode)
70                         set_title(_("Edit account"));
71                 else
72                         set_title(_("Create new account"));
73                 
74                 var login_label = new Label(_("Login"));
75                 login = new Entry();
76                 login.key_press_event.connect(on_enter);
77                 
78                 var password_label = new Label(_("Password"));
79                 password = new Entry();
80                 password.key_press_event.connect(on_enter);
81                 password.visible = 0;
82                 
83                 var service_label = new Label(_("Service"));
84                 service = new ComboBox.text();
85                 service.append_text("twitter.com"); //0
86                 service.append_text("identi.ca"); //1
87                 service.append_text("other"); //2
88                 service.changed.connect(() => {
89                         if(service.active == 2) //other
90                                 proxy.set_sensitive(true);
91                         else
92                                 proxy.set_sensitive(false);
93                 });
94                 
95                 service.set_active(0);
96                 
97                 var proxy_label = new Label(_("API proxy or other service"));
98                 var help_label = new Label("");
99                 help_label.set_markup("<small>(http://example.com/api/)</small>");
100                 proxy = new Entry();
101                 proxy.set_sensitive(false);
102                 proxy.key_press_event.connect(on_enter);
103                 var vp = new VBox(false, 0);
104                 vp.pack_start(proxy, false, false, 0);
105                 vp.pack_start(help_label, false, false, 0);
106                 var lp = new VBox(false, 0);
107                 lp.pack_start(proxy_label, false, false, 0);
108                 
109                 HigTable table = new HigTable(_("Account"));
110                 table.add_two_widgets(login_label, login);
111                 table.add_two_widgets(password_label, password);
112                 table.add_two_widgets(service_label, service);
113                 table.add_two_widgets(lp, vp);
114                 
115                 vbox.pack_start(table, true, true, 10);
116                 
117                 //action buttons
118                 add_button(STOCK_CANCEL, ResponseType.CANCEL);
119                 var cb = add_button(STOCK_OK, ResponseType.OK);
120                 
121                 response.connect(response_act);
122                 
123                 set_default(cb);
124         }
125         
126         private void data_setup() {
127                 if(acc == null)
128                         return;
129                 
130                 login.set_text(acc.login);
131                 password.set_text(acc.password);
132                 
133                 switch(acc.service) {
134                         case "twitter.com":
135                                 service.set_active(0);
136                                 break;
137                         
138                         case "identi.ca":
139                                 service.set_active(1);
140                                 break;
141                         
142                         case "other":
143                                 service.set_active(2);
144                                 break;
145                 }
146                 
147                 proxy.set_text(acc.proxy);
148         }
149         
150         /* when user pressed Enter key */
151         private bool on_enter(Gdk.EventKey event) {
152                 if(event.hardware_keycode == 36) {
153                         response_act(ResponseType.OK);
154                         return true;
155                 } else
156                         return false;
157         }
158         
159         private string service_from_box(int index) {
160                 switch(index) {
161                         case 0:
162                                 return "twitter.com";
163                                 
164                         case 1:
165                                 return "identi.ca";
166                         
167                         case 2:
168                                 return "other";
169                         
170                         default:
171                                 return "twitter.com";
172                 }
173         }
174         
175         private void response_act(int resp_id) {
176                 switch(resp_id) {
177                         case ResponseType.OK:
178                                 if(login.text.length > 0 && password.text.length > 0) {
179                                         if(service.active == 2) { //validation for proxy url
180                                                 if(proxy.text.length < 10) {
181                                                         var message_dialog = new MessageDialog(parent,
182                                                         Gtk.DialogFlags.DESTROY_WITH_PARENT | Gtk.DialogFlags.MODAL,
183                                                         Gtk.MessageType.INFO, Gtk.ButtonsType.OK, (_("You must enter a proxy address")));
184                 
185                                                         message_dialog.run();
186                                                         message_dialog.destroy();
187                                                         break;
188                                                 }
189                                                 
190                                                 if(proxy.text.substring(proxy.text.length - 1, 1) != "/")
191                                                         proxy.text += "/";
192                                                 
193                                                 bool valid_url = Regex.match_simple("(http|https)://([\\S]+)/",
194                                                         proxy.text);
195                                                 if(!valid_url) {
196                                                         var message_dialog = new MessageDialog(parent,
197                                                         Gtk.DialogFlags.DESTROY_WITH_PARENT | Gtk.DialogFlags.MODAL,
198                                                         Gtk.MessageType.INFO, Gtk.ButtonsType.OK, (_("Proxy address must contain a valid url")));
199                 
200                                                         message_dialog.run();
201                                                         message_dialog.destroy();
202                                                         break;
203                                                 }
204                                         }
205                                         
206                                         acc.login = login.text;
207                                         acc.password = password.text;
208                                         acc.service = service_from_box(service.active);
209                                         acc.proxy = proxy.text;
210                                         
211                                         ok = true;
212                                         close();
213                                 } else {
214                                         var message_dialog = new MessageDialog(parent,
215                                         Gtk.DialogFlags.DESTROY_WITH_PARENT | Gtk.DialogFlags.MODAL,
216                                         Gtk.MessageType.INFO, Gtk.ButtonsType.OK, (_("You must fill in all fields")));
217                 
218                                         message_dialog.run();
219                                         message_dialog.destroy();
220                                 }
221                                 
222                                 break;
223                         
224                         case ResponseType.CANCEL:
225                                 ok = false;
226                                 close();
227                                 break;
228                 }
229         }
230 }