aboutsummaryrefslogtreecommitdiff
path: root/src/fs/gnunet-fs-gtk_main-window-meta-data-context-menu.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/fs/gnunet-fs-gtk_main-window-meta-data-context-menu.c')
-rw-r--r--src/fs/gnunet-fs-gtk_main-window-meta-data-context-menu.c193
1 files changed, 193 insertions, 0 deletions
diff --git a/src/fs/gnunet-fs-gtk_main-window-meta-data-context-menu.c b/src/fs/gnunet-fs-gtk_main-window-meta-data-context-menu.c
new file mode 100644
index 00000000..06e58b67
--- /dev/null
+++ b/src/fs/gnunet-fs-gtk_main-window-meta-data-context-menu.c
@@ -0,0 +1,193 @@
1/*
2 This file is part of GNUnet.
3 (C) 2010, 2011, 2012 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/fs/gnunet-fs-gtk_main-window-meta-data-context-menu.c
23 * @brief context menu for the 'meta data' tree view in the main window
24 * @author Christian Grothoff
25 */
26#include "gnunet-fs-gtk.h"
27#include "gnunet-fs-gtk_download-save-as.h"
28#include "gnunet-fs-gtk_event-handler.h"
29#include <string.h>
30
31
32
33/**
34 * Helper function of GNUNET_GTK_FS_metadata_copy_selection_activated
35 * which copies the (selected) entries from the tree view to the
36 * GList.
37 *
38 * @param model the tree model with the data
39 * @param path unused
40 * @param iter position in the model to access
41 * @param user_data 'GList**' where we should store the types and values found
42 */
43static void
44copy_metadata_to_clipboard (GtkTreeModel * model, GtkTreePath * path,
45 GtkTreeIter * iter, gpointer user_data)
46{
47 GList **l = user_data;
48 gchar *type;
49 gchar *value;
50
51 gtk_tree_model_get (model, iter,
52 GNUNET_GTK_FS_MAIN_WINDOW_META_DATA_MC_META_TYPE_STRING,
53 &type,
54 GNUNET_GTK_FS_MAIN_WINDOW_META_DATA_MC_META_VALUE,
55 &value,
56 -1);
57 *l = g_list_prepend (*l, type);
58 *l = g_list_prepend (*l, value);
59}
60
61
62/**
63 * User activated metadata pop up menu "Copy selection" entry.
64 *
65 * @param menuitem the 'copy selection' menu item
66 * @param user_data the GtkBuilder of the main window
67 */
68void
69GNUNET_GTK_FS_metadata_copy_selection_activated (GtkMenuItem * menuitem,
70 gpointer user_data)
71{
72 struct GNUNET_GTK_MainWindowContext *main_ctx = user_data;
73 GtkTreeView *tree;
74 GtkClipboard *cb;
75 GList *pairs;
76 GList *pos;
77 GList *value;
78 GList *type;
79 guint total_len;
80 gchar *s;
81 gchar *p;
82
83 tree = main_ctx->md_treeview;
84 pairs = NULL;
85 gtk_tree_selection_selected_foreach (gtk_tree_view_get_selection (tree),
86 &copy_metadata_to_clipboard, &pairs);
87 if (NULL == pairs)
88 return; /* nothing selected */
89 total_len = 0;
90 pairs = g_list_reverse (pairs);
91 for (pos = pairs; NULL != pos; pos = value->next)
92 {
93 type = pos;
94 value = pos->next;
95 GNUNET_assert (NULL != value);
96 total_len +=
97 strlen ((gchar *) type->data) + strlen ((gchar *) value->data) +
98 2 /* ": " */ + ((NULL != value->next) ? 1 : 0) /* "\n" */ ;
99 }
100 GNUNET_assert (total_len > 0);
101 total_len++; /* "\0" */
102 s = g_new0 (gchar, total_len);
103 if (NULL == s)
104 {
105 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "malloc");
106 return;
107 }
108 p = s;
109 for (pos = pairs; NULL != pos; pos = value->next)
110 {
111 type = pos;
112 value = pos->next;
113 GNUNET_assert (NULL != value);
114 p = g_stpcpy (p, (gchar *) type->data);
115 p = g_stpcpy (p, ": ");
116 p = g_stpcpy (p, (gchar *) value->data);
117 if (NULL != value->next)
118 p = g_stpcpy (p, "\n");
119 }
120 g_list_foreach (pairs, (GFunc) &g_free, NULL);
121 g_list_free (pairs);
122 cb = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
123 gtk_clipboard_set_text (cb, s, -1);
124 gtk_clipboard_store (cb);
125 g_free (s);
126}
127
128
129/**
130 * Got asked to pop up the context menu in the metadata treeview in
131 * the main window. Do it.
132 *
133 * @param button which button caused the event (0 for none)
134 * @param event_time time of the event (current time or 'event->time')
135 * @param user_data the context of the main window
136 */
137static void
138do_metadata_popup_menu (int button,
139 int event_time,
140 gpointer user_data)
141{
142 GtkMenu *menu;
143 struct GNUNET_GTK_MainWindowContext *main_ctx = user_data;
144
145 menu = GTK_MENU (gtk_builder_get_object (main_ctx->builder, "metadata_popup_menu"));
146 gtk_menu_popup (menu, NULL, NULL, NULL, main_ctx, button, event_time);
147}
148
149
150/**
151 * Got a button press event on the metadata treeview in the main window.
152 * If it was a right click, pop up the context menu.
153 *
154 * @param widget the tree view widget
155 * @param user_data the gtk builder of the main window
156 */
157gboolean
158GNUNET_GTK_main_window_metadata_treeview_button_press_event_cb (GtkWidget *
159 widget,
160 GdkEventButton *
161 event,
162 gpointer
163 user_data)
164{
165 /* Ignore double-clicks and triple-clicks */
166 if ( (event->button != 3) || (event->type != GDK_BUTTON_PRESS) )
167 return FALSE;
168 do_metadata_popup_menu (event->button,
169 event->time,
170 user_data);
171 return FALSE;
172}
173
174
175/**
176 * Metadata treeview in the main window got the 'popup-menu' signal.
177 * Pop up the menu.
178 *
179 * @param widget the tree view widget
180 * @param user_data the gtk builder of the main window
181 * @return TRUE we did it
182 */
183gboolean
184GNUNET_GTK_main_window_metadata_treeview_popup_menu_cb (GtkWidget * widget,
185 gpointer user_data)
186{
187 do_metadata_popup_menu (0 /* no button */,
188 gtk_get_current_event_time (),
189 user_data);
190 return TRUE;
191}
192
193/* end of gnunet-fs-gtk_meta-data-context-menu.c */