blob: 2a4f08873f740eb533d20d4777dcf741495e1bf8 [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>
Stepan Salenikovichd81ef292015-02-17 18:47:37 -050053
Stepan Salenikovich472c9052016-07-20 19:16:02 -040054// Ring client
Stepan Salenikovichd81ef292015-02-17 18:47:37 -050055#include "ring_client_options.h"
Stepan Salenikovich434b88f2015-02-19 17:49:08 -050056#include "ringmainwindow.h"
Stepan Salenikovich24dcd5c2015-03-26 17:40:34 -040057#include "dialogs.h"
Stepan Salenikovich6f687072015-03-26 10:43:37 -040058#include "backends/edscontactbackend.h"
Stepan Salenikovichbbd6c132015-08-20 15:21:48 -040059#include "native/pixbufmanipulator.h"
Stepan Salenikovichbe6550c2015-08-21 16:16:03 -040060#include "native/dbuserrorhandler.h"
Stepan Salenikovich76c33e62015-05-22 12:24:07 -040061#include "ringnotify.h"
Stepan Salenikovich76350582015-05-25 14:56:32 -040062#include "config.h"
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -040063#include "utils/files.h"
Stepan Salenikovich4f9bb982015-06-23 14:26:30 -040064#include "revision.h"
Stepan Salenikovich75a39172015-07-10 13:21:08 -040065#include "utils/accounts.h"
Stepan Salenikovich434b88f2015-02-19 17:49:08 -050066
Stepan Salenikovich982b2882016-06-15 13:13:37 -040067#if USE_APPINDICATOR
68#include <libappindicator/app-indicator.h>
69#endif
70
Stepan Salenikovich472c9052016-07-20 19:16:02 -040071#if USE_LIBNM
Stepan Salenikovich28ca3172016-07-22 17:29:11 -040072#include <libnm-glib/nm-client.h>
Stepan Salenikovich472c9052016-07-20 19:16:02 -040073#endif
74
Stepan Salenikovich434b88f2015-02-19 17:49:08 -050075struct _RingClientClass
76{
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -040077 GtkApplicationClass parent_class;
Stepan Salenikovich434b88f2015-02-19 17:49:08 -050078};
79
80struct _RingClient
81{
82 GtkApplication parent;
83};
84
85typedef struct _RingClientPrivate RingClientPrivate;
Stepan Salenikovichd81ef292015-02-17 18:47:37 -050086
87struct _RingClientPrivate {
Stepan Salenikovich0d515e52015-05-19 16:31:05 -040088 /* args */
89 int argc;
90 char **argv;
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -040091
92 GSettings *settings;
93
Stepan Salenikovichd81ef292015-02-17 18:47:37 -050094 /* main window */
95 GtkWidget *win;
96 /* for libRingclient */
97 QCoreApplication *qtapp;
Stepan Salenikovich068fb692015-03-23 14:58:32 -040098 /* UAM */
99 QMetaObject::Connection uam_updated;
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400100
Stepan Salenikovich6c39b102015-09-15 14:24:45 -0400101 std::unique_ptr<QTranslator> translator;
102
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400103 GCancellable *cancellable;
Stepan Salenikovichbb9c24e2015-09-15 09:55:02 -0400104
105 gboolean restore_window_state;
Stepan Salenikovich982b2882016-06-15 13:13:37 -0400106
107 gpointer systray_icon;
108 GtkWidget *icon_menu;
Stepan Salenikovich472c9052016-07-20 19:16:02 -0400109
110#if USE_LIBNM
111 /* NetworkManager */
112 NMClient *nm_client;
113#endif
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500114};
115
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400116/* this union is used to pass ints as pointers and vice versa for GAction parameters*/
117typedef union _int_ptr_t
118{
119 int value;
120 gint64 value64;
121 gpointer ptr;
122} int_ptr_t;
123
Stepan Salenikovich434b88f2015-02-19 17:49:08 -0500124G_DEFINE_TYPE_WITH_PRIVATE(RingClient, ring_client, GTK_TYPE_APPLICATION);
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500125
126#define RING_CLIENT_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), RING_CLIENT_TYPE, RingClientPrivate))
127
128static void
Stepan Salenikovich541a40a2016-08-17 11:33:47 -0400129exception_dialog(const char* msg)
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500130{
Stepan Salenikovich541a40a2016-08-17 11:33:47 -0400131 g_critical("%s", msg);
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500132 GtkWidget *dialog = gtk_message_dialog_new(NULL,
133 (GtkDialogFlags)(GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT),
134 GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
135 _("Unable to initialize.\nMake sure the Ring daemon (dring) is running.\nError: %s"),
136 msg);
137
138 gtk_window_set_title(GTK_WINDOW(dialog), _("Ring Error"));
139 gtk_dialog_run(GTK_DIALOG(dialog));
140 gtk_widget_destroy(dialog);
141}
142
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400143static void
144ring_accelerators(RingClient *client)
145{
Stepan Salenikovich39ee49a2015-03-24 12:44:55 -0400146#if GTK_CHECK_VERSION(3,12,0)
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400147 const gchar *quit_accels[2] = { "<Ctrl>Q", NULL };
148 gtk_application_set_accels_for_action(GTK_APPLICATION(client), "app.quit", quit_accels);
149#else
Stepan Salenikovich870d7392015-09-08 11:18:46 -0400150 gtk_application_add_accelerator(GTK_APPLICATION(client), "<Control>Q", "app.quit", NULL);
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400151#endif
152}
153
154static void
155action_quit(G_GNUC_UNUSED GSimpleAction *simple,
156 G_GNUC_UNUSED GVariant *parameter,
157 gpointer user_data)
158{
159 g_return_if_fail(G_IS_APPLICATION(user_data));
160
161#if GLIB_CHECK_VERSION(2,32,0)
162 g_application_quit(G_APPLICATION(user_data));
163#else
164 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(user_data);
165 gtk_widget_destroy(priv->win);
166#endif
167}
168
Stepan Salenikovich24dcd5c2015-03-26 17:40:34 -0400169static void
170action_about(G_GNUC_UNUSED GSimpleAction *simple,
Stepan Salenikovich982b2882016-06-15 13:13:37 -0400171 G_GNUC_UNUSED GVariant *parameter,
172 gpointer user_data)
Stepan Salenikovich24dcd5c2015-03-26 17:40:34 -0400173{
174 g_return_if_fail(G_IS_APPLICATION(user_data));
175 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(user_data);
176
177 ring_about_dialog(priv->win);
178}
179
Stepan Salenikovich69771842015-02-24 18:11:45 -0500180static const GActionEntry ring_actions[] =
181{
Stepan Salenikovich24dcd5c2015-03-26 17:40:34 -0400182 { "accept", NULL, NULL, NULL, NULL, {0} },
183 { "hangup", NULL, NULL, NULL, NULL, {0} },
184 { "hold", NULL, NULL, "false", NULL, {0} },
185 { "quit", action_quit, NULL, NULL, NULL, {0} },
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400186 { "about", action_about, NULL, NULL, NULL, {0} },
Stepan Salenikovich943bd052015-09-04 17:42:08 -0400187 { "mute_audio", NULL, NULL, "false", NULL, {0} },
188 { "mute_video", NULL, NULL, "false", NULL, {0} },
189 { "record", NULL, NULL, "false", NULL, {0} },
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500190 /* TODO implement the other actions */
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500191 // { "transfer", NULL, NULL, "flase", NULL, {0} },
Stepan Salenikovich69771842015-02-24 18:11:45 -0500192};
193
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500194static void
195activate_action(GSimpleAction *action, G_GNUC_UNUSED GVariant *parameter, gpointer user_data)
196{
197 g_debug("activating action: %s", g_action_get_name(G_ACTION(action)));
198
199 int_ptr_t key;
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400200
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500201 key.ptr = user_data;
202 UserActionModel::Action a = static_cast<UserActionModel::Action>(key.value);
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400203 UserActionModel* uam = CallModel::instance().userActionModel();
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500204
205 uam << a;
206}
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500207
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400208static void
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -0400209autostart_toggled(GSettings *settings, G_GNUC_UNUSED gchar *key, G_GNUC_UNUSED gpointer user_data)
210{
211 autostart_symlink(g_settings_get_boolean(settings, "start-on-login"));
212}
213
Stepan Salenikovich982b2882016-06-15 13:13:37 -0400214
215static void
216show_main_window_toggled(RingClient *client)
217{
218 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
219
220 if (g_settings_get_boolean(priv->settings, "show-main-window")) {
221 gtk_window_present(GTK_WINDOW(priv->win));
222 } else {
223 gtk_widget_hide(priv->win);
224 }
225}
226
Stepan Salenikovichbb9c24e2015-09-15 09:55:02 -0400227static void
228ring_window_show(RingClient *client)
229{
Stepan Salenikovichbb9c24e2015-09-15 09:55:02 -0400230 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
Stepan Salenikovich982b2882016-06-15 13:13:37 -0400231 g_settings_set_boolean(priv->settings, "show-main-window", TRUE);
Stepan Salenikovichbb9c24e2015-09-15 09:55:02 -0400232}
233
234static void
235ring_window_hide(RingClient *client)
236{
Stepan Salenikovichbb9c24e2015-09-15 09:55:02 -0400237 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
Stepan Salenikovich982b2882016-06-15 13:13:37 -0400238 g_settings_set_boolean(priv->settings, "show-main-window", FALSE);
Stepan Salenikovichbb9c24e2015-09-15 09:55:02 -0400239}
240
Stepan Salenikovichbc6c4be2015-07-31 16:07:54 -0400241static gboolean
242on_close_window(GtkWidget *window, G_GNUC_UNUSED GdkEvent *event, RingClient *client)
243{
244 g_return_val_if_fail(GTK_IS_WINDOW(window) && IS_RING_CLIENT(client), FALSE);
Stepan Salenikovichbc6c4be2015-07-31 16:07:54 -0400245 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
246
Stepan Salenikovich982b2882016-06-15 13:13:37 -0400247 if (g_settings_get_boolean(priv->settings, "show-status-icon")) {
Stepan Salenikovichbc6c4be2015-07-31 16:07:54 -0400248 /* we want to simply hide the window and keep the client running */
Stepan Salenikovichbb9c24e2015-09-15 09:55:02 -0400249 ring_window_hide(client);
Stepan Salenikovichbc6c4be2015-07-31 16:07:54 -0400250 return TRUE; /* do not propogate event */
251 } else {
252 /* we want to quit the application, so just propogate the event */
253 return FALSE;
254 }
255}
256
Stepan Salenikovich982b2882016-06-15 13:13:37 -0400257#if !USE_APPINDICATOR
258static void
259popup_menu(GtkStatusIcon *self,
260 guint button,
261 guint when,
262 RingClient *client)
263{
264 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
265 G_GNUC_BEGIN_IGNORE_DEPRECATIONS // GtkStatusIcon is deprecated since 3.14, but we fallback on it
266 gtk_menu_popup(GTK_MENU(priv->icon_menu), NULL, NULL, gtk_status_icon_position_menu, self, button, when);
267 G_GNUC_END_IGNORE_DEPRECATIONS
268}
269#endif
270
271static void
272init_systray(RingClient *client)
273{
274 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
275
276 // init menu
277 if (!priv->icon_menu) {
Stepan Salenikovich982b2882016-06-15 13:13:37 -0400278
Stepan Salenikovich97177362016-06-22 18:38:24 -0400279 /* for some reason AppIndicator doesn't like the menu being built from a GMenuModel and/or
280 * the GMenuModel being built from an xml resource. So we build the menu in code.
281 */
282 priv->icon_menu = gtk_menu_new();
Stepan Salenikovich982b2882016-06-15 13:13:37 -0400283 g_object_ref_sink(priv->icon_menu);
Stepan Salenikovich97177362016-06-22 18:38:24 -0400284
285 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"));
286 gtk_actionable_set_action_name(GTK_ACTIONABLE(item), "app.show-main-window");
287 gtk_menu_shell_append(GTK_MENU_SHELL(priv->icon_menu), item);
288
289 item = gtk_menu_item_new_with_label(_("Quit"));
290 gtk_actionable_set_action_name(GTK_ACTIONABLE(item), "app.quit");
291 gtk_menu_shell_append(GTK_MENU_SHELL(priv->icon_menu), item);
292
293 gtk_widget_insert_action_group(priv->icon_menu, "app", G_ACTION_GROUP(client));
294 gtk_widget_show_all(priv->icon_menu);
Stepan Salenikovich982b2882016-06-15 13:13:37 -0400295 }
296
297#if USE_APPINDICATOR
298 auto indicator = app_indicator_new("ring", "ring", APP_INDICATOR_CATEGORY_COMMUNICATIONS);
299 app_indicator_set_status(indicator, APP_INDICATOR_STATUS_ACTIVE);
300 app_indicator_set_title(indicator, "ring");
301 /* app indicator requires a menu */
302 app_indicator_set_menu(indicator, GTK_MENU(priv->icon_menu));
303 priv->systray_icon = indicator;
304#else
305
306G_GNUC_BEGIN_IGNORE_DEPRECATIONS // GtkStatusIcon is deprecated since 3.14, but we fallback on it
307 auto status_icon = gtk_status_icon_new_from_icon_name("ring");
308 gtk_status_icon_set_title(status_icon, "ring");
309G_GNUC_END_IGNORE_DEPRECATIONS
310 g_signal_connect_swapped(status_icon, "activate", G_CALLBACK(ring_window_show), client);
311 g_signal_connect(status_icon, "popup-menu", G_CALLBACK(popup_menu), client);
312
313 priv->systray_icon = status_icon;
314#endif
315}
316
317static void
318systray_toggled(GSettings *settings, const gchar *key, RingClient *client)
319{
320 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
321
322 if (g_settings_get_boolean(settings, key)) {
323 if (!priv->systray_icon)
324 init_systray(client);
325 } else {
326 if (priv->systray_icon)
327 g_clear_object(&priv->systray_icon);
328 }
329}
330
Stepan Salenikovichbc6c4be2015-07-31 16:07:54 -0400331static void
332ring_client_activate(GApplication *app)
333{
334 RingClient *client = RING_CLIENT(app);
335 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
336
337 if (priv->win == NULL) {
Stepan Salenikovich982b2882016-06-15 13:13:37 -0400338 // activate being called for the first time
Stepan Salenikovichbc6c4be2015-07-31 16:07:54 -0400339 priv->win = ring_main_window_new(GTK_APPLICATION(app));
340
341 /* make sure win is set to NULL when the window is destroyed */
342 g_object_add_weak_pointer(G_OBJECT(priv->win), (gpointer *)&priv->win);
343
344 /* check if the window should be destoryed or not on close */
345 g_signal_connect(priv->win, "delete-event", G_CALLBACK(on_close_window), client);
Stepan Salenikovichbb9c24e2015-09-15 09:55:02 -0400346
Stepan Salenikovich982b2882016-06-15 13:13:37 -0400347 /* if we didn't launch with the '-r' (--restore-last-window-state) option then force the
348 * show-main-window to true */
349 if (!priv->restore_window_state)
350 ring_window_show(client);
Stepan Salenikovich3f27dc92016-06-22 18:08:01 -0400351 show_main_window_toggled(client);
352 g_signal_connect_swapped(priv->settings, "changed::show-main-window", G_CALLBACK(show_main_window_toggled), client);
Stepan Salenikovichbc6c4be2015-07-31 16:07:54 -0400353
Stepan Salenikovich982b2882016-06-15 13:13:37 -0400354 // track sys icon state
355 g_signal_connect(priv->settings, "changed::show-status-icon", G_CALLBACK(systray_toggled), client);
356 systray_toggled(priv->settings, "show-status-icon", client);
357 } else {
358 // activate not being called for the first time, force showing of main window
Stepan Salenikovichbb9c24e2015-09-15 09:55:02 -0400359 ring_window_show(client);
Stepan Salenikovich982b2882016-06-15 13:13:37 -0400360 }
Stepan Salenikovichbc6c4be2015-07-31 16:07:54 -0400361}
362
Stepan Salenikovich472c9052016-07-20 19:16:02 -0400363#if USE_LIBNM
364
365static void
366log_connection_info(NMActiveConnection *connection)
367{
368 if (connection) {
Stepan Salenikovich02b283d2016-07-27 18:09:21 -0400369 g_debug("primary network connection: %s, default: %s",
Stepan Salenikovich472c9052016-07-20 19:16:02 -0400370 nm_active_connection_get_uuid(connection),
Stepan Salenikovich472c9052016-07-20 19:16:02 -0400371 nm_active_connection_get_default(connection) ? "yes" : "no");
372 } else {
373 g_warning("no primary network connection detected, check network settings");
374 }
375}
376
377static void
Stepan Salenikovich28ca3172016-07-22 17:29:11 -0400378primary_connection_changed(NMClient *nm)
Stepan Salenikovich472c9052016-07-20 19:16:02 -0400379{
Stepan Salenikovich472c9052016-07-20 19:16:02 -0400380 auto connection = nm_client_get_primary_connection(nm);
381 log_connection_info(connection);
382
383 AccountModel::instance().slotConnectivityChanged();
384}
385
386static void
387nm_client_cb(G_GNUC_UNUSED GObject *source_object, GAsyncResult *result, RingClient *self)
388{
389 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(self);
390
391 GError* error = nullptr;
392 if (auto nm_client = nm_client_new_finish(result, &error)) {
393 priv->nm_client = nm_client;
Stepan Salenikovich28ca3172016-07-22 17:29:11 -0400394 g_debug("NetworkManager client initialized, version: %s\ndaemon running: %s\nnnetworking enabled: %s",
Stepan Salenikovich472c9052016-07-20 19:16:02 -0400395 nm_client_get_version(nm_client),
Stepan Salenikovich28ca3172016-07-22 17:29:11 -0400396 nm_client_get_manager_running(nm_client) ? "yes" : "no",
Stepan Salenikovich472c9052016-07-20 19:16:02 -0400397 nm_client_networking_get_enabled(nm_client) ? "yes" : "no");
398
399 auto connection = nm_client_get_primary_connection(nm_client);
400 log_connection_info(connection);
401
402 /* We monitor the primary connection and notify the daemon to re-load its connections
403 * (accounts, UPnP, ...) when it changes. For example, on most systems, if we have an
404 * ethernet connection and then also connect to wifi, the primary connection will not change;
405 * however it will change in the opposite case because an ethernet connection is preferred.
406 */
Stepan Salenikovich28ca3172016-07-22 17:29:11 -0400407 g_signal_connect(nm_client, "notify::primary-connection", G_CALLBACK(primary_connection_changed), nullptr);
Stepan Salenikovich472c9052016-07-20 19:16:02 -0400408
409 } else {
410 g_warning("error initializing NetworkManager client: %s", error->message);
411 g_clear_error(&error);
412 }
413}
414
415#endif /* USE_LIBNM */
416
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -0400417static void
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400418ring_client_startup(GApplication *app)
Stepan Salenikovich69771842015-02-24 18:11:45 -0500419{
Stepan Salenikovich69771842015-02-24 18:11:45 -0500420 RingClient *client = RING_CLIENT(app);
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400421 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
Stepan Salenikovich69771842015-02-24 18:11:45 -0500422
Stepan Salenikovich4f9bb982015-06-23 14:26:30 -0400423 g_message("Ring GNOME client version: %d.%d.%d", VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH);
424 g_message("git ref: %s", RING_CLIENT_REVISION);
425
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -0400426 /* make sure that the system corresponds to the autostart setting */
427 autostart_symlink(g_settings_get_boolean(priv->settings, "start-on-login"));
428 g_signal_connect(priv->settings, "changed::start-on-login", G_CALLBACK(autostart_toggled), NULL);
429
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400430 /* init clutter */
431 int clutter_error;
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400432 if ((clutter_error = gtk_clutter_init(&priv->argc, &priv->argv)) != CLUTTER_INIT_SUCCESS) {
433 g_error("Could not init clutter : %d\n", clutter_error);
434 exit(1); /* the g_error above should normally cause the applicaiton to exit */
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400435 }
436
437 /* init libRingClient and make sure its connected to the dbus */
438 try {
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400439 priv->qtapp = new QCoreApplication(priv->argc, priv->argv);
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400440 /* the call model will try to connect to dring via dbus */
441 CallModel::instance();
Stepan Salenikovich541a40a2016-08-17 11:33:47 -0400442 } catch(const char * msg) {
443 exception_dialog(msg);
444 exit(1);
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400445 } catch(QString& msg) {
Stepan Salenikovich541a40a2016-08-17 11:33:47 -0400446 exception_dialog(msg.toLocal8Bit().constData());
447 exit(1);
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400448 }
449
Stepan Salenikovich67c5dd32015-09-14 12:27:36 -0400450 /* load translations from LRC */
Stepan Salenikovich6c39b102015-09-15 14:24:45 -0400451 priv->translator.reset(new QTranslator);
452 if (priv->translator->load(QLocale::system(), "lrc", "_", RING_CLIENT_INSTALL "/share/libringclient/translations")) {
453 priv->qtapp->installTranslator(priv->translator.get());
Stepan Salenikovich67c5dd32015-09-14 12:27:36 -0400454 } else {
455 g_debug("could not load LRC translations for %s, %s",
456 QLocale::languageToString(QLocale::system().language()).toUtf8().constData(),
457 QLocale::countryToString(QLocale::system().country()).toUtf8().constData()
458 );
459 }
460
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400461 /* init delegates */
Stepan Salenikovichbbd6c132015-08-20 15:21:48 -0400462 GlobalInstances::setPixmapManipulator(std::unique_ptr<Interfaces::PixbufManipulator>(new Interfaces::PixbufManipulator()));
Stepan Salenikovichbe6550c2015-08-21 16:16:03 -0400463 GlobalInstances::setDBusErrorHandler(std::unique_ptr<Interfaces::DBusErrorHandler>(new Interfaces::DBusErrorHandler()));
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400464
Stepan Salenikovich75a39172015-07-10 13:21:08 -0400465 /* make sure all RING accounts have a display name... this basically makes sure
466 * that all accounts created before the display name patch have a display name
467 * set... a bit of a hack as this should maybe be done in LRC */
468 force_ring_display_name();
469
Stepan Salenikovichf2d76c52015-07-17 17:54:56 -0400470 /* make sure basic number categories exist, in case user has no contacts
471 * from which these would be automatically created
472 */
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400473 NumberCategoryModel::instance().addCategory("work", QVariant());
474 NumberCategoryModel::instance().addCategory("home", QVariant());
Stepan Salenikovichf2d76c52015-07-17 17:54:56 -0400475
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400476 /* add backends */
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400477 CategorizedHistoryModel::instance().addCollection<LocalHistoryCollection>(LoadOptions::FORCE_ENABLED);
Nicolas Jagerb413b302016-05-06 11:41:32 -0400478 PersonModel::instance().addCollection<PeerProfileCollection>(LoadOptions::FORCE_ENABLED);
479 ProfileModel::instance().addCollection<LocalProfileCollection>(LoadOptions::FORCE_ENABLED);
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400480
Stepan Salenikovichf2d76c52015-07-17 17:54:56 -0400481 /* fallback backend for vcards */
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400482 PersonModel::instance().addCollection<FallbackPersonCollection>(LoadOptions::FORCE_ENABLED);
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400483
Stepan Salenikovichf2d76c52015-07-17 17:54:56 -0400484 /* EDS backend(s) */
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400485 load_eds_sources(priv->cancellable);
486
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400487 /* Override theme since we don't have appropriate icons for a dark them (yet) */
488 GtkSettings *gtk_settings = gtk_settings_get_default();
489 g_object_set(G_OBJECT(gtk_settings), "gtk-application-prefer-dark-theme",
490 FALSE, NULL);
491 /* enable button icons */
492 g_object_set(G_OBJECT(gtk_settings), "gtk-button-images",
493 TRUE, NULL);
494
495 /* add GActions */
Stepan Salenikovich69771842015-02-24 18:11:45 -0500496 g_action_map_add_action_entries(
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400497 G_ACTION_MAP(app), ring_actions, G_N_ELEMENTS(ring_actions), app);
498
Stepan Salenikovich982b2882016-06-15 13:13:37 -0400499 /* GActions for settings */
500 auto action_window_visible = g_settings_create_action(priv->settings, "show-main-window");
501 g_action_map_add_action(G_ACTION_MAP(app), action_window_visible);
502
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400503 /* add accelerators */
504 ring_accelerators(RING_CLIENT(app));
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500505
Stepan Salenikovich347b73a2015-03-22 10:39:00 -0400506 /* Bind GActions to the UserActionModel */
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400507 UserActionModel* uam = CallModel::instance().userActionModel();
Stepan Salenikovich347b73a2015-03-22 10:39:00 -0400508 QHash<int, GSimpleAction*> actionHash;
509 actionHash[ (int)UserActionModel::Action::ACCEPT ] = G_SIMPLE_ACTION(g_action_map_lookup_action(G_ACTION_MAP(app), "accept"));
510 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 -0400511 actionHash[ (int)UserActionModel::Action::MUTE_AUDIO ] = G_SIMPLE_ACTION(g_action_map_lookup_action(G_ACTION_MAP(app), "mute_audio"));
512 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 -0400513 /* TODO: add commented actions when ready */
Stepan Salenikovich347b73a2015-03-22 10:39:00 -0400514 // 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 -0400515 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 -0400516 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 -0500517
Stepan Salenikovich347b73a2015-03-22 10:39:00 -0400518 for (QHash<int,GSimpleAction*>::const_iterator i = actionHash.begin(); i != actionHash.end(); ++i) {
519 GSimpleAction* sa = i.value();
520 int_ptr_t user_data;
521 user_data.value = i.key();
522 g_signal_connect(G_OBJECT(sa), "activate", G_CALLBACK(activate_action), user_data.ptr);
523 }
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500524
Stepan Salenikovich347b73a2015-03-22 10:39:00 -0400525 /* change the state of the GActions based on the UserActionModel */
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400526 priv->uam_updated = QObject::connect(uam,&UserActionModel::dataChanged, [actionHash,uam](const QModelIndex& tl, const QModelIndex& br) {
Stepan Salenikovich347b73a2015-03-22 10:39:00 -0400527 const int first(tl.row()),last(br.row());
528 for(int i = first; i <= last;i++) {
529 const QModelIndex& idx = uam->index(i,0);
530 GSimpleAction* sa = actionHash[(int)qvariant_cast<UserActionModel::Action>(idx.data(UserActionModel::Role::ACTION))];
531 if (sa) {
532 /* enable/disable GAction based on UserActionModel */
533 g_simple_action_set_enabled(sa, idx.flags() & Qt::ItemIsEnabled);
534 /* set the state of the action if its stateful */
535 if (g_action_get_state_type(G_ACTION(sa)) != NULL)
536 g_simple_action_set_state(sa, g_variant_new_boolean(idx.data(Qt::CheckStateRole) == Qt::Checked));
537 }
538 }
539 });
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400540
Stepan Salenikovichbc6c4be2015-07-31 16:07:54 -0400541 /* show window on incoming calls (if the option is set)*/
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400542 QObject::connect(&CallModel::instance(), &CallModel::incomingCall,
Stepan Salenikovichbc6c4be2015-07-31 16:07:54 -0400543 [app] (G_GNUC_UNUSED Call *call) {
544 RingClient *client = RING_CLIENT(app);
545 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
546 if (g_settings_get_boolean(priv->settings, "bring-window-to-front"))
Stepan Salenikovichbb9c24e2015-09-15 09:55:02 -0400547 ring_window_show(client);
Stepan Salenikovichbc6c4be2015-07-31 16:07:54 -0400548 }
549 );
550
Stepan Salenikovich76c33e62015-05-22 12:24:07 -0400551 /* send call notifications */
552 ring_notify_init();
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400553 QObject::connect(&CallModel::instance(), &CallModel::incomingCall,
Stepan Salenikovich67112d12015-06-16 16:57:06 -0400554 [] (Call *call) { ring_notify_incoming_call(call);}
Stepan Salenikovich76c33e62015-05-22 12:24:07 -0400555 );
556
Stepan Salenikovich67112d12015-06-16 16:57:06 -0400557 /* chat notifications for incoming messages on all calls which are not the
558 * currently selected call */
559 ring_notify_monitor_chat_notifications(client);
560
Stepan Salenikovich472c9052016-07-20 19:16:02 -0400561#if USE_LIBNM
562 /* monitor the network using libnm to notify the daemon about connectivity chagnes */
563 nm_client_new_async(priv->cancellable, (GAsyncReadyCallback)nm_client_cb, client);
564#endif
565
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400566#if GLIB_CHECK_VERSION(2,40,0)
567 G_APPLICATION_CLASS(ring_client_parent_class)->startup(app);
568#else
569 /* don't need to chain up to the parent callback as this function will
570 * be called manually by the command_line callback in this case */
571#endif
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400572}
573
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400574#if !GLIB_CHECK_VERSION(2,40,0)
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400575static int
576ring_client_command_line(GApplication *app, GApplicationCommandLine *cmdline)
577{
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400578 gint argc;
579 gchar **argv = g_application_command_line_get_arguments(cmdline, &argc);
580 GOptionContext *context = ring_client_options_get_context();
581 GError *error = NULL;
582 if (g_option_context_parse(context, &argc, &argv, &error) == FALSE) {
583 g_print(_("%s\nRun '%s --help' to see a full list of available command line options.\n"),
584 error->message, argv[0]);
Stepan Salenikovich8a287fc2015-05-01 16:53:20 -0400585 g_clear_error(&error);
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400586 g_option_context_free(context);
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400587 g_strfreev(argv);
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400588 return 1;
589 }
590 g_option_context_free(context);
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400591 g_strfreev(argv);
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400592
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400593 if (!g_application_get_is_remote(app)) {
594 /* if this is the primary instance, we must peform the startup */
595 ring_client_startup(app);
596 }
597
598 g_application_activate(app);
599
600 return 0;
601}
602#endif
603
604static 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);
617
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500618 /* free the QCoreApplication, which will destroy all libRingClient models
619 * and thus send the Unregister signal over dbus to dring */
620 delete priv->qtapp;
621
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400622 /* free the copied cmd line args */
623 g_strfreev(priv->argv);
624
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -0400625 g_clear_object(&priv->settings);
626
Stepan Salenikovich76c33e62015-05-22 12:24:07 -0400627 ring_notify_uninit();
628
Stepan Salenikovich472c9052016-07-20 19:16:02 -0400629#if USE_LIBNM
630 /* clear NetworkManager client if it was used */
631 g_clear_object(&priv->nm_client);
632#endif
633
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500634 /* Chain up to the parent class */
635 G_APPLICATION_CLASS(ring_client_parent_class)->shutdown(app);
636}
637
638static void
639ring_client_init(RingClient *self)
640{
641 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(self);
642
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500643 priv->win = NULL;
644 priv->qtapp = NULL;
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400645 priv->cancellable = g_cancellable_new();
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -0400646 priv->settings = g_settings_new_full(get_ring_schema(), NULL, NULL);
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400647
648#if GLIB_CHECK_VERSION(2,40,0)
649 /* add custom cmd line options */
650 ring_client_add_options(G_APPLICATION(self));
651#endif
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500652}
653
654static void
655ring_client_class_init(RingClientClass *klass)
656{
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400657#if GLIB_CHECK_VERSION(2,40,0)
658 G_APPLICATION_CLASS(klass)->startup = ring_client_startup;
659#else
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500660 G_APPLICATION_CLASS(klass)->command_line = ring_client_command_line;
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400661#endif
662 G_APPLICATION_CLASS(klass)->activate = ring_client_activate;
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500663 G_APPLICATION_CLASS(klass)->shutdown = ring_client_shutdown;
664}
665
666RingClient *
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400667ring_client_new(int argc, char *argv[])
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500668{
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400669 /* because the g_application_add_main_option_entries was only added in
670 * glib 2.40, for lower versions we must handle the command line options
671 * ourselves
672 */
673 RingClient *client = (RingClient *)g_object_new(ring_client_get_type(),
Stepan Salenikovich76350582015-05-25 14:56:32 -0400674 "application-id", RING_CLIENT_APP_ID,
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400675#if GLIB_CHECK_VERSION(2,40,0)
676 NULL);
677#else
678 "flags", G_APPLICATION_HANDLES_COMMAND_LINE,
679 NULL);
680#endif
681
682 /* copy the cmd line args before they get processed by the GApplication*/
683 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
684 priv->argc = argc;
685 priv->argv = g_strdupv((gchar **)argv);
686
687 return client;
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500688}
Stepan Salenikovich67112d12015-06-16 16:57:06 -0400689
690GtkWindow *
Stepan Salenikovichbe6550c2015-08-21 16:16:03 -0400691ring_client_get_main_window(RingClient *client)
Stepan Salenikovich67112d12015-06-16 16:57:06 -0400692{
693 g_return_val_if_fail(IS_RING_CLIENT(client), NULL);
694 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
695
696 return (GtkWindow *)priv->win;
697}
Stepan Salenikovichbb9c24e2015-09-15 09:55:02 -0400698
699void
700ring_client_set_restore_main_window_state(RingClient *client, gboolean restore)
701{
702 g_return_if_fail(IS_RING_CLIENT(client));
703 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
704
705 priv->restore_window_state = restore;
706}