Stepan Salenikovich | d81ef29 | 2015-02-17 18:47:37 -0500 | [diff] [blame] | 1 | /* |
| 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 Salenikovich | d2dbcee | 2015-02-27 16:52:28 -0500 | [diff] [blame] | 35 | #include <QtCore/QCoreApplication> |
| 36 | #include <QtCore/QString> |
| 37 | #include <QtCore/QByteArray> |
Stepan Salenikovich | d81ef29 | 2015-02-17 18:47:37 -0500 | [diff] [blame] | 38 | #include <callmodel.h> |
Stepan Salenikovich | d2dbcee | 2015-02-27 16:52:28 -0500 | [diff] [blame] | 39 | #include <QtCore/QItemSelectionModel> |
Stepan Salenikovich | c64523b | 2015-02-27 16:31:00 -0500 | [diff] [blame] | 40 | #include <useractionmodel.h> |
Stepan Salenikovich | 36c025c | 2015-03-03 19:06:44 -0500 | [diff] [blame] | 41 | #include <clutter-gtk/clutter-gtk.h> |
Stepan Salenikovich | dd84cf9 | 2015-03-19 21:38:19 -0400 | [diff] [blame] | 42 | #include <categorizedhistorymodel.h> |
Stepan Salenikovich | d81ef29 | 2015-02-17 18:47:37 -0500 | [diff] [blame] | 43 | |
| 44 | #include "ring_client_options.h" |
Stepan Salenikovich | 434b88f | 2015-02-19 17:49:08 -0500 | [diff] [blame] | 45 | #include "ringmainwindow.h" |
Stepan Salenikovich | 1514218 | 2015-03-11 17:15:26 -0400 | [diff] [blame] | 46 | #include "backends/minimalhistorybackend.h" |
Stepan Salenikovich | 434b88f | 2015-02-19 17:49:08 -0500 | [diff] [blame] | 47 | |
| 48 | struct _RingClientClass |
| 49 | { |
| 50 | GtkApplicationClass parent_class; |
| 51 | }; |
| 52 | |
| 53 | struct _RingClient |
| 54 | { |
| 55 | GtkApplication parent; |
| 56 | }; |
| 57 | |
| 58 | typedef struct _RingClientPrivate RingClientPrivate; |
Stepan Salenikovich | d81ef29 | 2015-02-17 18:47:37 -0500 | [diff] [blame] | 59 | |
| 60 | struct _RingClientPrivate { |
| 61 | /* main window */ |
| 62 | GtkWidget *win; |
| 63 | /* for libRingclient */ |
| 64 | QCoreApplication *qtapp; |
Stepan Salenikovich | 068fb69 | 2015-03-23 14:58:32 -0400 | [diff] [blame^] | 65 | /* UAM */ |
| 66 | QMetaObject::Connection uam_updated; |
Stepan Salenikovich | d81ef29 | 2015-02-17 18:47:37 -0500 | [diff] [blame] | 67 | }; |
| 68 | |
Stepan Salenikovich | 434b88f | 2015-02-19 17:49:08 -0500 | [diff] [blame] | 69 | G_DEFINE_TYPE_WITH_PRIVATE(RingClient, ring_client, GTK_TYPE_APPLICATION); |
Stepan Salenikovich | d81ef29 | 2015-02-17 18:47:37 -0500 | [diff] [blame] | 70 | |
| 71 | #define RING_CLIENT_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), RING_CLIENT_TYPE, RingClientPrivate)) |
| 72 | |
| 73 | static void |
| 74 | init_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 Salenikovich | 6977184 | 2015-02-24 18:11:45 -0500 | [diff] [blame] | 88 | static const GActionEntry ring_actions[] = |
| 89 | { |
Stepan Salenikovich | 347b73a | 2015-03-22 10:39:00 -0400 | [diff] [blame] | 90 | { "accept", NULL, NULL, NULL, NULL, {0} }, |
| 91 | { "hangup", NULL, NULL, NULL, NULL, {0} }, |
| 92 | { "hold", NULL, NULL, "false", NULL, {0} }, |
Stepan Salenikovich | c64523b | 2015-02-27 16:31:00 -0500 | [diff] [blame] | 93 | /* 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 Salenikovich | 6977184 | 2015-02-24 18:11:45 -0500 | [diff] [blame] | 98 | }; |
| 99 | |
Stepan Salenikovich | 347b73a | 2015-03-22 10:39:00 -0400 | [diff] [blame] | 100 | /* this union is used to pass the int refering to the Action as a parameter to the GAction callback */ |
Stepan Salenikovich | c64523b | 2015-02-27 16:31:00 -0500 | [diff] [blame] | 101 | typedef union _int_ptr_t |
| 102 | { |
| 103 | int value; |
| 104 | gpointer ptr; |
| 105 | } int_ptr_t; |
| 106 | |
| 107 | static void |
| 108 | activate_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 Salenikovich | c64523b | 2015-02-27 16:31:00 -0500 | [diff] [blame] | 119 | |
Stepan Salenikovich | 068fb69 | 2015-03-23 14:58:32 -0400 | [diff] [blame^] | 120 | static int |
| 121 | ring_client_startup(GApplication *app, gint argc, gchar **argv) |
Stepan Salenikovich | 6977184 | 2015-02-24 18:11:45 -0500 | [diff] [blame] | 122 | { |
| 123 | G_APPLICATION_CLASS(ring_client_parent_class)->startup(app); |
| 124 | |
| 125 | RingClient *client = RING_CLIENT(app); |
Stepan Salenikovich | 068fb69 | 2015-03-23 14:58:32 -0400 | [diff] [blame^] | 126 | RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client); |
Stepan Salenikovich | 6977184 | 2015-02-24 18:11:45 -0500 | [diff] [blame] | 127 | |
Stepan Salenikovich | 068fb69 | 2015-03-23 14:58:32 -0400 | [diff] [blame^] | 128 | /* 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 Salenikovich | 6977184 | 2015-02-24 18:11:45 -0500 | [diff] [blame] | 164 | g_action_map_add_action_entries( |
| 165 | G_ACTION_MAP(app), ring_actions, G_N_ELEMENTS(ring_actions), client); |
Stepan Salenikovich | c64523b | 2015-02-27 16:31:00 -0500 | [diff] [blame] | 166 | |
Stepan Salenikovich | 347b73a | 2015-03-22 10:39:00 -0400 | [diff] [blame] | 167 | /* 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 Salenikovich | c64523b | 2015-02-27 16:31:00 -0500 | [diff] [blame] | 177 | |
Stepan Salenikovich | 347b73a | 2015-03-22 10:39:00 -0400 | [diff] [blame] | 178 | 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 Salenikovich | c64523b | 2015-02-27 16:31:00 -0500 | [diff] [blame] | 184 | |
Stepan Salenikovich | 347b73a | 2015-03-22 10:39:00 -0400 | [diff] [blame] | 185 | /* change the state of the GActions based on the UserActionModel */ |
Stepan Salenikovich | 068fb69 | 2015-03-23 14:58:32 -0400 | [diff] [blame^] | 186 | priv->uam_updated = QObject::connect(uam,&UserActionModel::dataChanged, [actionHash,uam](const QModelIndex& tl, const QModelIndex& br) { |
Stepan Salenikovich | 347b73a | 2015-03-22 10:39:00 -0400 | [diff] [blame] | 187 | 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 Salenikovich | 068fb69 | 2015-03-23 14:58:32 -0400 | [diff] [blame^] | 200 | |
| 201 | return 0; |
| 202 | } |
| 203 | |
| 204 | static int |
| 205 | ring_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 Salenikovich | 6977184 | 2015-02-24 18:11:45 -0500 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | static void |
Stepan Salenikovich | d81ef29 | 2015-02-17 18:47:37 -0500 | [diff] [blame] | 236 | ring_client_shutdown(GApplication *app) |
| 237 | { |
| 238 | RingClient *self = RING_CLIENT(app); |
| 239 | RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(self); |
| 240 | |
Stepan Salenikovich | 068fb69 | 2015-03-23 14:58:32 -0400 | [diff] [blame^] | 241 | QObject::disconnect(priv->uam_updated); |
| 242 | |
Stepan Salenikovich | d81ef29 | 2015-02-17 18:47:37 -0500 | [diff] [blame] | 243 | /* 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 | |
| 251 | static void |
| 252 | ring_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 | |
| 261 | static void |
| 262 | ring_client_class_init(RingClientClass *klass) |
| 263 | { |
Stepan Salenikovich | d81ef29 | 2015-02-17 18:47:37 -0500 | [diff] [blame] | 264 | G_APPLICATION_CLASS(klass)->command_line = ring_client_command_line; |
| 265 | G_APPLICATION_CLASS(klass)->shutdown = ring_client_shutdown; |
| 266 | } |
| 267 | |
| 268 | RingClient * |
| 269 | ring_client_new() |
| 270 | { |
| 271 | return (RingClient *)g_object_new(ring_client_get_type(), |
Stepan Salenikovich | 434b88f | 2015-02-19 17:49:08 -0500 | [diff] [blame] | 272 | "application-id", "cx.ring.RingGnome", |
| 273 | "flags", G_APPLICATION_HANDLES_COMMAND_LINE, NULL); |
Stepan Salenikovich | d81ef29 | 2015-02-17 18:47:37 -0500 | [diff] [blame] | 274 | } |