summaryrefslogtreecommitdiff
path: root/src/anastasis/anastasis-gtk_handle-policy-meta.c
blob: 5ec16ed74b882b6281c95dcb168b002eba5683e7 (plain)
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/*
     This file is part of anastasis-gtk.
     Copyright (C) 2021 Anastasis SARL

     Anastasis 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 3, or (at your
     option) any later version.

     Anastasis 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 Anastasis; see the file COPYING.  If not, write to the
     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     Boston, MA 02110-1301, USA.
*/

/**
 * @file src/anastasis/anastasis-gtk_handle-policy-meta.c
 * @brief Handle right-click context menu in policy review
 * @author Christian Grothoff
 */
#include <gnunet/platform.h>
#include <gnunet/gnunet_util_lib.h>
#include "anastasis-gtk_action.h"
#include "anastasis-gtk_helper.h"
#include "anastasis-gtk_pe.h"
#include <jansson.h>


/**
 * Context for menu callbacks.
 */
struct MenuContext
{
  /**
   * Reference to the row that the user right-clicked.
   */
  GtkTreeRowReference *rr;

};


/**
 * The user selected the 'add policy' menu.
 *
 * @param menuitem the selected menu
 * @param user_data a `struct MenuContext`
 */
static void
add_from_ctx_menu (GtkMenuItem *menuitem,
                   gpointer user_data)
{
  (void) user_data;
  (void) menuitem;
  AG_add_policy ();
}


/**
 * The user selected the 'delete challenge' menu item.
 *
 * @param menuitem the selected menu
 * @param user_data a `struct MenuContext`
 */
static void
delete_challenge_from_ctx_menu (GtkMenuItem *menuitem,
                                gpointer user_data)
{
  struct MenuContext *ctx = user_data;
  GtkTreePath *path = gtk_tree_row_reference_get_path (ctx->rr);
  GtkTreeModel *tm = gtk_tree_row_reference_get_model (ctx->rr);
  GtkTreeIter iter;
  guint pindex;
  gboolean is_challenge;
  guint mindex;

  if (NULL == path)
    return;
  if (! gtk_tree_model_get_iter (tm,
                                 &iter,
                                 path))
  {
    GNUNET_break (0);
    return;
  }
  gtk_tree_path_free (path);
  gtk_tree_model_get (tm,
                      &iter,
                      AG_PRMC_POLICY_INDEX,
                      &pindex,
                      AG_PRMC_IS_CHALLENGE,
                      &is_challenge,
                      AG_PRMC_METHOD_INDEX,
                      &mindex,
                      -1);
  if (! is_challenge)
  {
    GNUNET_break (0);
    return;
  }
  AG_delete_challenge (pindex,
                       mindex);
}


/**
 * The user selected the 'delete policy' menu item.
 *
 * @param menuitem the selected menu
 * @param user_data a `struct MenuContext`
 */
static void
delete_policy_from_ctx_menu (GtkMenuItem *menuitem,
                             gpointer user_data)
{
  struct MenuContext *ctx = user_data;
  GtkTreePath *path = gtk_tree_row_reference_get_path (ctx->rr);
  GtkTreeModel *tm = gtk_tree_row_reference_get_model (ctx->rr);
  GtkTreeIter iter;
  guint pindex;

  if (NULL == path)
    return;
  if (! gtk_tree_model_get_iter (tm,
                                 &iter,
                                 path))
  {
    GNUNET_break (0);
    return;
  }
  gtk_tree_path_free (path);
  gtk_tree_model_get (tm,
                      &iter,
                      AG_PRMC_POLICY_INDEX,
                      &pindex,
                      -1);
  AG_delete_policy (pindex);
}


/**
 * The user selected the 'edit policy' menu.
 *
 * @param menuitem the selected menu
 * @param user_data a `struct MenuContext`
 */
static void
edit_from_ctx_menu (GtkMenuItem *menuitem,
                    gpointer user_data)
{
  struct MenuContext *ctx = user_data;
  GtkTreePath *path = gtk_tree_row_reference_get_path (ctx->rr);
  GtkTreeModel *tm = gtk_tree_row_reference_get_model (ctx->rr);
  GtkTreeIter iter;
  guint pindex;

  if (NULL == path)
    return;
  if (! gtk_tree_model_get_iter (tm,
                                 &iter,
                                 path))
  {
    GNUNET_break (0);
    return;
  }
  gtk_tree_path_free (path);
  gtk_tree_model_get (tm,
                      &iter,
                      AG_PRMC_POLICY_INDEX,
                      &pindex,
                      -1);
  AG_edit_policy (pindex);
}


/**
 * An item was selected from the context menu; destroy the menu shell.
 *
 * @param menushell menu to destroy
 * @param user_data the 'struct MenuContext' of the menu
 */
static void
context_popup_selection_done (GtkMenuShell *menushell,
                              gpointer user_data)
{
  struct MenuContext *ctx = user_data;

  gtk_widget_destroy (GTK_WIDGET (menushell));
  if (NULL != ctx->rr)
  {
    gtk_tree_row_reference_free (ctx->rr);
    ctx->rr = NULL;
  }
  GNUNET_free (ctx);
}


