blob: e3c9f3b1c728867c657841d76d17b001e4aad326 [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 Salenikovich15142182015-03-11 17:15:26 -040042#include <historymodel.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 */
129 HistoryModel::instance()->addCollection<MinimalHistoryBackend>(LoadOptions::FORCE_ENABLED);
130
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
195
Stepan Salenikovich69771842015-02-24 18:11:45 -0500196static const GActionEntry ring_actions[] =
197{
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500198 { "accept", call_accept, NULL, NULL, NULL, {0} },
199 { "hangup", call_hangup, NULL, NULL, NULL, {0} },
200 { "hold", NULL, NULL, "false", call_hold, {0} },
201 /* TODO implement the other actions */
202 // { "mute_audio", NULL, NULL, "false", NULL, {0} },
203 // { "mute_video", NULL, NULL, "false", NULL, {0} },
204 // { "transfer", NULL, NULL, "flase", NULL, {0} },
205 // { "record", NULL, NULL, "false", NULL, {0} }
Stepan Salenikovich69771842015-02-24 18:11:45 -0500206};
207
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500208/* TODO: uncomment when UserActionModel is fixed and used
209typedef union _int_ptr_t
210{
211 int value;
212 gpointer ptr;
213} int_ptr_t;
214
215static void
216activate_action(GSimpleAction *action, G_GNUC_UNUSED GVariant *parameter, gpointer user_data)
217{
218 g_debug("activating action: %s", g_action_get_name(G_ACTION(action)));
219
220 int_ptr_t key;
221 key.ptr = user_data;
222 UserActionModel::Action a = static_cast<UserActionModel::Action>(key.value);
223 UserActionModel* uam = CallModel::instance()->userActionModel();
224
225 uam << a;
226}
227*/
228
Stepan Salenikovich69771842015-02-24 18:11:45 -0500229static void
230ring_client_startup(GApplication *app)
231{
232 G_APPLICATION_CLASS(ring_client_parent_class)->startup(app);
233
234 RingClient *client = RING_CLIENT(app);
235
236 g_action_map_add_action_entries(
237 G_ACTION_MAP(app), ring_actions, G_N_ELEMENTS(ring_actions), client);
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500238
239 /* TODO: Bind actions to the useractionmodel once it is working */
240 // UserActionModel* uam = CallModel::instance()->userActionModel();
241 // QHash<int, GSimpleAction*> actionHash;
242 // actionHash[ (int)UserActionModel::Action::ACCEPT ] = G_SIMPLE_ACTION(g_action_map_lookup_action(G_ACTION_MAP(app), "accept"));
243 // actionHash[ (int)UserActionModel::Action::HOLD ] = G_SIMPLE_ACTION(g_action_map_lookup_action(G_ACTION_MAP(app), "hold"));
244 // // actionHash[ (int)UserActionModel::Action::MUTE_AUDIO ] = G_SIMPLE_ACTION(g_action_map_lookup_action(G_ACTION_MAP(app), "mute_audio"));
245 // // actionHash[ (int)UserActionModel::Action::SERVER_TRANSFER ] = G_SIMPLE_ACTION(g_action_map_lookup_action(G_ACTION_MAP(app), "transfer"));
246 // // actionHash[ (int)UserActionModel::Action::RECORD ] = G_SIMPLE_ACTION(g_action_map_lookup_action(G_ACTION_MAP(app), "record"));
247 // actionHash[ (int)UserActionModel::Action::HANGUP ] = G_SIMPLE_ACTION(g_action_map_lookup_action(G_ACTION_MAP(app), "hangup"));
248
249 // for (QHash<int,GSimpleAction*>::const_iterator i = actionHash.begin(); i != actionHash.end(); ++i) {
250 // GSimpleAction* sa = i.value();
251 // // UserActionModel::Action a = static_cast<UserActionModel::Action>(i.key());
252 // // connect(ea, &QAction::triggered, [uam,a](bool) {uam << a;});
253 // int_ptr_t user_data;
254 // user_data.value = i.key();
255 // g_signal_connect(G_OBJECT(sa), "activate", G_CALLBACK(activate_action), user_data.ptr);
256
257 // }
258
259 // QObject::connect(uam,&UserActionModel::dataChanged, [actionHash,uam](const QModelIndex& tl, const QModelIndex& br) {
260 // const int first(tl.row()),last(br.row());
261 // for(int i = first; i <= last;i++) {
262 // const QModelIndex& idx = uam->index(i,0);
263 // GSimpleAction* sa = actionHash[(int)qvariant_cast<UserActionModel::Action>(idx.data(UserActionModel::Role::ACTION))];
264 // if (sa) {
265 // // a->setText ( idx.data(Qt::DisplayRole).toString() );
266 // // a->setEnabled( idx.flags() & Qt::ItemIsEnabled );
267 // g_simple_action_set_enabled(sa, idx.flags() & Qt::ItemIsEnabled);
268 // // a->setChecked( idx.data(Qt::CheckStateRole) == Qt::Checked );
269 // /* check if statefull action */
270 // if (g_action_get_state_type(G_ACTION(sa)) != NULL)
271 // g_simple_action_set_state(sa, g_variant_new_boolean(idx.data(Qt::CheckStateRole) == Qt::Checked));
272 // // a->setAltIcon( qvariant_cast<QPixmap>(idx.data(Qt::DecorationRole)) );
273 // }
274 // }
275 // });
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
284 /* free the QCoreApplication, which will destroy all libRingClient models
285 * and thus send the Unregister signal over dbus to dring */
286 delete priv->qtapp;
287
288 /* Chain up to the parent class */
289 G_APPLICATION_CLASS(ring_client_parent_class)->shutdown(app);
290}
291
292static void
293ring_client_init(RingClient *self)
294{
295 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(self);
296
297 /* init widget */
298 priv->win = NULL;
299 priv->qtapp = NULL;
300}
301
302static void
303ring_client_class_init(RingClientClass *klass)
304{
Stepan Salenikovich69771842015-02-24 18:11:45 -0500305 G_APPLICATION_CLASS(klass)->startup = ring_client_startup;
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500306 G_APPLICATION_CLASS(klass)->command_line = ring_client_command_line;
307 G_APPLICATION_CLASS(klass)->shutdown = ring_client_shutdown;
308}
309
310RingClient *
311ring_client_new()
312{
313 return (RingClient *)g_object_new(ring_client_get_type(),
Stepan Salenikovich434b88f2015-02-19 17:49:08 -0500314 "application-id", "cx.ring.RingGnome",
315 "flags", G_APPLICATION_HANDLES_COMMAND_LINE, NULL);
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500316}