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