blob: da36280ade5086305427ba7d34f07c7436e97a70 [file] [log] [blame]
Stepan Salenikovichd81ef292015-02-17 18:47:37 -05001/*
Guillaume Roguez77c579d2018-01-30 15:54:02 -05002 * Copyright (C) 2015-2018 Savoir-faire Linux Inc.
Stepan Salenikovichd81ef292015-02-17 18:47:37 -05003 * Author: Stepan Salenikovich <stepan.salenikovich@savoirfairelinux.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Stepan Salenikovichd81ef292015-02-17 18:47:37 -050018 */
19
20#include "ring_client.h"
21
Stepan Salenikovich472c9052016-07-20 19:16:02 -040022// system
23#include <memory>
Victor Nikulshin167bbb62017-03-15 20:44:27 +000024#include <regex>
Stepan Salenikovich472c9052016-07-20 19:16:02 -040025
26// GTK+ related
Stepan Salenikovichd81ef292015-02-17 18:47:37 -050027#include <gtk/gtk.h>
28#include <glib/gi18n.h>
Stepan Salenikovich472c9052016-07-20 19:16:02 -040029#include <clutter-gtk/clutter-gtk.h>
30
31// Qt
Stepan Salenikovich67c5dd32015-09-14 12:27:36 -040032#include <QtCore/QTranslator>
Stepan Salenikovichd2dbcee2015-02-27 16:52:28 -050033#include <QtCore/QCoreApplication>
34#include <QtCore/QString>
35#include <QtCore/QByteArray>
Stepan Salenikovichd2dbcee2015-02-27 16:52:28 -050036#include <QtCore/QItemSelectionModel>
Stepan Salenikovich472c9052016-07-20 19:16:02 -040037#include <QtCore/QStandardPaths>
38
39// LRC
40#include <callmodel.h>
Stepan Salenikovichc64523b2015-02-27 16:31:00 -050041#include <useractionmodel.h>
Stepan Salenikovichdd84cf92015-03-19 21:38:19 -040042#include <categorizedhistorymodel.h>
Stepan Salenikovich6f687072015-03-26 10:43:37 -040043#include <personmodel.h>
44#include <fallbackpersoncollection.h>
Stepan Salenikovich1e131a42015-05-27 14:12:21 -040045#include <localhistorycollection.h>
Stepan Salenikovichf2d76c52015-07-17 17:54:56 -040046#include <numbercategorymodel.h>
Stepan Salenikovichbbd6c132015-08-20 15:21:48 -040047#include <globalinstances.h>
Stepan Salenikovich472c9052016-07-20 19:16:02 -040048#include <profilemodel.h>
49#include <profile.h>
50#include <peerprofilecollection.h>
51#include <localprofilecollection.h>
52#include <accountmodel.h>
Olivier Gregoire66e4df72016-06-17 18:39:05 -040053#include <smartinfohub.h>
Stepan Salenikovich5a127672016-09-13 11:19:50 -040054#include <media/recordingmodel.h>
Stepan Salenikovich509670b2017-04-24 15:18:06 -040055#include <availableaccountmodel.h>
Stepan Salenikovichd81ef292015-02-17 18:47:37 -050056
Stepan Salenikovich472c9052016-07-20 19:16:02 -040057// Ring client
Stepan Salenikovichd81ef292015-02-17 18:47:37 -050058#include "ring_client_options.h"
Stepan Salenikovich434b88f2015-02-19 17:49:08 -050059#include "ringmainwindow.h"
Stepan Salenikovich24dcd5c2015-03-26 17:40:34 -040060#include "dialogs.h"
Stepan Salenikovich6f687072015-03-26 10:43:37 -040061#include "backends/edscontactbackend.h"
Stepan Salenikovichbbd6c132015-08-20 15:21:48 -040062#include "native/pixbufmanipulator.h"
Stepan Salenikovichbe6550c2015-08-21 16:16:03 -040063#include "native/dbuserrorhandler.h"
Stepan Salenikovich76c33e62015-05-22 12:24:07 -040064#include "ringnotify.h"
Stepan Salenikovich76350582015-05-25 14:56:32 -040065#include "config.h"
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -040066#include "utils/files.h"
Stepan Salenikovich4f9bb982015-06-23 14:26:30 -040067#include "revision.h"
Stepan Salenikovich75a39172015-07-10 13:21:08 -040068#include "utils/accounts.h"
Victor Nikulshin167bbb62017-03-15 20:44:27 +000069#include "utils/calling.h"
Stepan Salenikovich434b88f2015-02-19 17:49:08 -050070
aviau59514ed2018-04-19 11:00:37 -040071#if HAVE_AYATANAAPPINDICATOR
72#include <libayatana-appindicator/app-indicator.h>
73#elif HAVE_APPINDICATOR
Stepan Salenikovich982b2882016-06-15 13:13:37 -040074#include <libappindicator/app-indicator.h>
75#endif
76
Stepan Salenikovich472c9052016-07-20 19:16:02 -040077#if USE_LIBNM
Lubomir Rintelabc26c32017-12-19 16:26:45 +010078#include <NetworkManager.h>
Stepan Salenikovich472c9052016-07-20 19:16:02 -040079#endif
80
Stepan Salenikovich434b88f2015-02-19 17:49:08 -050081struct _RingClientClass
82{
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -040083 GtkApplicationClass parent_class;
Stepan Salenikovich434b88f2015-02-19 17:49:08 -050084};
85
86struct _RingClient
87{
88 GtkApplication parent;
89};
90
91typedef struct _RingClientPrivate RingClientPrivate;
Stepan Salenikovichd81ef292015-02-17 18:47:37 -050092
93struct _RingClientPrivate {
Stepan Salenikovich0d515e52015-05-19 16:31:05 -040094 /* args */
95 int argc;
96 char **argv;
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -040097
98 GSettings *settings;
99
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500100 /* main window */
101 GtkWidget *win;
102 /* for libRingclient */
103 QCoreApplication *qtapp;
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400104 /* UAM */
105 QMetaObject::Connection uam_updated;
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400106
Guillaume Roguez884ef732017-07-14 12:53:35 -0400107 std::unique_ptr<QTranslator> translator_lang;
108 std::unique_ptr<QTranslator> translator_full;
Stepan Salenikovich6c39b102015-09-15 14:24:45 -0400109
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400110 GCancellable *cancellable;
Stepan Salenikovichbb9c24e2015-09-15 09:55:02 -0400111
112 gboolean restore_window_state;
Stepan Salenikovich982b2882016-06-15 13:13:37 -0400113
114 gpointer systray_icon;
115 GtkWidget *icon_menu;
Stepan Salenikovich472c9052016-07-20 19:16:02 -0400116
117#if USE_LIBNM
118 /* NetworkManager */
119 NMClient *nm_client;
Stepan Salenikovich221beae2016-10-12 11:29:56 -0400120 NMActiveConnection *primary_connection;
Stepan Salenikovich472c9052016-07-20 19:16:02 -0400121#endif
Stepan Salenikovich5a127672016-09-13 11:19:50 -0400122
123 /* notifications */
124 QMetaObject::Connection call_notification;
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500125};
126
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400127/* this union is used to pass ints as pointers and vice versa for GAction parameters*/
128typedef union _int_ptr_t
129{
130 int value;
131 gint64 value64;
132 gpointer ptr;
133} int_ptr_t;
134
Stepan Salenikovich434b88f2015-02-19 17:49:08 -0500135G_DEFINE_TYPE_WITH_PRIVATE(RingClient, ring_client, GTK_TYPE_APPLICATION);
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500136
137#define RING_CLIENT_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), RING_CLIENT_TYPE, RingClientPrivate))
138
139static void
Stepan Salenikovich541a40a2016-08-17 11:33:47 -0400140exception_dialog(const char* msg)
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500141{
Stepan Salenikovich541a40a2016-08-17 11:33:47 -0400142 g_critical("%s", msg);
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500143 GtkWidget *dialog = gtk_message_dialog_new(NULL,
144 (GtkDialogFlags)(GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT),
145 GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
146 _("Unable to initialize.\nMake sure the Ring daemon (dring) is running.\nError: %s"),
147 msg);
148
149 gtk_window_set_title(GTK_WINDOW(dialog), _("Ring Error"));
150 gtk_dialog_run(GTK_DIALOG(dialog));
151 gtk_widget_destroy(dialog);
152}
153
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400154static void
155ring_accelerators(RingClient *client)
156{
Stepan Salenikovich39ee49a2015-03-24 12:44:55 -0400157#if GTK_CHECK_VERSION(3,12,0)
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400158 const gchar *quit_accels[2] = { "<Ctrl>Q", NULL };
159 gtk_application_set_accels_for_action(GTK_APPLICATION(client), "app.quit", quit_accels);
160#else
Stepan Salenikovich870d7392015-09-08 11:18:46 -0400161 gtk_application_add_accelerator(GTK_APPLICATION(client), "<Control>Q", "app.quit", NULL);
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400162#endif
163}
164
165static void
166action_quit(G_GNUC_UNUSED GSimpleAction *simple,
167 G_GNUC_UNUSED GVariant *parameter,
168 gpointer user_data)
169{
170 g_return_if_fail(G_IS_APPLICATION(user_data));
171
172#if GLIB_CHECK_VERSION(2,32,0)
173 g_application_quit(G_APPLICATION(user_data));
174#else
175 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(user_data);
176 gtk_widget_destroy(priv->win);
177#endif
178}
179
Stepan Salenikovich24dcd5c2015-03-26 17:40:34 -0400180static void
181action_about(G_GNUC_UNUSED GSimpleAction *simple,
Stepan Salenikovich982b2882016-06-15 13:13:37 -0400182 G_GNUC_UNUSED GVariant *parameter,
183 gpointer user_data)
Stepan Salenikovich24dcd5c2015-03-26 17:40:34 -0400184{
185 g_return_if_fail(G_IS_APPLICATION(user_data));
186 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(user_data);
187
188 ring_about_dialog(priv->win);
189}
190
Olivier Gregoire66e4df72016-06-17 18:39:05 -0400191static void
192toggle_smartinfo(GSimpleAction *action, GVariant *parameter, gpointer)
193{
194 g_simple_action_set_state(action, parameter);
195 if (g_variant_get_boolean(parameter)) {
196 SmartInfoHub::instance().start();
197 } else {
198 SmartInfoHub::instance().stop();
199 }
200}
201
Stepan Salenikovich69771842015-02-24 18:11:45 -0500202static const GActionEntry ring_actions[] =
203{
Olivier Gregoire66e4df72016-06-17 18:39:05 -0400204 { "accept", NULL, NULL, NULL, NULL, {0} },
205 { "hangup", NULL, NULL, NULL, NULL, {0} },
206 { "hold", NULL, NULL, "false", NULL, {0} },
207 { "quit", action_quit, NULL, NULL, NULL, {0} },
208 { "about", action_about, NULL, NULL, NULL, {0} },
209 { "mute_audio", NULL, NULL, "false", NULL, {0} },
210 { "mute_video", NULL, NULL, "false", NULL, {0} },
211 { "record", NULL, NULL, "false", NULL, {0} },
212 { "display-smartinfo", NULL, NULL, "false", toggle_smartinfo, {0} },
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500213 /* TODO implement the other actions */
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500214 // { "transfer", NULL, NULL, "flase", NULL, {0} },
Stepan Salenikovich69771842015-02-24 18:11:45 -0500215};
216
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500217static void
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -0400218autostart_toggled(GSettings *settings, G_GNUC_UNUSED gchar *key, G_GNUC_UNUSED gpointer user_data)
219{
220 autostart_symlink(g_settings_get_boolean(settings, "start-on-login"));
221}
222
Stepan Salenikovich982b2882016-06-15 13:13:37 -0400223static void
224show_main_window_toggled(RingClient *client)
225{
226 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
227
228 if (g_settings_get_boolean(priv->settings, "show-main-window")) {
229 gtk_window_present(GTK_WINDOW(priv->win));
230 } else {
231 gtk_widget_hide(priv->win);
232 }
233}
234
Stepan Salenikovichbb9c24e2015-09-15 09:55:02 -0400235static void
236ring_window_show(RingClient *client)
237{
Stepan Salenikovichbb9c24e2015-09-15 09:55:02 -0400238 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
Stepan Salenikovich982b2882016-06-15 13:13:37 -0400239 g_settings_set_boolean(priv->settings, "show-main-window", TRUE);
Stepan Salenikovichbb9c24e2015-09-15 09:55:02 -0400240}
241
242static void
243ring_window_hide(RingClient *client)
244{
Stepan Salenikovichbb9c24e2015-09-15 09:55:02 -0400245 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
Stepan Salenikovich982b2882016-06-15 13:13:37 -0400246 g_settings_set_boolean(priv->settings, "show-main-window", FALSE);
Stepan Salenikovichbb9c24e2015-09-15 09:55:02 -0400247}
248
Stepan Salenikovichbc6c4be2015-07-31 16:07:54 -0400249static gboolean
250on_close_window(GtkWidget *window, G_GNUC_UNUSED GdkEvent *event, RingClient *client)
251{
252 g_return_val_if_fail(GTK_IS_WINDOW(window) && IS_RING_CLIENT(client), FALSE);
Stepan Salenikovichbc6c4be2015-07-31 16:07:54 -0400253 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
254
Stepan Salenikovich982b2882016-06-15 13:13:37 -0400255 if (g_settings_get_boolean(priv->settings, "show-status-icon")) {
Stepan Salenikovichbc6c4be2015-07-31 16:07:54 -0400256 /* we want to simply hide the window and keep the client running */
Stepan Salenikovichbb9c24e2015-09-15 09:55:02 -0400257 ring_window_hide(client);
Hugo Lefeuvre68bc0122018-04-11 16:43:07 -0400258 ring_main_window_reset(RING_MAIN_WINDOW(window));
Hugo Lefeuvre36425312018-07-24 16:46:01 -0400259 return TRUE; /* do not propagate event */
Stepan Salenikovichbc6c4be2015-07-31 16:07:54 -0400260 } else {
Hugo Lefeuvre36425312018-07-24 16:46:01 -0400261 /* we want to quit the application, so just propagate the event */
Stepan Salenikovichbc6c4be2015-07-31 16:07:54 -0400262 return FALSE;
263 }
264}
265
Stepan Salenikovich982b2882016-06-15 13:13:37 -0400266static void
267popup_menu(GtkStatusIcon *self,
268 guint button,
269 guint when,
270 RingClient *client)
271{
272 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
273 G_GNUC_BEGIN_IGNORE_DEPRECATIONS // GtkStatusIcon is deprecated since 3.14, but we fallback on it
274 gtk_menu_popup(GTK_MENU(priv->icon_menu), NULL, NULL, gtk_status_icon_position_menu, self, button, when);
275 G_GNUC_END_IGNORE_DEPRECATIONS
276}
Stepan Salenikovich982b2882016-06-15 13:13:37 -0400277
278static void
279init_systray(RingClient *client)
280{
281 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
282
283 // init menu
284 if (!priv->icon_menu) {
Stepan Salenikovich982b2882016-06-15 13:13:37 -0400285
Stepan Salenikovich97177362016-06-22 18:38:24 -0400286 /* for some reason AppIndicator doesn't like the menu being built from a GMenuModel and/or
287 * the GMenuModel being built from an xml resource. So we build the menu in code.
288 */
289 priv->icon_menu = gtk_menu_new();
Stepan Salenikovich982b2882016-06-15 13:13:37 -0400290 g_object_ref_sink(priv->icon_menu);
Stepan Salenikovich97177362016-06-22 18:38:24 -0400291
292 auto item = gtk_check_menu_item_new_with_label(C_("In the status icon menu, toggle action to show or hide the Ring main window", "Show Ring"));
293 gtk_actionable_set_action_name(GTK_ACTIONABLE(item), "app.show-main-window");
294 gtk_menu_shell_append(GTK_MENU_SHELL(priv->icon_menu), item);
295
296 item = gtk_menu_item_new_with_label(_("Quit"));
297 gtk_actionable_set_action_name(GTK_ACTIONABLE(item), "app.quit");
298 gtk_menu_shell_append(GTK_MENU_SHELL(priv->icon_menu), item);
299
300 gtk_widget_insert_action_group(priv->icon_menu, "app", G_ACTION_GROUP(client));
301 gtk_widget_show_all(priv->icon_menu);
Stepan Salenikovich982b2882016-06-15 13:13:37 -0400302 }
303
Stepan Salenikovich72d812f2017-01-05 12:16:25 -0500304 gboolean use_appinidcator = FALSE;
Stepan Salenikovich982b2882016-06-15 13:13:37 -0400305
Stepan Salenikovich72d812f2017-01-05 12:16:25 -0500306#if HAVE_APPINDICATOR
307 /* only use AppIndicator in Unity (Tuleap: #1440) */
308 const auto desktop = g_getenv("XDG_CURRENT_DESKTOP");
Marco Martin7f2b5ab2017-03-23 12:13:02 +0100309 if (g_strcmp0("Unity", desktop) == 0 || g_strcmp0("KDE", desktop) == 0) {
Stepan Salenikovich72d812f2017-01-05 12:16:25 -0500310 use_appinidcator = TRUE;
Stepan Salenikovich982b2882016-06-15 13:13:37 -0400311
Stepan Salenikovich72d812f2017-01-05 12:16:25 -0500312 auto indicator = app_indicator_new("ring", "ring", APP_INDICATOR_CATEGORY_COMMUNICATIONS);
313 app_indicator_set_status(indicator, APP_INDICATOR_STATUS_ACTIVE);
314 app_indicator_set_title(indicator, "ring");
315 /* app indicator requires a menu */
316 app_indicator_set_menu(indicator, GTK_MENU(priv->icon_menu));
317 priv->systray_icon = indicator;
318 }
Stepan Salenikovich982b2882016-06-15 13:13:37 -0400319#endif
Stepan Salenikovich72d812f2017-01-05 12:16:25 -0500320
321 if (!use_appinidcator) {
322G_GNUC_BEGIN_IGNORE_DEPRECATIONS // GtkStatusIcon is deprecated since 3.14, but we fallback on it
323 auto status_icon = gtk_status_icon_new_from_icon_name("ring");
324 gtk_status_icon_set_title(status_icon, "ring");
325G_GNUC_END_IGNORE_DEPRECATIONS
326 g_signal_connect_swapped(status_icon, "activate", G_CALLBACK(ring_window_show), client);
327 g_signal_connect(status_icon, "popup-menu", G_CALLBACK(popup_menu), client);
328
329 priv->systray_icon = status_icon;
330 }
Stepan Salenikovich982b2882016-06-15 13:13:37 -0400331}
332
333static void
334systray_toggled(GSettings *settings, const gchar *key, RingClient *client)
335{
336 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
337
338 if (g_settings_get_boolean(settings, key)) {
339 if (!priv->systray_icon)
340 init_systray(client);
341 } else {
342 if (priv->systray_icon)
343 g_clear_object(&priv->systray_icon);
344 }
345}
346
Stepan Salenikovichbc6c4be2015-07-31 16:07:54 -0400347static void
348ring_client_activate(GApplication *app)
349{
350 RingClient *client = RING_CLIENT(app);
351 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
352
353 if (priv->win == NULL) {
Stepan Salenikovich982b2882016-06-15 13:13:37 -0400354 // activate being called for the first time
Stepan Salenikovichbc6c4be2015-07-31 16:07:54 -0400355 priv->win = ring_main_window_new(GTK_APPLICATION(app));
356
357 /* make sure win is set to NULL when the window is destroyed */
358 g_object_add_weak_pointer(G_OBJECT(priv->win), (gpointer *)&priv->win);
359
360 /* check if the window should be destoryed or not on close */
361 g_signal_connect(priv->win, "delete-event", G_CALLBACK(on_close_window), client);
Stepan Salenikovichbb9c24e2015-09-15 09:55:02 -0400362
Stepan Salenikovich982b2882016-06-15 13:13:37 -0400363 /* if we didn't launch with the '-r' (--restore-last-window-state) option then force the
364 * show-main-window to true */
365 if (!priv->restore_window_state)
366 ring_window_show(client);
Stepan Salenikovich3f27dc92016-06-22 18:08:01 -0400367 show_main_window_toggled(client);
368 g_signal_connect_swapped(priv->settings, "changed::show-main-window", G_CALLBACK(show_main_window_toggled), client);
Stepan Salenikovichbc6c4be2015-07-31 16:07:54 -0400369
Stepan Salenikovich982b2882016-06-15 13:13:37 -0400370 // track sys icon state
371 g_signal_connect(priv->settings, "changed::show-status-icon", G_CALLBACK(systray_toggled), client);
372 systray_toggled(priv->settings, "show-status-icon", client);
373 } else {
374 // activate not being called for the first time, force showing of main window
Stepan Salenikovichbb9c24e2015-09-15 09:55:02 -0400375 ring_window_show(client);
Stepan Salenikovich982b2882016-06-15 13:13:37 -0400376 }
Stepan Salenikovichbc6c4be2015-07-31 16:07:54 -0400377}
378
Victor Nikulshin167bbb62017-03-15 20:44:27 +0000379static void
380ring_client_open(GApplication *app, GFile **file, gint /*arg3*/, const gchar* /*arg4*/)
381{
382 ring_client_activate(app);
383
384 if (strcmp(g_file_get_uri_scheme(*file), "ring") == 0) {
385 const char * call_id = g_file_get_basename(*file);
386 std::regex format {"^[[:xdigit:]]{40}$"};
387
388 if (std::regex_match(call_id, format)) {
389 auto cm = std::unique_ptr<TemporaryContactMethod>(new TemporaryContactMethod);
390 cm->setUri(URI(QString::fromStdString(call_id)));
391
392 place_new_call(cm.get());
393 cm.release();
394 }
395 }
396}
397
Stepan Salenikovich472c9052016-07-20 19:16:02 -0400398#if USE_LIBNM
399
400static void
401log_connection_info(NMActiveConnection *connection)
402{
403 if (connection) {
Stepan Salenikovich02b283d2016-07-27 18:09:21 -0400404 g_debug("primary network connection: %s, default: %s",
Stepan Salenikovich472c9052016-07-20 19:16:02 -0400405 nm_active_connection_get_uuid(connection),
Stepan Salenikovich472c9052016-07-20 19:16:02 -0400406 nm_active_connection_get_default(connection) ? "yes" : "no");
407 } else {
408 g_warning("no primary network connection detected, check network settings");
409 }
410}
411
412static void
Stepan Salenikovich221beae2016-10-12 11:29:56 -0400413primary_connection_changed(NMClient *nm, GParamSpec*, RingClient *self)
Stepan Salenikovich472c9052016-07-20 19:16:02 -0400414{
Stepan Salenikovich221beae2016-10-12 11:29:56 -0400415 auto priv = RING_CLIENT_GET_PRIVATE(self);
Stepan Salenikovich472c9052016-07-20 19:16:02 -0400416 auto connection = nm_client_get_primary_connection(nm);
Stepan Salenikovich472c9052016-07-20 19:16:02 -0400417
Stepan Salenikovich221beae2016-10-12 11:29:56 -0400418 if (priv->primary_connection != connection) {
419 /* make sure the connection really changed
420 * on client start it seems to always emit the notify::primary-connection signal though it
421 * hasn't changed */
422 log_connection_info(connection);
423 priv->primary_connection = connection;
424 AccountModel::instance().slotConnectivityChanged();
425 }
Stepan Salenikovich472c9052016-07-20 19:16:02 -0400426}
427
428static void
429nm_client_cb(G_GNUC_UNUSED GObject *source_object, GAsyncResult *result, RingClient *self)
430{
431 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(self);
432
433 GError* error = nullptr;
434 if (auto nm_client = nm_client_new_finish(result, &error)) {
435 priv->nm_client = nm_client;
Stepan Salenikovich28ca3172016-07-22 17:29:11 -0400436 g_debug("NetworkManager client initialized, version: %s\ndaemon running: %s\nnnetworking enabled: %s",
Stepan Salenikovich472c9052016-07-20 19:16:02 -0400437 nm_client_get_version(nm_client),
Lubomir Rintelabc26c32017-12-19 16:26:45 +0100438 nm_client_get_nm_running(nm_client) ? "yes" : "no",
Stepan Salenikovich472c9052016-07-20 19:16:02 -0400439 nm_client_networking_get_enabled(nm_client) ? "yes" : "no");
440
441 auto connection = nm_client_get_primary_connection(nm_client);
442 log_connection_info(connection);
Stepan Salenikovich221beae2016-10-12 11:29:56 -0400443 priv->primary_connection = connection;
Stepan Salenikovich472c9052016-07-20 19:16:02 -0400444
445 /* We monitor the primary connection and notify the daemon to re-load its connections
446 * (accounts, UPnP, ...) when it changes. For example, on most systems, if we have an
447 * ethernet connection and then also connect to wifi, the primary connection will not change;
448 * however it will change in the opposite case because an ethernet connection is preferred.
449 */
Stepan Salenikovich221beae2016-10-12 11:29:56 -0400450 g_signal_connect(nm_client, "notify::primary-connection", G_CALLBACK(primary_connection_changed), self);
Stepan Salenikovich472c9052016-07-20 19:16:02 -0400451
452 } else {
453 g_warning("error initializing NetworkManager client: %s", error->message);
454 g_clear_error(&error);
455 }
456}
457
458#endif /* USE_LIBNM */
459
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -0400460static void
Stepan Salenikovich5a127672016-09-13 11:19:50 -0400461call_notifications_toggled(RingClient *self)
462{
463 auto priv = RING_CLIENT_GET_PRIVATE(self);
464
465 if (g_settings_get_boolean(priv->settings, "enable-call-notifications")) {
466 priv->call_notification = QObject::connect(
467 &CallModel::instance(),
468 &CallModel::incomingCall,
469 [] (Call *call) { ring_notify_incoming_call(call); }
470 );
471 } else {
472 QObject::disconnect(priv->call_notification);
473 }
474}
475
476static void
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400477ring_client_startup(GApplication *app)
Stepan Salenikovich69771842015-02-24 18:11:45 -0500478{
Sébastien Blin55bff9d2017-10-03 15:15:23 -0400479 // TODO still use old LRC models, in the future, we will init the LRC here.
Stepan Salenikovich69771842015-02-24 18:11:45 -0500480 RingClient *client = RING_CLIENT(app);
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400481 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
Stepan Salenikovich69771842015-02-24 18:11:45 -0500482
Sébastien Blin897030c2018-06-06 12:33:49 -0400483 g_message("Ring GNOME client version: %d.%02d.%02d", VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH);
Stepan Salenikovich4f9bb982015-06-23 14:26:30 -0400484 g_message("git ref: %s", RING_CLIENT_REVISION);
485
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -0400486 /* make sure that the system corresponds to the autostart setting */
487 autostart_symlink(g_settings_get_boolean(priv->settings, "start-on-login"));
488 g_signal_connect(priv->settings, "changed::start-on-login", G_CALLBACK(autostart_toggled), NULL);
489
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400490 /* init clutter */
491 int clutter_error;
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400492 if ((clutter_error = gtk_clutter_init(&priv->argc, &priv->argv)) != CLUTTER_INIT_SUCCESS) {
493 g_error("Could not init clutter : %d\n", clutter_error);
Hugo Lefeuvre36425312018-07-24 16:46:01 -0400494 exit(1); /* the g_error above should normally cause the application to exit */
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400495 }
496
497 /* init libRingClient and make sure its connected to the dbus */
498 try {
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400499 priv->qtapp = new QCoreApplication(priv->argc, priv->argv);
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400500 /* the call model will try to connect to dring via dbus */
501 CallModel::instance();
Stepan Salenikovich541a40a2016-08-17 11:33:47 -0400502 } catch(const char * msg) {
503 exception_dialog(msg);
504 exit(1);
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400505 } catch(QString& msg) {
Stepan Salenikovich541a40a2016-08-17 11:33:47 -0400506 exception_dialog(msg.toLocal8Bit().constData());
507 exit(1);
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400508 }
509
Stepan Salenikovich67c5dd32015-09-14 12:27:36 -0400510 /* load translations from LRC */
Guillaume Roguez884ef732017-07-14 12:53:35 -0400511 const auto locale_name = QLocale::system().name();
512 const auto locale_lang = locale_name.split('_')[0];
513
514 if (locale_name != locale_lang) {
515 /* Install language first to have lowest priority */
516 priv->translator_lang.reset(new QTranslator);
517 if (priv->translator_lang->load(RING_CLIENT_INSTALL "/share/libringclient/translations/lrc_" + locale_lang)) {
518 g_debug("installed translations for %s", locale_lang.toUtf8().constData());
519 priv->qtapp->installTranslator(priv->translator_lang.get());
520 }
521 }
522
523 priv->translator_full.reset(new QTranslator);
524 if (priv->translator_full->load(RING_CLIENT_INSTALL "/share/libringclient/translations/lrc_" + locale_name)) {
525 g_debug("installed translations for %s", locale_name.toUtf8().constData());
526 }
527
528 if (not priv->translator_lang and not priv->translator_full) {
Stepan Salenikovich67c5dd32015-09-14 12:27:36 -0400529 g_debug("could not load LRC translations for %s, %s",
Guillaume Roguez884ef732017-07-14 12:53:35 -0400530 QLocale::languageToString(QLocale::system().language()).toUtf8().constData(),
531 QLocale::countryToString(QLocale::system().country()).toUtf8().constData()
Stepan Salenikovich67c5dd32015-09-14 12:27:36 -0400532 );
533 }
534
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400535 /* init delegates */
Stepan Salenikovichbbd6c132015-08-20 15:21:48 -0400536 GlobalInstances::setPixmapManipulator(std::unique_ptr<Interfaces::PixbufManipulator>(new Interfaces::PixbufManipulator()));
Stepan Salenikovichbe6550c2015-08-21 16:16:03 -0400537 GlobalInstances::setDBusErrorHandler(std::unique_ptr<Interfaces::DBusErrorHandler>(new Interfaces::DBusErrorHandler()));
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400538
Stepan Salenikovich75a39172015-07-10 13:21:08 -0400539 /* make sure all RING accounts have a display name... this basically makes sure
540 * that all accounts created before the display name patch have a display name
541 * set... a bit of a hack as this should maybe be done in LRC */
542 force_ring_display_name();
543
Stepan Salenikovichf2d76c52015-07-17 17:54:56 -0400544 /* make sure basic number categories exist, in case user has no contacts
545 * from which these would be automatically created
546 */
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400547 NumberCategoryModel::instance().addCategory("work", QVariant());
548 NumberCategoryModel::instance().addCategory("home", QVariant());
Stepan Salenikovichf2d76c52015-07-17 17:54:56 -0400549
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400550 /* add backends */
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400551 CategorizedHistoryModel::instance().addCollection<LocalHistoryCollection>(LoadOptions::FORCE_ENABLED);
Nicolas Jagerb413b302016-05-06 11:41:32 -0400552 PersonModel::instance().addCollection<PeerProfileCollection>(LoadOptions::FORCE_ENABLED);
553 ProfileModel::instance().addCollection<LocalProfileCollection>(LoadOptions::FORCE_ENABLED);
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400554
Stepan Salenikovichf2d76c52015-07-17 17:54:56 -0400555 /* fallback backend for vcards */
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400556 PersonModel::instance().addCollection<FallbackPersonCollection>(LoadOptions::FORCE_ENABLED);
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400557
Stepan Salenikovichf2d76c52015-07-17 17:54:56 -0400558 /* EDS backend(s) */
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400559 load_eds_sources(priv->cancellable);
560
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400561 /* Override theme since we don't have appropriate icons for a dark them (yet) */
562 GtkSettings *gtk_settings = gtk_settings_get_default();
563 g_object_set(G_OBJECT(gtk_settings), "gtk-application-prefer-dark-theme",
564 FALSE, NULL);
565 /* enable button icons */
566 g_object_set(G_OBJECT(gtk_settings), "gtk-button-images",
567 TRUE, NULL);
568
Nicolas Jager655b8db2017-12-15 14:04:32 -0500569 /* enable sound (for notification) */
570 g_object_set(G_OBJECT(gtk_settings), "gtk-enable-event-sounds",
571 TRUE, NULL);
572
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400573 /* add GActions */
Stepan Salenikovich69771842015-02-24 18:11:45 -0500574 g_action_map_add_action_entries(
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400575 G_ACTION_MAP(app), ring_actions, G_N_ELEMENTS(ring_actions), app);
576
Stepan Salenikovich982b2882016-06-15 13:13:37 -0400577 /* GActions for settings */
578 auto action_window_visible = g_settings_create_action(priv->settings, "show-main-window");
579 g_action_map_add_action(G_ACTION_MAP(app), action_window_visible);
580
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400581 /* add accelerators */
582 ring_accelerators(RING_CLIENT(app));
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500583
Stepan Salenikovichbc6c4be2015-07-31 16:07:54 -0400584 /* show window on incoming calls (if the option is set)*/
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400585 QObject::connect(&CallModel::instance(), &CallModel::incomingCall,
Stepan Salenikovichbc6c4be2015-07-31 16:07:54 -0400586 [app] (G_GNUC_UNUSED Call *call) {
587 RingClient *client = RING_CLIENT(app);
588 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
589 if (g_settings_get_boolean(priv->settings, "bring-window-to-front"))
Stepan Salenikovichbb9c24e2015-09-15 09:55:02 -0400590 ring_window_show(client);
Stepan Salenikovichbc6c4be2015-07-31 16:07:54 -0400591 }
592 );
593
Stepan Salenikovich5a127672016-09-13 11:19:50 -0400594 /* enable notifications based on settings */
Stepan Salenikovich76c33e62015-05-22 12:24:07 -0400595 ring_notify_init();
Stepan Salenikovich5a127672016-09-13 11:19:50 -0400596 call_notifications_toggled(client);
Stepan Salenikovich5a127672016-09-13 11:19:50 -0400597 g_signal_connect_swapped(priv->settings, "changed::enable-call-notifications", G_CALLBACK(call_notifications_toggled), client);
Stepan Salenikovich67112d12015-06-16 16:57:06 -0400598
Stepan Salenikovich472c9052016-07-20 19:16:02 -0400599#if USE_LIBNM
600 /* monitor the network using libnm to notify the daemon about connectivity chagnes */
601 nm_client_new_async(priv->cancellable, (GAsyncReadyCallback)nm_client_cb, client);
602#endif
603
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400604 G_APPLICATION_CLASS(ring_client_parent_class)->startup(app);
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400605}
606
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400607static void
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500608ring_client_shutdown(GApplication *app)
609{
610 RingClient *self = RING_CLIENT(app);
611 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(self);
612
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400613 g_debug("quitting");
614
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400615 /* cancel any pending cancellable operations */
616 g_cancellable_cancel(priv->cancellable);
617 g_object_unref(priv->cancellable);
618
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400619 QObject::disconnect(priv->uam_updated);
Stepan Salenikovich5a127672016-09-13 11:19:50 -0400620 QObject::disconnect(priv->call_notification);
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400621
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500622 /* free the QCoreApplication, which will destroy all libRingClient models
623 * and thus send the Unregister signal over dbus to dring */
Nicolas Jagerfad674f2017-10-19 11:35:36 -0400624 if (priv->qtapp) {
625 delete priv->qtapp;
626 priv->qtapp = nullptr;
627 }
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500628
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400629 /* free the copied cmd line args */
630 g_strfreev(priv->argv);
631
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -0400632 g_clear_object(&priv->settings);
633
Stepan Salenikovich76c33e62015-05-22 12:24:07 -0400634 ring_notify_uninit();
635
Stepan Salenikovich472c9052016-07-20 19:16:02 -0400636#if USE_LIBNM
637 /* clear NetworkManager client if it was used */
638 g_clear_object(&priv->nm_client);
639#endif
640
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500641 /* Chain up to the parent class */
642 G_APPLICATION_CLASS(ring_client_parent_class)->shutdown(app);
643}
644
645static void
646ring_client_init(RingClient *self)
647{
648 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(self);
649
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500650 priv->win = NULL;
651 priv->qtapp = NULL;
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400652 priv->cancellable = g_cancellable_new();
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -0400653 priv->settings = g_settings_new_full(get_ring_schema(), NULL, NULL);
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400654
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400655 /* add custom cmd line options */
656 ring_client_add_options(G_APPLICATION(self));
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500657}
658
659static void
660ring_client_class_init(RingClientClass *klass)
661{
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400662 G_APPLICATION_CLASS(klass)->startup = ring_client_startup;
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400663 G_APPLICATION_CLASS(klass)->activate = ring_client_activate;
Victor Nikulshin167bbb62017-03-15 20:44:27 +0000664 G_APPLICATION_CLASS(klass)->open = ring_client_open;
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500665 G_APPLICATION_CLASS(klass)->shutdown = ring_client_shutdown;
666}
667
Sébastien Blin55bff9d2017-10-03 15:15:23 -0400668RingClient*
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400669ring_client_new(int argc, char *argv[])
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500670{
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400671 RingClient *client = (RingClient *)g_object_new(ring_client_get_type(),
Stepan Salenikovich76350582015-05-25 14:56:32 -0400672 "application-id", RING_CLIENT_APP_ID,
Victor Nikulshin167bbb62017-03-15 20:44:27 +0000673 "flags", G_APPLICATION_HANDLES_OPEN ,
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400674 NULL);
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400675
676 /* copy the cmd line args before they get processed by the GApplication*/
677 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
678 priv->argc = argc;
679 priv->argv = g_strdupv((gchar **)argv);
680
681 return client;
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500682}
Stepan Salenikovich67112d12015-06-16 16:57:06 -0400683
Sébastien Blin55bff9d2017-10-03 15:15:23 -0400684GtkWindow*
Stepan Salenikovichbe6550c2015-08-21 16:16:03 -0400685ring_client_get_main_window(RingClient *client)
Stepan Salenikovich67112d12015-06-16 16:57:06 -0400686{
687 g_return_val_if_fail(IS_RING_CLIENT(client), NULL);
688 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
689
690 return (GtkWindow *)priv->win;
691}
Stepan Salenikovichbb9c24e2015-09-15 09:55:02 -0400692
693void
694ring_client_set_restore_main_window_state(RingClient *client, gboolean restore)
695{
696 g_return_if_fail(IS_RING_CLIENT(client));
697 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
698
699 priv->restore_window_state = restore;
700}