blob: 8326338ffef9e985e94980df80c0a03cb896da21 [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
22#include <gtk/gtk.h>
23#include <glib/gi18n.h>
Stepan Salenikovich67c5dd32015-09-14 12:27:36 -040024#include <QtCore/QTranslator>
Stepan Salenikovichd2dbcee2015-02-27 16:52:28 -050025#include <QtCore/QCoreApplication>
26#include <QtCore/QString>
27#include <QtCore/QByteArray>
Stepan Salenikovichd81ef292015-02-17 18:47:37 -050028#include <callmodel.h>
Stepan Salenikovichd2dbcee2015-02-27 16:52:28 -050029#include <QtCore/QItemSelectionModel>
Stepan Salenikovichc64523b2015-02-27 16:31:00 -050030#include <useractionmodel.h>
Stepan Salenikovich36c025c2015-03-03 19:06:44 -050031#include <clutter-gtk/clutter-gtk.h>
Stepan Salenikovichdd84cf92015-03-19 21:38:19 -040032#include <categorizedhistorymodel.h>
Stepan Salenikovich6f687072015-03-26 10:43:37 -040033#include <personmodel.h>
34#include <fallbackpersoncollection.h>
35#include <QtCore/QStandardPaths>
Stepan Salenikovich1e131a42015-05-27 14:12:21 -040036#include <localhistorycollection.h>
Stepan Salenikovich67112d12015-06-16 16:57:06 -040037#include <media/text.h>
Stepan Salenikovichf2d76c52015-07-17 17:54:56 -040038#include <numbercategorymodel.h>
Stepan Salenikovichbbd6c132015-08-20 15:21:48 -040039#include <globalinstances.h>
40#include <memory>
Stepan Salenikovichd81ef292015-02-17 18:47:37 -050041
42#include "ring_client_options.h"
Stepan Salenikovich434b88f2015-02-19 17:49:08 -050043#include "ringmainwindow.h"
Stepan Salenikovich24dcd5c2015-03-26 17:40:34 -040044#include "dialogs.h"
Stepan Salenikovich6f687072015-03-26 10:43:37 -040045#include "backends/edscontactbackend.h"
Stepan Salenikovichbbd6c132015-08-20 15:21:48 -040046#include "native/pixbufmanipulator.h"
Stepan Salenikovichbe6550c2015-08-21 16:16:03 -040047#include "native/dbuserrorhandler.h"
Stepan Salenikovich76c33e62015-05-22 12:24:07 -040048#include "ringnotify.h"
Stepan Salenikovich76350582015-05-25 14:56:32 -040049#include "config.h"
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -040050#include "utils/files.h"
Stepan Salenikovich4f9bb982015-06-23 14:26:30 -040051#include "revision.h"
Stepan Salenikovich75a39172015-07-10 13:21:08 -040052#include "utils/accounts.h"
Stepan Salenikovich434b88f2015-02-19 17:49:08 -050053
54struct _RingClientClass
55{
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -040056 GtkApplicationClass parent_class;
Stepan Salenikovich434b88f2015-02-19 17:49:08 -050057};
58
59struct _RingClient
60{
61 GtkApplication parent;
62};
63
64typedef struct _RingClientPrivate RingClientPrivate;
Stepan Salenikovichd81ef292015-02-17 18:47:37 -050065
66struct _RingClientPrivate {
Stepan Salenikovich0d515e52015-05-19 16:31:05 -040067 /* args */
68 int argc;
69 char **argv;
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -040070
71 GSettings *settings;
72
Stepan Salenikovichd81ef292015-02-17 18:47:37 -050073 /* main window */
74 GtkWidget *win;
75 /* for libRingclient */
76 QCoreApplication *qtapp;
Stepan Salenikovich068fb692015-03-23 14:58:32 -040077 /* UAM */
78 QMetaObject::Connection uam_updated;
Stepan Salenikovich6f687072015-03-26 10:43:37 -040079
Stepan Salenikovich6c39b102015-09-15 14:24:45 -040080 std::unique_ptr<QTranslator> translator;
81
Stepan Salenikovich6f687072015-03-26 10:43:37 -040082 GCancellable *cancellable;
Stepan Salenikovichbb9c24e2015-09-15 09:55:02 -040083
84 gboolean restore_window_state;
Stepan Salenikovichd81ef292015-02-17 18:47:37 -050085};
86
Stepan Salenikovich0d515e52015-05-19 16:31:05 -040087/* this union is used to pass ints as pointers and vice versa for GAction parameters*/
88typedef union _int_ptr_t
89{
90 int value;
91 gint64 value64;
92 gpointer ptr;
93} int_ptr_t;
94
Stepan Salenikovich434b88f2015-02-19 17:49:08 -050095G_DEFINE_TYPE_WITH_PRIVATE(RingClient, ring_client, GTK_TYPE_APPLICATION);
Stepan Salenikovichd81ef292015-02-17 18:47:37 -050096
97#define RING_CLIENT_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), RING_CLIENT_TYPE, RingClientPrivate))
98
99static void
100init_exception_dialog(const char* msg)
101{
102 g_warning("%s", msg);
103 GtkWidget *dialog = gtk_message_dialog_new(NULL,
104 (GtkDialogFlags)(GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT),
105 GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
106 _("Unable to initialize.\nMake sure the Ring daemon (dring) is running.\nError: %s"),
107 msg);
108
109 gtk_window_set_title(GTK_WINDOW(dialog), _("Ring Error"));
110 gtk_dialog_run(GTK_DIALOG(dialog));
111 gtk_widget_destroy(dialog);
112}
113
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400114static void
115ring_accelerators(RingClient *client)
116{
Stepan Salenikovich39ee49a2015-03-24 12:44:55 -0400117#if GTK_CHECK_VERSION(3,12,0)
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400118 const gchar *quit_accels[2] = { "<Ctrl>Q", NULL };
119 gtk_application_set_accels_for_action(GTK_APPLICATION(client), "app.quit", quit_accels);
120#else
Stepan Salenikovich870d7392015-09-08 11:18:46 -0400121 gtk_application_add_accelerator(GTK_APPLICATION(client), "<Control>Q", "app.quit", NULL);
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400122#endif
123}
124
125static void
126action_quit(G_GNUC_UNUSED GSimpleAction *simple,
127 G_GNUC_UNUSED GVariant *parameter,
128 gpointer user_data)
129{
130 g_return_if_fail(G_IS_APPLICATION(user_data));
131
132#if GLIB_CHECK_VERSION(2,32,0)
133 g_application_quit(G_APPLICATION(user_data));
134#else
135 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(user_data);
136 gtk_widget_destroy(priv->win);
137#endif
138}
139
Stepan Salenikovich24dcd5c2015-03-26 17:40:34 -0400140static void
141action_about(G_GNUC_UNUSED GSimpleAction *simple,
142 G_GNUC_UNUSED GVariant *parameter,
143 gpointer user_data)
144{
145 g_return_if_fail(G_IS_APPLICATION(user_data));
146 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(user_data);
147
148 ring_about_dialog(priv->win);
149}
150
Stepan Salenikovich69771842015-02-24 18:11:45 -0500151static const GActionEntry ring_actions[] =
152{
Stepan Salenikovich24dcd5c2015-03-26 17:40:34 -0400153 { "accept", NULL, NULL, NULL, NULL, {0} },
154 { "hangup", NULL, NULL, NULL, NULL, {0} },
155 { "hold", NULL, NULL, "false", NULL, {0} },
156 { "quit", action_quit, NULL, NULL, NULL, {0} },
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400157 { "about", action_about, NULL, NULL, NULL, {0} },
Stepan Salenikovich943bd052015-09-04 17:42:08 -0400158 { "mute_audio", NULL, NULL, "false", NULL, {0} },
159 { "mute_video", NULL, NULL, "false", NULL, {0} },
160 { "record", NULL, NULL, "false", NULL, {0} },
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500161 /* TODO implement the other actions */
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500162 // { "transfer", NULL, NULL, "flase", NULL, {0} },
Stepan Salenikovich69771842015-02-24 18:11:45 -0500163};
164
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500165static void
166activate_action(GSimpleAction *action, G_GNUC_UNUSED GVariant *parameter, gpointer user_data)
167{
168 g_debug("activating action: %s", g_action_get_name(G_ACTION(action)));
169
170 int_ptr_t key;
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400171
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500172 key.ptr = user_data;
173 UserActionModel::Action a = static_cast<UserActionModel::Action>(key.value);
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400174 UserActionModel* uam = CallModel::instance().userActionModel();
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500175
176 uam << a;
177}
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500178
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400179static void
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -0400180autostart_toggled(GSettings *settings, G_GNUC_UNUSED gchar *key, G_GNUC_UNUSED gpointer user_data)
181{
182 autostart_symlink(g_settings_get_boolean(settings, "start-on-login"));
183}
184
Stepan Salenikovichbb9c24e2015-09-15 09:55:02 -0400185static void
186ring_window_show(RingClient *client)
187{
188 g_return_if_fail(IS_RING_CLIENT(client));
189 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
190
191 g_return_if_fail(priv->win);
192
193 g_debug("showing main window");
194 gtk_window_present(GTK_WINDOW(priv->win));
195 g_settings_set_boolean(priv->settings, "window-state-hidden", FALSE);
196}
197
198static void
199ring_window_hide(RingClient *client)
200{
201 g_return_if_fail(IS_RING_CLIENT(client));
202 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
203
204 g_return_if_fail(priv->win);
205
206 g_debug("hiding main window");
207 gtk_widget_hide(priv->win);
208 g_settings_set_boolean(priv->settings, "window-state-hidden", TRUE);
209}
210
Stepan Salenikovichbc6c4be2015-07-31 16:07:54 -0400211static gboolean
212on_close_window(GtkWidget *window, G_GNUC_UNUSED GdkEvent *event, RingClient *client)
213{
214 g_return_val_if_fail(GTK_IS_WINDOW(window) && IS_RING_CLIENT(client), FALSE);
Stepan Salenikovichbc6c4be2015-07-31 16:07:54 -0400215 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
216
217 if (g_settings_get_boolean(priv->settings, "hide-on-close")) {
218 /* we want to simply hide the window and keep the client running */
Stepan Salenikovichbb9c24e2015-09-15 09:55:02 -0400219 ring_window_hide(client);
Stepan Salenikovichbc6c4be2015-07-31 16:07:54 -0400220 return TRUE; /* do not propogate event */
221 } else {
222 /* we want to quit the application, so just propogate the event */
223 return FALSE;
224 }
225}
226
227static void
228ring_client_activate(GApplication *app)
229{
230 RingClient *client = RING_CLIENT(app);
231 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
232
Stepan Salenikovichbb9c24e2015-09-15 09:55:02 -0400233 gboolean show_window = TRUE;
234
Stepan Salenikovichbc6c4be2015-07-31 16:07:54 -0400235 if (priv->win == NULL) {
236 priv->win = ring_main_window_new(GTK_APPLICATION(app));
237
238 /* make sure win is set to NULL when the window is destroyed */
239 g_object_add_weak_pointer(G_OBJECT(priv->win), (gpointer *)&priv->win);
240
241 /* check if the window should be destoryed or not on close */
242 g_signal_connect(priv->win, "delete-event", G_CALLBACK(on_close_window), client);
Stepan Salenikovichbb9c24e2015-09-15 09:55:02 -0400243
244 /* the only case in which we don't want to show the main window is we're launching the
245 * primary instance of the application with the '-r' (--restore-last-window-state) option
246 * and the window was hidden when the application last quit
247 */
248 if ( priv->restore_window_state && g_settings_get_boolean(priv->settings, "window-state-hidden") )
249 show_window = FALSE;
Stepan Salenikovichbc6c4be2015-07-31 16:07:54 -0400250 }
251
Stepan Salenikovichbb9c24e2015-09-15 09:55:02 -0400252 if (show_window)
253 ring_window_show(client);
Stepan Salenikovichbc6c4be2015-07-31 16:07:54 -0400254}
255
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -0400256static void
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400257ring_client_startup(GApplication *app)
Stepan Salenikovich69771842015-02-24 18:11:45 -0500258{
Stepan Salenikovich69771842015-02-24 18:11:45 -0500259 RingClient *client = RING_CLIENT(app);
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400260 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
Stepan Salenikovich69771842015-02-24 18:11:45 -0500261
Stepan Salenikovich4f9bb982015-06-23 14:26:30 -0400262 g_message("Ring GNOME client version: %d.%d.%d", VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH);
263 g_message("git ref: %s", RING_CLIENT_REVISION);
264
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -0400265 /* make sure that the system corresponds to the autostart setting */
266 autostart_symlink(g_settings_get_boolean(priv->settings, "start-on-login"));
267 g_signal_connect(priv->settings, "changed::start-on-login", G_CALLBACK(autostart_toggled), NULL);
268
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400269 /* init clutter */
270 int clutter_error;
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400271 if ((clutter_error = gtk_clutter_init(&priv->argc, &priv->argv)) != CLUTTER_INIT_SUCCESS) {
272 g_error("Could not init clutter : %d\n", clutter_error);
273 exit(1); /* the g_error above should normally cause the applicaiton to exit */
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400274 }
275
276 /* init libRingClient and make sure its connected to the dbus */
277 try {
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400278 priv->qtapp = new QCoreApplication(priv->argc, priv->argv);
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400279 /* the call model will try to connect to dring via dbus */
280 CallModel::instance();
281 } catch (const char * msg) {
282 init_exception_dialog(msg);
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400283 g_error("%s", msg);
284 exit(1); /* the g_error above should normally cause the applicaiton to exit */
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400285 } catch(QString& msg) {
286 QByteArray ba = msg.toLocal8Bit();
287 const char *c_str = ba.data();
288 init_exception_dialog(c_str);
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400289 g_error("%s", c_str);
290 exit(1); /* the g_error above should normally cause the applicaiton to exit */
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400291 }
292
Stepan Salenikovich67c5dd32015-09-14 12:27:36 -0400293 /* load translations from LRC */
Stepan Salenikovich6c39b102015-09-15 14:24:45 -0400294 priv->translator.reset(new QTranslator);
295 if (priv->translator->load(QLocale::system(), "lrc", "_", RING_CLIENT_INSTALL "/share/libringclient/translations")) {
296 priv->qtapp->installTranslator(priv->translator.get());
Stepan Salenikovich67c5dd32015-09-14 12:27:36 -0400297 } else {
298 g_debug("could not load LRC translations for %s, %s",
299 QLocale::languageToString(QLocale::system().language()).toUtf8().constData(),
300 QLocale::countryToString(QLocale::system().country()).toUtf8().constData()
301 );
302 }
303
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400304 /* init delegates */
Stepan Salenikovichbbd6c132015-08-20 15:21:48 -0400305 GlobalInstances::setPixmapManipulator(std::unique_ptr<Interfaces::PixbufManipulator>(new Interfaces::PixbufManipulator()));
Stepan Salenikovichbe6550c2015-08-21 16:16:03 -0400306 GlobalInstances::setDBusErrorHandler(std::unique_ptr<Interfaces::DBusErrorHandler>(new Interfaces::DBusErrorHandler()));
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400307
Stepan Salenikovich75a39172015-07-10 13:21:08 -0400308 /* make sure all RING accounts have a display name... this basically makes sure
309 * that all accounts created before the display name patch have a display name
310 * set... a bit of a hack as this should maybe be done in LRC */
311 force_ring_display_name();
312
Stepan Salenikovichf2d76c52015-07-17 17:54:56 -0400313 /* make sure basic number categories exist, in case user has no contacts
314 * from which these would be automatically created
315 */
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400316 NumberCategoryModel::instance().addCategory("work", QVariant());
317 NumberCategoryModel::instance().addCategory("home", QVariant());
Stepan Salenikovichf2d76c52015-07-17 17:54:56 -0400318
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400319 /* add backends */
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400320 CategorizedHistoryModel::instance().addCollection<LocalHistoryCollection>(LoadOptions::FORCE_ENABLED);
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400321
Stepan Salenikovichf2d76c52015-07-17 17:54:56 -0400322 /* fallback backend for vcards */
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400323 PersonModel::instance().addCollection<FallbackPersonCollection>(LoadOptions::FORCE_ENABLED);
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400324
Stepan Salenikovichf2d76c52015-07-17 17:54:56 -0400325 /* EDS backend(s) */
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400326 load_eds_sources(priv->cancellable);
327
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400328 /* Override theme since we don't have appropriate icons for a dark them (yet) */
329 GtkSettings *gtk_settings = gtk_settings_get_default();
330 g_object_set(G_OBJECT(gtk_settings), "gtk-application-prefer-dark-theme",
331 FALSE, NULL);
332 /* enable button icons */
333 g_object_set(G_OBJECT(gtk_settings), "gtk-button-images",
334 TRUE, NULL);
335
336 /* add GActions */
Stepan Salenikovich69771842015-02-24 18:11:45 -0500337 g_action_map_add_action_entries(
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400338 G_ACTION_MAP(app), ring_actions, G_N_ELEMENTS(ring_actions), app);
339
340 /* add accelerators */
341 ring_accelerators(RING_CLIENT(app));
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500342
Stepan Salenikovich347b73a2015-03-22 10:39:00 -0400343 /* Bind GActions to the UserActionModel */
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400344 UserActionModel* uam = CallModel::instance().userActionModel();
Stepan Salenikovich347b73a2015-03-22 10:39:00 -0400345 QHash<int, GSimpleAction*> actionHash;
346 actionHash[ (int)UserActionModel::Action::ACCEPT ] = G_SIMPLE_ACTION(g_action_map_lookup_action(G_ACTION_MAP(app), "accept"));
347 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 -0400348 actionHash[ (int)UserActionModel::Action::MUTE_AUDIO ] = G_SIMPLE_ACTION(g_action_map_lookup_action(G_ACTION_MAP(app), "mute_audio"));
349 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 -0400350 /* TODO: add commented actions when ready */
Stepan Salenikovich347b73a2015-03-22 10:39:00 -0400351 // 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 -0400352 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 -0400353 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 -0500354
Stepan Salenikovich347b73a2015-03-22 10:39:00 -0400355 for (QHash<int,GSimpleAction*>::const_iterator i = actionHash.begin(); i != actionHash.end(); ++i) {
356 GSimpleAction* sa = i.value();
357 int_ptr_t user_data;
358 user_data.value = i.key();
359 g_signal_connect(G_OBJECT(sa), "activate", G_CALLBACK(activate_action), user_data.ptr);
360 }
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500361
Stepan Salenikovich347b73a2015-03-22 10:39:00 -0400362 /* change the state of the GActions based on the UserActionModel */
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400363 priv->uam_updated = QObject::connect(uam,&UserActionModel::dataChanged, [actionHash,uam](const QModelIndex& tl, const QModelIndex& br) {
Stepan Salenikovich347b73a2015-03-22 10:39:00 -0400364 const int first(tl.row()),last(br.row());
365 for(int i = first; i <= last;i++) {
366 const QModelIndex& idx = uam->index(i,0);
367 GSimpleAction* sa = actionHash[(int)qvariant_cast<UserActionModel::Action>(idx.data(UserActionModel::Role::ACTION))];
368 if (sa) {
369 /* enable/disable GAction based on UserActionModel */
370 g_simple_action_set_enabled(sa, idx.flags() & Qt::ItemIsEnabled);
371 /* set the state of the action if its stateful */
372 if (g_action_get_state_type(G_ACTION(sa)) != NULL)
373 g_simple_action_set_state(sa, g_variant_new_boolean(idx.data(Qt::CheckStateRole) == Qt::Checked));
374 }
375 }
376 });
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400377
Stepan Salenikovichbc6c4be2015-07-31 16:07:54 -0400378 /* show window on incoming calls (if the option is set)*/
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400379 QObject::connect(&CallModel::instance(), &CallModel::incomingCall,
Stepan Salenikovichbc6c4be2015-07-31 16:07:54 -0400380 [app] (G_GNUC_UNUSED Call *call) {
381 RingClient *client = RING_CLIENT(app);
382 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
383 if (g_settings_get_boolean(priv->settings, "bring-window-to-front"))
Stepan Salenikovichbb9c24e2015-09-15 09:55:02 -0400384 ring_window_show(client);
Stepan Salenikovichbc6c4be2015-07-31 16:07:54 -0400385 }
386 );
387
Stepan Salenikovich76c33e62015-05-22 12:24:07 -0400388 /* send call notifications */
389 ring_notify_init();
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400390 QObject::connect(&CallModel::instance(), &CallModel::incomingCall,
Stepan Salenikovich67112d12015-06-16 16:57:06 -0400391 [] (Call *call) { ring_notify_incoming_call(call);}
Stepan Salenikovich76c33e62015-05-22 12:24:07 -0400392 );
393
Stepan Salenikovich67112d12015-06-16 16:57:06 -0400394 /* chat notifications for incoming messages on all calls which are not the
395 * currently selected call */
396 ring_notify_monitor_chat_notifications(client);
397
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400398#if GLIB_CHECK_VERSION(2,40,0)
399 G_APPLICATION_CLASS(ring_client_parent_class)->startup(app);
400#else
401 /* don't need to chain up to the parent callback as this function will
402 * be called manually by the command_line callback in this case */
403#endif
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400404}
405
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400406#if !GLIB_CHECK_VERSION(2,40,0)
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400407static int
408ring_client_command_line(GApplication *app, GApplicationCommandLine *cmdline)
409{
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400410 gint argc;
411 gchar **argv = g_application_command_line_get_arguments(cmdline, &argc);
412 GOptionContext *context = ring_client_options_get_context();
413 GError *error = NULL;
414 if (g_option_context_parse(context, &argc, &argv, &error) == FALSE) {
415 g_print(_("%s\nRun '%s --help' to see a full list of available command line options.\n"),
416 error->message, argv[0]);
Stepan Salenikovich8a287fc2015-05-01 16:53:20 -0400417 g_clear_error(&error);
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400418 g_option_context_free(context);
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400419 g_strfreev(argv);
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400420 return 1;
421 }
422 g_option_context_free(context);
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400423 g_strfreev(argv);
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400424
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400425 if (!g_application_get_is_remote(app)) {
426 /* if this is the primary instance, we must peform the startup */
427 ring_client_startup(app);
428 }
429
430 g_application_activate(app);
431
432 return 0;
433}
434#endif
435
436static void
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500437ring_client_shutdown(GApplication *app)
438{
439 RingClient *self = RING_CLIENT(app);
440 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(self);
441
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400442 g_debug("quitting");
443
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400444 /* cancel any pending cancellable operations */
445 g_cancellable_cancel(priv->cancellable);
446 g_object_unref(priv->cancellable);
447
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400448 QObject::disconnect(priv->uam_updated);
449
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500450 /* free the QCoreApplication, which will destroy all libRingClient models
451 * and thus send the Unregister signal over dbus to dring */
452 delete priv->qtapp;
453
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400454 /* free the copied cmd line args */
455 g_strfreev(priv->argv);
456
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -0400457 g_clear_object(&priv->settings);
458
Stepan Salenikovich76c33e62015-05-22 12:24:07 -0400459 ring_notify_uninit();
460
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500461 /* Chain up to the parent class */
462 G_APPLICATION_CLASS(ring_client_parent_class)->shutdown(app);
463}
464
465static void
466ring_client_init(RingClient *self)
467{
468 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(self);
469
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500470 priv->win = NULL;
471 priv->qtapp = NULL;
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400472 priv->cancellable = g_cancellable_new();
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -0400473 priv->settings = g_settings_new_full(get_ring_schema(), NULL, NULL);
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400474
475#if GLIB_CHECK_VERSION(2,40,0)
476 /* add custom cmd line options */
477 ring_client_add_options(G_APPLICATION(self));
478#endif
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500479}
480
481static void
482ring_client_class_init(RingClientClass *klass)
483{
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400484#if GLIB_CHECK_VERSION(2,40,0)
485 G_APPLICATION_CLASS(klass)->startup = ring_client_startup;
486#else
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500487 G_APPLICATION_CLASS(klass)->command_line = ring_client_command_line;
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400488#endif
489 G_APPLICATION_CLASS(klass)->activate = ring_client_activate;
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500490 G_APPLICATION_CLASS(klass)->shutdown = ring_client_shutdown;
491}
492
493RingClient *
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400494ring_client_new(int argc, char *argv[])
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500495{
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400496 /* because the g_application_add_main_option_entries was only added in
497 * glib 2.40, for lower versions we must handle the command line options
498 * ourselves
499 */
500 RingClient *client = (RingClient *)g_object_new(ring_client_get_type(),
Stepan Salenikovich76350582015-05-25 14:56:32 -0400501 "application-id", RING_CLIENT_APP_ID,
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400502#if GLIB_CHECK_VERSION(2,40,0)
503 NULL);
504#else
505 "flags", G_APPLICATION_HANDLES_COMMAND_LINE,
506 NULL);
507#endif
508
509 /* copy the cmd line args before they get processed by the GApplication*/
510 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
511 priv->argc = argc;
512 priv->argv = g_strdupv((gchar **)argv);
513
514 return client;
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500515}
Stepan Salenikovich67112d12015-06-16 16:57:06 -0400516
517GtkWindow *
Stepan Salenikovichbe6550c2015-08-21 16:16:03 -0400518ring_client_get_main_window(RingClient *client)
Stepan Salenikovich67112d12015-06-16 16:57:06 -0400519{
520 g_return_val_if_fail(IS_RING_CLIENT(client), NULL);
521 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
522
523 return (GtkWindow *)priv->win;
524}
Stepan Salenikovichbb9c24e2015-09-15 09:55:02 -0400525
526void
527ring_client_set_restore_main_window_state(RingClient *client, gboolean restore)
528{
529 g_return_if_fail(IS_RING_CLIENT(client));
530 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
531
532 priv->restore_window_state = restore;
533}