calls-application.c 9.52 KB
Newer Older
Mohammed Sadiq's avatar
Mohammed Sadiq committed
1
2
/* calls-application.c
 *
3
 * Copyright (C) 2018, 2019 Purism SPC
Mohammed Sadiq's avatar
Mohammed Sadiq committed
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
 * Copyright (C) 2018 Mohammed Sadiq <sadiq@sadiqpk.org>
 *
 * This file is part of Calls.
 *
 * Calls 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.
 *
 * Calls 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 Calls.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Authors:
 *      Bob Ham <bob.ham@puri.sm>
 *      Mohammed Sadiq <sadiq@sadiqpk.org>
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 */

#include "config.h"
#include "calls-history-box.h"
#include "calls-new-call-box.h"
#include "calls-encryption-indicator.h"
Bob Ham's avatar
Bob Ham committed
32
#include "calls-ringer.h"
33
#include "calls-record-store.h"
Mohammed Sadiq's avatar
Mohammed Sadiq committed
34
35
36
37
#include "calls-call-window.h"
#include "calls-main-window.h"
#include "calls-application.h"

38
39
40
41
42
43
#define HANDY_USE_UNSTABLE_API
#include <handy.h>

#include <libpeas/peas.h>
#include <glib/gi18n.h>

Mohammed Sadiq's avatar
Mohammed Sadiq committed
44
45
46
47
48
49
50
/**
 * SECTION: calls-application
 * @title: CallsApplication
 * @short_description: Base Application class
 * @include: "calls-application.h"
 */

51
52
#define DEFAULT_PROVIDER_PLUGIN "mm"

Mohammed Sadiq's avatar
Mohammed Sadiq committed
53
54
55
56
struct _CallsApplication
{
  GtkApplication parent_instance;

57
58
59
60
  GString          *provider_name;
  CallsProvider    *provider;
  CallsRinger      *ringer;
  CallsRecordStore *record_store;
Mohammed Sadiq's avatar
Mohammed Sadiq committed
61
62
63
64
65
};

G_DEFINE_TYPE (CallsApplication, calls_application, GTK_TYPE_APPLICATION)


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
static gint
handle_local_options (GApplication *application,
                      GVariantDict *options)
{
  gboolean ok;
  g_autoptr(GError) error = NULL;
  const gchar *name;

  g_debug ("Registering application");
  ok = g_application_register (application, NULL, &error);
  if (!ok)
    {
      g_error ("Error registering application: %s",
               error->message);
    }

  ok = g_variant_dict_lookup (options, "provider", "&s", &name);
  if (ok)
    {
      g_action_group_activate_action (G_ACTION_GROUP (application),
                                      "set-provider-name",
                                      g_variant_new_string (name));
    }

  return -1; // Continue processing signal
}


Mohammed Sadiq's avatar
Mohammed Sadiq committed
94
static void
95
96
97
set_provider_name_action (GSimpleAction *action,
                          GVariant      *parameter,
                          gpointer       user_data)
Mohammed Sadiq's avatar
Mohammed Sadiq committed
98
{
99
100
  CallsApplication *self = CALLS_APPLICATION (user_data);
  const gchar *name;
Mohammed Sadiq's avatar
Mohammed Sadiq committed
101

102
103
  name = g_variant_get_string (parameter, NULL);
  g_return_if_fail (name != NULL);
Mohammed Sadiq's avatar
Mohammed Sadiq committed
104

105
106
107
108
109
110
111
112
113
114
115
116
  if (self->provider)
    {
      g_warning ("Cannot set provider name to `%s'"
                 " because provider is already created",
                 name);
      return;
    }

  g_string_assign (self->provider_name, name);

  g_debug ("Provider name set to `%s'",
           self->provider_name->str);
Mohammed Sadiq's avatar
Mohammed Sadiq committed
117
118
}

119
120
121
122
123
124
125

static const GActionEntry actions[] =
{
  { "set-provider-name", set_provider_name_action, "s" },
};


