blob: e782de07451b9790d41ff48f156370b98a34133f [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 Salenikovich69771842015-02-24 18:11:45 -050088static const GActionEntry ring_actions[] =
89{
Stepan Salenikovich347b73a2015-03-22 10:39:00 -040090 { "accept", NULL, NULL, NULL, NULL, {0} },
91 { "hangup", NULL, NULL, NULL, NULL, {0} },
92 { "hold", NULL, NULL, "false", NULL, {0} },
Stepan Salenikovichc64523b2015-02-27 16:31:00 -050093 /* TODO implement the other actions */
94 // { "mute_audio", NULL, NULL, "false", NULL, {0} },
95 // { "mute_video", NULL, NULL, "false", NULL, {0} },
96 // { "transfer", NULL, NULL, "flase", NULL, {0} },
97 // { "record", NULL, NULL, "false", NULL, {0} }
Stepan Salenikovich69771842015-02-24 18:11:45 -050098};
99
Stepan Salenikovich347b73a2015-03-22 10:39:00 -0400100/* 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 -0500101typedef union _int_ptr_t
102{
103 int value;
104 gpointer ptr;
105} int_ptr_t;
106
107static void
108activate_action(GSimpleAction *action, G_GNUC_UNUSED GVariant *parameter, gpointer user_data)
109{
110 g_debug("activating action: %s", g_action_get_name(G_ACTION(action)));
111
112 int_ptr_t key;
113 key.ptr = user_data;
114 UserActionModel::Action a = static_cast<UserActionModel::Action>(key.value);
115 UserActionModel* uam = CallModel::instance()->userActionModel();
116
117 uam << a;
118}
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500119
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400120static int
121ring_client_startup(GApplication *app, gint argc, gchar **argv)
Stepan Salenikovich69771842015-02-24 18:11:45 -0500122{
123 G_APPLICATION_CLASS(ring_client_parent_class)->startup(app);
124
125 RingClient *client = RING_CLIENT(app);
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400126 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
Stepan Salenikovich69771842015-02-24 18:11:45 -0500127
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400128 /* init clutter */
129 int clutter_error;
130 if ((clutter_error = gtk_clutter_init(&argc, &argv)) != CLUTTER_INIT_SUCCESS) {
131 g_critical("Could not init clutter : %d\n", clutter_error);
132 return 1;
133 }
134
135 /* init libRingClient and make sure its connected to the dbus */
136 try {
137 priv->qtapp = new QCoreApplication(argc, argv);
138 /* the call model will try to connect to dring via dbus */
139 CallModel::instance();
140 } catch (const char * msg) {
141 init_exception_dialog(msg);
142 g_critical("%s", msg);
143 return 1;
144 } catch(QString& msg) {
145 QByteArray ba = msg.toLocal8Bit();
146 const char *c_str = ba.data();
147 init_exception_dialog(c_str);
148 g_critical("%s", c_str);
149 return 1;
150 }
151
152 /* add backends */
153 CategorizedHistoryModel::instance()->addCollection<MinimalHistoryBackend>(LoadOptions::FORCE_ENABLED);
154
155 /* Override theme since we don't have appropriate icons for a dark them (yet) */
156 GtkSettings *gtk_settings = gtk_settings_get_default();
157 g_object_set(G_OBJECT(gtk_settings), "gtk-application-prefer-dark-theme",
158 FALSE, NULL);
159 /* enable button icons */
160 g_object_set(G_OBJECT(gtk_settings), "gtk-button-images",
161 TRUE, NULL);
162
163 /* add GActions */
Stepan Salenikovich69771842015-02-24 18:11:45 -0500164 g_action_map_add_action_entries(
165 G_ACTION_MAP(app), ring_actions, G_N_ELEMENTS(ring_actions), client);
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500166
Stepan Salenikovich347b73a2015-03-22 10:39:00 -0400167 /* Bind GActions to the UserActionModel */
168 UserActionModel* uam = CallModel::instance()->userActionModel();
169 QHash<int, GSimpleAction*> actionHash;
170 actionHash[ (int)UserActionModel::Action::ACCEPT ] = G_SIMPLE_ACTION(g_action_map_lookup_action(G_ACTION_MAP(app), "accept"));
171 actionHash[ (int)UserActionModel::Action::HOLD ] = G_SIMPLE_ACTION(g_action_map_lookup_action(G_ACTION_MAP(app), "hold"));
172 /* TODO: add commented actions when ready */
173 // actionHash[ (int)UserActionModel::Action::MUTE_AUDIO ] = G_SIMPLE_ACTION(g_action_map_lookup_action(G_ACTION_MAP(app), "mute_audio"));
174 // actionHash[ (int)UserActionModel::Action::SERVER_TRANSFER ] = G_SIMPLE_ACTION(g_action_map_lookup_action(G_ACTION_MAP(app), "transfer"));
175 // actionHash[ (int)UserActionModel::Action::RECORD ] = G_SIMPLE_ACTION(g_action_map_lookup_action(G_ACTION_MAP(app), "record"));
176 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 -0500177
Stepan Salenikovich347b73a2015-03-22 10:39:00 -0400178 for (QHash<int,GSimpleAction*>::const_iterator i = actionHash.begin(); i != actionHash.end(); ++i) {
179 GSimpleAction* sa = i.value();
180 int_ptr_t user_data;
181 user_data.value = i.key();
182 g_signal_connect(G_OBJECT(sa), "activate", G_CALLBACK(activate_action), user_data.ptr);
183 }
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500184
Stepan Salenikovich347b73a2015-03-22 10:39:00 -0400185 /* change the state of the GActions based on the UserActionModel */
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400186 priv->uam_updated = QObject::connect(uam,&UserActionModel::dataChanged, [actionHash,uam](const QModelIndex& tl, const QModelIndex& br) {
Stepan Salenikovich347b73a2015-03-22 10:39:00 -0400187 const int first(tl.row()),last(br.row());
188 for(int i = first; i <= last;i++) {
189 const QModelIndex& idx = uam->index(i,0);
190 GSimpleAction* sa = actionHash[(int)qvariant_cast<UserActionModel::Action>(idx.data(UserActionModel::Role::ACTION))];
191 if (sa) {
192 /* enable/disable GAction based on UserActionModel */
193 g_simple_action_set_enabled(sa, idx.flags() & Qt::ItemIsEnabled);
194 /* set the state of the action if its stateful */
195 if (g_action_get_state_type(G_ACTION(sa)) != NULL)
196 g_simple_action_set_state(sa, g_variant_new_boolean(idx.data(Qt::CheckStateRole) == Qt::Checked));
197 }
198 }
199 });
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400200
201 return 0;
202}
203
204static int
205ring_client_command_line(GApplication *app, GApplicationCommandLine *cmdline)
206{
207 RingClient *client = RING_CLIENT(app);
208 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
209
210 gint argc;
211 gchar **argv = g_application_command_line_get_arguments(cmdline, &argc);
212 GOptionContext *context = ring_client_options_get_context();
213 GError *error = NULL;
214 if (g_option_context_parse(context, &argc, &argv, &error) == FALSE) {
215 g_print(_("%s\nRun '%s --help' to see a full list of available command line options.\n"),
216 error->message, argv[0]);
217 g_error_free(error);
218 g_option_context_free(context);
219 return 1;
220 }
221 g_option_context_free(context);
222
223 /* init libs and create main window only once */
224 if (priv->win == NULL) {
225 if (ring_client_startup(app, argc, argv) != 0)
226 return 1;
227 priv->win = ring_main_window_new(GTK_APPLICATION(app));
228 }
229
230 gtk_window_present(GTK_WINDOW(priv->win));
231
232 return 0;
Stepan Salenikovich69771842015-02-24 18:11:45 -0500233}
234
235static void
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500236ring_client_shutdown(GApplication *app)
237{
238 RingClient *self = RING_CLIENT(app);
239 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(self);
240
Stepan Salenikovich068fb692015-03-23 14:58:32 -0400241 QObject::disconnect(priv->uam_updated);
242
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500243 /* free the QCoreApplication, which will destroy all libRingClient models
244 * and thus send the Unregister signal over dbus to dring */
245 delete priv->qtapp;
246
247 /* Chain up to the parent class */
248 G_APPLICATION_CLASS(ring_client_parent_class)->shutdown(app);
249}
250
251static void
252ring_client_init(RingClient *self)
253{
254 RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(self);
255
256 /* init widget */
257 priv->win = NULL;
258 priv->qtapp = NULL;
259}
260
261static void
262ring_client_class_init(RingClientClass *klass)
263{
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500264 G_APPLICATION_CLASS(klass)->command_line = ring_client_command_line;
265 G_APPLICATION_CLASS(klass)->shutdown = ring_client_shutdown;
266}
267
268RingClient *
269ring_client_new()
270{
271 return (RingClient *)g_object_new(ring_client_get_type(),
Stepan Salenikovich434b88f2015-02-19 17:49:08 -0500272 "application-id", "cx.ring.RingGnome",
273 "flags", G_APPLICATION_HANDLES_COMMAND_LINE, NULL);
Stepan Salenikovichd81ef292015-02-17 18:47:37 -0500274}