blob: bbe75fa655415a1c76dedb4f73199110a17d2d15 [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 Salenikovichd81ef292015-02-17 18:47:37 -050041
42#include "ring_client_options.h"
Stepan Salenikovich434b88f2015-02-19 17:49:08 -050043#include "ringmainwindow.h"
44
45struct _RingClientClass
46{
47 GtkApplicationClass parent_class;
48};
49
50struct _RingClient
51{
52 GtkApplication parent;
53};
54
55typedef struct _RingClientPrivate RingClientPrivate;
Stepan Salenikovichd81ef292015-02-17 18:47:37 -050056
57struct _RingClientPrivate {
58 /* main window */
59 GtkWidget *win;
60 /* for libRingclient */
61 QCoreApplication *qtapp;
62};
63
Stepan Salenikovich434b88f2015-02-19 17:49:08 -050064G_DEFINE_TYPE_WITH_PRIVATE(RingClient, ring_client, GTK_TYPE_APPLICATION);
Stepan Salenikovichd81ef292015-02-17 18:47:37 -050065
66#define RING_CLIENT_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), RING_CLIENT_TYPE, RingClientPrivate))
67
68static void
69init_exception_dialog(const char* msg)
70{
71 g_warning("%s", msg);
72 GtkWidget *dialog = gtk_message_dialog_new(NULL,
73 (GtkDialogFlags)(GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT),
74 GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
75 _("Unable to initialize.\nMake sure the Ring daemon (dring) is running.\nError: %s"),
76 msg);
77
78 gtk_window_set_title(GTK_WINDOW(dialog), _("Ring Error"));
79 gtk_dialog_run(GTK_DIALOG(dialog));
80 gtk_widget_destroy(dialog);
81}
82
83static int
84ring_client_command_line(GApplication *app, GApplicationCommandLine *cmdline)
85{
86 RingClient *client = RING_CLIENT(app);
87 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
88
89 gint argc;
90 gchar **argv = g_application_command_line_get_arguments(cmdline, &argc);
91 GOptionContext *context = ring_client_options_get_context();
92 GError *error = NULL;
93 if (g_option_context_parse(context, &argc, &argv, &error) == FALSE) {
94 g_print(_("%s\nRun '%s --help' to see a full list of available command line options.\n"),
95 error->message, argv[0]);
96 g_error_free(error);
97 g_option_context_free(context);
98 return 1;
99 }
100 g_option_context_free(context);
101
102 /* Override theme since we don't have appropriate icons for a dark them (yet) */
103 GtkSettings *gtk_settings = gtk_settings_get_default();
104 g_object_set(G_OBJECT(gtk_settings), "gtk-application-prefer-dark-theme",
105 FALSE, NULL);
Stepan Salenikovich434b88f2015-02-19 17:49:08 -0500106 /* enable button icons */
107 g_object_set(G_OBJECT(gtk_settings), "gtk-button-images",
108 TRUE, NULL);
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500109
110 /* init libRingClient and make sure its connected to the dbus */
111 try {
112 /* TODO: do we care about passing the cmd line arguments here? */
113 priv->qtapp = new QCoreApplication(argc, argv);
114 /* the call model will try to connect to dring via dbus */
115 CallModel::instance();
116 } catch (const char * msg) {
117 init_exception_dialog(msg);
118 return 1;
119 } catch(QString& msg) {
120 QByteArray ba = msg.toLocal8Bit();
121 const char *c_str = ba.data();
122 init_exception_dialog(c_str);
123 return 1;
124 }
125
126 /* create an empty window */
Stepan Salenikovich434b88f2015-02-19 17:49:08 -0500127 if (priv->win == NULL) {
128 priv->win = ring_main_window_new(GTK_APPLICATION(app));
129 }
Stepan Salenikovicha8df7ea2015-02-18 12:46:36 -0500130
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500131 gtk_window_present(GTK_WINDOW(priv->win));
132
133 return 0;
134}
135
136static void
Stepan Salenikovich69771842015-02-24 18:11:45 -0500137call_accept(G_GNUC_UNUSED GSimpleAction *action, G_GNUC_UNUSED GVariant *param, G_GNUC_UNUSED gpointer client)
138{
139 g_debug("call accpet action");
140
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500141 /* TODO: implement using UserActionModel once its fixed */
Stepan Salenikovich69771842015-02-24 18:11:45 -0500142
143 QModelIndex idx = CallModel::instance()->selectionModel()->currentIndex();
144 if (idx.isValid()) {
145 Call *call = CallModel::instance()->getCall(idx);
146 call->performAction(Call::Action::ACCEPT);
147 }
148}
149
150static void
151call_hangup(G_GNUC_UNUSED GSimpleAction *action, G_GNUC_UNUSED GVariant *param, G_GNUC_UNUSED gpointer client)
152{
153 g_debug("call hangup action");
154
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500155 /* TODO: implement using UserActionModel once its fixed */
Stepan Salenikovich69771842015-02-24 18:11:45 -0500156
157 QModelIndex idx = CallModel::instance()->selectionModel()->currentIndex();
158 if (idx.isValid()) {
159 Call *call = CallModel::instance()->getCall(idx);
160 call->performAction(Call::Action::REFUSE);
161 }
162}
163
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500164static void
165call_hold(GSimpleAction *action, GVariant *state, G_GNUC_UNUSED gpointer data)
166{
167 g_debug("call hold action");
168
169 /* TODO: implement using UserActionModel once its fixed */
170
171 /* get the requested state and apply it */
172 gboolean requested = g_variant_get_boolean(state);
173 g_simple_action_set_state(action, g_variant_new_boolean(requested));
174
175 QModelIndex idx = CallModel::instance()->selectionModel()->currentIndex();
176 if (idx.isValid()) {
177 Call *call = CallModel::instance()->getCall(idx);
178 call->performAction(Call::Action::HOLD);
179 }
180}
181
182
Stepan Salenikovich69771842015-02-24 18:11:45 -0500183static const GActionEntry ring_actions[] =
184{
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500185 { "accept", call_accept, NULL, NULL, NULL, {0} },
186 { "hangup", call_hangup, NULL, NULL, NULL, {0} },
187 { "hold", NULL, NULL, "false", call_hold, {0} },
188 /* TODO implement the other actions */
189 // { "mute_audio", NULL, NULL, "false", NULL, {0} },
190 // { "mute_video", NULL, NULL, "false", NULL, {0} },
191 // { "transfer", NULL, NULL, "flase", NULL, {0} },
192 // { "record", NULL, NULL, "false", NULL, {0} }
Stepan Salenikovich69771842015-02-24 18:11:45 -0500193};
194
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500195/* TODO: uncomment when UserActionModel is fixed and used
196typedef union _int_ptr_t
197{
198 int value;
199 gpointer ptr;
200} int_ptr_t;
201
202static void
203activate_action(GSimpleAction *action, G_GNUC_UNUSED GVariant *parameter, gpointer user_data)
204{
205 g_debug("activating action: %s", g_action_get_name(G_ACTION(action)));
206
207 int_ptr_t key;
208 key.ptr = user_data;
209 UserActionModel::Action a = static_cast<UserActionModel::Action>(key.value);
210 UserActionModel* uam = CallModel::instance()->userActionModel();
211
212 uam << a;
213}
214*/
215
Stepan Salenikovich69771842015-02-24 18:11:45 -0500216static void
217ring_client_startup(GApplication *app)
218{
219 G_APPLICATION_CLASS(ring_client_parent_class)->startup(app);
220
221 RingClient *client = RING_CLIENT(app);
222
223 g_action_map_add_action_entries(
224 G_ACTION_MAP(app), ring_actions, G_N_ELEMENTS(ring_actions), client);
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500225
226 /* TODO: Bind actions to the useractionmodel once it is working */
227 // UserActionModel* uam = CallModel::instance()->userActionModel();
228 // QHash<int, GSimpleAction*> actionHash;
229 // actionHash[ (int)UserActionModel::Action::ACCEPT ] = G_SIMPLE_ACTION(g_action_map_lookup_action(G_ACTION_MAP(app), "accept"));
230 // actionHash[ (int)UserActionModel::Action::HOLD ] = G_SIMPLE_ACTION(g_action_map_lookup_action(G_ACTION_MAP(app), "hold"));
231 // // actionHash[ (int)UserActionModel::Action::MUTE_AUDIO ] = G_SIMPLE_ACTION(g_action_map_lookup_action(G_ACTION_MAP(app), "mute_audio"));
232 // // actionHash[ (int)UserActionModel::Action::SERVER_TRANSFER ] = G_SIMPLE_ACTION(g_action_map_lookup_action(G_ACTION_MAP(app), "transfer"));
233 // // actionHash[ (int)UserActionModel::Action::RECORD ] = G_SIMPLE_ACTION(g_action_map_lookup_action(G_ACTION_MAP(app), "record"));
234 // actionHash[ (int)UserActionModel::Action::HANGUP ] = G_SIMPLE_ACTION(g_action_map_lookup_action(G_ACTION_MAP(app), "hangup"));
235
236 // for (QHash<int,GSimpleAction*>::const_iterator i = actionHash.begin(); i != actionHash.end(); ++i) {
237 // GSimpleAction* sa = i.value();
238 // // UserActionModel::Action a = static_cast<UserActionModel::Action>(i.key());
239 // // connect(ea, &QAction::triggered, [uam,a](bool) {uam << a;});
240 // int_ptr_t user_data;
241 // user_data.value = i.key();
242 // g_signal_connect(G_OBJECT(sa), "activate", G_CALLBACK(activate_action), user_data.ptr);
243
244 // }
245
246 // QObject::connect(uam,&UserActionModel::dataChanged, [actionHash,uam](const QModelIndex& tl, const QModelIndex& br) {
247 // const int first(tl.row()),last(br.row());
248 // for(int i = first; i <= last;i++) {
249 // const QModelIndex& idx = uam->index(i,0);
250 // GSimpleAction* sa = actionHash[(int)qvariant_cast<UserActionModel::Action>(idx.data(UserActionModel::Role::ACTION))];
251 // if (sa) {
252 // // a->setText ( idx.data(Qt::DisplayRole).toString() );
253 // // a->setEnabled( idx.flags() & Qt::ItemIsEnabled );
254 // g_simple_action_set_enabled(sa, idx.flags() & Qt::ItemIsEnabled);
255 // // a->setChecked( idx.data(Qt::CheckStateRole) == Qt::Checked );
256 // /* check if statefull action */
257 // if (g_action_get_state_type(G_ACTION(sa)) != NULL)
258 // g_simple_action_set_state(sa, g_variant_new_boolean(idx.data(Qt::CheckStateRole) == Qt::Checked));
259 // // a->setAltIcon( qvariant_cast<QPixmap>(idx.data(Qt::DecorationRole)) );
260 // }
261 // }
262 // });
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
271 /* free the QCoreApplication, which will destroy all libRingClient models
272 * and thus send the Unregister signal over dbus to dring */
273 delete priv->qtapp;
274
275 /* Chain up to the parent class */
276 G_APPLICATION_CLASS(ring_client_parent_class)->shutdown(app);
277}
278
279static void
280ring_client_init(RingClient *self)
281{
282 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(self);
283
284 /* init widget */
285 priv->win = NULL;
286 priv->qtapp = NULL;
287}
288
289static void
290ring_client_class_init(RingClientClass *klass)
291{
Stepan Salenikovich69771842015-02-24 18:11:45 -0500292 G_APPLICATION_CLASS(klass)->startup = ring_client_startup;
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500293 G_APPLICATION_CLASS(klass)->command_line = ring_client_command_line;
294 G_APPLICATION_CLASS(klass)->shutdown = ring_client_shutdown;
295}
296
297RingClient *
298ring_client_new()
299{
300 return (RingClient *)g_object_new(ring_client_get_type(),
Stepan Salenikovich434b88f2015-02-19 17:49:08 -0500301 "application-id", "cx.ring.RingGnome",
302 "flags", G_APPLICATION_HANDLES_COMMAND_LINE, NULL);
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500303}