Mohammed Sadiq's avatar
Mohammed Sadiq committed
126
127
128
static void
startup (GApplication *application)
{
129
130
  GtkIconTheme *icon_theme;

Mohammed Sadiq's avatar
Mohammed Sadiq committed
131
132
  G_APPLICATION_CLASS (calls_application_parent_class)->startup (application);

Bob Ham's avatar
Bob Ham committed
133
134
135
  g_set_prgname (APP_ID);
  g_set_application_name (_("Calls"));

136
137
138
  icon_theme = gtk_icon_theme_get_default ();
  gtk_icon_theme_add_resource_path (icon_theme, "/sm/puri/calls/");

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
  g_action_map_add_action_entries (G_ACTION_MAP (application),
                                   actions,
                                   G_N_ELEMENTS (actions),
                                   application);
}


static void
load_provider_plugin (CallsApplication *self)
{
  const gchar * const name = self->provider_name->str;
  PeasEngine *plugins;
  PeasPluginInfo *info;
  PeasExtension *extension;

  g_assert (self->provider == NULL);

  // Add Calls search path and rescan
  plugins = peas_engine_get_default ();
  peas_engine_add_search_path (plugins, PLUGIN_LIBDIR, PLUGIN_LIBDIR);
  g_debug ("Scanning for plugins in `%s'", PLUGIN_LIBDIR);

  // Find the plugin
  info = peas_engine_get_plugin_info (plugins, name);
  if (!info)
    {
      g_critical ("Could not find plugin `%s'", name);
      return;
    }

  // Possibly load the plugin
  if (!peas_plugin_info_is_loaded (info))
    {
      g_autoptr(GError) error = NULL;
Mohammed Sadiq's avatar
Mohammed Sadiq committed
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
      peas_engine_load_plugin (plugins, info);

      if (!peas_plugin_info_is_available (info, &error))
        {
          if (error)
            {
              g_critical ("Error loading plugin `%s': %s",
                          name, error->message);
            }
          else
            {
              g_critical ("Could not load plugin `%s'", name);
            }

          return;
        }

      g_debug ("Loaded plugin `%s'", name);
    }

  // Check the plugin provides CallsProvider
  if (!peas_engine_provides_extension
      (plugins, info, CALLS_TYPE_PROVIDER))
    {
      g_critical ("Plugin `%s' does not have a provider extension",
                  name);
      return;
    }

  // Get the extension
  extension = peas_engine_create_extensionv
    (plugins, info, CALLS_TYPE_PROVIDER, 0, NULL);
  if (!extension)
    {
      g_critical ("Could not create provider from plugin `%s'",
                  name);
      return;
    }

  g_debug ("Created provider from plugin `%s'", name);
  self->provider = CALLS_PROVIDER (extension);
Mohammed Sadiq's avatar
Mohammed Sadiq committed
215
216
}

217

Mohammed Sadiq's avatar
Mohammed Sadiq committed
218
219
220
static void
activate (GApplication *application)
{
221
  GtkApplication *gtk_app;
Mohammed Sadiq's avatar
Mohammed Sadiq committed
222
223
  GtkWindow *window;

224
225
  g_assert (GTK_IS_APPLICATION (application));
  gtk_app = GTK_APPLICATION (application);
Mohammed Sadiq's avatar
Mohammed Sadiq committed
226

227
  window = gtk_application_get_active_window (gtk_app);
Mohammed Sadiq's avatar
Mohammed Sadiq committed
228
229
230

  if (window == NULL)
    {
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
      CallsApplication *self = CALLS_APPLICATION (application);

      // Later we will make provider loading/unloaded a dynamic
      // process but that will have far-reaching consequences and is
      // of no use immediately so for now, we just load one provider
      // at startup.  We can't put this in the actual startup() method
      // though, because we need to be able to set the provider name
      // from the command line and we use actions to do that, which
      // depend on the application already being started up.
      if (!self->provider)
        {
          load_provider_plugin (self);
          if (!self->provider)
            {
              g_application_quit (application);
              return;
            }

          self->ringer = calls_ringer_new (self->provider);
          g_assert (self->ringer != NULL);
251
252
253

          self->record_store = calls_record_store_new (self->provider);
          g_assert (self->record_store != NULL);
254
255
        }

Mohammed Sadiq's avatar
Mohammed Sadiq committed
256
257
258
259
260
      /*
       * We don't track the memory created.  Ideally, we might have to.
       * But we assume that the application is closed by closing the
       * window.  In that case, GTK+ frees the resources right.
       */
261
262
263
      window = GTK_WINDOW
        (calls_main_window_new (gtk_app, self->provider,
                                G_LIST_MODEL (self->record_store)));
264
      calls_call_window_new (gtk_app, self->provider);
Mohammed Sadiq's avatar
Mohammed Sadiq committed
265
266
267
268
269
    }

  gtk_window_present (window);
}

