blob: 219961ab75ef74d27d3d306276b3a0fcb29ce1c9 [file] [log] [blame]
Stepan Salenikovichd81ef292015-02-17 18:47:37 -05001/*
Guillaume Roguez2a6150d2017-07-19 18:24:47 -04002 * Copyright (C) 2015-2017 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 Salenikovich67112d12015-06-16 16:57:06 -040046#include <media/text.h>
Stepan Salenikovichf2d76c52015-07-17 17:54:56 -040047#include <numbercategorymodel.h>
Stepan Salenikovichbbd6c132015-08-20 15:21:48 -040048#include <globalinstances.h>
Stepan Salenikovich472c9052016-07-20 19:16:02 -040049#include <profilemodel.h>
50#include <profile.h>
51#include <peerprofilecollection.h>
52#include <localprofilecollection.h>
53#include <accountmodel.h>
Olivier Gregoire66e4df72016-06-17 18:39:05 -040054#include <smartinfohub.h>
Stepan Salenikovich5a127672016-09-13 11:19:50 -040055#include <media/textrecording.h>
56#include <media/recordingmodel.h>
Stepan Salenikovich509670b2017-04-24 15:18:06 -040057#include <availableaccountmodel.h>
Stepan Salenikovichd81ef292015-02-17 18:47:37 -050058
Stepan Salenikovich472c9052016-07-20 19:16:02 -040059// Ring client
Stepan Salenikovichd81ef292015-02-17 18:47:37 -050060#include "ring_client_options.h"
Stepan Salenikovich434b88f2015-02-19 17:49:08 -050061#include "ringmainwindow.h"
Stepan Salenikovich24dcd5c2015-03-26 17:40:34 -040062#include "dialogs.h"
Stepan Salenikovich6f687072015-03-26 10:43:37 -040063#include "backends/edscontactbackend.h"
Stepan Salenikovichbbd6c132015-08-20 15:21:48 -040064#include "native/pixbufmanipulator.h"
Stepan Salenikovichbe6550c2015-08-21 16:16:03 -040065#include "native/dbuserrorhandler.h"
Stepan Salenikovich76c33e62015-05-22 12:24:07 -040066#include "ringnotify.h"
Stepan Salenikovich76350582015-05-25 14:56:32 -040067#include "config.h"
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -040068#include "utils/files.h"
Stepan Salenikovich4f9bb982015-06-23 14:26:30 -040069#include "revision.h"
Stepan Salenikovich75a39172015-07-10 13:21:08 -040070#include "utils/accounts.h"
Victor Nikulshin167bbb62017-03-15 20:44:27 +000071#include "utils/calling.h"
Stepan Salenikovich434b88f2015-02-19 17:49:08 -050072
Stepan Salenikovich72d812f2017-01-05 12:16:25 -050073#if 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;
125 QMetaObject::Connection chat_notification;
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500126};
127
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400128/* this union is used to pass ints as pointers and vice versa for GAction parameters*/
129typedef union _int_ptr_t
130{
131 int value;
132 gint64 value64;
133 gpointer ptr;
134} int_ptr_t;
135
Stepan Salenikovich434b88f2015-02-19 17:49:08 -0500136G_DEFINE_TYPE_WITH_PRIVATE(RingClient, ring_client, GTK_TYPE_APPLICATION);
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500137
138#define RING_CLIENT_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), RING_CLIENT_TYPE, RingClientPrivate))
139
140static void
Stepan Salenikovich541a40a2016-08-17 11:33:47 -0400141exception_dialog(const char* msg)
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500142{
Stepan Salenikovich541a40a2016-08-17 11:33:47 -0400143 g_critical("%s", msg);
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500144 GtkWidget *dialog = gtk_message_dialog_new(NULL,
145 (GtkDialogFlags)(GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT),
146 GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
147 _("Unable to initialize.\nMake sure the Ring daemon (dring) is running.\nError: %s"),
148 msg);
149
150 gtk_window_set_title(GTK_WINDOW(dialog), _("Ring Error"));
151 gtk_dialog_run(GTK_DIALOG(dialog));
152 gtk_widget_destroy(dialog);
153}
154
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400155static void
156ring_accelerators(RingClient *client)
157{
Stepan Salenikovich39ee49a2015-03-24 12:44:55 -0400158#if GTK_CHECK_VERSION(3,12,0)
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400159 const gchar *quit_accels[2] = { "<Ctrl>Q", NULL };
160 gtk_application_set_accels_for_action(GTK_APPLICATION(client), "app.quit", quit_accels);
161#else
Stepan Salenikovich870d7392015-09-08 11:18:46 -0400162 gtk_application_add_accelerator(GTK_APPLICATION(client), "<Control>Q", "app.quit", NULL);
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400163#endif
164}
165
166static void
167action_quit(G_GNUC_UNUSED GSimpleAction *simple,
168 G_GNUC_UNUSED GVariant *parameter,
169 gpointer user_data)
170{
171 g_return_if_fail(G_IS_APPLICATION(user_data));
172
173#if GLIB_CHECK_VERSION(2,32,0)
174 g_application_quit(G_APPLICATION(user_data));
175#else
176 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(user_data);
177 gtk_widget_destroy(priv->win);
178#endif
179}
180
Stepan Salenikovich24dcd5c2015-03-26 17:40:34 -0400181static void
182action_about(G_GNUC_UNUSED GSimpleAction *simple,
Stepan Salenikovich982b2882016-06-15 13:13:37 -0400183 G_GNUC_UNUSED GVariant *parameter,
184 gpointer user_data)
Stepan Salenikovich24dcd5c2015-03-26 17:40:34 -0400185{
186 g_return_if_fail(G_IS_APPLICATION(user_data));
187 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(user_data);
188
189 ring_about_dialog(priv->win);
190}
191
Olivier Gregoire66e4df72016-06-17 18:39:05 -0400192static void
193toggle_smartinfo(GSimpleAction *action, GVariant *parameter, gpointer)
194{
195 g_simple_action_set_state(action, parameter);
196 if (g_variant_get_boolean(parameter)) {
197 SmartInfoHub::instance().start();
198 } else {
199 SmartInfoHub::instance().stop();
200 }
201}
202
Stepan Salenikovich69771842015-02-24 18:11:45 -0500203static const GActionEntry ring_actions[] =
204{
Olivier Gregoire66e4df72016-06-17 18:39:05 -0400205 { "accept", NULL, NULL, NULL, NULL, {0} },
206 { "hangup", NULL, NULL, NULL, NULL, {0} },
207 { "hold", NULL, NULL, "false", NULL, {0} },
208 { "quit", action_quit, NULL, NULL, NULL, {0} },
209 { "about", action_about, NULL, NULL, NULL, {0} },
210 { "mute_audio", NULL, NULL, "false", NULL, {0} },
211 { "mute_video", NULL, NULL, "false", NULL, {0} },
212 { "record", NULL, NULL, "false", NULL, {0} },
213 { "display-smartinfo", NULL, NULL, "false", toggle_smartinfo, {0} },
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500214 /* TODO implement the other actions */
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500215 // { "transfer", NULL, NULL, "flase", NULL, {0} },
Stepan Salenikovich69771842015-02-24 18:11:45 -0500216};
217
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500218static void
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -0400219autostart_toggled(GSettings *settings, G_GNUC_UNUSED gchar *key, G_GNUC_UNUSED gpointer user_data)
220{
221 autostart_symlink(g_settings_get_boolean(settings, "start-on-login"));
222}
223
Stepan Salenikovich982b2882016-06-15 13:13:37 -0400224static void
225show_main_window_toggled(RingClient *client)
226{
227 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
228
229 if (g_settings_get_boolean(priv->settings, "show-main-window")) {
230 gtk_window_present(GTK_WINDOW(priv->win));
231 } else {
232 gtk_widget_hide(priv->win);
233 }
234}
235
Stepan Salenikovichbb9c24e2015-09-15 09:55:02 -0400236static void
237ring_window_show(RingClient *client)
238{
Stepan Salenikovichbb9c24e2015-09-15 09:55:02 -0400239 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
Stepan Salenikovich982b2882016-06-15 13:13:37 -0400240 g_settings_set_boolean(priv->settings, "show-main-window", TRUE);
Stepan Salenikovichbb9c24e2015-09-15 09:55:02 -0400241}
242
243static void
244ring_window_hide(RingClient *client)
245{
Stepan Salenikovichbb9c24e2015-09-15 09:55:02 -0400246 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
Stepan Salenikovich982b2882016-06-15 13:13:37 -0400247 g_settings_set_boolean(priv->settings, "show-main-window", FALSE);
Stepan Salenikovichbb9c24e2015-09-15 09:55:02 -0400248}
249
Stepan Salenikovichbc6c4be2015-07-31 16:07:54 -0400250static gboolean
251on_close_window(GtkWidget *window, G_GNUC_UNUSED GdkEvent *event, RingClient *client)
252{
253 g_return_val_if_fail(GTK_IS_WINDOW(window) && IS_RING_CLIENT(client), FALSE);
Stepan Salenikovichbc6c4be2015-07-31 16:07:54 -0400254 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
255
Stepan Salenikovich982b2882016-06-15 13:13:37 -0400256 if (g_settings_get_boolean(priv->settings, "show-status-icon")) {
Stepan Salenikovichbc6c4be2015-07-31 16:07:54 -0400257 /* we want to simply hide the window and keep the client running */
Stepan Salenikovichbb9c24e2015-09-15 09:55:02 -0400258 ring_window_hide(client);
Stepan Salenikovichbc6c4be2015-07-31 16:07:54 -0400259 return TRUE; /* do not propogate event */
260 } else {
261 /* we want to quit the application, so just propogate the event */
262 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
477chat_notifications_toggled(RingClient *self)
478{
479 auto priv = RING_CLIENT_GET_PRIVATE(self);
480
481 if (g_settings_get_boolean(priv->settings, "enable-chat-notifications")) {
482 priv->chat_notification = QObject::connect(
483 &Media::RecordingModel::instance(),
484 &Media::RecordingModel::newTextMessage,
485 [self] (Media::TextRecording* t, ContactMethod* cm)
486 { ring_notify_message(cm, t, self); }
487 );
488 } else {
489 QObject::disconnect(priv->chat_notification);
490 }
491}
492
493static void
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400494ring_client_startup(GApplication *app)
Stepan Salenikovich69771842015-02-24 18:11:45 -0500495{
Sébastien Blin55bff9d2017-10-03 15:15:23 -0400496 // TODO still use old LRC models, in the future, we will init the LRC here.
Stepan Salenikovich69771842015-02-24 18:11:45 -0500497 RingClient *client = RING_CLIENT(app);
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400498 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
Stepan Salenikovich69771842015-02-24 18:11:45 -0500499
Stepan Salenikovich4f9bb982015-06-23 14:26:30 -0400500 g_message("Ring GNOME client version: %d.%d.%d", VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH);
501 g_message("git ref: %s", RING_CLIENT_REVISION);
502
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -0400503 /* make sure that the system corresponds to the autostart setting */
504 autostart_symlink(g_settings_get_boolean(priv->settings, "start-on-login"));
505 g_signal_connect(priv->settings, "changed::start-on-login", G_CALLBACK(autostart_toggled), NULL);
506
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400507 /* init clutter */
508 int clutter_error;
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400509 if ((clutter_error = gtk_clutter_init(&priv->argc, &priv->argv)) != CLUTTER_INIT_SUCCESS) {
510 g_error("Could not init clutter : %d\n", clutter_error);
511 exit(1); /* the g_error above should normally cause the applicaiton to exit */
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400512 }
513
514 /* init libRingClient and make sure its connected to the dbus */
515 try {
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400516 priv->qtapp = new QCoreApplication(priv->argc, priv->argv);
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400517 /* the call model will try to connect to dring via dbus */
518 CallModel::instance();
Stepan Salenikovich541a40a2016-08-17 11:33:47 -0400519 } catch(const char * msg) {
520 exception_dialog(msg);
521 exit(1);
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400522 } catch(QString& msg) {
Stepan Salenikovich541a40a2016-08-17 11:33:47 -0400523 exception_dialog(msg.toLocal8Bit().constData());
524 exit(1);
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400525 }
526
Stepan Salenikovich67c5dd32015-09-14 12:27:36 -0400527 /* load translations from LRC */
Guillaume Roguez884ef732017-07-14 12:53:35 -0400528 const auto locale_name = QLocale::system().name();
529 const auto locale_lang = locale_name.split('_')[0];
530
531 if (locale_name != locale_lang) {
532 /* Install language first to have lowest priority */
533 priv->translator_lang.reset(new QTranslator);
534 if (priv->translator_lang->load(RING_CLIENT_INSTALL "/share/libringclient/translations/lrc_" + locale_lang)) {
535 g_debug("installed translations for %s", locale_lang.toUtf8().constData());
536 priv->qtapp->installTranslator(priv->translator_lang.get());
537 }
538 }
539
540 priv->translator_full.reset(new QTranslator);
541 if (priv->translator_full->load(RING_CLIENT_INSTALL "/share/libringclient/translations/lrc_" + locale_name)) {
542 g_debug("installed translations for %s", locale_name.toUtf8().constData());
543 }
544
545 if (not priv->translator_lang and not priv->translator_full) {
Stepan Salenikovich67c5dd32015-09-14 12:27:36 -0400546 g_debug("could not load LRC translations for %s, %s",
Guillaume Roguez884ef732017-07-14 12:53:35 -0400547 QLocale::languageToString(QLocale::system().language()).toUtf8().constData(),
548 QLocale::countryToString(QLocale::system().country()).toUtf8().constData()
Stepan Salenikovich67c5dd32015-09-14 12:27:36 -0400549 );
550 }
551
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400552 /* init delegates */
Stepan Salenikovichbbd6c132015-08-20 15:21:48 -0400553 GlobalInstances::setPixmapManipulator(std::unique_ptr<Interfaces::PixbufManipulator>(new Interfaces::PixbufManipulator()));
Stepan Salenikovichbe6550c2015-08-21 16:16:03 -0400554 GlobalInstances::setDBusErrorHandler(std::unique_ptr<Interfaces::DBusErrorHandler>(new Interfaces::DBusErrorHandler()));
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400555
Stepan Salenikovich75a39172015-07-10 13:21:08 -0400556 /* make sure all RING accounts have a display name... this basically makes sure
557 * that all accounts created before the display name patch have a display name
558 * set... a bit of a hack as this should maybe be done in LRC */
559 force_ring_display_name();
560
Stepan Salenikovichf2d76c52015-07-17 17:54:56 -0400561 /* make sure basic number categories exist, in case user has no contacts
562 * from which these would be automatically created
563 */
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400564 NumberCategoryModel::instance().addCategory("work", QVariant());
565 NumberCategoryModel::instance().addCategory("home", QVariant());
Stepan Salenikovichf2d76c52015-07-17 17:54:56 -0400566
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400567 /* add backends */
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400568 CategorizedHistoryModel::instance().addCollection<LocalHistoryCollection>(LoadOptions::FORCE_ENABLED);
Nicolas Jagerb413b302016-05-06 11:41:32 -0400569 PersonModel::instance().addCollection<PeerProfileCollection>(LoadOptions::FORCE_ENABLED);
570 ProfileModel::instance().addCollection<LocalProfileCollection>(LoadOptions::FORCE_ENABLED);
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400571
Stepan Salenikovichf2d76c52015-07-17 17:54:56 -0400572 /* fallback backend for vcards */
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400573 PersonModel::instance().addCollection<FallbackPersonCollection>(LoadOptions::FORCE_ENABLED);
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400574
Stepan Salenikovichf2d76c52015-07-17 17:54:56 -0400575 /* EDS backend(s) */
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400576 load_eds_sources(priv->cancellable);
577
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400578 /* Override theme since we don't have appropriate icons for a dark them (yet) */
579 GtkSettings *gtk_settings = gtk_settings_get_default();
580 g_object_set(G_OBJECT(gtk_settings), "gtk-application-prefer-dark-theme",
581 FALSE, NULL);
582 /* enable button icons */
583 g_object_set(G_OBJECT(gtk_settings), "gtk-button-images",
584 TRUE, NULL);
585
Nicolas Jager655b8db2017-12-15 14:04:32 -0500586 /* enable sound (for notification) */
587 g_object_set(G_OBJECT(gtk_settings), "gtk-enable-event-sounds",
588 TRUE, NULL);
589
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400590 /* add GActions */
Stepan Salenikovich69771842015-02-24 18:11:45 -0500591 g_action_map_add_action_entries(
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400592 G_ACTION_MAP(app), ring_actions, G_N_ELEMENTS(ring_actions), app);
593
Stepan Salenikovich982b2882016-06-15 13:13:37 -0400594 /* GActions for settings */
595 auto action_window_visible = g_settings_create_action(priv->settings, "show-main-window");
596 g_action_map_add_action(G_ACTION_MAP(app), action_window_visible);
597
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400598 /* add accelerators */
599 ring_accelerators(RING_CLIENT(app));
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500600
Stepan Salenikovichbc6c4be2015-07-31 16:07:54 -0400601 /* show window on incoming calls (if the option is set)*/
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400602 QObject::connect(&CallModel::instance(), &CallModel::incomingCall,
Stepan Salenikovichbc6c4be2015-07-31 16:07:54 -0400603 [app] (G_GNUC_UNUSED Call *call) {
604 RingClient *client = RING_CLIENT(app);
605 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
606 if (g_settings_get_boolean(priv->settings, "bring-window-to-front"))
Stepan Salenikovichbb9c24e2015-09-15 09:55:02 -0400607 ring_window_show(client);
Stepan Salenikovichbc6c4be2015-07-31 16:07:54 -0400608 }
609 );
610
Stepan Salenikovich5a127672016-09-13 11:19:50 -0400611 /* enable notifications based on settings */
Stepan Salenikovich76c33e62015-05-22 12:24:07 -0400612 ring_notify_init();
Stepan Salenikovich5a127672016-09-13 11:19:50 -0400613 call_notifications_toggled(client);
614 chat_notifications_toggled(client);
615 g_signal_connect_swapped(priv->settings, "changed::enable-call-notifications", G_CALLBACK(call_notifications_toggled), client);
616 g_signal_connect_swapped(priv->settings, "changed::enable-chat-notifications", G_CALLBACK(chat_notifications_toggled), client);
Stepan Salenikovich67112d12015-06-16 16:57:06 -0400617
Stepan Salenikovich472c9052016-07-20 19:16:02 -0400618#if USE_LIBNM
619 /* monitor the network using libnm to notify the daemon about connectivity chagnes */
620 nm_client_new_async(priv->cancellable, (GAsyncReadyCallback)nm_client_cb, client);
621#endif
622
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400623 G_APPLICATION_CLASS(ring_client_parent_class)->startup(app);
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400624}
625
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400626static void
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500627ring_client_shutdown(GApplication *app)
628{
629 RingClient *self = RING_CLIENT(app);
630 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(self);
631
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400632 g_debug("quitting");
633
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400634 /* cancel any pending cancellable operations */
635 g_cancellable_cancel(priv->cancellable);
636 g_object_unref(priv->cancellable);
637
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400638 QObject::disconnect(priv->uam_updated);
Stepan Salenikovich5a127672016-09-13 11:19:50 -0400639 QObject::disconnect(priv->call_notification);
640 QObject::disconnect(priv->chat_notification);
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400641
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500642 /* free the QCoreApplication, which will destroy all libRingClient models
643 * and thus send the Unregister signal over dbus to dring */
Nicolas Jagerfad674f2017-10-19 11:35:36 -0400644 if (priv->qtapp) {
645 delete priv->qtapp;
646 priv->qtapp = nullptr;
647 }
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500648
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400649 /* free the copied cmd line args */
650 g_strfreev(priv->argv);
651
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -0400652 g_clear_object(&priv->settings);
653
Stepan Salenikovich76c33e62015-05-22 12:24:07 -0400654 ring_notify_uninit();
655
Stepan Salenikovich472c9052016-07-20 19:16:02 -0400656#if USE_LIBNM
657 /* clear NetworkManager client if it was used */
658 g_clear_object(&priv->nm_client);
659#endif
660
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500661 /* Chain up to the parent class */
662 G_APPLICATION_CLASS(ring_client_parent_class)->shutdown(app);
663}
664
665static void
666ring_client_init(RingClient *self)
667{
668 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(self);
669
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500670 priv->win = NULL;
671 priv->qtapp = NULL;
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400672 priv->cancellable = g_cancellable_new();
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -0400673 priv->settings = g_settings_new_full(get_ring_schema(), NULL, NULL);
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400674
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400675 /* add custom cmd line options */
676 ring_client_add_options(G_APPLICATION(self));
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500677}
678
679static void
680ring_client_class_init(RingClientClass *klass)
681{
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400682 G_APPLICATION_CLASS(klass)->startup = ring_client_startup;
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400683 G_APPLICATION_CLASS(klass)->activate = ring_client_activate;
Victor Nikulshin167bbb62017-03-15 20:44:27 +0000684 G_APPLICATION_CLASS(klass)->open = ring_client_open;
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500685 G_APPLICATION_CLASS(klass)->shutdown = ring_client_shutdown;
686}
687
Sébastien Blin55bff9d2017-10-03 15:15:23 -0400688RingClient*
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400689ring_client_new(int argc, char *argv[])
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500690{
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400691 RingClient *client = (RingClient *)g_object_new(ring_client_get_type(),
Stepan Salenikovich76350582015-05-25 14:56:32 -0400692 "application-id", RING_CLIENT_APP_ID,
Victor Nikulshin167bbb62017-03-15 20:44:27 +0000693 "flags", G_APPLICATION_HANDLES_OPEN ,
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400694 NULL);
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400695
696 /* copy the cmd line args before they get processed by the GApplication*/
697 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
698 priv->argc = argc;
699 priv->argv = g_strdupv((gchar **)argv);
700
701 return client;
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500702}
Stepan Salenikovich67112d12015-06-16 16:57:06 -0400703
Sébastien Blin55bff9d2017-10-03 15:15:23 -0400704GtkWindow*
Stepan Salenikovichbe6550c2015-08-21 16:16:03 -0400705ring_client_get_main_window(RingClient *client)
Stepan Salenikovich67112d12015-06-16 16:57:06 -0400706{
707 g_return_val_if_fail(IS_RING_CLIENT(client), NULL);
708 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
709
710 return (GtkWindow *)priv->win;
711}
Stepan Salenikovichbb9c24e2015-09-15 09:55:02 -0400712
713void
714ring_client_set_restore_main_window_state(RingClient *client, gboolean restore)
715{
716 g_return_if_fail(IS_RING_CLIENT(client));
717 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
718
719 priv->restore_window_state = restore;
720}