blob: 31a512a91f4c5c84af2faa18ea7d54910bc4741c [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 Salenikovich434b88f2015-02-19 17:49:08 -050047
48struct _RingClientClass
49{
50 GtkApplicationClass parent_class;
51};
52
53struct _RingClient
54{
55 GtkApplication parent;
56};
57
58typedef struct _RingClientPrivate RingClientPrivate;
Stepan Salenikovichd81ef292015-02-17 18:47:37 -050059
60struct _RingClientPrivate {
61 /* main window */
62 GtkWidget *win;
63 /* for libRingclient */
64 QCoreApplication *qtapp;
65};
66
Stepan Salenikovich434b88f2015-02-19 17:49:08 -050067G_DEFINE_TYPE_WITH_PRIVATE(RingClient, ring_client, GTK_TYPE_APPLICATION);
Stepan Salenikovichd81ef292015-02-17 18:47:37 -050068
69#define RING_CLIENT_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), RING_CLIENT_TYPE, RingClientPrivate))
70
71static void
72init_exception_dialog(const char* msg)
73{
74 g_warning("%s", msg);
75 GtkWidget *dialog = gtk_message_dialog_new(NULL,
76 (GtkDialogFlags)(GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT),
77 GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
78 _("Unable to initialize.\nMake sure the Ring daemon (dring) is running.\nError: %s"),
79 msg);
80
81 gtk_window_set_title(GTK_WINDOW(dialog), _("Ring Error"));
82 gtk_dialog_run(GTK_DIALOG(dialog));
83 gtk_widget_destroy(dialog);
84}
85
86static int
87ring_client_command_line(GApplication *app, GApplicationCommandLine *cmdline)
88{
89 RingClient *client = RING_CLIENT(app);
90 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
91
92 gint argc;
93 gchar **argv = g_application_command_line_get_arguments(cmdline, &argc);
94 GOptionContext *context = ring_client_options_get_context();
95 GError *error = NULL;
96 if (g_option_context_parse(context, &argc, &argv, &error) == FALSE) {
97 g_print(_("%s\nRun '%s --help' to see a full list of available command line options.\n"),
98 error->message, argv[0]);
99 g_error_free(error);
100 g_option_context_free(context);
101 return 1;
102 }
103 g_option_context_free(context);
104
Stepan Salenikovich36c025c2015-03-03 19:06:44 -0500105 /* init clutter */
106 int clutter_error;
107 if ((clutter_error = gtk_clutter_init(&argc, &argv)) != CLUTTER_INIT_SUCCESS) {
108 g_critical("Could not init clutter : %d\n", clutter_error);
109 return 1;
110 }
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500111
112 /* init libRingClient and make sure its connected to the dbus */
113 try {
114 /* TODO: do we care about passing the cmd line arguments here? */
115 priv->qtapp = new QCoreApplication(argc, argv);
116 /* the call model will try to connect to dring via dbus */
117 CallModel::instance();
118 } catch (const char * msg) {
119 init_exception_dialog(msg);
120 return 1;
121 } catch(QString& msg) {
122 QByteArray ba = msg.toLocal8Bit();
123 const char *c_str = ba.data();
124 init_exception_dialog(c_str);
125 return 1;
126 }
127
Stepan Salenikovich15142182015-03-11 17:15:26 -0400128 /* add backends */
Stepan Salenikovichdd84cf92015-03-19 21:38:19 -0400129 CategorizedHistoryModel::instance()->addCollection<MinimalHistoryBackend>(LoadOptions::FORCE_ENABLED);
Stepan Salenikovich15142182015-03-11 17:15:26 -0400130
Stepan Salenikovich36c025c2015-03-03 19:06:44 -0500131 /* Override theme since we don't have appropriate icons for a dark them (yet) */
132 GtkSettings *gtk_settings = gtk_settings_get_default();
133 g_object_set(G_OBJECT(gtk_settings), "gtk-application-prefer-dark-theme",
134 FALSE, NULL);
135 /* enable button icons */
136 g_object_set(G_OBJECT(gtk_settings), "gtk-button-images",
137 TRUE, NULL);
138
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500139 /* create an empty window */
Stepan Salenikovich434b88f2015-02-19 17:49:08 -0500140 if (priv->win == NULL) {
141 priv->win = ring_main_window_new(GTK_APPLICATION(app));
142 }
Stepan Salenikovicha8df7ea2015-02-18 12:46:36 -0500143
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500144 gtk_window_present(GTK_WINDOW(priv->win));
145
146 return 0;
147}
148
149static void
Stepan Salenikovich69771842015-02-24 18:11:45 -0500150call_accept(G_GNUC_UNUSED GSimpleAction *action, G_GNUC_UNUSED GVariant *param, G_GNUC_UNUSED gpointer client)
151{
152 g_debug("call accpet action");
153
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500154 /* TODO: implement using UserActionModel once its fixed */
Stepan Salenikovich69771842015-02-24 18:11:45 -0500155
156 QModelIndex idx = CallModel::instance()->selectionModel()->currentIndex();
157 if (idx.isValid()) {
158 Call *call = CallModel::instance()->getCall(idx);
159 call->performAction(Call::Action::ACCEPT);
160 }
161}
162
163static void
164call_hangup(G_GNUC_UNUSED GSimpleAction *action, G_GNUC_UNUSED GVariant *param, G_GNUC_UNUSED gpointer client)
165{
166 g_debug("call hangup action");
167
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500168 /* TODO: implement using UserActionModel once its fixed */
Stepan Salenikovich69771842015-02-24 18:11:45 -0500169
170 QModelIndex idx = CallModel::instance()->selectionModel()->currentIndex();
171 if (idx.isValid()) {
172 Call *call = CallModel::instance()->getCall(idx);
173 call->performAction(Call::Action::REFUSE);
174 }
175}
176
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500177static void
178call_hold(GSimpleAction *action, GVariant *state, G_GNUC_UNUSED gpointer data)
179{
180 g_debug("call hold action");
181
182 /* TODO: implement using UserActionModel once its fixed */
183
184 /* get the requested state and apply it */
185 gboolean requested = g_variant_get_boolean(state);
186 g_simple_action_set_state(action, g_variant_new_boolean(requested));
187
188 QModelIndex idx = CallModel::instance()->selectionModel()->currentIndex();
189 if (idx.isValid()) {
190 Call *call = CallModel::instance()->getCall(idx);
191 call->performAction(Call::Action::HOLD);
192 }
193}
194
Stepan Salenikovich9ae37732015-03-13 12:22:25 -0400195/* starting glib 2.40 the action() parameter in the action entry (the second one)
196 * can be left NULL for stateful boolean actions and they will automatically be
197 * toggled; for older versions of glib we must explicitly set a handler to toggle them */
198#if GLIB_CHECK_VERSION(2,40,0)
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500199
Stepan Salenikovich69771842015-02-24 18:11:45 -0500200static const GActionEntry ring_actions[] =
201{
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500202 { "accept", call_accept, NULL, NULL, NULL, {0} },
203 { "hangup", call_hangup, NULL, NULL, NULL, {0} },
204 { "hold", NULL, NULL, "false", call_hold, {0} },
205 /* TODO implement the other actions */
206 // { "mute_audio", NULL, NULL, "false", NULL, {0} },
207 // { "mute_video", NULL, NULL, "false", NULL, {0} },
208 // { "transfer", NULL, NULL, "flase", NULL, {0} },
209 // { "record", NULL, NULL, "false", NULL, {0} }
Stepan Salenikovich69771842015-02-24 18:11:45 -0500210};
211
Stepan Salenikovich9ae37732015-03-13 12:22:25 -0400212#else
213
214/* adapted from glib 2.40, gsimpleaction.c */
215static void
216g_simple_action_change_state(GSimpleAction *simple, GVariant *value)
217{
218 GAction *action = G_ACTION(simple);
219
220 guint change_state_id = g_signal_lookup("change-state", G_OBJECT_TYPE(simple));
221
222 /* If the user connected a signal handler then they are responsible
223 * for handling state changes.
224 */
225 if (g_signal_has_handler_pending(action, change_state_id, 0, TRUE))
226 g_signal_emit(action, change_state_id, 0, value);
227
228 /* If not, then the default behaviour is to just set the state. */
229 else
230 g_simple_action_set_state(simple, value);
231}
232
233/* define activate handler for simple toggle actions for glib < 2.40
234 * adapted from glib 2.40, gsimpleaction.c */
235static void
236g_simple_action_toggle(GSimpleAction *action, GVariant *parameter, G_GNUC_UNUSED gpointer user_data)
237{
238 const GVariantType *parameter_type = g_action_get_parameter_type(G_ACTION(action));
239 g_return_if_fail(parameter_type == NULL ?
240 parameter == NULL :
241 (parameter != NULL &&
242 g_variant_is_of_type(parameter, parameter_type)));
243
244 if (parameter != NULL)
245 g_variant_ref_sink(parameter);
246
247 if (g_action_get_enabled(G_ACTION(action))) {
248 /* make sure it is a stateful action and toggle it */
249 GVariant *state = g_action_get_state(G_ACTION(action));
250 if (state) {
251 /* If we have no parameter and this is a boolean action, toggle. */
252 if (parameter == NULL && g_variant_is_of_type(state, G_VARIANT_TYPE_BOOLEAN)) {
253 gboolean was_enabled = g_variant_get_boolean(state);
254 g_simple_action_change_state(action, g_variant_new_boolean(!was_enabled));
255 }
256 /* else, if the parameter and state type are the same, do a change-state */
257 else if (g_variant_is_of_type (state, g_variant_get_type(parameter)))
258 g_simple_action_change_state(action, parameter);
259 }
260 g_variant_unref(state);
261 }
262
263 if (parameter != NULL)
264 g_variant_unref (parameter);
265}
266
267static const GActionEntry ring_actions[] =
268{
269 { "accept", call_accept, NULL, NULL, NULL, {0} },
270 { "hangup", call_hangup, NULL, NULL, NULL, {0} },
271 { "hold", g_simple_action_toggle, NULL, "false", call_hold, {0} },
272 /* TODO implement the other actions */
273 // { "mute_audio", NULL, NULL, "false", NULL, {0} },
274 // { "mute_video", NULL, NULL, "false", NULL, {0} },
275 // { "transfer", NULL, NULL, "flase", NULL, {0} },
276 // { "record", NULL, NULL, "false", NULL, {0} }
277};
278
279#endif
280
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500281/* TODO: uncomment when UserActionModel is fixed and used
282typedef union _int_ptr_t
283{
284 int value;
285 gpointer ptr;
286} int_ptr_t;
287
288static void
289activate_action(GSimpleAction *action, G_GNUC_UNUSED GVariant *parameter, gpointer user_data)
290{
291 g_debug("activating action: %s", g_action_get_name(G_ACTION(action)));
292
293 int_ptr_t key;
294 key.ptr = user_data;
295 UserActionModel::Action a = static_cast<UserActionModel::Action>(key.value);
296 UserActionModel* uam = CallModel::instance()->userActionModel();
297
298 uam << a;
299}
300*/
301
Stepan Salenikovich69771842015-02-24 18:11:45 -0500302static void
303ring_client_startup(GApplication *app)
304{
305 G_APPLICATION_CLASS(ring_client_parent_class)->startup(app);
306
307 RingClient *client = RING_CLIENT(app);
308
309 g_action_map_add_action_entries(
310 G_ACTION_MAP(app), ring_actions, G_N_ELEMENTS(ring_actions), client);
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500311
312 /* TODO: Bind actions to the useractionmodel once it is working */
313 // UserActionModel* uam = CallModel::instance()->userActionModel();
314 // QHash<int, GSimpleAction*> actionHash;
315 // actionHash[ (int)UserActionModel::Action::ACCEPT ] = G_SIMPLE_ACTION(g_action_map_lookup_action(G_ACTION_MAP(app), "accept"));
316 // actionHash[ (int)UserActionModel::Action::HOLD ] = G_SIMPLE_ACTION(g_action_map_lookup_action(G_ACTION_MAP(app), "hold"));
317 // // actionHash[ (int)UserActionModel::Action::MUTE_AUDIO ] = G_SIMPLE_ACTION(g_action_map_lookup_action(G_ACTION_MAP(app), "mute_audio"));
318 // // actionHash[ (int)UserActionModel::Action::SERVER_TRANSFER ] = G_SIMPLE_ACTION(g_action_map_lookup_action(G_ACTION_MAP(app), "transfer"));
319 // // actionHash[ (int)UserActionModel::Action::RECORD ] = G_SIMPLE_ACTION(g_action_map_lookup_action(G_ACTION_MAP(app), "record"));
320 // actionHash[ (int)UserActionModel::Action::HANGUP ] = G_SIMPLE_ACTION(g_action_map_lookup_action(G_ACTION_MAP(app), "hangup"));
321
322 // for (QHash<int,GSimpleAction*>::const_iterator i = actionHash.begin(); i != actionHash.end(); ++i) {
323 // GSimpleAction* sa = i.value();
324 // // UserActionModel::Action a = static_cast<UserActionModel::Action>(i.key());
325 // // connect(ea, &QAction::triggered, [uam,a](bool) {uam << a;});
326 // int_ptr_t user_data;
327 // user_data.value = i.key();
328 // g_signal_connect(G_OBJECT(sa), "activate", G_CALLBACK(activate_action), user_data.ptr);
329
330 // }
331
332 // QObject::connect(uam,&UserActionModel::dataChanged, [actionHash,uam](const QModelIndex& tl, const QModelIndex& br) {
333 // const int first(tl.row()),last(br.row());
334 // for(int i = first; i <= last;i++) {
335 // const QModelIndex& idx = uam->index(i,0);
336 // GSimpleAction* sa = actionHash[(int)qvariant_cast<UserActionModel::Action>(idx.data(UserActionModel::Role::ACTION))];
337 // if (sa) {
338 // // a->setText ( idx.data(Qt::DisplayRole).toString() );
339 // // a->setEnabled( idx.flags() & Qt::ItemIsEnabled );
340 // g_simple_action_set_enabled(sa, idx.flags() & Qt::ItemIsEnabled);
341 // // a->setChecked( idx.data(Qt::CheckStateRole) == Qt::Checked );
342 // /* check if statefull action */
343 // if (g_action_get_state_type(G_ACTION(sa)) != NULL)
344 // g_simple_action_set_state(sa, g_variant_new_boolean(idx.data(Qt::CheckStateRole) == Qt::Checked));
345 // // a->setAltIcon( qvariant_cast<QPixmap>(idx.data(Qt::DecorationRole)) );
346 // }
347 // }
348 // });
Stepan Salenikovich69771842015-02-24 18:11:45 -0500349}
350
351static void
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500352ring_client_shutdown(GApplication *app)
353{
354 RingClient *self = RING_CLIENT(app);
355 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(self);
356
357 /* free the QCoreApplication, which will destroy all libRingClient models
358 * and thus send the Unregister signal over dbus to dring */
359 delete priv->qtapp;
360
361 /* Chain up to the parent class */
362 G_APPLICATION_CLASS(ring_client_parent_class)->shutdown(app);
363}
364
365static void
366ring_client_init(RingClient *self)
367{
368 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(self);
369
370 /* init widget */
371 priv->win = NULL;
372 priv->qtapp = NULL;
373}
374
375static void
376ring_client_class_init(RingClientClass *klass)
377{
Stepan Salenikovich69771842015-02-24 18:11:45 -0500378 G_APPLICATION_CLASS(klass)->startup = ring_client_startup;
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500379 G_APPLICATION_CLASS(klass)->command_line = ring_client_command_line;
380 G_APPLICATION_CLASS(klass)->shutdown = ring_client_shutdown;
381}
382
383RingClient *
384ring_client_new()
385{
386 return (RingClient *)g_object_new(ring_client_get_type(),
Stepan Salenikovich434b88f2015-02-19 17:49:08 -0500387 "application-id", "cx.ring.RingGnome",
388 "flags", G_APPLICATION_HANDLES_COMMAND_LINE, NULL);
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500389}