blob: 286f3617d70ac621df7767aa8f7048d00aa10454 [file] [log] [blame]
Stepan Salenikovichd81ef292015-02-17 18:47:37 -05001/*
Stepan Salenikovichbe87d2c2016-01-25 14:14:34 -05002 * Copyright (C) 2015-2016 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 Salenikovichd81ef292015-02-17 18:47:37 -050057
Stepan Salenikovich472c9052016-07-20 19:16:02 -040058// Ring client
Stepan Salenikovichd81ef292015-02-17 18:47:37 -050059#include "ring_client_options.h"
Stepan Salenikovich434b88f2015-02-19 17:49:08 -050060#include "ringmainwindow.h"
Stepan Salenikovich24dcd5c2015-03-26 17:40:34 -040061#include "dialogs.h"
Stepan Salenikovich6f687072015-03-26 10:43:37 -040062#include "backends/edscontactbackend.h"
Stepan Salenikovichbbd6c132015-08-20 15:21:48 -040063#include "native/pixbufmanipulator.h"
Stepan Salenikovichbe6550c2015-08-21 16:16:03 -040064#include "native/dbuserrorhandler.h"
Stepan Salenikovich76c33e62015-05-22 12:24:07 -040065#include "ringnotify.h"
Stepan Salenikovich76350582015-05-25 14:56:32 -040066#include "config.h"
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -040067#include "utils/files.h"
Stepan Salenikovich4f9bb982015-06-23 14:26:30 -040068#include "revision.h"
Stepan Salenikovich75a39172015-07-10 13:21:08 -040069#include "utils/accounts.h"
Victor Nikulshin167bbb62017-03-15 20:44:27 +000070#include "utils/calling.h"
Stepan Salenikovich434b88f2015-02-19 17:49:08 -050071
Stepan Salenikovich72d812f2017-01-05 12:16:25 -050072#if HAVE_APPINDICATOR
Stepan Salenikovich982b2882016-06-15 13:13:37 -040073#include <libappindicator/app-indicator.h>
74#endif
75
Stepan Salenikovich472c9052016-07-20 19:16:02 -040076#if USE_LIBNM
Stepan Salenikovich28ca3172016-07-22 17:29:11 -040077#include <libnm-glib/nm-client.h>
Stepan Salenikovich472c9052016-07-20 19:16:02 -040078#endif
79
Stepan Salenikovich434b88f2015-02-19 17:49:08 -050080struct _RingClientClass
81{
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -040082 GtkApplicationClass parent_class;
Stepan Salenikovich434b88f2015-02-19 17:49:08 -050083};
84
85struct _RingClient
86{
87 GtkApplication parent;
88};
89
90typedef struct _RingClientPrivate RingClientPrivate;
Stepan Salenikovichd81ef292015-02-17 18:47:37 -050091
92struct _RingClientPrivate {
Stepan Salenikovich0d515e52015-05-19 16:31:05 -040093 /* args */
94 int argc;
95 char **argv;
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -040096
97 GSettings *settings;
98
Stepan Salenikovichd81ef292015-02-17 18:47:37 -050099 /* main window */
100 GtkWidget *win;
101 /* for libRingclient */
102 QCoreApplication *qtapp;
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400103 /* UAM */
104 QMetaObject::Connection uam_updated;
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400105
Stepan Salenikovich6c39b102015-09-15 14:24:45 -0400106 std::unique_ptr<QTranslator> translator;
107
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;
123 QMetaObject::Connection chat_notification;
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500124};
125
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400126/* this union is used to pass ints as pointers and vice versa for GAction parameters*/
127typedef union _int_ptr_t
128{
129 int value;
130 gint64 value64;
131 gpointer ptr;
132} int_ptr_t;
133
Stepan Salenikovich434b88f2015-02-19 17:49:08 -0500134G_DEFINE_TYPE_WITH_PRIVATE(RingClient, ring_client, GTK_TYPE_APPLICATION);
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500135
136#define RING_CLIENT_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), RING_CLIENT_TYPE, RingClientPrivate))
137
138static void
Stepan Salenikovich541a40a2016-08-17 11:33:47 -0400139exception_dialog(const char* msg)
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500140{
Stepan Salenikovich541a40a2016-08-17 11:33:47 -0400141 g_critical("%s", msg);
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500142 GtkWidget *dialog = gtk_message_dialog_new(NULL,
143 (GtkDialogFlags)(GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT),
144 GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
145 _("Unable to initialize.\nMake sure the Ring daemon (dring) is running.\nError: %s"),
146 msg);
147
148 gtk_window_set_title(GTK_WINDOW(dialog), _("Ring Error"));
149 gtk_dialog_run(GTK_DIALOG(dialog));
150 gtk_widget_destroy(dialog);
151}
152
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400153static void
154ring_accelerators(RingClient *client)
155{
Stepan Salenikovich39ee49a2015-03-24 12:44:55 -0400156#if GTK_CHECK_VERSION(3,12,0)
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400157 const gchar *quit_accels[2] = { "<Ctrl>Q", NULL };
158 gtk_application_set_accels_for_action(GTK_APPLICATION(client), "app.quit", quit_accels);
159#else
Stepan Salenikovich870d7392015-09-08 11:18:46 -0400160 gtk_application_add_accelerator(GTK_APPLICATION(client), "<Control>Q", "app.quit", NULL);
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400161#endif
162}
163
164static void
165action_quit(G_GNUC_UNUSED GSimpleAction *simple,
166 G_GNUC_UNUSED GVariant *parameter,
167 gpointer user_data)
168{
169 g_return_if_fail(G_IS_APPLICATION(user_data));
170
171#if GLIB_CHECK_VERSION(2,32,0)
172 g_application_quit(G_APPLICATION(user_data));
173#else
174 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(user_data);
175 gtk_widget_destroy(priv->win);
176#endif
177}
178
Stepan Salenikovich24dcd5c2015-03-26 17:40:34 -0400179static void
180action_about(G_GNUC_UNUSED GSimpleAction *simple,
Stepan Salenikovich982b2882016-06-15 13:13:37 -0400181 G_GNUC_UNUSED GVariant *parameter,
182 gpointer user_data)
Stepan Salenikovich24dcd5c2015-03-26 17:40:34 -0400183{
184 g_return_if_fail(G_IS_APPLICATION(user_data));
185 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(user_data);
186
187 ring_about_dialog(priv->win);
188}
189
Olivier Gregoire66e4df72016-06-17 18:39:05 -0400190static void
191toggle_smartinfo(GSimpleAction *action, GVariant *parameter, gpointer)
192{
193 g_simple_action_set_state(action, parameter);
194 if (g_variant_get_boolean(parameter)) {
195 SmartInfoHub::instance().start();
196 } else {
197 SmartInfoHub::instance().stop();
198 }
199}
200
Stepan Salenikovich69771842015-02-24 18:11:45 -0500201static const GActionEntry ring_actions[] =
202{
Olivier Gregoire66e4df72016-06-17 18:39:05 -0400203 { "accept", NULL, NULL, NULL, NULL, {0} },
204 { "hangup", NULL, NULL, NULL, NULL, {0} },
205 { "hold", NULL, NULL, "false", NULL, {0} },
206 { "quit", action_quit, NULL, NULL, NULL, {0} },
207 { "about", action_about, NULL, NULL, NULL, {0} },
208 { "mute_audio", NULL, NULL, "false", NULL, {0} },
209 { "mute_video", NULL, NULL, "false", NULL, {0} },
210 { "record", NULL, NULL, "false", NULL, {0} },
211 { "display-smartinfo", NULL, NULL, "false", toggle_smartinfo, {0} },
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500212 /* TODO implement the other actions */
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500213 // { "transfer", NULL, NULL, "flase", NULL, {0} },
Stepan Salenikovich69771842015-02-24 18:11:45 -0500214};
215
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500216static void
217activate_action(GSimpleAction *action, G_GNUC_UNUSED GVariant *parameter, gpointer user_data)
218{
219 g_debug("activating action: %s", g_action_get_name(G_ACTION(action)));
220
221 int_ptr_t key;
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400222
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500223 key.ptr = user_data;
224 UserActionModel::Action a = static_cast<UserActionModel::Action>(key.value);
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400225 UserActionModel* uam = CallModel::instance().userActionModel();
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500226
227 uam << a;
228}
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500229
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400230static void
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -0400231autostart_toggled(GSettings *settings, G_GNUC_UNUSED gchar *key, G_GNUC_UNUSED gpointer user_data)
232{
233 autostart_symlink(g_settings_get_boolean(settings, "start-on-login"));
234}
235
Stepan Salenikovich982b2882016-06-15 13:13:37 -0400236
237static void
238show_main_window_toggled(RingClient *client)
239{
240 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
241
242 if (g_settings_get_boolean(priv->settings, "show-main-window")) {
243 gtk_window_present(GTK_WINDOW(priv->win));
244 } else {
245 gtk_widget_hide(priv->win);
246 }
247}
248
Stepan Salenikovichbb9c24e2015-09-15 09:55:02 -0400249static void
250ring_window_show(RingClient *client)
251{
Stepan Salenikovichbb9c24e2015-09-15 09:55:02 -0400252 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
Stepan Salenikovich982b2882016-06-15 13:13:37 -0400253 g_settings_set_boolean(priv->settings, "show-main-window", TRUE);
Stepan Salenikovichbb9c24e2015-09-15 09:55:02 -0400254}
255
256static void
257ring_window_hide(RingClient *client)
258{
Stepan Salenikovichbb9c24e2015-09-15 09:55:02 -0400259 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
Stepan Salenikovich982b2882016-06-15 13:13:37 -0400260 g_settings_set_boolean(priv->settings, "show-main-window", FALSE);
Stepan Salenikovichbb9c24e2015-09-15 09:55:02 -0400261}
262
Stepan Salenikovichbc6c4be2015-07-31 16:07:54 -0400263static gboolean
264on_close_window(GtkWidget *window, G_GNUC_UNUSED GdkEvent *event, RingClient *client)
265{
266 g_return_val_if_fail(GTK_IS_WINDOW(window) && IS_RING_CLIENT(client), FALSE);
Stepan Salenikovichbc6c4be2015-07-31 16:07:54 -0400267 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
268
Stepan Salenikovich982b2882016-06-15 13:13:37 -0400269 if (g_settings_get_boolean(priv->settings, "show-status-icon")) {
Stepan Salenikovichbc6c4be2015-07-31 16:07:54 -0400270 /* we want to simply hide the window and keep the client running */
Stepan Salenikovichbb9c24e2015-09-15 09:55:02 -0400271 ring_window_hide(client);
Stepan Salenikovichbc6c4be2015-07-31 16:07:54 -0400272 return TRUE; /* do not propogate event */
273 } else {
274 /* we want to quit the application, so just propogate the event */
275 return FALSE;
276 }
277}
278
Stepan Salenikovich982b2882016-06-15 13:13:37 -0400279static void
280popup_menu(GtkStatusIcon *self,
281 guint button,
282 guint when,
283 RingClient *client)
284{
285 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
286 G_GNUC_BEGIN_IGNORE_DEPRECATIONS // GtkStatusIcon is deprecated since 3.14, but we fallback on it
287 gtk_menu_popup(GTK_MENU(priv->icon_menu), NULL, NULL, gtk_status_icon_position_menu, self, button, when);
288 G_GNUC_END_IGNORE_DEPRECATIONS
289}
Stepan Salenikovich982b2882016-06-15 13:13:37 -0400290
291static void
292init_systray(RingClient *client)
293{
294 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
295
296 // init menu
297 if (!priv->icon_menu) {
Stepan Salenikovich982b2882016-06-15 13:13:37 -0400298
Stepan Salenikovich97177362016-06-22 18:38:24 -0400299 /* for some reason AppIndicator doesn't like the menu being built from a GMenuModel and/or
300 * the GMenuModel being built from an xml resource. So we build the menu in code.
301 */
302 priv->icon_menu = gtk_menu_new();
Stepan Salenikovich982b2882016-06-15 13:13:37 -0400303 g_object_ref_sink(priv->icon_menu);
Stepan Salenikovich97177362016-06-22 18:38:24 -0400304
305 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"));
306 gtk_actionable_set_action_name(GTK_ACTIONABLE(item), "app.show-main-window");
307 gtk_menu_shell_append(GTK_MENU_SHELL(priv->icon_menu), item);
308
309 item = gtk_menu_item_new_with_label(_("Quit"));
310 gtk_actionable_set_action_name(GTK_ACTIONABLE(item), "app.quit");
311 gtk_menu_shell_append(GTK_MENU_SHELL(priv->icon_menu), item);
312
313 gtk_widget_insert_action_group(priv->icon_menu, "app", G_ACTION_GROUP(client));
314 gtk_widget_show_all(priv->icon_menu);
Stepan Salenikovich982b2882016-06-15 13:13:37 -0400315 }
316
Stepan Salenikovich72d812f2017-01-05 12:16:25 -0500317 gboolean use_appinidcator = FALSE;
Stepan Salenikovich982b2882016-06-15 13:13:37 -0400318
Stepan Salenikovich72d812f2017-01-05 12:16:25 -0500319#if HAVE_APPINDICATOR
320 /* only use AppIndicator in Unity (Tuleap: #1440) */
321 const auto desktop = g_getenv("XDG_CURRENT_DESKTOP");
Marco Martin7f2b5ab2017-03-23 12:13:02 +0100322 if (g_strcmp0("Unity", desktop) == 0 || g_strcmp0("KDE", desktop) == 0) {
Stepan Salenikovich72d812f2017-01-05 12:16:25 -0500323 use_appinidcator = TRUE;
Stepan Salenikovich982b2882016-06-15 13:13:37 -0400324
Stepan Salenikovich72d812f2017-01-05 12:16:25 -0500325 auto indicator = app_indicator_new("ring", "ring", APP_INDICATOR_CATEGORY_COMMUNICATIONS);
326 app_indicator_set_status(indicator, APP_INDICATOR_STATUS_ACTIVE);
327 app_indicator_set_title(indicator, "ring");
328 /* app indicator requires a menu */
329 app_indicator_set_menu(indicator, GTK_MENU(priv->icon_menu));
330 priv->systray_icon = indicator;
331 }
Stepan Salenikovich982b2882016-06-15 13:13:37 -0400332#endif
Stepan Salenikovich72d812f2017-01-05 12:16:25 -0500333
334 if (!use_appinidcator) {
335G_GNUC_BEGIN_IGNORE_DEPRECATIONS // GtkStatusIcon is deprecated since 3.14, but we fallback on it
336 auto status_icon = gtk_status_icon_new_from_icon_name("ring");
337 gtk_status_icon_set_title(status_icon, "ring");
338G_GNUC_END_IGNORE_DEPRECATIONS
339 g_signal_connect_swapped(status_icon, "activate", G_CALLBACK(ring_window_show), client);
340 g_signal_connect(status_icon, "popup-menu", G_CALLBACK(popup_menu), client);
341
342 priv->systray_icon = status_icon;
343 }
Stepan Salenikovich982b2882016-06-15 13:13:37 -0400344}
345
346static void
347systray_toggled(GSettings *settings, const gchar *key, RingClient *client)
348{
349 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
350
351 if (g_settings_get_boolean(settings, key)) {
352 if (!priv->systray_icon)
353 init_systray(client);
354 } else {
355 if (priv->systray_icon)
356 g_clear_object(&priv->systray_icon);
357 }
358}
359
Stepan Salenikovichbc6c4be2015-07-31 16:07:54 -0400360static void
361ring_client_activate(GApplication *app)
362{
363 RingClient *client = RING_CLIENT(app);
364 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
365
366 if (priv->win == NULL) {
Stepan Salenikovich982b2882016-06-15 13:13:37 -0400367 // activate being called for the first time
Stepan Salenikovichbc6c4be2015-07-31 16:07:54 -0400368 priv->win = ring_main_window_new(GTK_APPLICATION(app));
369
370 /* make sure win is set to NULL when the window is destroyed */
371 g_object_add_weak_pointer(G_OBJECT(priv->win), (gpointer *)&priv->win);
372
373 /* check if the window should be destoryed or not on close */
374 g_signal_connect(priv->win, "delete-event", G_CALLBACK(on_close_window), client);
Stepan Salenikovichbb9c24e2015-09-15 09:55:02 -0400375
Stepan Salenikovich982b2882016-06-15 13:13:37 -0400376 /* if we didn't launch with the '-r' (--restore-last-window-state) option then force the
377 * show-main-window to true */
378 if (!priv->restore_window_state)
379 ring_window_show(client);
Stepan Salenikovich3f27dc92016-06-22 18:08:01 -0400380 show_main_window_toggled(client);
381 g_signal_connect_swapped(priv->settings, "changed::show-main-window", G_CALLBACK(show_main_window_toggled), client);
Stepan Salenikovichbc6c4be2015-07-31 16:07:54 -0400382
Stepan Salenikovich982b2882016-06-15 13:13:37 -0400383 // track sys icon state
384 g_signal_connect(priv->settings, "changed::show-status-icon", G_CALLBACK(systray_toggled), client);
385 systray_toggled(priv->settings, "show-status-icon", client);
386 } else {
387 // activate not being called for the first time, force showing of main window
Stepan Salenikovichbb9c24e2015-09-15 09:55:02 -0400388 ring_window_show(client);
Stepan Salenikovich982b2882016-06-15 13:13:37 -0400389 }
Stepan Salenikovichbc6c4be2015-07-31 16:07:54 -0400390}
391
Victor Nikulshin167bbb62017-03-15 20:44:27 +0000392static void
393ring_client_open(GApplication *app, GFile **file, gint /*arg3*/, const gchar* /*arg4*/)
394{
395 ring_client_activate(app);
396
397 if (strcmp(g_file_get_uri_scheme(*file), "ring") == 0) {
398 const char * call_id = g_file_get_basename(*file);
399 std::regex format {"^[[:xdigit:]]{40}$"};
400
401 if (std::regex_match(call_id, format)) {
402 auto cm = std::unique_ptr<TemporaryContactMethod>(new TemporaryContactMethod);
403 cm->setUri(URI(QString::fromStdString(call_id)));
404
405 place_new_call(cm.get());
406 cm.release();
407 }
408 }
409}
410
Stepan Salenikovich472c9052016-07-20 19:16:02 -0400411#if USE_LIBNM
412
413static void
414log_connection_info(NMActiveConnection *connection)
415{
416 if (connection) {
Stepan Salenikovich02b283d2016-07-27 18:09:21 -0400417 g_debug("primary network connection: %s, default: %s",
Stepan Salenikovich472c9052016-07-20 19:16:02 -0400418 nm_active_connection_get_uuid(connection),
Stepan Salenikovich472c9052016-07-20 19:16:02 -0400419 nm_active_connection_get_default(connection) ? "yes" : "no");
420 } else {
421 g_warning("no primary network connection detected, check network settings");
422 }
423}
424
425static void
Stepan Salenikovich221beae2016-10-12 11:29:56 -0400426primary_connection_changed(NMClient *nm, GParamSpec*, RingClient *self)
Stepan Salenikovich472c9052016-07-20 19:16:02 -0400427{
Stepan Salenikovich221beae2016-10-12 11:29:56 -0400428 auto priv = RING_CLIENT_GET_PRIVATE(self);
Stepan Salenikovich472c9052016-07-20 19:16:02 -0400429 auto connection = nm_client_get_primary_connection(nm);
Stepan Salenikovich472c9052016-07-20 19:16:02 -0400430
Stepan Salenikovich221beae2016-10-12 11:29:56 -0400431 if (priv->primary_connection != connection) {
432 /* make sure the connection really changed
433 * on client start it seems to always emit the notify::primary-connection signal though it
434 * hasn't changed */
435 log_connection_info(connection);
436 priv->primary_connection = connection;
437 AccountModel::instance().slotConnectivityChanged();
438 }
Stepan Salenikovich472c9052016-07-20 19:16:02 -0400439}
440
441static void
442nm_client_cb(G_GNUC_UNUSED GObject *source_object, GAsyncResult *result, RingClient *self)
443{
444 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(self);
445
446 GError* error = nullptr;
447 if (auto nm_client = nm_client_new_finish(result, &error)) {
448 priv->nm_client = nm_client;
Stepan Salenikovich28ca3172016-07-22 17:29:11 -0400449 g_debug("NetworkManager client initialized, version: %s\ndaemon running: %s\nnnetworking enabled: %s",
Stepan Salenikovich472c9052016-07-20 19:16:02 -0400450 nm_client_get_version(nm_client),
Stepan Salenikovich28ca3172016-07-22 17:29:11 -0400451 nm_client_get_manager_running(nm_client) ? "yes" : "no",
Stepan Salenikovich472c9052016-07-20 19:16:02 -0400452 nm_client_networking_get_enabled(nm_client) ? "yes" : "no");
453
454 auto connection = nm_client_get_primary_connection(nm_client);
455 log_connection_info(connection);
Stepan Salenikovich221beae2016-10-12 11:29:56 -0400456 priv->primary_connection = connection;
Stepan Salenikovich472c9052016-07-20 19:16:02 -0400457
458 /* We monitor the primary connection and notify the daemon to re-load its connections
459 * (accounts, UPnP, ...) when it changes. For example, on most systems, if we have an
460 * ethernet connection and then also connect to wifi, the primary connection will not change;
461 * however it will change in the opposite case because an ethernet connection is preferred.
462 */
Stepan Salenikovich221beae2016-10-12 11:29:56 -0400463 g_signal_connect(nm_client, "notify::primary-connection", G_CALLBACK(primary_connection_changed), self);
Stepan Salenikovich472c9052016-07-20 19:16:02 -0400464
465 } else {
466 g_warning("error initializing NetworkManager client: %s", error->message);
467 g_clear_error(&error);
468 }
469}
470
471#endif /* USE_LIBNM */
472
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -0400473static void
Stepan Salenikovich5a127672016-09-13 11:19:50 -0400474call_notifications_toggled(RingClient *self)
475{
476 auto priv = RING_CLIENT_GET_PRIVATE(self);
477
478 if (g_settings_get_boolean(priv->settings, "enable-call-notifications")) {
479 priv->call_notification = QObject::connect(
480 &CallModel::instance(),
481 &CallModel::incomingCall,
482 [] (Call *call) { ring_notify_incoming_call(call); }
483 );
484 } else {
485 QObject::disconnect(priv->call_notification);
486 }
487}
488
489static void
490chat_notifications_toggled(RingClient *self)
491{
492 auto priv = RING_CLIENT_GET_PRIVATE(self);
493
494 if (g_settings_get_boolean(priv->settings, "enable-chat-notifications")) {
495 priv->chat_notification = QObject::connect(
496 &Media::RecordingModel::instance(),
497 &Media::RecordingModel::newTextMessage,
498 [self] (Media::TextRecording* t, ContactMethod* cm)
499 { ring_notify_message(cm, t, self); }
500 );
501 } else {
502 QObject::disconnect(priv->chat_notification);
503 }
504}
505
506static void
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400507ring_client_startup(GApplication *app)
Stepan Salenikovich69771842015-02-24 18:11:45 -0500508{
Stepan Salenikovich69771842015-02-24 18:11:45 -0500509 RingClient *client = RING_CLIENT(app);
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400510 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
Stepan Salenikovich69771842015-02-24 18:11:45 -0500511
Stepan Salenikovich4f9bb982015-06-23 14:26:30 -0400512 g_message("Ring GNOME client version: %d.%d.%d", VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH);
513 g_message("git ref: %s", RING_CLIENT_REVISION);
514
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -0400515 /* make sure that the system corresponds to the autostart setting */
516 autostart_symlink(g_settings_get_boolean(priv->settings, "start-on-login"));
517 g_signal_connect(priv->settings, "changed::start-on-login", G_CALLBACK(autostart_toggled), NULL);
518
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400519 /* init clutter */
520 int clutter_error;
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400521 if ((clutter_error = gtk_clutter_init(&priv->argc, &priv->argv)) != CLUTTER_INIT_SUCCESS) {
522 g_error("Could not init clutter : %d\n", clutter_error);
523 exit(1); /* the g_error above should normally cause the applicaiton to exit */
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400524 }
525
526 /* init libRingClient and make sure its connected to the dbus */
527 try {
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400528 priv->qtapp = new QCoreApplication(priv->argc, priv->argv);
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400529 /* the call model will try to connect to dring via dbus */
530 CallModel::instance();
Stepan Salenikovich541a40a2016-08-17 11:33:47 -0400531 } catch(const char * msg) {
532 exception_dialog(msg);
533 exit(1);
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400534 } catch(QString& msg) {
Stepan Salenikovich541a40a2016-08-17 11:33:47 -0400535 exception_dialog(msg.toLocal8Bit().constData());
536 exit(1);
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400537 }
538
Stepan Salenikovich67c5dd32015-09-14 12:27:36 -0400539 /* load translations from LRC */
Stepan Salenikovich6c39b102015-09-15 14:24:45 -0400540 priv->translator.reset(new QTranslator);
541 if (priv->translator->load(QLocale::system(), "lrc", "_", RING_CLIENT_INSTALL "/share/libringclient/translations")) {
542 priv->qtapp->installTranslator(priv->translator.get());
Stepan Salenikovich67c5dd32015-09-14 12:27:36 -0400543 } else {
544 g_debug("could not load LRC translations for %s, %s",
545 QLocale::languageToString(QLocale::system().language()).toUtf8().constData(),
546 QLocale::countryToString(QLocale::system().country()).toUtf8().constData()
547 );
548 }
549
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400550 /* init delegates */
Stepan Salenikovichbbd6c132015-08-20 15:21:48 -0400551 GlobalInstances::setPixmapManipulator(std::unique_ptr<Interfaces::PixbufManipulator>(new Interfaces::PixbufManipulator()));
Stepan Salenikovichbe6550c2015-08-21 16:16:03 -0400552 GlobalInstances::setDBusErrorHandler(std::unique_ptr<Interfaces::DBusErrorHandler>(new Interfaces::DBusErrorHandler()));
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400553
Stepan Salenikovich75a39172015-07-10 13:21:08 -0400554 /* make sure all RING accounts have a display name... this basically makes sure
555 * that all accounts created before the display name patch have a display name
556 * set... a bit of a hack as this should maybe be done in LRC */
557 force_ring_display_name();
558
Stepan Salenikovichf2d76c52015-07-17 17:54:56 -0400559 /* make sure basic number categories exist, in case user has no contacts
560 * from which these would be automatically created
561 */
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400562 NumberCategoryModel::instance().addCategory("work", QVariant());
563 NumberCategoryModel::instance().addCategory("home", QVariant());
Stepan Salenikovichf2d76c52015-07-17 17:54:56 -0400564
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400565 /* add backends */
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400566 CategorizedHistoryModel::instance().addCollection<LocalHistoryCollection>(LoadOptions::FORCE_ENABLED);
Nicolas Jagerb413b302016-05-06 11:41:32 -0400567 PersonModel::instance().addCollection<PeerProfileCollection>(LoadOptions::FORCE_ENABLED);
568 ProfileModel::instance().addCollection<LocalProfileCollection>(LoadOptions::FORCE_ENABLED);
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400569
Stepan Salenikovichf2d76c52015-07-17 17:54:56 -0400570 /* fallback backend for vcards */
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400571 PersonModel::instance().addCollection<FallbackPersonCollection>(LoadOptions::FORCE_ENABLED);
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400572
Stepan Salenikovichf2d76c52015-07-17 17:54:56 -0400573 /* EDS backend(s) */
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400574 load_eds_sources(priv->cancellable);
575
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400576 /* Override theme since we don't have appropriate icons for a dark them (yet) */
577 GtkSettings *gtk_settings = gtk_settings_get_default();
578 g_object_set(G_OBJECT(gtk_settings), "gtk-application-prefer-dark-theme",
579 FALSE, NULL);
580 /* enable button icons */
581 g_object_set(G_OBJECT(gtk_settings), "gtk-button-images",
582 TRUE, NULL);
583
584 /* add GActions */
Stepan Salenikovich69771842015-02-24 18:11:45 -0500585 g_action_map_add_action_entries(
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400586 G_ACTION_MAP(app), ring_actions, G_N_ELEMENTS(ring_actions), app);
587
Stepan Salenikovich982b2882016-06-15 13:13:37 -0400588 /* GActions for settings */
589 auto action_window_visible = g_settings_create_action(priv->settings, "show-main-window");
590 g_action_map_add_action(G_ACTION_MAP(app), action_window_visible);
591
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400592 /* add accelerators */
593 ring_accelerators(RING_CLIENT(app));
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500594
Stepan Salenikovich347b73a2015-03-22 10:39:00 -0400595 /* Bind GActions to the UserActionModel */
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400596 UserActionModel* uam = CallModel::instance().userActionModel();
Stepan Salenikovich347b73a2015-03-22 10:39:00 -0400597 QHash<int, GSimpleAction*> actionHash;
598 actionHash[ (int)UserActionModel::Action::ACCEPT ] = G_SIMPLE_ACTION(g_action_map_lookup_action(G_ACTION_MAP(app), "accept"));
599 actionHash[ (int)UserActionModel::Action::HOLD ] = G_SIMPLE_ACTION(g_action_map_lookup_action(G_ACTION_MAP(app), "hold"));
Stepan Salenikovich7d75eab2015-06-12 12:11:45 -0400600 actionHash[ (int)UserActionModel::Action::MUTE_AUDIO ] = G_SIMPLE_ACTION(g_action_map_lookup_action(G_ACTION_MAP(app), "mute_audio"));
601 actionHash[ (int)UserActionModel::Action::MUTE_VIDEO ] = G_SIMPLE_ACTION(g_action_map_lookup_action(G_ACTION_MAP(app), "mute_video"));
Stepan Salenikovich347b73a2015-03-22 10:39:00 -0400602 /* TODO: add commented actions when ready */
Stepan Salenikovich347b73a2015-03-22 10:39:00 -0400603 // actionHash[ (int)UserActionModel::Action::SERVER_TRANSFER ] = G_SIMPLE_ACTION(g_action_map_lookup_action(G_ACTION_MAP(app), "transfer"));
Stepan Salenikovich943bd052015-09-04 17:42:08 -0400604 actionHash[ (int)UserActionModel::Action::RECORD ] = G_SIMPLE_ACTION(g_action_map_lookup_action(G_ACTION_MAP(app), "record"));
Stepan Salenikovich347b73a2015-03-22 10:39:00 -0400605 actionHash[ (int)UserActionModel::Action::HANGUP ] = G_SIMPLE_ACTION(g_action_map_lookup_action(G_ACTION_MAP(app), "hangup"));
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500606
Stepan Salenikovich347b73a2015-03-22 10:39:00 -0400607 for (QHash<int,GSimpleAction*>::const_iterator i = actionHash.begin(); i != actionHash.end(); ++i) {
Stepan Salenikovich5fd97bc2016-08-30 09:40:38 -0400608 GSimpleAction* sa = i.value();
609 int_ptr_t user_data;
610 user_data.value = i.key();
611 g_signal_connect(G_OBJECT(sa), "activate", G_CALLBACK(activate_action), user_data.ptr);
Stepan Salenikovich347b73a2015-03-22 10:39:00 -0400612 }
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500613
Stepan Salenikovich347b73a2015-03-22 10:39:00 -0400614 /* change the state of the GActions based on the UserActionModel */
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400615 priv->uam_updated = QObject::connect(uam,&UserActionModel::dataChanged, [actionHash,uam](const QModelIndex& tl, const QModelIndex& br) {
Stepan Salenikovich5fd97bc2016-08-30 09:40:38 -0400616 const int first(tl.row()),last(br.row());
617 for(int i = first; i <= last;i++) {
618 const QModelIndex& idx = uam->index(i,0);
619 GSimpleAction* sa = actionHash[(int)qvariant_cast<UserActionModel::Action>(idx.data(UserActionModel::Role::ACTION))];
620 if (sa) {
621 /* enable/disable GAction based on UserActionModel */
622 g_simple_action_set_enabled(sa, idx.flags() & Qt::ItemIsEnabled);
623 /* set the state of the action if its stateful */
624 if (g_action_get_state_type(G_ACTION(sa)) != NULL)
625 g_simple_action_set_state(sa, g_variant_new_boolean(idx.data(Qt::CheckStateRole) == Qt::Checked));
626 }
627 }
Stepan Salenikovich347b73a2015-03-22 10:39:00 -0400628 });
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400629
Stepan Salenikovichbc6c4be2015-07-31 16:07:54 -0400630 /* show window on incoming calls (if the option is set)*/
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400631 QObject::connect(&CallModel::instance(), &CallModel::incomingCall,
Stepan Salenikovichbc6c4be2015-07-31 16:07:54 -0400632 [app] (G_GNUC_UNUSED Call *call) {
633 RingClient *client = RING_CLIENT(app);
634 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
635 if (g_settings_get_boolean(priv->settings, "bring-window-to-front"))
Stepan Salenikovichbb9c24e2015-09-15 09:55:02 -0400636 ring_window_show(client);
Stepan Salenikovichbc6c4be2015-07-31 16:07:54 -0400637 }
638 );
639
Stepan Salenikovich5a127672016-09-13 11:19:50 -0400640 /* enable notifications based on settings */
Stepan Salenikovich76c33e62015-05-22 12:24:07 -0400641 ring_notify_init();
Stepan Salenikovich5a127672016-09-13 11:19:50 -0400642 call_notifications_toggled(client);
643 chat_notifications_toggled(client);
644 g_signal_connect_swapped(priv->settings, "changed::enable-call-notifications", G_CALLBACK(call_notifications_toggled), client);
645 g_signal_connect_swapped(priv->settings, "changed::enable-chat-notifications", G_CALLBACK(chat_notifications_toggled), client);
Stepan Salenikovich67112d12015-06-16 16:57:06 -0400646
Stepan Salenikovich472c9052016-07-20 19:16:02 -0400647#if USE_LIBNM
648 /* monitor the network using libnm to notify the daemon about connectivity chagnes */
649 nm_client_new_async(priv->cancellable, (GAsyncReadyCallback)nm_client_cb, client);
650#endif
651
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400652 G_APPLICATION_CLASS(ring_client_parent_class)->startup(app);
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400653}
654
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400655static void
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500656ring_client_shutdown(GApplication *app)
657{
658 RingClient *self = RING_CLIENT(app);
659 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(self);
660
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400661 g_debug("quitting");
662
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400663 /* cancel any pending cancellable operations */
664 g_cancellable_cancel(priv->cancellable);
665 g_object_unref(priv->cancellable);
666
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400667 QObject::disconnect(priv->uam_updated);
Stepan Salenikovich5a127672016-09-13 11:19:50 -0400668 QObject::disconnect(priv->call_notification);
669 QObject::disconnect(priv->chat_notification);
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400670
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500671 /* free the QCoreApplication, which will destroy all libRingClient models
672 * and thus send the Unregister signal over dbus to dring */
673 delete priv->qtapp;
674
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400675 /* free the copied cmd line args */
676 g_strfreev(priv->argv);
677
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -0400678 g_clear_object(&priv->settings);
679
Stepan Salenikovich76c33e62015-05-22 12:24:07 -0400680 ring_notify_uninit();
681
Stepan Salenikovich472c9052016-07-20 19:16:02 -0400682#if USE_LIBNM
683 /* clear NetworkManager client if it was used */
684 g_clear_object(&priv->nm_client);
685#endif
686
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500687 /* Chain up to the parent class */
688 G_APPLICATION_CLASS(ring_client_parent_class)->shutdown(app);
689}
690
691static void
692ring_client_init(RingClient *self)
693{
694 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(self);
695
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500696 priv->win = NULL;
697 priv->qtapp = NULL;
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400698 priv->cancellable = g_cancellable_new();
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -0400699 priv->settings = g_settings_new_full(get_ring_schema(), NULL, NULL);
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400700
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400701 /* add custom cmd line options */
702 ring_client_add_options(G_APPLICATION(self));
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500703}
704
705static void
706ring_client_class_init(RingClientClass *klass)
707{
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400708 G_APPLICATION_CLASS(klass)->startup = ring_client_startup;
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400709 G_APPLICATION_CLASS(klass)->activate = ring_client_activate;
Victor Nikulshin167bbb62017-03-15 20:44:27 +0000710 G_APPLICATION_CLASS(klass)->open = ring_client_open;
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500711 G_APPLICATION_CLASS(klass)->shutdown = ring_client_shutdown;
712}
713
714RingClient *
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400715ring_client_new(int argc, char *argv[])
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500716{
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400717 RingClient *client = (RingClient *)g_object_new(ring_client_get_type(),
Stepan Salenikovich76350582015-05-25 14:56:32 -0400718 "application-id", RING_CLIENT_APP_ID,
Victor Nikulshin167bbb62017-03-15 20:44:27 +0000719 "flags", G_APPLICATION_HANDLES_OPEN ,
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400720 NULL);
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400721
722 /* copy the cmd line args before they get processed by the GApplication*/
723 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
724 priv->argc = argc;
725 priv->argv = g_strdupv((gchar **)argv);
726
727 return client;
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500728}
Stepan Salenikovich67112d12015-06-16 16:57:06 -0400729
730GtkWindow *
Stepan Salenikovichbe6550c2015-08-21 16:16:03 -0400731ring_client_get_main_window(RingClient *client)
Stepan Salenikovich67112d12015-06-16 16:57:06 -0400732{
733 g_return_val_if_fail(IS_RING_CLIENT(client), NULL);
734 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
735
736 return (GtkWindow *)priv->win;
737}
Stepan Salenikovichbb9c24e2015-09-15 09:55:02 -0400738
739void
740ring_client_set_restore_main_window_state(RingClient *client, gboolean restore)
741{
742 g_return_if_fail(IS_RING_CLIENT(client));
743 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
744
745 priv->restore_window_state = restore;
746}