chatty-application.c 7.37 KB
Newer Older
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
/* chatty-application.c
 *
 * Copyright 2019 Purism SPC
 *
 * 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/>.
 *
 * Authors:
 *   Mohammed Sadiq <sadiq@sadiqpk.org>
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 */

#define G_LOG_DOMAIN "chatty-application"

#include <glib/gi18n.h>

#include "chatty-config.h"
#include "chatty-window.h"
#include "chatty-application.h"
#include "chatty-purple-init.h"
32
#include "chatty-buddy-list.h"
33 34 35 36 37 38 39 40 41 42

/**
 * SECTION: chatty-application
 * @title: ChattyApplication
 * @short_description: Base Application class
 * @include: "chatty-application.h"
 */

struct _ChattyApplication
{
43
  GtkApplication  parent_instance;
Andrea Schaefer's avatar
Andrea Schaefer committed
44
  gboolean        daemon;
45
  GtkCssProvider *css_provider;
46 47 48

  gboolean enable_debug;
  gboolean enable_verbose;
49 50 51 52
};

G_DEFINE_TYPE (ChattyApplication, chatty_application, GTK_TYPE_APPLICATION)

53
static GOptionEntry cmd_options[] = {
54
  { "version", 'v', G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, NULL, N_("Show release version"), NULL },
Andrea Schaefer's avatar
Andrea Schaefer committed
55 56
  { "daemon", 'D', G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, NULL, N_("Start in daemon mode"), NULL },
  { "nologin", 'n', G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, NULL, N_("Disable all accounts"), NULL },
57
  { "debug", 'd', G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, NULL, N_("Enable libpurple debug messages"), NULL },
58
  { "verbose", 'V', G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, NULL, N_("Enable verbose libpurple debug messages"), NULL },
59 60 61
  { NULL }
};

62 63 64
static void
chatty_application_finalize (GObject *object)
{
65 66 67 68
  ChattyApplication *self = (ChattyApplication *)object;

  g_clear_object (&self->css_provider);

69 70 71
  G_OBJECT_CLASS (chatty_application_parent_class)->finalize (object);
}

72 73 74 75
static gint
chatty_application_handle_local_options (GApplication *application,
                                         GVariantDict *options)
{
76
  if (g_variant_dict_contains (options, "version")) {
77
    g_print ("%s %s\n", PACKAGE_NAME, GIT_VERSION);
78 79
    return 0;
  }
Andrea Schaefer's avatar
Andrea Schaefer committed
80

81 82
  return -1;
}
83

84 85 86 87 88 89 90 91 92
static gint
chatty_application_command_line (GApplication            *application,
                                 GApplicationCommandLine *command_line)
{
  ChattyApplication *self = (ChattyApplication *)application;
  GVariantDict  *options;
  chatty_data_t *chatty;
  g_auto(GStrv) arguments = NULL;
  gint argc;
93

94 95
  chatty = chatty_get_data ();
  options = g_application_command_line_get_options_dict (command_line);
96

97 98
  if (!g_application_command_line_get_is_remote (command_line))
    chatty->cml_options = CHATTY_CML_OPT_NONE;
99

Andrea Schaefer's avatar
Andrea Schaefer committed
100
  if (g_variant_dict_contains (options, "daemon")) {
101
    if (!g_application_command_line_get_is_remote (command_line))
Andrea Schaefer's avatar
Andrea Schaefer committed
102
      self->daemon = TRUE;
103
    else
Andrea Schaefer's avatar
Andrea Schaefer committed
104
      g_debug ("Daemon mode not possible, application is already running");
105 106 107
  }

  if (g_variant_dict_contains (options, "nologin")) {
108
    chatty->cml_options |= CHATTY_CML_OPT_DISABLE;
109
  } else if (g_variant_dict_contains (options, "debug")) {
110
    self->enable_debug = TRUE;
111
  } else if (g_variant_dict_contains (options, "verbose")) {
112 113
    self->enable_debug = TRUE;
    self->enable_verbose = TRUE;
114
  }
115

116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
  purple_debug_set_enabled (self->enable_debug);
  purple_debug_set_verbose (self->enable_verbose);

  arguments = g_application_command_line_get_arguments (command_line, &argc);

  if (argc <= 1) {
    g_application_activate (application);
  } else if (!(g_application_get_flags (application) & G_APPLICATION_HANDLES_OPEN)) {
    g_critical ("This application can not open files.");
    return 1;
  } else {
    GFile **files;
    gint n_files;
    gint i;

    n_files = argc - 1;
    files = g_new (GFile *, n_files);

    for (i = 0; i < n_files; i++)
      files[i] = g_file_new_for_commandline_arg (arguments[i + 1]);

    g_application_open (application, files, n_files, "");
138

139 140 141 142 143 144 145
    for (i = 0; i < n_files; i++)
      g_object_unref (files[i]);
    g_free (files);
  }

  return 0;
}
146

