blob: 59c08dc459f1d4c9f6efb744da5d605abe9807b4 [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 Salenikovichd81ef292015-02-17 18:47:37 -050043
44#include "ring_client_options.h"
Stepan Salenikovich434b88f2015-02-19 17:49:08 -050045#include "ringmainwindow.h"
Stepan Salenikovich15142182015-03-11 17:15:26 -040046#include "backends/minimalhistorybackend.h"
Stepan Salenikovich24dcd5c2015-03-26 17:40:34 -040047#include "dialogs.h"
Stepan Salenikovich434b88f2015-02-19 17:49:08 -050048
49struct _RingClientClass
50{
51 GtkApplicationClass parent_class;
52};
53
54struct _RingClient
55{
56 GtkApplication parent;
57};
58
59typedef struct _RingClientPrivate RingClientPrivate;
Stepan Salenikovichd81ef292015-02-17 18:47:37 -050060
61struct _RingClientPrivate {
62 /* main window */
63 GtkWidget *win;
64 /* for libRingclient */
65 QCoreApplication *qtapp;
Stepan Salenikovich068fb692015-03-23 14:58:32 -040066 /* UAM */
67 QMetaObject::Connection uam_updated;
Stepan Salenikovichd81ef292015-02-17 18:47:37 -050068};
69
Stepan Salenikovich434b88f2015-02-19 17:49:08 -050070G_DEFINE_TYPE_WITH_PRIVATE(RingClient, ring_client, GTK_TYPE_APPLICATION);
Stepan Salenikovichd81ef292015-02-17 18:47:37 -050071
72#define RING_CLIENT_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), RING_CLIENT_TYPE, RingClientPrivate))
73
74static void
75init_exception_dialog(const char* msg)
76{
77 g_warning("%s", msg);
78 GtkWidget *dialog = gtk_message_dialog_new(NULL,
79 (GtkDialogFlags)(GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT),
80 GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
81 _("Unable to initialize.\nMake sure the Ring daemon (dring) is running.\nError: %s"),
82 msg);
83
84 gtk_window_set_title(GTK_WINDOW(dialog), _("Ring Error"));
85 gtk_dialog_run(GTK_DIALOG(dialog));
86 gtk_widget_destroy(dialog);
87}
88
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -040089static void
90ring_accelerators(RingClient *client)
91{
Stepan Salenikovich39ee49a2015-03-24 12:44:55 -040092#if GTK_CHECK_VERSION(3,12,0)
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -040093 const gchar *quit_accels[2] = { "<Ctrl>Q", NULL };
94 gtk_application_set_accels_for_action(GTK_APPLICATION(client), "app.quit", quit_accels);
95#else
96 gtk_application_add_accelerator(GTK_APPLICATION(client), "<Control>Q", "win.quit", NULL);
97#endif
98}
99
100static void
101action_quit(G_GNUC_UNUSED GSimpleAction *simple,
102 G_GNUC_UNUSED GVariant *parameter,
103 gpointer user_data)
104{
105 g_return_if_fail(G_IS_APPLICATION(user_data));
106
107#if GLIB_CHECK_VERSION(2,32,0)
108 g_application_quit(G_APPLICATION(user_data));
109#else
110 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(user_data);
111 gtk_widget_destroy(priv->win);
112#endif
113}
114
Stepan Salenikovich24dcd5c2015-03-26 17:40:34 -0400115static void
116action_about(G_GNUC_UNUSED GSimpleAction *simple,
117 G_GNUC_UNUSED GVariant *parameter,
118 gpointer user_data)
119{
120 g_return_if_fail(G_IS_APPLICATION(user_data));
121 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(user_data);
122
123 ring_about_dialog(priv->win);
124}
125
Stepan Salenikovich69771842015-02-24 18:11:45 -0500126static const GActionEntry ring_actions[] =
127{
Stepan Salenikovich24dcd5c2015-03-26 17:40:34 -0400128 { "accept", NULL, NULL, NULL, NULL, {0} },
129 { "hangup", NULL, NULL, NULL, NULL, {0} },
130 { "hold", NULL, NULL, "false", NULL, {0} },
131 { "quit", action_quit, NULL, NULL, NULL, {0} },
132 { "about", action_about, NULL, NULL, NULL, {0} }
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500133 /* TODO implement the other actions */
134 // { "mute_audio", NULL, NULL, "false", NULL, {0} },
135 // { "mute_video", NULL, NULL, "false", NULL, {0} },
136 // { "transfer", NULL, NULL, "flase", NULL, {0} },
137 // { "record", NULL, NULL, "false", NULL, {0} }
Stepan Salenikovich69771842015-02-24 18:11:45 -0500138};
139
Stepan Salenikovich347b73a2015-03-22 10:39:00 -0400140/* this union is used to pass the int refering to the Action as a parameter to the GAction callback */
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500141typedef union _int_ptr_t
142{
143 int value;
144 gpointer ptr;
145} int_ptr_t;
146
147static void
148activate_action(GSimpleAction *action, G_GNUC_UNUSED GVariant *parameter, gpointer user_data)
149{
150 g_debug("activating action: %s", g_action_get_name(G_ACTION(action)));
151
152 int_ptr_t key;
153 key.ptr = user_data;
154 UserActionModel::Action a = static_cast<UserActionModel::Action>(key.value);
155 UserActionModel* uam = CallModel::instance()->userActionModel();
156
157 uam << a;
158}
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500159
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400160static int
161ring_client_startup(GApplication *app, gint argc, gchar **argv)
Stepan Salenikovich69771842015-02-24 18:11:45 -0500162{
163 G_APPLICATION_CLASS(ring_client_parent_class)->startup(app);
164
165 RingClient *client = RING_CLIENT(app);
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400166 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
Stepan Salenikovich69771842015-02-24 18:11:45 -0500167
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400168 /* init clutter */
169 int clutter_error;
170 if ((clutter_error = gtk_clutter_init(&argc, &argv)) != CLUTTER_INIT_SUCCESS) {
171 g_critical("Could not init clutter : %d\n", clutter_error);
172 return 1;
173 }
174
175 /* init libRingClient and make sure its connected to the dbus */
176 try {
177 priv->qtapp = new QCoreApplication(argc, argv);
178 /* the call model will try to connect to dring via dbus */
179 CallModel::instance();
180 } catch (const char * msg) {
181 init_exception_dialog(msg);
182 g_critical("%s", msg);
183 return 1;
184 } catch(QString& msg) {
185 QByteArray ba = msg.toLocal8Bit();
186 const char *c_str = ba.data();
187 init_exception_dialog(c_str);
188 g_critical("%s", c_str);
189 return 1;
190 }
191
192 /* add backends */
193 CategorizedHistoryModel::instance()->addCollection<MinimalHistoryBackend>(LoadOptions::FORCE_ENABLED);
194
195 /* Override theme since we don't have appropriate icons for a dark them (yet) */
196 GtkSettings *gtk_settings = gtk_settings_get_default();
197 g_object_set(G_OBJECT(gtk_settings), "gtk-application-prefer-dark-theme",
198 FALSE, NULL);
199 /* enable button icons */
200 g_object_set(G_OBJECT(gtk_settings), "gtk-button-images",
201 TRUE, NULL);
202
203 /* add GActions */
Stepan Salenikovich69771842015-02-24 18:11:45 -0500204 g_action_map_add_action_entries(
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400205 G_ACTION_MAP(app), ring_actions, G_N_ELEMENTS(ring_actions), app);
206
207 /* add accelerators */
208 ring_accelerators(RING_CLIENT(app));
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500209
Stepan Salenikovich347b73a2015-03-22 10:39:00 -0400210 /* Bind GActions to the UserActionModel */
211 UserActionModel* uam = CallModel::instance()->userActionModel();
212 QHash<int, GSimpleAction*> actionHash;
213 actionHash[ (int)UserActionModel::Action::ACCEPT ] = G_SIMPLE_ACTION(g_action_map_lookup_action(G_ACTION_MAP(app), "accept"));
214 actionHash[ (int)UserActionModel::Action::HOLD ] = G_SIMPLE_ACTION(g_action_map_lookup_action(G_ACTION_MAP(app), "hold"));
215 /* TODO: add commented actions when ready */
216 // actionHash[ (int)UserActionModel::Action::MUTE_AUDIO ] = G_SIMPLE_ACTION(g_action_map_lookup_action(G_ACTION_MAP(app), "mute_audio"));
217 // actionHash[ (int)UserActionModel::Action::SERVER_TRANSFER ] = G_SIMPLE_ACTION(g_action_map_lookup_action(G_ACTION_MAP(app), "transfer"));
218 // actionHash[ (int)UserActionModel::Action::RECORD ] = G_SIMPLE_ACTION(g_action_map_lookup_action(G_ACTION_MAP(app), "record"));
219 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 -0500220
Stepan Salenikovich347b73a2015-03-22 10:39:00 -0400221 for (QHash<int,GSimpleAction*>::const_iterator i = actionHash.begin(); i != actionHash.end(); ++i) {
222 GSimpleAction* sa = i.value();
223 int_ptr_t user_data;
224 user_data.value = i.key();
225 g_signal_connect(G_OBJECT(sa), "activate", G_CALLBACK(activate_action), user_data.ptr);
226 }
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500227
Stepan Salenikovich347b73a2015-03-22 10:39:00 -0400228 /* change the state of the GActions based on the UserActionModel */
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400229 priv->uam_updated = QObject::connect(uam,&UserActionModel::dataChanged, [actionHash,uam](const QModelIndex& tl, const QModelIndex& br) {
Stepan Salenikovich347b73a2015-03-22 10:39:00 -0400230 const int first(tl.row()),last(br.row());
231 for(int i = first; i <= last;i++) {
232 const QModelIndex& idx = uam->index(i,0);
233 GSimpleAction* sa = actionHash[(int)qvariant_cast<UserActionModel::Action>(idx.data(UserActionModel::Role::ACTION))];
234 if (sa) {
235 /* enable/disable GAction based on UserActionModel */
236 g_simple_action_set_enabled(sa, idx.flags() & Qt::ItemIsEnabled);
237 /* set the state of the action if its stateful */
238 if (g_action_get_state_type(G_ACTION(sa)) != NULL)
239 g_simple_action_set_state(sa, g_variant_new_boolean(idx.data(Qt::CheckStateRole) == Qt::Checked));
240 }
241 }
242 });
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400243
244 return 0;
245}
246
247static int
248ring_client_command_line(GApplication *app, GApplicationCommandLine *cmdline)
249{
250 RingClient *client = RING_CLIENT(app);
251 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
252
253 gint argc;
254 gchar **argv = g_application_command_line_get_arguments(cmdline, &argc);
255 GOptionContext *context = ring_client_options_get_context();
256 GError *error = NULL;
257 if (g_option_context_parse(context, &argc, &argv, &error) == FALSE) {
258 g_print(_("%s\nRun '%s --help' to see a full list of available command line options.\n"),
259 error->message, argv[0]);
260 g_error_free(error);
261 g_option_context_free(context);
262 return 1;
263 }
264 g_option_context_free(context);
265
266 /* init libs and create main window only once */
267 if (priv->win == NULL) {
268 if (ring_client_startup(app, argc, argv) != 0)
269 return 1;
270 priv->win = ring_main_window_new(GTK_APPLICATION(app));
271 }
272
273 gtk_window_present(GTK_WINDOW(priv->win));
274
275 return 0;
Stepan Salenikovich69771842015-02-24 18:11:45 -0500276}
277
278static void
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500279ring_client_shutdown(GApplication *app)
280{
281 RingClient *self = RING_CLIENT(app);
282 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(self);
283
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400284 g_debug("quitting");
285
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400286 QObject::disconnect(priv->uam_updated);
287
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500288 /* free the QCoreApplication, which will destroy all libRingClient models
289 * and thus send the Unregister signal over dbus to dring */
290 delete priv->qtapp;
291
292 /* Chain up to the parent class */
293 G_APPLICATION_CLASS(ring_client_parent_class)->shutdown(app);
294}
295
296static void
297ring_client_init(RingClient *self)
298{
299 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(self);
300
301 /* init widget */
302 priv->win = NULL;
303 priv->qtapp = NULL;
304}
305
306static void
307ring_client_class_init(RingClientClass *klass)
308{
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500309 G_APPLICATION_CLASS(klass)->command_line = ring_client_command_line;
310 G_APPLICATION_CLASS(klass)->shutdown = ring_client_shutdown;
311}
312
313RingClient *
314ring_client_new()
315{
316 return (RingClient *)g_object_new(ring_client_get_type(),
Stepan Salenikovich434b88f2015-02-19 17:49:08 -0500317 "application-id", "cx.ring.RingGnome",
318 "flags", G_APPLICATION_HANDLES_COMMAND_LINE, NULL);
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500319}