blob: d2c25ef11f56dae1ebea6c0814bf3aeb6d0af0f2 [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 Salenikovichd81ef292015-02-17 18:47:37 -0500122};
123
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400124/* this union is used to pass ints as pointers and vice versa for GAction parameters*/
125typedef union _int_ptr_t
126{
127 int value;
128 gint64 value64;
129 gpointer ptr;
130} int_ptr_t;
131
Stepan Salenikovich434b88f2015-02-19 17:49:08 -0500132G_DEFINE_TYPE_WITH_PRIVATE(RingClient, ring_client, GTK_TYPE_APPLICATION);
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500133
134#define RING_CLIENT_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), RING_CLIENT_TYPE, RingClientPrivate))
135
136static void
Stepan Salenikovich541a40a2016-08-17 11:33:47 -0400137exception_dialog(const char* msg)
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500138{
Stepan Salenikovich541a40a2016-08-17 11:33:47 -0400139 g_critical("%s", msg);
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500140 GtkWidget *dialog = gtk_message_dialog_new(NULL,
141 (GtkDialogFlags)(GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT),
142 GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
143 _("Unable to initialize.\nMake sure the Ring daemon (dring) is running.\nError: %s"),
144 msg);
145
146 gtk_window_set_title(GTK_WINDOW(dialog), _("Ring Error"));
147 gtk_dialog_run(GTK_DIALOG(dialog));
148 gtk_widget_destroy(dialog);
149}
150
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400151static void
152ring_accelerators(RingClient *client)
153{
Stepan Salenikovich39ee49a2015-03-24 12:44:55 -0400154#if GTK_CHECK_VERSION(3,12,0)
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400155 const gchar *quit_accels[2] = { "<Ctrl>Q", NULL };
156 gtk_application_set_accels_for_action(GTK_APPLICATION(client), "app.quit", quit_accels);
157#else
Stepan Salenikovich870d7392015-09-08 11:18:46 -0400158 gtk_application_add_accelerator(GTK_APPLICATION(client), "<Control>Q", "app.quit", NULL);
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400159#endif
160}
161
162static void
163action_quit(G_GNUC_UNUSED GSimpleAction *simple,
164 G_GNUC_UNUSED GVariant *parameter,
165 gpointer user_data)
166{
167 g_return_if_fail(G_IS_APPLICATION(user_data));
168
169#if GLIB_CHECK_VERSION(2,32,0)
170 g_application_quit(G_APPLICATION(user_data));
171#else
172 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(user_data);
173 gtk_widget_destroy(priv->win);
174#endif
175}
176
Stepan Salenikovich24dcd5c2015-03-26 17:40:34 -0400177static void
178action_about(G_GNUC_UNUSED GSimpleAction *simple,
Stepan Salenikovich982b2882016-06-15 13:13:37 -0400179 G_GNUC_UNUSED GVariant *parameter,
180 gpointer user_data)
Stepan Salenikovich24dcd5c2015-03-26 17:40:34 -0400181{
182 g_return_if_fail(G_IS_APPLICATION(user_data));
183 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(user_data);
184
185 ring_about_dialog(priv->win);
186}
187
Olivier Gregoire66e4df72016-06-17 18:39:05 -0400188static void
189toggle_smartinfo(GSimpleAction *action, GVariant *parameter, gpointer)
190{
191 g_simple_action_set_state(action, parameter);
192 if (g_variant_get_boolean(parameter)) {
193 SmartInfoHub::instance().start();
194 } else {
195 SmartInfoHub::instance().stop();
196 }
197}
198
Stepan Salenikovich69771842015-02-24 18:11:45 -0500199static const GActionEntry ring_actions[] =
200{
Olivier Gregoire66e4df72016-06-17 18:39:05 -0400201 { "accept", NULL, NULL, NULL, NULL, {0} },
202 { "hangup", NULL, NULL, NULL, NULL, {0} },
203 { "hold", NULL, NULL, "false", NULL, {0} },
204 { "quit", action_quit, NULL, NULL, NULL, {0} },
205 { "about", action_about, NULL, NULL, NULL, {0} },
206 { "mute_audio", NULL, NULL, "false", NULL, {0} },
207 { "mute_video", NULL, NULL, "false", NULL, {0} },
208 { "record", NULL, NULL, "false", NULL, {0} },
209 { "display-smartinfo", NULL, NULL, "false", toggle_smartinfo, {0} },
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500210 /* TODO implement the other actions */
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500211 // { "transfer", NULL, NULL, "flase", NULL, {0} },
Stepan Salenikovich69771842015-02-24 18:11:45 -0500212};
213
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500214static void
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -0400215autostart_toggled(GSettings *settings, G_GNUC_UNUSED gchar *key, G_GNUC_UNUSED gpointer user_data)
216{
217 autostart_symlink(g_settings_get_boolean(settings, "start-on-login"));
218}
219
Stepan Salenikovich982b2882016-06-15 13:13:37 -0400220static void
221show_main_window_toggled(RingClient *client)
222{
223 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
224
225 if (g_settings_get_boolean(priv->settings, "show-main-window")) {
226 gtk_window_present(GTK_WINDOW(priv->win));
227 } else {
228 gtk_widget_hide(priv->win);
229 }
230}
231
Stepan Salenikovichbb9c24e2015-09-15 09:55:02 -0400232static void
233ring_window_show(RingClient *client)
234{
Stepan Salenikovichbb9c24e2015-09-15 09:55:02 -0400235 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
Stepan Salenikovich982b2882016-06-15 13:13:37 -0400236 g_settings_set_boolean(priv->settings, "show-main-window", TRUE);
Stepan Salenikovichbb9c24e2015-09-15 09:55:02 -0400237}
238
239static void
240ring_window_hide(RingClient *client)
241{
Stepan Salenikovichbb9c24e2015-09-15 09:55:02 -0400242 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
Stepan Salenikovich982b2882016-06-15 13:13:37 -0400243 g_settings_set_boolean(priv->settings, "show-main-window", FALSE);
Stepan Salenikovichbb9c24e2015-09-15 09:55:02 -0400244}
245
Stepan Salenikovichbc6c4be2015-07-31 16:07:54 -0400246static gboolean
247on_close_window(GtkWidget *window, G_GNUC_UNUSED GdkEvent *event, RingClient *client)
248{
249 g_return_val_if_fail(GTK_IS_WINDOW(window) && IS_RING_CLIENT(client), FALSE);
Stepan Salenikovichbc6c4be2015-07-31 16:07:54 -0400250 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
251
Stepan Salenikovich982b2882016-06-15 13:13:37 -0400252 if (g_settings_get_boolean(priv->settings, "show-status-icon")) {
Stepan Salenikovichbc6c4be2015-07-31 16:07:54 -0400253 /* we want to simply hide the window and keep the client running */
Stepan Salenikovichbb9c24e2015-09-15 09:55:02 -0400254 ring_window_hide(client);
Hugo Lefeuvre68bc0122018-04-11 16:43:07 -0400255 ring_main_window_reset(RING_MAIN_WINDOW(window));
Hugo Lefeuvre36425312018-07-24 16:46:01 -0400256 return TRUE; /* do not propagate event */
Stepan Salenikovichbc6c4be2015-07-31 16:07:54 -0400257 } else {
Hugo Lefeuvre36425312018-07-24 16:46:01 -0400258 /* we want to quit the application, so just propagate the event */
Stepan Salenikovichbc6c4be2015-07-31 16:07:54 -0400259 return FALSE;
260 }
261}
262
Stepan Salenikovich982b2882016-06-15 13:13:37 -0400263static void
264popup_menu(GtkStatusIcon *self,
265 guint button,
266 guint when,
267 RingClient *client)
268{
269 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
270 G_GNUC_BEGIN_IGNORE_DEPRECATIONS // GtkStatusIcon is deprecated since 3.14, but we fallback on it
271 gtk_menu_popup(GTK_MENU(priv->icon_menu), NULL, NULL, gtk_status_icon_position_menu, self, button, when);
272 G_GNUC_END_IGNORE_DEPRECATIONS
273}
Stepan Salenikovich982b2882016-06-15 13:13:37 -0400274
275static void
276init_systray(RingClient *client)
277{
278 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
279
280 // init menu
281 if (!priv->icon_menu) {
Stepan Salenikovich982b2882016-06-15 13:13:37 -0400282
Stepan Salenikovich97177362016-06-22 18:38:24 -0400283 /* for some reason AppIndicator doesn't like the menu being built from a GMenuModel and/or
284 * the GMenuModel being built from an xml resource. So we build the menu in code.
285 */
286 priv->icon_menu = gtk_menu_new();
Stepan Salenikovich982b2882016-06-15 13:13:37 -0400287 g_object_ref_sink(priv->icon_menu);
Stepan Salenikovich97177362016-06-22 18:38:24 -0400288
289 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"));
290 gtk_actionable_set_action_name(GTK_ACTIONABLE(item), "app.show-main-window");
291 gtk_menu_shell_append(GTK_MENU_SHELL(priv->icon_menu), item);
292
293 item = gtk_menu_item_new_with_label(_("Quit"));
294 gtk_actionable_set_action_name(GTK_ACTIONABLE(item), "app.quit");
295 gtk_menu_shell_append(GTK_MENU_SHELL(priv->icon_menu), item);
296
297 gtk_widget_insert_action_group(priv->icon_menu, "app", G_ACTION_GROUP(client));
298 gtk_widget_show_all(priv->icon_menu);
Stepan Salenikovich982b2882016-06-15 13:13:37 -0400299 }
300
Stepan Salenikovich72d812f2017-01-05 12:16:25 -0500301 gboolean use_appinidcator = FALSE;
Stepan Salenikovich982b2882016-06-15 13:13:37 -0400302
Stepan Salenikovich72d812f2017-01-05 12:16:25 -0500303#if HAVE_APPINDICATOR
304 /* only use AppIndicator in Unity (Tuleap: #1440) */
305 const auto desktop = g_getenv("XDG_CURRENT_DESKTOP");
Marco Martin7f2b5ab2017-03-23 12:13:02 +0100306 if (g_strcmp0("Unity", desktop) == 0 || g_strcmp0("KDE", desktop) == 0) {
Stepan Salenikovich72d812f2017-01-05 12:16:25 -0500307 use_appinidcator = TRUE;
Stepan Salenikovich982b2882016-06-15 13:13:37 -0400308
Stepan Salenikovich72d812f2017-01-05 12:16:25 -0500309 auto indicator = app_indicator_new("ring", "ring", APP_INDICATOR_CATEGORY_COMMUNICATIONS);
310 app_indicator_set_status(indicator, APP_INDICATOR_STATUS_ACTIVE);
311 app_indicator_set_title(indicator, "ring");
312 /* app indicator requires a menu */
313 app_indicator_set_menu(indicator, GTK_MENU(priv->icon_menu));
314 priv->systray_icon = indicator;
315 }
Stepan Salenikovich982b2882016-06-15 13:13:37 -0400316#endif
Stepan Salenikovich72d812f2017-01-05 12:16:25 -0500317
318 if (!use_appinidcator) {
319G_GNUC_BEGIN_IGNORE_DEPRECATIONS // GtkStatusIcon is deprecated since 3.14, but we fallback on it
320 auto status_icon = gtk_status_icon_new_from_icon_name("ring");
321 gtk_status_icon_set_title(status_icon, "ring");
322G_GNUC_END_IGNORE_DEPRECATIONS
323 g_signal_connect_swapped(status_icon, "activate", G_CALLBACK(ring_window_show), client);
324 g_signal_connect(status_icon, "popup-menu", G_CALLBACK(popup_menu), client);
325
326 priv->systray_icon = status_icon;
327 }
Stepan Salenikovich982b2882016-06-15 13:13:37 -0400328}
329
330static void
331systray_toggled(GSettings *settings, const gchar *key, RingClient *client)
332{
333 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
334
335 if (g_settings_get_boolean(settings, key)) {
336 if (!priv->systray_icon)
337 init_systray(client);
338 } else {
339 if (priv->systray_icon)
340 g_clear_object(&priv->systray_icon);
341 }
342}
343
Stepan Salenikovichbc6c4be2015-07-31 16:07:54 -0400344static void
345ring_client_activate(GApplication *app)
346{
347 RingClient *client = RING_CLIENT(app);
348 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
349
350 if (priv->win == NULL) {
Stepan Salenikovich982b2882016-06-15 13:13:37 -0400351 // activate being called for the first time
Stepan Salenikovichbc6c4be2015-07-31 16:07:54 -0400352 priv->win = ring_main_window_new(GTK_APPLICATION(app));
353
354 /* make sure win is set to NULL when the window is destroyed */
355 g_object_add_weak_pointer(G_OBJECT(priv->win), (gpointer *)&priv->win);
356
357 /* check if the window should be destoryed or not on close */
358 g_signal_connect(priv->win, "delete-event", G_CALLBACK(on_close_window), client);
Stepan Salenikovichbb9c24e2015-09-15 09:55:02 -0400359
Stepan Salenikovich982b2882016-06-15 13:13:37 -0400360 /* if we didn't launch with the '-r' (--restore-last-window-state) option then force the
361 * show-main-window to true */
362 if (!priv->restore_window_state)
363 ring_window_show(client);
Stepan Salenikovich3f27dc92016-06-22 18:08:01 -0400364 show_main_window_toggled(client);
365 g_signal_connect_swapped(priv->settings, "changed::show-main-window", G_CALLBACK(show_main_window_toggled), client);
Stepan Salenikovichbc6c4be2015-07-31 16:07:54 -0400366
Stepan Salenikovich982b2882016-06-15 13:13:37 -0400367 // track sys icon state
368 g_signal_connect(priv->settings, "changed::show-status-icon", G_CALLBACK(systray_toggled), client);
369 systray_toggled(priv->settings, "show-status-icon", client);
370 } else {
371 // activate not being called for the first time, force showing of main window
Stepan Salenikovichbb9c24e2015-09-15 09:55:02 -0400372 ring_window_show(client);
Stepan Salenikovich982b2882016-06-15 13:13:37 -0400373 }
Stepan Salenikovichbc6c4be2015-07-31 16:07:54 -0400374}
375
Victor Nikulshin167bbb62017-03-15 20:44:27 +0000376static void
377ring_client_open(GApplication *app, GFile **file, gint /*arg3*/, const gchar* /*arg4*/)
378{
379 ring_client_activate(app);
380
381 if (strcmp(g_file_get_uri_scheme(*file), "ring") == 0) {
382 const char * call_id = g_file_get_basename(*file);
383 std::regex format {"^[[:xdigit:]]{40}$"};
384
385 if (std::regex_match(call_id, format)) {
386 auto cm = std::unique_ptr<TemporaryContactMethod>(new TemporaryContactMethod);
387 cm->setUri(URI(QString::fromStdString(call_id)));
388
389 place_new_call(cm.get());
390 cm.release();
391 }
392 }
393}
394
Stepan Salenikovich472c9052016-07-20 19:16:02 -0400395#if USE_LIBNM
396
397static void
398log_connection_info(NMActiveConnection *connection)
399{
400 if (connection) {
Stepan Salenikovich02b283d2016-07-27 18:09:21 -0400401 g_debug("primary network connection: %s, default: %s",
Stepan Salenikovich472c9052016-07-20 19:16:02 -0400402 nm_active_connection_get_uuid(connection),
Stepan Salenikovich472c9052016-07-20 19:16:02 -0400403 nm_active_connection_get_default(connection) ? "yes" : "no");
404 } else {
405 g_warning("no primary network connection detected, check network settings");
406 }
407}
408
409static void
Stepan Salenikovich221beae2016-10-12 11:29:56 -0400410primary_connection_changed(NMClient *nm, GParamSpec*, RingClient *self)
Stepan Salenikovich472c9052016-07-20 19:16:02 -0400411{
Stepan Salenikovich221beae2016-10-12 11:29:56 -0400412 auto priv = RING_CLIENT_GET_PRIVATE(self);
Stepan Salenikovich472c9052016-07-20 19:16:02 -0400413 auto connection = nm_client_get_primary_connection(nm);
Stepan Salenikovich472c9052016-07-20 19:16:02 -0400414
Stepan Salenikovich221beae2016-10-12 11:29:56 -0400415 if (priv->primary_connection != connection) {
416 /* make sure the connection really changed
417 * on client start it seems to always emit the notify::primary-connection signal though it
418 * hasn't changed */
419 log_connection_info(connection);
420 priv->primary_connection = connection;
421 AccountModel::instance().slotConnectivityChanged();
422 }
Stepan Salenikovich472c9052016-07-20 19:16:02 -0400423}
424
425static void
426nm_client_cb(G_GNUC_UNUSED GObject *source_object, GAsyncResult *result, RingClient *self)
427{
428 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(self);
429
430 GError* error = nullptr;
431 if (auto nm_client = nm_client_new_finish(result, &error)) {
432 priv->nm_client = nm_client;
Stepan Salenikovich28ca3172016-07-22 17:29:11 -0400433 g_debug("NetworkManager client initialized, version: %s\ndaemon running: %s\nnnetworking enabled: %s",
Stepan Salenikovich472c9052016-07-20 19:16:02 -0400434 nm_client_get_version(nm_client),
Lubomir Rintelabc26c32017-12-19 16:26:45 +0100435 nm_client_get_nm_running(nm_client) ? "yes" : "no",
Stepan Salenikovich472c9052016-07-20 19:16:02 -0400436 nm_client_networking_get_enabled(nm_client) ? "yes" : "no");
437
438 auto connection = nm_client_get_primary_connection(nm_client);
439 log_connection_info(connection);
Stepan Salenikovich221beae2016-10-12 11:29:56 -0400440 priv->primary_connection = connection;
Stepan Salenikovich472c9052016-07-20 19:16:02 -0400441
442 /* We monitor the primary connection and notify the daemon to re-load its connections
443 * (accounts, UPnP, ...) when it changes. For example, on most systems, if we have an
444 * ethernet connection and then also connect to wifi, the primary connection will not change;
445 * however it will change in the opposite case because an ethernet connection is preferred.
446 */
Stepan Salenikovich221beae2016-10-12 11:29:56 -0400447 g_signal_connect(nm_client, "notify::primary-connection", G_CALLBACK(primary_connection_changed), self);
Stepan Salenikovich472c9052016-07-20 19:16:02 -0400448
449 } else {
450 g_warning("error initializing NetworkManager client: %s", error->message);
451 g_clear_error(&error);
452 }
453}
454
455#endif /* USE_LIBNM */
456
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -0400457static void
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400458ring_client_startup(GApplication *app)
Stepan Salenikovich69771842015-02-24 18:11:45 -0500459{
Sébastien Blin55bff9d2017-10-03 15:15:23 -0400460 // TODO still use old LRC models, in the future, we will init the LRC here.
Stepan Salenikovich69771842015-02-24 18:11:45 -0500461 RingClient *client = RING_CLIENT(app);
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400462 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
Stepan Salenikovich69771842015-02-24 18:11:45 -0500463
Sébastien Blin897030c2018-06-06 12:33:49 -0400464 g_message("Ring GNOME client version: %d.%02d.%02d", VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH);
Stepan Salenikovich4f9bb982015-06-23 14:26:30 -0400465 g_message("git ref: %s", RING_CLIENT_REVISION);
466
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -0400467 /* make sure that the system corresponds to the autostart setting */
468 autostart_symlink(g_settings_get_boolean(priv->settings, "start-on-login"));
469 g_signal_connect(priv->settings, "changed::start-on-login", G_CALLBACK(autostart_toggled), NULL);
470
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400471 /* init clutter */
472 int clutter_error;
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400473 if ((clutter_error = gtk_clutter_init(&priv->argc, &priv->argv)) != CLUTTER_INIT_SUCCESS) {
474 g_error("Could not init clutter : %d\n", clutter_error);
Hugo Lefeuvre36425312018-07-24 16:46:01 -0400475 exit(1); /* the g_error above should normally cause the application to exit */
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400476 }
477
478 /* init libRingClient and make sure its connected to the dbus */
479 try {
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400480 priv->qtapp = new QCoreApplication(priv->argc, priv->argv);
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400481 /* the call model will try to connect to dring via dbus */
482 CallModel::instance();
Stepan Salenikovich541a40a2016-08-17 11:33:47 -0400483 } catch(const char * msg) {
484 exception_dialog(msg);
485 exit(1);
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400486 } catch(QString& msg) {
Stepan Salenikovich541a40a2016-08-17 11:33:47 -0400487 exception_dialog(msg.toLocal8Bit().constData());
488 exit(1);
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400489 }
490
Stepan Salenikovich67c5dd32015-09-14 12:27:36 -0400491 /* load translations from LRC */
Guillaume Roguez884ef732017-07-14 12:53:35 -0400492 const auto locale_name = QLocale::system().name();
493 const auto locale_lang = locale_name.split('_')[0];
494
495 if (locale_name != locale_lang) {
496 /* Install language first to have lowest priority */
497 priv->translator_lang.reset(new QTranslator);
498 if (priv->translator_lang->load(RING_CLIENT_INSTALL "/share/libringclient/translations/lrc_" + locale_lang)) {
499 g_debug("installed translations for %s", locale_lang.toUtf8().constData());
500 priv->qtapp->installTranslator(priv->translator_lang.get());
501 }
502 }
503
504 priv->translator_full.reset(new QTranslator);
505 if (priv->translator_full->load(RING_CLIENT_INSTALL "/share/libringclient/translations/lrc_" + locale_name)) {
506 g_debug("installed translations for %s", locale_name.toUtf8().constData());
507 }
508
509 if (not priv->translator_lang and not priv->translator_full) {
Stepan Salenikovich67c5dd32015-09-14 12:27:36 -0400510 g_debug("could not load LRC translations for %s, %s",
Guillaume Roguez884ef732017-07-14 12:53:35 -0400511 QLocale::languageToString(QLocale::system().language()).toUtf8().constData(),
512 QLocale::countryToString(QLocale::system().country()).toUtf8().constData()
Stepan Salenikovich67c5dd32015-09-14 12:27:36 -0400513 );
514 }
515
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400516 /* init delegates */
Stepan Salenikovichbbd6c132015-08-20 15:21:48 -0400517 GlobalInstances::setPixmapManipulator(std::unique_ptr<Interfaces::PixbufManipulator>(new Interfaces::PixbufManipulator()));
Stepan Salenikovichbe6550c2015-08-21 16:16:03 -0400518 GlobalInstances::setDBusErrorHandler(std::unique_ptr<Interfaces::DBusErrorHandler>(new Interfaces::DBusErrorHandler()));
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400519
Stepan Salenikovich75a39172015-07-10 13:21:08 -0400520 /* make sure all RING accounts have a display name... this basically makes sure
521 * that all accounts created before the display name patch have a display name
522 * set... a bit of a hack as this should maybe be done in LRC */
523 force_ring_display_name();
524
Stepan Salenikovichf2d76c52015-07-17 17:54:56 -0400525 /* make sure basic number categories exist, in case user has no contacts
526 * from which these would be automatically created
527 */
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400528 NumberCategoryModel::instance().addCategory("work", QVariant());
529 NumberCategoryModel::instance().addCategory("home", QVariant());
Stepan Salenikovichf2d76c52015-07-17 17:54:56 -0400530
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400531 /* add backends */
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400532 CategorizedHistoryModel::instance().addCollection<LocalHistoryCollection>(LoadOptions::FORCE_ENABLED);
Nicolas Jagerb413b302016-05-06 11:41:32 -0400533 PersonModel::instance().addCollection<PeerProfileCollection>(LoadOptions::FORCE_ENABLED);
534 ProfileModel::instance().addCollection<LocalProfileCollection>(LoadOptions::FORCE_ENABLED);
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400535
Stepan Salenikovichf2d76c52015-07-17 17:54:56 -0400536 /* fallback backend for vcards */
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400537 PersonModel::instance().addCollection<FallbackPersonCollection>(LoadOptions::FORCE_ENABLED);
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400538
Stepan Salenikovichf2d76c52015-07-17 17:54:56 -0400539 /* EDS backend(s) */
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400540 load_eds_sources(priv->cancellable);
541
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400542 /* Override theme since we don't have appropriate icons for a dark them (yet) */
543 GtkSettings *gtk_settings = gtk_settings_get_default();
544 g_object_set(G_OBJECT(gtk_settings), "gtk-application-prefer-dark-theme",
545 FALSE, NULL);
546 /* enable button icons */
547 g_object_set(G_OBJECT(gtk_settings), "gtk-button-images",
548 TRUE, NULL);
549
Nicolas Jager655b8db2017-12-15 14:04:32 -0500550 /* enable sound (for notification) */
551 g_object_set(G_OBJECT(gtk_settings), "gtk-enable-event-sounds",
552 TRUE, NULL);
553
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400554 /* add GActions */
Stepan Salenikovich69771842015-02-24 18:11:45 -0500555 g_action_map_add_action_entries(
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400556 G_ACTION_MAP(app), ring_actions, G_N_ELEMENTS(ring_actions), app);
557
Stepan Salenikovich982b2882016-06-15 13:13:37 -0400558 /* GActions for settings */
559 auto action_window_visible = g_settings_create_action(priv->settings, "show-main-window");
560 g_action_map_add_action(G_ACTION_MAP(app), action_window_visible);
561
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400562 /* add accelerators */
563 ring_accelerators(RING_CLIENT(app));
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500564
Stepan Salenikovichbc6c4be2015-07-31 16:07:54 -0400565 /* show window on incoming calls (if the option is set)*/
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400566 QObject::connect(&CallModel::instance(), &CallModel::incomingCall,
Stepan Salenikovichbc6c4be2015-07-31 16:07:54 -0400567 [app] (G_GNUC_UNUSED Call *call) {
568 RingClient *client = RING_CLIENT(app);
569 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
570 if (g_settings_get_boolean(priv->settings, "bring-window-to-front"))
Stepan Salenikovichbb9c24e2015-09-15 09:55:02 -0400571 ring_window_show(client);
Stepan Salenikovichbc6c4be2015-07-31 16:07:54 -0400572 }
573 );
Stepan Salenikovich472c9052016-07-20 19:16:02 -0400574#if USE_LIBNM
575 /* monitor the network using libnm to notify the daemon about connectivity chagnes */
576 nm_client_new_async(priv->cancellable, (GAsyncReadyCallback)nm_client_cb, client);
577#endif
578
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400579 G_APPLICATION_CLASS(ring_client_parent_class)->startup(app);
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400580}
581
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400582static void
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500583ring_client_shutdown(GApplication *app)
584{
585 RingClient *self = RING_CLIENT(app);
586 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(self);
587
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400588 g_debug("quitting");
589
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400590 /* cancel any pending cancellable operations */
591 g_cancellable_cancel(priv->cancellable);
592 g_object_unref(priv->cancellable);
593
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400594 QObject::disconnect(priv->uam_updated);
595
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500596 /* free the QCoreApplication, which will destroy all libRingClient models
597 * and thus send the Unregister signal over dbus to dring */
Nicolas Jagerfad674f2017-10-19 11:35:36 -0400598 if (priv->qtapp) {
599 delete priv->qtapp;
600 priv->qtapp = nullptr;
601 }
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500602
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400603 /* free the copied cmd line args */
604 g_strfreev(priv->argv);
605
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -0400606 g_clear_object(&priv->settings);
607
Stepan Salenikovich472c9052016-07-20 19:16:02 -0400608#if USE_LIBNM
609 /* clear NetworkManager client if it was used */
610 g_clear_object(&priv->nm_client);
611#endif
612
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500613 /* Chain up to the parent class */
614 G_APPLICATION_CLASS(ring_client_parent_class)->shutdown(app);
615}
616
617static void
618ring_client_init(RingClient *self)
619{
620 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(self);
621
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500622 priv->win = NULL;
623 priv->qtapp = NULL;
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400624 priv->cancellable = g_cancellable_new();
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -0400625 priv->settings = g_settings_new_full(get_ring_schema(), NULL, NULL);
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400626
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400627 /* add custom cmd line options */
628 ring_client_add_options(G_APPLICATION(self));
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500629}
630
631static void
632ring_client_class_init(RingClientClass *klass)
633{
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400634 G_APPLICATION_CLASS(klass)->startup = ring_client_startup;
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400635 G_APPLICATION_CLASS(klass)->activate = ring_client_activate;
Victor Nikulshin167bbb62017-03-15 20:44:27 +0000636 G_APPLICATION_CLASS(klass)->open = ring_client_open;
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500637 G_APPLICATION_CLASS(klass)->shutdown = ring_client_shutdown;
638}
639
Sébastien Blin55bff9d2017-10-03 15:15:23 -0400640RingClient*
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400641ring_client_new(int argc, char *argv[])
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500642{
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400643 RingClient *client = (RingClient *)g_object_new(ring_client_get_type(),
Stepan Salenikovich76350582015-05-25 14:56:32 -0400644 "application-id", RING_CLIENT_APP_ID,
Victor Nikulshin167bbb62017-03-15 20:44:27 +0000645 "flags", G_APPLICATION_HANDLES_OPEN ,
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400646 NULL);
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400647
648 /* copy the cmd line args before they get processed by the GApplication*/
649 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
650 priv->argc = argc;
651 priv->argv = g_strdupv((gchar **)argv);
652
653 return client;
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500654}
Stepan Salenikovich67112d12015-06-16 16:57:06 -0400655
Sébastien Blin55bff9d2017-10-03 15:15:23 -0400656GtkWindow*
Stepan Salenikovichbe6550c2015-08-21 16:16:03 -0400657ring_client_get_main_window(RingClient *client)
Stepan Salenikovich67112d12015-06-16 16:57:06 -0400658{
659 g_return_val_if_fail(IS_RING_CLIENT(client), NULL);
660 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
661
662 return (GtkWindow *)priv->win;
663}
Stepan Salenikovichbb9c24e2015-09-15 09:55:02 -0400664
665void
666ring_client_set_restore_main_window_state(RingClient *client, gboolean restore)
667{
668 g_return_if_fail(IS_RING_CLIENT(client));
669 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
670
671 priv->restore_window_state = restore;
672}