147 148 149
static void
chatty_application_startup (GApplication *application)
{
150 151
  ChattyApplication *self = (ChattyApplication *)application;

152 153
  chatty_data_t *chatty = chatty_get_data ();

Andrea Schaefer's avatar
Andrea Schaefer committed
154 155
  self->daemon = FALSE;

156 157
  chatty->uri = NULL;

158 159 160
  G_APPLICATION_CLASS (chatty_application_parent_class)->startup (application);

  g_set_application_name (CHATTY_APP_NAME);
161 162 163 164 165 166 167 168

  self->css_provider = gtk_css_provider_new ();
  gtk_css_provider_load_from_resource (self->css_provider,
                                       "/sm/puri/chatty/css/style.css");

  gtk_style_context_add_provider_for_screen (gdk_screen_get_default(),
                                             GTK_STYLE_PROVIDER (self->css_provider),
                                             GTK_STYLE_PROVIDER_PRIORITY_USER);
169 170
}

Andrea Schaefer's avatar
Andrea Schaefer committed
171

172 173 174 175
static void
chatty_application_activate (GApplication *application)
{
  GtkApplication *app = (GtkApplication *)application;
Andrea Schaefer's avatar
Andrea Schaefer committed
176 177 178 179
  GtkWindow      *window;
  gboolean        show_win;

  ChattyApplication *self = (ChattyApplication *)application;
180 181 182 183 184

  g_assert (GTK_IS_APPLICATION (app));

  window = gtk_application_get_active_window (app);

Andrea Schaefer's avatar
Andrea Schaefer committed
185 186 187 188
  if (window) {
    show_win = TRUE;
  } else {
    chatty_window_activate (app, NULL);
189

Andrea Schaefer's avatar
Andrea Schaefer committed
190 191 192 193 194 195 196 197
    show_win = !self->daemon;
  }

  if (show_win) {
    window = gtk_application_get_active_window (app);

    gtk_window_present (window);
  }
198 199
}

200 201 202 203 204 205 206 207 208 209 210 211

static void
chatty_application_open (GApplication  *application,
                         GFile        **files,
                         gint           n_files,
                         const gchar   *hint)
{
  gint i;

  chatty_data_t *chatty = chatty_get_data ();

  for (i = 0; i < n_files; i++) {
212
    char *uri;
213 214 215 216

    if (g_file_has_uri_scheme (files[i], "sms")) {
      uri = g_file_get_uri (files[i]);

217
      if (gtk_application_get_active_window (GTK_APPLICATION (application))) {
218 219
        chatty_blist_add_buddy_from_uri (uri);
      } else {
220
        g_free (chatty->uri);
221 222 223 224 225 226 227 228 229 230 231
        chatty->uri = g_strdup (uri);
      }

      g_free (uri);
    }
  }

  g_application_activate (application);
}


232 233 234 235 236 237 238 239
static void
chatty_application_class_init (ChattyApplicationClass *klass)
{
  GApplicationClass *application_class = G_APPLICATION_CLASS (klass);
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->finalize = chatty_application_finalize;

240
  application_class->handle_local_options = chatty_application_handle_local_options;
241
  application_class->command_line = chatty_application_command_line;
242 243
  application_class->startup = chatty_application_startup;
  application_class->activate = chatty_application_activate;
244
  application_class->open = chatty_application_open;
245 246
}

247

248 249 250
static void
chatty_application_init (ChattyApplication *self)
{
251
  g_application_add_main_option_entries (G_APPLICATION (self), cmd_options);
252 253
}

254

255 256 257 258 259
ChattyApplication *
chatty_application_new (void)
{
  return g_object_new (CHATTY_TYPE_APPLICATION,
                       "application-id", CHATTY_APP_ID,
260
                       "flags", G_APPLICATION_HANDLES_OPEN | G_APPLICATION_HANDLES_COMMAND_LINE,
Andrea Schaefer's avatar
Andrea Schaefer committed
261
                       "register-session", TRUE,
262 263
                       NULL);
}