Add description to the previously committed patch
[debian/pino.git] / src / account_widget.vala
1 /* account_widget.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 AccountWidget : HBox {
26         
27         private TreeView table;
28         private Button add_button;
29         private Button edit_button;
30         private Button delete_button;
31         
32         ListStore model;
33         
34         private Window parent;
35         private Accounts accounts;
36         
37         public AccountWidget(Window _parent, Accounts _accounts) {
38                 parent = _parent;
39                 accounts = _accounts;
40                 
41                 //gui setup
42                 homogeneous = false;
43                 spacing = 4;
44                 
45                 table = new TreeView();
46                 var frame = new Frame(null);
47                 frame.add(table);
48                 
49                 HBox tbl_box = new HBox(true, 0);
50                 tbl_box.pack_start(frame, true, true, 0);
51                 VBox tvbox = new VBox(true, 0);
52                 tvbox.pack_start(tbl_box, true, true, 0);
53                 
54                 add_button = new Button.from_stock("gtk-add");
55                 add_button.clicked.connect(add_event);
56                 edit_button = new Button.from_stock("gtk-edit");
57                 edit_button.clicked.connect(edit_event);
58                 delete_button = new Button.from_stock("gtk-delete");
59                 delete_button.clicked.connect(delete_event);
60                 
61                 VBox btn_box = new VBox(false, 4);
62                 btn_box.pack_start(add_button, false, false, 0);
63                 btn_box.pack_start(edit_button, false, false, 0);
64                 btn_box.pack_start(delete_button, false, false, 0);
65                 
66                 pack_start(tvbox, true, true, 0);
67                 pack_end(btn_box, false, false, 0);
68                 
69                 //list model setup
70                 model = new ListStore(3, typeof(string), typeof(string), typeof(string));
71                 table.set_model(model);
72                 table.get_selection().set_mode(SelectionMode.SINGLE);
73                 select_first();
74                 
75                 table.insert_column_with_attributes(-1, _("Login"), new CellRendererText(), "text", 0);
76                 table.insert_column_with_attributes(-1, _("Service"), new CellRendererText(), "text", 1);
77                 table.insert_column_with_attributes(-1, _("API proxy"), new CellRendererText(), "text", 2);
78                 
79                 table_setup();
80                 
81                 table.cursor_changed.connect(() => {
82                         edit_button.set_sensitive(true);
83                         delete_button.set_sensitive(true);
84                 });
85         }
86         
87         /* select first item in table */
88         private void select_first() {
89                 TreeIter iter;
90                 if(model.get_iter_first(out iter)) {
91                         table.get_selection().select_iter(iter);
92                 } else {
93                         edit_button.set_sensitive(false);
94                         delete_button.set_sensitive(false);
95                 }
96         }
97         
98         /* edit account */
99         private void edit_event() {
100                 TreeIter iter;
101                 TreeModel tmp_model;
102                 table.get_selection().get_selected(out tmp_model, out iter);
103                 
104                 Value login;
105                 Value service;
106                 Value proxy;
107                 
108                 tmp_model.get_value(iter, 0, out login);
109                 tmp_model.get_value(iter, 1, out service);
110                 tmp_model.get_value(iter, 2, out proxy);
111                 
112                 var acc = accounts.get_by_hash((string)login + (string)service + (string)proxy);
113                 
114                 var edit_dialog = new EditAccount.with_acc(parent, acc);
115                 edit_dialog.destroy.connect(() => {
116                         if(edit_dialog.ok) {
117                                 warning(edit_dialog.acc.login);
118                                 model.set_value(iter, 0, acc.login);
119                                 model.set_value(iter, 1, acc.service);
120                                 model.set_value(iter, 2, acc.proxy);
121                                 
122                                 string hash = acc.login + acc.password + acc.proxy;
123                                 
124                                 accounts.changed(hash);
125                                 
126                                 accounts.write();
127                         }
128                 });
129         }
130         
131         /* create new account */
132         private void add_event() {
133                 var edit_dialog = new EditAccount(parent);
134                 edit_dialog.destroy.connect(() => {
135                         if(edit_dialog.ok) { //create new account
136                                 accounts.add_account(edit_dialog.acc);
137                                 accounts.write();
138                                 
139                                 //add entry to the table
140                                 TreeIter iter;
141                                 model.append(out iter);
142                                 model.set(iter, 0, edit_dialog.acc.login, 1, edit_dialog.acc.service);
143                                 table.get_selection().select_iter(iter);
144                                 table.cursor_changed();
145                                 
146                                 //accounts.changed();
147                                 
148                                 if(accounts.accounts.size == 1)
149                                         accounts.active_changed();
150                         }
151                 });
152         }
153         
154         /* delete account */
155         private void delete_event() {
156                 var message_dialog = new MessageDialog(parent,
157                         Gtk.DialogFlags.DESTROY_WITH_PARENT | Gtk.DialogFlags.MODAL,
158                         Gtk.MessageType.QUESTION, Gtk.ButtonsType.YES_NO,
159                         (_("Sure you want to delete this account?")));
160                 
161                 if(message_dialog.run() != ResponseType.YES) {
162                         message_dialog.destroy();
163                         return;
164                 }
165                 
166                 message_dialog.destroy();
167                 
168                 TreeIter iter;
169                 TreeModel tmp_model;
170                 table.get_selection().get_selected(out tmp_model, out iter);
171                 
172                 Value login;
173                 Value service;
174                 Value proxy;
175                 
176                 tmp_model.get_value(iter, 0, out login);
177                 tmp_model.get_value(iter, 1, out service);
178                 tmp_model.get_value(iter, 2, out proxy);
179                 
180                 string hash = (string)login + (string)service + (string)proxy;
181                 var acc = accounts.get_by_hash(hash);
182                 bool was_active = acc.active; //if we delete active account
183                 
184                 accounts.delete_account(hash);
185                 accounts.write();
186                 
187                 model.remove(iter);
188                 
189                 select_first();
190                 
191                 //accounts.changed(hash);
192                 
193                 if(was_active)
194                         accounts.active_changed();
195         }
196         
197         /* insert data to the table */
198         private void table_setup() {
199                 TreeIter iter;
200                 
201                 foreach(Account acc in accounts.accounts) {
202                         model.append(out iter);
203                         model.set(iter, 0, acc.login, 1, acc.service, 2, acc.proxy);
204                 }
205         }
206 }