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 | |
Stepan Salenikovich | 472c905 | 2016-07-20 19:16:02 -0400 | [diff] [blame] | 22 | // system |
| 23 | #include <memory> |
Victor Nikulshin | 167bbb6 | 2017-03-15 20:44:27 +0000 | [diff] [blame] | 24 | #include <regex> |
Stepan Salenikovich | 472c905 | 2016-07-20 19:16:02 -0400 | [diff] [blame] | 25 | |
| 26 | // GTK+ related |
Stepan Salenikovich | d81ef29 | 2015-02-17 18:47:37 -0500 | [diff] [blame] | 27 | #include <gtk/gtk.h> |
| 28 | #include <glib/gi18n.h> |
Stepan Salenikovich | 472c905 | 2016-07-20 19:16:02 -0400 | [diff] [blame] | 29 | #include <clutter-gtk/clutter-gtk.h> |
| 30 | |
| 31 | // Qt |
Stepan Salenikovich | 67c5dd3 | 2015-09-14 12:27:36 -0400 | [diff] [blame] | 32 | #include <QtCore/QTranslator> |
Stepan Salenikovich | d2dbcee | 2015-02-27 16:52:28 -0500 | [diff] [blame] | 33 | #include <QtCore/QCoreApplication> |
| 34 | #include <QtCore/QString> |
| 35 | #include <QtCore/QByteArray> |
Stepan Salenikovich | d2dbcee | 2015-02-27 16:52:28 -0500 | [diff] [blame] | 36 | #include <QtCore/QItemSelectionModel> |
Stepan Salenikovich | 472c905 | 2016-07-20 19:16:02 -0400 | [diff] [blame] | 37 | #include <QtCore/QStandardPaths> |
| 38 | |
| 39 | // LRC |
| 40 | #include <callmodel.h> |
Stepan Salenikovich | c64523b | 2015-02-27 16:31:00 -0500 | [diff] [blame] | 41 | #include <useractionmodel.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> |
Stepan Salenikovich | 1e131a4 | 2015-05-27 14:12:21 -0400 | [diff] [blame] | 45 | #include <localhistorycollection.h> |
Stepan Salenikovich | 67112d1 | 2015-06-16 16:57:06 -0400 | [diff] [blame] | 46 | #include <media/text.h> |
Stepan Salenikovich | f2d76c5 | 2015-07-17 17:54:56 -0400 | [diff] [blame] | 47 | #include <numbercategorymodel.h> |
Stepan Salenikovich | bbd6c13 | 2015-08-20 15:21:48 -0400 | [diff] [blame] | 48 | #include <globalinstances.h> |
Stepan Salenikovich | 472c905 | 2016-07-20 19:16:02 -0400 | [diff] [blame] | 49 | #include <profilemodel.h> |
| 50 | #include <profile.h> |
| 51 | #include <peerprofilecollection.h> |
| 52 | #include <localprofilecollection.h> |
| 53 | #include <accountmodel.h> |
Olivier Gregoire | 66e4df7 | 2016-06-17 18:39:05 -0400 | [diff] [blame] | 54 | #include <smartinfohub.h> |
Stepan Salenikovich | 5a12767 | 2016-09-13 11:19:50 -0400 | [diff] [blame] | 55 | #include <media/textrecording.h> |
| 56 | #include <media/recordingmodel.h> |
Stepan Salenikovich | d81ef29 | 2015-02-17 18:47:37 -0500 | [diff] [blame] | 57 | |
Stepan Salenikovich | 472c905 | 2016-07-20 19:16:02 -0400 | [diff] [blame] | 58 | // Ring client |
Stepan Salenikovich | d81ef29 | 2015-02-17 18:47:37 -0500 | [diff] [blame] | 59 | #include "ring_client_options.h" |
Stepan Salenikovich | 434b88f | 2015-02-19 17:49:08 -0500 | [diff] [blame] | 60 | #include "ringmainwindow.h" |
Stepan Salenikovich | 24dcd5c | 2015-03-26 17:40:34 -0400 | [diff] [blame] | 61 | #include "dialogs.h" |
Stepan Salenikovich | 6f68707 | 2015-03-26 10:43:37 -0400 | [diff] [blame] | 62 | #include "backends/edscontactbackend.h" |
Stepan Salenikovich | bbd6c13 | 2015-08-20 15:21:48 -0400 | [diff] [blame] | 63 | #include "native/pixbufmanipulator.h" |
Stepan Salenikovich | be6550c | 2015-08-21 16:16:03 -0400 | [diff] [blame] | 64 | #include "native/dbuserrorhandler.h" |
Stepan Salenikovich | 76c33e6 | 2015-05-22 12:24:07 -0400 | [diff] [blame] | 65 | #include "ringnotify.h" |
Stepan Salenikovich | 7635058 | 2015-05-25 14:56:32 -0400 | [diff] [blame] | 66 | #include "config.h" |
Stepan Salenikovich | fb7f295 | 2015-05-25 16:44:19 -0400 | [diff] [blame] | 67 | #include "utils/files.h" |
Stepan Salenikovich | 4f9bb98 | 2015-06-23 14:26:30 -0400 | [diff] [blame] | 68 | #include "revision.h" |
Stepan Salenikovich | 75a3917 | 2015-07-10 13:21:08 -0400 | [diff] [blame] | 69 | #include "utils/accounts.h" |
Victor Nikulshin | 167bbb6 | 2017-03-15 20:44:27 +0000 | [diff] [blame] | 70 | #include "utils/calling.h" |
Stepan Salenikovich | 434b88f | 2015-02-19 17:49:08 -0500 | [diff] [blame] | 71 | |
Stepan Salenikovich | 72d812f | 2017-01-05 12:16:25 -0500 | [diff] [blame] | 72 | #if HAVE_APPINDICATOR |
Stepan Salenikovich | 982b288 | 2016-06-15 13:13:37 -0400 | [diff] [blame] | 73 | #include <libappindicator/app-indicator.h> |
| 74 | #endif |
| 75 | |
Stepan Salenikovich | 472c905 | 2016-07-20 19:16:02 -0400 | [diff] [blame] | 76 | #if USE_LIBNM |
Stepan Salenikovich | 28ca317 | 2016-07-22 17:29:11 -0400 | [diff] [blame] | 77 | #include <libnm-glib/nm-client.h> |
Stepan Salenikovich | 472c905 | 2016-07-20 19:16:02 -0400 | [diff] [blame] | 78 | #endif |
| 79 | |
Stepan Salenikovich | 434b88f | 2015-02-19 17:49:08 -0500 | [diff] [blame] | 80 | struct _RingClientClass |
| 81 | { |
Stepan Salenikovich | fb7f295 | 2015-05-25 16:44:19 -0400 | [diff] [blame] | 82 | GtkApplicationClass parent_class; |
Stepan Salenikovich | 434b88f | 2015-02-19 17:49:08 -0500 | [diff] [blame] | 83 | }; |
| 84 | |
| 85 | struct _RingClient |
| 86 | { |
| 87 | GtkApplication parent; |
| 88 | }; |
| 89 | |
| 90 | typedef struct _RingClientPrivate RingClientPrivate; |
Stepan Salenikovich | d81ef29 | 2015-02-17 18:47:37 -0500 | [diff] [blame] | 91 | |
| 92 | struct _RingClientPrivate { |
Stepan Salenikovich | 0d515e5 | 2015-05-19 16:31:05 -0400 | [diff] [blame] | 93 | /* args */ |
| 94 | int argc; |
| 95 | char **argv; |
Stepan Salenikovich | fb7f295 | 2015-05-25 16:44:19 -0400 | [diff] [blame] | 96 | |
| 97 | GSettings *settings; |
| 98 | |
Stepan Salenikovich | d81ef29 | 2015-02-17 18:47:37 -0500 | [diff] [blame] | 99 | /* main window */ |
| 100 | GtkWidget *win; |
| 101 | /* for libRingclient */ |
| 102 | QCoreApplication *qtapp; |
Stepan Salenikovich | 068fb69 | 2015-03-23 14:58:32 -0400 | [diff] [blame] | 103 | /* UAM */ |
| 104 | QMetaObject::Connection uam_updated; |
Stepan Salenikovich | 6f68707 | 2015-03-26 10:43:37 -0400 | [diff] [blame] | 105 | |
Stepan Salenikovich | 6c39b10 | 2015-09-15 14:24:45 -0400 | [diff] [blame] | 106 | std::unique_ptr<QTranslator> translator; |
| 107 | |
Stepan Salenikovich | 6f68707 | 2015-03-26 10:43:37 -0400 | [diff] [blame] | 108 | GCancellable *cancellable; |
Stepan Salenikovich | bb9c24e | 2015-09-15 09:55:02 -0400 | [diff] [blame] | 109 | |
| 110 | gboolean restore_window_state; |
Stepan Salenikovich | 982b288 | 2016-06-15 13:13:37 -0400 | [diff] [blame] | 111 | |
| 112 | gpointer systray_icon; |
| 113 | GtkWidget *icon_menu; |
Stepan Salenikovich | 472c905 | 2016-07-20 19:16:02 -0400 | [diff] [blame] | 114 | |
| 115 | #if USE_LIBNM |
| 116 | /* NetworkManager */ |
| 117 | NMClient *nm_client; |
Stepan Salenikovich | 221beae | 2016-10-12 11:29:56 -0400 | [diff] [blame] | 118 | NMActiveConnection *primary_connection; |
Stepan Salenikovich | 472c905 | 2016-07-20 19:16:02 -0400 | [diff] [blame] | 119 | #endif |
Stepan Salenikovich | 5a12767 | 2016-09-13 11:19:50 -0400 | [diff] [blame] | 120 | |
| 121 | /* notifications */ |
| 122 | QMetaObject::Connection call_notification; |
| 123 | QMetaObject::Connection chat_notification; |
Stepan Salenikovich | d81ef29 | 2015-02-17 18:47:37 -0500 | [diff] [blame] | 124 | }; |
| 125 | |
Stepan Salenikovich | 0d515e5 | 2015-05-19 16:31:05 -0400 | [diff] [blame] | 126 | /* this union is used to pass ints as pointers and vice versa for GAction parameters*/ |
| 127 | typedef union _int_ptr_t |
| 128 | { |
| 129 | int value; |
| 130 | gint64 value64; |
| 131 | gpointer ptr; |
| 132 | } int_ptr_t; |
| 133 | |
Stepan Salenikovich | 434b88f | 2015-02-19 17:49:08 -0500 | [diff] [blame] | 134 | G_DEFINE_TYPE_WITH_PRIVATE(RingClient, ring_client, GTK_TYPE_APPLICATION); |
Stepan Salenikovich | d81ef29 | 2015-02-17 18:47:37 -0500 | [diff] [blame] | 135 | |
| 136 | #define RING_CLIENT_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), RING_CLIENT_TYPE, RingClientPrivate)) |
| 137 | |
| 138 | static void |
Stepan Salenikovich | 541a40a | 2016-08-17 11:33:47 -0400 | [diff] [blame] | 139 | exception_dialog(const char* msg) |
Stepan Salenikovich | d81ef29 | 2015-02-17 18:47:37 -0500 | [diff] [blame] | 140 | { |
Stepan Salenikovich | 541a40a | 2016-08-17 11:33:47 -0400 | [diff] [blame] | 141 | g_critical("%s", msg); |
Stepan Salenikovich | d81ef29 | 2015-02-17 18:47:37 -0500 | [diff] [blame] | 142 | GtkWidget *dialog = gtk_message_dialog_new(NULL, |
| 143 | (GtkDialogFlags)(GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT), |
| 144 | GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE, |
| 145 | _("Unable to initialize.\nMake sure the Ring daemon (dring) is running.\nError: %s"), |
| 146 | msg); |
| 147 | |
| 148 | gtk_window_set_title(GTK_WINDOW(dialog), _("Ring Error")); |
| 149 | gtk_dialog_run(GTK_DIALOG(dialog)); |
| 150 | gtk_widget_destroy(dialog); |
| 151 | } |
| 152 | |
Stepan Salenikovich | 2d63d5e | 2015-03-22 23:23:54 -0400 | [diff] [blame] | 153 | static void |
| 154 | ring_accelerators(RingClient *client) |
| 155 | { |
Stepan Salenikovich | 39ee49a | 2015-03-24 12:44:55 -0400 | [diff] [blame] | 156 | #if GTK_CHECK_VERSION(3,12,0) |
Stepan Salenikovich | 2d63d5e | 2015-03-22 23:23:54 -0400 | [diff] [blame] | 157 | const gchar *quit_accels[2] = { "<Ctrl>Q", NULL }; |
| 158 | gtk_application_set_accels_for_action(GTK_APPLICATION(client), "app.quit", quit_accels); |
| 159 | #else |
Stepan Salenikovich | 870d739 | 2015-09-08 11:18:46 -0400 | [diff] [blame] | 160 | gtk_application_add_accelerator(GTK_APPLICATION(client), "<Control>Q", "app.quit", NULL); |
Stepan Salenikovich | 2d63d5e | 2015-03-22 23:23:54 -0400 | [diff] [blame] | 161 | #endif |
| 162 | } |
| 163 | |
| 164 | static void |
| 165 | action_quit(G_GNUC_UNUSED GSimpleAction *simple, |
| 166 | G_GNUC_UNUSED GVariant *parameter, |
| 167 | gpointer user_data) |
| 168 | { |
| 169 | g_return_if_fail(G_IS_APPLICATION(user_data)); |
| 170 | |
| 171 | #if GLIB_CHECK_VERSION(2,32,0) |
| 172 | g_application_quit(G_APPLICATION(user_data)); |
| 173 | #else |
| 174 | RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(user_data); |
| 175 | gtk_widget_destroy(priv->win); |
| 176 | #endif |
| 177 | } |
| 178 | |
Stepan Salenikovich | 24dcd5c | 2015-03-26 17:40:34 -0400 | [diff] [blame] | 179 | static void |
| 180 | action_about(G_GNUC_UNUSED GSimpleAction *simple, |
Stepan Salenikovich | 982b288 | 2016-06-15 13:13:37 -0400 | [diff] [blame] | 181 | G_GNUC_UNUSED GVariant *parameter, |
| 182 | gpointer user_data) |
Stepan Salenikovich | 24dcd5c | 2015-03-26 17:40:34 -0400 | [diff] [blame] | 183 | { |
| 184 | g_return_if_fail(G_IS_APPLICATION(user_data)); |
| 185 | RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(user_data); |
| 186 | |
| 187 | ring_about_dialog(priv->win); |
| 188 | } |
| 189 | |
Olivier Gregoire | 66e4df7 | 2016-06-17 18:39:05 -0400 | [diff] [blame] | 190 | static void |
| 191 | toggle_smartinfo(GSimpleAction *action, GVariant *parameter, gpointer) |
| 192 | { |
| 193 | g_simple_action_set_state(action, parameter); |
| 194 | if (g_variant_get_boolean(parameter)) { |
| 195 | SmartInfoHub::instance().start(); |
| 196 | } else { |
| 197 | SmartInfoHub::instance().stop(); |
| 198 | } |
| 199 | } |
| 200 | |
Stepan Salenikovich | 6977184 | 2015-02-24 18:11:45 -0500 | [diff] [blame] | 201 | static const GActionEntry ring_actions[] = |
| 202 | { |
Olivier Gregoire | 66e4df7 | 2016-06-17 18:39:05 -0400 | [diff] [blame] | 203 | { "accept", NULL, NULL, NULL, NULL, {0} }, |
| 204 | { "hangup", NULL, NULL, NULL, NULL, {0} }, |
| 205 | { "hold", NULL, NULL, "false", NULL, {0} }, |
| 206 | { "quit", action_quit, NULL, NULL, NULL, {0} }, |
| 207 | { "about", action_about, NULL, NULL, NULL, {0} }, |
| 208 | { "mute_audio", NULL, NULL, "false", NULL, {0} }, |
| 209 | { "mute_video", NULL, NULL, "false", NULL, {0} }, |
| 210 | { "record", NULL, NULL, "false", NULL, {0} }, |
| 211 | { "display-smartinfo", NULL, NULL, "false", toggle_smartinfo, {0} }, |
Stepan Salenikovich | c64523b | 2015-02-27 16:31:00 -0500 | [diff] [blame] | 212 | /* TODO implement the other actions */ |
Stepan Salenikovich | c64523b | 2015-02-27 16:31:00 -0500 | [diff] [blame] | 213 | // { "transfer", NULL, NULL, "flase", NULL, {0} }, |
Stepan Salenikovich | 6977184 | 2015-02-24 18:11:45 -0500 | [diff] [blame] | 214 | }; |
| 215 | |
Stepan Salenikovich | c64523b | 2015-02-27 16:31:00 -0500 | [diff] [blame] | 216 | static void |
| 217 | activate_action(GSimpleAction *action, G_GNUC_UNUSED GVariant *parameter, gpointer user_data) |
| 218 | { |
| 219 | g_debug("activating action: %s", g_action_get_name(G_ACTION(action))); |
| 220 | |
| 221 | int_ptr_t key; |
Stepan Salenikovich | 0d515e5 | 2015-05-19 16:31:05 -0400 | [diff] [blame] | 222 | |
Stepan Salenikovich | c64523b | 2015-02-27 16:31:00 -0500 | [diff] [blame] | 223 | key.ptr = user_data; |
| 224 | UserActionModel::Action a = static_cast<UserActionModel::Action>(key.value); |
Guillaume Roguez | 5d1514b | 2015-10-22 15:55:31 -0400 | [diff] [blame] | 225 | UserActionModel* uam = CallModel::instance().userActionModel(); |
Stepan Salenikovich | c64523b | 2015-02-27 16:31:00 -0500 | [diff] [blame] | 226 | |
| 227 | uam << a; |
| 228 | } |
Stepan Salenikovich | c64523b | 2015-02-27 16:31:00 -0500 | [diff] [blame] | 229 | |
Stepan Salenikovich | 0d515e5 | 2015-05-19 16:31:05 -0400 | [diff] [blame] | 230 | static void |
Stepan Salenikovich | fb7f295 | 2015-05-25 16:44:19 -0400 | [diff] [blame] | 231 | autostart_toggled(GSettings *settings, G_GNUC_UNUSED gchar *key, G_GNUC_UNUSED gpointer user_data) |
| 232 | { |
| 233 | autostart_symlink(g_settings_get_boolean(settings, "start-on-login")); |
| 234 | } |
| 235 | |
Stepan Salenikovich | 982b288 | 2016-06-15 13:13:37 -0400 | [diff] [blame] | 236 | |
| 237 | static void |
| 238 | show_main_window_toggled(RingClient *client) |
| 239 | { |
| 240 | RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client); |
| 241 | |
| 242 | if (g_settings_get_boolean(priv->settings, "show-main-window")) { |
| 243 | gtk_window_present(GTK_WINDOW(priv->win)); |
| 244 | } else { |
| 245 | gtk_widget_hide(priv->win); |
| 246 | } |
| 247 | } |
| 248 | |
Stepan Salenikovich | bb9c24e | 2015-09-15 09:55:02 -0400 | [diff] [blame] | 249 | static void |
| 250 | ring_window_show(RingClient *client) |
| 251 | { |
Stepan Salenikovich | bb9c24e | 2015-09-15 09:55:02 -0400 | [diff] [blame] | 252 | RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client); |
Stepan Salenikovich | 982b288 | 2016-06-15 13:13:37 -0400 | [diff] [blame] | 253 | g_settings_set_boolean(priv->settings, "show-main-window", TRUE); |
Stepan Salenikovich | bb9c24e | 2015-09-15 09:55:02 -0400 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | static void |
| 257 | ring_window_hide(RingClient *client) |
| 258 | { |
Stepan Salenikovich | bb9c24e | 2015-09-15 09:55:02 -0400 | [diff] [blame] | 259 | RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client); |
Stepan Salenikovich | 982b288 | 2016-06-15 13:13:37 -0400 | [diff] [blame] | 260 | g_settings_set_boolean(priv->settings, "show-main-window", FALSE); |
Stepan Salenikovich | bb9c24e | 2015-09-15 09:55:02 -0400 | [diff] [blame] | 261 | } |
| 262 | |
Stepan Salenikovich | bc6c4be | 2015-07-31 16:07:54 -0400 | [diff] [blame] | 263 | static gboolean |
| 264 | on_close_window(GtkWidget *window, G_GNUC_UNUSED GdkEvent *event, RingClient *client) |
| 265 | { |
| 266 | 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] | 267 | RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client); |
| 268 | |
Stepan Salenikovich | 982b288 | 2016-06-15 13:13:37 -0400 | [diff] [blame] | 269 | if (g_settings_get_boolean(priv->settings, "show-status-icon")) { |
Stepan Salenikovich | bc6c4be | 2015-07-31 16:07:54 -0400 | [diff] [blame] | 270 | /* we want to simply hide the window and keep the client running */ |
Stepan Salenikovich | bb9c24e | 2015-09-15 09:55:02 -0400 | [diff] [blame] | 271 | ring_window_hide(client); |
Stepan Salenikovich | bc6c4be | 2015-07-31 16:07:54 -0400 | [diff] [blame] | 272 | return TRUE; /* do not propogate event */ |
| 273 | } else { |
| 274 | /* we want to quit the application, so just propogate the event */ |
| 275 | return FALSE; |
| 276 | } |
| 277 | } |
| 278 | |
Stepan Salenikovich | 982b288 | 2016-06-15 13:13:37 -0400 | [diff] [blame] | 279 | static void |
| 280 | popup_menu(GtkStatusIcon *self, |
| 281 | guint button, |
| 282 | guint when, |
| 283 | RingClient *client) |
| 284 | { |
| 285 | RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client); |
| 286 | G_GNUC_BEGIN_IGNORE_DEPRECATIONS // GtkStatusIcon is deprecated since 3.14, but we fallback on it |
| 287 | gtk_menu_popup(GTK_MENU(priv->icon_menu), NULL, NULL, gtk_status_icon_position_menu, self, button, when); |
| 288 | G_GNUC_END_IGNORE_DEPRECATIONS |
| 289 | } |
Stepan Salenikovich | 982b288 | 2016-06-15 13:13:37 -0400 | [diff] [blame] | 290 | |
| 291 | static void |
| 292 | init_systray(RingClient *client) |
| 293 | { |
| 294 | RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client); |
| 295 | |
| 296 | // init menu |
| 297 | if (!priv->icon_menu) { |
Stepan Salenikovich | 982b288 | 2016-06-15 13:13:37 -0400 | [diff] [blame] | 298 | |
Stepan Salenikovich | 9717736 | 2016-06-22 18:38:24 -0400 | [diff] [blame] | 299 | /* for some reason AppIndicator doesn't like the menu being built from a GMenuModel and/or |
| 300 | * the GMenuModel being built from an xml resource. So we build the menu in code. |
| 301 | */ |
| 302 | priv->icon_menu = gtk_menu_new(); |
Stepan Salenikovich | 982b288 | 2016-06-15 13:13:37 -0400 | [diff] [blame] | 303 | g_object_ref_sink(priv->icon_menu); |
Stepan Salenikovich | 9717736 | 2016-06-22 18:38:24 -0400 | [diff] [blame] | 304 | |
| 305 | auto item = gtk_check_menu_item_new_with_label(C_("In the status icon menu, toggle action to show or hide the Ring main window", "Show Ring")); |
| 306 | gtk_actionable_set_action_name(GTK_ACTIONABLE(item), "app.show-main-window"); |
| 307 | gtk_menu_shell_append(GTK_MENU_SHELL(priv->icon_menu), item); |
| 308 | |
| 309 | item = gtk_menu_item_new_with_label(_("Quit")); |
| 310 | gtk_actionable_set_action_name(GTK_ACTIONABLE(item), "app.quit"); |
| 311 | gtk_menu_shell_append(GTK_MENU_SHELL(priv->icon_menu), item); |
| 312 | |
| 313 | gtk_widget_insert_action_group(priv->icon_menu, "app", G_ACTION_GROUP(client)); |
| 314 | gtk_widget_show_all(priv->icon_menu); |
Stepan Salenikovich | 982b288 | 2016-06-15 13:13:37 -0400 | [diff] [blame] | 315 | } |
| 316 | |
Stepan Salenikovich | 72d812f | 2017-01-05 12:16:25 -0500 | [diff] [blame] | 317 | gboolean use_appinidcator = FALSE; |
Stepan Salenikovich | 982b288 | 2016-06-15 13:13:37 -0400 | [diff] [blame] | 318 | |
Stepan Salenikovich | 72d812f | 2017-01-05 12:16:25 -0500 | [diff] [blame] | 319 | #if HAVE_APPINDICATOR |
| 320 | /* only use AppIndicator in Unity (Tuleap: #1440) */ |
| 321 | const auto desktop = g_getenv("XDG_CURRENT_DESKTOP"); |
Marco Martin | 7f2b5ab | 2017-03-23 12:13:02 +0100 | [diff] [blame] | 322 | if (g_strcmp0("Unity", desktop) == 0 || g_strcmp0("KDE", desktop) == 0) { |
Stepan Salenikovich | 72d812f | 2017-01-05 12:16:25 -0500 | [diff] [blame] | 323 | use_appinidcator = TRUE; |
Stepan Salenikovich | 982b288 | 2016-06-15 13:13:37 -0400 | [diff] [blame] | 324 | |
Stepan Salenikovich | 72d812f | 2017-01-05 12:16:25 -0500 | [diff] [blame] | 325 | auto indicator = app_indicator_new("ring", "ring", APP_INDICATOR_CATEGORY_COMMUNICATIONS); |
| 326 | app_indicator_set_status(indicator, APP_INDICATOR_STATUS_ACTIVE); |
| 327 | app_indicator_set_title(indicator, "ring"); |
| 328 | /* app indicator requires a menu */ |
| 329 | app_indicator_set_menu(indicator, GTK_MENU(priv->icon_menu)); |
| 330 | priv->systray_icon = indicator; |
| 331 | } |
Stepan Salenikovich | 982b288 | 2016-06-15 13:13:37 -0400 | [diff] [blame] | 332 | #endif |
Stepan Salenikovich | 72d812f | 2017-01-05 12:16:25 -0500 | [diff] [blame] | 333 | |
| 334 | if (!use_appinidcator) { |
| 335 | G_GNUC_BEGIN_IGNORE_DEPRECATIONS // GtkStatusIcon is deprecated since 3.14, but we fallback on it |
| 336 | auto status_icon = gtk_status_icon_new_from_icon_name("ring"); |
| 337 | gtk_status_icon_set_title(status_icon, "ring"); |
| 338 | G_GNUC_END_IGNORE_DEPRECATIONS |
| 339 | g_signal_connect_swapped(status_icon, "activate", G_CALLBACK(ring_window_show), client); |
| 340 | g_signal_connect(status_icon, "popup-menu", G_CALLBACK(popup_menu), client); |
| 341 | |
| 342 | priv->systray_icon = status_icon; |
| 343 | } |
Stepan Salenikovich | 982b288 | 2016-06-15 13:13:37 -0400 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | static void |
| 347 | systray_toggled(GSettings *settings, const gchar *key, RingClient *client) |
| 348 | { |
| 349 | RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client); |
| 350 | |
| 351 | if (g_settings_get_boolean(settings, key)) { |
| 352 | if (!priv->systray_icon) |
| 353 | init_systray(client); |
| 354 | } else { |
| 355 | if (priv->systray_icon) |
| 356 | g_clear_object(&priv->systray_icon); |
| 357 | } |
| 358 | } |
| 359 | |
Stepan Salenikovich | bc6c4be | 2015-07-31 16:07:54 -0400 | [diff] [blame] | 360 | static void |
| 361 | ring_client_activate(GApplication *app) |
| 362 | { |
| 363 | RingClient *client = RING_CLIENT(app); |
| 364 | RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client); |
| 365 | |
| 366 | if (priv->win == NULL) { |
Stepan Salenikovich | 982b288 | 2016-06-15 13:13:37 -0400 | [diff] [blame] | 367 | // activate being called for the first time |
Stepan Salenikovich | bc6c4be | 2015-07-31 16:07:54 -0400 | [diff] [blame] | 368 | priv->win = ring_main_window_new(GTK_APPLICATION(app)); |
| 369 | |
| 370 | /* make sure win is set to NULL when the window is destroyed */ |
| 371 | g_object_add_weak_pointer(G_OBJECT(priv->win), (gpointer *)&priv->win); |
| 372 | |
| 373 | /* check if the window should be destoryed or not on close */ |
| 374 | 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] | 375 | |
Stepan Salenikovich | 982b288 | 2016-06-15 13:13:37 -0400 | [diff] [blame] | 376 | /* if we didn't launch with the '-r' (--restore-last-window-state) option then force the |
| 377 | * show-main-window to true */ |
| 378 | if (!priv->restore_window_state) |
| 379 | ring_window_show(client); |
Stepan Salenikovich | 3f27dc9 | 2016-06-22 18:08:01 -0400 | [diff] [blame] | 380 | show_main_window_toggled(client); |
| 381 | g_signal_connect_swapped(priv->settings, "changed::show-main-window", G_CALLBACK(show_main_window_toggled), client); |
Stepan Salenikovich | bc6c4be | 2015-07-31 16:07:54 -0400 | [diff] [blame] | 382 | |
Stepan Salenikovich | 982b288 | 2016-06-15 13:13:37 -0400 | [diff] [blame] | 383 | // track sys icon state |
| 384 | g_signal_connect(priv->settings, "changed::show-status-icon", G_CALLBACK(systray_toggled), client); |
| 385 | systray_toggled(priv->settings, "show-status-icon", client); |
| 386 | } else { |
| 387 | // activate not being called for the first time, force showing of main window |
Stepan Salenikovich | bb9c24e | 2015-09-15 09:55:02 -0400 | [diff] [blame] | 388 | ring_window_show(client); |
Stepan Salenikovich | 982b288 | 2016-06-15 13:13:37 -0400 | [diff] [blame] | 389 | } |
Stepan Salenikovich | bc6c4be | 2015-07-31 16:07:54 -0400 | [diff] [blame] | 390 | } |
| 391 | |
Victor Nikulshin | 167bbb6 | 2017-03-15 20:44:27 +0000 | [diff] [blame] | 392 | static void |
| 393 | ring_client_open(GApplication *app, GFile **file, gint /*arg3*/, const gchar* /*arg4*/) |
| 394 | { |
| 395 | ring_client_activate(app); |
| 396 | |
| 397 | if (strcmp(g_file_get_uri_scheme(*file), "ring") == 0) { |
| 398 | const char * call_id = g_file_get_basename(*file); |
| 399 | std::regex format {"^[[:xdigit:]]{40}$"}; |
| 400 | |
| 401 | if (std::regex_match(call_id, format)) { |
| 402 | auto cm = std::unique_ptr<TemporaryContactMethod>(new TemporaryContactMethod); |
| 403 | cm->setUri(URI(QString::fromStdString(call_id))); |
| 404 | |
| 405 | place_new_call(cm.get()); |
| 406 | cm.release(); |
| 407 | } |
| 408 | } |
| 409 | } |
| 410 | |
Stepan Salenikovich | 472c905 | 2016-07-20 19:16:02 -0400 | [diff] [blame] | 411 | #if USE_LIBNM |
| 412 | |
| 413 | static void |
| 414 | log_connection_info(NMActiveConnection *connection) |
| 415 | { |
| 416 | if (connection) { |
Stepan Salenikovich | 02b283d | 2016-07-27 18:09:21 -0400 | [diff] [blame] | 417 | g_debug("primary network connection: %s, default: %s", |
Stepan Salenikovich | 472c905 | 2016-07-20 19:16:02 -0400 | [diff] [blame] | 418 | nm_active_connection_get_uuid(connection), |
Stepan Salenikovich | 472c905 | 2016-07-20 19:16:02 -0400 | [diff] [blame] | 419 | nm_active_connection_get_default(connection) ? "yes" : "no"); |
| 420 | } else { |
| 421 | g_warning("no primary network connection detected, check network settings"); |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | static void |
Stepan Salenikovich | 221beae | 2016-10-12 11:29:56 -0400 | [diff] [blame] | 426 | primary_connection_changed(NMClient *nm, GParamSpec*, RingClient *self) |
Stepan Salenikovich | 472c905 | 2016-07-20 19:16:02 -0400 | [diff] [blame] | 427 | { |
Stepan Salenikovich | 221beae | 2016-10-12 11:29:56 -0400 | [diff] [blame] | 428 | auto priv = RING_CLIENT_GET_PRIVATE(self); |
Stepan Salenikovich | 472c905 | 2016-07-20 19:16:02 -0400 | [diff] [blame] | 429 | auto connection = nm_client_get_primary_connection(nm); |
Stepan Salenikovich | 472c905 | 2016-07-20 19:16:02 -0400 | [diff] [blame] | 430 | |
Stepan Salenikovich | 221beae | 2016-10-12 11:29:56 -0400 | [diff] [blame] | 431 | if (priv->primary_connection != connection) { |
| 432 | /* make sure the connection really changed |
| 433 | * on client start it seems to always emit the notify::primary-connection signal though it |
| 434 | * hasn't changed */ |
| 435 | log_connection_info(connection); |
| 436 | priv->primary_connection = connection; |
| 437 | AccountModel::instance().slotConnectivityChanged(); |
| 438 | } |
Stepan Salenikovich | 472c905 | 2016-07-20 19:16:02 -0400 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | static void |
| 442 | nm_client_cb(G_GNUC_UNUSED GObject *source_object, GAsyncResult *result, RingClient *self) |
| 443 | { |
| 444 | RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(self); |
| 445 | |
| 446 | GError* error = nullptr; |
| 447 | if (auto nm_client = nm_client_new_finish(result, &error)) { |
| 448 | priv->nm_client = nm_client; |
Stepan Salenikovich | 28ca317 | 2016-07-22 17:29:11 -0400 | [diff] [blame] | 449 | g_debug("NetworkManager client initialized, version: %s\ndaemon running: %s\nnnetworking enabled: %s", |
Stepan Salenikovich | 472c905 | 2016-07-20 19:16:02 -0400 | [diff] [blame] | 450 | nm_client_get_version(nm_client), |
Stepan Salenikovich | 28ca317 | 2016-07-22 17:29:11 -0400 | [diff] [blame] | 451 | nm_client_get_manager_running(nm_client) ? "yes" : "no", |
Stepan Salenikovich | 472c905 | 2016-07-20 19:16:02 -0400 | [diff] [blame] | 452 | nm_client_networking_get_enabled(nm_client) ? "yes" : "no"); |
| 453 | |
| 454 | auto connection = nm_client_get_primary_connection(nm_client); |
| 455 | log_connection_info(connection); |
Stepan Salenikovich | 221beae | 2016-10-12 11:29:56 -0400 | [diff] [blame] | 456 | priv->primary_connection = connection; |
Stepan Salenikovich | 472c905 | 2016-07-20 19:16:02 -0400 | [diff] [blame] | 457 | |
| 458 | /* We monitor the primary connection and notify the daemon to re-load its connections |
| 459 | * (accounts, UPnP, ...) when it changes. For example, on most systems, if we have an |
| 460 | * ethernet connection and then also connect to wifi, the primary connection will not change; |
| 461 | * however it will change in the opposite case because an ethernet connection is preferred. |
| 462 | */ |
Stepan Salenikovich | 221beae | 2016-10-12 11:29:56 -0400 | [diff] [blame] | 463 | g_signal_connect(nm_client, "notify::primary-connection", G_CALLBACK(primary_connection_changed), self); |
Stepan Salenikovich | 472c905 | 2016-07-20 19:16:02 -0400 | [diff] [blame] | 464 | |
| 465 | } else { |
| 466 | g_warning("error initializing NetworkManager client: %s", error->message); |
| 467 | g_clear_error(&error); |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | #endif /* USE_LIBNM */ |
| 472 | |
Stepan Salenikovich | fb7f295 | 2015-05-25 16:44:19 -0400 | [diff] [blame] | 473 | static void |
Stepan Salenikovich | 5a12767 | 2016-09-13 11:19:50 -0400 | [diff] [blame] | 474 | call_notifications_toggled(RingClient *self) |
| 475 | { |
| 476 | auto priv = RING_CLIENT_GET_PRIVATE(self); |
| 477 | |
| 478 | if (g_settings_get_boolean(priv->settings, "enable-call-notifications")) { |
| 479 | priv->call_notification = QObject::connect( |
| 480 | &CallModel::instance(), |
| 481 | &CallModel::incomingCall, |
| 482 | [] (Call *call) { ring_notify_incoming_call(call); } |
| 483 | ); |
| 484 | } else { |
| 485 | QObject::disconnect(priv->call_notification); |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | static void |
| 490 | chat_notifications_toggled(RingClient *self) |
| 491 | { |
| 492 | auto priv = RING_CLIENT_GET_PRIVATE(self); |
| 493 | |
| 494 | if (g_settings_get_boolean(priv->settings, "enable-chat-notifications")) { |
| 495 | priv->chat_notification = QObject::connect( |
| 496 | &Media::RecordingModel::instance(), |
| 497 | &Media::RecordingModel::newTextMessage, |
| 498 | [self] (Media::TextRecording* t, ContactMethod* cm) |
| 499 | { ring_notify_message(cm, t, self); } |
| 500 | ); |
| 501 | } else { |
| 502 | QObject::disconnect(priv->chat_notification); |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | static void |
Stepan Salenikovich | 0d515e5 | 2015-05-19 16:31:05 -0400 | [diff] [blame] | 507 | ring_client_startup(GApplication *app) |
Stepan Salenikovich | 6977184 | 2015-02-24 18:11:45 -0500 | [diff] [blame] | 508 | { |
Stepan Salenikovich | 6977184 | 2015-02-24 18:11:45 -0500 | [diff] [blame] | 509 | RingClient *client = RING_CLIENT(app); |
Stepan Salenikovich | 068fb69 | 2015-03-23 14:58:32 -0400 | [diff] [blame] | 510 | RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client); |
Stepan Salenikovich | 6977184 | 2015-02-24 18:11:45 -0500 | [diff] [blame] | 511 | |
Stepan Salenikovich | 4f9bb98 | 2015-06-23 14:26:30 -0400 | [diff] [blame] | 512 | g_message("Ring GNOME client version: %d.%d.%d", VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH); |
| 513 | g_message("git ref: %s", RING_CLIENT_REVISION); |
| 514 | |
Stepan Salenikovich | fb7f295 | 2015-05-25 16:44:19 -0400 | [diff] [blame] | 515 | /* make sure that the system corresponds to the autostart setting */ |
| 516 | autostart_symlink(g_settings_get_boolean(priv->settings, "start-on-login")); |
| 517 | g_signal_connect(priv->settings, "changed::start-on-login", G_CALLBACK(autostart_toggled), NULL); |
| 518 | |
Stepan Salenikovich | 068fb69 | 2015-03-23 14:58:32 -0400 | [diff] [blame] | 519 | /* init clutter */ |
| 520 | int clutter_error; |
Stepan Salenikovich | 0d515e5 | 2015-05-19 16:31:05 -0400 | [diff] [blame] | 521 | if ((clutter_error = gtk_clutter_init(&priv->argc, &priv->argv)) != CLUTTER_INIT_SUCCESS) { |
| 522 | g_error("Could not init clutter : %d\n", clutter_error); |
| 523 | 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] | 524 | } |
| 525 | |
| 526 | /* init libRingClient and make sure its connected to the dbus */ |
| 527 | try { |
Stepan Salenikovich | 0d515e5 | 2015-05-19 16:31:05 -0400 | [diff] [blame] | 528 | priv->qtapp = new QCoreApplication(priv->argc, priv->argv); |
Stepan Salenikovich | 068fb69 | 2015-03-23 14:58:32 -0400 | [diff] [blame] | 529 | /* the call model will try to connect to dring via dbus */ |
| 530 | CallModel::instance(); |
Stepan Salenikovich | 541a40a | 2016-08-17 11:33:47 -0400 | [diff] [blame] | 531 | } catch(const char * msg) { |
| 532 | exception_dialog(msg); |
| 533 | exit(1); |
Stepan Salenikovich | 068fb69 | 2015-03-23 14:58:32 -0400 | [diff] [blame] | 534 | } catch(QString& msg) { |
Stepan Salenikovich | 541a40a | 2016-08-17 11:33:47 -0400 | [diff] [blame] | 535 | exception_dialog(msg.toLocal8Bit().constData()); |
| 536 | exit(1); |
Stepan Salenikovich | 068fb69 | 2015-03-23 14:58:32 -0400 | [diff] [blame] | 537 | } |
| 538 | |
Stepan Salenikovich | 67c5dd3 | 2015-09-14 12:27:36 -0400 | [diff] [blame] | 539 | /* load translations from LRC */ |
Stepan Salenikovich | 6c39b10 | 2015-09-15 14:24:45 -0400 | [diff] [blame] | 540 | priv->translator.reset(new QTranslator); |
| 541 | if (priv->translator->load(QLocale::system(), "lrc", "_", RING_CLIENT_INSTALL "/share/libringclient/translations")) { |
| 542 | priv->qtapp->installTranslator(priv->translator.get()); |
Stepan Salenikovich | 67c5dd3 | 2015-09-14 12:27:36 -0400 | [diff] [blame] | 543 | } else { |
| 544 | g_debug("could not load LRC translations for %s, %s", |
| 545 | QLocale::languageToString(QLocale::system().language()).toUtf8().constData(), |
| 546 | QLocale::countryToString(QLocale::system().country()).toUtf8().constData() |
| 547 | ); |
| 548 | } |
| 549 | |
Stepan Salenikovich | 6f68707 | 2015-03-26 10:43:37 -0400 | [diff] [blame] | 550 | /* init delegates */ |
Stepan Salenikovich | bbd6c13 | 2015-08-20 15:21:48 -0400 | [diff] [blame] | 551 | GlobalInstances::setPixmapManipulator(std::unique_ptr<Interfaces::PixbufManipulator>(new Interfaces::PixbufManipulator())); |
Stepan Salenikovich | be6550c | 2015-08-21 16:16:03 -0400 | [diff] [blame] | 552 | GlobalInstances::setDBusErrorHandler(std::unique_ptr<Interfaces::DBusErrorHandler>(new Interfaces::DBusErrorHandler())); |
Stepan Salenikovich | 6f68707 | 2015-03-26 10:43:37 -0400 | [diff] [blame] | 553 | |
Stepan Salenikovich | 75a3917 | 2015-07-10 13:21:08 -0400 | [diff] [blame] | 554 | /* make sure all RING accounts have a display name... this basically makes sure |
| 555 | * that all accounts created before the display name patch have a display name |
| 556 | * set... a bit of a hack as this should maybe be done in LRC */ |
| 557 | force_ring_display_name(); |
| 558 | |
Stepan Salenikovich | f2d76c5 | 2015-07-17 17:54:56 -0400 | [diff] [blame] | 559 | /* make sure basic number categories exist, in case user has no contacts |
| 560 | * from which these would be automatically created |
| 561 | */ |
Guillaume Roguez | 5d1514b | 2015-10-22 15:55:31 -0400 | [diff] [blame] | 562 | NumberCategoryModel::instance().addCategory("work", QVariant()); |
| 563 | NumberCategoryModel::instance().addCategory("home", QVariant()); |
Stepan Salenikovich | f2d76c5 | 2015-07-17 17:54:56 -0400 | [diff] [blame] | 564 | |
Stepan Salenikovich | 068fb69 | 2015-03-23 14:58:32 -0400 | [diff] [blame] | 565 | /* add backends */ |
Guillaume Roguez | 5d1514b | 2015-10-22 15:55:31 -0400 | [diff] [blame] | 566 | CategorizedHistoryModel::instance().addCollection<LocalHistoryCollection>(LoadOptions::FORCE_ENABLED); |
Nicolas Jager | b413b30 | 2016-05-06 11:41:32 -0400 | [diff] [blame] | 567 | PersonModel::instance().addCollection<PeerProfileCollection>(LoadOptions::FORCE_ENABLED); |
| 568 | ProfileModel::instance().addCollection<LocalProfileCollection>(LoadOptions::FORCE_ENABLED); |
Stepan Salenikovich | 068fb69 | 2015-03-23 14:58:32 -0400 | [diff] [blame] | 569 | |
Stepan Salenikovich | f2d76c5 | 2015-07-17 17:54:56 -0400 | [diff] [blame] | 570 | /* fallback backend for vcards */ |
Guillaume Roguez | 5d1514b | 2015-10-22 15:55:31 -0400 | [diff] [blame] | 571 | PersonModel::instance().addCollection<FallbackPersonCollection>(LoadOptions::FORCE_ENABLED); |
Stepan Salenikovich | 6f68707 | 2015-03-26 10:43:37 -0400 | [diff] [blame] | 572 | |
Stepan Salenikovich | f2d76c5 | 2015-07-17 17:54:56 -0400 | [diff] [blame] | 573 | /* EDS backend(s) */ |
Stepan Salenikovich | 6f68707 | 2015-03-26 10:43:37 -0400 | [diff] [blame] | 574 | load_eds_sources(priv->cancellable); |
| 575 | |
Stepan Salenikovich | 068fb69 | 2015-03-23 14:58:32 -0400 | [diff] [blame] | 576 | /* Override theme since we don't have appropriate icons for a dark them (yet) */ |
| 577 | GtkSettings *gtk_settings = gtk_settings_get_default(); |
| 578 | g_object_set(G_OBJECT(gtk_settings), "gtk-application-prefer-dark-theme", |
| 579 | FALSE, NULL); |
| 580 | /* enable button icons */ |
| 581 | g_object_set(G_OBJECT(gtk_settings), "gtk-button-images", |
| 582 | TRUE, NULL); |
| 583 | |
| 584 | /* add GActions */ |
Stepan Salenikovich | 6977184 | 2015-02-24 18:11:45 -0500 | [diff] [blame] | 585 | g_action_map_add_action_entries( |
Stepan Salenikovich | 2d63d5e | 2015-03-22 23:23:54 -0400 | [diff] [blame] | 586 | G_ACTION_MAP(app), ring_actions, G_N_ELEMENTS(ring_actions), app); |
| 587 | |
Stepan Salenikovich | 982b288 | 2016-06-15 13:13:37 -0400 | [diff] [blame] | 588 | /* GActions for settings */ |
| 589 | auto action_window_visible = g_settings_create_action(priv->settings, "show-main-window"); |
| 590 | g_action_map_add_action(G_ACTION_MAP(app), action_window_visible); |
| 591 | |
Stepan Salenikovich | 2d63d5e | 2015-03-22 23:23:54 -0400 | [diff] [blame] | 592 | /* add accelerators */ |
| 593 | ring_accelerators(RING_CLIENT(app)); |
Stepan Salenikovich | c64523b | 2015-02-27 16:31:00 -0500 | [diff] [blame] | 594 | |
Stepan Salenikovich | 347b73a | 2015-03-22 10:39:00 -0400 | [diff] [blame] | 595 | /* Bind GActions to the UserActionModel */ |
Guillaume Roguez | 5d1514b | 2015-10-22 15:55:31 -0400 | [diff] [blame] | 596 | UserActionModel* uam = CallModel::instance().userActionModel(); |
Stepan Salenikovich | 347b73a | 2015-03-22 10:39:00 -0400 | [diff] [blame] | 597 | QHash<int, GSimpleAction*> actionHash; |
| 598 | actionHash[ (int)UserActionModel::Action::ACCEPT ] = G_SIMPLE_ACTION(g_action_map_lookup_action(G_ACTION_MAP(app), "accept")); |
| 599 | 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] | 600 | actionHash[ (int)UserActionModel::Action::MUTE_AUDIO ] = G_SIMPLE_ACTION(g_action_map_lookup_action(G_ACTION_MAP(app), "mute_audio")); |
| 601 | 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] | 602 | /* TODO: add commented actions when ready */ |
Stepan Salenikovich | 347b73a | 2015-03-22 10:39:00 -0400 | [diff] [blame] | 603 | // 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] | 604 | 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] | 605 | 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] | 606 | |
Stepan Salenikovich | 347b73a | 2015-03-22 10:39:00 -0400 | [diff] [blame] | 607 | for (QHash<int,GSimpleAction*>::const_iterator i = actionHash.begin(); i != actionHash.end(); ++i) { |
Stepan Salenikovich | 5fd97bc | 2016-08-30 09:40:38 -0400 | [diff] [blame] | 608 | GSimpleAction* sa = i.value(); |
| 609 | int_ptr_t user_data; |
| 610 | user_data.value = i.key(); |
| 611 | g_signal_connect(G_OBJECT(sa), "activate", G_CALLBACK(activate_action), user_data.ptr); |
Stepan Salenikovich | 347b73a | 2015-03-22 10:39:00 -0400 | [diff] [blame] | 612 | } |
Stepan Salenikovich | c64523b | 2015-02-27 16:31:00 -0500 | [diff] [blame] | 613 | |
Stepan Salenikovich | 347b73a | 2015-03-22 10:39:00 -0400 | [diff] [blame] | 614 | /* change the state of the GActions based on the UserActionModel */ |
Stepan Salenikovich | 068fb69 | 2015-03-23 14:58:32 -0400 | [diff] [blame] | 615 | priv->uam_updated = QObject::connect(uam,&UserActionModel::dataChanged, [actionHash,uam](const QModelIndex& tl, const QModelIndex& br) { |
Stepan Salenikovich | 5fd97bc | 2016-08-30 09:40:38 -0400 | [diff] [blame] | 616 | const int first(tl.row()),last(br.row()); |
| 617 | for(int i = first; i <= last;i++) { |
| 618 | const QModelIndex& idx = uam->index(i,0); |
| 619 | GSimpleAction* sa = actionHash[(int)qvariant_cast<UserActionModel::Action>(idx.data(UserActionModel::Role::ACTION))]; |
| 620 | if (sa) { |
| 621 | /* enable/disable GAction based on UserActionModel */ |
| 622 | g_simple_action_set_enabled(sa, idx.flags() & Qt::ItemIsEnabled); |
| 623 | /* set the state of the action if its stateful */ |
| 624 | if (g_action_get_state_type(G_ACTION(sa)) != NULL) |
| 625 | g_simple_action_set_state(sa, g_variant_new_boolean(idx.data(Qt::CheckStateRole) == Qt::Checked)); |
| 626 | } |
| 627 | } |
Stepan Salenikovich | 347b73a | 2015-03-22 10:39:00 -0400 | [diff] [blame] | 628 | }); |
Stepan Salenikovich | 068fb69 | 2015-03-23 14:58:32 -0400 | [diff] [blame] | 629 | |
Stepan Salenikovich | bc6c4be | 2015-07-31 16:07:54 -0400 | [diff] [blame] | 630 | /* show window on incoming calls (if the option is set)*/ |
Guillaume Roguez | 5d1514b | 2015-10-22 15:55:31 -0400 | [diff] [blame] | 631 | QObject::connect(&CallModel::instance(), &CallModel::incomingCall, |
Stepan Salenikovich | bc6c4be | 2015-07-31 16:07:54 -0400 | [diff] [blame] | 632 | [app] (G_GNUC_UNUSED Call *call) { |
| 633 | RingClient *client = RING_CLIENT(app); |
| 634 | RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client); |
| 635 | if (g_settings_get_boolean(priv->settings, "bring-window-to-front")) |
Stepan Salenikovich | bb9c24e | 2015-09-15 09:55:02 -0400 | [diff] [blame] | 636 | ring_window_show(client); |
Stepan Salenikovich | bc6c4be | 2015-07-31 16:07:54 -0400 | [diff] [blame] | 637 | } |
| 638 | ); |
| 639 | |
Stepan Salenikovich | 5a12767 | 2016-09-13 11:19:50 -0400 | [diff] [blame] | 640 | /* enable notifications based on settings */ |
Stepan Salenikovich | 76c33e6 | 2015-05-22 12:24:07 -0400 | [diff] [blame] | 641 | ring_notify_init(); |
Stepan Salenikovich | 5a12767 | 2016-09-13 11:19:50 -0400 | [diff] [blame] | 642 | call_notifications_toggled(client); |
| 643 | chat_notifications_toggled(client); |
| 644 | g_signal_connect_swapped(priv->settings, "changed::enable-call-notifications", G_CALLBACK(call_notifications_toggled), client); |
| 645 | g_signal_connect_swapped(priv->settings, "changed::enable-chat-notifications", G_CALLBACK(chat_notifications_toggled), client); |
Stepan Salenikovich | 67112d1 | 2015-06-16 16:57:06 -0400 | [diff] [blame] | 646 | |
Stepan Salenikovich | 472c905 | 2016-07-20 19:16:02 -0400 | [diff] [blame] | 647 | #if USE_LIBNM |
| 648 | /* monitor the network using libnm to notify the daemon about connectivity chagnes */ |
| 649 | nm_client_new_async(priv->cancellable, (GAsyncReadyCallback)nm_client_cb, client); |
| 650 | #endif |
| 651 | |
Stepan Salenikovich | 0d515e5 | 2015-05-19 16:31:05 -0400 | [diff] [blame] | 652 | G_APPLICATION_CLASS(ring_client_parent_class)->startup(app); |
Stepan Salenikovich | 068fb69 | 2015-03-23 14:58:32 -0400 | [diff] [blame] | 653 | } |
| 654 | |
Stepan Salenikovich | 0d515e5 | 2015-05-19 16:31:05 -0400 | [diff] [blame] | 655 | static void |
Stepan Salenikovich | d81ef29 | 2015-02-17 18:47:37 -0500 | [diff] [blame] | 656 | ring_client_shutdown(GApplication *app) |
| 657 | { |
| 658 | RingClient *self = RING_CLIENT(app); |
| 659 | RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(self); |
| 660 | |
Stepan Salenikovich | 2d63d5e | 2015-03-22 23:23:54 -0400 | [diff] [blame] | 661 | g_debug("quitting"); |
| 662 | |
Stepan Salenikovich | 6f68707 | 2015-03-26 10:43:37 -0400 | [diff] [blame] | 663 | /* cancel any pending cancellable operations */ |
| 664 | g_cancellable_cancel(priv->cancellable); |
| 665 | g_object_unref(priv->cancellable); |
| 666 | |
Stepan Salenikovich | 068fb69 | 2015-03-23 14:58:32 -0400 | [diff] [blame] | 667 | QObject::disconnect(priv->uam_updated); |
Stepan Salenikovich | 5a12767 | 2016-09-13 11:19:50 -0400 | [diff] [blame] | 668 | QObject::disconnect(priv->call_notification); |
| 669 | QObject::disconnect(priv->chat_notification); |
Stepan Salenikovich | 068fb69 | 2015-03-23 14:58:32 -0400 | [diff] [blame] | 670 | |
Stepan Salenikovich | d81ef29 | 2015-02-17 18:47:37 -0500 | [diff] [blame] | 671 | /* free the QCoreApplication, which will destroy all libRingClient models |
| 672 | * and thus send the Unregister signal over dbus to dring */ |
| 673 | delete priv->qtapp; |
| 674 | |
Stepan Salenikovich | 0d515e5 | 2015-05-19 16:31:05 -0400 | [diff] [blame] | 675 | /* free the copied cmd line args */ |
| 676 | g_strfreev(priv->argv); |
| 677 | |
Stepan Salenikovich | fb7f295 | 2015-05-25 16:44:19 -0400 | [diff] [blame] | 678 | g_clear_object(&priv->settings); |
| 679 | |
Stepan Salenikovich | 76c33e6 | 2015-05-22 12:24:07 -0400 | [diff] [blame] | 680 | ring_notify_uninit(); |
| 681 | |
Stepan Salenikovich | 472c905 | 2016-07-20 19:16:02 -0400 | [diff] [blame] | 682 | #if USE_LIBNM |
| 683 | /* clear NetworkManager client if it was used */ |
| 684 | g_clear_object(&priv->nm_client); |
| 685 | #endif |
| 686 | |
Stepan Salenikovich | d81ef29 | 2015-02-17 18:47:37 -0500 | [diff] [blame] | 687 | /* Chain up to the parent class */ |
| 688 | G_APPLICATION_CLASS(ring_client_parent_class)->shutdown(app); |
| 689 | } |
| 690 | |
| 691 | static void |
| 692 | ring_client_init(RingClient *self) |
| 693 | { |
| 694 | RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(self); |
| 695 | |
Stepan Salenikovich | d81ef29 | 2015-02-17 18:47:37 -0500 | [diff] [blame] | 696 | priv->win = NULL; |
| 697 | priv->qtapp = NULL; |
Stepan Salenikovich | 6f68707 | 2015-03-26 10:43:37 -0400 | [diff] [blame] | 698 | priv->cancellable = g_cancellable_new(); |
Stepan Salenikovich | fb7f295 | 2015-05-25 16:44:19 -0400 | [diff] [blame] | 699 | priv->settings = g_settings_new_full(get_ring_schema(), NULL, NULL); |
Stepan Salenikovich | 0d515e5 | 2015-05-19 16:31:05 -0400 | [diff] [blame] | 700 | |
Stepan Salenikovich | 0d515e5 | 2015-05-19 16:31:05 -0400 | [diff] [blame] | 701 | /* add custom cmd line options */ |
| 702 | ring_client_add_options(G_APPLICATION(self)); |
Stepan Salenikovich | d81ef29 | 2015-02-17 18:47:37 -0500 | [diff] [blame] | 703 | } |
| 704 | |
| 705 | static void |
| 706 | ring_client_class_init(RingClientClass *klass) |
| 707 | { |
Stepan Salenikovich | 0d515e5 | 2015-05-19 16:31:05 -0400 | [diff] [blame] | 708 | G_APPLICATION_CLASS(klass)->startup = ring_client_startup; |
Stepan Salenikovich | 0d515e5 | 2015-05-19 16:31:05 -0400 | [diff] [blame] | 709 | G_APPLICATION_CLASS(klass)->activate = ring_client_activate; |
Victor Nikulshin | 167bbb6 | 2017-03-15 20:44:27 +0000 | [diff] [blame] | 710 | G_APPLICATION_CLASS(klass)->open = ring_client_open; |
Stepan Salenikovich | d81ef29 | 2015-02-17 18:47:37 -0500 | [diff] [blame] | 711 | G_APPLICATION_CLASS(klass)->shutdown = ring_client_shutdown; |
| 712 | } |
| 713 | |
| 714 | RingClient * |
Stepan Salenikovich | 0d515e5 | 2015-05-19 16:31:05 -0400 | [diff] [blame] | 715 | ring_client_new(int argc, char *argv[]) |
Stepan Salenikovich | d81ef29 | 2015-02-17 18:47:37 -0500 | [diff] [blame] | 716 | { |
Stepan Salenikovich | 0d515e5 | 2015-05-19 16:31:05 -0400 | [diff] [blame] | 717 | RingClient *client = (RingClient *)g_object_new(ring_client_get_type(), |
Stepan Salenikovich | 7635058 | 2015-05-25 14:56:32 -0400 | [diff] [blame] | 718 | "application-id", RING_CLIENT_APP_ID, |
Victor Nikulshin | 167bbb6 | 2017-03-15 20:44:27 +0000 | [diff] [blame] | 719 | "flags", G_APPLICATION_HANDLES_OPEN , |
Stepan Salenikovich | 0d515e5 | 2015-05-19 16:31:05 -0400 | [diff] [blame] | 720 | NULL); |
Stepan Salenikovich | 0d515e5 | 2015-05-19 16:31:05 -0400 | [diff] [blame] | 721 | |
| 722 | /* copy the cmd line args before they get processed by the GApplication*/ |
| 723 | RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client); |
| 724 | priv->argc = argc; |
| 725 | priv->argv = g_strdupv((gchar **)argv); |
| 726 | |
| 727 | return client; |
Stepan Salenikovich | d81ef29 | 2015-02-17 18:47:37 -0500 | [diff] [blame] | 728 | } |
Stepan Salenikovich | 67112d1 | 2015-06-16 16:57:06 -0400 | [diff] [blame] | 729 | |
| 730 | GtkWindow * |
Stepan Salenikovich | be6550c | 2015-08-21 16:16:03 -0400 | [diff] [blame] | 731 | ring_client_get_main_window(RingClient *client) |
Stepan Salenikovich | 67112d1 | 2015-06-16 16:57:06 -0400 | [diff] [blame] | 732 | { |
| 733 | g_return_val_if_fail(IS_RING_CLIENT(client), NULL); |
| 734 | RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client); |
| 735 | |
| 736 | return (GtkWindow *)priv->win; |
| 737 | } |
Stepan Salenikovich | bb9c24e | 2015-09-15 09:55:02 -0400 | [diff] [blame] | 738 | |
| 739 | void |
| 740 | ring_client_set_restore_main_window_state(RingClient *client, gboolean restore) |
| 741 | { |
| 742 | g_return_if_fail(IS_RING_CLIENT(client)); |
| 743 | RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client); |
| 744 | |
| 745 | priv->restore_window_state = restore; |
| 746 | } |