blob: 7b9c05aab6379d8a63a98a9955faa10975fe27db [file] [log] [blame]
Stepan Salenikovichd81ef292015-02-17 18:47:37 -05001/*
Stepan Salenikovichbbd6c132015-08-20 15:21:48 -04002 * Copyright (C) 2015 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.
18 *
19 * Additional permission under GNU GPL version 3 section 7:
20 *
21 * If you modify this program, or any covered work, by linking or
22 * combining it with the OpenSSL project's OpenSSL library (or a
23 * modified version of that library), containing parts covered by the
Stepan Salenikovichbbd6c132015-08-20 15:21:48 -040024 * terms of the OpenSSL or SSLeay licenses, Savoir-faire Linux Inc.
Stepan Salenikovichd81ef292015-02-17 18:47:37 -050025 * grants you additional permission to convey the resulting work.
26 * Corresponding Source for a non-source form of such a combination
27 * shall include the source code for the parts of OpenSSL used as well
28 * as that of the covered work.
29 */
30
31#include "ring_client.h"
32
33#include <gtk/gtk.h>
34#include <glib/gi18n.h>
Stepan Salenikovichd2dbcee2015-02-27 16:52:28 -050035#include <QtCore/QCoreApplication>
36#include <QtCore/QString>
37#include <QtCore/QByteArray>
Stepan Salenikovichd81ef292015-02-17 18:47:37 -050038#include <callmodel.h>
Stepan Salenikovichd2dbcee2015-02-27 16:52:28 -050039#include <QtCore/QItemSelectionModel>
Stepan Salenikovichc64523b2015-02-27 16:31:00 -050040#include <useractionmodel.h>
Stepan Salenikovich36c025c2015-03-03 19:06:44 -050041#include <clutter-gtk/clutter-gtk.h>
Stepan Salenikovichdd84cf92015-03-19 21:38:19 -040042#include <categorizedhistorymodel.h>
Stepan Salenikovich6f687072015-03-26 10:43:37 -040043#include <personmodel.h>
44#include <fallbackpersoncollection.h>
45#include <QtCore/QStandardPaths>
Stepan Salenikovich1e131a42015-05-27 14:12:21 -040046#include <localhistorycollection.h>
Stepan Salenikovich67112d12015-06-16 16:57:06 -040047#include <media/text.h>
Stepan Salenikovichf2d76c52015-07-17 17:54:56 -040048#include <numbercategorymodel.h>
Stepan Salenikovichbbd6c132015-08-20 15:21:48 -040049#include <globalinstances.h>
50#include <memory>
Stepan Salenikovichd81ef292015-02-17 18:47:37 -050051
52#include "ring_client_options.h"
Stepan Salenikovich434b88f2015-02-19 17:49:08 -050053#include "ringmainwindow.h"
Stepan Salenikovich24dcd5c2015-03-26 17:40:34 -040054#include "dialogs.h"
Stepan Salenikovich6f687072015-03-26 10:43:37 -040055#include "backends/edscontactbackend.h"
Stepan Salenikovichbbd6c132015-08-20 15:21:48 -040056#include "native/pixbufmanipulator.h"
Stepan Salenikovich76c33e62015-05-22 12:24:07 -040057#include "ringnotify.h"
Stepan Salenikovich76350582015-05-25 14:56:32 -040058#include "config.h"
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -040059#include "utils/files.h"
Stepan Salenikovich4f9bb982015-06-23 14:26:30 -040060#include "revision.h"
Stepan Salenikovich75a39172015-07-10 13:21:08 -040061#include "utils/accounts.h"
Stepan Salenikovich434b88f2015-02-19 17:49:08 -050062
63struct _RingClientClass
64{
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -040065 GtkApplicationClass parent_class;
Stepan Salenikovich434b88f2015-02-19 17:49:08 -050066};
67
68struct _RingClient
69{
70 GtkApplication parent;
71};
72
73typedef struct _RingClientPrivate RingClientPrivate;
Stepan Salenikovichd81ef292015-02-17 18:47:37 -050074
75struct _RingClientPrivate {
Stepan Salenikovich0d515e52015-05-19 16:31:05 -040076 /* args */
77 int argc;
78 char **argv;
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -040079
80 GSettings *settings;
81
Stepan Salenikovichd81ef292015-02-17 18:47:37 -050082 /* main window */
83 GtkWidget *win;
84 /* for libRingclient */
85 QCoreApplication *qtapp;
Stepan Salenikovich068fb692015-03-23 14:58:32 -040086 /* UAM */
87 QMetaObject::Connection uam_updated;
Stepan Salenikovich6f687072015-03-26 10:43:37 -040088
89 GCancellable *cancellable;
Stepan Salenikovichd81ef292015-02-17 18:47:37 -050090};
91
Stepan Salenikovich0d515e52015-05-19 16:31:05 -040092/* this union is used to pass ints as pointers and vice versa for GAction parameters*/
93typedef union _int_ptr_t
94{
95 int value;
96 gint64 value64;
97 gpointer ptr;
98} int_ptr_t;
99
Stepan Salenikovich434b88f2015-02-19 17:49:08 -0500100G_DEFINE_TYPE_WITH_PRIVATE(RingClient, ring_client, GTK_TYPE_APPLICATION);
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500101
102#define RING_CLIENT_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), RING_CLIENT_TYPE, RingClientPrivate))
103
104static void
105init_exception_dialog(const char* msg)
106{
107 g_warning("%s", msg);
108 GtkWidget *dialog = gtk_message_dialog_new(NULL,
109 (GtkDialogFlags)(GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT),
110 GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
111 _("Unable to initialize.\nMake sure the Ring daemon (dring) is running.\nError: %s"),
112 msg);
113
114 gtk_window_set_title(GTK_WINDOW(dialog), _("Ring Error"));
115 gtk_dialog_run(GTK_DIALOG(dialog));
116 gtk_widget_destroy(dialog);
117}
118
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400119static void
120ring_accelerators(RingClient *client)
121{
Stepan Salenikovich39ee49a2015-03-24 12:44:55 -0400122#if GTK_CHECK_VERSION(3,12,0)
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400123 const gchar *quit_accels[2] = { "<Ctrl>Q", NULL };
124 gtk_application_set_accels_for_action(GTK_APPLICATION(client), "app.quit", quit_accels);
125#else
Stepan Salenikovich870d7392015-09-08 11:18:46 -0400126 gtk_application_add_accelerator(GTK_APPLICATION(client), "<Control>Q", "app.quit", NULL);
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400127#endif
128}
129
130static void
131action_quit(G_GNUC_UNUSED GSimpleAction *simple,
132 G_GNUC_UNUSED GVariant *parameter,
133 gpointer user_data)
134{
135 g_return_if_fail(G_IS_APPLICATION(user_data));
136
137#if GLIB_CHECK_VERSION(2,32,0)
138 g_application_quit(G_APPLICATION(user_data));
139#else
140 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(user_data);
141 gtk_widget_destroy(priv->win);
142#endif
143}
144
Stepan Salenikovich24dcd5c2015-03-26 17:40:34 -0400145static void
146action_about(G_GNUC_UNUSED GSimpleAction *simple,
147 G_GNUC_UNUSED GVariant *parameter,
148 gpointer user_data)
149{
150 g_return_if_fail(G_IS_APPLICATION(user_data));
151 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(user_data);
152
153 ring_about_dialog(priv->win);
154}
155
Stepan Salenikovich69771842015-02-24 18:11:45 -0500156static const GActionEntry ring_actions[] =
157{
Stepan Salenikovich24dcd5c2015-03-26 17:40:34 -0400158 { "accept", NULL, NULL, NULL, NULL, {0} },
159 { "hangup", NULL, NULL, NULL, NULL, {0} },
160 { "hold", NULL, NULL, "false", NULL, {0} },
161 { "quit", action_quit, NULL, NULL, NULL, {0} },
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400162 { "about", action_about, NULL, NULL, NULL, {0} },
Stepan Salenikovich943bd052015-09-04 17:42:08 -0400163 { "mute_audio", NULL, NULL, "false", NULL, {0} },
164 { "mute_video", NULL, NULL, "false", NULL, {0} },
165 { "record", NULL, NULL, "false", NULL, {0} },
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500166 /* TODO implement the other actions */
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500167 // { "transfer", NULL, NULL, "flase", NULL, {0} },
Stepan Salenikovich69771842015-02-24 18:11:45 -0500168};
169
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500170static void
171activate_action(GSimpleAction *action, G_GNUC_UNUSED GVariant *parameter, gpointer user_data)
172{
173 g_debug("activating action: %s", g_action_get_name(G_ACTION(action)));
174
175 int_ptr_t key;
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400176
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500177 key.ptr = user_data;
178 UserActionModel::Action a = static_cast<UserActionModel::Action>(key.value);
179 UserActionModel* uam = CallModel::instance()->userActionModel();
180
181 uam << a;
182}
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500183
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400184static void
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -0400185autostart_toggled(GSettings *settings, G_GNUC_UNUSED gchar *key, G_GNUC_UNUSED gpointer user_data)
186{
187 autostart_symlink(g_settings_get_boolean(settings, "start-on-login"));
188}
189
Stepan Salenikovichbc6c4be2015-07-31 16:07:54 -0400190static gboolean
191on_close_window(GtkWidget *window, G_GNUC_UNUSED GdkEvent *event, RingClient *client)
192{
193 g_return_val_if_fail(GTK_IS_WINDOW(window) && IS_RING_CLIENT(client), FALSE);
194
195 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
196
197 if (g_settings_get_boolean(priv->settings, "hide-on-close")) {
198 /* we want to simply hide the window and keep the client running */
199 g_debug("hiding main window");
200 gtk_widget_hide(window);
201 return TRUE; /* do not propogate event */
202 } else {
203 /* we want to quit the application, so just propogate the event */
204 return FALSE;
205 }
206}
207
208static void
209ring_client_activate(GApplication *app)
210{
211 RingClient *client = RING_CLIENT(app);
212 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
213
214 if (priv->win == NULL) {
215 priv->win = ring_main_window_new(GTK_APPLICATION(app));
216
217 /* make sure win is set to NULL when the window is destroyed */
218 g_object_add_weak_pointer(G_OBJECT(priv->win), (gpointer *)&priv->win);
219
220 /* check if the window should be destoryed or not on close */
221 g_signal_connect(priv->win, "delete-event", G_CALLBACK(on_close_window), client);
222 }
223
224 g_debug("show window");
225 gtk_window_present(GTK_WINDOW(priv->win));
226}
227
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -0400228static void
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400229ring_client_startup(GApplication *app)
Stepan Salenikovich69771842015-02-24 18:11:45 -0500230{
Stepan Salenikovich69771842015-02-24 18:11:45 -0500231 RingClient *client = RING_CLIENT(app);
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400232 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
Stepan Salenikovich69771842015-02-24 18:11:45 -0500233
Stepan Salenikovich4f9bb982015-06-23 14:26:30 -0400234 g_message("Ring GNOME client version: %d.%d.%d", VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH);
235 g_message("git ref: %s", RING_CLIENT_REVISION);
236
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -0400237 /* make sure that the system corresponds to the autostart setting */
238 autostart_symlink(g_settings_get_boolean(priv->settings, "start-on-login"));
239 g_signal_connect(priv->settings, "changed::start-on-login", G_CALLBACK(autostart_toggled), NULL);
240
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400241 /* init clutter */
242 int clutter_error;
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400243 if ((clutter_error = gtk_clutter_init(&priv->argc, &priv->argv)) != CLUTTER_INIT_SUCCESS) {
244 g_error("Could not init clutter : %d\n", clutter_error);
245 exit(1); /* the g_error above should normally cause the applicaiton to exit */
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400246 }
247
248 /* init libRingClient and make sure its connected to the dbus */
249 try {
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400250 priv->qtapp = new QCoreApplication(priv->argc, priv->argv);
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400251 /* the call model will try to connect to dring via dbus */
252 CallModel::instance();
253 } catch (const char * msg) {
254 init_exception_dialog(msg);
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400255 g_error("%s", msg);
256 exit(1); /* the g_error above should normally cause the applicaiton to exit */
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400257 } catch(QString& msg) {
258 QByteArray ba = msg.toLocal8Bit();
259 const char *c_str = ba.data();
260 init_exception_dialog(c_str);
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400261 g_error("%s", c_str);
262 exit(1); /* the g_error above should normally cause the applicaiton to exit */
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400263 }
264
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400265 /* init delegates */
Stepan Salenikovichbbd6c132015-08-20 15:21:48 -0400266 GlobalInstances::setPixmapManipulator(std::unique_ptr<Interfaces::PixbufManipulator>(new Interfaces::PixbufManipulator()));
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400267
Stepan Salenikovich75a39172015-07-10 13:21:08 -0400268 /* make sure all RING accounts have a display name... this basically makes sure
269 * that all accounts created before the display name patch have a display name
270 * set... a bit of a hack as this should maybe be done in LRC */
271 force_ring_display_name();
272
Stepan Salenikovichf2d76c52015-07-17 17:54:56 -0400273 /* make sure basic number categories exist, in case user has no contacts
274 * from which these would be automatically created
275 */
276 NumberCategoryModel::instance()->addCategory("work", QVariant());
277 NumberCategoryModel::instance()->addCategory("home", QVariant());
278
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400279 /* add backends */
Stepan Salenikovich1e131a42015-05-27 14:12:21 -0400280 CategorizedHistoryModel::instance()->addCollection<LocalHistoryCollection>(LoadOptions::FORCE_ENABLED);
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400281
Stepan Salenikovichf2d76c52015-07-17 17:54:56 -0400282 /* fallback backend for vcards */
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400283 PersonModel::instance()->addCollection<FallbackPersonCollection>(LoadOptions::FORCE_ENABLED);
284
Stepan Salenikovichf2d76c52015-07-17 17:54:56 -0400285 /* EDS backend(s) */
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400286 load_eds_sources(priv->cancellable);
287
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400288 /* Override theme since we don't have appropriate icons for a dark them (yet) */
289 GtkSettings *gtk_settings = gtk_settings_get_default();
290 g_object_set(G_OBJECT(gtk_settings), "gtk-application-prefer-dark-theme",
291 FALSE, NULL);
292 /* enable button icons */
293 g_object_set(G_OBJECT(gtk_settings), "gtk-button-images",
294 TRUE, NULL);
295
296 /* add GActions */
Stepan Salenikovich69771842015-02-24 18:11:45 -0500297 g_action_map_add_action_entries(
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400298 G_ACTION_MAP(app), ring_actions, G_N_ELEMENTS(ring_actions), app);
299
300 /* add accelerators */
301 ring_accelerators(RING_CLIENT(app));
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500302
Stepan Salenikovich347b73a2015-03-22 10:39:00 -0400303 /* Bind GActions to the UserActionModel */
304 UserActionModel* uam = CallModel::instance()->userActionModel();
305 QHash<int, GSimpleAction*> actionHash;
306 actionHash[ (int)UserActionModel::Action::ACCEPT ] = G_SIMPLE_ACTION(g_action_map_lookup_action(G_ACTION_MAP(app), "accept"));
307 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 -0400308 actionHash[ (int)UserActionModel::Action::MUTE_AUDIO ] = G_SIMPLE_ACTION(g_action_map_lookup_action(G_ACTION_MAP(app), "mute_audio"));
309 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 -0400310 /* TODO: add commented actions when ready */
Stepan Salenikovich347b73a2015-03-22 10:39:00 -0400311 // 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 -0400312 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 -0400313 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 -0500314
Stepan Salenikovich347b73a2015-03-22 10:39:00 -0400315 for (QHash<int,GSimpleAction*>::const_iterator i = actionHash.begin(); i != actionHash.end(); ++i) {
316 GSimpleAction* sa = i.value();
317 int_ptr_t user_data;
318 user_data.value = i.key();
319 g_signal_connect(G_OBJECT(sa), "activate", G_CALLBACK(activate_action), user_data.ptr);
320 }
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500321
Stepan Salenikovich347b73a2015-03-22 10:39:00 -0400322 /* change the state of the GActions based on the UserActionModel */
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400323 priv->uam_updated = QObject::connect(uam,&UserActionModel::dataChanged, [actionHash,uam](const QModelIndex& tl, const QModelIndex& br) {
Stepan Salenikovich347b73a2015-03-22 10:39:00 -0400324 const int first(tl.row()),last(br.row());
325 for(int i = first; i <= last;i++) {
326 const QModelIndex& idx = uam->index(i,0);
327 GSimpleAction* sa = actionHash[(int)qvariant_cast<UserActionModel::Action>(idx.data(UserActionModel::Role::ACTION))];
328 if (sa) {
329 /* enable/disable GAction based on UserActionModel */
330 g_simple_action_set_enabled(sa, idx.flags() & Qt::ItemIsEnabled);
331 /* set the state of the action if its stateful */
332 if (g_action_get_state_type(G_ACTION(sa)) != NULL)
333 g_simple_action_set_state(sa, g_variant_new_boolean(idx.data(Qt::CheckStateRole) == Qt::Checked));
334 }
335 }
336 });
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400337
Stepan Salenikovichbc6c4be2015-07-31 16:07:54 -0400338 /* show window on incoming calls (if the option is set)*/
339 QObject::connect(CallModel::instance(), &CallModel::incomingCall,
340 [app] (G_GNUC_UNUSED Call *call) {
341 RingClient *client = RING_CLIENT(app);
342 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
343 if (g_settings_get_boolean(priv->settings, "bring-window-to-front"))
344 ring_client_activate(app);
345 }
346 );
347
Stepan Salenikovich76c33e62015-05-22 12:24:07 -0400348 /* send call notifications */
349 ring_notify_init();
350 QObject::connect(CallModel::instance(), &CallModel::incomingCall,
Stepan Salenikovich67112d12015-06-16 16:57:06 -0400351 [] (Call *call) { ring_notify_incoming_call(call);}
Stepan Salenikovich76c33e62015-05-22 12:24:07 -0400352 );
353
Stepan Salenikovich67112d12015-06-16 16:57:06 -0400354 /* chat notifications for incoming messages on all calls which are not the
355 * currently selected call */
356 ring_notify_monitor_chat_notifications(client);
357
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400358#if GLIB_CHECK_VERSION(2,40,0)
359 G_APPLICATION_CLASS(ring_client_parent_class)->startup(app);
360#else
361 /* don't need to chain up to the parent callback as this function will
362 * be called manually by the command_line callback in this case */
363#endif
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400364}
365
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400366#if !GLIB_CHECK_VERSION(2,40,0)
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400367static int
368ring_client_command_line(GApplication *app, GApplicationCommandLine *cmdline)
369{
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400370 gint argc;
371 gchar **argv = g_application_command_line_get_arguments(cmdline, &argc);
372 GOptionContext *context = ring_client_options_get_context();
373 GError *error = NULL;
374 if (g_option_context_parse(context, &argc, &argv, &error) == FALSE) {
375 g_print(_("%s\nRun '%s --help' to see a full list of available command line options.\n"),
376 error->message, argv[0]);
Stepan Salenikovich8a287fc2015-05-01 16:53:20 -0400377 g_clear_error(&error);
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400378 g_option_context_free(context);
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400379 g_strfreev(argv);
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400380 return 1;
381 }
382 g_option_context_free(context);
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400383 g_strfreev(argv);
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400384
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400385 if (!g_application_get_is_remote(app)) {
386 /* if this is the primary instance, we must peform the startup */
387 ring_client_startup(app);
388 }
389
390 g_application_activate(app);
391
392 return 0;
393}
394#endif
395
396static void
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500397ring_client_shutdown(GApplication *app)
398{
399 RingClient *self = RING_CLIENT(app);
400 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(self);
401
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400402 g_debug("quitting");
403
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400404 /* cancel any pending cancellable operations */
405 g_cancellable_cancel(priv->cancellable);
406 g_object_unref(priv->cancellable);
407
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400408 QObject::disconnect(priv->uam_updated);
409
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500410 /* free the QCoreApplication, which will destroy all libRingClient models
411 * and thus send the Unregister signal over dbus to dring */
412 delete priv->qtapp;
413
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400414 /* free the copied cmd line args */
415 g_strfreev(priv->argv);
416
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -0400417 g_clear_object(&priv->settings);
418
Stepan Salenikovich76c33e62015-05-22 12:24:07 -0400419 ring_notify_uninit();
420
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500421 /* Chain up to the parent class */
422 G_APPLICATION_CLASS(ring_client_parent_class)->shutdown(app);
423}
424
425static void
426ring_client_init(RingClient *self)
427{
428 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(self);
429
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500430 priv->win = NULL;
431 priv->qtapp = NULL;
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400432 priv->cancellable = g_cancellable_new();
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -0400433 priv->settings = g_settings_new_full(get_ring_schema(), NULL, NULL);
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400434
435#if GLIB_CHECK_VERSION(2,40,0)
436 /* add custom cmd line options */
437 ring_client_add_options(G_APPLICATION(self));
438#endif
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500439}
440
441static void
442ring_client_class_init(RingClientClass *klass)
443{
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400444#if GLIB_CHECK_VERSION(2,40,0)
445 G_APPLICATION_CLASS(klass)->startup = ring_client_startup;
446#else
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500447 G_APPLICATION_CLASS(klass)->command_line = ring_client_command_line;
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400448#endif
449 G_APPLICATION_CLASS(klass)->activate = ring_client_activate;
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500450 G_APPLICATION_CLASS(klass)->shutdown = ring_client_shutdown;
451}
452
453RingClient *
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400454ring_client_new(int argc, char *argv[])
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500455{
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400456 /* because the g_application_add_main_option_entries was only added in
457 * glib 2.40, for lower versions we must handle the command line options
458 * ourselves
459 */
460 RingClient *client = (RingClient *)g_object_new(ring_client_get_type(),
Stepan Salenikovich76350582015-05-25 14:56:32 -0400461 "application-id", RING_CLIENT_APP_ID,
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400462#if GLIB_CHECK_VERSION(2,40,0)
463 NULL);
464#else
465 "flags", G_APPLICATION_HANDLES_COMMAND_LINE,
466 NULL);
467#endif
468
469 /* copy the cmd line args before they get processed by the GApplication*/
470 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
471 priv->argc = argc;
472 priv->argv = g_strdupv((gchar **)argv);
473
474 return client;
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500475}
Stepan Salenikovich67112d12015-06-16 16:57:06 -0400476
477GtkWindow *
478ring_client_get_main_windw(RingClient *client)
479{
480 g_return_val_if_fail(IS_RING_CLIENT(client), NULL);
481 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
482
483 return (GtkWindow *)priv->win;
484}