/*
     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-button.c
 * @brief Handle right-click context menu in policy review
 * @author Christian Grothoff
 */
#include <gnunet/gnunet_util_lib.h>
#include "anastasis-gtk_action.h"
#include "anastasis-gtk_helper.h"
#include "anastasis-gtk_pe.h"
#include "anastasis-gtk_rows.h"
#include <jansson.h>


/**
 * The user pressed a key while the policy list had the focus.  'Delete'
 * removes the selected policy or challenge.
 *
 * @param controller the key controller of the tree view
 * @param keyval the key that was pressed
 * @param keycode the hardware code of that key
 * @param state the modifiers that were held
 * @param user_data NULL
 * @return TRUE if the key was handled here
 */
gboolean
anastasis_gtk_review_policy_treeview_key_pressed_cb (
  GtkEventControllerKey *controller,
  guint keyval,
  guint keycode,
  GdkModifierType state,
  gpointer user_data)
{
  AGRow *row;
  guint pindex;

  (void) controller;
  (void) keycode;
  (void) state;
  (void) user_data;
  if (GDK_KEY_Delete != keyval)
    return FALSE;
  row = AG_selected_row ("anastasis_gtk_review_policy_selection");
  if (NULL == row)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                "Nothing selected, cannot delete\n");
    return FALSE;
  }
  pindex = AG_row_uint (row,
                        "policy_index");
  if (AG_row_bool (row,
                   "is_challenge"))
    AG_delete_challenge (pindex,
                         AG_row_uint (row,
                                      "method_index"));
  else
    AG_delete_policy (pindex);
  return TRUE;
}
