Add missing copyright notice to bridge-dialog.vala and exit-node-dialog.vala
[tor-status] / src / exit-node-dialog.vala
1 /* This file is part of status-area-applet-tor.
2  *
3  * Copyright (C) 2010-2011 Philipp Zabel
4  *
5  * status-area-applet-tor is free software: you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as published
7  * by the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * status-area-applet-tor 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 General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with status-area-applet-tor. If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 class ExitNodeDialog : Gtk.Dialog {
20         private const string GCONF_DIR_TOR       = "/apps/maemo/tor";
21         private const string GCONF_KEY_EXITNODES = GCONF_DIR_TOR + "/exit_nodes";
22
23         GConf.Client gconf;
24         Gtk.ListStore list_store;
25
26         /**
27          * Show the exit node configuration dialog
28          */
29         private const int RESPONSE_NEW = 1;
30         public ExitNodeDialog (TorControl.Connection? tor_control = null) {
31                 var content = (Gtk.VBox) get_content_area ();
32                 content.set_size_request (-1, 5*70);
33
34                 set_title (_("Exit nodes"));
35
36                 gconf = GConf.Client.get_default ();
37                 var exit_nodes = new SList<string> ();
38                 try {
39                         exit_nodes = gconf.get_list (GCONF_KEY_EXITNODES, GConf.ValueType.STRING);
40                 } catch (Error e) {
41                         Hildon.Banner.show_information (this, null, "Error loading exit nodes: %s".printf (e.message));
42                 }
43
44                 list_store = new Gtk.ListStore (1, typeof (string));
45                 Gtk.TreeIter iter;
46                 foreach (string exit_node in exit_nodes) {
47                         list_store.append (out iter);
48                         list_store.@set (iter, 0, exit_node);
49                 }
50
51                 var pannable_area = new Hildon.PannableArea ();
52                 var tree_view = new Gtk.TreeView.with_model (list_store);
53                 var renderer = new Gtk.CellRendererText ();
54                 var column = new Gtk.TreeViewColumn.with_attributes ("IP", renderer, "text", 0);
55                 tree_view.append_column (column);
56                 pannable_area.add (tree_view);
57                 content.pack_start (pannable_area, true, true, 0);
58
59                 tree_view.row_activated.connect ((path, column) => {
60                         exit_node_edit_dialog (list_store, path);
61                 });
62
63                 add_button (_("New"), RESPONSE_NEW);
64                 response.connect ((response_id) => {
65                         if (response_id == RESPONSE_NEW) {
66                                 exit_node_edit_dialog (list_store, null);
67                         }
68                 });
69
70                 content.show_all ();
71         }
72
73         /**
74          * Show the exit node edit dialog
75          */
76         private const int RESPONSE_DELETE = 1;
77         private void exit_node_edit_dialog (Gtk.ListStore store, Gtk.TreePath? path) {
78                 var dialog = new Gtk.Dialog ();
79                 var content = (Gtk.VBox) dialog.get_content_area ();
80
81                 if (path == null)
82                         dialog.set_title (_("New exit node"));
83                 else
84                         dialog.set_title (_("Edit exit node"));
85
86                 var size_group = new Gtk.SizeGroup (Gtk.SizeGroupMode.HORIZONTAL);
87
88                 var hbox = new Gtk.HBox (false, Hildon.MARGIN_DOUBLE);
89                 var label = new Gtk.Label (_("Name"));
90                 label.set_alignment (0, 0.5f);
91                 size_group.add_widget (label);
92                 hbox.pack_start (label, false, false, 0);
93                 var name_entry = new Hildon.Entry (Hildon.SizeType.FINGER_HEIGHT);
94                 hbox.pack_start (name_entry, true, true, 0);
95                 content.pack_start (hbox, false, false, 0);
96
97                 var iter = Gtk.TreeIter ();
98                 if (path != null && store.get_iter (out iter, path)) {
99                         string tmp;
100                         store.@get (iter, 0, out tmp);
101                         name_entry.set_text (tmp);
102
103                         dialog.add_button (_("Delete"), RESPONSE_DELETE);
104                 }
105                 dialog.add_button (_("Save"), Gtk.ResponseType.OK);
106                 dialog.response.connect ((response_id) => {
107                         var exit_nodes = new SList<string> ();
108
109                         if (response_id == RESPONSE_DELETE) {
110                                 if (path != null) {
111                                         Gtk.TreeIter iter2;
112                                         store.get_iter (out iter2, path);
113                                         store.remove (iter2);
114                                         string exit_node;
115                                         if (store.get_iter_first (out iter2)) do {
116                                                 store.@get (iter2, 0, out exit_node);
117                                                 exit_nodes.append (exit_node);
118                                         } while (store.iter_next (ref iter2));
119                                         try {
120                                                 gconf.set_list (GCONF_KEY_EXITNODES,
121                                                                 GConf.ValueType.STRING,
122                                                                 exit_nodes);
123                                         } catch (Error e) {
124                                                 Hildon.Banner.show_information (dialog, null,
125                                                                                 "Failed to save exit node list: %s".printf (e.message));
126                                         }
127                                 }
128                                 dialog.destroy ();
129                         }
130                         if (response_id == Gtk.ResponseType.OK) {
131                                 Gtk.TreeIter iter2;
132                                 if (path == null) {
133                                         store.append (out iter2);
134                                 } else {
135                                         store.get_iter (out iter2, path);
136                                 }
137                                 store.@set (iter2, 0, name_entry.get_text ());
138                                 try {
139                                         exit_nodes = gconf.get_list (GCONF_KEY_EXITNODES,
140                                                                      GConf.ValueType.STRING);
141                                 } catch (Error e) {
142                                         Hildon.Banner.show_information (null, null,
143                                                                         "Error loading exit nodes: %s".printf (e.message));
144                                 }
145                                 if (path == null) {
146                                         exit_nodes.append (name_entry.get_text ());
147                                 } else {
148                                         exit_nodes = null;
149                                         string exit_node;
150                                         if (store.get_iter_first (out iter2)) do {
151                                                 store.@get (iter2, 0, out exit_node);
152                                                 exit_nodes.append (exit_node);
153                                         } while (store.iter_next (ref iter2));
154                                 }
155                                 try {
156                                         gconf.set_list (GCONF_KEY_EXITNODES,
157                                                         GConf.ValueType.STRING,
158                                                         exit_nodes);
159                                 } catch (Error e) {
160                                                 Hildon.Banner.show_information (dialog, null,
161                                                                                 "Failed to save exit node list: %s".printf (e.message));
162                                 }
163
164                                 dialog.destroy ();
165                         }
166                 });
167
168                 dialog.show_all ();
169         }
170 }