aboutsummaryrefslogtreecommitdiff
path: root/src/anastasis/anastasis-gtk_autocomplete.c
blob: 69ed302d8157e07647850ec181739c857333e377 (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
/*
     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_autocomplete.c
 * @brief Autocomplete logic for GtkEntry widgets
 * @author Christian Grothoff
 */
#include <gnunet/gnunet_util_lib.h>
#include "anastasis-gtk_attributes.h"
#include "anastasis-gtk_dispatch.h"
#include "anastasis-gtk_helper.h"
#include <jansson.h>


/**
 * Lookup autocompletion string for @a editable widget.
 *
 * @param editable widget to lookup
 * @return NULL if not found
 */
static const char *
lookup_autocomplete (GtkEditable *editable)
{
  json_t *a;
  size_t index;
  json_t *ra = json_object_get (AG_redux_state,
                                "required_attributes");
  if (NULL == ra)
  {
    GNUNET_break (0);
    return NULL;
  }
  json_array_foreach (ra, index, a)
  {
    const char *autocomplete = NULL;
    const char *widget_name = NULL;
    const char *type = NULL;
    char *data_name;
    bool match;
    struct GNUNET_JSON_Specification spec[] = {
      GNUNET_JSON_spec_mark_optional (
        GNUNET_JSON_spec_string ("widget",
                                 &widget_name),
        NULL),
      GNUNET_JSON_spec_mark_optional (
        GNUNET_JSON_spec_string ("autocomplete",
                                 &autocomplete),
        NULL),
      GNUNET_JSON_spec_string ("type",
                               &type),
      GNUNET_JSON_spec_end ()
    };

    if (GNUNET_OK !=
        GNUNET_JSON_parse (a,
                           spec,
                           NULL, NULL))
    {
      GNUNET_break (0);
      continue;
    }
    if ( (NULL == autocomplete) ||
         (NULL == widget_name) )
      continue;
    data_name = AG_expand_name (widget_name,
                                type);
    match = (editable ==
             GTK_EDITABLE (GCG_get_main_window_object (data_name)));
    GNUNET_free (data_name);
    if (match)
      return autocomplete;
  }
  GNUNET_break (0);
  return NULL;
}


/**
 * Function called from an auto-completed entry widget up on upon text
 * deletion.
 *
 * @param editable the widget
 * @param start_pos starting position
 * @param end_pos end position
 * @param user_data unused
 */
void
anastasis_gtk_autocomplete_delete_text (GtkEditable *editable,
                                        int start_pos,
                                        int end_pos,
                                        void *user_data)
{
  const char *ac;
  (void) user_data;

  ac = lookup_autocomplete (editable);
  if (NULL == ac)
    return;
  if (0 == start_pos)
    return;
#if CONFUSING_UX_DESIRED
  if (ac[start_pos] != '?')
  {
    g_signal_stop_emission_by_name (editable,
                                    "delete-text");
    gtk_editable_delete_text (editable,
                              start_pos - 1,
                              end_pos);
  }
#endif
}


/**
 * Function called from an auto-completed widget upon text insertion.
 *
 * @param editable the widget
 * @param new_text inserted text
 * @param new_text_length number of bytes in @a new_text
 * @param position insertion position
 * @param user_data unused
 */
void
anastasis_gtk_autocomplete_insert_text (GtkEditable *editable,
                                        const char *new_text,
                                        int new_text_length,
                                        gpointer position,
                                        void *user_data)
{
  const char *ac;
  gint pos;
  char *replacement;

  (void) user_data;
  ac = lookup_autocomplete (editable);
  if (NULL == ac)
    return;
  pos = gtk_editable_get_position (editable);
  if (strlen (ac) <= pos + new_text_length)
    return;
  if (ac[pos + new_text_length] != '?')
  {
    g_signal_stop_emission_by_name (editable,
                                    "insert-text");
    GNUNET_asprintf (&replacement,
                     "%.*s%c",
                     new_text_length,
                     new_text,
                     ac[pos + new_text_length]);
    gtk_editable_insert_text (editable,
                              replacement,
                              -1,
                              position);
    GNUNET_free (replacement);
  }
}


/**
 * Function called from an auto-completed PIN widget upon text insertion.
 *
 * @param editable the widget
 * @param new_text inserted text
 * @param new_text_length number of bytes in @a new_text
 * @param position insertion position
 * @param user_data unused
 */
void
anastasis_gtk_pin_autocomplete_insert_text (GtkEditable *editable,
                                            const char *new_text,
                                            int new_text_length,
                                            gpointer position,
                                            void *user_data)
{
  /* Use \? to break up trigraphs */
  const char *acn =   "????\?-??\?-???\?-??\?";
  const char *aca = "A-????\?-??\?-???\?-??\?";
  const char *ac;
  gint pos;
  char *replacement;
  gchar *pfx;

  (void) user_data;
  pfx = gtk_editable_get_chars (editable,
                                0,
                                2);
  if (0 == strncasecmp (pfx,
                        "A-",
                        2))
    ac = aca;
  else
    ac = acn;
  g_free (pfx);

  pos = gtk_editable_get_position (editable);
  if (strlen (ac) <= pos + new_text_length)
    return;
  if (ac[pos + new_text_length] != '?')
  {
    g_signal_stop_emission_by_name (editable,
                                    "insert-text");
    GNUNET_asprintf (&replacement,
                     "%.*s%c",
                     new_text_length,
                     new_text,
                     ac[pos + new_text_length]);
    gtk_editable_insert_text (editable,
                              replacement,
                              -1,
                              position);
    GNUNET_free (replacement);
  }
}