gtd-application.c 10.6 KB
Newer Older
1 2
/* gtd-application.c
 *
3
 * Copyright (C) 2015-2020 Georges Basile Stavracas Neto <georges.stavracas@gmail.com>
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * 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/>.
 */

19 20
#define G_LOG_DOMAIN "GtdApplication"

21 22 23
#include "config.h"

#include "gtd-application.h"
24
#include "gtd-debug.h"
25
#include "gtd-initial-setup-window.h"
26
#include "gtd-log.h"
27
#include "gtd-manager.h"
28
#include "gtd-manager-protected.h"
29
#include "gtd-plugin-dialog.h"
30
#include "gtd-theme-manager.h"
31
#include "gtd-vcs.h"
32
#include "gtd-window.h"
33
#include "gtd-window-private.h"
34 35 36 37

#include <glib.h>
#include <glib-object.h>
#include <gio/gio.h>
38
#include <girepository.h>
39 40 41 42 43 44 45
#include <glib/gi18n.h>


struct _GtdApplication
{
  GtkApplication         application;

46 47
  GtdThemeManager       *theme_manager;

48
  GtkWindow             *window;
49 50
  GtkWidget             *plugin_dialog;
  GtkWidget             *initial_setup;
51 52
};

53 54 55 56 57 58 59 60
static void           gtd_application_activate_action             (GSimpleAction        *simple,
                                                                   GVariant             *parameter,
                                                                   gpointer              user_data);

static void           gtd_application_start_client                (GSimpleAction        *simple,
                                                                   GVariant             *parameter,
                                                                   gpointer              user_data);

61 62 63 64
static void           gtd_application_show_extensions             (GSimpleAction        *simple,
                                                                   GVariant             *parameter,
                                                                   gpointer              user_data);

65 66 67 68 69 70 71 72
static void           gtd_application_show_about                  (GSimpleAction        *simple,
                                                                   GVariant             *parameter,
                                                                   gpointer              user_data);

static void           gtd_application_quit                        (GSimpleAction        *simple,
                                                                   GVariant             *parameter,
                                                                   gpointer              user_data);

73
G_DEFINE_TYPE (GtdApplication, gtd_application, GTK_TYPE_APPLICATION)
74

75
static GOptionEntry cmd_options[] = {
76 77
  { "quit", 'q', 0, G_OPTION_ARG_NONE, NULL, N_("Quit GNOME To Do"), NULL },
  { "debug", 'd', 0, G_OPTION_ARG_NONE, NULL, N_("Enable debug messages"), NULL },
78
  { NULL }
79 80
};

81
static const GActionEntry gtd_application_entries[] = {
82 83
  { "activate", gtd_application_activate_action },
  { "start-client", gtd_application_start_client },
84
  { "show-extensions",  gtd_application_show_extensions },
85 86 87 88
  { "about",  gtd_application_show_about },
  { "quit",   gtd_application_quit }
};

89 90 91 92 93
static void
gtd_application_activate_action (GSimpleAction *simple,
                                 GVariant      *parameter,
                                 gpointer       user_data)
{
94
  GtdApplication *self = GTD_APPLICATION (user_data);
95

96
  gtk_window_present (GTK_WINDOW (self->window));
97 98 99 100 101 102 103 104
}

static void
gtd_application_start_client (GSimpleAction *simple,
                              GVariant      *parameter,
                              gpointer       user_data)
{
  /* TODO */
105
  g_debug ("Starting up client");
106 107
}

108 109 110 111 112
static void
gtd_application_show_extensions (GSimpleAction *simple,
                                 GVariant      *parameter,
                                 gpointer       user_data)
{
113
  GtdApplication *self = GTD_APPLICATION (user_data);
114

115
  gtk_widget_show (self->plugin_dialog);
116 117
}

118 119 120 121 122
static void
gtd_application_show_about (GSimpleAction *simple,
                            GVariant      *parameter,
                            gpointer       user_data)
{
123
  GtdApplication *self;
124

Emmanuele Bassi's avatar
Emmanuele Bassi committed
125
  static const gchar *authors[] = {
126
    "Emmanuele Bassi <ebassi@gnome.org>",
127
    "Georges Basile Stavracas Neto <georges.stavracas@gmail.com>",
128
    "Isaque Galdino <igaldino@gmail.com>",
129
    "Patrick Griffis <tingping@tingping.se>",
130
    "Saiful B. Khan <saifulbkhan@gmail.com>",
131 132 133
    NULL
  };

Emmanuele Bassi's avatar
Emmanuele Bassi committed
134
  static const gchar *artists[] = {
135
    "Allan Day <allanpday@gmail.com>",
136
    "Jakub Steiner <jimmac@gmail.com>",
137
    "Tobias Bernard <tbernard@gnome.org>",
138 139 140
    NULL
  };

141
  self = GTD_APPLICATION (user_data);
142

143
  g_autofree gchar *program_name = g_strconcat (_("To Do"), NAME_SUFFIX, NULL);
144
  gtk_show_about_dialog (GTK_WINDOW (self->window),
145
                         "program-name", program_name,
146
                         "version", GTD_VCS_TAG,
147
                         "copyright", _("Copyright \xC2\xA9 2015–2020 The To Do authors"),
148 149 150
                         "license-type", GTK_LICENSE_GPL_3_0,
                         "authors", authors,
                         "artists", artists,
151
                         "logo-icon-name", APPLICATION_ID,
152 153 154 155 156 157 158 159 160
                         "translator-credits", _("translator-credits"),
                         NULL);
}

