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