File: test-plugin.c

package info (click to toggle)
mousepad 0.6.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,600 kB
  • sloc: ansic: 17,021; xml: 653; sh: 545; makefile: 405
file content (611 lines) | stat: -rw-r--r-- 18,637 bytes parent folder | download | duplicates (2)
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
/*
 * This program 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 of the License, or (at your option)
 * any later version.
 *
 * This program 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
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "mousepad/mousepad-private.h"
#include "test-plugin.h"

#include "mousepad/mousepad-dialogs.h"
#include "mousepad/mousepad-history.h"
#include "mousepad/mousepad-settings.h"
#include "mousepad/mousepad-util.h"



/* GObject virtual functions */
static void
test_plugin_finalize (GObject *object);

/* MousepadPlugin virtual functions */
static void
test_plugin_enable (MousepadPlugin *mplugin);
static void
test_plugin_disable (MousepadPlugin *mplugin);

/* TestPlugin own functions */
static void
test_plugin_window_actions (GSimpleAction *test_action,
                            GVariant *parameter,
                            gpointer data);

#define LOG_COMMAND(command) g_printerr ("Command: %s: %s\n", G_STRLOC, command);
#define LOG_WARNING(warning) g_printerr ("%s: %s\n", G_STRLOC, warning);



struct _TestPlugin
{
  MousepadPlugin __parent__;

  /* actions opening a blocking or non-blocking dialog */
  GHashTable *dialog_actions;
};

static GApplication *application;

#define PF(str) ("mousepad-test-plugin." str)

static const GActionEntry test_actions[] = {
  { PF ("window-actions"), test_plugin_window_actions, "s", NULL, NULL },
};

#undef PF



G_DEFINE_DYNAMIC_TYPE (TestPlugin, test_plugin, MOUSEPAD_TYPE_PLUGIN);



void
test_plugin_register (MousepadPluginProvider *plugin)
{
  test_plugin_register_type (G_TYPE_MODULE (plugin));
}



static void
test_plugin_class_init (TestPluginClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
  MousepadPluginClass *mplugin_class = MOUSEPAD_PLUGIN_CLASS (klass);

  gobject_class->finalize = test_plugin_finalize;

  mplugin_class->enable = test_plugin_enable;
  mplugin_class->disable = test_plugin_disable;
}



static void
test_plugin_class_finalize (TestPluginClass *klass)
{
}



static void
test_plugin_init (TestPlugin *plugin)
{
  /* get the mousepad application */
  application = g_application_get_default ();

  /* add test actions to the application */
  g_action_map_add_action_entries (G_ACTION_MAP (application), test_actions,
                                   G_N_ELEMENTS (test_actions), plugin);

  /* setup the dialog actions hash table */
  plugin->dialog_actions = g_hash_table_new (g_str_hash, g_str_equal);
  g_hash_table_insert (plugin->dialog_actions, "preferences", GINT_TO_POINTER (FALSE));
  g_hash_table_insert (plugin->dialog_actions, "file.open", GINT_TO_POINTER (TRUE));
  g_hash_table_insert (plugin->dialog_actions, "file.open-recent.clear-history",
                       GINT_TO_POINTER (TRUE));
  g_hash_table_insert (plugin->dialog_actions, "file.save-as", GINT_TO_POINTER (TRUE));
  g_hash_table_insert (plugin->dialog_actions, "file.print", GINT_TO_POINTER (FALSE));
  g_hash_table_insert (plugin->dialog_actions, "search.find-and-replace", GINT_TO_POINTER (FALSE));
  g_hash_table_insert (plugin->dialog_actions, "search.go-to", GINT_TO_POINTER (TRUE));
  g_hash_table_insert (plugin->dialog_actions, "view.select-font", GINT_TO_POINTER (TRUE));
  g_hash_table_insert (plugin->dialog_actions, "document.tab.tab-size", GINT_TO_POINTER (TRUE));
  g_hash_table_insert (plugin->dialog_actions, "help.about", GINT_TO_POINTER (TRUE));

  test_plugin_enable (MOUSEPAD_PLUGIN (plugin));
}