static void
gtd_application_quit (GSimpleAction *simple,
                      GVariant      *parameter,
                      gpointer       user_data)
{
161
  GtdApplication *self = GTD_APPLICATION (user_data);
162

163
  gtk_window_destroy (self->window);
164 165
}

166 167 168 169
GtdApplication *
gtd_application_new (void)
{
  return g_object_new (GTD_TYPE_APPLICATION,
170
                       "application-id", APPLICATION_ID,
171
                       "flags", G_APPLICATION_HANDLES_COMMAND_LINE,
172
                       "resource-base-path", "/org/gnome/todo",
173 174 175
                       NULL);
}

176
static void
177
run_window (GtdApplication *self)
178
{
179
  gtk_window_present (GTK_WINDOW (self->window));
180 181
}

182
/*
183 184 185 186 187 188 189
static void
finish_initial_setup (GtdApplication *application)
{
  g_return_if_fail (GTD_IS_APPLICATION (application));

  run_window (application);

190
  gtd_manager_set_is_first_run (application->priv->manager, FALSE);
191

192
  g_clear_pointer (&application->priv->initial_setup, gtk_widget_destroy);
193 194 195 196 197 198 199 200 201 202 203 204 205
}

static void
run_initial_setup (GtdApplication *application)
{
  GtdApplicationPrivate *priv;

  g_return_if_fail (GTD_IS_APPLICATION (application));

  priv = application->priv;

  if (!priv->initial_setup)
    {
206 207 208 209 210 211
      priv->initial_setup = gtd_initial_setup_window_new (application);

      g_signal_connect (priv->initial_setup,
                        "cancel",
                        G_CALLBACK (gtk_widget_destroy),
                        application);
212 213

      g_signal_connect_swapped (priv->initial_setup,
214
                                "done",
215 216 217 218 219 220
                                G_CALLBACK (finish_initial_setup),
                                application);
    }

  gtk_widget_show (priv->initial_setup);
}
221
*/
222

223 224 225 226 227 228 229 230 231 232
static void
gtd_application_finalize (GObject *object)
{
  GtdApplication *self = (GtdApplication *)object;

  g_clear_object (&self->theme_manager);

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

233 234 235
static void
gtd_application_activate (GApplication *application)
{
236 237
  GTD_ENTRY;

238 239 240 241
  /* FIXME: the initial setup is disabled for the 3.18 release because
   * we can't create tasklists on GOA accounts.
   */
  run_window (GTD_APPLICATION (application));
242

243
  GTD_EXIT;
244 245
}

246 247 248
static void
gtd_application_startup (GApplication *application)
{
249
  GtdApplication *self;
250

251 252
  GTD_ENTRY;

253
  self = GTD_APPLICATION (application);
254

255
  /* add actions */
256
  g_action_map_add_action_entries (G_ACTION_MAP (self),
257 258
                                   gtd_application_entries,
                                   G_N_ELEMENTS (gtd_application_entries),
259
                                   self);
260 261 262

  G_APPLICATION_CLASS (gtd_application_parent_class)->startup (application);

263 264 265
  /* CSS provider */
  gtd_theme_manager_add_resources (self->theme_manager, "resource:///org/gnome/todo");

266
  /* window */
267
  gtk_window_set_default_icon_name (APPLICATION_ID);
268
  self->window = GTK_WINDOW (gtd_window_new (self));
269

270
  /* plugin dialog */
271
  self->plugin_dialog = gtd_plugin_dialog_new ();
272

273
  gtk_window_set_transient_for (GTK_WINDOW (self->plugin_dialog), GTK_WINDOW (self->window));
274 275

  /* Load the plugins */
276
  gtd_manager_load_plugins (gtd_manager_get_default ());
277 278

  GTD_EXIT;
279 280
}

281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
static gint
gtd_application_command_line (GApplication            *app,
                              GApplicationCommandLine *command_line)
{
  GVariantDict *options;

  options = g_application_command_line_get_options_dict (command_line);

  if (g_variant_dict_contains (options, "quit"))
    {
      g_application_quit (app);
      return 0;
    }

  g_application_activate (app);

  return 0;
}

300 301 302 303 304 305 306 307 308 309 310 311
static gboolean
gtd_application_local_command_line (GApplication   *application,
                                    gchar        ***arguments,
                                    gint           *exit_status)
{
  g_application_add_option_group (application, g_irepository_get_option_group());

  return G_APPLICATION_CLASS (gtd_application_parent_class)->local_command_line (application,
                                                                                 arguments,
                                                                                 exit_status);
}

312 313 314 315 316 317 318 319 320 321
static gint
gtd_application_handle_local_options (GApplication *application,
                                      GVariantDict *options)
{
  if (g_variant_dict_contains (options, "debug"))
    gtd_log_init ();

  return -1;
}

322 323 324
static void
gtd_application_class_init (GtdApplicationClass *klass)
{
325
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
326 327
  GApplicationClass *application_class = G_APPLICATION_CLASS (klass);

328 329
  object_class->finalize = gtd_application_finalize;

330
  application_class->activate = gtd_application_activate;
331
  application_class->startup = gtd_application_startup;
332
  application_class->command_line = gtd_application_command_line;
333
  application_class->local_command_line = gtd_application_local_command_line;
334
  application_class->handle_local_options = gtd_application_handle_local_options;
335 336 337 338 339
}

static void
gtd_application_init (GtdApplication *self)
{
340 341
  self->theme_manager = gtd_theme_manager_new ();

342
  g_application_add_main_option_entries (G_APPLICATION (self), cmd_options);
343
}