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