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