blob: ca99755066ef99af23d4d0a3fc7286400689453c [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;
Stepan Salenikovich068fb692015-03-23 14:58:32 -040065 /* UAM */
66 QMetaObject::Connection uam_updated;
Stepan Salenikovichd81ef292015-02-17 18:47:37 -050067};
68
Stepan Salenikovich434b88f2015-02-19 17:49:08 -050069G_DEFINE_TYPE_WITH_PRIVATE(RingClient, ring_client, GTK_TYPE_APPLICATION);
Stepan Salenikovichd81ef292015-02-17 18:47:37 -050070
71#define RING_CLIENT_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), RING_CLIENT_TYPE, RingClientPrivate))
72
73static void
74init_exception_dialog(const char* msg)
75{
76 g_warning("%s", msg);
77 GtkWidget *dialog = gtk_message_dialog_new(NULL,
78 (GtkDialogFlags)(GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT),
79 GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
80 _("Unable to initialize.\nMake sure the Ring daemon (dring) is running.\nError: %s"),
81 msg);
82
83 gtk_window_set_title(GTK_WINDOW(dialog), _("Ring Error"));
84 gtk_dialog_run(GTK_DIALOG(dialog));
85 gtk_widget_destroy(dialog);
86}
87
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -040088static void
89ring_accelerators(RingClient *client)
90{
91#if GTK_CHECK_VERSION(3,10,0)
92 const gchar *quit_accels[2] = { "<Ctrl>Q", NULL };
93 gtk_application_set_accels_for_action(GTK_APPLICATION(client), "app.quit", quit_accels);
94#else
95 gtk_application_add_accelerator(GTK_APPLICATION(client), "<Control>Q", "win.quit", NULL);
96#endif
97}
98
99static void
100action_quit(G_GNUC_UNUSED GSimpleAction *simple,
101 G_GNUC_UNUSED GVariant *parameter,
102 gpointer user_data)
103{
104 g_return_if_fail(G_IS_APPLICATION(user_data));
105
106#if GLIB_CHECK_VERSION(2,32,0)
107 g_application_quit(G_APPLICATION(user_data));
108#else
109 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(user_data);
110 gtk_widget_destroy(priv->win);
111#endif
112}
113
Stepan Salenikovich69771842015-02-24 18:11:45 -0500114static const GActionEntry ring_actions[] =
115{
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400116 { "accept", NULL, NULL, NULL, NULL, {0} },
117 { "hangup", NULL, NULL, NULL, NULL, {0} },
118 { "hold", NULL, NULL, "false", NULL, {0} },
119 { "quit", action_quit, NULL, NULL, NULL, {0} }
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500120 /* TODO implement the other actions */
121 // { "mute_audio", NULL, NULL, "false", NULL, {0} },
122 // { "mute_video", NULL, NULL, "false", NULL, {0} },
123 // { "transfer", NULL, NULL, "flase", NULL, {0} },
124 // { "record", NULL, NULL, "false", NULL, {0} }
Stepan Salenikovich69771842015-02-24 18:11:45 -0500125};
126
Stepan Salenikovich347b73a2015-03-22 10:39:00 -0400127/* 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 -0500128typedef union _int_ptr_t
129{
130 int value;
131 gpointer ptr;
132} int_ptr_t;
133
134static void
135activate_action(GSimpleAction *action, G_GNUC_UNUSED GVariant *parameter, gpointer user_data)
136{
137 g_debug("activating action: %s", g_action_get_name(G_ACTION(action)));
138
139 int_ptr_t key;
140 key.ptr = user_data;
141 UserActionModel::Action a = static_cast<UserActionModel::Action>(key.value);
142 UserActionModel* uam = CallModel::instance()->userActionModel();
143
144 uam << a;
145}
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500146
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400147static int
148ring_client_startup(GApplication *app, gint argc, gchar **argv)
Stepan Salenikovich69771842015-02-24 18:11:45 -0500149{
150 G_APPLICATION_CLASS(ring_client_parent_class)->startup(app);
151
152 RingClient *client = RING_CLIENT(app);
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400153 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
Stepan Salenikovich69771842015-02-24 18:11:45 -0500154
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400155 /* init clutter */
156 int clutter_error;
157 if ((clutter_error = gtk_clutter_init(&argc, &argv)) != CLUTTER_INIT_SUCCESS) {
158 g_critical("Could not init clutter : %d\n", clutter_error);
159 return 1;
160 }
161
162 /* init libRingClient and make sure its connected to the dbus */
163 try {
164 priv->qtapp = new QCoreApplication(argc, argv);
165 /* the call model will try to connect to dring via dbus */
166 CallModel::instance();
167 } catch (const char * msg) {
168 init_exception_dialog(msg);
169 g_critical("%s", msg);
170 return 1;
171 } catch(QString& msg) {
172 QByteArray ba = msg.toLocal8Bit();
173 const char *c_str = ba.data();
174 init_exception_dialog(c_str);
175 g_critical("%s", c_str);
176 return 1;
177 }
178
179 /* add backends */
180 CategorizedHistoryModel::instance()->addCollection<MinimalHistoryBackend>(LoadOptions::FORCE_ENABLED);
181
182 /* Override theme since we don't have appropriate icons for a dark them (yet) */
183 GtkSettings *gtk_settings = gtk_settings_get_default();
184 g_object_set(G_OBJECT(gtk_settings), "gtk-application-prefer-dark-theme",
185 FALSE, NULL);
186 /* enable button icons */
187 g_object_set(G_OBJECT(gtk_settings), "gtk-button-images",
188 TRUE, NULL);
189
190 /* add GActions */
Stepan Salenikovich69771842015-02-24 18:11:45 -0500191 g_action_map_add_action_entries(
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400192 G_ACTION_MAP(app), ring_actions, G_N_ELEMENTS(ring_actions), app);
193
194 /* add accelerators */
195 ring_accelerators(RING_CLIENT(app));
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500196
Stepan Salenikovich347b73a2015-03-22 10:39:00 -0400197 /* Bind GActions to the UserActionModel */
198 UserActionModel* uam = CallModel::instance()->userActionModel();
199 QHash<int, GSimpleAction*> actionHash;
200 actionHash[ (int)UserActionModel::Action::ACCEPT ] = G_SIMPLE_ACTION(g_action_map_lookup_action(G_ACTION_MAP(app), "accept"));
201 actionHash[ (int)UserActionModel::Action::HOLD ] = G_SIMPLE_ACTION(g_action_map_lookup_action(G_ACTION_MAP(app), "hold"));
202 /* TODO: add commented actions when ready */
203 // actionHash[ (int)UserActionModel::Action::MUTE_AUDIO ] = G_SIMPLE_ACTION(g_action_map_lookup_action(G_ACTION_MAP(app), "mute_audio"));
204 // actionHash[ (int)UserActionModel::Action::SERVER_TRANSFER ] = G_SIMPLE_ACTION(g_action_map_lookup_action(G_ACTION_MAP(app), "transfer"));
205 // actionHash[ (int)UserActionModel::Action::RECORD ] = G_SIMPLE_ACTION(g_action_map_lookup_action(G_ACTION_MAP(app), "record"));
206 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 -0500207
Stepan Salenikovich347b73a2015-03-22 10:39:00 -0400208 for (QHash<int,GSimpleAction*>::const_iterator i = actionHash.begin(); i != actionHash.end(); ++i) {
209 GSimpleAction* sa = i.value();
210 int_ptr_t user_data;
211 user_data.value = i.key();
212 g_signal_connect(G_OBJECT(sa), "activate", G_CALLBACK(activate_action), user_data.ptr);
213 }
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500214
Stepan Salenikovich347b73a2015-03-22 10:39:00 -0400215 /* change the state of the GActions based on the UserActionModel */
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400216 priv->uam_updated = QObject::connect(uam,&UserActionModel::dataChanged, [actionHash,uam](const QModelIndex& tl, const QModelIndex& br) {
Stepan Salenikovich347b73a2015-03-22 10:39:00 -0400217 const int first(tl.row()),last(br.row());
218 for(int i = first; i <= last;i++) {
219 const QModelIndex& idx = uam->index(i,0);
220 GSimpleAction* sa = actionHash[(int)qvariant_cast<UserActionModel::Action>(idx.data(UserActionModel::Role::ACTION))];
221 if (sa) {
222 /* enable/disable GAction based on UserActionModel */
223 g_simple_action_set_enabled(sa, idx.flags() & Qt::ItemIsEnabled);
224 /* set the state of the action if its stateful */
225 if (g_action_get_state_type(G_ACTION(sa)) != NULL)
226 g_simple_action_set_state(sa, g_variant_new_boolean(idx.data(Qt::CheckStateRole) == Qt::Checked));
227 }
228 }
229 });
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400230
231 return 0;
232}
233
234static int
235ring_client_command_line(GApplication *app, GApplicationCommandLine *cmdline)
236{
237 RingClient *client = RING_CLIENT(app);
238 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
239
240 gint argc;
241 gchar **argv = g_application_command_line_get_arguments(cmdline, &argc);
242 GOptionContext *context = ring_client_options_get_context();
243 GError *error = NULL;
244 if (g_option_context_parse(context, &argc, &argv, &error) == FALSE) {
245 g_print(_("%s\nRun '%s --help' to see a full list of available command line options.\n"),
246 error->message, argv[0]);
247 g_error_free(error);
248 g_option_context_free(context);
249 return 1;
250 }
251 g_option_context_free(context);
252
253 /* init libs and create main window only once */
254 if (priv->win == NULL) {
255 if (ring_client_startup(app, argc, argv) != 0)
256 return 1;
257 priv->win = ring_main_window_new(GTK_APPLICATION(app));
258 }
259
260 gtk_window_present(GTK_WINDOW(priv->win));
261
262 return 0;
Stepan Salenikovich69771842015-02-24 18:11:45 -0500263}
264
265static void
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500266ring_client_shutdown(GApplication *app)
267{
268 RingClient *self = RING_CLIENT(app);
269 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(self);
270
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400271 g_debug("quitting");
272
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400273 QObject::disconnect(priv->uam_updated);
274
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500275 /* free the QCoreApplication, which will destroy all libRingClient models
276 * and thus send the Unregister signal over dbus to dring */
277 delete priv->qtapp;
278
279 /* Chain up to the parent class */
280 G_APPLICATION_CLASS(ring_client_parent_class)->shutdown(app);
281}
282
283static void
284ring_client_init(RingClient *self)
285{
286 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(self);
287
288 /* init widget */
289 priv->win = NULL;
290 priv->qtapp = NULL;
291}
292
293static void
294ring_client_class_init(RingClientClass *klass)
295{
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500296 G_APPLICATION_CLASS(klass)->command_line = ring_client_command_line;
297 G_APPLICATION_CLASS(klass)->shutdown = ring_client_shutdown;
298}
299
300RingClient *
301ring_client_new()
302{
303 return (RingClient *)g_object_new(ring_client_get_type(),
Stepan Salenikovich434b88f2015-02-19 17:49:08 -0500304 "application-id", "cx.ring.RingGnome",
305 "flags", G_APPLICATION_HANDLES_COMMAND_LINE, NULL);
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500306}