blob: 5906e9966321b1c791f0a948a5027ae51bd4f235 [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>
24
25// GTK+ related
Stepan Salenikovichd81ef292015-02-17 18:47:37 -050026#include <gtk/gtk.h>
27#include <glib/gi18n.h>
Stepan Salenikovich472c9052016-07-20 19:16:02 -040028#include <clutter-gtk/clutter-gtk.h>
29
30// Qt
Stepan Salenikovich67c5dd32015-09-14 12:27:36 -040031#include <QtCore/QTranslator>
Stepan Salenikovichd2dbcee2015-02-27 16:52:28 -050032#include <QtCore/QCoreApplication>
33#include <QtCore/QString>
34#include <QtCore/QByteArray>
Stepan Salenikovichd2dbcee2015-02-27 16:52:28 -050035#include <QtCore/QItemSelectionModel>
Stepan Salenikovich472c9052016-07-20 19:16:02 -040036#include <QtCore/QStandardPaths>
37
38// LRC
39#include <callmodel.h>
Stepan Salenikovichc64523b2015-02-27 16:31:00 -050040#include <useractionmodel.h>
Stepan Salenikovichdd84cf92015-03-19 21:38:19 -040041#include <categorizedhistorymodel.h>
Stepan Salenikovich6f687072015-03-26 10:43:37 -040042#include <personmodel.h>
43#include <fallbackpersoncollection.h>
Stepan Salenikovich1e131a42015-05-27 14:12:21 -040044#include <localhistorycollection.h>
Stepan Salenikovich67112d12015-06-16 16:57:06 -040045#include <media/text.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 Salenikovichd81ef292015-02-17 18:47:37 -050054
Stepan Salenikovich472c9052016-07-20 19:16:02 -040055// Ring client
Stepan Salenikovichd81ef292015-02-17 18:47:37 -050056#include "ring_client_options.h"
Stepan Salenikovich434b88f2015-02-19 17:49:08 -050057#include "ringmainwindow.h"
Stepan Salenikovich24dcd5c2015-03-26 17:40:34 -040058#include "dialogs.h"
Stepan Salenikovich6f687072015-03-26 10:43:37 -040059#include "backends/edscontactbackend.h"
Stepan Salenikovichbbd6c132015-08-20 15:21:48 -040060#include "native/pixbufmanipulator.h"
Stepan Salenikovichbe6550c2015-08-21 16:16:03 -040061#include "native/dbuserrorhandler.h"
Stepan Salenikovich76c33e62015-05-22 12:24:07 -040062#include "ringnotify.h"
Stepan Salenikovich76350582015-05-25 14:56:32 -040063#include "config.h"
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -040064#include "utils/files.h"
Stepan Salenikovich4f9bb982015-06-23 14:26:30 -040065#include "revision.h"
Stepan Salenikovich75a39172015-07-10 13:21:08 -040066#include "utils/accounts.h"
Stepan Salenikovich434b88f2015-02-19 17:49:08 -050067
Stepan Salenikovich982b2882016-06-15 13:13:37 -040068#if USE_APPINDICATOR
69#include <libappindicator/app-indicator.h>
70#endif
71
Stepan Salenikovich472c9052016-07-20 19:16:02 -040072#if USE_LIBNM
Stepan Salenikovich28ca3172016-07-22 17:29:11 -040073#include <libnm-glib/nm-client.h>
Stepan Salenikovich472c9052016-07-20 19:16:02 -040074#endif
75
Stepan Salenikovich434b88f2015-02-19 17:49:08 -050076struct _RingClientClass
77{
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -040078 GtkApplicationClass parent_class;
Stepan Salenikovich434b88f2015-02-19 17:49:08 -050079};
80
81struct _RingClient
82{
83 GtkApplication parent;
84};
85
86typedef struct _RingClientPrivate RingClientPrivate;
Stepan Salenikovichd81ef292015-02-17 18:47:37 -050087
88struct _RingClientPrivate {
Stepan Salenikovich0d515e52015-05-19 16:31:05 -040089 /* args */
90 int argc;
91 char **argv;
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -040092
93 GSettings *settings;
94
Stepan Salenikovichd81ef292015-02-17 18:47:37 -050095 /* main window */
96 GtkWidget *win;
97 /* for libRingclient */
98 QCoreApplication *qtapp;
Stepan Salenikovich068fb692015-03-23 14:58:32 -040099 /* UAM */
100 QMetaObject::Connection uam_updated;
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400101
Stepan Salenikovich6c39b102015-09-15 14:24:45 -0400102 std::unique_ptr<QTranslator> translator;
103
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400104 GCancellable *cancellable;
Stepan Salenikovichbb9c24e2015-09-15 09:55:02 -0400105
106 gboolean restore_window_state;
Stepan Salenikovich982b2882016-06-15 13:13:37 -0400107
108 gpointer systray_icon;
109 GtkWidget *icon_menu;
Stepan Salenikovich472c9052016-07-20 19:16:02 -0400110
111#if USE_LIBNM
112 /* NetworkManager */
113 NMClient *nm_client;
114#endif
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500115};
116
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400117/* this union is used to pass ints as pointers and vice versa for GAction parameters*/
118typedef union _int_ptr_t
119{
120 int value;
121 gint64 value64;
122 gpointer ptr;
123} int_ptr_t;
124
Stepan Salenikovich434b88f2015-02-19 17:49:08 -0500125G_DEFINE_TYPE_WITH_PRIVATE(RingClient, ring_client, GTK_TYPE_APPLICATION);
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500126
127#define RING_CLIENT_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), RING_CLIENT_TYPE, RingClientPrivate))
128
129static void
Stepan Salenikovich541a40a2016-08-17 11:33:47 -0400130exception_dialog(const char* msg)
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500131{
Stepan Salenikovich541a40a2016-08-17 11:33:47 -0400132 g_critical("%s", msg);
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500133 GtkWidget *dialog = gtk_message_dialog_new(NULL,
134 (GtkDialogFlags)(GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT),
135 GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
136 _("Unable to initialize.\nMake sure the Ring daemon (dring) is running.\nError: %s"),
137 msg);
138
139 gtk_window_set_title(GTK_WINDOW(dialog), _("Ring Error"));
140 gtk_dialog_run(GTK_DIALOG(dialog));
141 gtk_widget_destroy(dialog);
142}
143
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400144static void
145ring_accelerators(RingClient *client)
146{
Stepan Salenikovich39ee49a2015-03-24 12:44:55 -0400147#if GTK_CHECK_VERSION(3,12,0)
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400148 const gchar *quit_accels[2] = { "<Ctrl>Q", NULL };
149 gtk_application_set_accels_for_action(GTK_APPLICATION(client), "app.quit", quit_accels);
150#else
Stepan Salenikovich870d7392015-09-08 11:18:46 -0400151 gtk_application_add_accelerator(GTK_APPLICATION(client), "<Control>Q", "app.quit", NULL);
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400152#endif
153}
154
155static void
156action_quit(G_GNUC_UNUSED GSimpleAction *simple,
157 G_GNUC_UNUSED GVariant *parameter,
158 gpointer user_data)
159{
160 g_return_if_fail(G_IS_APPLICATION(user_data));
161
162#if GLIB_CHECK_VERSION(2,32,0)
163 g_application_quit(G_APPLICATION(user_data));
164#else
165 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(user_data);
166 gtk_widget_destroy(priv->win);
167#endif
168}
169
Stepan Salenikovich24dcd5c2015-03-26 17:40:34 -0400170static void
171action_about(G_GNUC_UNUSED GSimpleAction *simple,
Stepan Salenikovich982b2882016-06-15 13:13:37 -0400172 G_GNUC_UNUSED GVariant *parameter,
173 gpointer user_data)
Stepan Salenikovich24dcd5c2015-03-26 17:40:34 -0400174{
175 g_return_if_fail(G_IS_APPLICATION(user_data));
176 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(user_data);
177
178 ring_about_dialog(priv->win);
179}
180
Olivier Gregoire66e4df72016-06-17 18:39:05 -0400181static void
182toggle_smartinfo(GSimpleAction *action, GVariant *parameter, gpointer)
183{
184 g_simple_action_set_state(action, parameter);
185 if (g_variant_get_boolean(parameter)) {
186 SmartInfoHub::instance().start();
187 } else {
188 SmartInfoHub::instance().stop();
189 }
190}
191
Stepan Salenikovich69771842015-02-24 18:11:45 -0500192static const GActionEntry ring_actions[] =
193{
Olivier Gregoire66e4df72016-06-17 18:39:05 -0400194 { "accept", NULL, NULL, NULL, NULL, {0} },
195 { "hangup", NULL, NULL, NULL, NULL, {0} },
196 { "hold", NULL, NULL, "false", NULL, {0} },
197 { "quit", action_quit, NULL, NULL, NULL, {0} },
198 { "about", action_about, NULL, NULL, NULL, {0} },
199 { "mute_audio", NULL, NULL, "false", NULL, {0} },
200 { "mute_video", NULL, NULL, "false", NULL, {0} },
201 { "record", NULL, NULL, "false", NULL, {0} },
202 { "display-smartinfo", NULL, NULL, "false", toggle_smartinfo, {0} },
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500203 /* TODO implement the other actions */
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500204 // { "transfer", NULL, NULL, "flase", NULL, {0} },
Stepan Salenikovich69771842015-02-24 18:11:45 -0500205};
206
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500207static void
208activate_action(GSimpleAction *action, G_GNUC_UNUSED GVariant *parameter, gpointer user_data)
209{
210 g_debug("activating action: %s", g_action_get_name(G_ACTION(action)));
211
212 int_ptr_t key;
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400213
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500214 key.ptr = user_data;
215 UserActionModel::Action a = static_cast<UserActionModel::Action>(key.value);
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400216 UserActionModel* uam = CallModel::instance().userActionModel();
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500217
218 uam << a;
219}
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500220
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400221static void
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -0400222autostart_toggled(GSettings *settings, G_GNUC_UNUSED gchar *key, G_GNUC_UNUSED gpointer user_data)
223{
224 autostart_symlink(g_settings_get_boolean(settings, "start-on-login"));
225}
226
Stepan Salenikovich982b2882016-06-15 13:13:37 -0400227
228static void
229show_main_window_toggled(RingClient *client)
230{
231 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
232
233 if (g_settings_get_boolean(priv->settings, "show-main-window")) {
234 gtk_window_present(GTK_WINDOW(priv->win));
235 } else {
236 gtk_widget_hide(priv->win);
237 }
238}
239
Stepan Salenikovichbb9c24e2015-09-15 09:55:02 -0400240static void
241ring_window_show(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", TRUE);
Stepan Salenikovichbb9c24e2015-09-15 09:55:02 -0400245}
246
247static void
248ring_window_hide(RingClient *client)
249{
Stepan Salenikovichbb9c24e2015-09-15 09:55:02 -0400250 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
Stepan Salenikovich982b2882016-06-15 13:13:37 -0400251 g_settings_set_boolean(priv->settings, "show-main-window", FALSE);
Stepan Salenikovichbb9c24e2015-09-15 09:55:02 -0400252}
253
Stepan Salenikovichbc6c4be2015-07-31 16:07:54 -0400254static gboolean
255on_close_window(GtkWidget *window, G_GNUC_UNUSED GdkEvent *event, RingClient *client)
256{
257 g_return_val_if_fail(GTK_IS_WINDOW(window) && IS_RING_CLIENT(client), FALSE);
Stepan Salenikovichbc6c4be2015-07-31 16:07:54 -0400258 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
259
Stepan Salenikovich982b2882016-06-15 13:13:37 -0400260 if (g_settings_get_boolean(priv->settings, "show-status-icon")) {
Stepan Salenikovichbc6c4be2015-07-31 16:07:54 -0400261 /* we want to simply hide the window and keep the client running */
Stepan Salenikovichbb9c24e2015-09-15 09:55:02 -0400262 ring_window_hide(client);
Stepan Salenikovichbc6c4be2015-07-31 16:07:54 -0400263 return TRUE; /* do not propogate event */
264 } else {
265 /* we want to quit the application, so just propogate the event */
266 return FALSE;
267 }
268}
269
Stepan Salenikovich982b2882016-06-15 13:13:37 -0400270#if !USE_APPINDICATOR
271static void
272popup_menu(GtkStatusIcon *self,
273 guint button,
274 guint when,
275 RingClient *client)
276{
277 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
278 G_GNUC_BEGIN_IGNORE_DEPRECATIONS // GtkStatusIcon is deprecated since 3.14, but we fallback on it
279 gtk_menu_popup(GTK_MENU(priv->icon_menu), NULL, NULL, gtk_status_icon_position_menu, self, button, when);
280 G_GNUC_END_IGNORE_DEPRECATIONS
281}
282#endif
283
284static void
285init_systray(RingClient *client)
286{
287 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
288
289 // init menu
290 if (!priv->icon_menu) {
Stepan Salenikovich982b2882016-06-15 13:13:37 -0400291
Stepan Salenikovich97177362016-06-22 18:38:24 -0400292 /* for some reason AppIndicator doesn't like the menu being built from a GMenuModel and/or
293 * the GMenuModel being built from an xml resource. So we build the menu in code.
294 */
295 priv->icon_menu = gtk_menu_new();
Stepan Salenikovich982b2882016-06-15 13:13:37 -0400296 g_object_ref_sink(priv->icon_menu);
Stepan Salenikovich97177362016-06-22 18:38:24 -0400297
298 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"));
299 gtk_actionable_set_action_name(GTK_ACTIONABLE(item), "app.show-main-window");
300 gtk_menu_shell_append(GTK_MENU_SHELL(priv->icon_menu), item);
301
302 item = gtk_menu_item_new_with_label(_("Quit"));
303 gtk_actionable_set_action_name(GTK_ACTIONABLE(item), "app.quit");
304 gtk_menu_shell_append(GTK_MENU_SHELL(priv->icon_menu), item);
305
306 gtk_widget_insert_action_group(priv->icon_menu, "app", G_ACTION_GROUP(client));
307 gtk_widget_show_all(priv->icon_menu);
Stepan Salenikovich982b2882016-06-15 13:13:37 -0400308 }
309
310#if USE_APPINDICATOR
311 auto indicator = app_indicator_new("ring", "ring", APP_INDICATOR_CATEGORY_COMMUNICATIONS);
312 app_indicator_set_status(indicator, APP_INDICATOR_STATUS_ACTIVE);
313 app_indicator_set_title(indicator, "ring");
314 /* app indicator requires a menu */
315 app_indicator_set_menu(indicator, GTK_MENU(priv->icon_menu));
316 priv->systray_icon = indicator;
317#else
318
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#endif
328}
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
Stepan Salenikovich472c9052016-07-20 19:16:02 -0400376#if USE_LIBNM
377
378static void
379log_connection_info(NMActiveConnection *connection)
380{
381 if (connection) {
Stepan Salenikovich02b283d2016-07-27 18:09:21 -0400382 g_debug("primary network connection: %s, default: %s",
Stepan Salenikovich472c9052016-07-20 19:16:02 -0400383 nm_active_connection_get_uuid(connection),
Stepan Salenikovich472c9052016-07-20 19:16:02 -0400384 nm_active_connection_get_default(connection) ? "yes" : "no");
385 } else {
386 g_warning("no primary network connection detected, check network settings");
387 }
388}
389
390static void
Stepan Salenikovich28ca3172016-07-22 17:29:11 -0400391primary_connection_changed(NMClient *nm)
Stepan Salenikovich472c9052016-07-20 19:16:02 -0400392{
Stepan Salenikovich472c9052016-07-20 19:16:02 -0400393 auto connection = nm_client_get_primary_connection(nm);
394 log_connection_info(connection);
395
396 AccountModel::instance().slotConnectivityChanged();
397}
398
399static void
400nm_client_cb(G_GNUC_UNUSED GObject *source_object, GAsyncResult *result, RingClient *self)
401{
402 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(self);
403
404 GError* error = nullptr;
405 if (auto nm_client = nm_client_new_finish(result, &error)) {
406 priv->nm_client = nm_client;
Stepan Salenikovich28ca3172016-07-22 17:29:11 -0400407 g_debug("NetworkManager client initialized, version: %s\ndaemon running: %s\nnnetworking enabled: %s",
Stepan Salenikovich472c9052016-07-20 19:16:02 -0400408 nm_client_get_version(nm_client),
Stepan Salenikovich28ca3172016-07-22 17:29:11 -0400409 nm_client_get_manager_running(nm_client) ? "yes" : "no",
Stepan Salenikovich472c9052016-07-20 19:16:02 -0400410 nm_client_networking_get_enabled(nm_client) ? "yes" : "no");
411
412 auto connection = nm_client_get_primary_connection(nm_client);
413 log_connection_info(connection);
414
415 /* We monitor the primary connection and notify the daemon to re-load its connections
416 * (accounts, UPnP, ...) when it changes. For example, on most systems, if we have an
417 * ethernet connection and then also connect to wifi, the primary connection will not change;
418 * however it will change in the opposite case because an ethernet connection is preferred.
419 */
Stepan Salenikovich28ca3172016-07-22 17:29:11 -0400420 g_signal_connect(nm_client, "notify::primary-connection", G_CALLBACK(primary_connection_changed), nullptr);
Stepan Salenikovich472c9052016-07-20 19:16:02 -0400421
422 } else {
423 g_warning("error initializing NetworkManager client: %s", error->message);
424 g_clear_error(&error);
425 }
426}
427
428#endif /* USE_LIBNM */
429
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -0400430static void
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400431ring_client_startup(GApplication *app)
Stepan Salenikovich69771842015-02-24 18:11:45 -0500432{
Stepan Salenikovich69771842015-02-24 18:11:45 -0500433 RingClient *client = RING_CLIENT(app);
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400434 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
Stepan Salenikovich69771842015-02-24 18:11:45 -0500435
Stepan Salenikovich4f9bb982015-06-23 14:26:30 -0400436 g_message("Ring GNOME client version: %d.%d.%d", VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH);
437 g_message("git ref: %s", RING_CLIENT_REVISION);
438
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -0400439 /* make sure that the system corresponds to the autostart setting */
440 autostart_symlink(g_settings_get_boolean(priv->settings, "start-on-login"));
441 g_signal_connect(priv->settings, "changed::start-on-login", G_CALLBACK(autostart_toggled), NULL);
442
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400443 /* init clutter */
444 int clutter_error;
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400445 if ((clutter_error = gtk_clutter_init(&priv->argc, &priv->argv)) != CLUTTER_INIT_SUCCESS) {
446 g_error("Could not init clutter : %d\n", clutter_error);
447 exit(1); /* the g_error above should normally cause the applicaiton to exit */
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400448 }
449
450 /* init libRingClient and make sure its connected to the dbus */
451 try {
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400452 priv->qtapp = new QCoreApplication(priv->argc, priv->argv);
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400453 /* the call model will try to connect to dring via dbus */
454 CallModel::instance();
Stepan Salenikovich541a40a2016-08-17 11:33:47 -0400455 } catch(const char * msg) {
456 exception_dialog(msg);
457 exit(1);
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400458 } catch(QString& msg) {
Stepan Salenikovich541a40a2016-08-17 11:33:47 -0400459 exception_dialog(msg.toLocal8Bit().constData());
460 exit(1);
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400461 }
462
Stepan Salenikovich67c5dd32015-09-14 12:27:36 -0400463 /* load translations from LRC */
Stepan Salenikovich6c39b102015-09-15 14:24:45 -0400464 priv->translator.reset(new QTranslator);
465 if (priv->translator->load(QLocale::system(), "lrc", "_", RING_CLIENT_INSTALL "/share/libringclient/translations")) {
466 priv->qtapp->installTranslator(priv->translator.get());
Stepan Salenikovich67c5dd32015-09-14 12:27:36 -0400467 } else {
468 g_debug("could not load LRC translations for %s, %s",
469 QLocale::languageToString(QLocale::system().language()).toUtf8().constData(),
470 QLocale::countryToString(QLocale::system().country()).toUtf8().constData()
471 );
472 }
473
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400474 /* init delegates */
Stepan Salenikovichbbd6c132015-08-20 15:21:48 -0400475 GlobalInstances::setPixmapManipulator(std::unique_ptr<Interfaces::PixbufManipulator>(new Interfaces::PixbufManipulator()));
Stepan Salenikovichbe6550c2015-08-21 16:16:03 -0400476 GlobalInstances::setDBusErrorHandler(std::unique_ptr<Interfaces::DBusErrorHandler>(new Interfaces::DBusErrorHandler()));
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400477
Stepan Salenikovich75a39172015-07-10 13:21:08 -0400478 /* make sure all RING accounts have a display name... this basically makes sure
479 * that all accounts created before the display name patch have a display name
480 * set... a bit of a hack as this should maybe be done in LRC */
481 force_ring_display_name();
482
Stepan Salenikovichf2d76c52015-07-17 17:54:56 -0400483 /* make sure basic number categories exist, in case user has no contacts
484 * from which these would be automatically created
485 */
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400486 NumberCategoryModel::instance().addCategory("work", QVariant());
487 NumberCategoryModel::instance().addCategory("home", QVariant());
Stepan Salenikovichf2d76c52015-07-17 17:54:56 -0400488
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400489 /* add backends */
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400490 CategorizedHistoryModel::instance().addCollection<LocalHistoryCollection>(LoadOptions::FORCE_ENABLED);
Nicolas Jagerb413b302016-05-06 11:41:32 -0400491 PersonModel::instance().addCollection<PeerProfileCollection>(LoadOptions::FORCE_ENABLED);
492 ProfileModel::instance().addCollection<LocalProfileCollection>(LoadOptions::FORCE_ENABLED);
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400493
Stepan Salenikovichf2d76c52015-07-17 17:54:56 -0400494 /* fallback backend for vcards */
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400495 PersonModel::instance().addCollection<FallbackPersonCollection>(LoadOptions::FORCE_ENABLED);
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400496
Stepan Salenikovichf2d76c52015-07-17 17:54:56 -0400497 /* EDS backend(s) */
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400498 load_eds_sources(priv->cancellable);
499
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400500 /* Override theme since we don't have appropriate icons for a dark them (yet) */
501 GtkSettings *gtk_settings = gtk_settings_get_default();
502 g_object_set(G_OBJECT(gtk_settings), "gtk-application-prefer-dark-theme",
503 FALSE, NULL);
504 /* enable button icons */
505 g_object_set(G_OBJECT(gtk_settings), "gtk-button-images",
506 TRUE, NULL);
507
508 /* add GActions */
Stepan Salenikovich69771842015-02-24 18:11:45 -0500509 g_action_map_add_action_entries(
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400510 G_ACTION_MAP(app), ring_actions, G_N_ELEMENTS(ring_actions), app);
511
Stepan Salenikovich982b2882016-06-15 13:13:37 -0400512 /* GActions for settings */
513 auto action_window_visible = g_settings_create_action(priv->settings, "show-main-window");
514 g_action_map_add_action(G_ACTION_MAP(app), action_window_visible);
515
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400516 /* add accelerators */
517 ring_accelerators(RING_CLIENT(app));
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500518
Stepan Salenikovich347b73a2015-03-22 10:39:00 -0400519 /* Bind GActions to the UserActionModel */
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400520 UserActionModel* uam = CallModel::instance().userActionModel();
Stepan Salenikovich347b73a2015-03-22 10:39:00 -0400521 QHash<int, GSimpleAction*> actionHash;
522 actionHash[ (int)UserActionModel::Action::ACCEPT ] = G_SIMPLE_ACTION(g_action_map_lookup_action(G_ACTION_MAP(app), "accept"));
523 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 -0400524 actionHash[ (int)UserActionModel::Action::MUTE_AUDIO ] = G_SIMPLE_ACTION(g_action_map_lookup_action(G_ACTION_MAP(app), "mute_audio"));
525 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 -0400526 /* TODO: add commented actions when ready */
Stepan Salenikovich347b73a2015-03-22 10:39:00 -0400527 // 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 -0400528 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 -0400529 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 -0500530
Stepan Salenikovich347b73a2015-03-22 10:39:00 -0400531 for (QHash<int,GSimpleAction*>::const_iterator i = actionHash.begin(); i != actionHash.end(); ++i) {
Stepan Salenikovich5fd97bc2016-08-30 09:40:38 -0400532 GSimpleAction* sa = i.value();
533 int_ptr_t user_data;
534 user_data.value = i.key();
535 g_signal_connect(G_OBJECT(sa), "activate", G_CALLBACK(activate_action), user_data.ptr);
Stepan Salenikovich347b73a2015-03-22 10:39:00 -0400536 }
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500537
Stepan Salenikovich347b73a2015-03-22 10:39:00 -0400538 /* change the state of the GActions based on the UserActionModel */
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400539 priv->uam_updated = QObject::connect(uam,&UserActionModel::dataChanged, [actionHash,uam](const QModelIndex& tl, const QModelIndex& br) {
Stepan Salenikovich5fd97bc2016-08-30 09:40:38 -0400540 const int first(tl.row()),last(br.row());
541 for(int i = first; i <= last;i++) {
542 const QModelIndex& idx = uam->index(i,0);
543 GSimpleAction* sa = actionHash[(int)qvariant_cast<UserActionModel::Action>(idx.data(UserActionModel::Role::ACTION))];
544 if (sa) {
545 /* enable/disable GAction based on UserActionModel */
546 g_simple_action_set_enabled(sa, idx.flags() & Qt::ItemIsEnabled);
547 /* set the state of the action if its stateful */
548 if (g_action_get_state_type(G_ACTION(sa)) != NULL)
549 g_simple_action_set_state(sa, g_variant_new_boolean(idx.data(Qt::CheckStateRole) == Qt::Checked));
550 }
551 }
Stepan Salenikovich347b73a2015-03-22 10:39:00 -0400552 });
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400553
Stepan Salenikovichbc6c4be2015-07-31 16:07:54 -0400554 /* show window on incoming calls (if the option is set)*/
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400555 QObject::connect(&CallModel::instance(), &CallModel::incomingCall,
Stepan Salenikovichbc6c4be2015-07-31 16:07:54 -0400556 [app] (G_GNUC_UNUSED Call *call) {
557 RingClient *client = RING_CLIENT(app);
558 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
559 if (g_settings_get_boolean(priv->settings, "bring-window-to-front"))
Stepan Salenikovichbb9c24e2015-09-15 09:55:02 -0400560 ring_window_show(client);
Stepan Salenikovichbc6c4be2015-07-31 16:07:54 -0400561 }
562 );
563
Stepan Salenikovich76c33e62015-05-22 12:24:07 -0400564 /* send call notifications */
565 ring_notify_init();
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400566 QObject::connect(&CallModel::instance(), &CallModel::incomingCall,
Stepan Salenikovich67112d12015-06-16 16:57:06 -0400567 [] (Call *call) { ring_notify_incoming_call(call);}
Stepan Salenikovich76c33e62015-05-22 12:24:07 -0400568 );
569
Stepan Salenikovich67112d12015-06-16 16:57:06 -0400570 /* chat notifications for incoming messages on all calls which are not the
571 * currently selected call */
572 ring_notify_monitor_chat_notifications(client);
573
Stepan Salenikovich472c9052016-07-20 19:16:02 -0400574#if USE_LIBNM
575 /* monitor the network using libnm to notify the daemon about connectivity chagnes */
576 nm_client_new_async(priv->cancellable, (GAsyncReadyCallback)nm_client_cb, client);
577#endif
578
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400579#if GLIB_CHECK_VERSION(2,40,0)
580 G_APPLICATION_CLASS(ring_client_parent_class)->startup(app);
581#else
582 /* don't need to chain up to the parent callback as this function will
583 * be called manually by the command_line callback in this case */
584#endif
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400585}
586
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400587#if !GLIB_CHECK_VERSION(2,40,0)
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400588static int
589ring_client_command_line(GApplication *app, GApplicationCommandLine *cmdline)
590{
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400591 gint argc;
592 gchar **argv = g_application_command_line_get_arguments(cmdline, &argc);
593 GOptionContext *context = ring_client_options_get_context();
594 GError *error = NULL;
595 if (g_option_context_parse(context, &argc, &argv, &error) == FALSE) {
596 g_print(_("%s\nRun '%s --help' to see a full list of available command line options.\n"),
597 error->message, argv[0]);
Stepan Salenikovich8a287fc2015-05-01 16:53:20 -0400598 g_clear_error(&error);
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400599 g_option_context_free(context);
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400600 g_strfreev(argv);
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400601 return 1;
602 }
603 g_option_context_free(context);
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400604 g_strfreev(argv);
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400605
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400606 if (!g_application_get_is_remote(app)) {
607 /* if this is the primary instance, we must peform the startup */
608 ring_client_startup(app);
609 }
610
611 g_application_activate(app);
612
613 return 0;
614}
615#endif
616
617static void
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500618ring_client_shutdown(GApplication *app)
619{
620 RingClient *self = RING_CLIENT(app);
621 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(self);
622
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400623 g_debug("quitting");
624
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400625 /* cancel any pending cancellable operations */
626 g_cancellable_cancel(priv->cancellable);
627 g_object_unref(priv->cancellable);
628
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400629 QObject::disconnect(priv->uam_updated);
630
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500631 /* free the QCoreApplication, which will destroy all libRingClient models
632 * and thus send the Unregister signal over dbus to dring */
633 delete priv->qtapp;
634
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400635 /* free the copied cmd line args */
636 g_strfreev(priv->argv);
637
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -0400638 g_clear_object(&priv->settings);
639
Stepan Salenikovich76c33e62015-05-22 12:24:07 -0400640 ring_notify_uninit();
641
Stepan Salenikovich472c9052016-07-20 19:16:02 -0400642#if USE_LIBNM
643 /* clear NetworkManager client if it was used */
644 g_clear_object(&priv->nm_client);
645#endif
646
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500647 /* Chain up to the parent class */
648 G_APPLICATION_CLASS(ring_client_parent_class)->shutdown(app);
649}
650
651static void
652ring_client_init(RingClient *self)
653{
654 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(self);
655
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500656 priv->win = NULL;
657 priv->qtapp = NULL;
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400658 priv->cancellable = g_cancellable_new();
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -0400659 priv->settings = g_settings_new_full(get_ring_schema(), NULL, NULL);
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400660
661#if GLIB_CHECK_VERSION(2,40,0)
662 /* add custom cmd line options */
663 ring_client_add_options(G_APPLICATION(self));
664#endif
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500665}
666
667static void
668ring_client_class_init(RingClientClass *klass)
669{
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400670#if GLIB_CHECK_VERSION(2,40,0)
671 G_APPLICATION_CLASS(klass)->startup = ring_client_startup;
672#else
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500673 G_APPLICATION_CLASS(klass)->command_line = ring_client_command_line;
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400674#endif
675 G_APPLICATION_CLASS(klass)->activate = ring_client_activate;
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500676 G_APPLICATION_CLASS(klass)->shutdown = ring_client_shutdown;
677}
678
679RingClient *
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400680ring_client_new(int argc, char *argv[])
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500681{
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400682 /* because the g_application_add_main_option_entries was only added in
683 * glib 2.40, for lower versions we must handle the command line options
684 * ourselves
685 */
686 RingClient *client = (RingClient *)g_object_new(ring_client_get_type(),
Stepan Salenikovich76350582015-05-25 14:56:32 -0400687 "application-id", RING_CLIENT_APP_ID,
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400688#if GLIB_CHECK_VERSION(2,40,0)
689 NULL);
690#else
691 "flags", G_APPLICATION_HANDLES_COMMAND_LINE,
692 NULL);
693#endif
694
695 /* copy the cmd line args before they get processed by the GApplication*/
696 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
697 priv->argc = argc;
698 priv->argv = g_strdupv((gchar **)argv);
699
700 return client;
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500701}
Stepan Salenikovich67112d12015-06-16 16:57:06 -0400702
703GtkWindow *
Stepan Salenikovichbe6550c2015-08-21 16:16:03 -0400704ring_client_get_main_window(RingClient *client)
Stepan Salenikovich67112d12015-06-16 16:57:06 -0400705{
706 g_return_val_if_fail(IS_RING_CLIENT(client), NULL);
707 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
708
709 return (GtkWindow *)priv->win;
710}
Stepan Salenikovichbb9c24e2015-09-15 09:55:02 -0400711
712void
713ring_client_set_restore_main_window_state(RingClient *client, gboolean restore)
714{
715 g_return_if_fail(IS_RING_CLIENT(client));
716 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
717
718 priv->restore_window_state = restore;
719}