File: shortcuts-rc.c

package info (click to toggle)
gimp 3.0.4-3
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 210,076 kB
  • sloc: ansic: 842,287; lisp: 10,761; python: 10,318; cpp: 7,238; perl: 4,355; sh: 1,043; xml: 963; yacc: 609; lex: 348; javascript: 150; makefile: 43
file content (342 lines) | stat: -rw-r--r-- 10,515 bytes parent folder | download
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
/* GIMP - The GNU Image Manipulation Program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * shortcuts-rc.c
 * Copyright (C) 2023 Jehan
 *
 * 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 3 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, see <https://www.gnu.org/licenses/>.
 */

#include "config.h"

#include <gegl.h>
#include <gtk/gtk.h>

#include "libgimpbase/gimpbase.h"
#include "libgimpconfig/gimpconfig.h"

#include "menus-types.h"

#include "widgets/gimpaction.h"
#include "widgets/gimpradioaction.h"

#include "shortcuts-rc.h"

#include "gimp-intl.h"


#define SHORTCUTS_RC_FILE_VERSION 1


/*
 *  All deserialize functions return G_TOKEN_LEFT_PAREN on success,
 *  or the GTokenType they would have expected but didn't get,
 *  or G_TOKEN_ERROR if the function already set an error itself.
 */

static GTokenType shortcuts_action_deserialize (GScanner       *scanner,
                                                GtkApplication *application);


enum
{
  FILE_VERSION = 1,
  ACTION,
};


gboolean
shortcuts_rc_parse (GtkApplication  *application,
                    GFile           *file,
                    GError         **error)
{
  GScanner   *scanner;
  gint        file_version = SHORTCUTS_RC_FILE_VERSION;
  GTokenType  token;

  g_return_val_if_fail (GTK_IS_APPLICATION (application), FALSE);
  g_return_val_if_fail (G_IS_FILE (file), FALSE);
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);

  scanner = gimp_scanner_new_file (file, error);

  if (! scanner)
    return FALSE;

  g_scanner_scope_add_symbol (scanner, 0,
                              "file-version",
                              GINT_TO_POINTER (FILE_VERSION));
  g_scanner_scope_add_symbol (scanner, 0,
                              "action", GINT_TO_POINTER (ACTION));

  token = G_TOKEN_LEFT_PAREN;

  while (g_scanner_peek_next_token (scanner) == token ||
         (token == G_TOKEN_SYMBOL &&
          g_scanner_peek_next_token (scanner) == G_TOKEN_IDENTIFIER))
    {
      token = g_scanner_get_next_token (scanner);

      switch (token)
        {
        case G_TOKEN_LEFT_PAREN:
          token = G_TOKEN_SYMBOL;
          break;

        case G_TOKEN_SYMBOL:
          switch (GPOINTER_TO_INT (scanner->value.v_symbol))
            {
            case FILE_VERSION:
              token = G_TOKEN_INT;
              if (gimp_scanner_parse_int (scanner, &file_version))
                token = G_TOKEN_RIGHT_PAREN;
              break;

            case ACTION:
              g_scanner_set_scope (scanner, ACTION);
              token = shortcuts_action_deserialize (scanner, application);
              g_scanner_set_scope (scanner, 0);
              break;
            default:
              break;
            }
          break;

        case G_TOKEN_IDENTIFIER:
          g_printerr ("%s: ignoring unknown symbol '%s'.\n", G_STRFUNC, scanner->value.v_string);
          while ((token = g_scanner_get_next_token (scanner)) != G_TOKEN_EOF)
            {
              if (token == G_TOKEN_RIGHT_PAREN)
                break;
            }
          token = G_TOKEN_LEFT_PAREN;
          break;

        case G_TOKEN_RIGHT_PAREN:
          token = G_TOKEN_LEFT_PAREN;
          break;

        default: /* do nothing */
          break;
        }
    }

  if (file_version != SHORTCUTS_RC_FILE_VERSION)
    {
      g_printerr (_("Wrong shortcutsrc (%s) file format version: %d (expected: %d). "
                    "We tried to load shortcuts as well as possible.\n"),
                   gimp_file_get_utf8_name (file),
                   file_version, SHORTCUTS_RC_FILE_VERSION);
    }
  if (token != G_TOKEN_LEFT_PAREN)
    {
      if (token != G_TOKEN_ERROR)
        {
          g_scanner_get_next_token (scanner);
          g_scanner_unexp_token (scanner, token, NULL, NULL, NULL,
                                 _("fatal parse error"), TRUE);
        }

      return FALSE;
    }

  gimp_scanner_unref (scanner);

  return TRUE;
}

