diff options
Diffstat (limited to 'src/peerinfo/gnunet-peerinfo-gtk.c')
-rw-r--r-- | src/peerinfo/gnunet-peerinfo-gtk.c | 212 |
1 files changed, 203 insertions, 9 deletions
diff --git a/src/peerinfo/gnunet-peerinfo-gtk.c b/src/peerinfo/gnunet-peerinfo-gtk.c index 32b0b92c..afa27d70 100644 --- a/src/peerinfo/gnunet-peerinfo-gtk.c +++ b/src/peerinfo/gnunet-peerinfo-gtk.c | |||
@@ -1,11 +1,205 @@ | |||
1 | mc->pnc = GNUNET_PEERINFO_notify (cfg, &GNUNET_GTK_peerinfo_processor, NULL); | 1 | /* |
2 | if (mc->pnc == NULL) | 2 | This file is part of GNUnet. |
3 | (C) 2010 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 2, 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/peerinfo/gnunet-peerinfo-gtk.c | ||
23 | * @brief Main function of gnunet-peerinfo-gtk | ||
24 | * @author Christian Grothoff | ||
25 | */ | ||
26 | #include "gnunet_gtk.h" | ||
27 | #include <gnunet/gnunet_peerinfo_service.h> | ||
28 | |||
29 | /** | ||
30 | * Handle to our main loop. | ||
31 | */ | ||
32 | static struct GNUNET_GTK_MainLoop *ml; | ||
33 | |||
34 | static struct GNUNET_PEERINFO_NotifyContext *pnc; | ||
35 | |||
36 | /** | ||
37 | * Should gnunet-peerinfo-gtk start in tray mode? | ||
38 | */ | ||
39 | static int tray_only; | ||
40 | |||
41 | |||
42 | /** | ||
43 | * Get cfg. | ||
44 | */ | ||
45 | static const struct GNUNET_CONFIGURATION_Handle * | ||
46 | get_configuration () | ||
3 | { | 47 | { |
4 | gtk_widget_hide (GTK_WIDGET | 48 | return GNUNET_GTK_main_loop_get_configuration (ml); |
5 | (gtk_builder_get_object | ||
6 | (mc->builder, "GNUNET_GTK_main_window_peerinfo_treeview"))); | ||
7 | gtk_widget_hide (GTK_WIDGET | ||
8 | (gtk_builder_get_object | ||
9 | (mc->builder, "GNUNET_GTK_main_menu_view_neighbours"))); | ||
10 | /* FIXME: set warning in status bar... */ | ||
11 | } | 49 | } |
50 | |||
51 | /** | ||
52 | * Get an object from the main window. | ||
53 | * | ||
54 | * @param name name of the object | ||
55 | * @return NULL on error | ||
56 | */ | ||
57 | static GObject * | ||
58 | get_object (const char *name) | ||
59 | { | ||
60 | return GNUNET_GTK_main_loop_get_object (ml, name); | ||
61 | } | ||
62 | |||
63 | |||
64 | /** | ||
65 | * Task run on shutdown. | ||
66 | * | ||
67 | * @param cls unused | ||
68 | * @param tc scheduler context, unused | ||
69 | */ | ||
70 | static void | ||
71 | shutdown_task (void *cls, | ||
72 | const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
73 | { | ||
74 | GNUNET_PEERINFO_notify_cancel (pnc); | ||
75 | pnc = NULL; | ||
76 | } | ||
77 | |||
78 | |||
79 | /** | ||
80 | * Function called for peers that we know about. | ||
81 | * | ||
82 | * @param cls closure | ||
83 | * @param peer id of the peer, NULL for last call | ||
84 | * @param hello hello message for the peer (can be NULL) | ||
85 | * @param err_msg NULL if successful, otherwise contains error message | ||
86 | */ | ||
87 | static void | ||
88 | peerinfo_processor (void *cls, | ||
89 | const struct GNUNET_PeerIdentity *peer, | ||
90 | const struct GNUNET_HELLO_Message *hello, | ||
91 | const char *err_msg) | ||
92 | { | ||
93 | GtkListStore *ls; | ||
94 | GtkTreeModel *tm; | ||
95 | GtkTreeIter iter; | ||
96 | int found; | ||
97 | gchar *pid; | ||
98 | const char *npid; | ||
99 | struct GNUNET_CRYPTO_HashAsciiEncoded enc; | ||
100 | |||
101 | GNUNET_CRYPTO_hash_to_enc (&peer->hashPubKey, &enc); | ||
102 | npid = (const char *) &enc; | ||
103 | ls = GTK_LIST_STORE (get_object ("GNUNET_GTK_peer_info_list_store")); | ||
104 | tm = GTK_TREE_MODEL (ls); | ||
105 | found = GNUNET_NO; | ||
106 | if (TRUE == gtk_tree_model_get_iter_first (tm, &iter)) | ||
107 | { | ||
108 | do | ||
109 | { | ||
110 | pid = NULL; | ||
111 | gtk_tree_model_get (tm, &iter, 0, &pid, -1); | ||
112 | if (pid != NULL) | ||
113 | { | ||
114 | if (0 == strcmp (pid, npid)) | ||
115 | { | ||
116 | found = GNUNET_YES; | ||
117 | g_free (pid); | ||
118 | break; | ||
119 | } | ||
120 | } | ||
121 | g_free (pid); | ||
122 | } | ||
123 | while ((found == GNUNET_NO) && | ||
124 | (TRUE == gtk_tree_model_iter_next (tm, &iter))); | ||
125 | } | ||
126 | if (found == GNUNET_NO) | ||
127 | gtk_list_store_append (ls, &iter); | ||
128 | gtk_list_store_set (ls, &iter, 0, npid, 1, 0 /* number of known addresses */ , | ||
129 | 2, "" /* country name */ , | ||
130 | 3, NULL /* country flag */ , | ||
131 | -1); | ||
132 | } | ||
133 | |||
134 | |||
135 | /** | ||
136 | * Callback invoked if the application is supposed to exit. | ||
137 | */ | ||
138 | void | ||
139 | GNUNET_GTK_quit_cb (GObject * object, gpointer user_data) | ||
140 | { | ||
141 | GNUNET_GTK_tray_icon_destroy (); | ||
142 | GNUNET_GTK_main_loop_quit (ml); | ||
143 | GNUNET_SCHEDULER_add_now (&shutdown_task, NULL); | ||
144 | } | ||
145 | |||
146 | |||
147 | /** | ||
148 | * Actual main function run right after GNUnet's scheduler | ||
149 | * is initialized. Initializes up GTK and Glade. | ||
150 | */ | ||
151 | static void | ||
152 | run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
153 | { | ||
154 | GtkWidget *main_window; | ||
155 | ml = cls; | ||
156 | |||
157 | GNUNET_GTK_set_icon_search_path (); | ||
158 | GNUNET_GTK_setup_nls (); | ||
159 | pnc = GNUNET_PEERINFO_notify (get_configuration(), | ||
160 | &peerinfo_processor, NULL); | ||
161 | if (pnc == NULL) | ||
162 | { | ||
163 | fprintf (stderr, _("Failed to initialize communication with peerinfo service!\n")); | ||
164 | exit (1); | ||
165 | } | ||
166 | |||
167 | /* setup main window */ | ||
168 | main_window = | ||
169 | GTK_WIDGET (get_object ("GNUNET_GTK_PEERINFO_main_window")); | ||
170 | gtk_window_maximize (GTK_WINDOW (main_window)); | ||
171 | GNUNET_GTK_tray_icon_create (GTK_WINDOW (main_window), | ||
172 | "gnunet-gtk" /* FIXME: different icon? */ , | ||
173 | "gnunet-peerinfo-gtk"); | ||
174 | |||
175 | /* make GUI visible */ | ||
176 | if (!tray_only) | ||
177 | { | ||
178 | gtk_widget_show (main_window); | ||
179 | gtk_window_present (GTK_WINDOW (main_window)); | ||
180 | } | ||
181 | } | ||
182 | |||
183 | |||
184 | int | ||
185 | main (int argc, char *const *argv) | ||
186 | { | ||
187 | static struct GNUNET_GETOPT_CommandLineOption options[] = { | ||
188 | {'t', "tray", NULL, | ||
189 | gettext_noop ("start in tray mode"), 0, | ||
190 | &GNUNET_GETOPT_set_one, &tray_only}, | ||
191 | GNUNET_GETOPT_OPTION_END | ||
192 | }; | ||
193 | |||
194 | if (GNUNET_OK != | ||
195 | GNUNET_GTK_main_loop_start ("gnunet-peerinfo-gtk", | ||
196 | "GTK GUI for inspecting GNUnet Peers", | ||
197 | argc, | ||
198 | argv, options, | ||
199 | "gnunet_peerinfo_gtk_main_window.glade", &run)) | ||
200 | return 1; | ||
201 | return 0; | ||
202 | } | ||
203 | |||
204 | |||
205 | /* end of gnunet-peerinfo-gtk.c */ | ||