summaryrefslogtreecommitdiffstats
path: root/subprojects/extensions-tool/src/main.c
blob: c33058dc676b238d13eb68939ca9c03e56130f48 (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
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
/* main.c
 *
 * Copyright 2018 Florian Müllner <fmuellner@gnome.org>
 *
 * 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 <http://www.gnu.org/licenses/>.
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 */

#include <gio/gio.h>
#include <glib/gi18n.h>
#include <locale.h>

#include "config.h"
#include "commands.h"
#include "common.h"

static const char *
extension_state_to_string (ExtensionState state)
{
  switch (state)
    {
    case STATE_ENABLED:
      return "ENABLED";
    case STATE_DISABLED:
      return "DISABLED";
    case STATE_ERROR:
      return "ERROR";
    case STATE_OUT_OF_DATE:
      return "OUT OF DATE";
    case STATE_DOWNLOADING:
      return "DOWNLOADING";
    case STATE_INITIALIZED:
      return "INITIALIZED";
    case STATE_UNINSTALLED:
      return "UNINSTALLED";
    }
  return "UNKNOWN";
}

static void
print_nothing (const char *message)
{
}

static gboolean
quiet_cb (const gchar  *option_name,
          const gchar  *value,
          gpointer      data,
          GError      **error)
{
  g_set_printerr_handler (print_nothing);
  return TRUE;
}

GOptionGroup *
get_option_group ()
{
  GOptionEntry entries[] = {
    { .long_name = "quiet", .short_name = 'q',
      .description = _("Do not print error messages"),
      .arg = G_OPTION_ARG_CALLBACK, .arg_data = &quiet_cb,
      .flags = G_OPTION_FLAG_NO_ARG | G_OPTION_FLAG_IN_MAIN },
    { NULL }
  };
  GOptionGroup *group;

  group = g_option_group_new ("Common", "common options", "common options", NULL, NULL);
  g_option_group_add_entries (group, entries);

  return group;
}

void
show_help (GOptionContext *context, const char *message)
{
  g_autofree char *help = NULL;

  if (message)
    g_printerr ("gnome-extensions: %s\n\n", message);

  help = g_option_context_get_help (context, TRUE, NULL);
  g_printerr ("%s", help);
}

GDBusProxy *
get_shell_proxy (GError **error)
{
  return g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
                                        G_DBUS_PROXY_FLAGS_NONE,
                                        NULL,
                                        "org.gnome.Shell.Extensions",
                                        "/org/gnome/Shell/Extensions",
                                        "org.gnome.Shell.Extensions",
                                        NULL,
                                        error);
}

GSettings *
get_shell_settings (void)
{
  g_autoptr (GSettingsSchema) schema = NULL;
  GSettingsSchemaSource *schema_source;

  schema_source = g_settings_schema_source_get_default ();
  schema = g_settings_schema_source_lookup (schema_source,
                                            "org.gnome.shell",
                                            TRUE);

  if (schema == NULL)
    return NULL;

  return g_settings_new_full (schema, NULL, NULL);
}

GVariant *
get_extension_property (GDBusProxy *proxy,
                        const char *uuid,
                        const char *property)
{
  g_autoptr (GVariant) response = NULL;
  g_autoptr (GVariant) asv = NULL;
  g_autoptr (GVariantDict) info = NULL;
  g_autoptr (GError) error = NULL;

  response = g_dbus_proxy_call_sync (proxy,
                                     "GetExtensionInfo",
                                     g_variant_new ("(s)", uuid),
                                     0,
                                     -1,
                                     NULL,
                                     &error);
  if (response == NULL)
    {
      g_printerr (_("Failed to connect to GNOME Shell"));
      return NULL;
    }

  asv = g_variant_get_child_value (response, 0);
  info = g_variant_dict_new (asv);

  if (!g_variant_dict_contains (info, "uuid"))
    {
      g_printerr (_("Extension “%s” doesn't exist\n"), uuid);
      return NULL;
    }

  return g_variant_dict_lookup_value (info, property, NULL);
}

gboolean
settings_list_add (GSettings  *settings,
                   const char *key,
                   const char *value)
{
  g_auto(GStrv) list = NULL;
  g_auto(GStrv) new_value = NULL;
  guint n_values;
  int i;

  if (!g_settings_is_writable (settings, key))
    return FALSE;

  list = g_settings_get_strv (settings, key);

  if (g_strv_contains ((const char **)list, value))
    return TRUE;

  n_values = g_strv_length (list);
  new_value = g_new0 (char *, n_values + 2);
  for (i = 0; i < n_values; i++)
    new_value[i] = g_strdup (list[i]);
  new_value[i] = g_strdup (value);

  g_settings_set_strv (settings, key, (const char **)new_value);
  g_settings_sync ();

  return TRUE;
}

gboolean
settings_list_remove (GSettings  *settings,
                      const char *key,
                      const char *value)
{
  g_auto(GStrv) list = NULL;
  g_auto(GStrv) new_value = NULL;
  const char **s;
  guint n_values;
  int i;

  if (!g_settings_is_writable (settings, key))
    return FALSE;

  list = g_settings_get_strv (settings, key);

  if (!g_strv_contains ((const char **)list, value))
    return TRUE;

  n_values = g_strv_length (list);
  new_value = g_new0 (char *, n_values);
  i = 0;
  for (s = (const char **)list; *s != NULL; s++)
    if (!g_str_equal (*s, value))
      new_value[i++] = g_strdup (*s);

  g_settings_set_strv (settings, key, (const char **)new_value);
  g_settings_sync ();

  return TRUE;
}

