blob: c1b5233743add84dc88d4ebf8b48ed5861c02ee6 [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 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
Stepan Salenikovich72d812f2017-01-05 12:16:25 -050071#if HAVE_APPINDICATOR
Stepan Salenikovich982b2882016-06-15 13:13:37 -040072#include <libappindicator/app-indicator.h>
73#endif
74
Stepan Salenikovich472c9052016-07-20 19:16:02 -040075#if USE_LIBNM
Lubomir Rintelabc26c32017-12-19 16:26:45 +010076#include <NetworkManager.h>
Stepan Salenikovich472c9052016-07-20 19:16:02 -040077#endif
78
Stepan Salenikovich434b88f2015-02-19 17:49:08 -050079struct _RingClientClass
80{
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -040081 GtkApplicationClass parent_class;
Stepan Salenikovich434b88f2015-02-19 17:49:08 -050082};
83
84struct _RingClient
85{
86 GtkApplication parent;
87};
88
89typedef struct _RingClientPrivate RingClientPrivate;
Stepan Salenikovichd81ef292015-02-17 18:47:37 -050090
91struct _RingClientPrivate {
Stepan Salenikovich0d515e52015-05-19 16:31:05 -040092 /* args */
93 int argc;
94 char **argv;
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -040095
96 GSettings *settings;
97
Stepan Salenikovichd81ef292015-02-17 18:47:37 -050098 /* main window */
99 GtkWidget *win;
100 /* for libRingclient */
101 QCoreApplication *qtapp;
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400102 /* UAM */
103 QMetaObject::Connection uam_updated;
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400104
Guillaume Roguez884ef732017-07-14 12:53:35 -0400105 std::unique_ptr<QTranslator> translator_lang;
106 std::unique_ptr<QTranslator> translator_full;
Stepan Salenikovich6c39b102015-09-15 14:24:45 -0400107
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400108 GCancellable *cancellable;
Stepan Salenikovichbb9c24e2015-09-15 09:55:02 -0400109
110 gboolean restore_window_state;
Stepan Salenikovich982b2882016-06-15 13:13:37 -0400111
112 gpointer systray_icon;
113 GtkWidget *icon_menu;
Stepan Salenikovich472c9052016-07-20 19:16:02 -0400114
115#if USE_LIBNM
116 /* NetworkManager */
117 NMClient *nm_client;
Stepan Salenikovich221beae2016-10-12 11:29:56 -0400118 NMActiveConnection *primary_connection;
Stepan Salenikovich472c9052016-07-20 19:16:02 -0400119#endif
Stepan Salenikovich5a127672016-09-13 11:19:50 -0400120
121 /* notifications */
122 QMetaObject::Connection call_notification;
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500123};
124
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400125/* this union is used to pass ints as pointers and vice versa for GAction parameters*/
126typedef union _int_ptr_t
127{
128 int value;
129 gint64 value64;
130 gpointer ptr;
131} int_ptr_t;
132
Stepan Salenikovich434b88f2015-02-19 17:49:08 -0500133G_DEFINE_TYPE_WITH_PRIVATE(RingClient, ring_client, GTK_TYPE_APPLICATION);
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500134
135#define RING_CLIENT_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), RING_CLIENT_TYPE, RingClientPrivate))
136
137static void
Stepan Salenikovich541a40a2016-08-17 11:33:47 -0400138exception_dialog(const char* msg)
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500139{
Stepan Salenikovich541a40a2016-08-17 11:33:47 -0400140 g_critical("%s", msg);
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500141 GtkWidget *dialog = gtk_message_dialog_new(NULL,
142 (GtkDialogFlags)(GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT),
143 GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
144 _("Unable to initialize.\nMake sure the Ring daemon (dring) is running.\nError: %s"),
145 msg);
146
147 gtk_window_set_title(GTK_WINDOW(dialog), _("Ring Error"));
148 gtk_dialog_run(GTK_DIALOG(dialog));
149 gtk_widget_destroy(dialog);
150}
151
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400152static void
153ring_accelerators(RingClient *client)
154{
Stepan Salenikovich39ee49a2015-03-24 12:44:55 -0400155#if GTK_CHECK_VERSION(3,12,0)
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400156 const gchar *quit_accels[2] = { "<Ctrl>Q", NULL };
157 gtk_application_set_accels_for_action(GTK_APPLICATION(client), "app.quit", quit_accels);
158#else
Stepan Salenikovich870d7392015-09-08 11:18:46 -0400159 gtk_application_add_accelerator(GTK_APPLICATION(client), "<Control>Q", "app.quit", NULL);
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400160#endif
161}
162
163static void
164action_quit(G_GNUC_UNUSED GSimpleAction *simple,
165 G_GNUC_UNUSED GVariant *parameter,
166 gpointer user_data)
167{
168 g_return_if_fail(G_IS_APPLICATION(user_data));
169
170#if GLIB_CHECK_VERSION(2,32,0)
171 g_application_quit(G_APPLICATION(user_data));
172#else
173 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(user_data);
174 gtk_widget_destroy(priv->win);
175#endif
176}
177
Stepan Salenikovich24dcd5c2015-03-26 17:40:34 -0400178static void
179action_about(G_GNUC_UNUSED GSimpleAction *simple,
Stepan Salenikovich982b2882016-06-15 13:13:37 -0400180 G_GNUC_UNUSED GVariant *parameter,
181 gpointer user_data)
Stepan Salenikovich24dcd5c2015-03-26 17:40:34 -0400182{
183 g_return_if_fail(G_IS_APPLICATION(user_data));
184 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(user_data);
185
186 ring_about_dialog(priv->win);
187}
188
Olivier Gregoire66e4df72016-06-17 18:39:05 -0400189static void
190toggle_smartinfo(GSimpleAction *action, GVariant *parameter, gpointer)
191{
192 g_simple_action_set_state(action, parameter);
193 if (g_variant_get_boolean(parameter)) {
194 SmartInfoHub::instance().start();
195 } else {
196 SmartInfoHub::instance().stop();
197 }
198}
199
Stepan Salenikovich69771842015-02-24 18:11:45 -0500200static const GActionEntry ring_actions[] =
201{
Olivier Gregoire66e4df72016-06-17 18:39:05 -0400202 { "accept", NULL, NULL, NULL, NULL, {0} },
203 { "hangup", NULL, NULL, NULL, NULL, {0} },
204 { "hold", NULL, NULL, "false", NULL, {0} },
205 { "quit", action_quit, NULL, NULL, NULL, {0} },
206 { "about", action_about, NULL, NULL, NULL, {0} },
207 { "mute_audio", NULL, NULL, "false", NULL, {0} },
208 { "mute_video", NULL, NULL, "false", NULL, {0} },
209 { "record", NULL, NULL, "false", NULL, {0} },
210 { "display-smartinfo", NULL, NULL, "false", toggle_smartinfo, {0} },
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500211 /* TODO implement the other actions */
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500212 // { "transfer", NULL, NULL, "flase", NULL, {0} },
Stepan Salenikovich69771842015-02-24 18:11:45 -0500213};
214
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500215static void
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -0400216autostart_toggled(GSettings *settings, G_GNUC_UNUSED gchar *key, G_GNUC_UNUSED gpointer user_data)
217{
218 autostart_symlink(g_settings_get_boolean(settings, "start-on-login"));
219}
220
Stepan Salenikovich982b2882016-06-15 13:13:37 -0400221static void
222show_main_window_toggled(RingClient *client)
223{
224 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
225
226 if (g_settings_get_boolean(priv->settings, "show-main-window")) {
227 gtk_window_present(GTK_WINDOW(priv->win));
228 } else {
229 gtk_widget_hide(priv->win);
230 }
231}
232
Stepan Salenikovichbb9c24e2015-09-15 09:55:02 -0400233static void
234ring_window_show(RingClient *client)
235{
Stepan Salenikovichbb9c24e2015-09-15 09:55:02 -0400236 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
Stepan Salenikovich982b2882016-06-15 13:13:37 -0400237 g_settings_set_boolean(priv->settings, "show-main-window", TRUE);
Stepan Salenikovichbb9c24e2015-09-15 09:55:02 -0400238}
239
240static void
241ring_window_hide(RingClient *client)
242{
Stepan Salenikovichbb9c24e2015-09-15 09:55:02 -0400243 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
Stepan Salenikovich982b2882016-06-15 13:13:37 -0400244 g_settings_set_boolean(priv->settings, "show-main-window", FALSE);
Stepan Salenikovichbb9c24e2015-09-15 09:55:02 -0400245}
246
Stepan Salenikovichbc6c4be2015-07-31 16:07:54 -0400247static gboolean
248on_close_window(GtkWidget *window, G_GNUC_UNUSED GdkEvent *event, RingClient *client)
249{
250 g_return_val_if_fail(GTK_IS_WINDOW(window) && IS_RING_CLIENT(client), FALSE);
Stepan Salenikovichbc6c4be2015-07-31 16:07:54 -0400251 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
252
Stepan Salenikovich982b2882016-06-15 13:13:37 -0400253 if (g_settings_get_boolean(priv->settings, "show-status-icon")) {
Stepan Salenikovichbc6c4be2015-07-31 16:07:54 -0400254 /* we want to simply hide the window and keep the client running */
Stepan Salenikovichbb9c24e2015-09-15 09:55:02 -0400255 ring_window_hide(client);
Stepan Salenikovichbc6c4be2015-07-31 16:07:54 -0400256 return TRUE; /* do not propogate event */
257 } else {
258 /* we want to quit the application, so just propogate the event */
259 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 Salenikovich5a127672016-09-13 11:19:50 -0400458call_notifications_toggled(RingClient *self)
459{
460 auto priv = RING_CLIENT_GET_PRIVATE(self);
461
462 if (g_settings_get_boolean(priv->settings, "enable-call-notifications")) {
463 priv->call_notification = QObject::connect(
464 &CallModel::instance(),
465 &CallModel::incomingCall,
466 [] (Call *call) { ring_notify_incoming_call(call); }
467 );
468 } else {
469 QObject::disconnect(priv->call_notification);
470 }
471}
472
473static void
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400474ring_client_startup(GApplication *app)
Stepan Salenikovich69771842015-02-24 18:11:45 -0500475{
Sébastien Blin55bff9d2017-10-03 15:15:23 -0400476 // TODO still use old LRC models, in the future, we will init the LRC here.
Stepan Salenikovich69771842015-02-24 18:11:45 -0500477 RingClient *client = RING_CLIENT(app);
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400478 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
Stepan Salenikovich69771842015-02-24 18:11:45 -0500479
Stepan Salenikovich4f9bb982015-06-23 14:26:30 -0400480 g_message("Ring GNOME client version: %d.%d.%d", VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH);
481 g_message("git ref: %s", RING_CLIENT_REVISION);
482
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -0400483 /* make sure that the system corresponds to the autostart setting */
484 autostart_symlink(g_settings_get_boolean(priv->settings, "start-on-login"));
485 g_signal_connect(priv->settings, "changed::start-on-login", G_CALLBACK(autostart_toggled), NULL);
486
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400487 /* init clutter */
488 int clutter_error;
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400489 if ((clutter_error = gtk_clutter_init(&priv->argc, &priv->argv)) != CLUTTER_INIT_SUCCESS) {
490 g_error("Could not init clutter : %d\n", clutter_error);
491 exit(1); /* the g_error above should normally cause the applicaiton to exit */
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400492 }
493
494 /* init libRingClient and make sure its connected to the dbus */
495 try {
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400496 priv->qtapp = new QCoreApplication(priv->argc, priv->argv);
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400497 /* the call model will try to connect to dring via dbus */
498 CallModel::instance();
Stepan Salenikovich541a40a2016-08-17 11:33:47 -0400499 } catch(const char * msg) {
500 exception_dialog(msg);
501 exit(1);
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400502 } catch(QString& msg) {
Stepan Salenikovich541a40a2016-08-17 11:33:47 -0400503 exception_dialog(msg.toLocal8Bit().constData());
504 exit(1);
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400505 }
506
Stepan Salenikovich67c5dd32015-09-14 12:27:36 -0400507 /* load translations from LRC */
Guillaume Roguez884ef732017-07-14 12:53:35 -0400508 const auto locale_name = QLocale::system().name();
509 const auto locale_lang = locale_name.split('_')[0];
510
511 if (locale_name != locale_lang) {
512 /* Install language first to have lowest priority */
513 priv->translator_lang.reset(new QTranslator);
514 if (priv->translator_lang->load(RING_CLIENT_INSTALL "/share/libringclient/translations/lrc_" + locale_lang)) {
515 g_debug("installed translations for %s", locale_lang.toUtf8().constData());
516 priv->qtapp->installTranslator(priv->translator_lang.get());
517 }
518 }
519
520 priv->translator_full.reset(new QTranslator);
521 if (priv->translator_full->load(RING_CLIENT_INSTALL "/share/libringclient/translations/lrc_" + locale_name)) {
522 g_debug("installed translations for %s", locale_name.toUtf8().constData());
523 }
524
525 if (not priv->translator_lang and not priv->translator_full) {
Stepan Salenikovich67c5dd32015-09-14 12:27:36 -0400526 g_debug("could not load LRC translations for %s, %s",
Guillaume Roguez884ef732017-07-14 12:53:35 -0400527 QLocale::languageToString(QLocale::system().language()).toUtf8().constData(),
528 QLocale::countryToString(QLocale::system().country()).toUtf8().constData()
Stepan Salenikovich67c5dd32015-09-14 12:27:36 -0400529 );
530 }
531
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400532 /* init delegates */
Stepan Salenikovichbbd6c132015-08-20 15:21:48 -0400533 GlobalInstances::setPixmapManipulator(std::unique_ptr<Interfaces::PixbufManipulator>(new Interfaces::PixbufManipulator()));
Stepan Salenikovichbe6550c2015-08-21 16:16:03 -0400534 GlobalInstances::setDBusErrorHandler(std::unique_ptr<Interfaces::DBusErrorHandler>(new Interfaces::DBusErrorHandler()));
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400535
Stepan Salenikovich75a39172015-07-10 13:21:08 -0400536 /* make sure all RING accounts have a display name... this basically makes sure
537 * that all accounts created before the display name patch have a display name
538 * set... a bit of a hack as this should maybe be done in LRC */
539 force_ring_display_name();
540
Stepan Salenikovichf2d76c52015-07-17 17:54:56 -0400541 /* make sure basic number categories exist, in case user has no contacts
542 * from which these would be automatically created
543 */
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400544 NumberCategoryModel::instance().addCategory("work", QVariant());
545 NumberCategoryModel::instance().addCategory("home", QVariant());
Stepan Salenikovichf2d76c52015-07-17 17:54:56 -0400546
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400547 /* add backends */
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400548 CategorizedHistoryModel::instance().addCollection<LocalHistoryCollection>(LoadOptions::FORCE_ENABLED);
Nicolas Jagerb413b302016-05-06 11:41:32 -0400549 PersonModel::instance().addCollection<PeerProfileCollection>(LoadOptions::FORCE_ENABLED);
550 ProfileModel::instance().addCollection<LocalProfileCollection>(LoadOptions::FORCE_ENABLED);
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400551
Stepan Salenikovichf2d76c52015-07-17 17:54:56 -0400552 /* fallback backend for vcards */
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400553 PersonModel::instance().addCollection<FallbackPersonCollection>(LoadOptions::FORCE_ENABLED);
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400554
Stepan Salenikovichf2d76c52015-07-17 17:54:56 -0400555 /* EDS backend(s) */
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400556 load_eds_sources(priv->cancellable);
557
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400558 /* Override theme since we don't have appropriate icons for a dark them (yet) */
559 GtkSettings *gtk_settings = gtk_settings_get_default();
560 g_object_set(G_OBJECT(gtk_settings), "gtk-application-prefer-dark-theme",
561 FALSE, NULL);
562 /* enable button icons */
563 g_object_set(G_OBJECT(gtk_settings), "gtk-button-images",
564 TRUE, NULL);
565
Nicolas Jager655b8db2017-12-15 14:04:32 -0500566 /* enable sound (for notification) */
567 g_object_set(G_OBJECT(gtk_settings), "gtk-enable-event-sounds",
568 TRUE, NULL);
569
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400570 /* add GActions */
Stepan Salenikovich69771842015-02-24 18:11:45 -0500571 g_action_map_add_action_entries(
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400572 G_ACTION_MAP(app), ring_actions, G_N_ELEMENTS(ring_actions), app);
573
Stepan Salenikovich982b2882016-06-15 13:13:37 -0400574 /* GActions for settings */
575 auto action_window_visible = g_settings_create_action(priv->settings, "show-main-window");
576 g_action_map_add_action(G_ACTION_MAP(app), action_window_visible);
577
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400578 /* add accelerators */
579 ring_accelerators(RING_CLIENT(app));
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500580
Stepan Salenikovichbc6c4be2015-07-31 16:07:54 -0400581 /* show window on incoming calls (if the option is set)*/
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400582 QObject::connect(&CallModel::instance(), &CallModel::incomingCall,
Stepan Salenikovichbc6c4be2015-07-31 16:07:54 -0400583 [app] (G_GNUC_UNUSED Call *call) {
584 RingClient *client = RING_CLIENT(app);
585 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
586 if (g_settings_get_boolean(priv->settings, "bring-window-to-front"))
Stepan Salenikovichbb9c24e2015-09-15 09:55:02 -0400587 ring_window_show(client);
Stepan Salenikovichbc6c4be2015-07-31 16:07:54 -0400588 }
589 );
590
Stepan Salenikovich5a127672016-09-13 11:19:50 -0400591 /* enable notifications based on settings */
Stepan Salenikovich76c33e62015-05-22 12:24:07 -0400592 ring_notify_init();
Stepan Salenikovich5a127672016-09-13 11:19:50 -0400593 call_notifications_toggled(client);
Stepan Salenikovich5a127672016-09-13 11:19:50 -0400594 g_signal_connect_swapped(priv->settings, "changed::enable-call-notifications", G_CALLBACK(call_notifications_toggled), client);
Stepan Salenikovich67112d12015-06-16 16:57:06 -0400595
Stepan Salenikovich472c9052016-07-20 19:16:02 -0400596#if USE_LIBNM
597 /* monitor the network using libnm to notify the daemon about connectivity chagnes */
598 nm_client_new_async(priv->cancellable, (GAsyncReadyCallback)nm_client_cb, client);
599#endif
600
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400601 G_APPLICATION_CLASS(ring_client_parent_class)->startup(app);
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400602}
603
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400604static void
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500605ring_client_shutdown(GApplication *app)
606{
607 RingClient *self = RING_CLIENT(app);
608 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(self);
609
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400610 g_debug("quitting");
611
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400612 /* cancel any pending cancellable operations */
613 g_cancellable_cancel(priv->cancellable);
614 g_object_unref(priv->cancellable);
615
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400616 QObject::disconnect(priv->uam_updated);
Stepan Salenikovich5a127672016-09-13 11:19:50 -0400617 QObject::disconnect(priv->call_notification);
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400618
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500619 /* free the QCoreApplication, which will destroy all libRingClient models
620 * and thus send the Unregister signal over dbus to dring */
Nicolas Jagerfad674f2017-10-19 11:35:36 -0400621 if (priv->qtapp) {
622 delete priv->qtapp;
623 priv->qtapp = nullptr;
624 }
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500625
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400626 /* free the copied cmd line args */
627 g_strfreev(priv->argv);
628
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -0400629 g_clear_object(&priv->settings);
630
Stepan Salenikovich76c33e62015-05-22 12:24:07 -0400631 ring_notify_uninit();
632
Stepan Salenikovich472c9052016-07-20 19:16:02 -0400633#if USE_LIBNM
634 /* clear NetworkManager client if it was used */
635 g_clear_object(&priv->nm_client);
636#endif
637
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500638 /* Chain up to the parent class */
639 G_APPLICATION_CLASS(ring_client_parent_class)->shutdown(app);
640}
641
642static void
643ring_client_init(RingClient *self)
644{
645 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(self);
646
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500647 priv->win = NULL;
648 priv->qtapp = NULL;
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400649 priv->cancellable = g_cancellable_new();
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -0400650 priv->settings = g_settings_new_full(get_ring_schema(), NULL, NULL);
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400651
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400652 /* add custom cmd line options */
653 ring_client_add_options(G_APPLICATION(self));
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500654}
655
656static void
657ring_client_class_init(RingClientClass *klass)
658{
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400659 G_APPLICATION_CLASS(klass)->startup = ring_client_startup;
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400660 G_APPLICATION_CLASS(klass)->activate = ring_client_activate;
Victor Nikulshin167bbb62017-03-15 20:44:27 +0000661 G_APPLICATION_CLASS(klass)->open = ring_client_open;
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500662 G_APPLICATION_CLASS(klass)->shutdown = ring_client_shutdown;
663}
664
Sébastien Blin55bff9d2017-10-03 15:15:23 -0400665RingClient*
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400666ring_client_new(int argc, char *argv[])
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500667{
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400668 RingClient *client = (RingClient *)g_object_new(ring_client_get_type(),
Stepan Salenikovich76350582015-05-25 14:56:32 -0400669 "application-id", RING_CLIENT_APP_ID,
Victor Nikulshin167bbb62017-03-15 20:44:27 +0000670 "flags", G_APPLICATION_HANDLES_OPEN ,
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400671 NULL);
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400672
673 /* copy the cmd line args before they get processed by the GApplication*/
674 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
675 priv->argc = argc;
676 priv->argv = g_strdupv((gchar **)argv);
677
678 return client;
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500679}
Stepan Salenikovich67112d12015-06-16 16:57:06 -0400680
Sébastien Blin55bff9d2017-10-03 15:15:23 -0400681GtkWindow*
Stepan Salenikovichbe6550c2015-08-21 16:16:03 -0400682ring_client_get_main_window(RingClient *client)
Stepan Salenikovich67112d12015-06-16 16:57:06 -0400683{
684 g_return_val_if_fail(IS_RING_CLIENT(client), NULL);
685 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
686
687 return (GtkWindow *)priv->win;
688}
Stepan Salenikovichbb9c24e2015-09-15 09:55:02 -0400689
690void
691ring_client_set_restore_main_window_state(RingClient *client, gboolean restore)
692{
693 g_return_if_fail(IS_RING_CLIENT(client));
694 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
695
696 priv->restore_window_state = restore;
697}