static void
test_plugin_finalize (GObject *object)
{
  TestPlugin *plugin = TEST_PLUGIN (object);

  test_plugin_disable (MOUSEPAD_PLUGIN (plugin));

  g_hash_table_destroy (plugin->dialog_actions);

  G_OBJECT_CLASS (test_plugin_parent_class)->finalize (object);
}



static gboolean
test_plugin_dialog_shown (gpointer instance)
{
  gpointer dialog;
  GList *windows, *window;

  /* disconnect this handler */
  if (G_IS_ACTION (instance))
    mousepad_disconnect_by_func (instance, test_plugin_dialog_shown, NULL);

  /* search for a GtkDialog in the GdkWindow list */
  windows = gdk_screen_get_toplevel_windows (gdk_screen_get_default ());
  for (window = windows; window != NULL; window = window->next)
    {
      gdk_window_get_user_data (window->data, &dialog);
      if (GTK_IS_DIALOG (dialog) && gtk_widget_get_visible (dialog))
        {
          if (g_application_get_is_busy (application))
            g_application_unmark_busy (application);
          else
            gtk_dialog_response (dialog, MOUSEPAD_RESPONSE_CANCEL);

          break;
        }
    }

  g_list_free (windows);

  if (window == NULL)
    LOG_WARNING ("Dialog not found");

  return FALSE;
}



static void
test_plugin_dialog_shown_timeout (gpointer instance)
{
  g_timeout_add_seconds (2, test_plugin_dialog_shown, mousepad_util_source_autoremove (instance));
}



static void
test_plugin_disable_monitoring (TestPlugin *plugin)
{
  GAction *action;

  /* remove pending sources */
  while (g_source_remove_by_user_data (application))
    ;

  /* disconnect signal handlers */
  g_signal_handlers_disconnect_by_data (application, plugin);

  action = g_action_map_lookup_action (G_ACTION_MAP (application), "preferences");
  g_signal_handlers_disconnect_by_data (action, NULL);
}



static void
test_plugin_is_busy_changed (GApplication *gapplication,
                             GParamSpec *pspec,
                             TestPlugin *plugin)
{
  if (!g_application_get_is_busy (gapplication))
    test_plugin_disable_monitoring (plugin);
}



static void
test_plugin_action_added (GActionGroup *action_group,
                          const gchar *action_name,
                          TestPlugin *plugin)
{
  GAction *action;
  gpointer blocking;

  if (g_hash_table_lookup_extended (plugin->dialog_actions, action_name, NULL, &blocking))
    {
      action = g_action_map_lookup_action (G_ACTION_MAP (action_group), action_name);
      if (blocking)
        test_plugin_dialog_shown_timeout (action);
      else
        g_signal_connect (action, "activate", G_CALLBACK (test_plugin_dialog_shown), NULL);
    }
}



static gboolean
test_plugin_window_shown_idle (gpointer data)
{
  g_application_unmark_busy (application);

  /* `mousepad --disable-server` case: the test script can't run `mousepad --quit`
   * to terminate the current instance, so let's do it internally */
  if (g_application_get_flags (application) & G_APPLICATION_NON_UNIQUE)
    {
      LOG_COMMAND ("app.quit");
      g_action_group_activate_action (G_ACTION_GROUP (application), "quit", NULL);
    }

  return FALSE;
}



static void
test_plugin_window_shown (TestPlugin *plugin)
{
  GtkWindow *window;

  window = gtk_application_get_active_window (GTK_APPLICATION (application));
  if (window != NULL && gtk_widget_get_visible (GTK_WIDGET (window)))
    {
      /* allow time to restore the entire session if needed */
      if (MOUSEPAD_SETTING_GET_ENUM (SESSION_RESTORE) != MOUSEPAD_SESSION_RESTORE_NEVER)
        {
          test_plugin_disable_monitoring (plugin);
          g_idle_add (test_plugin_window_shown_idle, mousepad_util_source_autoremove (plugin));
        }
      else
        test_plugin_window_shown_idle (plugin);
    }
}