/**
 * Context menu was requested for the policy.  Compute which menu
 * items are applicable and display an appropriate menu.
 *
 * @param tm tree model underlying the tree view where the event happened
 * @param iter location in the tree model selected at the time
 * @return NULL if no menu could be created,
 *         otherwise the a pop-up menu
 */
static GtkMenu *
get_popup (GtkTreeModel *tm,
           GtkTreeIter *iter)
{
  GtkMenu *menu;
  struct MenuContext *ctx;

  ctx = GNUNET_new (struct MenuContext);
  menu = GTK_MENU (gtk_menu_new ());
  if (NULL != iter)
  {
    gboolean is_challenge;

    {
      GtkTreePath *path;

      path = gtk_tree_model_get_path (tm,
                                      iter);
      ctx->rr = gtk_tree_row_reference_new (tm,
                                            path);
      gtk_tree_path_free (path);
    }
    gtk_tree_model_get (tm,
                        iter,
                        AG_PRMC_IS_CHALLENGE,
                        &is_challenge,
                        -1);
    if (! is_challenge)
    {
      GtkWidget *child;

      /* only show 'edit' entry for lines that
         are for an entire policy */
      child = gtk_menu_item_new_with_label (_ ("_Edit policy..."));
      g_signal_connect (child,
                        "activate",
                        G_CALLBACK (&edit_from_ctx_menu),
                        ctx);
      gtk_label_set_use_underline (GTK_LABEL (
                                     gtk_bin_get_child (GTK_BIN (child))),
                                   TRUE);
      gtk_widget_show (child);
      gtk_menu_shell_append (GTK_MENU_SHELL (menu),
                             child);
    }
    {
      GtkWidget *child;
      const char *label;

      if (is_challenge)
        label = _ ("Delete challenge");
      else
        label = _ ("Delete policy");
      child = gtk_menu_item_new_with_label (label);
      g_signal_connect (child,
                        "activate",
                        G_CALLBACK (is_challenge
                                    ? &delete_challenge_from_ctx_menu
                                    : &delete_policy_from_ctx_menu),
                        ctx);
      gtk_label_set_use_underline (GTK_LABEL (
                                     gtk_bin_get_child (GTK_BIN (child))),
                                   TRUE);
      gtk_widget_show (child);
      gtk_menu_shell_append (GTK_MENU_SHELL (menu), child);
    }

    {
      GtkWidget *child;

      /* Insert a separator */
      child = gtk_separator_menu_item_new ();
      gtk_widget_show (child);
      gtk_menu_shell_append (GTK_MENU_SHELL (menu), child);
    }
  }

  {
    GtkWidget *child;

    /* only show 'edit' entry for lines that
       are for an entire policy */
    child = gtk_menu_item_new_with_label (_ ("_Add policy..."));
    g_signal_connect (child,
                      "activate",
                      G_CALLBACK (&add_from_ctx_menu),
                      ctx);
    gtk_label_set_use_underline (GTK_LABEL (
                                   gtk_bin_get_child (GTK_BIN (child))),
                                 TRUE);
    gtk_widget_show (child);
    gtk_menu_shell_append (GTK_MENU_SHELL (menu),
                           child);
  }

  g_signal_connect (menu,
                    "selection-done",
                    G_CALLBACK (&context_popup_selection_done),
                    ctx);
  return menu;
}


/**
 * We got a button-click on the policy treeview.  If it was a
 * right-click, display the context menu.
 *
 * @param widget the GtkTreeView with the search result list
 * @param event the event, we only care about button events
 * @param user_data NULL
 * @return FALSE to propagate the event further,
 *         TRUE to stop the propagation
 */
gboolean
anastasis_gtk_review_policy_treeview_button_press_event_cb (GtkWidget *widget,
                                                            GdkEvent *event,
                                                            gpointer user_data)
{
  GtkTreeView *tv = GTK_TREE_VIEW (widget);
  GdkEventButton *event_button = (GdkEventButton *) event;
  GtkTreeModel *tm;
  GtkTreePath *path;
  GtkTreeIter iter;
  GtkMenu *menu;

  if ((GDK_BUTTON_PRESS != event->type) ||
      (3 != event_button->button))
    return FALSE; /* not a right-click */
  if (! gtk_tree_view_get_path_at_pos (tv,
                                       event_button->x,
                                       event_button->y,
                                       &path,
                                       NULL,
                                       NULL,
                                       NULL))
  {
    menu = get_popup (NULL,
                      NULL);
  }
  else
  {
    tm = gtk_tree_view_get_model (tv);
    if (! gtk_tree_model_get_iter (tm,
                                   &iter,
                                   path))
    {
      /* not sure how we got a path but no iter... */
      GNUNET_break (0);
      return FALSE;
    }
    gtk_tree_path_free (path);

    menu = get_popup (tm,
                      &iter);
  }
  if (NULL == menu)
  {
    GNUNET_break (0);
    return FALSE;
  }
  gtk_menu_popup_at_pointer (menu,
                             event);
  return FALSE;
}