Stepan Salenikovich | d81ef29 | 2015-02-17 18:47:37 -0500 | [diff] [blame] | 1 | /* |
Stepan Salenikovich | bbd6c13 | 2015-08-20 15:21:48 -0400 | [diff] [blame] | 2 | * Copyright (C) 2015 Savoir-faire Linux Inc. |
Stepan Salenikovich | d81ef29 | 2015-02-17 18:47:37 -0500 | [diff] [blame] | 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 |
Stepan Salenikovich | bbd6c13 | 2015-08-20 15:21:48 -0400 | [diff] [blame] | 24 | * terms of the OpenSSL or SSLeay licenses, Savoir-faire Linux Inc. |
Stepan Salenikovich | d81ef29 | 2015-02-17 18:47:37 -0500 | [diff] [blame] | 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 | 67c5dd3 | 2015-09-14 12:27:36 -0400 | [diff] [blame^] | 35 | #include <QtCore/QTranslator> |
Stepan Salenikovich | d2dbcee | 2015-02-27 16:52:28 -0500 | [diff] [blame] | 36 | #include <QtCore/QCoreApplication> |
| 37 | #include <QtCore/QString> |
| 38 | #include <QtCore/QByteArray> |
Stepan Salenikovich | d81ef29 | 2015-02-17 18:47:37 -0500 | [diff] [blame] | 39 | #include <callmodel.h> |
Stepan Salenikovich | d2dbcee | 2015-02-27 16:52:28 -0500 | [diff] [blame] | 40 | #include <QtCore/QItemSelectionModel> |
Stepan Salenikovich | c64523b | 2015-02-27 16:31:00 -0500 | [diff] [blame] | 41 | #include <useractionmodel.h> |
Stepan Salenikovich | 36c025c | 2015-03-03 19:06:44 -0500 | [diff] [blame] | 42 | #include <clutter-gtk/clutter-gtk.h> |
Stepan Salenikovich | dd84cf9 | 2015-03-19 21:38:19 -0400 | [diff] [blame] | 43 | #include <categorizedhistorymodel.h> |
Stepan Salenikovich | 6f68707 | 2015-03-26 10:43:37 -0400 | [diff] [blame] | 44 | #include <personmodel.h> |
| 45 | #include <fallbackpersoncollection.h> |
| 46 | #include <QtCore/QStandardPaths> |
Stepan Salenikovich | 1e131a4 | 2015-05-27 14:12:21 -0400 | [diff] [blame] | 47 | #include <localhistorycollection.h> |
Stepan Salenikovich | 67112d1 | 2015-06-16 16:57:06 -0400 | [diff] [blame] | 48 | #include <media/text.h> |
Stepan Salenikovich | f2d76c5 | 2015-07-17 17:54:56 -0400 | [diff] [blame] | 49 | #include <numbercategorymodel.h> |
Stepan Salenikovich | bbd6c13 | 2015-08-20 15:21:48 -0400 | [diff] [blame] | 50 | #include <globalinstances.h> |
| 51 | #include <memory> |
Stepan Salenikovich | d81ef29 | 2015-02-17 18:47:37 -0500 | [diff] [blame] | 52 | |
| 53 | #include "ring_client_options.h" |
Stepan Salenikovich | 434b88f | 2015-02-19 17:49:08 -0500 | [diff] [blame] | 54 | #include "ringmainwindow.h" |
Stepan Salenikovich | 24dcd5c | 2015-03-26 17:40:34 -0400 | [diff] [blame] | 55 | #include "dialogs.h" |
Stepan Salenikovich | 6f68707 | 2015-03-26 10:43:37 -0400 | [diff] [blame] | 56 | #include "backends/edscontactbackend.h" |
Stepan Salenikovich | bbd6c13 | 2015-08-20 15:21:48 -0400 | [diff] [blame] | 57 | #include "native/pixbufmanipulator.h" |
Stepan Salenikovich | 76c33e6 | 2015-05-22 12:24:07 -0400 | [diff] [blame] | 58 | #include "ringnotify.h" |
Stepan Salenikovich | 7635058 | 2015-05-25 14:56:32 -0400 | [diff] [blame] | 59 | #include "config.h" |
Stepan Salenikovich | fb7f295 | 2015-05-25 16:44:19 -0400 | [diff] [blame] | 60 | #include "utils/files.h" |
Stepan Salenikovich | 4f9bb98 | 2015-06-23 14:26:30 -0400 | [diff] [blame] | 61 | #include "revision.h" |
Stepan Salenikovich | 75a3917 | 2015-07-10 13:21:08 -0400 | [diff] [blame] | 62 | #include "utils/accounts.h" |
Stepan Salenikovich | 434b88f | 2015-02-19 17:49:08 -0500 | [diff] [blame] | 63 | |
| 64 | struct _RingClientClass |
| 65 | { |
Stepan Salenikovich | fb7f295 | 2015-05-25 16:44:19 -0400 | [diff] [blame] | 66 | GtkApplicationClass parent_class; |
Stepan Salenikovich | 434b88f | 2015-02-19 17:49:08 -0500 | [diff] [blame] | 67 | }; |
| 68 | |
| 69 | struct _RingClient |
| 70 | { |
| 71 | GtkApplication parent; |
| 72 | }; |
| 73 | |
| 74 | typedef struct _RingClientPrivate RingClientPrivate; |
Stepan Salenikovich | d81ef29 | 2015-02-17 18:47:37 -0500 | [diff] [blame] | 75 | |
| 76 | struct _RingClientPrivate { |
Stepan Salenikovich | 0d515e5 | 2015-05-19 16:31:05 -0400 | [diff] [blame] | 77 | /* args */ |
| 78 | int argc; |
| 79 | char **argv; |
Stepan Salenikovich | fb7f295 | 2015-05-25 16:44:19 -0400 | [diff] [blame] | 80 | |
| 81 | GSettings *settings; |
| 82 | |
Stepan Salenikovich | d81ef29 | 2015-02-17 18:47:37 -0500 | [diff] [blame] | 83 | /* main window */ |
| 84 | GtkWidget *win; |
| 85 | /* for libRingclient */ |
| 86 | QCoreApplication *qtapp; |
Stepan Salenikovich | 068fb69 | 2015-03-23 14:58:32 -0400 | [diff] [blame] | 87 | /* UAM */ |
| 88 | QMetaObject::Connection uam_updated; |
Stepan Salenikovich | 6f68707 | 2015-03-26 10:43:37 -0400 | [diff] [blame] | 89 | |
| 90 | GCancellable *cancellable; |
Stepan Salenikovich | d81ef29 | 2015-02-17 18:47:37 -0500 | [diff] [blame] | 91 | }; |
| 92 | |
Stepan Salenikovich | 0d515e5 | 2015-05-19 16:31:05 -0400 | [diff] [blame] | 93 | /* this union is used to pass ints as pointers and vice versa for GAction parameters*/ |
| 94 | typedef union _int_ptr_t |
| 95 | { |
| 96 | int value; |
| 97 | gint64 value64; |
| 98 | gpointer ptr; |
| 99 | } int_ptr_t; |
| 100 | |
Stepan Salenikovich | 434b88f | 2015-02-19 17:49:08 -0500 | [diff] [blame] | 101 | G_DEFINE_TYPE_WITH_PRIVATE(RingClient, ring_client, GTK_TYPE_APPLICATION); |
Stepan Salenikovich | d81ef29 | 2015-02-17 18:47:37 -0500 | [diff] [blame] | 102 | |
| 103 | #define RING_CLIENT_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), RING_CLIENT_TYPE, RingClientPrivate)) |
| 104 | |
| 105 | static void |
| 106 | init_exception_dialog(const char* msg) |
| 107 | { |
| 108 | g_warning("%s", msg); |
| 109 | GtkWidget *dialog = gtk_message_dialog_new(NULL, |
| 110 | (GtkDialogFlags)(GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT), |
| 111 | GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE, |
| 112 | _("Unable to initialize.\nMake sure the Ring daemon (dring) is running.\nError: %s"), |
| 113 | msg); |
| 114 | |
| 115 | gtk_window_set_title(GTK_WINDOW(dialog), _("Ring Error")); |
| 116 | gtk_dialog_run(GTK_DIALOG(dialog)); |
| 117 | gtk_widget_destroy(dialog); |
| 118 | } |
| 119 | |
Stepan Salenikovich | 2d63d5e | 2015-03-22 23:23:54 -0400 | [diff] [blame] | 120 | static void |
| 121 | ring_accelerators(RingClient *client) |
| 122 | { |
Stepan Salenikovich | 39ee49a | 2015-03-24 12:44:55 -0400 | [diff] [blame] | 123 | #if GTK_CHECK_VERSION(3,12,0) |
Stepan Salenikovich | 2d63d5e | 2015-03-22 23:23:54 -0400 | [diff] [blame] | 124 | const gchar *quit_accels[2] = { "<Ctrl>Q", NULL }; |
| 125 | gtk_application_set_accels_for_action(GTK_APPLICATION(client), "app.quit", quit_accels); |
| 126 | #else |
Stepan Salenikovich | 870d739 | 2015-09-08 11:18:46 -0400 | [diff] [blame] | 127 | gtk_application_add_accelerator(GTK_APPLICATION(client), "<Control>Q", "app.quit", NULL); |
Stepan Salenikovich | 2d63d5e | 2015-03-22 23:23:54 -0400 | [diff] [blame] | 128 | #endif |
| 129 | } |
| 130 | |
| 131 | static void |
| 132 | action_quit(G_GNUC_UNUSED GSimpleAction *simple, |
| 133 | G_GNUC_UNUSED GVariant *parameter, |
| 134 | gpointer user_data) |
| 135 | { |
| 136 | g_return_if_fail(G_IS_APPLICATION(user_data)); |
| 137 | |
| 138 | #if GLIB_CHECK_VERSION(2,32,0) |
| 139 | g_application_quit(G_APPLICATION(user_data)); |
| 140 | #else |
| 141 | RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(user_data); |
| 142 | gtk_widget_destroy(priv->win); |
| 143 | #endif |
| 144 | } |
| 145 | |
Stepan Salenikovich | 24dcd5c | 2015-03-26 17:40:34 -0400 | [diff] [blame] | 146 | static void |
| 147 | action_about(G_GNUC_UNUSED GSimpleAction *simple, |
| 148 | G_GNUC_UNUSED GVariant *parameter, |
| 149 | gpointer user_data) |
| 150 | { |
| 151 | g_return_if_fail(G_IS_APPLICATION(user_data)); |
| 152 | RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(user_data); |
| 153 | |
| 154 | ring_about_dialog(priv->win); |
| 155 | } |
| 156 | |
Stepan Salenikovich | 6977184 | 2015-02-24 18:11:45 -0500 | [diff] [blame] | 157 | static const GActionEntry ring_actions[] = |
| 158 | { |
Stepan Salenikovich | 24dcd5c | 2015-03-26 17:40:34 -0400 | [diff] [blame] | 159 | { "accept", NULL, NULL, NULL, NULL, {0} }, |
| 160 | { "hangup", NULL, NULL, NULL, NULL, {0} }, |
| 161 | { "hold", NULL, NULL, "false", NULL, {0} }, |
| 162 | { "quit", action_quit, NULL, NULL, NULL, {0} }, |
Stepan Salenikovich | 0d515e5 | 2015-05-19 16:31:05 -0400 | [diff] [blame] | 163 | { "about", action_about, NULL, NULL, NULL, {0} }, |
Stepan Salenikovich | 943bd05 | 2015-09-04 17:42:08 -0400 | [diff] [blame] | 164 | { "mute_audio", NULL, NULL, "false", NULL, {0} }, |
| 165 | { "mute_video", NULL, NULL, "false", NULL, {0} }, |
| 166 | { "record", NULL, NULL, "false", NULL, {0} }, |
Stepan Salenikovich | c64523b | 2015-02-27 16:31:00 -0500 | [diff] [blame] | 167 | /* TODO implement the other actions */ |
Stepan Salenikovich | c64523b | 2015-02-27 16:31:00 -0500 | [diff] [blame] | 168 | // { "transfer", NULL, NULL, "flase", NULL, {0} }, |
Stepan Salenikovich | 6977184 | 2015-02-24 18:11:45 -0500 | [diff] [blame] | 169 | }; |
| 170 | |
Stepan Salenikovich | c64523b | 2015-02-27 16:31:00 -0500 | [diff] [blame] | 171 | static void |
| 172 | activate_action(GSimpleAction *action, G_GNUC_UNUSED GVariant *parameter, gpointer user_data) |
| 173 | { |
| 174 | g_debug("activating action: %s", g_action_get_name(G_ACTION(action))); |
| 175 | |
| 176 | int_ptr_t key; |
Stepan Salenikovich | 0d515e5 | 2015-05-19 16:31:05 -0400 | [diff] [blame] | 177 | |
Stepan Salenikovich | c64523b | 2015-02-27 16:31:00 -0500 | [diff] [blame] | 178 | key.ptr = user_data; |
| 179 | UserActionModel::Action a = static_cast<UserActionModel::Action>(key.value); |
| 180 | UserActionModel* uam = CallModel::instance()->userActionModel(); |
| 181 | |
| 182 | uam << a; |
| 183 | } |
Stepan Salenikovich | c64523b | 2015-02-27 16:31:00 -0500 | [diff] [blame] | 184 | |
Stepan Salenikovich | 0d515e5 | 2015-05-19 16:31:05 -0400 | [diff] [blame] | 185 | static void |
Stepan Salenikovich | fb7f295 | 2015-05-25 16:44:19 -0400 | [diff] [blame] | 186 | autostart_toggled(GSettings *settings, G_GNUC_UNUSED gchar *key, G_GNUC_UNUSED gpointer user_data) |
| 187 | { |
| 188 | autostart_symlink(g_settings_get_boolean(settings, "start-on-login")); |
| 189 | } |
| 190 | |
Stepan Salenikovich | bc6c4be | 2015-07-31 16:07:54 -0400 | [diff] [blame] | 191 | static gboolean |
| 192 | on_close_window(GtkWidget *window, G_GNUC_UNUSED GdkEvent *event, RingClient *client) |
| 193 | { |
| 194 | g_return_val_if_fail(GTK_IS_WINDOW(window) && IS_RING_CLIENT(client), FALSE); |
| 195 | |
| 196 | RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client); |
| 197 | |
| 198 | if (g_settings_get_boolean(priv->settings, "hide-on-close")) { |
| 199 | /* we want to simply hide the window and keep the client running */ |
| 200 | g_debug("hiding main window"); |
| 201 | gtk_widget_hide(window); |
| 202 | return TRUE; /* do not propogate event */ |
| 203 | } else { |
| 204 | /* we want to quit the application, so just propogate the event */ |
| 205 | return FALSE; |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | static void |
| 210 | ring_client_activate(GApplication *app) |
| 211 | { |
| 212 | RingClient *client = RING_CLIENT(app); |
| 213 | RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client); |
| 214 | |
| 215 | if (priv->win == NULL) { |
| 216 | priv->win = ring_main_window_new(GTK_APPLICATION(app)); |
| 217 | |
| 218 | /* make sure win is set to NULL when the window is destroyed */ |
| 219 | g_object_add_weak_pointer(G_OBJECT(priv->win), (gpointer *)&priv->win); |
| 220 | |
| 221 | /* check if the window should be destoryed or not on close */ |
| 222 | g_signal_connect(priv->win, "delete-event", G_CALLBACK(on_close_window), client); |
| 223 | } |
| 224 | |
| 225 | g_debug("show window"); |
| 226 | gtk_window_present(GTK_WINDOW(priv->win)); |
| 227 | } |
| 228 | |
Stepan Salenikovich | fb7f295 | 2015-05-25 16:44:19 -0400 | [diff] [blame] | 229 | static void |
Stepan Salenikovich | 0d515e5 | 2015-05-19 16:31:05 -0400 | [diff] [blame] | 230 | ring_client_startup(GApplication *app) |
Stepan Salenikovich | 6977184 | 2015-02-24 18:11:45 -0500 | [diff] [blame] | 231 | { |
Stepan Salenikovich | 6977184 | 2015-02-24 18:11:45 -0500 | [diff] [blame] | 232 | RingClient *client = RING_CLIENT(app); |
Stepan Salenikovich | 068fb69 | 2015-03-23 14:58:32 -0400 | [diff] [blame] | 233 | RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client); |
Stepan Salenikovich | 6977184 | 2015-02-24 18:11:45 -0500 | [diff] [blame] | 234 | |
Stepan Salenikovich | 4f9bb98 | 2015-06-23 14:26:30 -0400 | [diff] [blame] | 235 | g_message("Ring GNOME client version: %d.%d.%d", VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH); |
| 236 | g_message("git ref: %s", RING_CLIENT_REVISION); |
| 237 | |
Stepan Salenikovich | fb7f295 | 2015-05-25 16:44:19 -0400 | [diff] [blame] | 238 | /* make sure that the system corresponds to the autostart setting */ |
| 239 | autostart_symlink(g_settings_get_boolean(priv->settings, "start-on-login")); |
| 240 | g_signal_connect(priv->settings, "changed::start-on-login", G_CALLBACK(autostart_toggled), NULL); |
| 241 | |
Stepan Salenikovich | 068fb69 | 2015-03-23 14:58:32 -0400 | [diff] [blame] | 242 | /* init clutter */ |
| 243 | int clutter_error; |
Stepan Salenikovich | 0d515e5 | 2015-05-19 16:31:05 -0400 | [diff] [blame] | 244 | if ((clutter_error = gtk_clutter_init(&priv->argc, &priv->argv)) != CLUTTER_INIT_SUCCESS) { |
| 245 | g_error("Could not init clutter : %d\n", clutter_error); |
| 246 | exit(1); /* the g_error above should normally cause the applicaiton to exit */ |
Stepan Salenikovich | 068fb69 | 2015-03-23 14:58:32 -0400 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | /* init libRingClient and make sure its connected to the dbus */ |
| 250 | try { |
Stepan Salenikovich | 0d515e5 | 2015-05-19 16:31:05 -0400 | [diff] [blame] | 251 | priv->qtapp = new QCoreApplication(priv->argc, priv->argv); |
Stepan Salenikovich | 068fb69 | 2015-03-23 14:58:32 -0400 | [diff] [blame] | 252 | /* the call model will try to connect to dring via dbus */ |
| 253 | CallModel::instance(); |
| 254 | } catch (const char * msg) { |
| 255 | init_exception_dialog(msg); |
Stepan Salenikovich | 0d515e5 | 2015-05-19 16:31:05 -0400 | [diff] [blame] | 256 | g_error("%s", msg); |
| 257 | exit(1); /* the g_error above should normally cause the applicaiton to exit */ |
Stepan Salenikovich | 068fb69 | 2015-03-23 14:58:32 -0400 | [diff] [blame] | 258 | } catch(QString& msg) { |
| 259 | QByteArray ba = msg.toLocal8Bit(); |
| 260 | const char *c_str = ba.data(); |
| 261 | init_exception_dialog(c_str); |
Stepan Salenikovich | 0d515e5 | 2015-05-19 16:31:05 -0400 | [diff] [blame] | 262 | g_error("%s", c_str); |
| 263 | exit(1); /* the g_error above should normally cause the applicaiton to exit */ |
Stepan Salenikovich | 068fb69 | 2015-03-23 14:58:32 -0400 | [diff] [blame] | 264 | } |
| 265 | |
Stepan Salenikovich | 67c5dd3 | 2015-09-14 12:27:36 -0400 | [diff] [blame^] | 266 | /* load translations from LRC */ |
| 267 | QTranslator translator; |
| 268 | if (translator.load(QLocale::system(), "lrc", "_", RING_CLIENT_INSTALL "/share/libringclient/translations")) { |
| 269 | priv->qtapp->installTranslator(&translator); |
| 270 | } else { |
| 271 | g_debug("could not load LRC translations for %s, %s", |
| 272 | QLocale::languageToString(QLocale::system().language()).toUtf8().constData(), |
| 273 | QLocale::countryToString(QLocale::system().country()).toUtf8().constData() |
| 274 | ); |
| 275 | } |
| 276 | |
Stepan Salenikovich | 6f68707 | 2015-03-26 10:43:37 -0400 | [diff] [blame] | 277 | /* init delegates */ |
Stepan Salenikovich | bbd6c13 | 2015-08-20 15:21:48 -0400 | [diff] [blame] | 278 | GlobalInstances::setPixmapManipulator(std::unique_ptr<Interfaces::PixbufManipulator>(new Interfaces::PixbufManipulator())); |
Stepan Salenikovich | 6f68707 | 2015-03-26 10:43:37 -0400 | [diff] [blame] | 279 | |
Stepan Salenikovich | 75a3917 | 2015-07-10 13:21:08 -0400 | [diff] [blame] | 280 | /* make sure all RING accounts have a display name... this basically makes sure |
| 281 | * that all accounts created before the display name patch have a display name |
| 282 | * set... a bit of a hack as this should maybe be done in LRC */ |
| 283 | force_ring_display_name(); |
| 284 | |
Stepan Salenikovich | f2d76c5 | 2015-07-17 17:54:56 -0400 | [diff] [blame] | 285 | /* make sure basic number categories exist, in case user has no contacts |
| 286 | * from which these would be automatically created |
| 287 | */ |
| 288 | NumberCategoryModel::instance()->addCategory("work", QVariant()); |
| 289 | NumberCategoryModel::instance()->addCategory("home", QVariant()); |
| 290 | |
Stepan Salenikovich | 068fb69 | 2015-03-23 14:58:32 -0400 | [diff] [blame] | 291 | /* add backends */ |
Stepan Salenikovich | 1e131a4 | 2015-05-27 14:12:21 -0400 | [diff] [blame] | 292 | CategorizedHistoryModel::instance()->addCollection<LocalHistoryCollection>(LoadOptions::FORCE_ENABLED); |
Stepan Salenikovich | 068fb69 | 2015-03-23 14:58:32 -0400 | [diff] [blame] | 293 | |
Stepan Salenikovich | f2d76c5 | 2015-07-17 17:54:56 -0400 | [diff] [blame] | 294 | /* fallback backend for vcards */ |
Stepan Salenikovich | 6f68707 | 2015-03-26 10:43:37 -0400 | [diff] [blame] | 295 | PersonModel::instance()->addCollection<FallbackPersonCollection>(LoadOptions::FORCE_ENABLED); |
| 296 | |
Stepan Salenikovich | f2d76c5 | 2015-07-17 17:54:56 -0400 | [diff] [blame] | 297 | /* EDS backend(s) */ |
Stepan Salenikovich | 6f68707 | 2015-03-26 10:43:37 -0400 | [diff] [blame] | 298 | load_eds_sources(priv->cancellable); |
| 299 | |
Stepan Salenikovich | 068fb69 | 2015-03-23 14:58:32 -0400 | [diff] [blame] | 300 | /* Override theme since we don't have appropriate icons for a dark them (yet) */ |
| 301 | GtkSettings *gtk_settings = gtk_settings_get_default(); |
| 302 | g_object_set(G_OBJECT(gtk_settings), "gtk-application-prefer-dark-theme", |
| 303 | FALSE, NULL); |
| 304 | /* enable button icons */ |
| 305 | g_object_set(G_OBJECT(gtk_settings), "gtk-button-images", |
| 306 | TRUE, NULL); |
| 307 | |
| 308 | /* add GActions */ |
Stepan Salenikovich | 6977184 | 2015-02-24 18:11:45 -0500 | [diff] [blame] | 309 | g_action_map_add_action_entries( |
Stepan Salenikovich | 2d63d5e | 2015-03-22 23:23:54 -0400 | [diff] [blame] | 310 | G_ACTION_MAP(app), ring_actions, G_N_ELEMENTS(ring_actions), app); |
| 311 | |
| 312 | /* add accelerators */ |
| 313 | ring_accelerators(RING_CLIENT(app)); |
Stepan Salenikovich | c64523b | 2015-02-27 16:31:00 -0500 | [diff] [blame] | 314 | |
Stepan Salenikovich | 347b73a | 2015-03-22 10:39:00 -0400 | [diff] [blame] | 315 | /* Bind GActions to the UserActionModel */ |
| 316 | UserActionModel* uam = CallModel::instance()->userActionModel(); |
| 317 | QHash<int, GSimpleAction*> actionHash; |
| 318 | actionHash[ (int)UserActionModel::Action::ACCEPT ] = G_SIMPLE_ACTION(g_action_map_lookup_action(G_ACTION_MAP(app), "accept")); |
| 319 | actionHash[ (int)UserActionModel::Action::HOLD ] = G_SIMPLE_ACTION(g_action_map_lookup_action(G_ACTION_MAP(app), "hold")); |
Stepan Salenikovich | 7d75eab | 2015-06-12 12:11:45 -0400 | [diff] [blame] | 320 | actionHash[ (int)UserActionModel::Action::MUTE_AUDIO ] = G_SIMPLE_ACTION(g_action_map_lookup_action(G_ACTION_MAP(app), "mute_audio")); |
| 321 | actionHash[ (int)UserActionModel::Action::MUTE_VIDEO ] = G_SIMPLE_ACTION(g_action_map_lookup_action(G_ACTION_MAP(app), "mute_video")); |
Stepan Salenikovich | 347b73a | 2015-03-22 10:39:00 -0400 | [diff] [blame] | 322 | /* TODO: add commented actions when ready */ |
Stepan Salenikovich | 347b73a | 2015-03-22 10:39:00 -0400 | [diff] [blame] | 323 | // actionHash[ (int)UserActionModel::Action::SERVER_TRANSFER ] = G_SIMPLE_ACTION(g_action_map_lookup_action(G_ACTION_MAP(app), "transfer")); |
Stepan Salenikovich | 943bd05 | 2015-09-04 17:42:08 -0400 | [diff] [blame] | 324 | actionHash[ (int)UserActionModel::Action::RECORD ] = G_SIMPLE_ACTION(g_action_map_lookup_action(G_ACTION_MAP(app), "record")); |
Stepan Salenikovich | 347b73a | 2015-03-22 10:39:00 -0400 | [diff] [blame] | 325 | 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] | 326 | |
Stepan Salenikovich | 347b73a | 2015-03-22 10:39:00 -0400 | [diff] [blame] | 327 | for (QHash<int,GSimpleAction*>::const_iterator i = actionHash.begin(); i != actionHash.end(); ++i) { |
| 328 | GSimpleAction* sa = i.value(); |
| 329 | int_ptr_t user_data; |
| 330 | user_data.value = i.key(); |
| 331 | g_signal_connect(G_OBJECT(sa), "activate", G_CALLBACK(activate_action), user_data.ptr); |
| 332 | } |
Stepan Salenikovich | c64523b | 2015-02-27 16:31:00 -0500 | [diff] [blame] | 333 | |
Stepan Salenikovich | 347b73a | 2015-03-22 10:39:00 -0400 | [diff] [blame] | 334 | /* change the state of the GActions based on the UserActionModel */ |
Stepan Salenikovich | 068fb69 | 2015-03-23 14:58:32 -0400 | [diff] [blame] | 335 | 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] | 336 | const int first(tl.row()),last(br.row()); |
| 337 | for(int i = first; i <= last;i++) { |
| 338 | const QModelIndex& idx = uam->index(i,0); |
| 339 | GSimpleAction* sa = actionHash[(int)qvariant_cast<UserActionModel::Action>(idx.data(UserActionModel::Role::ACTION))]; |
| 340 | if (sa) { |
| 341 | /* enable/disable GAction based on UserActionModel */ |
| 342 | g_simple_action_set_enabled(sa, idx.flags() & Qt::ItemIsEnabled); |
| 343 | /* set the state of the action if its stateful */ |
| 344 | if (g_action_get_state_type(G_ACTION(sa)) != NULL) |
| 345 | g_simple_action_set_state(sa, g_variant_new_boolean(idx.data(Qt::CheckStateRole) == Qt::Checked)); |
| 346 | } |
| 347 | } |
| 348 | }); |
Stepan Salenikovich | 068fb69 | 2015-03-23 14:58:32 -0400 | [diff] [blame] | 349 | |
Stepan Salenikovich | bc6c4be | 2015-07-31 16:07:54 -0400 | [diff] [blame] | 350 | /* show window on incoming calls (if the option is set)*/ |
| 351 | QObject::connect(CallModel::instance(), &CallModel::incomingCall, |
| 352 | [app] (G_GNUC_UNUSED Call *call) { |
| 353 | RingClient *client = RING_CLIENT(app); |
| 354 | RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client); |
| 355 | if (g_settings_get_boolean(priv->settings, "bring-window-to-front")) |
| 356 | ring_client_activate(app); |
| 357 | } |
| 358 | ); |
| 359 | |
Stepan Salenikovich | 76c33e6 | 2015-05-22 12:24:07 -0400 | [diff] [blame] | 360 | /* send call notifications */ |
| 361 | ring_notify_init(); |
| 362 | QObject::connect(CallModel::instance(), &CallModel::incomingCall, |
Stepan Salenikovich | 67112d1 | 2015-06-16 16:57:06 -0400 | [diff] [blame] | 363 | [] (Call *call) { ring_notify_incoming_call(call);} |
Stepan Salenikovich | 76c33e6 | 2015-05-22 12:24:07 -0400 | [diff] [blame] | 364 | ); |
| 365 | |
Stepan Salenikovich | 67112d1 | 2015-06-16 16:57:06 -0400 | [diff] [blame] | 366 | /* chat notifications for incoming messages on all calls which are not the |
| 367 | * currently selected call */ |
| 368 | ring_notify_monitor_chat_notifications(client); |
| 369 | |
Stepan Salenikovich | 0d515e5 | 2015-05-19 16:31:05 -0400 | [diff] [blame] | 370 | #if GLIB_CHECK_VERSION(2,40,0) |
| 371 | G_APPLICATION_CLASS(ring_client_parent_class)->startup(app); |
| 372 | #else |
| 373 | /* don't need to chain up to the parent callback as this function will |
| 374 | * be called manually by the command_line callback in this case */ |
| 375 | #endif |
Stepan Salenikovich | 068fb69 | 2015-03-23 14:58:32 -0400 | [diff] [blame] | 376 | } |
| 377 | |
Stepan Salenikovich | 0d515e5 | 2015-05-19 16:31:05 -0400 | [diff] [blame] | 378 | #if !GLIB_CHECK_VERSION(2,40,0) |
Stepan Salenikovich | 068fb69 | 2015-03-23 14:58:32 -0400 | [diff] [blame] | 379 | static int |
| 380 | ring_client_command_line(GApplication *app, GApplicationCommandLine *cmdline) |
| 381 | { |
Stepan Salenikovich | 068fb69 | 2015-03-23 14:58:32 -0400 | [diff] [blame] | 382 | gint argc; |
| 383 | gchar **argv = g_application_command_line_get_arguments(cmdline, &argc); |
| 384 | GOptionContext *context = ring_client_options_get_context(); |
| 385 | GError *error = NULL; |
| 386 | if (g_option_context_parse(context, &argc, &argv, &error) == FALSE) { |
| 387 | g_print(_("%s\nRun '%s --help' to see a full list of available command line options.\n"), |
| 388 | error->message, argv[0]); |
Stepan Salenikovich | 8a287fc | 2015-05-01 16:53:20 -0400 | [diff] [blame] | 389 | g_clear_error(&error); |
Stepan Salenikovich | 068fb69 | 2015-03-23 14:58:32 -0400 | [diff] [blame] | 390 | g_option_context_free(context); |
Stepan Salenikovich | 0d515e5 | 2015-05-19 16:31:05 -0400 | [diff] [blame] | 391 | g_strfreev(argv); |
Stepan Salenikovich | 068fb69 | 2015-03-23 14:58:32 -0400 | [diff] [blame] | 392 | return 1; |
| 393 | } |
| 394 | g_option_context_free(context); |
Stepan Salenikovich | 0d515e5 | 2015-05-19 16:31:05 -0400 | [diff] [blame] | 395 | g_strfreev(argv); |
Stepan Salenikovich | 068fb69 | 2015-03-23 14:58:32 -0400 | [diff] [blame] | 396 | |
Stepan Salenikovich | 0d515e5 | 2015-05-19 16:31:05 -0400 | [diff] [blame] | 397 | if (!g_application_get_is_remote(app)) { |
| 398 | /* if this is the primary instance, we must peform the startup */ |
| 399 | ring_client_startup(app); |
| 400 | } |
| 401 | |
| 402 | g_application_activate(app); |
| 403 | |
| 404 | return 0; |
| 405 | } |
| 406 | #endif |
| 407 | |
| 408 | static void |
Stepan Salenikovich | d81ef29 | 2015-02-17 18:47:37 -0500 | [diff] [blame] | 409 | ring_client_shutdown(GApplication *app) |
| 410 | { |
| 411 | RingClient *self = RING_CLIENT(app); |
| 412 | RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(self); |
| 413 | |
Stepan Salenikovich | 2d63d5e | 2015-03-22 23:23:54 -0400 | [diff] [blame] | 414 | g_debug("quitting"); |
| 415 | |
Stepan Salenikovich | 6f68707 | 2015-03-26 10:43:37 -0400 | [diff] [blame] | 416 | /* cancel any pending cancellable operations */ |
| 417 | g_cancellable_cancel(priv->cancellable); |
| 418 | g_object_unref(priv->cancellable); |
| 419 | |
Stepan Salenikovich | 068fb69 | 2015-03-23 14:58:32 -0400 | [diff] [blame] | 420 | QObject::disconnect(priv->uam_updated); |
| 421 | |
Stepan Salenikovich | d81ef29 | 2015-02-17 18:47:37 -0500 | [diff] [blame] | 422 | /* free the QCoreApplication, which will destroy all libRingClient models |
| 423 | * and thus send the Unregister signal over dbus to dring */ |
| 424 | delete priv->qtapp; |
| 425 | |
Stepan Salenikovich | 0d515e5 | 2015-05-19 16:31:05 -0400 | [diff] [blame] | 426 | /* free the copied cmd line args */ |
| 427 | g_strfreev(priv->argv); |
| 428 | |
Stepan Salenikovich | fb7f295 | 2015-05-25 16:44:19 -0400 | [diff] [blame] | 429 | g_clear_object(&priv->settings); |
| 430 | |
Stepan Salenikovich | 76c33e6 | 2015-05-22 12:24:07 -0400 | [diff] [blame] | 431 | ring_notify_uninit(); |
| 432 | |
Stepan Salenikovich | d81ef29 | 2015-02-17 18:47:37 -0500 | [diff] [blame] | 433 | /* Chain up to the parent class */ |
| 434 | G_APPLICATION_CLASS(ring_client_parent_class)->shutdown(app); |
| 435 | } |
| 436 | |
| 437 | static void |
| 438 | ring_client_init(RingClient *self) |
| 439 | { |
| 440 | RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(self); |
| 441 | |
Stepan Salenikovich | d81ef29 | 2015-02-17 18:47:37 -0500 | [diff] [blame] | 442 | priv->win = NULL; |
| 443 | priv->qtapp = NULL; |
Stepan Salenikovich | 6f68707 | 2015-03-26 10:43:37 -0400 | [diff] [blame] | 444 | priv->cancellable = g_cancellable_new(); |
Stepan Salenikovich | fb7f295 | 2015-05-25 16:44:19 -0400 | [diff] [blame] | 445 | priv->settings = g_settings_new_full(get_ring_schema(), NULL, NULL); |
Stepan Salenikovich | 0d515e5 | 2015-05-19 16:31:05 -0400 | [diff] [blame] | 446 | |
| 447 | #if GLIB_CHECK_VERSION(2,40,0) |
| 448 | /* add custom cmd line options */ |
| 449 | ring_client_add_options(G_APPLICATION(self)); |
| 450 | #endif |
Stepan Salenikovich | d81ef29 | 2015-02-17 18:47:37 -0500 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | static void |
| 454 | ring_client_class_init(RingClientClass *klass) |
| 455 | { |
Stepan Salenikovich | 0d515e5 | 2015-05-19 16:31:05 -0400 | [diff] [blame] | 456 | #if GLIB_CHECK_VERSION(2,40,0) |
| 457 | G_APPLICATION_CLASS(klass)->startup = ring_client_startup; |
| 458 | #else |
Stepan Salenikovich | d81ef29 | 2015-02-17 18:47:37 -0500 | [diff] [blame] | 459 | G_APPLICATION_CLASS(klass)->command_line = ring_client_command_line; |
Stepan Salenikovich | 0d515e5 | 2015-05-19 16:31:05 -0400 | [diff] [blame] | 460 | #endif |
| 461 | G_APPLICATION_CLASS(klass)->activate = ring_client_activate; |
Stepan Salenikovich | d81ef29 | 2015-02-17 18:47:37 -0500 | [diff] [blame] | 462 | G_APPLICATION_CLASS(klass)->shutdown = ring_client_shutdown; |
| 463 | } |
| 464 | |
| 465 | RingClient * |
Stepan Salenikovich | 0d515e5 | 2015-05-19 16:31:05 -0400 | [diff] [blame] | 466 | ring_client_new(int argc, char *argv[]) |
Stepan Salenikovich | d81ef29 | 2015-02-17 18:47:37 -0500 | [diff] [blame] | 467 | { |
Stepan Salenikovich | 0d515e5 | 2015-05-19 16:31:05 -0400 | [diff] [blame] | 468 | /* because the g_application_add_main_option_entries was only added in |
| 469 | * glib 2.40, for lower versions we must handle the command line options |
| 470 | * ourselves |
| 471 | */ |
| 472 | RingClient *client = (RingClient *)g_object_new(ring_client_get_type(), |
Stepan Salenikovich | 7635058 | 2015-05-25 14:56:32 -0400 | [diff] [blame] | 473 | "application-id", RING_CLIENT_APP_ID, |
Stepan Salenikovich | 0d515e5 | 2015-05-19 16:31:05 -0400 | [diff] [blame] | 474 | #if GLIB_CHECK_VERSION(2,40,0) |
| 475 | NULL); |
| 476 | #else |
| 477 | "flags", G_APPLICATION_HANDLES_COMMAND_LINE, |
| 478 | NULL); |
| 479 | #endif |
| 480 | |
| 481 | /* copy the cmd line args before they get processed by the GApplication*/ |
| 482 | RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client); |
| 483 | priv->argc = argc; |
| 484 | priv->argv = g_strdupv((gchar **)argv); |
| 485 | |
| 486 | return client; |
Stepan Salenikovich | d81ef29 | 2015-02-17 18:47:37 -0500 | [diff] [blame] | 487 | } |
Stepan Salenikovich | 67112d1 | 2015-06-16 16:57:06 -0400 | [diff] [blame] | 488 | |
| 489 | GtkWindow * |
| 490 | ring_client_get_main_windw(RingClient *client) |
| 491 | { |
| 492 | g_return_val_if_fail(IS_RING_CLIENT(client), NULL); |
| 493 | RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client); |
| 494 | |
| 495 | return (GtkWindow *)priv->win; |
| 496 | } |