static void
test_plugin_enable (MousepadPlugin *mplugin)
{
  /* mark application as busy by default */
  g_application_mark_busy (application);

  /* connect to all required signals to monitor application state */
  g_signal_connect_object (application, "activate", G_CALLBACK (test_plugin_window_shown), mplugin,
                           G_CONNECT_AFTER | G_CONNECT_SWAPPED);
  g_signal_connect_object (application, "open", G_CALLBACK (test_plugin_window_shown), mplugin,
                           G_CONNECT_AFTER | G_CONNECT_SWAPPED);
  g_signal_connect (application, "action-added", G_CALLBACK (test_plugin_action_added), mplugin);

  /* `mousepad --encoding -- file`: a special case where the dialog is not opened by an action */
  g_signal_connect (application, "open", G_CALLBACK (test_plugin_dialog_shown_timeout), mplugin);
  g_signal_connect (application, "notify::is-busy",
                    G_CALLBACK (test_plugin_is_busy_changed), mplugin);
}



static void
test_plugin_disable (MousepadPlugin *mplugin)
{
  test_plugin_disable_monitoring (TEST_PLUGIN (mplugin));
}



static void
test_plugin_activate_action (GActionGroup *group,
                             const gchar *detailed_name)
{
  GAction *action;
  GVariant *parameter;
  GError *error = NULL;
  gchar *name, *prefixed_name;

  /* retrieve action name and parameter */
  if (!g_action_parse_detailed_name (detailed_name, &name, &parameter, &error))
    {
      LOG_WARNING (error->message);
      return;
    }

  /* log command */
  prefixed_name = g_strconcat ("win.", name, NULL);
  LOG_COMMAND (prefixed_name);

  /* ensure action is enabled (e.g. if disabled when the view is not focused) */
  action = g_action_map_lookup_action (G_ACTION_MAP (group), name);
  g_simple_action_set_enabled (G_SIMPLE_ACTION (action), TRUE);

  /* activate action */
  g_action_activate (action, parameter);

  /* cleanup */
  g_free (name);
  g_free (prefixed_name);
  if (parameter != NULL)
    g_variant_unref (parameter);
}



typedef struct _ActionHook
{
  gchar **prereqs;
  gchar **postprocs;
} ActionHook;

/*
 * Allocate a new ActionHook from 'pr' prerequisite and 'pp' post-process action names,
 * to be run before and after a given action:
 *   test_plugin_action_hook_new (pr, pp, "a_1", ..., "a_pr", "b_1", ..., "b_pp")
 *
 * The returned hook is to be inserted in a hash table with the name of this action as key.
 * If this action or one of the prerequisite or post-process actions takes a parameter, it
 * must be specified in the action name following the general syntax for detailed action names:
 * "action-name(parameter)" (see g_action_parse_detailed_name() documentation).
 *
 * As a special case, to only specify a parameter for an action via the hash table key, use
 * an empty hook:
 *   test_plugin_action_hook_new (0, 0)
 *
 * The returned hook should be freed with test_plugin_action_hook_free() when no longer needed.
 */
static ActionHook *
test_plugin_action_hook_new (guint pr,
                             guint pp,
                             ...)
{
  ActionHook *hook;
  guint n;
  va_list ap;

  hook = g_new (ActionHook, 1);
  hook->prereqs = g_new (gchar *, pr + 1);
  hook->postprocs = g_new (gchar *, pp + 1);

  va_start (ap, pp);

  hook->prereqs[pr] = NULL;
  for (n = 0; n < pr; n++)
    hook->prereqs[n] = g_strdup (va_arg (ap, gchar *));

  hook->postprocs[pp] = NULL;
  for (n = 0; n < pp; n++)
    hook->postprocs[n] = g_strdup (va_arg (ap, gchar *));

  va_end (ap);

  return hook;
}

static void
test_plugin_action_hook_free (gpointer data)
{
  ActionHook *hook = data;

  g_strfreev (hook->prereqs);
  g_strfreev (hook->postprocs);
  g_free (hook);
}

