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 | { |
Hugo Lefeuvre | 24356d4 | 2018-05-11 10:41:41 -0400 | [diff] [blame] | 129 | // cancel account creation, this will call hide_account_creation_wizard via callback |
| 130 | account_creation_wizard_cancel(ACCOUNT_CREATION_WIZARD(old_view)); |
aviau | 6aeb485 | 2016-08-18 16:01:09 -0400 | [diff] [blame] | 131 | return; |
| 132 | } |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 133 | |
| 134 | QModelIndex account_idx = get_index_from_selection(selection); |
| 135 | if (!account_idx.isValid()) { |
Hugo Lefeuvre | 24356d4 | 2018-05-11 10:41:41 -0400 | [diff] [blame] | 136 | g_debug("accountview: invalid account selection"); |
| 137 | |
| 138 | /* If no account is selected, check for account list size. If not 0, select |
| 139 | the first account. */ |
| 140 | GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(priv->treeview_account_list)); |
| 141 | if (gtk_tree_model_iter_n_children(model, NULL)) { |
| 142 | g_debug("accountview: other accounts are available, selecting first one"); |
| 143 | GtkTreePath *path = gtk_tree_path_new_from_indices(0, -1); |
| 144 | gtk_tree_selection_select_path(gtk_tree_view_get_selection (GTK_TREE_VIEW(priv->treeview_account_list)), path); |
| 145 | gtk_tree_path_free(path); |
| 146 | return; |
| 147 | } |
| 148 | |
| 149 | /* there is no account to select, display empty */ |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 150 | GtkWidget *empty_box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0); |
| 151 | gtk_widget_show(empty_box); |
| 152 | gtk_stack_add_named(GTK_STACK(priv->stack_account), empty_box, "placeholder"); |
| 153 | gtk_stack_set_visible_child(GTK_STACK(priv->stack_account), empty_box); |
Stepan Salenikovich | bdb0ecd | 2016-05-18 13:30:55 -0400 | [diff] [blame] | 154 | |
Sébastien Blin | 1f92b4a | 2018-02-28 11:39:11 -0500 | [diff] [blame] | 155 | /* cannot delete accounts */ |
Stepan Salenikovich | bdb0ecd | 2016-05-18 13:30:55 -0400 | [diff] [blame] | 156 | gtk_widget_set_sensitive(priv->button_remove_account, FALSE); |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 157 | } else { |
Guillaume Roguez | 5d1514b | 2015-10-22 15:55:31 -0400 | [diff] [blame] | 158 | Account *account = AccountModel::instance().getAccountByModelIndex(account_idx); |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 159 | |
| 160 | /* build new account view */ |
| 161 | GtkWidget *hbox_account = gtk_box_new(GTK_ORIENTATION_VERTICAL, 10); |
aviau | b135a69 | 2016-12-12 14:41:01 -0500 | [diff] [blame] | 162 | gtk_widget_show(hbox_account); |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 163 | |
| 164 | /* create account notebook */ |
Stepan Salenikovich | bdb0ecd | 2016-05-18 13:30:55 -0400 | [diff] [blame] | 165 | auto account_notebook = gtk_notebook_new(); |
aviau | b135a69 | 2016-12-12 14:41:01 -0500 | [diff] [blame] | 166 | gtk_widget_show(account_notebook); |
Stepan Salenikovich | bdb0ecd | 2016-05-18 13:30:55 -0400 | [diff] [blame] | 167 | gtk_notebook_set_scrollable(GTK_NOTEBOOK(account_notebook), TRUE); |
| 168 | gtk_notebook_set_show_border(GTK_NOTEBOOK(account_notebook), FALSE); |
| 169 | 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] | 170 | |
Sébastien Blin | 276202f | 2018-05-11 09:37:34 -0400 | [diff] [blame] | 171 | // Build a new AccountInfoPointer pointing on selected account in this view, not in the app |
| 172 | // TODO in the future, get rid of the Account class (pass the selected id) |
| 173 | auto selectedId = account->id().toStdString(); |
Sébastien Blin | e05cde1 | 2018-05-14 16:21:14 -0400 | [diff] [blame^] | 174 | try { |
| 175 | priv->selectedInfo_ = &(*priv->accountInfo_)->accountModel->getAccountInfo(selectedId); |
| 176 | } catch (std::out_of_range& e) { |
| 177 | g_debug("Can't load account %s", selectedId.c_str()); |
| 178 | return; |
| 179 | } |
Sébastien Blin | 276202f | 2018-05-11 09:37:34 -0400 | [diff] [blame] | 180 | |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 181 | /* customize account view based on account */ |
Sébastien Blin | 276202f | 2018-05-11 09:37:34 -0400 | [diff] [blame] | 182 | 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] | 183 | gtk_widget_show(general_tab); |
Stepan Salenikovich | bdb0ecd | 2016-05-18 13:30:55 -0400 | [diff] [blame] | 184 | gtk_notebook_append_page(GTK_NOTEBOOK(account_notebook), |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 185 | general_tab, |
Stepan Salenikovich | a1b8cb3 | 2015-09-11 14:58:35 -0400 | [diff] [blame] | 186 | gtk_label_new(C_("Account settings", "General"))); |
aviau | 2a23183 | 2016-12-12 12:27:38 -0500 | [diff] [blame] | 187 | |
aviau | 9e4e84d | 2016-12-12 12:41:54 -0500 | [diff] [blame] | 188 | if (account->protocol() == Account::Protocol::RING) |
| 189 | { |
| 190 | auto devices_tab = create_scrolled_account_view(account_devices_tab_new(account)); |
aviau | b135a69 | 2016-12-12 14:41:01 -0500 | [diff] [blame] | 191 | gtk_widget_show(devices_tab); |
aviau | 9e4e84d | 2016-12-12 12:41:54 -0500 | [diff] [blame] | 192 | gtk_notebook_append_page(GTK_NOTEBOOK(account_notebook), |
| 193 | devices_tab, |
| 194 | gtk_label_new(C_("Account settings", "Devices"))); |
| 195 | } |
Stepan Salenikovich | d45e639 | 2015-08-11 10:16:35 -0400 | [diff] [blame] | 196 | auto security_tab = create_scrolled_account_view(account_security_tab_new(account)); |
aviau | b135a69 | 2016-12-12 14:41:01 -0500 | [diff] [blame] | 197 | gtk_widget_show(security_tab); |
Stepan Salenikovich | bdb0ecd | 2016-05-18 13:30:55 -0400 | [diff] [blame] | 198 | gtk_notebook_append_page(GTK_NOTEBOOK(account_notebook), |
Stepan Salenikovich | 7a1e71c | 2015-05-07 11:14:48 -0400 | [diff] [blame] | 199 | security_tab, |
Stepan Salenikovich | a1b8cb3 | 2015-09-11 14:58:35 -0400 | [diff] [blame] | 200 | gtk_label_new(C_("Account settings", "Security"))); |
aviau | 2a23183 | 2016-12-12 12:27:38 -0500 | [diff] [blame] | 201 | auto advanced_tab = create_scrolled_account_view(account_advanced_tab_new(account)); |
aviau | b135a69 | 2016-12-12 14:41:01 -0500 | [diff] [blame] | 202 | gtk_widget_show(advanced_tab); |
aviau | 6aeb485 | 2016-08-18 16:01:09 -0400 | [diff] [blame] | 203 | gtk_notebook_append_page(GTK_NOTEBOOK(account_notebook), |
aviau | 2a23183 | 2016-12-12 12:27:38 -0500 | [diff] [blame] | 204 | advanced_tab, |
| 205 | gtk_label_new(C_("Account settings", "Advanced"))); |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 206 | |
Nicolas Jager | 005fc55 | 2017-04-26 07:50:26 -0400 | [diff] [blame] | 207 | if (account->protocol() == Account::Protocol::RING) { |
| 208 | auto banned_tab = create_scrolled_account_view(account_bans_tab_new(account)); |
| 209 | gtk_widget_show(banned_tab); |
| 210 | gtk_notebook_append_page(GTK_NOTEBOOK(account_notebook), |
| 211 | banned_tab, |
| 212 | gtk_label_new(C_("List of banned peers", "Bans"))); |
| 213 | } |
| 214 | |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 215 | /* set the tab displayed to the same as the prev account selected */ |
Stepan Salenikovich | bdb0ecd | 2016-05-18 13:30:55 -0400 | [diff] [blame] | 216 | gtk_notebook_set_current_page(GTK_NOTEBOOK(account_notebook), priv->current_page); |
| 217 | /* now connect to the tab changed signal */ |
| 218 | 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] | 219 | |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 220 | /* set the new account view as visible */ |
| 221 | char *account_view_name = g_strdup_printf("%p_account", account); |
| 222 | gtk_stack_add_named(GTK_STACK(priv->stack_account), hbox_account, account_view_name); |
| 223 | gtk_stack_set_visible_child(GTK_STACK(priv->stack_account), hbox_account); |
| 224 | g_free(account_view_name); |
Stepan Salenikovich | bdb0ecd | 2016-05-18 13:30:55 -0400 | [diff] [blame] | 225 | |
Sébastien Blin | 1f92b4a | 2018-02-28 11:39:11 -0500 | [diff] [blame] | 226 | /* can delete accounts */ |
Stepan Salenikovich | bdb0ecd | 2016-05-18 13:30:55 -0400 | [diff] [blame] | 227 | gtk_widget_set_sensitive(priv->button_remove_account, TRUE); |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 228 | } |
| 229 | |
Stepan Salenikovich | bdb0ecd | 2016-05-18 13:30:55 -0400 | [diff] [blame] | 230 | /* remove the old view */ |
| 231 | if (old_view) |
| 232 | gtk_container_remove(GTK_CONTAINER(priv->stack_account), old_view); |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | static void |
| 236 | account_active_toggled(GtkCellRendererToggle *renderer, gchar *path, AccountView *view) |
| 237 | { |
| 238 | g_return_if_fail(IS_ACCOUNT_VIEW(view)); |
| 239 | AccountViewPrivate *priv = ACCOUNT_VIEW_GET_PRIVATE(view); |
| 240 | |
| 241 | /* we want to set it to the opposite of the current value */ |
| 242 | gboolean toggle = !gtk_cell_renderer_toggle_get_active(renderer); |
| 243 | |
| 244 | /* get iter which was clicked */ |
| 245 | GtkTreePath *tree_path = gtk_tree_path_new_from_string(path); |
| 246 | GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(priv->treeview_account_list)); |
| 247 | GtkTreeIter iter; |
| 248 | gtk_tree_model_get_iter(model, &iter, tree_path); |
| 249 | |
| 250 | /* get qmodelindex from iter and set the model data */ |
| 251 | QModelIndex idx = gtk_q_tree_model_get_source_idx(GTK_Q_TREE_MODEL(model), &iter); |
| 252 | if (idx.isValid()) { |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 253 | QVariant alias = idx.data(static_cast<int>(Account::Role::Alias)); |
Stepan Salenikovich | fea8405 | 2016-05-13 16:32:51 -0400 | [diff] [blame] | 254 | AccountModel::instance().setData(idx, QVariant(toggle), Qt::CheckStateRole); |
| 255 | /* save the account to apply the changed state right away */ |
| 256 | AccountModel::instance().getAccountByModelIndex(idx)->performAction(Account::EditAction::SAVE); |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 257 | } |
| 258 | } |
| 259 | |
Stepan Salenikovich | c3fede2 | 2015-03-20 17:01:47 -0400 | [diff] [blame] | 260 | static gboolean |
Stepan Salenikovich | bd02958 | 2015-03-24 11:00:56 -0400 | [diff] [blame] | 261 | remove_account_dialog(AccountView *view, Account *account) |
Stepan Salenikovich | c3fede2 | 2015-03-20 17:01:47 -0400 | [diff] [blame] | 262 | { |
| 263 | gboolean response = FALSE; |
| 264 | GtkWidget *dialog = gtk_message_dialog_new(NULL, |
| 265 | (GtkDialogFlags)(GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT), |
| 266 | GTK_MESSAGE_QUESTION, GTK_BUTTONS_OK_CANCEL, |
Stepan Salenikovich | a1b8cb3 | 2015-09-11 14:58:35 -0400 | [diff] [blame] | 267 | _("Are you sure you want to delete account \"%s\"?"), |
Stepan Salenikovich | c3fede2 | 2015-03-20 17:01:47 -0400 | [diff] [blame] | 268 | account->alias().toLocal8Bit().constData()); |
| 269 | |
Stepan Salenikovich | bd02958 | 2015-03-24 11:00:56 -0400 | [diff] [blame] | 270 | gtk_window_set_destroy_with_parent(GTK_WINDOW(dialog), TRUE); |
| 271 | |
| 272 | /* get parent window so we can center on it */ |
| 273 | GtkWidget *parent = gtk_widget_get_toplevel(GTK_WIDGET(view)); |
| 274 | if (gtk_widget_is_toplevel(parent)) { |
| 275 | gtk_window_set_transient_for(GTK_WINDOW(dialog), GTK_WINDOW(parent)); |
| 276 | gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER_ON_PARENT); |
| 277 | } |
| 278 | |
Stepan Salenikovich | c3fede2 | 2015-03-20 17:01:47 -0400 | [diff] [blame] | 279 | switch (gtk_dialog_run(GTK_DIALOG(dialog))) { |
| 280 | case GTK_RESPONSE_OK: |
| 281 | response = TRUE; |
| 282 | break; |
| 283 | default: |
| 284 | response = FALSE; |
| 285 | break; |
| 286 | } |
| 287 | |
| 288 | gtk_widget_destroy(dialog); |
| 289 | |
| 290 | return response; |
| 291 | } |
| 292 | |
Stepan Salenikovich | bd02958 | 2015-03-24 11:00:56 -0400 | [diff] [blame] | 293 | static gboolean |
| 294 | save_account(GtkWidget *working_dialog) |
| 295 | { |
Guillaume Roguez | 5d1514b | 2015-10-22 15:55:31 -0400 | [diff] [blame] | 296 | AccountModel::instance().save(); |
Stepan Salenikovich | bd02958 | 2015-03-24 11:00:56 -0400 | [diff] [blame] | 297 | if (working_dialog) |
| 298 | gtk_widget_destroy(working_dialog); |
| 299 | |
| 300 | return G_SOURCE_REMOVE; |
| 301 | } |
| 302 | |
Stepan Salenikovich | c3fede2 | 2015-03-20 17:01:47 -0400 | [diff] [blame] | 303 | static void |
| 304 | remove_account(G_GNUC_UNUSED GtkWidget *entry, AccountView *view) |
| 305 | { |
| 306 | g_return_if_fail(IS_ACCOUNT_VIEW(view)); |
| 307 | AccountViewPrivate *priv = ACCOUNT_VIEW_GET_PRIVATE(view); |
| 308 | |
| 309 | GtkTreeSelection *selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_account_list)); |
| 310 | QModelIndex idx = get_index_from_selection(selection); |
| 311 | |
| 312 | if (idx.isValid()) { |
| 313 | /* this is a destructive operation, ask the user if they are sure */ |
Guillaume Roguez | 5d1514b | 2015-10-22 15:55:31 -0400 | [diff] [blame] | 314 | Account *account = AccountModel::instance().getAccountByModelIndex(idx); |
Stepan Salenikovich | bd02958 | 2015-03-24 11:00:56 -0400 | [diff] [blame] | 315 | if (remove_account_dialog(view, account)) { |
| 316 | /* show working dialog in case save operation takes time */ |
| 317 | GtkWidget *working = ring_dialog_working(GTK_WIDGET(view), NULL); |
| 318 | gtk_window_present(GTK_WINDOW(working)); |
| 319 | |
Guillaume Roguez | 5d1514b | 2015-10-22 15:55:31 -0400 | [diff] [blame] | 320 | AccountModel::instance().remove(idx); |
Stepan Salenikovich | bd02958 | 2015-03-24 11:00:56 -0400 | [diff] [blame] | 321 | |
| 322 | /* now save the time it takes to transition the account view to the new account (300ms) |
| 323 | * the save doesn't happen before the "working" dialog is presented |
| 324 | * the timeout function should destroy the "working" dialog when done saving |
| 325 | */ |
Stepan Salenikovich | 12fee94 | 2015-03-25 18:38:47 -0400 | [diff] [blame] | 326 | 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] | 327 | } |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | static void |
aviau | 6aeb485 | 2016-08-18 16:01:09 -0400 | [diff] [blame] | 332 | hide_account_creation_wizard(AccountView *view) |
| 333 | { |
| 334 | g_return_if_fail(IS_ACCOUNT_VIEW(view)); |
| 335 | AccountViewPrivate *priv = ACCOUNT_VIEW_GET_PRIVATE(view); |
| 336 | |
| 337 | auto old_view = gtk_stack_get_visible_child(GTK_STACK(priv->stack_account)); |
| 338 | if (IS_ACCOUNT_CREATION_WIZARD(old_view)) |
| 339 | { |
Hugo Lefeuvre | 24356d4 | 2018-05-11 10:41:41 -0400 | [diff] [blame] | 340 | account_creation_wizard_show_preview(ACCOUNT_CREATION_WIZARD(old_view), FALSE); |
aviau | 6aeb485 | 2016-08-18 16:01:09 -0400 | [diff] [blame] | 341 | gtk_container_remove(GTK_CONTAINER(priv->stack_account), old_view); |
| 342 | } |
| 343 | |
| 344 | auto selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_account_list)); |
| 345 | account_selection_changed(selection, view); |
| 346 | } |
| 347 | |
| 348 | static void |
| 349 | show_account_creation_wizard(AccountView *view) |
| 350 | { |
| 351 | g_return_if_fail(IS_ACCOUNT_VIEW(view)); |
| 352 | AccountViewPrivate *priv = ACCOUNT_VIEW_GET_PRIVATE(view); |
| 353 | |
| 354 | auto old_view = gtk_stack_get_visible_child(GTK_STACK(priv->stack_account)); |
| 355 | |
aviau | 6908184 | 2016-10-14 14:51:54 -0400 | [diff] [blame] | 356 | if (!priv->account_creation_wizard) |
aviau | 6aeb485 | 2016-08-18 16:01:09 -0400 | [diff] [blame] | 357 | { |
aviau | 6908184 | 2016-10-14 14:51:54 -0400 | [diff] [blame] | 358 | priv->account_creation_wizard = account_creation_wizard_new(true); |
| 359 | 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] | 360 | gtk_stack_add_named(GTK_STACK(priv->stack_account), |
aviau | 6908184 | 2016-10-14 14:51:54 -0400 | [diff] [blame] | 361 | priv->account_creation_wizard, |
aviau | 6aeb485 | 2016-08-18 16:01:09 -0400 | [diff] [blame] | 362 | ACCOUNT_CREATION_WIZARD_VIEW_NAME); |
aviau | 6908184 | 2016-10-14 14:51:54 -0400 | [diff] [blame] | 363 | g_signal_connect_swapped(priv->account_creation_wizard, "account-creation-completed", G_CALLBACK(hide_account_creation_wizard), view); |
| 364 | 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] | 365 | } |
| 366 | |
aviau | 6908184 | 2016-10-14 14:51:54 -0400 | [diff] [blame] | 367 | gtk_widget_show(priv->account_creation_wizard); |
aviau | 6aeb485 | 2016-08-18 16:01:09 -0400 | [diff] [blame] | 368 | |
| 369 | gtk_stack_set_visible_child_name(GTK_STACK(priv->stack_account), ACCOUNT_CREATION_WIZARD_VIEW_NAME); |
| 370 | |
| 371 | /* remove the old view */ |
| 372 | if (old_view) |
| 373 | gtk_container_remove(GTK_CONTAINER(priv->stack_account), old_view); |
| 374 | } |
| 375 | |
| 376 | static void |
Stepan Salenikovich | c3fede2 | 2015-03-20 17:01:47 -0400 | [diff] [blame] | 377 | add_account(G_GNUC_UNUSED GtkWidget *entry, AccountView *view) |
| 378 | { |
| 379 | g_return_if_fail(IS_ACCOUNT_VIEW(view)); |
| 380 | AccountViewPrivate *priv = ACCOUNT_VIEW_GET_PRIVATE(view); |
| 381 | |
Stepan Salenikovich | 08c4ab2 | 2017-04-26 14:27:24 -0400 | [diff] [blame] | 382 | auto idx = gtk_combo_box_get_index(GTK_COMBO_BOX(priv->combobox_account_type)); |
| 383 | if (idx.isValid()) { |
| 384 | Account::Protocol protocol = qvariant_cast<Account::Protocol>(idx.data((int)ProtocolModel::Role::Protocol)); |
| 385 | if (protocol == Account::Protocol::RING) |
| 386 | { |
Hugo Lefeuvre | 24356d4 | 2018-05-11 10:41:41 -0400 | [diff] [blame] | 387 | auto old_view = gtk_stack_get_visible_child(GTK_STACK(priv->stack_account)); |
| 388 | if (IS_ACCOUNT_CREATION_WIZARD(old_view)) |
| 389 | { |
| 390 | account_creation_wizard_cancel(ACCOUNT_CREATION_WIZARD(old_view)); |
| 391 | } else { |
| 392 | show_account_creation_wizard(view); |
| 393 | } |
Stepan Salenikovich | 08c4ab2 | 2017-04-26 14:27:24 -0400 | [diff] [blame] | 394 | } |
| 395 | else |
| 396 | { |
| 397 | /* show working dialog in case save operation takes time */ |
| 398 | GtkWidget *working = ring_dialog_working(GTK_WIDGET(view), NULL); |
| 399 | gtk_window_present(GTK_WINDOW(working)); |
Stepan Salenikovich | bd02958 | 2015-03-24 11:00:56 -0400 | [diff] [blame] | 400 | |
Stepan Salenikovich | 08c4ab2 | 2017-04-26 14:27:24 -0400 | [diff] [blame] | 401 | AccountModel::instance().add(QString(_("New Account")), idx); |
Stepan Salenikovich | bd02958 | 2015-03-24 11:00:56 -0400 | [diff] [blame] | 402 | |
Stepan Salenikovich | 08c4ab2 | 2017-04-26 14:27:24 -0400 | [diff] [blame] | 403 | /* now save after a short timeout to make sure that |
| 404 | * the save doesn't happen before the "working" dialog is presented |
| 405 | * the timeout function should destroy the "working" dialog when done saving |
| 406 | */ |
| 407 | 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] | 408 | } |
| 409 | } |
| 410 | } |
| 411 | |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 412 | static void |
Stepan Salenikovich | d6a9ba9 | 2015-03-29 11:14:36 -0400 | [diff] [blame] | 413 | state_to_string(G_GNUC_UNUSED GtkTreeViewColumn *tree_column, |
| 414 | GtkCellRenderer *cell, |
| 415 | GtkTreeModel *tree_model, |
| 416 | GtkTreeIter *iter, |
Stepan Salenikovich | e4981b2 | 2015-10-22 15:22:59 -0400 | [diff] [blame] | 417 | GtkTreeView *treeview) |
Stepan Salenikovich | d6a9ba9 | 2015-03-29 11:14:36 -0400 | [diff] [blame] | 418 | { |
Stepan Salenikovich | e4981b2 | 2015-10-22 15:22:59 -0400 | [diff] [blame] | 419 | // check if this iter is selected |
| 420 | gboolean is_selected = FALSE; |
| 421 | if (GTK_IS_TREE_VIEW(treeview)) { |
| 422 | auto selection = gtk_tree_view_get_selection(treeview); |
| 423 | is_selected = gtk_tree_selection_iter_is_selected(selection, iter); |
| 424 | } |
| 425 | |
Stepan Salenikovich | d6a9ba9 | 2015-03-29 11:14:36 -0400 | [diff] [blame] | 426 | gchar *display_state = NULL; |
Stepan Salenikovich | e7a7314 | 2015-08-11 11:55:59 -0400 | [diff] [blame] | 427 | |
| 428 | /* get account */ |
| 429 | QModelIndex idx = gtk_q_tree_model_get_source_idx(GTK_Q_TREE_MODEL(tree_model), iter); |
| 430 | if (idx.isValid()) { |
| 431 | |
Guillaume Roguez | 5d1514b | 2015-10-22 15:55:31 -0400 | [diff] [blame] | 432 | auto account = AccountModel::instance().getAccountByModelIndex(idx); |
Stepan Salenikovich | e7a7314 | 2015-08-11 11:55:59 -0400 | [diff] [blame] | 433 | auto humanState = account->toHumanStateName(); |
| 434 | |
Stepan Salenikovich | 0731de0 | 2016-05-17 17:47:16 -0400 | [diff] [blame] | 435 | auto escaped_state = g_markup_escape_text(humanState.toUtf8().constData(), -1); |
Stepan Salenikovich | e4981b2 | 2015-10-22 15:22:59 -0400 | [diff] [blame] | 436 | /* we want the color of the status text to be the default color if this iter is |
| 437 | * selected so that the treeview is able to invert it against the selection color */ |
| 438 | if (is_selected) { |
Stepan Salenikovich | 0731de0 | 2016-05-17 17:47:16 -0400 | [diff] [blame] | 439 | display_state = escaped_state; |
Stepan Salenikovich | e4981b2 | 2015-10-22 15:22:59 -0400 | [diff] [blame] | 440 | } else { |
| 441 | switch (account->registrationState()) { |
| 442 | case Account::RegistrationState::READY: |
Stepan Salenikovich | 0731de0 | 2016-05-17 17:47:16 -0400 | [diff] [blame] | 443 | display_state = g_strdup_printf("<span fgcolor=\"green\">%s</span>", escaped_state); |
Stepan Salenikovich | e4981b2 | 2015-10-22 15:22:59 -0400 | [diff] [blame] | 444 | break; |
| 445 | case Account::RegistrationState::UNREGISTERED: |
Stepan Salenikovich | 0731de0 | 2016-05-17 17:47:16 -0400 | [diff] [blame] | 446 | display_state = g_strdup_printf("<span fgcolor=\"gray\">%s</span>", escaped_state); |
Stepan Salenikovich | e4981b2 | 2015-10-22 15:22:59 -0400 | [diff] [blame] | 447 | break; |
| 448 | case Account::RegistrationState::TRYING: |
aviau | 6aeb485 | 2016-08-18 16:01:09 -0400 | [diff] [blame] | 449 | case Account::RegistrationState::INITIALIZING: |
Stepan Salenikovich | 0731de0 | 2016-05-17 17:47:16 -0400 | [diff] [blame] | 450 | display_state = g_strdup_printf("<span fgcolor=\"orange\">%s</span>", escaped_state); |
Stepan Salenikovich | e4981b2 | 2015-10-22 15:22:59 -0400 | [diff] [blame] | 451 | break; |
| 452 | case Account::RegistrationState::ERROR: |
Stepan Salenikovich | 0731de0 | 2016-05-17 17:47:16 -0400 | [diff] [blame] | 453 | display_state = g_strdup_printf("<span fgcolor=\"red\">%s</span>", escaped_state); |
Stepan Salenikovich | e4981b2 | 2015-10-22 15:22:59 -0400 | [diff] [blame] | 454 | break; |
| 455 | case Account::RegistrationState::COUNT__: |
| 456 | g_warning("registration state should never be \"count\""); |
Stepan Salenikovich | 0731de0 | 2016-05-17 17:47:16 -0400 | [diff] [blame] | 457 | display_state = g_strdup_printf("<span fgcolor=\"red\">%s</span>", escaped_state); |
Stepan Salenikovich | e4981b2 | 2015-10-22 15:22:59 -0400 | [diff] [blame] | 458 | break; |
| 459 | } |
Stepan Salenikovich | 0731de0 | 2016-05-17 17:47:16 -0400 | [diff] [blame] | 460 | g_free(escaped_state); |
Stepan Salenikovich | e7a7314 | 2015-08-11 11:55:59 -0400 | [diff] [blame] | 461 | } |
Stepan Salenikovich | d6a9ba9 | 2015-03-29 11:14:36 -0400 | [diff] [blame] | 462 | } |
Stepan Salenikovich | e7a7314 | 2015-08-11 11:55:59 -0400 | [diff] [blame] | 463 | |
Stepan Salenikovich | d6a9ba9 | 2015-03-29 11:14:36 -0400 | [diff] [blame] | 464 | g_object_set(G_OBJECT(cell), "markup", display_state, NULL); |
| 465 | g_free(display_state); |
| 466 | } |
| 467 | |
| 468 | static void |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 469 | account_view_init(AccountView *view) |
| 470 | { |
| 471 | gtk_widget_init_template(GTK_WIDGET(view)); |
| 472 | |
| 473 | AccountViewPrivate *priv = ACCOUNT_VIEW_GET_PRIVATE(view); |
| 474 | |
| 475 | /* account model */ |
| 476 | GtkQTreeModel *account_model; |
| 477 | GtkCellRenderer *renderer; |
| 478 | GtkTreeViewColumn *column; |
| 479 | |
Guillaume Roguez | 5d1514b | 2015-10-22 15:55:31 -0400 | [diff] [blame] | 480 | account_model = gtk_q_tree_model_new(&AccountModel::instance(), 4, |
aviau | 9bbb19b | 2016-05-16 15:53:44 -0400 | [diff] [blame] | 481 | 0, Account::Role::Enabled, G_TYPE_BOOLEAN, |
| 482 | 0, Account::Role::Alias, G_TYPE_STRING, |
| 483 | 0, Account::Role::Proto, G_TYPE_STRING, |
| 484 | 0, Account::Role::RegistrationState, G_TYPE_UINT); |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 485 | gtk_tree_view_set_model(GTK_TREE_VIEW(priv->treeview_account_list), GTK_TREE_MODEL(account_model)); |
| 486 | |
| 487 | renderer = gtk_cell_renderer_toggle_new(); |
Stepan Salenikovich | a1b8cb3 | 2015-09-11 14:58:35 -0400 | [diff] [blame] | 488 | 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] | 489 | gtk_tree_view_append_column(GTK_TREE_VIEW(priv->treeview_account_list), column); |
| 490 | |
| 491 | g_signal_connect(renderer, "toggled", G_CALLBACK(account_active_toggled), view); |
| 492 | |
| 493 | renderer = gtk_cell_renderer_text_new(); |
Stepan Salenikovich | a1b8cb3 | 2015-09-11 14:58:35 -0400 | [diff] [blame] | 494 | 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] | 495 | 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] | 496 | g_object_set(G_OBJECT(renderer), "ellipsize", PANGO_ELLIPSIZE_END, NULL); |
| 497 | gtk_tree_view_column_set_expand(column, TRUE); |
| 498 | // set a min width so most of the account name is visible |
| 499 | g_object_set(G_OBJECT(renderer), "width", 75, NULL); |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 500 | |
| 501 | renderer = gtk_cell_renderer_text_new(); |
Stepan Salenikovich | a1b8cb3 | 2015-09-11 14:58:35 -0400 | [diff] [blame] | 502 | 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] | 503 | 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] | 504 | g_object_set(G_OBJECT(renderer), "ellipsize", PANGO_ELLIPSIZE_END, NULL); |
| 505 | gtk_tree_view_column_set_expand(column, TRUE); |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 506 | |
Stepan Salenikovich | d6a9ba9 | 2015-03-29 11:14:36 -0400 | [diff] [blame] | 507 | /* the registration state is an enum, we want to display it as a string */ |
| 508 | gtk_tree_view_column_set_cell_data_func( |
| 509 | column, |
| 510 | renderer, |
| 511 | (GtkTreeCellDataFunc)state_to_string, |
Stepan Salenikovich | e4981b2 | 2015-10-22 15:22:59 -0400 | [diff] [blame] | 512 | priv->treeview_account_list, |
Stepan Salenikovich | d6a9ba9 | 2015-03-29 11:14:36 -0400 | [diff] [blame] | 513 | NULL); |
| 514 | |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 515 | /* add an empty box to the account stack initially, otherwise there will |
| 516 | * be no cool animation when the first account is selected */ |
| 517 | GtkWidget *empty_box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0); |
| 518 | gtk_widget_show(empty_box); |
| 519 | gtk_stack_add_named(GTK_STACK(priv->stack_account), empty_box, "placeholder"); |
| 520 | gtk_stack_set_visible_child(GTK_STACK(priv->stack_account), empty_box); |
Stepan Salenikovich | c3fede2 | 2015-03-20 17:01:47 -0400 | [diff] [blame] | 521 | |
| 522 | /* populate account type combo box */ |
Stepan Salenikovich | 08c4ab2 | 2017-04-26 14:27:24 -0400 | [diff] [blame] | 523 | priv->protocol_selection_changed = gtk_combo_box_set_qmodel_text( |
| 524 | GTK_COMBO_BOX(priv->combobox_account_type), |
| 525 | AccountModel::instance().protocolModel(), |
| 526 | AccountModel::instance().protocolModel()->selectionModel() |
| 527 | ); |
Stepan Salenikovich | c3fede2 | 2015-03-20 17:01:47 -0400 | [diff] [blame] | 528 | |
Stepan Salenikovich | c3fede2 | 2015-03-20 17:01:47 -0400 | [diff] [blame] | 529 | GtkTreeSelection *account_selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_account_list)); |
| 530 | g_signal_connect(account_selection, "changed", G_CALLBACK(update_account_model_selection), NULL); |
| 531 | g_signal_connect(account_selection, "changed", G_CALLBACK(account_selection_changed), view); |
| 532 | |
Stepan Salenikovich | c3fede2 | 2015-03-20 17:01:47 -0400 | [diff] [blame] | 533 | /* connect signals to add/remove accounts */ |
| 534 | g_signal_connect(priv->button_remove_account, "clicked", G_CALLBACK(remove_account), view); |
| 535 | 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] | 536 | } |
| 537 | |
| 538 | static void |
| 539 | account_view_class_init(AccountViewClass *klass) |
| 540 | { |
| 541 | G_OBJECT_CLASS(klass)->dispose = account_view_dispose; |
| 542 | |
| 543 | gtk_widget_class_set_template_from_resource(GTK_WIDGET_CLASS (klass), |
| 544 | "/cx/ring/RingGnome/accountview.ui"); |
| 545 | |
| 546 | gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), AccountView, treeview_account_list); |
| 547 | 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] | 548 | gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), AccountView, button_remove_account); |
| 549 | gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), AccountView, button_add_account); |
| 550 | 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] | 551 | } |
| 552 | |
Sébastien Blin | e05cde1 | 2018-05-14 16:21:14 -0400 | [diff] [blame^] | 553 | static void |
| 554 | build_account_view(AccountView *view) { |
| 555 | g_return_if_fail(IS_ACCOUNT_VIEW(view)); |
| 556 | AccountViewPrivate *priv = ACCOUNT_VIEW_GET_PRIVATE(view); |
| 557 | |
| 558 | // NOTE: this is temporary. We still use AccountModel here, so if a new account is added, |
| 559 | // we should select it! |
| 560 | priv->account_selection_changed = QObject::connect( |
| 561 | &*(*priv->accountInfo_)->accountModel, |
| 562 | &lrc::api::NewAccountModel::accountAdded, |
| 563 | [=] (const std::string& id) { |
| 564 | auto idx = 0; |
| 565 | auto iterIsCorrect = true; |
| 566 | GtkTreeIter iter; |
| 567 | auto selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_account_list)); |
| 568 | GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(priv->treeview_account_list)); |
| 569 | |
| 570 | while (iterIsCorrect) { |
| 571 | iterIsCorrect = gtk_tree_model_iter_nth_child(model, &iter, nullptr, idx); |
| 572 | if (!iterIsCorrect) |
| 573 | break; |
| 574 | QModelIndex accountIdx = gtk_q_tree_model_get_source_idx(GTK_Q_TREE_MODEL(model), &iter); |
| 575 | if (accountIdx.isValid()) { |
| 576 | auto account = AccountModel::instance().getAccountByModelIndex(accountIdx); |
| 577 | if (account->id().toStdString() == id) |
| 578 | gtk_tree_selection_select_iter(selection, &iter); |
| 579 | } |
| 580 | idx++; |
| 581 | } |
| 582 | } |
| 583 | ); |
| 584 | } |
| 585 | |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 586 | GtkWidget * |
Sébastien Blin | 276202f | 2018-05-11 09:37:34 -0400 | [diff] [blame] | 587 | account_view_new(AccountInfoPointer const & accountInfo) |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 588 | { |
Sébastien Blin | 276202f | 2018-05-11 09:37:34 -0400 | [diff] [blame] | 589 | auto* view = g_object_new(ACCOUNT_VIEW_TYPE, NULL); |
| 590 | AccountViewPrivate *priv = ACCOUNT_VIEW_GET_PRIVATE(view); |
| 591 | priv->accountInfo_ = &accountInfo; |
Sébastien Blin | e05cde1 | 2018-05-14 16:21:14 -0400 | [diff] [blame^] | 592 | if (*priv->accountInfo_) |
| 593 | build_account_view(ACCOUNT_VIEW(view)); |
Sébastien Blin | 276202f | 2018-05-11 09:37:34 -0400 | [diff] [blame] | 594 | return (GtkWidget *)view; |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 595 | } |
Hugo Lefeuvre | 24356d4 | 2018-05-11 10:41:41 -0400 | [diff] [blame] | 596 | |
| 597 | void |
| 598 | account_settings_view_show(AccountView *view, gboolean show) { |
| 599 | AccountViewPrivate *priv = ACCOUNT_VIEW_GET_PRIVATE(view); |
| 600 | |
| 601 | /* If displayed stack element is still the placeholder element, thats is it hasn't |
| 602 | been initialized yet, change account selection. */ |
| 603 | if (show && strcmp(gtk_stack_get_visible_child_name (GTK_STACK(priv->stack_account)), "placeholder") == 0) { |
| 604 | auto selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_account_list)); |
| 605 | account_selection_changed(selection, view); |
| 606 | return; |
| 607 | } |
| 608 | |
| 609 | /* If account creation wizard is currently open and settings get closed, cancel so |
| 610 | the preview is stopped properly. */ |
| 611 | auto old_view = gtk_stack_get_visible_child(GTK_STACK(priv->stack_account)); |
| 612 | if (!show && IS_ACCOUNT_CREATION_WIZARD(old_view)) { |
| 613 | account_creation_wizard_cancel(ACCOUNT_CREATION_WIZARD(old_view)); |
| 614 | } |
| 615 | } |