void
print_extension_info (GVariantDict  *info,
                      DisplayFormat  format)
{
  const char *uuid, *name, *desc, *path, *url, *author;
  double state, version;

  g_variant_dict_lookup (info, "uuid", "&s", &uuid);
  g_print ("%s\n", uuid);

  if (format == DISPLAY_ONELINE)
    return;

  g_variant_dict_lookup (info, "name", "&s", &name);
  g_print ("  %s: %s\n", _("Name"), name);

  g_variant_dict_lookup (info, "description", "&s", &desc);
  g_print ("  %s: %s\n", _("Description"), desc);

  g_variant_dict_lookup (info, "path", "&s", &path);
  g_print ("  %s: %s\n", _("Path"), path);

  if (g_variant_dict_lookup (info, "url", "&s", &url))
    g_print ("  %s: %s\n", _("URL"), url);

  if (g_variant_dict_lookup (info, "original-author", "&s", &author))
    g_print ("  %s: %s\n", _("Original author"), author);

  if (g_variant_dict_lookup (info, "version", "d", &version))
    g_print ("  %s: %.0f\n", _("Version"), version);

  g_variant_dict_lookup (info, "state", "d", &state);
  g_print ("  %s: %s\n", _("State"), extension_state_to_string (state));
}

gboolean
file_delete_recursively (GFile   *file,
                         GError **error)
{
  g_autoptr (GFileEnumerator) file_enum = NULL;
  GFile *child;

  file_enum = g_file_enumerate_children (file, NULL, 0, NULL, NULL);
  if (file_enum)
    while (TRUE)
      {
        if (!g_file_enumerator_iterate (file_enum, NULL, &child, NULL, error))
          return FALSE;

        if (child == NULL)
          break;

        if (!file_delete_recursively (child, error))
          return FALSE;
      }

  return g_file_delete (file, NULL, error);
}


static int
handle_version (int argc, char *argv[], gboolean do_help)
{
  if (do_help || argc > 1)
    {
      if (!do_help)
        g_printerr ("gnome-extensions: %s\n\n", _("“version” takes no arguments"));

      g_printerr ("%s\n", _("Usage:"));
      g_printerr ("  gnome-extensions version\n");
      g_printerr ("\n");
      g_printerr ("%s\n", _("Print version information and exit."));

      return do_help ? 0 : 2;
    }

  g_print ("%s\n", VERSION);

  return 0;
}

static void
usage (void)
{
  g_autofree char *help_command = NULL;

  help_command = g_strdup_printf ("gnome-extensions help %s", _("COMMAND"));

  g_printerr ("%s\n", _("Usage:"));
  g_printerr ("  gnome-extensions %s %s\n", _("COMMAND"), _("[ARGS…]"));
  g_printerr ("\n");
  g_printerr ("%s\n", _("Commands:"));
  g_printerr ("  help      %s\n", _("Print help"));
  g_printerr ("  version   %s\n", _("Print version"));
  g_printerr ("  enable    %s\n", _("Enable extension"));
  g_printerr ("  disable   %s\n", _("Disable extension"));
  g_printerr ("  reset     %s\n", _("Reset extension"));
  g_printerr ("  uninstall %s\n", _("Uninstall extension"));
  g_printerr ("  list      %s\n", _("List extensions"));
  g_printerr ("  info      %s\n", _("Show extension info"));
  g_printerr ("  show      %s\n", _("Show extension info"));
  g_printerr ("  prefs     %s\n", _("Open extension preferences"));
  g_printerr ("  create    %s\n", _("Create extension"));
  g_printerr ("  pack      %s\n", _("Package extension"));
  g_printerr ("  install   %s\n", _("Install extension bundle"));
  g_printerr ("\n");
  g_printerr (_("Use “%s” to get detailed help.\n"), help_command);
}

int
main (int argc, char *argv[])
{
  const char *command;
  gboolean do_help = FALSE;

  setlocale (LC_ALL, "");
  textdomain (GETTEXT_PACKAGE);
  bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);

#ifdef HAVE_BIND_TEXTDOMAIN_CODESET
  bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
#endif

  if (argc < 2)
    {
      usage ();
      return 1;
    }

  command = argv[1];
  argc--;
  argv++;

  if (g_str_equal (command, "help"))
    {
      if (argc == 1)
        {
          usage ();
          return 0;
        }
      else
        {
          command = argv[1];
          do_help = TRUE;
        }
    }
  else if (g_str_equal (command, "--help"))
    {
      usage ();
      return 0;
    }
  else if (g_str_equal (command, "--version"))
    {
      command = "version";
    }

  if (g_str_equal (command, "version"))
    return handle_version (argc, argv, do_help);
  else if (g_str_equal (command, "enable"))
    return handle_enable (argc, argv, do_help);
  else if (g_str_equal (command, "disable"))
    return handle_disable (argc, argv, do_help);
  else if (g_str_equal (command, "reset"))
    return handle_reset (argc, argv, do_help);
  else if (g_str_equal (command, "list"))
    return handle_list (argc, argv, do_help);
  else if (g_str_equal (command, "info"))
    return handle_info (argc, argv, do_help);
  else if (g_str_equal (command, "show"))
    return handle_info (argc, argv, do_help);
  else if (g_str_equal (command, "prefs"))
    return handle_prefs (argc, argv, do_help);
  else if (g_str_equal (command, "create"))
    return handle_create (argc, argv, do_help);
  else if (g_str_equal (command, "pack"))
    return handle_pack (argc, argv, do_help);
  else if (g_str_equal (command, "install"))
    return handle_install (argc, argv, do_help);
  else if (g_str_equal (command, "uninstall"))
    return handle_uninstall (argc, argv, do_help);
  else
    usage ();

  return 1;
}