static gboolean
test_plugin_action_hook_equal_func (gconstpointer a,
                                    gconstpointer b)
{
  gchar *p, *s = (gchar *) a, *t = (gchar *) b;
  gboolean r;

  if ((p = g_strstr_len (s, -1, "(")) != NULL)
    s = g_strndup (s, p - s);
  else
    return g_str_equal (s, t);

  r = g_str_equal (s, t);
  g_free (s);

  return r;
}

static guint
test_plugin_action_hook_hash_func (gconstpointer k)
{
  gchar *p, *s = (gchar *) k;
  guint r;

  if ((p = g_strstr_len (s, -1, "(")) != NULL)
    s = g_strndup (s, p - s);
  else
    return g_str_hash (s);

  r = g_str_hash (s);
  g_free (s);

  return r;
}



static void
test_plugin_window_actions (GSimpleAction *test_action,
                            GVariant *parameter,
                            gpointer data)
{
  ActionHook *hook;
  GActionGroup *group;
  GRegex *included, *excluded;
  GHashTable *hooks;
  gpointer key;
  gchar **actions, **pname, **qname;
  const gchar *type;
  gboolean save = FALSE;
  gint restore;

  /* get the window actions list */
  group = G_ACTION_GROUP (gtk_application_get_active_window (GTK_APPLICATION (application)));
  actions = g_action_group_list_actions (group);

  /* create the hooks hash table */
  hooks = g_hash_table_new_full (test_plugin_action_hook_hash_func,
                                 test_plugin_action_hook_equal_func,
                                 NULL, test_plugin_action_hook_free);

  /* setup regexes and action hooks */
  type = g_variant_get_string (parameter, NULL);
  if (g_strcmp0 (type, "off-menu") == 0)
    {
      included = g_regex_new ("font-size", 0, 0, NULL);
      excluded = g_regex_new ("^[^.]+\\.", 0, 0, NULL);
    }
  else if (g_strcmp0 (type, "file") == 0)
    {
      included = g_regex_new ("^file\\.", 0, 0, NULL);
      excluded = g_regex_new ("\\.(new-from-template\\.new|open-recent\\.new|close-window)$",
                              0, 0, NULL);

      hook = test_plugin_action_hook_new (1, 0, "file.new");
      g_hash_table_insert (hooks, "file.close-tab", hook);

      hook = test_plugin_action_hook_new (1, 0, "file.new");
      g_hash_table_insert (hooks, "file.detach-tab", hook);

      hook = test_plugin_action_hook_new (0, 1, "file.close-tab");
      g_hash_table_insert (hooks, "file.new", hook);

      hook = test_plugin_action_hook_new (0, 0);
      g_hash_table_insert (hooks, "file.reload(false)", hook);
    }
  else if (g_strcmp0 (type, "edit") == 0)
    {
      included = g_regex_new ("^edit\\.", 0, 0, NULL);
      excluded = g_regex_new ("\\.(paste-from-history|select-all|undo)$", 0, 0, NULL);

      hook = test_plugin_action_hook_new (1, 0, "edit.select-all");
      g_hash_table_insert (hooks, "edit.copy", hook);

      hook = test_plugin_action_hook_new (1, 1, "edit.select-all", "edit.undo");
      g_hash_table_insert (hooks, "edit.cut", hook);

      hook = test_plugin_action_hook_new (2, 0, "edit.select-all", "edit.copy");
      g_hash_table_insert (hooks, "edit.paste", hook);

      hook = test_plugin_action_hook_new (2, 0, "edit.select-all", "edit.copy");
      g_hash_table_insert (hooks, "edit.paste-special.paste-as-column", hook);

      hook = test_plugin_action_hook_new (2, 0, "edit.delete-selection", "edit.undo");
      g_hash_table_insert (hooks, "edit.redo", hook);

      hook = test_plugin_action_hook_new (0, 1, "edit.undo");
      g_hash_table_insert (hooks, "edit.convert.spaces-to-tabs", hook);

      hook = test_plugin_action_hook_new (0, 1, "edit.undo");
      g_hash_table_insert (hooks, "edit.convert.tabs-to-spaces", hook);

      hook = test_plugin_action_hook_new (0, 1, "edit.undo");
      g_hash_table_insert (hooks, "edit.delete-selection", hook);

      save = TRUE;
    }
  else if (g_strcmp0 (type, "search") == 0)
    {
      included = g_regex_new ("^search\\.", 0, 0, NULL);
      excluded = g_regex_new ("$^", 0, 0, NULL);
    }
  else if (g_strcmp0 (type, "view") == 0)
    {
      included = g_regex_new ("^view\\.|bar-visible$", 0, 0, NULL);
      excluded = g_regex_new ("$^", 0, 0, NULL);

      hook = test_plugin_action_hook_new (0, 1, "preferences.window.menubar-visible");
      g_hash_table_insert (hooks, "preferences.window.menubar-visible", hook);

      hook = test_plugin_action_hook_new (0, 1, "preferences.window.statusbar-visible");
      g_hash_table_insert (hooks, "preferences.window.statusbar-visible", hook);

      hook = test_plugin_action_hook_new (0, 1, "preferences.window.toolbar-visible");
      g_hash_table_insert (hooks, "preferences.window.toolbar-visible", hook);

      hook = test_plugin_action_hook_new (0, 1, "view.fullscreen");
      g_hash_table_insert (hooks, "view.fullscreen", hook);
    }
  else if (g_strcmp0 (type, "document") == 0)
    {
      included = g_regex_new ("^document", 0, 0, NULL);
      excluded = g_regex_new ("$^", 0, 0, NULL);

      hook = test_plugin_action_hook_new (1, 0, "file.new");
      g_hash_table_insert (hooks, "document", hook);

      hook = test_plugin_action_hook_new (0, 1, "document.go-to-tab(0)");
      g_hash_table_insert (hooks, "document.go-to-tab(1)", hook);

      hook = test_plugin_action_hook_new (0, 1, "document.viewer-mode");
      g_hash_table_insert (hooks, "document.viewer-mode", hook);

      hook = test_plugin_action_hook_new (0, 1, "document.write-unicode-bom");
      g_hash_table_insert (hooks, "document.write-unicode-bom", hook);

      hook = test_plugin_action_hook_new (0, 0);
      g_hash_table_insert (hooks, "document.filetype('sh')", hook);

      hook = test_plugin_action_hook_new (0, 0);
      g_hash_table_insert (hooks, "document.line-ending(1)", hook);

      hook = test_plugin_action_hook_new (0, 0);
      g_hash_table_insert (hooks, "document.tab.tab-size(0)", hook);

      save = TRUE;
    }
  else if (g_strcmp0 (type, "help") == 0)
    {
      included = g_regex_new ("^help\\.", 0, 0, NULL);
      excluded = g_regex_new ("\\.contents$", 0, 0, NULL);
    }
  else
    {
      included = g_regex_new ("$^", 0, 0, NULL);
      excluded = g_regex_new ("", 0, 0, NULL);
    }

  /* filter action list */
  for (pname = actions; *pname != NULL; pname++)
    if (g_regex_match (included, *pname, 0, NULL) && !g_regex_match (excluded, *pname, 0, NULL))
      {
        /* activate prerequisites */
        if (g_hash_table_lookup_extended (hooks, *pname, &key, (gpointer *) &hook))
          for (qname = hook->prereqs; *qname != NULL; qname++)
            test_plugin_activate_action (group, *qname);

        /* handle dialog if the current action open one */
        test_plugin_action_added (group, *pname, data);

        /* activate current action */
        test_plugin_activate_action (group, key != NULL ? key : *pname);

        /* activate post-processes */
        if (hook != NULL)
          for (qname = hook->postprocs; *qname != NULL; qname++)
            test_plugin_activate_action (group, *qname);
      }

  /* final save to avoid triggering a dialog */
  restore = MOUSEPAD_SETTING_GET_ENUM (SESSION_RESTORE);
  if (save && restore != MOUSEPAD_SESSION_RESTORE_UNSAVED
      && restore != MOUSEPAD_SESSION_RESTORE_ALWAYS)
    test_plugin_activate_action (group, "file.save");

  /* cleanup */
  g_strfreev (actions);
  g_hash_table_destroy (hooks);
  g_regex_unref (included);
  g_regex_unref (excluded);
}