GnuCash  5.6-150-g038405b370+
gnc-flicker-gui.c
1 /*
2  * gnc-flicker-gui.c --
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of
7  * the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, contact:
16  *
17  * Free Software Foundation Voice: +1-617-542-5942
18  * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652
19  * Boston, MA 02110-1301, USA gnu@gnu.org
20  */
21 
29 #include <config.h>
30 
31 #include <gtk/gtk.h>
32 #include <glib/gi18n.h>
33 
34 #include "dialog-utils.h"
35 #include "gnc-flicker-gui.h"
36 #include "gnc-state.h"
37 #include "gnc-ui.h"
38 
39 #define GNC_PREFS_GROUP "dialogs.flicker"
40 #define GNC_STATE_SECTION "Flicker"
41 #define STATE_KEY_BAR_WIDTH "barwidth"
42 #define STATE_KEY_DELAY "delay"
43 
44 #define BAR_WIDTH 44 /* Width of the flicker bars */
45 #define BAR_HEIGHT 200 /* Height of the flicker bars */
46 #define MARGIN 12 /* Distance between the flicker bars */
47 #define DELAY 50 /* Pause between the flickering painting */
48 
49 static guint flicker_data (char const *challenge);
50 static gboolean time_handler (GtkWidget *widget);
51 static void do_marker_drawing (cairo_t *cr);
52 static void draw_bit (cairo_t *cr, _Bool bit, int i);
53 static void do_flicker_drawing (GtkWidget *widget, cairo_t *cr);
54 static void do_flicker_load_state (GtkWidget *dialog);
55 static void do_flicker_store_state (GtkWidget *dialog);
56 static gboolean on_flicker_challenge_draw (GtkWidget *widget, cairo_t *cr,
57  gpointer user_data);
58 static void on_flicker_challenge_map (GtkWidget *widget);
59 static void on_flicker_challenge_destroy (GtkWidget *widget, gpointer user_data);
60 static gboolean on_flicker_marker_draw (GtkWidget *widget, cairo_t *cr,
61  gpointer user_data);
62 static void on_flicker_marker_map (GtkWidget *widget);
63 static void on_spin_barwidth_value_changed (GtkSpinButton *spin, GtkWidget *widget);
64 static void on_spin_delay_value_changed (GtkSpinButton *spin, GtkWidget *widget);
65 static void on_dialog_destroy (GtkWidget *dialog, gpointer user_data);
66 
67 /* structured data for the flicker variables */
68 struct Flickerdraw {
69  const char *challenge;
70  guint challenge_length;
71  guint margin; /* Distance between bars */
72  guint barwidth; /* Bar width */
73  guint barheight; /* Bar height */
74  guint x_barpos; /* x-value for the position of the bar */
75  guint y_barpos; /* y-value for the position of the bar */
76  guint x_drawpos; /* x-value of the first painting position */
77  guint y_drawpos; /* y-value of the first painting position */
78  guint height; /* Height of the drawing area */
79  guint width; /* Width of the drawing area */
80  guint delay; /* Waiting time between frames in milliseconds */
81  guint halfbyteid;
82  guint clock;
83  guint interval;
84  gboolean change_interval;
85 };
86 static struct Flickerdraw flickerdraw;
87 
88 static GncFlickerGui *flickergui = NULL;
89 static _Bool bitarray[510][5];
90 
91 /* this function will return number corresponding 0,1,2..,9,A,B,C,D,E,F */
92 static guint
93 get_num (const char ch)
94 {
95  int num =0;
96  if (ch >= '0' && ch <= '9')
97  num = ch - 0x30;
98  else
99  {
100  switch (ch)
101  {
102  case 'A': case 'a': num = 10; break;
103  case 'B': case 'b': num = 11; break;
104  case 'C': case 'c': num = 12; break;
105  case 'D': case 'd': num = 13; break;
106  case 'E': case 'e': num = 14; break;
107  case 'F': case 'f': num = 15; break;
108  default: num = 0;
109  }
110  }
111  /* The bank challenge has been verified by Aqbanking,
112  * so I assume that no error can occur. */
113  return num;
114 }
115 
116 /* convert the bank challenge into the 5 bits for the flicker data */
117 static guint
118 flicker_data (const char *challenge)
119 {
120  /* bitfield is a clock bit and a 4-bit code with the bits reversed
121  (bit 1 is the least significant and bit 4 the most
122  so 0x1 is 1000 and 0x8 is 0001) */
123  static const _Bool bits[16][5] =
124  {
125  {0, 0, 0, 0, 0}, {0, 1, 0, 0, 0}, {0, 0, 1, 0, 0}, {0, 1, 1, 0, 0},
126  {0, 0, 0, 1, 0}, {0, 1, 0, 1, 0}, {0, 0, 1, 1, 0}, {0, 1, 1, 1, 0},
127  {0, 0, 0, 0, 1}, {0, 1, 0, 0, 1}, {0, 0, 1, 0, 1}, {0, 1, 1, 0, 1},
128  {0, 0, 0, 1, 1}, {0, 1, 0, 1, 1}, {0, 0, 1, 1, 1}, {0, 1, 1, 1, 1}
129  };
130 
131  /* prepend synchronization identifier */
132  char* code = g_strdup_printf ("0FFF%s", challenge);
133  guint challenge_length = strlen (code);
134 
135  /* bitarray has G_N_ELEMENTS (bitarray) rows; each loop iteration writes
136  rows i and i+1, so i+1 must stay within bounds. A bank/FinTS server
137  is untrusted input: reject it growing bitarray writes out of bounds
138  instead of assuming the challenge length is safe. */
139  static const guint max_challenge_length = G_N_ELEMENTS (bitarray) - 1;
140  if (challenge_length > max_challenge_length)
141  {
142  g_warning ("flicker_data: challenge too long (%u bytes), "
143  "truncating to %u to avoid writing past bitarray",
144  challenge_length, max_challenge_length);
145  challenge_length = max_challenge_length;
146  }
147 
148  /* Swap the position of the bits in pairs throughout the bank challenge
149  (low-order nibble first). */
150  for (guint i = 0; i < challenge_length; i += 2)
151  {
152  guint val1 = get_num (code[i]);
153  guint val2 = get_num (code[i+1]);
154 
155  memcpy (&bitarray[i], bits[val2], sizeof(bits[val2]));
156  memcpy (&bitarray[i+1], bits[val1], sizeof(bits[val1]));
157  }
158  g_free (code);
159 
160  return challenge_length;
161 }
162 
163 /* A timer for redrawing the flickering painting, is started here and
164  * called up again when the "Delay" value is changed */
165 static gboolean
166 time_handler (GtkWidget *widget)
167 {
168  /* Change of waiting time */
169  if (flickerdraw.change_interval)
170  {
171  g_source_remove (flickerdraw.interval);
172  flickerdraw.interval = g_timeout_add (flickerdraw.delay,
173  (GSourceFunc) time_handler,
174  (gpointer) widget);
175  flickerdraw.change_interval = FALSE;
176  return FALSE;
177  }
178  gtk_widget_queue_draw (widget);
179 
180  return TRUE;
181 }
182 
183 /* Show the colored triangle as a pointer for the position of the TAN generator */
184 static void
185 do_marker_drawing (cairo_t *cr)
186 {
187  guint pos1;
188  guint pos2;
189 
190  /* Initialize the drawing area to black */
191  cairo_set_source_rgb (cr, 0, 0, 0);
192  cairo_paint (cr);
193 
194  cairo_set_source_rgb (cr, 0.9, 0.1, 0.1);
195  /* draw the left triangle */
196  pos1 = flickerdraw.x_drawpos + flickerdraw.barwidth / 2;
197  cairo_move_to (cr, pos1, 20);
198  cairo_line_to (cr, pos1 + 10, 2);
199  cairo_line_to (cr, pos1 - 10, 2);
200  cairo_close_path (cr);
201  cairo_stroke_preserve (cr);
202  cairo_fill (cr);
203 
204  /* draw the right triangle */
205  pos2 = flickerdraw.x_drawpos + 4 * flickerdraw.margin + 4 * flickerdraw.barwidth +
206  flickerdraw.barwidth / 2;
207  cairo_move_to (cr, pos2, 20);
208  cairo_line_to (cr, pos2 + 10, 2);
209  cairo_line_to (cr, pos2 - 10, 2);
210  cairo_close_path (cr);
211  cairo_stroke_preserve (cr);
212  cairo_fill (cr);
213 }
214 
215 /* draws the 5 flickering bars of the bank data into the drawing area */
216 static void
217 draw_bit (cairo_t *cr, _Bool bit, int i)
218 {
219  if (bit & 1)
220  cairo_set_source_rgb (cr, 1, 1, 1);
221  else
222  cairo_set_source_rgb (cr, 0, 0, 0);
223 
224  flickerdraw.x_barpos = flickerdraw.x_drawpos + i * flickerdraw.margin +
225  i * flickerdraw.barwidth;
226  cairo_rectangle (cr, flickerdraw.x_barpos, flickerdraw.y_barpos,
227  flickerdraw.barwidth, flickerdraw.barheight);
228  cairo_fill (cr);
229 }
230 
231 /* Prepares the drawing area for the flicker graphic. */
232 static void
233 do_flicker_drawing (GtkWidget *widget, cairo_t *cr)
234 {
235  /* Always align the flicker display in the middle of the drawing area */
236  flickerdraw.width = gtk_widget_get_allocated_width (widget);
237 
238  /* Start position of the first bar */
239  flickerdraw.x_drawpos = (flickerdraw.width - 4 * flickerdraw.margin -
240  5 * flickerdraw.barwidth) / 2;
241 
242  /* Initialize the drawing area to black */
243  cairo_set_source_rgb (cr, 0, 0, 0);
244  cairo_paint (cr);
245 
246  /* paint the flicker graphic */
247  bitarray[flickerdraw.halfbyteid][0] = flickerdraw.clock;
248  draw_bit (cr, flickerdraw.clock, 0);
249  for (int i = 1; i <= 4; i++)
250  draw_bit (cr, bitarray[flickerdraw.halfbyteid][i], i);
251 
252  /* Each flicker point is drawn twice. Once with clock = 1 and once with clock = 0 */
253  if (!flickerdraw.clock)
254  {
255  flickerdraw.clock = 1;
256  flickerdraw.halfbyteid++;
257  if (flickerdraw.halfbyteid >= flickerdraw.challenge_length)
258  flickerdraw.halfbyteid = 0;
259  }
260  else if (flickerdraw.clock)
261  flickerdraw.clock = 0;
262 }
263 
264 /* Load the state of the GUI (Size of the Dialog, Value of the Spinbutton) */
265 static void
266 do_flicker_load_state (GtkWidget *dialog)
267 {
268  /* Load the values in the spin button */
269  GKeyFile *state_file = gnc_state_get_current ();
270 
271  if (g_key_file_has_key (state_file, GNC_STATE_SECTION, STATE_KEY_BAR_WIDTH, NULL))
272  flickerdraw.barwidth = g_key_file_get_integer (state_file,
273  GNC_STATE_SECTION,
274  STATE_KEY_BAR_WIDTH, NULL);
275  else
276  flickerdraw.barwidth = BAR_WIDTH;
277 
278  if (g_key_file_has_key (state_file, GNC_STATE_SECTION, STATE_KEY_DELAY, NULL))
279  flickerdraw.delay = g_key_file_get_integer (state_file,
280  GNC_STATE_SECTION,
281  STATE_KEY_DELAY, NULL);
282  else
283  flickerdraw.delay = DELAY;
284 
285  /* Load window size and position */
286  gnc_restore_window_size (GNC_PREFS_GROUP, GTK_WINDOW (dialog),
287  gnc_ui_get_main_window (NULL));
288 }
289 
290 /* Stores the state of the GUI (Size of the Dialog, Value of the Spinbutton) */
291 static void
292 do_flicker_store_state (GtkWidget *dialog)
293 {
294  /* Save the values in the spin button */
295  GKeyFile *state_file = gnc_state_get_current ();
296 
297  if (flickerdraw.barwidth != BAR_WIDTH)
298  g_key_file_set_integer (state_file, GNC_STATE_SECTION,
299  STATE_KEY_BAR_WIDTH, flickerdraw.barwidth);
300  else if (g_key_file_has_key (state_file, GNC_STATE_SECTION,
301  STATE_KEY_BAR_WIDTH, NULL))
302  g_key_file_remove_key (state_file, GNC_STATE_SECTION,
303  STATE_KEY_BAR_WIDTH, NULL);
304 
305  if (flickerdraw.delay != DELAY)
306  g_key_file_set_integer (state_file, GNC_STATE_SECTION,
307  STATE_KEY_DELAY, flickerdraw.delay);
308  else if (g_key_file_has_key (state_file, GNC_STATE_SECTION,
309  STATE_KEY_DELAY, NULL))
310  g_key_file_remove_key (state_file, GNC_STATE_SECTION,
311  STATE_KEY_DELAY, NULL);
312 
313  /* Save window size and position */
314  gnc_save_window_size (GNC_PREFS_GROUP, GTK_WINDOW (dialog));
315 }
316 
317 /* This signal is emitted when the drawing area "flicker challenge" is visible */
318 static void
319 on_flicker_challenge_map (GtkWidget *widget)
320 {
321  flickerdraw.challenge_length = flicker_data (flickerdraw.challenge);
322 
323  /* Set the height of the drawing area */
324  flickerdraw.height = flickerdraw.barheight + 2 * flickerdraw.y_barpos;
325  gtk_widget_set_size_request (widget, -1, flickerdraw.height);
326 
327  /* Call up the time function and start the flicker display */
328  flickerdraw.interval = g_timeout_add (flickerdraw.delay,
329  (GSourceFunc) time_handler,
330  (gpointer) widget);
331 }
332 
333 /* Initialize the drawingarea to black and paint the flickerchallenge */
334 static gboolean
335 on_flicker_challenge_draw (GtkWidget *widget, cairo_t *cr,
336  __attribute__((unused)) gpointer user_data)
337 {
338  do_flicker_drawing (widget, cr);
339 
340  return FALSE;
341 }
342 
343 /* called when the drawing area is destroyed */
344 static void
345 on_flicker_challenge_destroy (GtkWidget *widget,
346  __attribute__((unused)) gpointer user_data)
347 {
348  /* remove the timeout function */
349  g_source_remove (flickerdraw.interval);
350 }
351 
352 /* Initialize the drawing area "flicker marker" in black and draw the marker for
353  * the position of the TAN-Generator */
354 static gboolean
355 on_flicker_marker_draw (__attribute__((unused)) GtkWidget *widget, cairo_t *cr,
356  __attribute__((unused)) gpointer data)
357 {
358  do_marker_drawing (cr);
359 
360  return FALSE;
361 }
362 
363 /* This signal is emitted when the drawing area "flicker marker" is visible */
364 static void
365 on_flicker_marker_map (GtkWidget *widget)
366 {
367  /* Set the height of the drawing area */
368  gtk_widget_set_size_request (widget, -1, flickerdraw.y_barpos);
369 }
370 
371 /* The value for "Field width" has been changed on the spin button and the
372  * flicker display is updated */
373 static void
374 on_spin_barwidth_value_changed (GtkSpinButton *spin, GtkWidget *widget)
375 {
376  flickerdraw.barwidth = gtk_spin_button_get_value_as_int (spin);
377  flickerdraw.x_drawpos = (flickerdraw.width - 4 * flickerdraw.margin -
378  5 * flickerdraw.barwidth) / 2;
379 
380  /* Moving the position triangles */
381  gtk_widget_queue_draw (widget);
382 }
383 
384 /* The value for "waiting time" was changed on the spin button and
385  * the speed of the flickering display is updated */
386 static void
387 on_spin_delay_value_changed (GtkSpinButton *spin, GtkWidget *widget)
388 {
389  flickerdraw.delay = gtk_spin_button_get_value_as_int (spin);
390 
391  flickerdraw.change_interval = TRUE;
392  time_handler (widget);
393 }
394 
395 static void
396 on_dialog_destroy (GtkWidget *dialog, __attribute__((unused)) gpointer user_data)
397 {
398  /* Store window size and initial setting values */
399  do_flicker_store_state (dialog);
400 }
401 
402 /* The widgets for the GUI are prepared and the first parameters are set */
403 void
404 ini_flicker_gui (const char *pChallenge, GncFlickerGui *gui)
405 {
406  /* Establish reference to the dialog widgets created in gnc_gwen_gui */
407  flickergui = gui;
408 
409  /* Load window size and initial setting values */
410  do_flicker_load_state (GTK_WIDGET (flickergui->dialog));
411 
412  /* Initialize application */
413  flickerdraw.barheight = BAR_HEIGHT;
414  flickerdraw.margin = MARGIN;
415  flickerdraw.y_barpos = 20; /* First painting position */
416  flickerdraw.halfbyteid = 0;
417  flickerdraw.clock = 1;
418 
419  flickerdraw.challenge = pChallenge;
420 
421  g_signal_connect (GTK_WINDOW (flickergui->dialog), "destroy",
422  G_CALLBACK (on_dialog_destroy), NULL);
423 
424  g_signal_connect (GTK_WIDGET (flickergui->flicker_challenge), "map",
425  G_CALLBACK (on_flicker_challenge_map), NULL);
426  g_signal_connect (GTK_WIDGET (flickergui->flicker_challenge), "draw",
427  G_CALLBACK (on_flicker_challenge_draw), NULL);
428  g_signal_connect (GTK_WIDGET (flickergui->flicker_challenge), "destroy",
429  G_CALLBACK (on_flicker_challenge_destroy), NULL);
430 
431  g_signal_connect (GTK_WIDGET (flickergui->flicker_marker), "map",
432  G_CALLBACK (on_flicker_marker_map), NULL);
433  g_signal_connect (GTK_WIDGET (flickergui->flicker_marker), "draw",
434  G_CALLBACK (on_flicker_marker_draw), NULL);
435 
436  flickergui->adj_barwidth = gtk_adjustment_new (0.0, 10.0, 80.0, 1.0, 10.0, 0.0);
437  gtk_spin_button_set_adjustment (flickergui->spin_barwidth, flickergui->adj_barwidth);
438  gtk_spin_button_set_value (GTK_SPIN_BUTTON (flickergui->spin_barwidth),
439  flickerdraw.barwidth);
440  g_signal_connect (GTK_WIDGET (flickergui->spin_barwidth), "value-changed",
441  G_CALLBACK (on_spin_barwidth_value_changed),
442  flickergui->flicker_marker);
443  gtk_widget_set_focus_on_click (GTK_WIDGET (flickergui->spin_barwidth), FALSE);
444 
445  flickergui->adj_delay = gtk_adjustment_new (0.0, 10.0, 100.0, 10.0, 10.0, 0.0);
446  gtk_spin_button_set_adjustment (flickergui->spin_delay, flickergui->adj_delay);
447  gtk_spin_button_set_value (GTK_SPIN_BUTTON (flickergui->spin_delay),
448  flickerdraw.delay);
449  g_signal_connect (GTK_WIDGET (flickergui->spin_delay), "value-changed",
450  G_CALLBACK (on_spin_delay_value_changed),
451  flickergui->flicker_challenge);
452  gtk_widget_set_focus_on_click (GTK_WIDGET (flickergui->spin_delay), FALSE);
453 
454  gtk_widget_grab_focus (GTK_WIDGET (flickergui->input_entry));
455 }
Functions to load, save and get gui state.
GtkWindow * gnc_ui_get_main_window(GtkWidget *widget)
Get a pointer to the final GncMainWindow widget is rooted in.
GKeyFile * gnc_state_get_current(void)
Returns a pointer to the most recently loaded state.
Definition: gnc-state.c:248
GUI callbacks for Flicker and ChipTAN(optisch)
void ini_flicker_gui(const char *pChallenge, GncFlickerGui *gui)
Initialize the dialog and drawing area.