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