Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [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 | 61cbab0 | 2015-03-16 18:35:10 -0400 | [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 | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | #include "accountview.h" |
| 21 | |
| 22 | #include <gtk/gtk.h> |
Stepan Salenikovich | a1b8cb3 | 2015-09-11 14:58:35 -0400 | [diff] [blame] | 23 | #include <glib/gi18n.h> |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 24 | #include <accountmodel.h> |
Edric Milaret | 58e7107 | 2016-03-21 12:16:37 -0400 | [diff] [blame] | 25 | #include <codecmodel.h> |
Stepan Salenikovich | c3fede2 | 2015-03-20 17:01:47 -0400 | [diff] [blame] | 26 | #include <protocolmodel.h> |
| 27 | #include <QtCore/QItemSelectionModel> |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 28 | #include "models/gtkqtreemodel.h" |
Stepan Salenikovich | f607822 | 2016-10-03 17:31:16 -0400 | [diff] [blame] | 29 | #include "models/gtkqtreemodel.h" |
Stepan Salenikovich | c3fede2 | 2015-03-20 17:01:47 -0400 | [diff] [blame] | 30 | #include "models/activeitemproxymodel.h" |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 31 | #include "accountgeneraltab.h" |
aviau | 6aeb485 | 2016-08-18 16:01:09 -0400 | [diff] [blame^] | 32 | #include "accountcreationwizard.h" |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 33 | #include "accountaudiotab.h" |
| 34 | #include "accountvideotab.h" |
Stepan Salenikovich | b7e6a73 | 2015-05-04 17:13:22 -0400 | [diff] [blame] | 35 | #include "accountadvancedtab.h" |
Stepan Salenikovich | 7a1e71c | 2015-05-07 11:14:48 -0400 | [diff] [blame] | 36 | #include "accountsecuritytab.h" |
aviau | 6aeb485 | 2016-08-18 16:01:09 -0400 | [diff] [blame^] | 37 | #include "accountdevicestab.h" |
Stepan Salenikovich | bd02958 | 2015-03-24 11:00:56 -0400 | [diff] [blame] | 38 | #include "dialogs.h" |
Stepan Salenikovich | d6a9ba9 | 2015-03-29 11:14:36 -0400 | [diff] [blame] | 39 | #include <glib/gprintf.h> |
Stepan Salenikovich | 9816a94 | 2015-04-22 17:49:16 -0400 | [diff] [blame] | 40 | #include "utils/models.h" |
Stepan Salenikovich | bdb0ecd | 2016-05-18 13:30:55 -0400 | [diff] [blame] | 41 | #include "accountimportexportview.h" |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 42 | |
aviau | 6aeb485 | 2016-08-18 16:01:09 -0400 | [diff] [blame^] | 43 | static constexpr const char* ACCOUNT_CREATION_WIZARD_VIEW_NAME = "account-creation-wizard"; |
| 44 | |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 45 | struct _AccountView |
| 46 | { |
Stepan Salenikovich | ab0f5be | 2016-02-05 15:16:35 -0500 | [diff] [blame] | 47 | GtkPaned parent; |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 48 | }; |
| 49 | |
| 50 | struct _AccountViewClass |
| 51 | { |
Stepan Salenikovich | ab0f5be | 2016-02-05 15:16:35 -0500 | [diff] [blame] | 52 | GtkPanedClass parent_class; |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 53 | }; |
| 54 | |
| 55 | typedef struct _AccountViewPrivate AccountViewPrivate; |
| 56 | |
| 57 | struct _AccountViewPrivate |
| 58 | { |
| 59 | GtkWidget *treeview_account_list; |
Stepan Salenikovich | bdb0ecd | 2016-05-18 13:30:55 -0400 | [diff] [blame] | 60 | GtkWidget *account_import_view; |
| 61 | GtkWidget *account_export_view; |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 62 | GtkWidget *stack_account; |
Stepan Salenikovich | c3fede2 | 2015-03-20 17:01:47 -0400 | [diff] [blame] | 63 | GtkWidget *button_remove_account; |
| 64 | GtkWidget *button_add_account; |
| 65 | GtkWidget *combobox_account_type; |
Stepan Salenikovich | bdb0ecd | 2016-05-18 13:30:55 -0400 | [diff] [blame] | 66 | GtkWidget *button_import_account; |
| 67 | GtkWidget *button_export_account; |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 68 | |
Stepan Salenikovich | 2d63d5e | 2015-03-22 23:23:54 -0400 | [diff] [blame] | 69 | gint current_page; /* keeps track of current notebook page displayed */ |
Stepan Salenikovich | c3fede2 | 2015-03-20 17:01:47 -0400 | [diff] [blame] | 70 | |
| 71 | ActiveItemProxyModel *active_protocols; |
Stepan Salenikovich | 2d63d5e | 2015-03-22 23:23:54 -0400 | [diff] [blame] | 72 | QMetaObject::Connection protocol_selection_changed; |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 73 | }; |
| 74 | |
Stepan Salenikovich | ab0f5be | 2016-02-05 15:16:35 -0500 | [diff] [blame] | 75 | G_DEFINE_TYPE_WITH_PRIVATE(AccountView, account_view, GTK_TYPE_PANED); |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 76 | |
| 77 | #define ACCOUNT_VIEW_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), ACCOUNT_VIEW_TYPE, AccountViewPrivate)) |
| 78 | |
| 79 | static void |
| 80 | account_view_dispose(GObject *object) |
| 81 | { |
| 82 | AccountView *view = ACCOUNT_VIEW(object); |
| 83 | AccountViewPrivate *priv = ACCOUNT_VIEW_GET_PRIVATE(view); |
| 84 | |
Stepan Salenikovich | 2d63d5e | 2015-03-22 23:23:54 -0400 | [diff] [blame] | 85 | QObject::disconnect(priv->protocol_selection_changed); |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 86 | |
| 87 | G_OBJECT_CLASS(account_view_parent_class)->dispose(object); |
| 88 | } |
| 89 | |
Stepan Salenikovich | 2d63d5e | 2015-03-22 23:23:54 -0400 | [diff] [blame] | 90 | static void |
| 91 | account_view_finalize(GObject *object) |
| 92 | { |
| 93 | AccountView *view = ACCOUNT_VIEW(object); |
| 94 | AccountViewPrivate *priv = ACCOUNT_VIEW_GET_PRIVATE(view); |
| 95 | |
| 96 | delete priv->active_protocols; |
| 97 | |
| 98 | G_OBJECT_CLASS(account_view_parent_class)->finalize(object); |
| 99 | } |
| 100 | |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 101 | static void |
Stepan Salenikovich | c3fede2 | 2015-03-20 17:01:47 -0400 | [diff] [blame] | 102 | update_account_model_selection(GtkTreeSelection *selection, G_GNUC_UNUSED gpointer user_data) |
| 103 | { |
| 104 | QModelIndex current = get_index_from_selection(selection); |
| 105 | if (current.isValid()) |
Guillaume Roguez | 5d1514b | 2015-10-22 15:55:31 -0400 | [diff] [blame] | 106 | AccountModel::instance().selectionModel()->setCurrentIndex(current, QItemSelectionModel::ClearAndSelect); |
Stepan Salenikovich | c3fede2 | 2015-03-20 17:01:47 -0400 | [diff] [blame] | 107 | else |
Guillaume Roguez | 5d1514b | 2015-10-22 15:55:31 -0400 | [diff] [blame] | 108 | AccountModel::instance().selectionModel()->clearCurrentIndex(); |
Stepan Salenikovich | c3fede2 | 2015-03-20 17:01:47 -0400 | [diff] [blame] | 109 | } |
| 110 | |
Stepan Salenikovich | d45e639 | 2015-08-11 10:16:35 -0400 | [diff] [blame] | 111 | static GtkWidget * |
| 112 | create_scrolled_account_view(GtkWidget *account_view) |
| 113 | { |
| 114 | auto scrolled = gtk_scrolled_window_new(NULL, NULL); |
| 115 | gtk_container_add(GTK_CONTAINER(scrolled), account_view); |
| 116 | return scrolled; |
| 117 | } |
| 118 | |
Stepan Salenikovich | c3fede2 | 2015-03-20 17:01:47 -0400 | [diff] [blame] | 119 | static void |
Stepan Salenikovich | bdb0ecd | 2016-05-18 13:30:55 -0400 | [diff] [blame] | 120 | tab_selection_changed(G_GNUC_UNUSED GtkNotebook *notebook, |
| 121 | G_GNUC_UNUSED GtkWidget *page, |
| 122 | guint page_num, |
| 123 | AccountView *self) |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 124 | { |
Stepan Salenikovich | bdb0ecd | 2016-05-18 13:30:55 -0400 | [diff] [blame] | 125 | g_return_if_fail(IS_ACCOUNT_VIEW(self)); |
| 126 | AccountViewPrivate *priv = ACCOUNT_VIEW_GET_PRIVATE(self); |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 127 | |
Stepan Salenikovich | bdb0ecd | 2016-05-18 13:30:55 -0400 | [diff] [blame] | 128 | priv->current_page = page_num; |
| 129 | } |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 130 | |
Stepan Salenikovich | bdb0ecd | 2016-05-18 13:30:55 -0400 | [diff] [blame] | 131 | static void |
| 132 | account_selection_changed(GtkTreeSelection *selection, AccountView *self) |
| 133 | { |
| 134 | g_return_if_fail(IS_ACCOUNT_VIEW(self)); |
| 135 | AccountViewPrivate *priv = ACCOUNT_VIEW_GET_PRIVATE(self); |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 136 | |
Stepan Salenikovich | bdb0ecd | 2016-05-18 13:30:55 -0400 | [diff] [blame] | 137 | auto old_view = gtk_stack_get_visible_child(GTK_STACK(priv->stack_account)); |
aviau | 6aeb485 | 2016-08-18 16:01:09 -0400 | [diff] [blame^] | 138 | if(IS_ACCOUNT_CREATION_WIZARD(old_view)) |
| 139 | { |
| 140 | /* When using the account creation wizard, The user might create |
| 141 | * accounts that will be deleted by the daemon. The fact that the |
| 142 | * selection has changed should be ignored. The account creation wizard |
| 143 | * should be responsible to remove itself of the stack and then call |
| 144 | * account_selection_changed. |
| 145 | */ |
| 146 | return; |
| 147 | } |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 148 | |
| 149 | QModelIndex account_idx = get_index_from_selection(selection); |
| 150 | if (!account_idx.isValid()) { |
| 151 | /* it nothing is slected, simply display something empty */ |
| 152 | GtkWidget *empty_box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0); |
| 153 | gtk_widget_show(empty_box); |
| 154 | gtk_stack_add_named(GTK_STACK(priv->stack_account), empty_box, "placeholder"); |
| 155 | gtk_stack_set_visible_child(GTK_STACK(priv->stack_account), empty_box); |
Stepan Salenikovich | bdb0ecd | 2016-05-18 13:30:55 -0400 | [diff] [blame] | 156 | |
| 157 | /* cannot delete nor export accounts */ |
| 158 | gtk_widget_set_sensitive(priv->button_remove_account, FALSE); |
| 159 | gtk_widget_set_sensitive(priv->button_export_account, FALSE); |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 160 | } else { |
Guillaume Roguez | 5d1514b | 2015-10-22 15:55:31 -0400 | [diff] [blame] | 161 | Account *account = AccountModel::instance().getAccountByModelIndex(account_idx); |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 162 | |
| 163 | /* build new account view */ |
| 164 | GtkWidget *hbox_account = gtk_box_new(GTK_ORIENTATION_VERTICAL, 10); |
| 165 | |
| 166 | /* create account notebook */ |
Stepan Salenikovich | bdb0ecd | 2016-05-18 13:30:55 -0400 | [diff] [blame] | 167 | auto account_notebook = gtk_notebook_new(); |
| 168 | gtk_notebook_set_scrollable(GTK_NOTEBOOK(account_notebook), TRUE); |
| 169 | gtk_notebook_set_show_border(GTK_NOTEBOOK(account_notebook), FALSE); |
| 170 | gtk_box_pack_start(GTK_BOX(hbox_account), account_notebook, TRUE, TRUE, 0); |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 171 | |
| 172 | /* customize account view based on account */ |
Stepan Salenikovich | d45e639 | 2015-08-11 10:16:35 -0400 | [diff] [blame] | 173 | auto general_tab = create_scrolled_account_view(account_general_tab_new(account)); |
Stepan Salenikovich | bdb0ecd | 2016-05-18 13:30:55 -0400 | [diff] [blame] | 174 | gtk_notebook_append_page(GTK_NOTEBOOK(account_notebook), |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 175 | general_tab, |
Stepan Salenikovich | a1b8cb3 | 2015-09-11 14:58:35 -0400 | [diff] [blame] | 176 | gtk_label_new(C_("Account settings", "General"))); |
Stepan Salenikovich | d45e639 | 2015-08-11 10:16:35 -0400 | [diff] [blame] | 177 | auto audio_tab = create_scrolled_account_view(account_audio_tab_new(account)); |
Stepan Salenikovich | bdb0ecd | 2016-05-18 13:30:55 -0400 | [diff] [blame] | 178 | gtk_notebook_append_page(GTK_NOTEBOOK(account_notebook), |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 179 | audio_tab, |
Stepan Salenikovich | a1b8cb3 | 2015-09-11 14:58:35 -0400 | [diff] [blame] | 180 | gtk_label_new(C_("Account settings", "Audio"))); |
Stepan Salenikovich | d45e639 | 2015-08-11 10:16:35 -0400 | [diff] [blame] | 181 | auto video_tab = create_scrolled_account_view(account_video_tab_new(account)); |
Stepan Salenikovich | bdb0ecd | 2016-05-18 13:30:55 -0400 | [diff] [blame] | 182 | gtk_notebook_append_page(GTK_NOTEBOOK(account_notebook), |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 183 | video_tab, |
Stepan Salenikovich | a1b8cb3 | 2015-09-11 14:58:35 -0400 | [diff] [blame] | 184 | gtk_label_new(C_("Account settings", "Video"))); |
Stepan Salenikovich | d45e639 | 2015-08-11 10:16:35 -0400 | [diff] [blame] | 185 | auto advanced_tab = create_scrolled_account_view(account_advanced_tab_new(account)); |
Stepan Salenikovich | bdb0ecd | 2016-05-18 13:30:55 -0400 | [diff] [blame] | 186 | gtk_notebook_append_page(GTK_NOTEBOOK(account_notebook), |
Stepan Salenikovich | b7e6a73 | 2015-05-04 17:13:22 -0400 | [diff] [blame] | 187 | advanced_tab, |
Stepan Salenikovich | a1b8cb3 | 2015-09-11 14:58:35 -0400 | [diff] [blame] | 188 | gtk_label_new(C_("Account settings", "Advanced"))); |
Stepan Salenikovich | d45e639 | 2015-08-11 10:16:35 -0400 | [diff] [blame] | 189 | auto security_tab = create_scrolled_account_view(account_security_tab_new(account)); |
Stepan Salenikovich | bdb0ecd | 2016-05-18 13:30:55 -0400 | [diff] [blame] | 190 | gtk_notebook_append_page(GTK_NOTEBOOK(account_notebook), |
Stepan Salenikovich | 7a1e71c | 2015-05-07 11:14:48 -0400 | [diff] [blame] | 191 | security_tab, |
Stepan Salenikovich | a1b8cb3 | 2015-09-11 14:58:35 -0400 | [diff] [blame] | 192 | gtk_label_new(C_("Account settings", "Security"))); |
aviau | 6aeb485 | 2016-08-18 16:01:09 -0400 | [diff] [blame^] | 193 | auto devices_tab = create_scrolled_account_view(account_devices_tab_new(account)); |
| 194 | gtk_notebook_append_page(GTK_NOTEBOOK(account_notebook), |
| 195 | devices_tab, |
| 196 | gtk_label_new(C_("Account settings", "Devices"))); |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 197 | |
Stepan Salenikovich | ad58908 | 2016-05-18 13:22:52 -0400 | [diff] [blame] | 198 | gtk_widget_show_all(hbox_account); |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 199 | /* set the tab displayed to the same as the prev account selected */ |
Stepan Salenikovich | bdb0ecd | 2016-05-18 13:30:55 -0400 | [diff] [blame] | 200 | gtk_notebook_set_current_page(GTK_NOTEBOOK(account_notebook), priv->current_page); |
| 201 | /* now connect to the tab changed signal */ |
| 202 | g_signal_connect(account_notebook, "switch-page", G_CALLBACK(tab_selection_changed), self); |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 203 | |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 204 | /* set the new account view as visible */ |
| 205 | char *account_view_name = g_strdup_printf("%p_account", account); |
| 206 | gtk_stack_add_named(GTK_STACK(priv->stack_account), hbox_account, account_view_name); |
| 207 | gtk_stack_set_visible_child(GTK_STACK(priv->stack_account), hbox_account); |
| 208 | g_free(account_view_name); |
Stepan Salenikovich | bdb0ecd | 2016-05-18 13:30:55 -0400 | [diff] [blame] | 209 | |
| 210 | /* can delete and export accounts */ |
| 211 | gtk_widget_set_sensitive(priv->button_remove_account, TRUE); |
| 212 | gtk_widget_set_sensitive(priv->button_export_account, TRUE); |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 213 | } |
| 214 | |
Stepan Salenikovich | bdb0ecd | 2016-05-18 13:30:55 -0400 | [diff] [blame] | 215 | /* remove the old view */ |
| 216 | if (old_view) |
| 217 | gtk_container_remove(GTK_CONTAINER(priv->stack_account), old_view); |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | static void |
| 221 | account_active_toggled(GtkCellRendererToggle *renderer, gchar *path, AccountView *view) |
| 222 | { |
| 223 | g_return_if_fail(IS_ACCOUNT_VIEW(view)); |
| 224 | AccountViewPrivate *priv = ACCOUNT_VIEW_GET_PRIVATE(view); |
| 225 | |
| 226 | /* we want to set it to the opposite of the current value */ |
| 227 | gboolean toggle = !gtk_cell_renderer_toggle_get_active(renderer); |
| 228 | |
| 229 | /* get iter which was clicked */ |
| 230 | GtkTreePath *tree_path = gtk_tree_path_new_from_string(path); |
| 231 | GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(priv->treeview_account_list)); |
| 232 | GtkTreeIter iter; |
| 233 | gtk_tree_model_get_iter(model, &iter, tree_path); |
| 234 | |
| 235 | /* get qmodelindex from iter and set the model data */ |
| 236 | QModelIndex idx = gtk_q_tree_model_get_source_idx(GTK_Q_TREE_MODEL(model), &iter); |
| 237 | if (idx.isValid()) { |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 238 | QVariant alias = idx.data(static_cast<int>(Account::Role::Alias)); |
Stepan Salenikovich | fea8405 | 2016-05-13 16:32:51 -0400 | [diff] [blame] | 239 | AccountModel::instance().setData(idx, QVariant(toggle), Qt::CheckStateRole); |
| 240 | /* save the account to apply the changed state right away */ |
| 241 | AccountModel::instance().getAccountByModelIndex(idx)->performAction(Account::EditAction::SAVE); |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 242 | } |
| 243 | } |
| 244 | |
Stepan Salenikovich | c3fede2 | 2015-03-20 17:01:47 -0400 | [diff] [blame] | 245 | static gboolean |
Stepan Salenikovich | bd02958 | 2015-03-24 11:00:56 -0400 | [diff] [blame] | 246 | remove_account_dialog(AccountView *view, Account *account) |
Stepan Salenikovich | c3fede2 | 2015-03-20 17:01:47 -0400 | [diff] [blame] | 247 | { |
| 248 | gboolean response = FALSE; |
| 249 | GtkWidget *dialog = gtk_message_dialog_new(NULL, |
| 250 | (GtkDialogFlags)(GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT), |
| 251 | GTK_MESSAGE_QUESTION, GTK_BUTTONS_OK_CANCEL, |
Stepan Salenikovich | a1b8cb3 | 2015-09-11 14:58:35 -0400 | [diff] [blame] | 252 | _("Are you sure you want to delete account \"%s\"?"), |
Stepan Salenikovich | c3fede2 | 2015-03-20 17:01:47 -0400 | [diff] [blame] | 253 | account->alias().toLocal8Bit().constData()); |
| 254 | |
Stepan Salenikovich | bd02958 | 2015-03-24 11:00:56 -0400 | [diff] [blame] | 255 | gtk_window_set_destroy_with_parent(GTK_WINDOW(dialog), TRUE); |
| 256 | |
| 257 | /* get parent window so we can center on it */ |
| 258 | GtkWidget *parent = gtk_widget_get_toplevel(GTK_WIDGET(view)); |
| 259 | if (gtk_widget_is_toplevel(parent)) { |
| 260 | gtk_window_set_transient_for(GTK_WINDOW(dialog), GTK_WINDOW(parent)); |
| 261 | gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER_ON_PARENT); |
| 262 | } |
| 263 | |
Stepan Salenikovich | c3fede2 | 2015-03-20 17:01:47 -0400 | [diff] [blame] | 264 | switch (gtk_dialog_run(GTK_DIALOG(dialog))) { |
| 265 | case GTK_RESPONSE_OK: |
| 266 | response = TRUE; |
| 267 | break; |
| 268 | default: |
| 269 | response = FALSE; |
| 270 | break; |
| 271 | } |
| 272 | |
| 273 | gtk_widget_destroy(dialog); |
| 274 | |
| 275 | return response; |
| 276 | } |
| 277 | |
Stepan Salenikovich | bd02958 | 2015-03-24 11:00:56 -0400 | [diff] [blame] | 278 | static gboolean |
| 279 | save_account(GtkWidget *working_dialog) |
| 280 | { |
Guillaume Roguez | 5d1514b | 2015-10-22 15:55:31 -0400 | [diff] [blame] | 281 | AccountModel::instance().save(); |
Stepan Salenikovich | bd02958 | 2015-03-24 11:00:56 -0400 | [diff] [blame] | 282 | if (working_dialog) |
| 283 | gtk_widget_destroy(working_dialog); |
| 284 | |
| 285 | return G_SOURCE_REMOVE; |
| 286 | } |
| 287 | |
Stepan Salenikovich | c3fede2 | 2015-03-20 17:01:47 -0400 | [diff] [blame] | 288 | static void |
| 289 | remove_account(G_GNUC_UNUSED GtkWidget *entry, AccountView *view) |
| 290 | { |
| 291 | g_return_if_fail(IS_ACCOUNT_VIEW(view)); |
| 292 | AccountViewPrivate *priv = ACCOUNT_VIEW_GET_PRIVATE(view); |
| 293 | |
| 294 | GtkTreeSelection *selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_account_list)); |
| 295 | QModelIndex idx = get_index_from_selection(selection); |
| 296 | |
| 297 | if (idx.isValid()) { |
| 298 | /* this is a destructive operation, ask the user if they are sure */ |
Guillaume Roguez | 5d1514b | 2015-10-22 15:55:31 -0400 | [diff] [blame] | 299 | Account *account = AccountModel::instance().getAccountByModelIndex(idx); |
Stepan Salenikovich | bd02958 | 2015-03-24 11:00:56 -0400 | [diff] [blame] | 300 | if (remove_account_dialog(view, account)) { |
| 301 | /* show working dialog in case save operation takes time */ |
| 302 | GtkWidget *working = ring_dialog_working(GTK_WIDGET(view), NULL); |
| 303 | gtk_window_present(GTK_WINDOW(working)); |
| 304 | |
Guillaume Roguez | 5d1514b | 2015-10-22 15:55:31 -0400 | [diff] [blame] | 305 | AccountModel::instance().remove(idx); |
Stepan Salenikovich | bd02958 | 2015-03-24 11:00:56 -0400 | [diff] [blame] | 306 | |
| 307 | /* now save the time it takes to transition the account view to the new account (300ms) |
| 308 | * the save doesn't happen before the "working" dialog is presented |
| 309 | * the timeout function should destroy the "working" dialog when done saving |
| 310 | */ |
Stepan Salenikovich | 12fee94 | 2015-03-25 18:38:47 -0400 | [diff] [blame] | 311 | g_timeout_add_full(G_PRIORITY_DEFAULT, 300, (GSourceFunc)save_account, working, NULL); |
Stepan Salenikovich | c3fede2 | 2015-03-20 17:01:47 -0400 | [diff] [blame] | 312 | } |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | static void |
aviau | 6aeb485 | 2016-08-18 16:01:09 -0400 | [diff] [blame^] | 317 | hide_account_creation_wizard(AccountView *view) |
| 318 | { |
| 319 | g_return_if_fail(IS_ACCOUNT_VIEW(view)); |
| 320 | AccountViewPrivate *priv = ACCOUNT_VIEW_GET_PRIVATE(view); |
| 321 | |
| 322 | auto old_view = gtk_stack_get_visible_child(GTK_STACK(priv->stack_account)); |
| 323 | if (IS_ACCOUNT_CREATION_WIZARD(old_view)) |
| 324 | { |
| 325 | gtk_container_remove(GTK_CONTAINER(priv->stack_account), old_view); |
| 326 | } |
| 327 | |
| 328 | auto selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_account_list)); |
| 329 | account_selection_changed(selection, view); |
| 330 | } |
| 331 | |
| 332 | static void |
| 333 | show_account_creation_wizard(AccountView *view) |
| 334 | { |
| 335 | g_return_if_fail(IS_ACCOUNT_VIEW(view)); |
| 336 | AccountViewPrivate *priv = ACCOUNT_VIEW_GET_PRIVATE(view); |
| 337 | |
| 338 | auto old_view = gtk_stack_get_visible_child(GTK_STACK(priv->stack_account)); |
| 339 | |
| 340 | auto account_creation_wizard = gtk_stack_get_child_by_name( |
| 341 | GTK_STACK(priv->stack_account), |
| 342 | ACCOUNT_CREATION_WIZARD_VIEW_NAME |
| 343 | ); |
| 344 | |
| 345 | if (!account_creation_wizard) |
| 346 | { |
| 347 | account_creation_wizard = account_creation_wizard_new(true); |
| 348 | gtk_stack_add_named(GTK_STACK(priv->stack_account), |
| 349 | account_creation_wizard, |
| 350 | ACCOUNT_CREATION_WIZARD_VIEW_NAME); |
| 351 | g_signal_connect_swapped(account_creation_wizard, "account-creation-completed", G_CALLBACK(hide_account_creation_wizard), view); |
| 352 | g_signal_connect_swapped(account_creation_wizard, "account-creation-canceled", G_CALLBACK(hide_account_creation_wizard), view); |
| 353 | } |
| 354 | |
| 355 | gtk_widget_show(account_creation_wizard); |
| 356 | |
| 357 | gtk_stack_set_visible_child_name(GTK_STACK(priv->stack_account), ACCOUNT_CREATION_WIZARD_VIEW_NAME); |
| 358 | |
| 359 | /* remove the old view */ |
| 360 | if (old_view) |
| 361 | gtk_container_remove(GTK_CONTAINER(priv->stack_account), old_view); |
| 362 | } |
| 363 | |
| 364 | static void |
Stepan Salenikovich | c3fede2 | 2015-03-20 17:01:47 -0400 | [diff] [blame] | 365 | add_account(G_GNUC_UNUSED GtkWidget *entry, AccountView *view) |
| 366 | { |
| 367 | g_return_if_fail(IS_ACCOUNT_VIEW(view)); |
| 368 | AccountViewPrivate *priv = ACCOUNT_VIEW_GET_PRIVATE(view); |
| 369 | |
| 370 | GtkTreeIter protocol_iter; |
| 371 | if (gtk_combo_box_get_active_iter(GTK_COMBO_BOX(priv->combobox_account_type), &protocol_iter)) { |
| 372 | /* get the qmodelindex of the protocol */ |
| 373 | GtkTreeModel *protocol_model = gtk_combo_box_get_model(GTK_COMBO_BOX(priv->combobox_account_type)); |
Stepan Salenikovich | f607822 | 2016-10-03 17:31:16 -0400 | [diff] [blame] | 374 | QModelIndex protocol_idx = gtk_q_tree_model_get_source_idx( |
| 375 | GTK_Q_TREE_MODEL(protocol_model), |
Stepan Salenikovich | c3fede2 | 2015-03-20 17:01:47 -0400 | [diff] [blame] | 376 | &protocol_iter); |
| 377 | if (protocol_idx.isValid()) { |
| 378 | protocol_idx = priv->active_protocols->mapToSource(protocol_idx); |
Stepan Salenikovich | bd02958 | 2015-03-24 11:00:56 -0400 | [diff] [blame] | 379 | |
aviau | 6aeb485 | 2016-08-18 16:01:09 -0400 | [diff] [blame^] | 380 | Account::Protocol protocol = qvariant_cast<Account::Protocol>(protocol_idx.data((int)ProtocolModel::Role::Protocol)); |
| 381 | if (protocol == Account::Protocol::RING) |
| 382 | { |
| 383 | show_account_creation_wizard(view); |
| 384 | } |
| 385 | else |
| 386 | { |
| 387 | /* show working dialog in case save operation takes time */ |
| 388 | GtkWidget *working = ring_dialog_working(GTK_WIDGET(view), NULL); |
| 389 | gtk_window_present(GTK_WINDOW(working)); |
Stepan Salenikovich | bd02958 | 2015-03-24 11:00:56 -0400 | [diff] [blame] | 390 | |
aviau | 6aeb485 | 2016-08-18 16:01:09 -0400 | [diff] [blame^] | 391 | AccountModel::instance().add(QString(_("New Account")), protocol_idx); |
Stepan Salenikovich | bd02958 | 2015-03-24 11:00:56 -0400 | [diff] [blame] | 392 | |
aviau | 6aeb485 | 2016-08-18 16:01:09 -0400 | [diff] [blame^] | 393 | /* now save after a short timeout to make sure that |
| 394 | * the save doesn't happen before the "working" dialog is presented |
| 395 | * the timeout function should destroy the "working" dialog when done saving |
| 396 | */ |
| 397 | g_timeout_add_full(G_PRIORITY_DEFAULT, 300, (GSourceFunc)save_account, working, NULL); |
| 398 | } |
| 399 | |
Stepan Salenikovich | c3fede2 | 2015-03-20 17:01:47 -0400 | [diff] [blame] | 400 | } |
| 401 | } |
| 402 | } |
| 403 | |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 404 | static void |
Stepan Salenikovich | d6a9ba9 | 2015-03-29 11:14:36 -0400 | [diff] [blame] | 405 | state_to_string(G_GNUC_UNUSED GtkTreeViewColumn *tree_column, |
| 406 | GtkCellRenderer *cell, |
| 407 | GtkTreeModel *tree_model, |
| 408 | GtkTreeIter *iter, |
Stepan Salenikovich | e4981b2 | 2015-10-22 15:22:59 -0400 | [diff] [blame] | 409 | GtkTreeView *treeview) |
Stepan Salenikovich | d6a9ba9 | 2015-03-29 11:14:36 -0400 | [diff] [blame] | 410 | { |
Stepan Salenikovich | e4981b2 | 2015-10-22 15:22:59 -0400 | [diff] [blame] | 411 | // check if this iter is selected |
| 412 | gboolean is_selected = FALSE; |
| 413 | if (GTK_IS_TREE_VIEW(treeview)) { |
| 414 | auto selection = gtk_tree_view_get_selection(treeview); |
| 415 | is_selected = gtk_tree_selection_iter_is_selected(selection, iter); |
| 416 | } |
| 417 | |
Stepan Salenikovich | d6a9ba9 | 2015-03-29 11:14:36 -0400 | [diff] [blame] | 418 | gchar *display_state = NULL; |
Stepan Salenikovich | e7a7314 | 2015-08-11 11:55:59 -0400 | [diff] [blame] | 419 | |
| 420 | /* get account */ |
| 421 | QModelIndex idx = gtk_q_tree_model_get_source_idx(GTK_Q_TREE_MODEL(tree_model), iter); |
| 422 | if (idx.isValid()) { |
| 423 | |
Guillaume Roguez | 5d1514b | 2015-10-22 15:55:31 -0400 | [diff] [blame] | 424 | auto account = AccountModel::instance().getAccountByModelIndex(idx); |
Stepan Salenikovich | e7a7314 | 2015-08-11 11:55:59 -0400 | [diff] [blame] | 425 | auto humanState = account->toHumanStateName(); |
| 426 | |
Stepan Salenikovich | 0731de0 | 2016-05-17 17:47:16 -0400 | [diff] [blame] | 427 | auto escaped_state = g_markup_escape_text(humanState.toUtf8().constData(), -1); |
Stepan Salenikovich | e4981b2 | 2015-10-22 15:22:59 -0400 | [diff] [blame] | 428 | /* we want the color of the status text to be the default color if this iter is |
| 429 | * selected so that the treeview is able to invert it against the selection color */ |
| 430 | if (is_selected) { |
Stepan Salenikovich | 0731de0 | 2016-05-17 17:47:16 -0400 | [diff] [blame] | 431 | display_state = escaped_state; |
Stepan Salenikovich | e4981b2 | 2015-10-22 15:22:59 -0400 | [diff] [blame] | 432 | } else { |
| 433 | switch (account->registrationState()) { |
| 434 | case Account::RegistrationState::READY: |
Stepan Salenikovich | 0731de0 | 2016-05-17 17:47:16 -0400 | [diff] [blame] | 435 | display_state = g_strdup_printf("<span fgcolor=\"green\">%s</span>", escaped_state); |
Stepan Salenikovich | e4981b2 | 2015-10-22 15:22:59 -0400 | [diff] [blame] | 436 | break; |
| 437 | case Account::RegistrationState::UNREGISTERED: |
Stepan Salenikovich | 0731de0 | 2016-05-17 17:47:16 -0400 | [diff] [blame] | 438 | display_state = g_strdup_printf("<span fgcolor=\"gray\">%s</span>", escaped_state); |
Stepan Salenikovich | e4981b2 | 2015-10-22 15:22:59 -0400 | [diff] [blame] | 439 | break; |
| 440 | case Account::RegistrationState::TRYING: |
aviau | 6aeb485 | 2016-08-18 16:01:09 -0400 | [diff] [blame^] | 441 | case Account::RegistrationState::INITIALIZING: |
Stepan Salenikovich | 0731de0 | 2016-05-17 17:47:16 -0400 | [diff] [blame] | 442 | display_state = g_strdup_printf("<span fgcolor=\"orange\">%s</span>", escaped_state); |
Stepan Salenikovich | e4981b2 | 2015-10-22 15:22:59 -0400 | [diff] [blame] | 443 | break; |
| 444 | case Account::RegistrationState::ERROR: |
Stepan Salenikovich | 0731de0 | 2016-05-17 17:47:16 -0400 | [diff] [blame] | 445 | display_state = g_strdup_printf("<span fgcolor=\"red\">%s</span>", escaped_state); |
Stepan Salenikovich | e4981b2 | 2015-10-22 15:22:59 -0400 | [diff] [blame] | 446 | break; |
| 447 | case Account::RegistrationState::COUNT__: |
| 448 | g_warning("registration state should never be \"count\""); |
Stepan Salenikovich | 0731de0 | 2016-05-17 17:47:16 -0400 | [diff] [blame] | 449 | display_state = g_strdup_printf("<span fgcolor=\"red\">%s</span>", escaped_state); |
Stepan Salenikovich | e4981b2 | 2015-10-22 15:22:59 -0400 | [diff] [blame] | 450 | break; |
| 451 | } |
Stepan Salenikovich | 0731de0 | 2016-05-17 17:47:16 -0400 | [diff] [blame] | 452 | g_free(escaped_state); |
Stepan Salenikovich | e7a7314 | 2015-08-11 11:55:59 -0400 | [diff] [blame] | 453 | } |
Stepan Salenikovich | d6a9ba9 | 2015-03-29 11:14:36 -0400 | [diff] [blame] | 454 | } |
Stepan Salenikovich | e7a7314 | 2015-08-11 11:55:59 -0400 | [diff] [blame] | 455 | |
Stepan Salenikovich | d6a9ba9 | 2015-03-29 11:14:36 -0400 | [diff] [blame] | 456 | g_object_set(G_OBJECT(cell), "markup", display_state, NULL); |
| 457 | g_free(display_state); |
| 458 | } |
| 459 | |
| 460 | static void |
Stepan Salenikovich | bdb0ecd | 2016-05-18 13:30:55 -0400 | [diff] [blame] | 461 | close_import_export_view(AccountView *self) |
| 462 | { |
| 463 | g_return_if_fail(IS_ACCOUNT_VIEW(self)); |
| 464 | AccountViewPrivate *priv = ACCOUNT_VIEW_GET_PRIVATE(self); |
| 465 | |
| 466 | auto selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_account_list)); |
| 467 | account_selection_changed(selection, self); |
| 468 | } |
| 469 | |
| 470 | static void |
| 471 | import_account(AccountView* self) |
| 472 | { |
| 473 | g_return_if_fail(IS_ACCOUNT_VIEW(self)); |
| 474 | AccountViewPrivate *priv = ACCOUNT_VIEW_GET_PRIVATE(self); |
| 475 | |
| 476 | auto old_view = gtk_stack_get_visible_child(GTK_STACK(priv->stack_account)); |
| 477 | |
| 478 | auto import_account = account_import_view_new(); |
| 479 | g_signal_connect_swapped(import_account, "import-export-canceled", G_CALLBACK(close_import_export_view), self); |
| 480 | g_signal_connect_swapped(import_account, "import-export-completed", G_CALLBACK(close_import_export_view), self); |
| 481 | auto scrolled_view = create_scrolled_account_view(import_account); |
| 482 | gtk_widget_show_all(scrolled_view); |
| 483 | gtk_stack_add_named(GTK_STACK(priv->stack_account), scrolled_view, "import_account"); |
| 484 | gtk_stack_set_visible_child(GTK_STACK(priv->stack_account), scrolled_view); |
| 485 | |
| 486 | /* remove the old view */ |
| 487 | if (old_view) |
| 488 | gtk_container_remove(GTK_CONTAINER(priv->stack_account), old_view); |
| 489 | } |
| 490 | |
| 491 | static void |
| 492 | export_account(AccountView* self) |
| 493 | { |
| 494 | g_return_if_fail(IS_ACCOUNT_VIEW(self)); |
| 495 | AccountViewPrivate *priv = ACCOUNT_VIEW_GET_PRIVATE(self); |
| 496 | |
| 497 | auto selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_account_list)); |
| 498 | auto idx = get_index_from_selection(selection); |
| 499 | if (idx.isValid()) { |
| 500 | auto old_view = gtk_stack_get_visible_child(GTK_STACK(priv->stack_account)); |
| 501 | |
| 502 | auto account = AccountModel::instance().getAccountByModelIndex(idx); |
| 503 | auto export_account = account_export_view_new(); |
| 504 | g_signal_connect_swapped(export_account, "import-export-canceled", G_CALLBACK(close_import_export_view), self); |
| 505 | g_signal_connect_swapped(export_account, "import-export-completed", G_CALLBACK(close_import_export_view), self); |
| 506 | GList *account_list = nullptr; |
| 507 | account_list = g_list_append(account_list, account); |
| 508 | account_export_view_set_accounts(ACCOUNT_IMPORTEXPORT_VIEW(export_account), account_list); |
| 509 | g_list_free(account_list); |
| 510 | auto scrolled_view = create_scrolled_account_view(export_account); |
| 511 | gtk_widget_show_all(scrolled_view); |
| 512 | gtk_stack_add_named(GTK_STACK(priv->stack_account), scrolled_view, "export_account"); |
| 513 | gtk_stack_set_visible_child(GTK_STACK(priv->stack_account), scrolled_view); |
| 514 | |
| 515 | /* remove the old view */ |
| 516 | if (old_view) |
| 517 | gtk_container_remove(GTK_CONTAINER(priv->stack_account), old_view); |
| 518 | |
| 519 | } |
| 520 | |
| 521 | } |
| 522 | |
| 523 | static void |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 524 | account_view_init(AccountView *view) |
| 525 | { |
| 526 | gtk_widget_init_template(GTK_WIDGET(view)); |
| 527 | |
| 528 | AccountViewPrivate *priv = ACCOUNT_VIEW_GET_PRIVATE(view); |
| 529 | |
| 530 | /* account model */ |
| 531 | GtkQTreeModel *account_model; |
| 532 | GtkCellRenderer *renderer; |
| 533 | GtkTreeViewColumn *column; |
| 534 | |
Guillaume Roguez | 5d1514b | 2015-10-22 15:55:31 -0400 | [diff] [blame] | 535 | account_model = gtk_q_tree_model_new(&AccountModel::instance(), 4, |
aviau | 9bbb19b | 2016-05-16 15:53:44 -0400 | [diff] [blame] | 536 | 0, Account::Role::Enabled, G_TYPE_BOOLEAN, |
| 537 | 0, Account::Role::Alias, G_TYPE_STRING, |
| 538 | 0, Account::Role::Proto, G_TYPE_STRING, |
| 539 | 0, Account::Role::RegistrationState, G_TYPE_UINT); |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 540 | gtk_tree_view_set_model(GTK_TREE_VIEW(priv->treeview_account_list), GTK_TREE_MODEL(account_model)); |
| 541 | |
| 542 | renderer = gtk_cell_renderer_toggle_new(); |
Stepan Salenikovich | a1b8cb3 | 2015-09-11 14:58:35 -0400 | [diff] [blame] | 543 | column = gtk_tree_view_column_new_with_attributes(C_("Account state column", "Enabled"), renderer, "active", 0, NULL); |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 544 | gtk_tree_view_append_column(GTK_TREE_VIEW(priv->treeview_account_list), column); |
| 545 | |
| 546 | g_signal_connect(renderer, "toggled", G_CALLBACK(account_active_toggled), view); |
| 547 | |
| 548 | renderer = gtk_cell_renderer_text_new(); |
Stepan Salenikovich | a1b8cb3 | 2015-09-11 14:58:35 -0400 | [diff] [blame] | 549 | column = gtk_tree_view_column_new_with_attributes(C_("Account alias (name) column", "Alias"), renderer, "text", 1, NULL); |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 550 | gtk_tree_view_append_column(GTK_TREE_VIEW(priv->treeview_account_list), column); |
Stepan Salenikovich | ab0f5be | 2016-02-05 15:16:35 -0500 | [diff] [blame] | 551 | g_object_set(G_OBJECT(renderer), "ellipsize", PANGO_ELLIPSIZE_END, NULL); |
| 552 | gtk_tree_view_column_set_expand(column, TRUE); |
| 553 | // set a min width so most of the account name is visible |
| 554 | g_object_set(G_OBJECT(renderer), "width", 75, NULL); |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 555 | |
| 556 | renderer = gtk_cell_renderer_text_new(); |
Stepan Salenikovich | a1b8cb3 | 2015-09-11 14:58:35 -0400 | [diff] [blame] | 557 | column = gtk_tree_view_column_new_with_attributes(C_("Account status column", "Status"), renderer, "text", 3, NULL); |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 558 | gtk_tree_view_append_column(GTK_TREE_VIEW(priv->treeview_account_list), column); |
Stepan Salenikovich | ab0f5be | 2016-02-05 15:16:35 -0500 | [diff] [blame] | 559 | g_object_set(G_OBJECT(renderer), "ellipsize", PANGO_ELLIPSIZE_END, NULL); |
| 560 | gtk_tree_view_column_set_expand(column, TRUE); |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 561 | |
Stepan Salenikovich | d6a9ba9 | 2015-03-29 11:14:36 -0400 | [diff] [blame] | 562 | /* the registration state is an enum, we want to display it as a string */ |
| 563 | gtk_tree_view_column_set_cell_data_func( |
| 564 | column, |
| 565 | renderer, |
| 566 | (GtkTreeCellDataFunc)state_to_string, |
Stepan Salenikovich | e4981b2 | 2015-10-22 15:22:59 -0400 | [diff] [blame] | 567 | priv->treeview_account_list, |
Stepan Salenikovich | d6a9ba9 | 2015-03-29 11:14:36 -0400 | [diff] [blame] | 568 | NULL); |
| 569 | |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 570 | /* add an empty box to the account stack initially, otherwise there will |
| 571 | * be no cool animation when the first account is selected */ |
| 572 | GtkWidget *empty_box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0); |
| 573 | gtk_widget_show(empty_box); |
| 574 | gtk_stack_add_named(GTK_STACK(priv->stack_account), empty_box, "placeholder"); |
| 575 | gtk_stack_set_visible_child(GTK_STACK(priv->stack_account), empty_box); |
Stepan Salenikovich | c3fede2 | 2015-03-20 17:01:47 -0400 | [diff] [blame] | 576 | |
| 577 | /* populate account type combo box */ |
| 578 | /* TODO: when to delete this model? */ |
Guillaume Roguez | 5d1514b | 2015-10-22 15:55:31 -0400 | [diff] [blame] | 579 | priv->active_protocols = new ActiveItemProxyModel((QAbstractItemModel *)AccountModel::instance().protocolModel()); |
Stepan Salenikovich | c3fede2 | 2015-03-20 17:01:47 -0400 | [diff] [blame] | 580 | |
Stepan Salenikovich | f607822 | 2016-10-03 17:31:16 -0400 | [diff] [blame] | 581 | GtkQTreeModel *protocol_model = gtk_q_tree_model_new( |
Stepan Salenikovich | c3fede2 | 2015-03-20 17:01:47 -0400 | [diff] [blame] | 582 | (QSortFilterProxyModel *)priv->active_protocols, |
| 583 | 1, |
aviau | 271bcc2 | 2016-05-27 17:25:19 -0400 | [diff] [blame] | 584 | 0, Qt::DisplayRole, G_TYPE_STRING); |
Stepan Salenikovich | c3fede2 | 2015-03-20 17:01:47 -0400 | [diff] [blame] | 585 | |
| 586 | gtk_combo_box_set_model(GTK_COMBO_BOX(priv->combobox_account_type), GTK_TREE_MODEL(protocol_model)); |
| 587 | |
| 588 | renderer = gtk_cell_renderer_text_new(); |
| 589 | gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(priv->combobox_account_type), renderer, FALSE); |
| 590 | gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(priv->combobox_account_type), renderer, |
| 591 | "text", 0, NULL); |
| 592 | |
| 593 | /* connect signals to and from the selection model of the account model */ |
Stepan Salenikovich | 2d63d5e | 2015-03-22 23:23:54 -0400 | [diff] [blame] | 594 | priv->protocol_selection_changed = QObject::connect( |
Guillaume Roguez | 5d1514b | 2015-10-22 15:55:31 -0400 | [diff] [blame] | 595 | AccountModel::instance().selectionModel(), |
Stepan Salenikovich | c3fede2 | 2015-03-20 17:01:47 -0400 | [diff] [blame] | 596 | &QItemSelectionModel::currentChanged, |
Stepan Salenikovich | 825bb0a | 2016-03-11 15:33:52 -0500 | [diff] [blame] | 597 | [=](const QModelIndex & current, G_GNUC_UNUSED const QModelIndex & previous) { |
Stepan Salenikovich | c3fede2 | 2015-03-20 17:01:47 -0400 | [diff] [blame] | 598 | GtkTreeSelection *selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_account_list)); |
| 599 | |
Stepan Salenikovich | c3fede2 | 2015-03-20 17:01:47 -0400 | [diff] [blame] | 600 | /* select the current */ |
| 601 | if (current.isValid()) { |
| 602 | GtkTreeIter new_iter; |
| 603 | if (gtk_q_tree_model_source_index_to_iter(account_model, current, &new_iter)) { |
| 604 | gtk_tree_selection_select_iter(selection, &new_iter); |
| 605 | } else { |
Stepan Salenikovich | f903d1b | 2015-03-25 14:51:45 -0400 | [diff] [blame] | 606 | g_warning("SelectionModel of AccountModel changed to invalid QModelIndex?"); |
Stepan Salenikovich | c3fede2 | 2015-03-20 17:01:47 -0400 | [diff] [blame] | 607 | } |
| 608 | } |
| 609 | } |
| 610 | ); |
| 611 | |
| 612 | GtkTreeSelection *account_selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_account_list)); |
| 613 | g_signal_connect(account_selection, "changed", G_CALLBACK(update_account_model_selection), NULL); |
| 614 | g_signal_connect(account_selection, "changed", G_CALLBACK(account_selection_changed), view); |
| 615 | |
| 616 | /* select the default protocol */ |
Guillaume Roguez | 5d1514b | 2015-10-22 15:55:31 -0400 | [diff] [blame] | 617 | QModelIndex protocol_idx = AccountModel::instance().protocolModel()->selectionModel()->currentIndex(); |
Stepan Salenikovich | c3fede2 | 2015-03-20 17:01:47 -0400 | [diff] [blame] | 618 | if (protocol_idx.isValid()) { |
| 619 | protocol_idx = priv->active_protocols->mapFromSource(protocol_idx); |
| 620 | GtkTreeIter protocol_iter; |
Stepan Salenikovich | f607822 | 2016-10-03 17:31:16 -0400 | [diff] [blame] | 621 | if (gtk_q_tree_model_source_index_to_iter( |
| 622 | (GtkQTreeModel *)protocol_model, |
Stepan Salenikovich | c3fede2 | 2015-03-20 17:01:47 -0400 | [diff] [blame] | 623 | protocol_idx, |
| 624 | &protocol_iter)) { |
| 625 | gtk_combo_box_set_active_iter(GTK_COMBO_BOX(priv->combobox_account_type), &protocol_iter); |
| 626 | } |
| 627 | } |
| 628 | |
| 629 | /* connect signals to add/remove accounts */ |
| 630 | g_signal_connect(priv->button_remove_account, "clicked", G_CALLBACK(remove_account), view); |
| 631 | g_signal_connect(priv->button_add_account, "clicked", G_CALLBACK(add_account), view); |
Stepan Salenikovich | bdb0ecd | 2016-05-18 13:30:55 -0400 | [diff] [blame] | 632 | |
| 633 | /* signals to import/export acounts */ |
| 634 | g_signal_connect_swapped(priv->button_import_account, "clicked", G_CALLBACK(import_account), view); |
| 635 | g_signal_connect_swapped(priv->button_export_account, "clicked", G_CALLBACK(export_account), view); |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | static void |
| 639 | account_view_class_init(AccountViewClass *klass) |
| 640 | { |
| 641 | G_OBJECT_CLASS(klass)->dispose = account_view_dispose; |
Stepan Salenikovich | 2d63d5e | 2015-03-22 23:23:54 -0400 | [diff] [blame] | 642 | G_OBJECT_CLASS(klass)->finalize = account_view_finalize; |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 643 | |
| 644 | gtk_widget_class_set_template_from_resource(GTK_WIDGET_CLASS (klass), |
| 645 | "/cx/ring/RingGnome/accountview.ui"); |
| 646 | |
| 647 | gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), AccountView, treeview_account_list); |
| 648 | gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), AccountView, stack_account); |
Stepan Salenikovich | c3fede2 | 2015-03-20 17:01:47 -0400 | [diff] [blame] | 649 | gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), AccountView, button_remove_account); |
| 650 | gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), AccountView, button_add_account); |
| 651 | gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), AccountView, combobox_account_type); |
Stepan Salenikovich | bdb0ecd | 2016-05-18 13:30:55 -0400 | [diff] [blame] | 652 | gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), AccountView, button_import_account); |
| 653 | gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), AccountView, button_export_account); |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 654 | } |
| 655 | |
| 656 | GtkWidget * |
| 657 | account_view_new(void) |
| 658 | { |
| 659 | return (GtkWidget *)g_object_new(ACCOUNT_VIEW_TYPE, NULL); |
| 660 | } |