blob: b234690a4145294a01a8b140325b022cb1613a8c [file] [log] [blame]
Stepan Salenikovich61cbab02015-03-16 18:35:10 -04001/*
Guillaume Roguez77c579d2018-01-30 15:54:02 -05002 * Copyright (C) 2015-2018 Savoir-faire Linux Inc.
Stepan Salenikovich61cbab02015-03-16 18:35:10 -04003 * 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 Salenikovich61cbab02015-03-16 18:35:10 -040018 */
19
20#include "accountview.h"
21
22#include <gtk/gtk.h>
Stepan Salenikovicha1b8cb32015-09-11 14:58:35 -040023#include <glib/gi18n.h>
Stepan Salenikovich61cbab02015-03-16 18:35:10 -040024#include <accountmodel.h>
Stepan Salenikovichc3fede22015-03-20 17:01:47 -040025#include <protocolmodel.h>
26#include <QtCore/QItemSelectionModel>
Stepan Salenikovich61cbab02015-03-16 18:35:10 -040027#include "models/gtkqtreemodel.h"
Stepan Salenikovichf6078222016-10-03 17:31:16 -040028#include "models/gtkqtreemodel.h"
Stepan Salenikovichc3fede22015-03-20 17:01:47 -040029#include "models/activeitemproxymodel.h"
Stepan Salenikovich61cbab02015-03-16 18:35:10 -040030#include "accountgeneraltab.h"
aviau6aeb4852016-08-18 16:01:09 -040031#include "accountcreationwizard.h"
Stepan Salenikovichb7e6a732015-05-04 17:13:22 -040032#include "accountadvancedtab.h"
Stepan Salenikovich7a1e71c2015-05-07 11:14:48 -040033#include "accountsecuritytab.h"
aviau6aeb4852016-08-18 16:01:09 -040034#include "accountdevicestab.h"
Stepan Salenikovichbd029582015-03-24 11:00:56 -040035#include "dialogs.h"
Stepan Salenikovichd6a9ba92015-03-29 11:14:36 -040036#include <glib/gprintf.h>
Stepan Salenikovich9816a942015-04-22 17:49:16 -040037#include "utils/models.h"
Nicolas Jager005fc552017-04-26 07:50:26 -040038#include "accountbanstab.h"
Stepan Salenikovich61cbab02015-03-16 18:35:10 -040039
Sébastien Blin276202f2018-05-11 09:37:34 -040040#include <api/newaccountmodel.h>
41
aviau6aeb4852016-08-18 16:01:09 -040042static constexpr const char* ACCOUNT_CREATION_WIZARD_VIEW_NAME = "account-creation-wizard";
43
Stepan Salenikovich61cbab02015-03-16 18:35:10 -040044struct _AccountView
45{
Stepan Salenikovichab0f5be2016-02-05 15:16:35 -050046 GtkPaned parent;
Stepan Salenikovich61cbab02015-03-16 18:35:10 -040047};
48
49struct _AccountViewClass
50{
Stepan Salenikovichab0f5be2016-02-05 15:16:35 -050051 GtkPanedClass parent_class;
Stepan Salenikovich61cbab02015-03-16 18:35:10 -040052};
53
54typedef struct _AccountViewPrivate AccountViewPrivate;
55
56struct _AccountViewPrivate
57{
Sébastien Blin276202f2018-05-11 09:37:34 -040058 AccountInfoPointer const *accountInfo_ = nullptr;
59 AccountInfoPointer selectedInfo_ = nullptr;
60
Stepan Salenikovich61cbab02015-03-16 18:35:10 -040061 GtkWidget *treeview_account_list;
62 GtkWidget *stack_account;
Stepan Salenikovichc3fede22015-03-20 17:01:47 -040063 GtkWidget *button_remove_account;
64 GtkWidget *button_add_account;
65 GtkWidget *combobox_account_type;
aviau69081842016-10-14 14:51:54 -040066 GtkWidget *account_creation_wizard;
Stepan Salenikovich61cbab02015-03-16 18:35:10 -040067
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -040068 gint current_page; /* keeps track of current notebook page displayed */
Stepan Salenikovichc3fede22015-03-20 17:01:47 -040069
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -040070 QMetaObject::Connection protocol_selection_changed;
Stepan Salenikovich08c4ab22017-04-26 14:27:24 -040071 QMetaObject::Connection account_selection_changed;
Stepan Salenikovich61cbab02015-03-16 18:35:10 -040072};
73
Stepan Salenikovichab0f5be2016-02-05 15:16:35 -050074G_DEFINE_TYPE_WITH_PRIVATE(AccountView, account_view, GTK_TYPE_PANED);
Stepan Salenikovich61cbab02015-03-16 18:35:10 -040075
76#define ACCOUNT_VIEW_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), ACCOUNT_VIEW_TYPE, AccountViewPrivate))
77
78static void
79account_view_dispose(GObject *object)
80{
81 AccountView *view = ACCOUNT_VIEW(object);
82 AccountViewPrivate *priv = ACCOUNT_VIEW_GET_PRIVATE(view);
83
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -040084 QObject::disconnect(priv->protocol_selection_changed);
Stepan Salenikovich08c4ab22017-04-26 14:27:24 -040085 QObject::disconnect(priv->account_selection_changed);
Stepan Salenikovich61cbab02015-03-16 18:35:10 -040086
87 G_OBJECT_CLASS(account_view_parent_class)->dispose(object);
88}
89
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -040090static void
Stepan Salenikovichc3fede22015-03-20 17:01:47 -040091update_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 Roguez5d1514b2015-10-22 15:55:31 -040095 AccountModel::instance().selectionModel()->setCurrentIndex(current, QItemSelectionModel::ClearAndSelect);
Stepan Salenikovichc3fede22015-03-20 17:01:47 -040096 else
Guillaume Roguez5d1514b2015-10-22 15:55:31 -040097 AccountModel::instance().selectionModel()->clearCurrentIndex();
Stepan Salenikovichc3fede22015-03-20 17:01:47 -040098}
99
Stepan Salenikovichd45e6392015-08-11 10:16:35 -0400100static GtkWidget *
101create_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 Salenikovichc3fede22015-03-20 17:01:47 -0400108static void
Stepan Salenikovichbdb0ecd2016-05-18 13:30:55 -0400109tab_selection_changed(G_GNUC_UNUSED GtkNotebook *notebook,
110 G_GNUC_UNUSED GtkWidget *page,
111 guint page_num,
112 AccountView *self)
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400113{
Stepan Salenikovichbdb0ecd2016-05-18 13:30:55 -0400114 g_return_if_fail(IS_ACCOUNT_VIEW(self));
115 AccountViewPrivate *priv = ACCOUNT_VIEW_GET_PRIVATE(self);
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400116
Stepan Salenikovichbdb0ecd2016-05-18 13:30:55 -0400117 priv->current_page = page_num;
118}
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400119
Stepan Salenikovichbdb0ecd2016-05-18 13:30:55 -0400120static void
121account_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 Salenikovich61cbab02015-03-16 18:35:10 -0400125
Stepan Salenikovichbdb0ecd2016-05-18 13:30:55 -0400126 auto old_view = gtk_stack_get_visible_child(GTK_STACK(priv->stack_account));
aviau6aeb4852016-08-18 16:01:09 -0400127 if(IS_ACCOUNT_CREATION_WIZARD(old_view))
128 {
Hugo Lefeuvre24356d42018-05-11 10:41:41 -0400129 // cancel account creation, this will call hide_account_creation_wizard via callback
130 account_creation_wizard_cancel(ACCOUNT_CREATION_WIZARD(old_view));
aviau6aeb4852016-08-18 16:01:09 -0400131 return;
132 }
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400133
134 QModelIndex account_idx = get_index_from_selection(selection);
135 if (!account_idx.isValid()) {
Hugo Lefeuvre24356d42018-05-11 10:41:41 -0400136 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 Salenikovich61cbab02015-03-16 18:35:10 -0400150 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 Salenikovichbdb0ecd2016-05-18 13:30:55 -0400154
Sébastien Blin1f92b4a2018-02-28 11:39:11 -0500155 /* cannot delete accounts */
Stepan Salenikovichbdb0ecd2016-05-18 13:30:55 -0400156 gtk_widget_set_sensitive(priv->button_remove_account, FALSE);
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400157 } else {
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400158 Account *account = AccountModel::instance().getAccountByModelIndex(account_idx);
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400159
160 /* build new account view */
161 GtkWidget *hbox_account = gtk_box_new(GTK_ORIENTATION_VERTICAL, 10);
aviaub135a692016-12-12 14:41:01 -0500162 gtk_widget_show(hbox_account);
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400163
164 /* create account notebook */
Stepan Salenikovichbdb0ecd2016-05-18 13:30:55 -0400165 auto account_notebook = gtk_notebook_new();
aviaub135a692016-12-12 14:41:01 -0500166 gtk_widget_show(account_notebook);
Stepan Salenikovichbdb0ecd2016-05-18 13:30:55 -0400167 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 Salenikovich61cbab02015-03-16 18:35:10 -0400170
Sébastien Blin276202f2018-05-11 09:37:34 -0400171 // 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();
174 priv->selectedInfo_ = &(*priv->accountInfo_)->accountModel->getAccountInfo(selectedId);
175
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400176 /* customize account view based on account */
Sébastien Blin276202f2018-05-11 09:37:34 -0400177 auto general_tab = create_scrolled_account_view(account_general_tab_new(account, priv->selectedInfo_));
aviaub135a692016-12-12 14:41:01 -0500178 gtk_widget_show(general_tab);
Stepan Salenikovichbdb0ecd2016-05-18 13:30:55 -0400179 gtk_notebook_append_page(GTK_NOTEBOOK(account_notebook),
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400180 general_tab,
Stepan Salenikovicha1b8cb32015-09-11 14:58:35 -0400181 gtk_label_new(C_("Account settings", "General")));
aviau2a231832016-12-12 12:27:38 -0500182
aviau9e4e84d2016-12-12 12:41:54 -0500183 if (account->protocol() == Account::Protocol::RING)
184 {
185 auto devices_tab = create_scrolled_account_view(account_devices_tab_new(account));
aviaub135a692016-12-12 14:41:01 -0500186 gtk_widget_show(devices_tab);
aviau9e4e84d2016-12-12 12:41:54 -0500187 gtk_notebook_append_page(GTK_NOTEBOOK(account_notebook),
188 devices_tab,
189 gtk_label_new(C_("Account settings", "Devices")));
190 }
Stepan Salenikovichd45e6392015-08-11 10:16:35 -0400191 auto security_tab = create_scrolled_account_view(account_security_tab_new(account));
aviaub135a692016-12-12 14:41:01 -0500192 gtk_widget_show(security_tab);
Stepan Salenikovichbdb0ecd2016-05-18 13:30:55 -0400193 gtk_notebook_append_page(GTK_NOTEBOOK(account_notebook),
Stepan Salenikovich7a1e71c2015-05-07 11:14:48 -0400194 security_tab,
Stepan Salenikovicha1b8cb32015-09-11 14:58:35 -0400195 gtk_label_new(C_("Account settings", "Security")));
aviau2a231832016-12-12 12:27:38 -0500196 auto advanced_tab = create_scrolled_account_view(account_advanced_tab_new(account));
aviaub135a692016-12-12 14:41:01 -0500197 gtk_widget_show(advanced_tab);
aviau6aeb4852016-08-18 16:01:09 -0400198 gtk_notebook_append_page(GTK_NOTEBOOK(account_notebook),
aviau2a231832016-12-12 12:27:38 -0500199 advanced_tab,
200 gtk_label_new(C_("Account settings", "Advanced")));
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400201
Nicolas Jager005fc552017-04-26 07:50:26 -0400202 if (account->protocol() == Account::Protocol::RING) {
203 auto banned_tab = create_scrolled_account_view(account_bans_tab_new(account));
204 gtk_widget_show(banned_tab);
205 gtk_notebook_append_page(GTK_NOTEBOOK(account_notebook),
206 banned_tab,
207 gtk_label_new(C_("List of banned peers", "Bans")));
208 }
209
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400210 /* set the tab displayed to the same as the prev account selected */
Stepan Salenikovichbdb0ecd2016-05-18 13:30:55 -0400211 gtk_notebook_set_current_page(GTK_NOTEBOOK(account_notebook), priv->current_page);
212 /* now connect to the tab changed signal */
213 g_signal_connect(account_notebook, "switch-page", G_CALLBACK(tab_selection_changed), self);
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400214
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400215 /* set the new account view as visible */
216 char *account_view_name = g_strdup_printf("%p_account", account);
217 gtk_stack_add_named(GTK_STACK(priv->stack_account), hbox_account, account_view_name);
218 gtk_stack_set_visible_child(GTK_STACK(priv->stack_account), hbox_account);
219 g_free(account_view_name);
Stepan Salenikovichbdb0ecd2016-05-18 13:30:55 -0400220
Sébastien Blin1f92b4a2018-02-28 11:39:11 -0500221 /* can delete accounts */
Stepan Salenikovichbdb0ecd2016-05-18 13:30:55 -0400222 gtk_widget_set_sensitive(priv->button_remove_account, TRUE);
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400223 }
224
Stepan Salenikovichbdb0ecd2016-05-18 13:30:55 -0400225 /* remove the old view */
226 if (old_view)
227 gtk_container_remove(GTK_CONTAINER(priv->stack_account), old_view);
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400228}
229
230static void
231account_active_toggled(GtkCellRendererToggle *renderer, gchar *path, AccountView *view)
232{
233 g_return_if_fail(IS_ACCOUNT_VIEW(view));
234 AccountViewPrivate *priv = ACCOUNT_VIEW_GET_PRIVATE(view);
235
236 /* we want to set it to the opposite of the current value */
237 gboolean toggle = !gtk_cell_renderer_toggle_get_active(renderer);
238
239 /* get iter which was clicked */
240 GtkTreePath *tree_path = gtk_tree_path_new_from_string(path);
241 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(priv->treeview_account_list));
242 GtkTreeIter iter;
243 gtk_tree_model_get_iter(model, &iter, tree_path);
244
245 /* get qmodelindex from iter and set the model data */
246 QModelIndex idx = gtk_q_tree_model_get_source_idx(GTK_Q_TREE_MODEL(model), &iter);
247 if (idx.isValid()) {
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400248 QVariant alias = idx.data(static_cast<int>(Account::Role::Alias));
Stepan Salenikovichfea84052016-05-13 16:32:51 -0400249 AccountModel::instance().setData(idx, QVariant(toggle), Qt::CheckStateRole);
250 /* save the account to apply the changed state right away */
251 AccountModel::instance().getAccountByModelIndex(idx)->performAction(Account::EditAction::SAVE);
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400252 }
253}
254
Stepan Salenikovichc3fede22015-03-20 17:01:47 -0400255static gboolean
Stepan Salenikovichbd029582015-03-24 11:00:56 -0400256remove_account_dialog(AccountView *view, Account *account)
Stepan Salenikovichc3fede22015-03-20 17:01:47 -0400257{
258 gboolean response = FALSE;
259 GtkWidget *dialog = gtk_message_dialog_new(NULL,
260 (GtkDialogFlags)(GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT),
261 GTK_MESSAGE_QUESTION, GTK_BUTTONS_OK_CANCEL,
Stepan Salenikovicha1b8cb32015-09-11 14:58:35 -0400262 _("Are you sure you want to delete account \"%s\"?"),
Stepan Salenikovichc3fede22015-03-20 17:01:47 -0400263 account->alias().toLocal8Bit().constData());
264
Stepan Salenikovichbd029582015-03-24 11:00:56 -0400265 gtk_window_set_destroy_with_parent(GTK_WINDOW(dialog), TRUE);
266
267 /* get parent window so we can center on it */
268 GtkWidget *parent = gtk_widget_get_toplevel(GTK_WIDGET(view));
269 if (gtk_widget_is_toplevel(parent)) {
270 gtk_window_set_transient_for(GTK_WINDOW(dialog), GTK_WINDOW(parent));
271 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER_ON_PARENT);
272 }
273
Stepan Salenikovichc3fede22015-03-20 17:01:47 -0400274 switch (gtk_dialog_run(GTK_DIALOG(dialog))) {
275 case GTK_RESPONSE_OK:
276 response = TRUE;
277 break;
278 default:
279 response = FALSE;
280 break;
281 }
282
283 gtk_widget_destroy(dialog);
284
285 return response;
286}
287
Stepan Salenikovichbd029582015-03-24 11:00:56 -0400288static gboolean
289save_account(GtkWidget *working_dialog)
290{
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400291 AccountModel::instance().save();
Stepan Salenikovichbd029582015-03-24 11:00:56 -0400292 if (working_dialog)
293 gtk_widget_destroy(working_dialog);
294
295 return G_SOURCE_REMOVE;
296}
297
Stepan Salenikovichc3fede22015-03-20 17:01:47 -0400298static void
299remove_account(G_GNUC_UNUSED GtkWidget *entry, AccountView *view)
300{
301 g_return_if_fail(IS_ACCOUNT_VIEW(view));
302 AccountViewPrivate *priv = ACCOUNT_VIEW_GET_PRIVATE(view);
303
304 GtkTreeSelection *selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_account_list));
305 QModelIndex idx = get_index_from_selection(selection);
306
307 if (idx.isValid()) {
308 /* this is a destructive operation, ask the user if they are sure */
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400309 Account *account = AccountModel::instance().getAccountByModelIndex(idx);
Stepan Salenikovichbd029582015-03-24 11:00:56 -0400310 if (remove_account_dialog(view, account)) {
311 /* show working dialog in case save operation takes time */
312 GtkWidget *working = ring_dialog_working(GTK_WIDGET(view), NULL);
313 gtk_window_present(GTK_WINDOW(working));
314
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400315 AccountModel::instance().remove(idx);
Stepan Salenikovichbd029582015-03-24 11:00:56 -0400316
317 /* now save the time it takes to transition the account view to the new account (300ms)
318 * the save doesn't happen before the "working" dialog is presented
319 * the timeout function should destroy the "working" dialog when done saving
320 */
Stepan Salenikovich12fee942015-03-25 18:38:47 -0400321 g_timeout_add_full(G_PRIORITY_DEFAULT, 300, (GSourceFunc)save_account, working, NULL);
Stepan Salenikovichc3fede22015-03-20 17:01:47 -0400322 }
323 }
324}
325
326static void
aviau6aeb4852016-08-18 16:01:09 -0400327hide_account_creation_wizard(AccountView *view)
328{
329 g_return_if_fail(IS_ACCOUNT_VIEW(view));
330 AccountViewPrivate *priv = ACCOUNT_VIEW_GET_PRIVATE(view);
331
332 auto old_view = gtk_stack_get_visible_child(GTK_STACK(priv->stack_account));
333 if (IS_ACCOUNT_CREATION_WIZARD(old_view))
334 {
Hugo Lefeuvre24356d42018-05-11 10:41:41 -0400335 account_creation_wizard_show_preview(ACCOUNT_CREATION_WIZARD(old_view), FALSE);
aviau6aeb4852016-08-18 16:01:09 -0400336 gtk_container_remove(GTK_CONTAINER(priv->stack_account), old_view);
337 }
338
339 auto selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_account_list));
340 account_selection_changed(selection, view);
341}
342
343static void
344show_account_creation_wizard(AccountView *view)
345{
346 g_return_if_fail(IS_ACCOUNT_VIEW(view));
347 AccountViewPrivate *priv = ACCOUNT_VIEW_GET_PRIVATE(view);
348
349 auto old_view = gtk_stack_get_visible_child(GTK_STACK(priv->stack_account));
350
aviau69081842016-10-14 14:51:54 -0400351 if (!priv->account_creation_wizard)
aviau6aeb4852016-08-18 16:01:09 -0400352 {
aviau69081842016-10-14 14:51:54 -0400353 priv->account_creation_wizard = account_creation_wizard_new(true);
354 g_object_add_weak_pointer(G_OBJECT(priv->account_creation_wizard), (gpointer *)&priv->account_creation_wizard);
aviau6aeb4852016-08-18 16:01:09 -0400355 gtk_stack_add_named(GTK_STACK(priv->stack_account),
aviau69081842016-10-14 14:51:54 -0400356 priv->account_creation_wizard,
aviau6aeb4852016-08-18 16:01:09 -0400357 ACCOUNT_CREATION_WIZARD_VIEW_NAME);
aviau69081842016-10-14 14:51:54 -0400358 g_signal_connect_swapped(priv->account_creation_wizard, "account-creation-completed", G_CALLBACK(hide_account_creation_wizard), view);
359 g_signal_connect_swapped(priv->account_creation_wizard, "account-creation-canceled", G_CALLBACK(hide_account_creation_wizard), view);
aviau6aeb4852016-08-18 16:01:09 -0400360 }
361
aviau69081842016-10-14 14:51:54 -0400362 gtk_widget_show(priv->account_creation_wizard);
aviau6aeb4852016-08-18 16:01:09 -0400363
364 gtk_stack_set_visible_child_name(GTK_STACK(priv->stack_account), ACCOUNT_CREATION_WIZARD_VIEW_NAME);
365
366 /* remove the old view */
367 if (old_view)
368 gtk_container_remove(GTK_CONTAINER(priv->stack_account), old_view);
369}
370
371static void
Stepan Salenikovichc3fede22015-03-20 17:01:47 -0400372add_account(G_GNUC_UNUSED GtkWidget *entry, AccountView *view)
373{
374 g_return_if_fail(IS_ACCOUNT_VIEW(view));
375 AccountViewPrivate *priv = ACCOUNT_VIEW_GET_PRIVATE(view);
376
Stepan Salenikovich08c4ab22017-04-26 14:27:24 -0400377 auto idx = gtk_combo_box_get_index(GTK_COMBO_BOX(priv->combobox_account_type));
378 if (idx.isValid()) {
379 Account::Protocol protocol = qvariant_cast<Account::Protocol>(idx.data((int)ProtocolModel::Role::Protocol));
380 if (protocol == Account::Protocol::RING)
381 {
Hugo Lefeuvre24356d42018-05-11 10:41:41 -0400382 auto old_view = gtk_stack_get_visible_child(GTK_STACK(priv->stack_account));
383 if (IS_ACCOUNT_CREATION_WIZARD(old_view))
384 {
385 account_creation_wizard_cancel(ACCOUNT_CREATION_WIZARD(old_view));
386 } else {
387 show_account_creation_wizard(view);
388 }
Stepan Salenikovich08c4ab22017-04-26 14:27:24 -0400389 }
390 else
391 {
392 /* show working dialog in case save operation takes time */
393 GtkWidget *working = ring_dialog_working(GTK_WIDGET(view), NULL);
394 gtk_window_present(GTK_WINDOW(working));
Stepan Salenikovichbd029582015-03-24 11:00:56 -0400395
Stepan Salenikovich08c4ab22017-04-26 14:27:24 -0400396 AccountModel::instance().add(QString(_("New Account")), idx);
Stepan Salenikovichbd029582015-03-24 11:00:56 -0400397
Stepan Salenikovich08c4ab22017-04-26 14:27:24 -0400398 /* now save after a short timeout to make sure that
399 * the save doesn't happen before the "working" dialog is presented
400 * the timeout function should destroy the "working" dialog when done saving
401 */
402 g_timeout_add_full(G_PRIORITY_DEFAULT, 300, (GSourceFunc)save_account, working, NULL);
Stepan Salenikovichc3fede22015-03-20 17:01:47 -0400403 }
404 }
405}
406
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400407static void
Stepan Salenikovichd6a9ba92015-03-29 11:14:36 -0400408state_to_string(G_GNUC_UNUSED GtkTreeViewColumn *tree_column,
409 GtkCellRenderer *cell,
410 GtkTreeModel *tree_model,
411 GtkTreeIter *iter,
Stepan Salenikoviche4981b22015-10-22 15:22:59 -0400412 GtkTreeView *treeview)
Stepan Salenikovichd6a9ba92015-03-29 11:14:36 -0400413{
Stepan Salenikoviche4981b22015-10-22 15:22:59 -0400414 // check if this iter is selected
415 gboolean is_selected = FALSE;
416 if (GTK_IS_TREE_VIEW(treeview)) {
417 auto selection = gtk_tree_view_get_selection(treeview);
418 is_selected = gtk_tree_selection_iter_is_selected(selection, iter);
419 }
420
Stepan Salenikovichd6a9ba92015-03-29 11:14:36 -0400421 gchar *display_state = NULL;
Stepan Salenikoviche7a73142015-08-11 11:55:59 -0400422
423 /* get account */
424 QModelIndex idx = gtk_q_tree_model_get_source_idx(GTK_Q_TREE_MODEL(tree_model), iter);
425 if (idx.isValid()) {
426
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400427 auto account = AccountModel::instance().getAccountByModelIndex(idx);
Stepan Salenikoviche7a73142015-08-11 11:55:59 -0400428 auto humanState = account->toHumanStateName();
429
Stepan Salenikovich0731de02016-05-17 17:47:16 -0400430 auto escaped_state = g_markup_escape_text(humanState.toUtf8().constData(), -1);
Stepan Salenikoviche4981b22015-10-22 15:22:59 -0400431 /* we want the color of the status text to be the default color if this iter is
432 * selected so that the treeview is able to invert it against the selection color */
433 if (is_selected) {
Stepan Salenikovich0731de02016-05-17 17:47:16 -0400434 display_state = escaped_state;
Stepan Salenikoviche4981b22015-10-22 15:22:59 -0400435 } else {
436 switch (account->registrationState()) {
437 case Account::RegistrationState::READY:
Stepan Salenikovich0731de02016-05-17 17:47:16 -0400438 display_state = g_strdup_printf("<span fgcolor=\"green\">%s</span>", escaped_state);
Stepan Salenikoviche4981b22015-10-22 15:22:59 -0400439 break;
440 case Account::RegistrationState::UNREGISTERED:
Stepan Salenikovich0731de02016-05-17 17:47:16 -0400441 display_state = g_strdup_printf("<span fgcolor=\"gray\">%s</span>", escaped_state);
Stepan Salenikoviche4981b22015-10-22 15:22:59 -0400442 break;
443 case Account::RegistrationState::TRYING:
aviau6aeb4852016-08-18 16:01:09 -0400444 case Account::RegistrationState::INITIALIZING:
Stepan Salenikovich0731de02016-05-17 17:47:16 -0400445 display_state = g_strdup_printf("<span fgcolor=\"orange\">%s</span>", escaped_state);
Stepan Salenikoviche4981b22015-10-22 15:22:59 -0400446 break;
447 case Account::RegistrationState::ERROR:
Stepan Salenikovich0731de02016-05-17 17:47:16 -0400448 display_state = g_strdup_printf("<span fgcolor=\"red\">%s</span>", escaped_state);
Stepan Salenikoviche4981b22015-10-22 15:22:59 -0400449 break;
450 case Account::RegistrationState::COUNT__:
451 g_warning("registration state should never be \"count\"");
Stepan Salenikovich0731de02016-05-17 17:47:16 -0400452 display_state = g_strdup_printf("<span fgcolor=\"red\">%s</span>", escaped_state);
Stepan Salenikoviche4981b22015-10-22 15:22:59 -0400453 break;
454 }
Stepan Salenikovich0731de02016-05-17 17:47:16 -0400455 g_free(escaped_state);
Stepan Salenikoviche7a73142015-08-11 11:55:59 -0400456 }
Stepan Salenikovichd6a9ba92015-03-29 11:14:36 -0400457 }
Stepan Salenikoviche7a73142015-08-11 11:55:59 -0400458
Stepan Salenikovichd6a9ba92015-03-29 11:14:36 -0400459 g_object_set(G_OBJECT(cell), "markup", display_state, NULL);
460 g_free(display_state);
461}
462
463static void
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400464account_view_init(AccountView *view)
465{
466 gtk_widget_init_template(GTK_WIDGET(view));
467
468 AccountViewPrivate *priv = ACCOUNT_VIEW_GET_PRIVATE(view);
469
470 /* account model */
471 GtkQTreeModel *account_model;
472 GtkCellRenderer *renderer;
473 GtkTreeViewColumn *column;
474
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400475 account_model = gtk_q_tree_model_new(&AccountModel::instance(), 4,
aviau9bbb19b2016-05-16 15:53:44 -0400476 0, Account::Role::Enabled, G_TYPE_BOOLEAN,
477 0, Account::Role::Alias, G_TYPE_STRING,
478 0, Account::Role::Proto, G_TYPE_STRING,
479 0, Account::Role::RegistrationState, G_TYPE_UINT);
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400480 gtk_tree_view_set_model(GTK_TREE_VIEW(priv->treeview_account_list), GTK_TREE_MODEL(account_model));
481
482 renderer = gtk_cell_renderer_toggle_new();
Stepan Salenikovicha1b8cb32015-09-11 14:58:35 -0400483 column = gtk_tree_view_column_new_with_attributes(C_("Account state column", "Enabled"), renderer, "active", 0, NULL);
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400484 gtk_tree_view_append_column(GTK_TREE_VIEW(priv->treeview_account_list), column);
485
486 g_signal_connect(renderer, "toggled", G_CALLBACK(account_active_toggled), view);
487
488 renderer = gtk_cell_renderer_text_new();
Stepan Salenikovicha1b8cb32015-09-11 14:58:35 -0400489 column = gtk_tree_view_column_new_with_attributes(C_("Account alias (name) column", "Alias"), renderer, "text", 1, NULL);
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400490 gtk_tree_view_append_column(GTK_TREE_VIEW(priv->treeview_account_list), column);
Stepan Salenikovichab0f5be2016-02-05 15:16:35 -0500491 g_object_set(G_OBJECT(renderer), "ellipsize", PANGO_ELLIPSIZE_END, NULL);
492 gtk_tree_view_column_set_expand(column, TRUE);
493 // set a min width so most of the account name is visible
494 g_object_set(G_OBJECT(renderer), "width", 75, NULL);
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400495
496 renderer = gtk_cell_renderer_text_new();
Stepan Salenikovicha1b8cb32015-09-11 14:58:35 -0400497 column = gtk_tree_view_column_new_with_attributes(C_("Account status column", "Status"), renderer, "text", 3, NULL);
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400498 gtk_tree_view_append_column(GTK_TREE_VIEW(priv->treeview_account_list), column);
Stepan Salenikovichab0f5be2016-02-05 15:16:35 -0500499 g_object_set(G_OBJECT(renderer), "ellipsize", PANGO_ELLIPSIZE_END, NULL);
500 gtk_tree_view_column_set_expand(column, TRUE);
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400501
Stepan Salenikovichd6a9ba92015-03-29 11:14:36 -0400502 /* the registration state is an enum, we want to display it as a string */
503 gtk_tree_view_column_set_cell_data_func(
504 column,
505 renderer,
506 (GtkTreeCellDataFunc)state_to_string,
Stepan Salenikoviche4981b22015-10-22 15:22:59 -0400507 priv->treeview_account_list,
Stepan Salenikovichd6a9ba92015-03-29 11:14:36 -0400508 NULL);
509
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400510 /* add an empty box to the account stack initially, otherwise there will
511 * be no cool animation when the first account is selected */
512 GtkWidget *empty_box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
513 gtk_widget_show(empty_box);
514 gtk_stack_add_named(GTK_STACK(priv->stack_account), empty_box, "placeholder");
515 gtk_stack_set_visible_child(GTK_STACK(priv->stack_account), empty_box);
Stepan Salenikovichc3fede22015-03-20 17:01:47 -0400516
517 /* populate account type combo box */
Stepan Salenikovich08c4ab22017-04-26 14:27:24 -0400518 priv->protocol_selection_changed = gtk_combo_box_set_qmodel_text(
519 GTK_COMBO_BOX(priv->combobox_account_type),
520 AccountModel::instance().protocolModel(),
521 AccountModel::instance().protocolModel()->selectionModel()
522 );
Stepan Salenikovichc3fede22015-03-20 17:01:47 -0400523
524 /* connect signals to and from the selection model of the account model */
Stepan Salenikovich08c4ab22017-04-26 14:27:24 -0400525 priv->account_selection_changed = QObject::connect(
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400526 AccountModel::instance().selectionModel(),
Stepan Salenikovichc3fede22015-03-20 17:01:47 -0400527 &QItemSelectionModel::currentChanged,
Stepan Salenikovich825bb0a2016-03-11 15:33:52 -0500528 [=](const QModelIndex & current, G_GNUC_UNUSED const QModelIndex & previous) {
Stepan Salenikovichc3fede22015-03-20 17:01:47 -0400529 GtkTreeSelection *selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_account_list));
530
Stepan Salenikovichc3fede22015-03-20 17:01:47 -0400531 /* select the current */
532 if (current.isValid()) {
533 GtkTreeIter new_iter;
534 if (gtk_q_tree_model_source_index_to_iter(account_model, current, &new_iter)) {
535 gtk_tree_selection_select_iter(selection, &new_iter);
536 } else {
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -0400537 g_warning("SelectionModel of AccountModel changed to invalid QModelIndex?");
Stepan Salenikovichc3fede22015-03-20 17:01:47 -0400538 }
539 }
540 }
541 );
542
543 GtkTreeSelection *account_selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_account_list));
544 g_signal_connect(account_selection, "changed", G_CALLBACK(update_account_model_selection), NULL);
545 g_signal_connect(account_selection, "changed", G_CALLBACK(account_selection_changed), view);
546
Stepan Salenikovichc3fede22015-03-20 17:01:47 -0400547 /* connect signals to add/remove accounts */
548 g_signal_connect(priv->button_remove_account, "clicked", G_CALLBACK(remove_account), view);
549 g_signal_connect(priv->button_add_account, "clicked", G_CALLBACK(add_account), view);
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400550}
551
552static void
553account_view_class_init(AccountViewClass *klass)
554{
555 G_OBJECT_CLASS(klass)->dispose = account_view_dispose;
556
557 gtk_widget_class_set_template_from_resource(GTK_WIDGET_CLASS (klass),
558 "/cx/ring/RingGnome/accountview.ui");
559
560 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), AccountView, treeview_account_list);
561 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), AccountView, stack_account);
Stepan Salenikovichc3fede22015-03-20 17:01:47 -0400562 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), AccountView, button_remove_account);
563 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), AccountView, button_add_account);
564 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), AccountView, combobox_account_type);
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400565}
566
567GtkWidget *
Sébastien Blin276202f2018-05-11 09:37:34 -0400568account_view_new(AccountInfoPointer const & accountInfo)
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400569{
Sébastien Blin276202f2018-05-11 09:37:34 -0400570 auto* view = g_object_new(ACCOUNT_VIEW_TYPE, NULL);
571 AccountViewPrivate *priv = ACCOUNT_VIEW_GET_PRIVATE(view);
572 priv->accountInfo_ = &accountInfo;
573 return (GtkWidget *)view;
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400574}
Hugo Lefeuvre24356d42018-05-11 10:41:41 -0400575
576void
577account_settings_view_show(AccountView *view, gboolean show) {
578 AccountViewPrivate *priv = ACCOUNT_VIEW_GET_PRIVATE(view);
579
580 /* If displayed stack element is still the placeholder element, thats is it hasn't
581 been initialized yet, change account selection. */
582 if (show && strcmp(gtk_stack_get_visible_child_name (GTK_STACK(priv->stack_account)), "placeholder") == 0) {
583 auto selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_account_list));
584 account_selection_changed(selection, view);
585 return;
586 }
587
588 /* If account creation wizard is currently open and settings get closed, cancel so
589 the preview is stopped properly. */
590 auto old_view = gtk_stack_get_visible_child(GTK_STACK(priv->stack_account));
591 if (!show && IS_ACCOUNT_CREATION_WIZARD(old_view)) {
592 account_creation_wizard_cancel(ACCOUNT_CREATION_WIZARD(old_view));
593 }
594}