diff options
Diffstat (limited to 'src/identity/gnunet-identity-gtk.c')
-rw-r--r-- | src/identity/gnunet-identity-gtk.c | 269 |
1 files changed, 269 insertions, 0 deletions
diff --git a/src/identity/gnunet-identity-gtk.c b/src/identity/gnunet-identity-gtk.c new file mode 100644 index 00000000..7c9f24bd --- /dev/null +++ b/src/identity/gnunet-identity-gtk.c | |||
@@ -0,0 +1,269 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2010-2013 Christian Grothoff (and other contributing authors) | ||
4 | |||
5 | GNUnet is free software; you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published | ||
7 | by the Free Software Foundation; either version 3, or (at your | ||
8 | option) any later version. | ||
9 | |||
10 | GNUnet is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with GNUnet; see the file COPYING. If not, write to the | ||
17 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
18 | Boston, MA 02111-1307, USA. | ||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * @file src/identity/gnunet-identity-gtk.c | ||
23 | * @brief Main function of gnunet-identity-gtk | ||
24 | * @author Christian Grothoff | ||
25 | */ | ||
26 | #include "gnunet_gtk.h" | ||
27 | #include <gnunet/gnunet_identity_service.h> | ||
28 | |||
29 | |||
30 | /** | ||
31 | * Columns in the identity model. | ||
32 | */ | ||
33 | enum IDENTITY_ModelColumns | ||
34 | { | ||
35 | /** | ||
36 | * A gchararray | ||
37 | */ | ||
38 | IDENTITY_MC_NAME = 0, | ||
39 | |||
40 | /** | ||
41 | * A gchararray | ||
42 | */ | ||
43 | IDENTITY_MC_IDENTIFIER = 1, | ||
44 | |||
45 | /** | ||
46 | * A 'struct GNUNET_IDENTIFIER_Ego' | ||
47 | */ | ||
48 | IDENTITY_MC_EGO = 2 | ||
49 | |||
50 | }; | ||
51 | |||
52 | |||
53 | /** | ||
54 | * Handle to our main loop. | ||
55 | */ | ||
56 | static struct GNUNET_GTK_MainLoop *ml; | ||
57 | |||
58 | /** | ||
59 | * Handle to IDENTITY service. | ||
60 | */ | ||
61 | static struct GNUNET_IDENTITY_Handle *identity; | ||
62 | |||
63 | /** | ||
64 | * Should gnunet-identity-gtk start in tray mode? | ||
65 | */ | ||
66 | static int tray_only; | ||
67 | |||
68 | /** | ||
69 | * Main window list store. | ||
70 | */ | ||
71 | static GtkListStore *ls; | ||
72 | |||
73 | |||
74 | /** | ||
75 | * Get cfg. | ||
76 | */ | ||
77 | static const struct GNUNET_CONFIGURATION_Handle * | ||
78 | get_configuration () | ||
79 | { | ||
80 | return GNUNET_GTK_main_loop_get_configuration (ml); | ||
81 | } | ||
82 | |||
83 | |||
84 | /** | ||
85 | * Get an object from the main window. | ||
86 | * | ||
87 | * @param name name of the object | ||
88 | * @return NULL on error | ||
89 | */ | ||
90 | static GObject * | ||
91 | get_object (const char *name) | ||
92 | { | ||
93 | return GNUNET_GTK_main_loop_get_object (ml, name); | ||
94 | } | ||
95 | |||
96 | |||
97 | /** | ||
98 | * Task run on shutdown. | ||
99 | * | ||
100 | * @param cls unused | ||
101 | * @param tc scheduler context, unused | ||
102 | */ | ||
103 | static void | ||
104 | shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
105 | { | ||
106 | if (NULL != identity) | ||
107 | { | ||
108 | GNUNET_IDENTITY_disconnect (identity); | ||
109 | identity = NULL; | ||
110 | } | ||
111 | GNUNET_GTK_tray_icon_destroy (); | ||
112 | GNUNET_GTK_main_loop_quit (ml); | ||
113 | ml = NULL; | ||
114 | } | ||
115 | |||
116 | |||
117 | /** | ||
118 | * Callback invoked if the application is supposed to exit. | ||
119 | * | ||
120 | * @param object | ||
121 | * @param user_data unused | ||
122 | */ | ||
123 | void | ||
124 | GNUNET_GTK_identity_quit_cb (GObject * object, gpointer user_data) | ||
125 | { | ||
126 | GNUNET_SCHEDULER_shutdown (); | ||
127 | } | ||
128 | |||
129 | |||
130 | /** | ||
131 | * Add all updateable entries of the current namespace to the | ||
132 | * tree store. | ||
133 | * | ||
134 | * @param cls our 'struct MainPublishingDialogContext' | ||
135 | * @param ego identity of the namespace to add | ||
136 | * @param ego_ctx where to store context data | ||
137 | * @param name name of the namespace to add | ||
138 | */ | ||
139 | static void | ||
140 | add_ego (void *cls, | ||
141 | struct GNUNET_IDENTITY_Ego *ego, | ||
142 | void **ego_ctx, | ||
143 | const char *name) | ||
144 | { | ||
145 | GtkTreePath *path; | ||
146 | GtkTreeRowReference *rr; | ||
147 | GtkTreeIter iter; | ||
148 | char *id; | ||
149 | struct GNUNET_CRYPTO_EccPublicKey pub; | ||
150 | |||
151 | if (NULL == ego) | ||
152 | return; /* nothing to be done */ | ||
153 | rr = *ego_ctx; | ||
154 | if (NULL == rr) | ||
155 | { | ||
156 | /* insert operation */ | ||
157 | GNUNET_assert (NULL != name); | ||
158 | GNUNET_IDENTITY_ego_get_public_key (ego, &pub); | ||
159 | id = GNUNET_CRYPTO_ecc_public_key_to_string (&pub); | ||
160 | gtk_list_store_insert_with_values (ls, | ||
161 | &iter, G_MAXINT, | ||
162 | IDENTITY_MC_NAME, name, | ||
163 | IDENTITY_MC_IDENTIFIER, id, | ||
164 | IDENTITY_MC_EGO, ego, | ||
165 | -1); | ||
166 | GNUNET_free (id); | ||
167 | path = gtk_tree_model_get_path (GTK_TREE_MODEL (ls), | ||
168 | &iter); | ||
169 | rr = gtk_tree_row_reference_new (GTK_TREE_MODEL (ls), | ||
170 | path); | ||
171 | gtk_tree_path_free (path); | ||
172 | *ego_ctx = rr; | ||
173 | } | ||
174 | else if (NULL == name) | ||
175 | { | ||
176 | /* delete operation */ | ||
177 | path = gtk_tree_row_reference_get_path (rr); | ||
178 | gtk_tree_row_reference_free (rr); | ||
179 | GNUNET_assert (gtk_tree_model_get_iter (GTK_TREE_MODEL (ls), | ||
180 | &iter, path)); | ||
181 | gtk_tree_path_free (path); | ||
182 | gtk_list_store_remove (ls, &iter); | ||
183 | *ego_ctx = NULL; | ||
184 | } | ||
185 | else | ||
186 | { | ||
187 | /* rename operation */ | ||
188 | path = gtk_tree_row_reference_get_path (rr); | ||
189 | GNUNET_assert (gtk_tree_model_get_iter (GTK_TREE_MODEL (ls), | ||
190 | &iter, path)); | ||
191 | gtk_list_store_set (ls, | ||
192 | &iter, G_MAXINT, | ||
193 | IDENTITY_MC_NAME, name, | ||
194 | -1); | ||
195 | gtk_tree_path_free (path); | ||
196 | } | ||
197 | } | ||
198 | |||
199 | |||
200 | /** | ||
201 | * Actual main function run right after GNUnet's scheduler | ||
202 | * is initialized. Initializes up GTK and Glade. | ||
203 | * | ||
204 | * @param cls NULL | ||
205 | * @param tc schedule context | ||
206 | */ | ||
207 | static void | ||
208 | run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
209 | { | ||
210 | GtkWidget *main_window; | ||
211 | |||
212 | ml = cls; | ||
213 | if (GNUNET_OK != GNUNET_GTK_main_loop_build_window (ml, NULL)) | ||
214 | return; | ||
215 | GNUNET_GTK_set_icon_search_path (); | ||
216 | GNUNET_GTK_setup_nls (); | ||
217 | /* setup main window */ | ||
218 | main_window = GTK_WIDGET (get_object ("GNUNET_GTK_identity_window")); | ||
219 | ls = GTK_LIST_STORE (get_object ("GNUNET_GTK_identity_liststore")); | ||
220 | GNUNET_assert (NULL != ls); | ||
221 | gtk_window_maximize (GTK_WINDOW (main_window)); | ||
222 | GNUNET_GTK_tray_icon_create (ml, | ||
223 | GTK_WINDOW (main_window), | ||
224 | "gnunet-gtk" /* FIXME: different icon? */ , | ||
225 | "gnunet-identity-gtk"); | ||
226 | |||
227 | /* make GUI visible */ | ||
228 | if (!tray_only) | ||
229 | { | ||
230 | gtk_widget_show (main_window); | ||
231 | gtk_window_present (GTK_WINDOW (main_window)); | ||
232 | } | ||
233 | identity = GNUNET_IDENTITY_connect (get_configuration (), | ||
234 | &add_ego, | ||
235 | NULL); | ||
236 | GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, | ||
237 | &shutdown_task, NULL); | ||
238 | } | ||
239 | |||
240 | |||
241 | /** | ||
242 | * Main function of gnunet-identity-gtk. | ||
243 | * | ||
244 | * @param argc number of arguments | ||
245 | * @param argv arguments | ||
246 | * @return 0 on success | ||
247 | */ | ||
248 | int | ||
249 | main (int argc, char *const *argv) | ||
250 | { | ||
251 | static struct GNUNET_GETOPT_CommandLineOption options[] = { | ||
252 | {'t', "tray", NULL, | ||
253 | gettext_noop ("start in tray mode"), 0, | ||
254 | &GNUNET_GETOPT_set_one, &tray_only}, | ||
255 | GNUNET_GETOPT_OPTION_END | ||
256 | }; | ||
257 | |||
258 | if (GNUNET_OK != | ||
259 | GNUNET_GTK_main_loop_start ("gnunet-identity-gtk", | ||
260 | "GTK GUI for managing egos", argc, | ||
261 | argv, options, | ||
262 | "gnunet_identity_gtk_main_window.glade", | ||
263 | &run)) | ||
264 | return 1; | ||
265 | return 0; | ||
266 | } | ||
267 | |||
268 | |||
269 | /* end of gnunet-identity-gtk.c */ | ||