aboutsummaryrefslogtreecommitdiff
path: root/src/fs/gnunet-fs-gtk_main-window-meta-data-context-menu.c
blob: bf3421ee72bc1b65418d6d4bea2e6edd19ed7e09 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
     This file is part of GNUnet.
     (C) 2010, 2011, 2012 Christian Grothoff (and other contributing authors)

     GNUnet is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published
     by the Free Software Foundation; either version 2, or (at your
     option) any later version.

     GNUnet is distributed in the hope that it will be useful, but
     WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with GNUnet; see the file COPYING.  If not, write to the
     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
     Boston, MA 02111-1307, USA.
*/

/**
 * @file src/fs/gnunet-fs-gtk_main-window-meta-data-context-menu.c
 * @brief context menu for the 'meta data' tree view in the main window
 * @author Christian Grothoff
 */
#include "gnunet-fs-gtk.h"
#include "gnunet-fs-gtk_download-save-as.h"
#include "gnunet-fs-gtk_event-handler.h"
#include <string.h>



/**
 * Helper function of GNUNET_GTK_FS_metadata_copy_selection_activated
 * which copies the (selected) entries from the tree view to the
 * GList.
 *
 * @param model the tree model with the data
 * @param path unused
 * @param iter position in the model to access
 * @param user_data 'GList**' where we should store the types and values found
 */ 
static void
copy_metadata_to_clipboard (GtkTreeModel * model, GtkTreePath * path,
                            GtkTreeIter * iter, gpointer user_data)
{
  GList **l = user_data;
  gchar *type;
  gchar *value;

  gtk_tree_model_get (model, iter, 2, &type, 3, &value, -1);
  *l = g_list_prepend (*l, type);
  *l = g_list_prepend (*l, value);
}


/**
 * User activated metadata pop up menu "Copy selection" entry.
 *
 * @param menuitem the 'copy selection' menu item
 * @param user_data the GtkBuilder of the main window
 */
void
GNUNET_GTK_FS_metadata_copy_selection_activated (GtkMenuItem * menuitem, 
						 gpointer user_data)
{
  GtkBuilder *builder = GTK_BUILDER (user_data);
  GtkTreeView *tree;
  GtkClipboard *cb;
  GList *pairs;
  GList *pos;
  GList *value;
  GList *type;
  guint total_len;
  gchar *s;
  gchar *p;

  tree =
      GTK_TREE_VIEW (gtk_builder_get_object
                     (builder, "GNUNET_GTK_main_window_metadata_treeview"));
  pairs = NULL;
  gtk_tree_selection_selected_foreach (gtk_tree_view_get_selection (tree),
                                       &copy_metadata_to_clipboard, &pairs);
  if (NULL == pairs)
    return; /* nothing selected */
  total_len = 0;
  pairs = g_list_reverse (pairs);
  for (pos = pairs; NULL != pos; pos = value->next)
  {
    type = pos;
    value = pos->next;
    GNUNET_assert (NULL != value);
    total_len +=
      strlen ((gchar *) type->data) + strlen ((gchar *) value->data) +
      2 /* ": " */  + ((NULL != value->next) ? 1 : 0) /* "\n" */ ;
  }
  GNUNET_assert (total_len > 0);
  total_len++;             /* "\0" */
  s = g_new0 (gchar, total_len);
  if (NULL == s)
  {
    GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "malloc");
    return;
  }
  p = s;
  for (pos = pairs; NULL != pos; pos = value->next)
  {
    type = pos;
    value = pos->next;
    GNUNET_assert (NULL != value);
    p = g_stpcpy (p, (gchar *) type->data);
    p = g_stpcpy (p, ": ");
    p = g_stpcpy (p, (gchar *) value->data);
    if (NULL != value->next)
      p = g_stpcpy (p, "\n");        
  }
  g_list_foreach (pairs, (GFunc) &g_free, NULL);
  g_list_free (pairs);
  cb = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
  gtk_clipboard_set_text (cb, s, -1);
  gtk_clipboard_store (cb);
  g_free (s);  
}


/**
 * Got asked to pop up the context menu in the metadata treeview in
 * the main window.  Do it.
 *
 * @param button which button caused the event (0 for none)
 * @param event_time time of the event (current time or 'event->time')
 * @param user_data the gtk builder of the main window
 */
static void
do_metadata_popup_menu (int button,
			int event_time,
                        GtkBuilder *builder)
{
  GtkMenu *menu;

  menu = GTK_MENU (gtk_builder_get_object (builder, "metadata_popup_menu"));
  gtk_menu_popup (menu, NULL, NULL, NULL, builder, button, event_time);
}


/**
 * Got a button press event on the metadata treeview in the main window.
 * If it was a right click, pop up the context menu.
 *
 * @param widget the tree view widget
 * @param user_data the gtk builder of the main window
 */
gboolean
GNUNET_GTK_main_window_metadata_treeview_button_press_event_cb (GtkWidget *
                                                                widget,
                                                                GdkEventButton *
                                                                event,
                                                                gpointer
                                                                user_data)
{
  GtkBuilder *builder = GTK_BUILDER (user_data);

  /* Ignore double-clicks and triple-clicks */
  if ( (event->button != 3) || (event->type != GDK_BUTTON_PRESS) )
    return FALSE;
  do_metadata_popup_menu (event->button,
			  event->time,
			  builder);
  return TRUE;
}


/**
 * Metadata treeview in the main window got the 'popup-menu' signal.
 * Pop up the menu.
 *
 * @param widget the tree view widget
 * @param user_data the gtk builder of the main window
 * @return TRUE we did it
 */
gboolean
GNUNET_GTK_main_window_metadata_treeview_popup_menu_cb (GtkWidget * widget,
                                                        gpointer user_data)
{
  GtkBuilder *builder = GTK_BUILDER (user_data);

  do_metadata_popup_menu (0 /* no button */,
			  gtk_get_current_event_time (),
			  builder);
  return TRUE;
}

/* end of gnunet-fs-gtk_meta-data-context-menu.c */