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