270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291

static void
constructed (GObject *object)
{
  GObjectClass *parent_class = g_type_class_peek (GTK_TYPE_APPLICATION);
  CallsApplication *self = CALLS_APPLICATION (object);
  GSimpleActionGroup *action_group;

  action_group = g_simple_action_group_new ();
  g_action_map_add_action_entries (G_ACTION_MAP (action_group),
                                   actions, G_N_ELEMENTS (actions), self);
  g_object_unref (action_group);

  parent_class->constructed (object);
}


static void
dispose (GObject *object)
{
  CallsApplication *self = (CallsApplication *)object;

292
  g_clear_object (&self->record_store);
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
  g_clear_object (&self->ringer);
  g_clear_object (&self->provider);

  G_OBJECT_CLASS (calls_application_parent_class)->dispose (object);
}


static void
finalize (GObject *object)
{
  CallsApplication *self = (CallsApplication *)object;

  g_string_free (self->provider_name, TRUE);

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


Mohammed Sadiq's avatar
Mohammed Sadiq committed
311
312
313
314
315
316
static void
calls_application_class_init (CallsApplicationClass *klass)
{
  GApplicationClass *application_class = G_APPLICATION_CLASS (klass);
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

317
  object_class->constructed = constructed;
318
  object_class->dispose = dispose;
319
  object_class->finalize = finalize;
Mohammed Sadiq's avatar
Mohammed Sadiq committed
320

321
  application_class->handle_local_options = handle_local_options;
Mohammed Sadiq's avatar
Mohammed Sadiq committed
322
323
324
325
326
327
328
  application_class->startup = startup;
  application_class->activate = activate;

  g_type_ensure (CALLS_TYPE_ENCRYPTION_INDICATOR);
  g_type_ensure (CALLS_TYPE_HISTORY_BOX);
  g_type_ensure (CALLS_TYPE_NEW_CALL_BOX);
  g_type_ensure (HDY_TYPE_DIALER);
329
330
331
332
  g_type_ensure (HDY_TYPE_HEADER_BAR);
  g_type_ensure (HDY_TYPE_SQUEEZER);
  g_type_ensure (HDY_TYPE_VIEW_SWITCHER);
  g_type_ensure (HDY_TYPE_VIEW_SWITCHER_BAR);
Mohammed Sadiq's avatar
Mohammed Sadiq committed
333
334
}

335

Mohammed Sadiq's avatar
Mohammed Sadiq committed
336
337
338
static void
calls_application_init (CallsApplication *self)
{
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
  const GOptionEntry options[] = {
    {
      "provider", 'p', G_OPTION_FLAG_NONE,
      G_OPTION_ARG_STRING, NULL,
      _("The name of the plugin to use for the call Provider"),
      _("PLUGIN")
    },
    {
      NULL
    }
  };

  g_application_add_main_option_entries (G_APPLICATION (self), options);

  self->provider_name = g_string_new (DEFAULT_PROVIDER_PLUGIN);
Mohammed Sadiq's avatar
Mohammed Sadiq committed
354
355
}

356

Mohammed Sadiq's avatar
Mohammed Sadiq committed
357
358
359
360
361
362
363
364
CallsApplication *
calls_application_new (void)
{
  return g_object_new (CALLS_TYPE_APPLICATION,
                       "application-id", APP_ID,
                       "flags", G_APPLICATION_FLAGS_NONE,
                       NULL);
}