gboolean
shortcuts_rc_write (GtkApplication  *application,
                   GFile            *file,
                   GError          **error)
{
  GimpConfigWriter  *writer;
  gchar            **actions;

  g_return_val_if_fail (GTK_IS_APPLICATION (application), FALSE);
  g_return_val_if_fail (G_IS_FILE (file), FALSE);
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);

  writer = gimp_config_writer_new_from_file (file,
                                             FALSE,
                                             "GIMP shortcutsrc\n\n"
                                             "If you delete this file, all shortcuts "
                                             "will be reset to defaults.",
                                             error);
  if (! writer)
    return FALSE;

  actions = g_action_group_list_actions (G_ACTION_GROUP (application));

  gimp_config_writer_open (writer, "file-version");
  gimp_config_writer_printf (writer, "%d", SHORTCUTS_RC_FILE_VERSION);
  gimp_config_writer_close (writer);

  gimp_config_writer_linefeed (writer);

  for (gint i = 0; actions[i] != NULL; i++)
    {
      GimpAction  *action;
      gchar      **accels;
      gchar       *detailed_name;
      gboolean     commented = FALSE;

      action = (GimpAction *) g_action_map_lookup_action (G_ACTION_MAP (application), actions[i]);

      if (GIMP_IS_RADIO_ACTION (action))
        {
          gint value;

          g_object_get ((GObject *) action,
                        "value",
                        &value,
                        NULL);
          detailed_name = g_strdup_printf ("app.%s(%i)", actions[i],
                                           value);

        }
      else
        {
          detailed_name = g_strdup_printf ("app.%s", actions[i]);
        }

      accels = gtk_application_get_accels_for_action (application, detailed_name);

      if (gimp_action_use_default_accels (action))
        commented = TRUE;

      gimp_config_writer_comment_mode (writer, commented);

      gimp_config_writer_open (writer, "action");
      gimp_config_writer_string (writer, actions[i]);

      for (gint j = 0; accels[j]; j++)
        gimp_config_writer_string (writer, accels[j]);

      gimp_config_writer_close (writer);
      gimp_config_writer_comment_mode (writer, FALSE);

      g_strfreev (accels);
      g_free (detailed_name);
    }

  g_strfreev (actions);

  return gimp_config_writer_finish (writer, "end of shortcutsrc", error);
}


/* Private functions */

static GTokenType
shortcuts_action_deserialize (GScanner       *scanner,
                              GtkApplication *application)
{
  GStrvBuilder  *builder;
  gchar         *action_name;
  gchar         *accel;
  gchar        **accels;

  if (! gimp_scanner_parse_string (scanner, &action_name))
    return G_TOKEN_STRING;

  builder = g_strv_builder_new ();
  while (gimp_scanner_parse_string (scanner, &accel))
    {
      gchar           **dup_actions;
      gboolean          add_accel = TRUE;
      guint             accelerator_key  = 0;
      GdkModifierType   accelerator_mods = 0;

      gtk_accelerator_parse (accel, &accelerator_key, &accelerator_mods);
      if (accelerator_key == 0 && accelerator_mods == 0)
        {
          g_printerr ("INFO: invalid accelerator '%s' on '%s'.\n"
                      "      Removing this accelerator.\n",
                      accel, action_name);
          g_free (accel);
          continue;
        }
      dup_actions = gtk_application_get_actions_for_accel (application, accel);

      for (gint i = 0; dup_actions[i] != NULL; i++)
        {
          GimpAction *conflict_action;
          gchar      *left_paren_ptr = strchr (dup_actions[i], '(');

          if (left_paren_ptr)
            *left_paren_ptr = '\0';     /* ignore target part of detailed name */

          /* dup_actions[i] will be the detailed name prefixed with "app." */
          if (g_strcmp0 (dup_actions[i] + 4, action_name) == 0)
            continue;

          conflict_action = (GimpAction *) g_action_map_lookup_action (G_ACTION_MAP (application),
                                                                       dup_actions[i] + 4);
          if (gimp_action_use_default_accels (conflict_action))
            {
              /* We might simply not have scanned this action's accelerators yet
               * in the shortcutsrc file. Just delete its current accelerators
               * without any message.
               */
              gimp_action_set_accels (conflict_action, NULL);
            }
          else
            {
              g_printerr ("INFO: duplicate accelerator '%s' on '%s' and '%s'.\n"
                          "      Removing the accelerator from '%s'.\n",
                          accel, action_name, dup_actions[i], action_name);
              add_accel = FALSE;
              break;
            }
        }
      g_strfreev (dup_actions);

      if (add_accel)
        g_strv_builder_add (builder, accel);

      g_free (accel);
    }
  accels = g_strv_builder_end (builder);

  if (g_action_group_has_action (G_ACTION_GROUP (application), action_name))
    {
      GimpAction *action;
      gchar      *detailed_name;

      action = (GimpAction *) g_action_map_lookup_action (G_ACTION_MAP (application),
                                                          action_name);

      detailed_name = g_strdup_printf ("app.%s", action_name);
      gimp_action_set_accels (action, (const gchar **) accels);
      g_free (detailed_name);
    }
  else
    {
      /* Don't set a breaking error, just output on stderr, so that we can make a
       * notice while still loading other actions.
       */
      g_printerr ("INFO: not existing action '%s' was ignored from the shortcutsrc file.\n",
                  action_name);
    }

  g_strv_builder_unref (builder);
  g_free (action_name);
  g_strfreev (accels);

  if (! gimp_scanner_parse_token (scanner, G_TOKEN_RIGHT_PAREN))
    return G_TOKEN_RIGHT_PAREN;

  return G_TOKEN_LEFT_PAREN;
}