blob: 828f336dbbd3d566e883452eb02beb69daabfee7 [file] [log] [blame]
Stepan Salenikovichd81ef292015-02-17 18:47:37 -05001/*
2 * Copyright (C) 2015 Savoir-Faire Linux Inc.
3 * 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
24 * terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
25 * 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 Salenikovichd81ef292015-02-17 18:47:37 -050046
47#include "ring_client_options.h"
Stepan Salenikovich434b88f2015-02-19 17:49:08 -050048#include "ringmainwindow.h"
Stepan Salenikovich15142182015-03-11 17:15:26 -040049#include "backends/minimalhistorybackend.h"
Stepan Salenikovich24dcd5c2015-03-26 17:40:34 -040050#include "dialogs.h"
Stepan Salenikovich6f687072015-03-26 10:43:37 -040051#include "backends/edscontactbackend.h"
52#include "delegates/pixbufdelegate.h"
Stepan Salenikovich76c33e62015-05-22 12:24:07 -040053#include "ringnotify.h"
Stepan Salenikovich76350582015-05-25 14:56:32 -040054#include "config.h"
Stepan Salenikovich434b88f2015-02-19 17:49:08 -050055
56struct _RingClientClass
57{
58 GtkApplicationClass parent_class;
59};
60
61struct _RingClient
62{
63 GtkApplication parent;
64};
65
66typedef struct _RingClientPrivate RingClientPrivate;
Stepan Salenikovichd81ef292015-02-17 18:47:37 -050067
68struct _RingClientPrivate {
Stepan Salenikovich0d515e52015-05-19 16:31:05 -040069 /* args */
70 int argc;
71 char **argv;
Stepan Salenikovichd81ef292015-02-17 18:47:37 -050072 /* main window */
73 GtkWidget *win;
74 /* for libRingclient */
75 QCoreApplication *qtapp;
Stepan Salenikovich068fb692015-03-23 14:58:32 -040076 /* UAM */
77 QMetaObject::Connection uam_updated;
Stepan Salenikovich6f687072015-03-26 10:43:37 -040078
79 GCancellable *cancellable;
Stepan Salenikovichd81ef292015-02-17 18:47:37 -050080};
81
Stepan Salenikovich0d515e52015-05-19 16:31:05 -040082/* this union is used to pass ints as pointers and vice versa for GAction parameters*/
83typedef union _int_ptr_t
84{
85 int value;
86 gint64 value64;
87 gpointer ptr;
88} int_ptr_t;
89
Stepan Salenikovich434b88f2015-02-19 17:49:08 -050090G_DEFINE_TYPE_WITH_PRIVATE(RingClient, ring_client, GTK_TYPE_APPLICATION);
Stepan Salenikovichd81ef292015-02-17 18:47:37 -050091
92#define RING_CLIENT_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), RING_CLIENT_TYPE, RingClientPrivate))
93
94static void
95init_exception_dialog(const char* msg)
96{
97 g_warning("%s", msg);
98 GtkWidget *dialog = gtk_message_dialog_new(NULL,
99 (GtkDialogFlags)(GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT),
100 GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
101 _("Unable to initialize.\nMake sure the Ring daemon (dring) is running.\nError: %s"),
102 msg);
103
104 gtk_window_set_title(GTK_WINDOW(dialog), _("Ring Error"));
105 gtk_dialog_run(GTK_DIALOG(dialog));
106 gtk_widget_destroy(dialog);
107}
108
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400109static void
110ring_accelerators(RingClient *client)
111{
Stepan Salenikovich39ee49a2015-03-24 12:44:55 -0400112#if GTK_CHECK_VERSION(3,12,0)
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400113 const gchar *quit_accels[2] = { "<Ctrl>Q", NULL };
114 gtk_application_set_accels_for_action(GTK_APPLICATION(client), "app.quit", quit_accels);
115#else
116 gtk_application_add_accelerator(GTK_APPLICATION(client), "<Control>Q", "win.quit", NULL);
117#endif
118}
119
120static void
121action_quit(G_GNUC_UNUSED GSimpleAction *simple,
122 G_GNUC_UNUSED GVariant *parameter,
123 gpointer user_data)
124{
125 g_return_if_fail(G_IS_APPLICATION(user_data));
126
127#if GLIB_CHECK_VERSION(2,32,0)
128 g_application_quit(G_APPLICATION(user_data));
129#else
130 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(user_data);
131 gtk_widget_destroy(priv->win);
132#endif
133}
134
Stepan Salenikovich24dcd5c2015-03-26 17:40:34 -0400135static void
136action_about(G_GNUC_UNUSED GSimpleAction *simple,
137 G_GNUC_UNUSED GVariant *parameter,
138 gpointer user_data)
139{
140 g_return_if_fail(G_IS_APPLICATION(user_data));
141 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(user_data);
142
143 ring_about_dialog(priv->win);
144}
145
Stepan Salenikovich69771842015-02-24 18:11:45 -0500146static const GActionEntry ring_actions[] =
147{
Stepan Salenikovich24dcd5c2015-03-26 17:40:34 -0400148 { "accept", NULL, NULL, NULL, NULL, {0} },
149 { "hangup", NULL, NULL, NULL, NULL, {0} },
150 { "hold", NULL, NULL, "false", NULL, {0} },
151 { "quit", action_quit, NULL, NULL, NULL, {0} },
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400152 { "about", action_about, NULL, NULL, NULL, {0} },
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500153 /* TODO implement the other actions */
154 // { "mute_audio", NULL, NULL, "false", NULL, {0} },
155 // { "mute_video", NULL, NULL, "false", NULL, {0} },
156 // { "transfer", NULL, NULL, "flase", NULL, {0} },
157 // { "record", NULL, NULL, "false", NULL, {0} }
Stepan Salenikovich69771842015-02-24 18:11:45 -0500158};
159
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500160static void
161activate_action(GSimpleAction *action, G_GNUC_UNUSED GVariant *parameter, gpointer user_data)
162{
163 g_debug("activating action: %s", g_action_get_name(G_ACTION(action)));
164
165 int_ptr_t key;
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400166
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500167 key.ptr = user_data;
168 UserActionModel::Action a = static_cast<UserActionModel::Action>(key.value);
169 UserActionModel* uam = CallModel::instance()->userActionModel();
170
171 uam << a;
172}
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500173
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400174static void
175ring_client_startup(GApplication *app)
Stepan Salenikovich69771842015-02-24 18:11:45 -0500176{
Stepan Salenikovich69771842015-02-24 18:11:45 -0500177 RingClient *client = RING_CLIENT(app);
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400178 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
Stepan Salenikovich69771842015-02-24 18:11:45 -0500179
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400180 /* init clutter */
181 int clutter_error;
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400182 if ((clutter_error = gtk_clutter_init(&priv->argc, &priv->argv)) != CLUTTER_INIT_SUCCESS) {
183 g_error("Could not init clutter : %d\n", clutter_error);
184 exit(1); /* the g_error above should normally cause the applicaiton to exit */
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400185 }
186
187 /* init libRingClient and make sure its connected to the dbus */
188 try {
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400189 priv->qtapp = new QCoreApplication(priv->argc, priv->argv);
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400190 /* the call model will try to connect to dring via dbus */
191 CallModel::instance();
192 } catch (const char * msg) {
193 init_exception_dialog(msg);
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400194 g_error("%s", msg);
195 exit(1); /* the g_error above should normally cause the applicaiton to exit */
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400196 } catch(QString& msg) {
197 QByteArray ba = msg.toLocal8Bit();
198 const char *c_str = ba.data();
199 init_exception_dialog(c_str);
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400200 g_error("%s", c_str);
201 exit(1); /* the g_error above should normally cause the applicaiton to exit */
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400202 }
203
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400204 /* init delegates */
205 /* FIXME: put in smart pointer? */
206 new PixbufDelegate();
207
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400208 /* add backends */
209 CategorizedHistoryModel::instance()->addCollection<MinimalHistoryBackend>(LoadOptions::FORCE_ENABLED);
210
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400211 PersonModel::instance()->addCollection<FallbackPersonCollection>(LoadOptions::FORCE_ENABLED);
212
213 /* TODO: should a local vcard location be added ?
214 * PersonModel::instance()->addCollection<FallbackPersonCollection, QString>(
215 * QStandardPaths::writableLocation(QStandardPaths::DataLocation)+QLatin1Char('/')+"vcard",
216 * LoadOptions::FORCE_ENABLED);
217 */
218
219 /* EDS backend */
220 load_eds_sources(priv->cancellable);
221
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400222 /* Override theme since we don't have appropriate icons for a dark them (yet) */
223 GtkSettings *gtk_settings = gtk_settings_get_default();
224 g_object_set(G_OBJECT(gtk_settings), "gtk-application-prefer-dark-theme",
225 FALSE, NULL);
226 /* enable button icons */
227 g_object_set(G_OBJECT(gtk_settings), "gtk-button-images",
228 TRUE, NULL);
229
230 /* add GActions */
Stepan Salenikovich69771842015-02-24 18:11:45 -0500231 g_action_map_add_action_entries(
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400232 G_ACTION_MAP(app), ring_actions, G_N_ELEMENTS(ring_actions), app);
233
234 /* add accelerators */
235 ring_accelerators(RING_CLIENT(app));
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500236
Stepan Salenikovich347b73a2015-03-22 10:39:00 -0400237 /* Bind GActions to the UserActionModel */
238 UserActionModel* uam = CallModel::instance()->userActionModel();
239 QHash<int, GSimpleAction*> actionHash;
240 actionHash[ (int)UserActionModel::Action::ACCEPT ] = G_SIMPLE_ACTION(g_action_map_lookup_action(G_ACTION_MAP(app), "accept"));
241 actionHash[ (int)UserActionModel::Action::HOLD ] = G_SIMPLE_ACTION(g_action_map_lookup_action(G_ACTION_MAP(app), "hold"));
242 /* TODO: add commented actions when ready */
243 // actionHash[ (int)UserActionModel::Action::MUTE_AUDIO ] = G_SIMPLE_ACTION(g_action_map_lookup_action(G_ACTION_MAP(app), "mute_audio"));
244 // actionHash[ (int)UserActionModel::Action::SERVER_TRANSFER ] = G_SIMPLE_ACTION(g_action_map_lookup_action(G_ACTION_MAP(app), "transfer"));
245 // actionHash[ (int)UserActionModel::Action::RECORD ] = G_SIMPLE_ACTION(g_action_map_lookup_action(G_ACTION_MAP(app), "record"));
246 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 -0500247
Stepan Salenikovich347b73a2015-03-22 10:39:00 -0400248 for (QHash<int,GSimpleAction*>::const_iterator i = actionHash.begin(); i != actionHash.end(); ++i) {
249 GSimpleAction* sa = i.value();
250 int_ptr_t user_data;
251 user_data.value = i.key();
252 g_signal_connect(G_OBJECT(sa), "activate", G_CALLBACK(activate_action), user_data.ptr);
253 }
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500254
Stepan Salenikovich347b73a2015-03-22 10:39:00 -0400255 /* change the state of the GActions based on the UserActionModel */
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400256 priv->uam_updated = QObject::connect(uam,&UserActionModel::dataChanged, [actionHash,uam](const QModelIndex& tl, const QModelIndex& br) {
Stepan Salenikovich347b73a2015-03-22 10:39:00 -0400257 const int first(tl.row()),last(br.row());
258 for(int i = first; i <= last;i++) {
259 const QModelIndex& idx = uam->index(i,0);
260 GSimpleAction* sa = actionHash[(int)qvariant_cast<UserActionModel::Action>(idx.data(UserActionModel::Role::ACTION))];
261 if (sa) {
262 /* enable/disable GAction based on UserActionModel */
263 g_simple_action_set_enabled(sa, idx.flags() & Qt::ItemIsEnabled);
264 /* set the state of the action if its stateful */
265 if (g_action_get_state_type(G_ACTION(sa)) != NULL)
266 g_simple_action_set_state(sa, g_variant_new_boolean(idx.data(Qt::CheckStateRole) == Qt::Checked));
267 }
268 }
269 });
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400270
Stepan Salenikovich76c33e62015-05-22 12:24:07 -0400271 /* send call notifications */
272 ring_notify_init();
273 QObject::connect(CallModel::instance(), &CallModel::incomingCall,
274 [=] (Call *call) { ring_notify_incoming_call(call);}
275 );
276
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400277#if GLIB_CHECK_VERSION(2,40,0)
278 G_APPLICATION_CLASS(ring_client_parent_class)->startup(app);
279#else
280 /* don't need to chain up to the parent callback as this function will
281 * be called manually by the command_line callback in this case */
282#endif
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400283}
284
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400285#if !GLIB_CHECK_VERSION(2,40,0)
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400286static int
287ring_client_command_line(GApplication *app, GApplicationCommandLine *cmdline)
288{
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400289 gint argc;
290 gchar **argv = g_application_command_line_get_arguments(cmdline, &argc);
291 GOptionContext *context = ring_client_options_get_context();
292 GError *error = NULL;
293 if (g_option_context_parse(context, &argc, &argv, &error) == FALSE) {
294 g_print(_("%s\nRun '%s --help' to see a full list of available command line options.\n"),
295 error->message, argv[0]);
Stepan Salenikovich8a287fc2015-05-01 16:53:20 -0400296 g_clear_error(&error);
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400297 g_option_context_free(context);
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400298 g_strfreev(argv);
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400299 return 1;
300 }
301 g_option_context_free(context);
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400302 g_strfreev(argv);
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400303
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400304 if (!g_application_get_is_remote(app)) {
305 /* if this is the primary instance, we must peform the startup */
306 ring_client_startup(app);
307 }
308
309 g_application_activate(app);
310
311 return 0;
312}
313#endif
314
315static void
316ring_client_activate(GApplication *app)
317{
318 RingClient *client = RING_CLIENT(app);
319 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
320
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400321 if (priv->win == NULL) {
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400322 priv->win = ring_main_window_new(GTK_APPLICATION(app));
323 }
324
325 gtk_window_present(GTK_WINDOW(priv->win));
Stepan Salenikovich69771842015-02-24 18:11:45 -0500326}
327
328static void
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500329ring_client_shutdown(GApplication *app)
330{
331 RingClient *self = RING_CLIENT(app);
332 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(self);
333
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400334 g_debug("quitting");
335
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400336 /* cancel any pending cancellable operations */
337 g_cancellable_cancel(priv->cancellable);
338 g_object_unref(priv->cancellable);
339
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400340 QObject::disconnect(priv->uam_updated);
341
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500342 /* free the QCoreApplication, which will destroy all libRingClient models
343 * and thus send the Unregister signal over dbus to dring */
344 delete priv->qtapp;
345
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400346 /* free the copied cmd line args */
347 g_strfreev(priv->argv);
348
Stepan Salenikovich76c33e62015-05-22 12:24:07 -0400349 ring_notify_uninit();
350
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500351 /* Chain up to the parent class */
352 G_APPLICATION_CLASS(ring_client_parent_class)->shutdown(app);
353}
354
355static void
356ring_client_init(RingClient *self)
357{
358 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(self);
359
360 /* init widget */
361 priv->win = NULL;
362 priv->qtapp = NULL;
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400363 priv->cancellable = g_cancellable_new();
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400364
365#if GLIB_CHECK_VERSION(2,40,0)
366 /* add custom cmd line options */
367 ring_client_add_options(G_APPLICATION(self));
368#endif
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500369}
370
371static void
372ring_client_class_init(RingClientClass *klass)
373{
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400374#if GLIB_CHECK_VERSION(2,40,0)
375 G_APPLICATION_CLASS(klass)->startup = ring_client_startup;
376#else
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500377 G_APPLICATION_CLASS(klass)->command_line = ring_client_command_line;
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400378#endif
379 G_APPLICATION_CLASS(klass)->activate = ring_client_activate;
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500380 G_APPLICATION_CLASS(klass)->shutdown = ring_client_shutdown;
381}
382
383RingClient *
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400384ring_client_new(int argc, char *argv[])
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500385{
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400386 /* because the g_application_add_main_option_entries was only added in
387 * glib 2.40, for lower versions we must handle the command line options
388 * ourselves
389 */
390 RingClient *client = (RingClient *)g_object_new(ring_client_get_type(),
Stepan Salenikovich76350582015-05-25 14:56:32 -0400391 "application-id", RING_CLIENT_APP_ID,
Stepan Salenikovich0d515e52015-05-19 16:31:05 -0400392#if GLIB_CHECK_VERSION(2,40,0)
393 NULL);
394#else
395 "flags", G_APPLICATION_HANDLES_COMMAND_LINE,
396 NULL);
397#endif
398
399 /* copy the cmd line args before they get processed by the GApplication*/
400 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
401 priv->argc = argc;
402 priv->argv = g_strdupv((gchar **)argv);
403
404 return client;
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500405}