blob: 30ef947206d32cffc78b74c074084a190da06242 [file] [log] [blame]
Stepan Salenikovich61cbab02015-03-16 18:35:10 -04001/*
Stepan Salenikovichbe87d2c2016-01-25 14:14:34 -05002 * Copyright (C) 2015-2016 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>
Edric Milaret58e71072016-03-21 12:16:37 -040025#include <codecmodel.h>
Stepan Salenikovichc3fede22015-03-20 17:01:47 -040026#include <protocolmodel.h>
27#include <QtCore/QItemSelectionModel>
Stepan Salenikovich61cbab02015-03-16 18:35:10 -040028#include "models/gtkqtreemodel.h"
Stepan Salenikovichc3fede22015-03-20 17:01:47 -040029#include "models/gtkqsortfiltertreemodel.h"
30#include "models/activeitemproxymodel.h"
Stepan Salenikovich61cbab02015-03-16 18:35:10 -040031#include "accountgeneraltab.h"
32#include "accountaudiotab.h"
33#include "accountvideotab.h"
Stepan Salenikovichb7e6a732015-05-04 17:13:22 -040034#include "accountadvancedtab.h"
Stepan Salenikovich7a1e71c2015-05-07 11:14:48 -040035#include "accountsecuritytab.h"
Stepan Salenikovichbd029582015-03-24 11:00:56 -040036#include "dialogs.h"
Stepan Salenikovichd6a9ba92015-03-29 11:14:36 -040037#include <glib/gprintf.h>
Stepan Salenikovich9816a942015-04-22 17:49:16 -040038#include "utils/models.h"
Stepan Salenikovichbdb0ecd2016-05-18 13:30:55 -040039#include "accountimportexportview.h"
Stepan Salenikovich61cbab02015-03-16 18:35:10 -040040
41struct _AccountView
42{
Stepan Salenikovichab0f5be2016-02-05 15:16:35 -050043 GtkPaned parent;
Stepan Salenikovich61cbab02015-03-16 18:35:10 -040044};
45
46struct _AccountViewClass
47{
Stepan Salenikovichab0f5be2016-02-05 15:16:35 -050048 GtkPanedClass parent_class;
Stepan Salenikovich61cbab02015-03-16 18:35:10 -040049};
50
51typedef struct _AccountViewPrivate AccountViewPrivate;
52
53struct _AccountViewPrivate
54{
55 GtkWidget *treeview_account_list;
Stepan Salenikovichbdb0ecd2016-05-18 13:30:55 -040056 GtkWidget *account_import_view;
57 GtkWidget *account_export_view;
Stepan Salenikovich61cbab02015-03-16 18:35:10 -040058 GtkWidget *stack_account;
Stepan Salenikovichc3fede22015-03-20 17:01:47 -040059 GtkWidget *button_remove_account;
60 GtkWidget *button_add_account;
61 GtkWidget *combobox_account_type;
Stepan Salenikovichbdb0ecd2016-05-18 13:30:55 -040062 GtkWidget *button_import_account;
63 GtkWidget *button_export_account;
Stepan Salenikovich61cbab02015-03-16 18:35:10 -040064
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -040065 gint current_page; /* keeps track of current notebook page displayed */
Stepan Salenikovichc3fede22015-03-20 17:01:47 -040066
67 ActiveItemProxyModel *active_protocols;
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -040068 QMetaObject::Connection protocol_selection_changed;
Stepan Salenikovich61cbab02015-03-16 18:35:10 -040069};
70
Stepan Salenikovichab0f5be2016-02-05 15:16:35 -050071G_DEFINE_TYPE_WITH_PRIVATE(AccountView, account_view, GTK_TYPE_PANED);
Stepan Salenikovich61cbab02015-03-16 18:35:10 -040072
73#define ACCOUNT_VIEW_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), ACCOUNT_VIEW_TYPE, AccountViewPrivate))
74
75static void
76account_view_dispose(GObject *object)
77{
78 AccountView *view = ACCOUNT_VIEW(object);
79 AccountViewPrivate *priv = ACCOUNT_VIEW_GET_PRIVATE(view);
80
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -040081 QObject::disconnect(priv->protocol_selection_changed);
Stepan Salenikovich61cbab02015-03-16 18:35:10 -040082
83 G_OBJECT_CLASS(account_view_parent_class)->dispose(object);
84}
85
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -040086static void
87account_view_finalize(GObject *object)
88{
89 AccountView *view = ACCOUNT_VIEW(object);
90 AccountViewPrivate *priv = ACCOUNT_VIEW_GET_PRIVATE(view);
91
92 delete priv->active_protocols;
93
94 G_OBJECT_CLASS(account_view_parent_class)->finalize(object);
95}
96
Stepan Salenikovich61cbab02015-03-16 18:35:10 -040097static void
Stepan Salenikovichc3fede22015-03-20 17:01:47 -040098update_account_model_selection(GtkTreeSelection *selection, G_GNUC_UNUSED gpointer user_data)
99{
100 QModelIndex current = get_index_from_selection(selection);
101 if (current.isValid())
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400102 AccountModel::instance().selectionModel()->setCurrentIndex(current, QItemSelectionModel::ClearAndSelect);
Stepan Salenikovichc3fede22015-03-20 17:01:47 -0400103 else
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400104 AccountModel::instance().selectionModel()->clearCurrentIndex();
Stepan Salenikovichc3fede22015-03-20 17:01:47 -0400105}
106
Stepan Salenikovichd45e6392015-08-11 10:16:35 -0400107static GtkWidget *
108create_scrolled_account_view(GtkWidget *account_view)
109{
110 auto scrolled = gtk_scrolled_window_new(NULL, NULL);
111 gtk_container_add(GTK_CONTAINER(scrolled), account_view);
112 return scrolled;
113}
114
Stepan Salenikovichc3fede22015-03-20 17:01:47 -0400115static void
Stepan Salenikovichbdb0ecd2016-05-18 13:30:55 -0400116tab_selection_changed(G_GNUC_UNUSED GtkNotebook *notebook,
117 G_GNUC_UNUSED GtkWidget *page,
118 guint page_num,
119 AccountView *self)
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400120{
Stepan Salenikovichbdb0ecd2016-05-18 13:30:55 -0400121 g_return_if_fail(IS_ACCOUNT_VIEW(self));
122 AccountViewPrivate *priv = ACCOUNT_VIEW_GET_PRIVATE(self);
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400123
Stepan Salenikovichbdb0ecd2016-05-18 13:30:55 -0400124 priv->current_page = page_num;
125}
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400126
Stepan Salenikovichbdb0ecd2016-05-18 13:30:55 -0400127static void
128account_selection_changed(GtkTreeSelection *selection, AccountView *self)
129{
130 g_return_if_fail(IS_ACCOUNT_VIEW(self));
131 AccountViewPrivate *priv = ACCOUNT_VIEW_GET_PRIVATE(self);
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400132
Stepan Salenikovichbdb0ecd2016-05-18 13:30:55 -0400133 auto old_view = gtk_stack_get_visible_child(GTK_STACK(priv->stack_account));
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400134
135 QModelIndex account_idx = get_index_from_selection(selection);
136 if (!account_idx.isValid()) {
137 /* it nothing is slected, simply display something empty */
138 GtkWidget *empty_box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
139 gtk_widget_show(empty_box);
140 gtk_stack_add_named(GTK_STACK(priv->stack_account), empty_box, "placeholder");
141 gtk_stack_set_visible_child(GTK_STACK(priv->stack_account), empty_box);
Stepan Salenikovichbdb0ecd2016-05-18 13:30:55 -0400142
143 /* cannot delete nor export accounts */
144 gtk_widget_set_sensitive(priv->button_remove_account, FALSE);
145 gtk_widget_set_sensitive(priv->button_export_account, FALSE);
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400146 } else {
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400147 Account *account = AccountModel::instance().getAccountByModelIndex(account_idx);
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400148
149 /* build new account view */
150 GtkWidget *hbox_account = gtk_box_new(GTK_ORIENTATION_VERTICAL, 10);
151
152 /* create account notebook */
Stepan Salenikovichbdb0ecd2016-05-18 13:30:55 -0400153 auto account_notebook = gtk_notebook_new();
154 gtk_notebook_set_scrollable(GTK_NOTEBOOK(account_notebook), TRUE);
155 gtk_notebook_set_show_border(GTK_NOTEBOOK(account_notebook), FALSE);
156 gtk_box_pack_start(GTK_BOX(hbox_account), account_notebook, TRUE, TRUE, 0);
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400157
158 /* customize account view based on account */
Stepan Salenikovichd45e6392015-08-11 10:16:35 -0400159 auto general_tab = create_scrolled_account_view(account_general_tab_new(account));
Stepan Salenikovichbdb0ecd2016-05-18 13:30:55 -0400160 gtk_notebook_append_page(GTK_NOTEBOOK(account_notebook),
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400161 general_tab,
Stepan Salenikovicha1b8cb32015-09-11 14:58:35 -0400162 gtk_label_new(C_("Account settings", "General")));
Stepan Salenikovichd45e6392015-08-11 10:16:35 -0400163 auto audio_tab = create_scrolled_account_view(account_audio_tab_new(account));
Stepan Salenikovichbdb0ecd2016-05-18 13:30:55 -0400164 gtk_notebook_append_page(GTK_NOTEBOOK(account_notebook),
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400165 audio_tab,
Stepan Salenikovicha1b8cb32015-09-11 14:58:35 -0400166 gtk_label_new(C_("Account settings", "Audio")));
Stepan Salenikovichd45e6392015-08-11 10:16:35 -0400167 auto video_tab = create_scrolled_account_view(account_video_tab_new(account));
Stepan Salenikovichbdb0ecd2016-05-18 13:30:55 -0400168 gtk_notebook_append_page(GTK_NOTEBOOK(account_notebook),
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400169 video_tab,
Stepan Salenikovicha1b8cb32015-09-11 14:58:35 -0400170 gtk_label_new(C_("Account settings", "Video")));
Stepan Salenikovichd45e6392015-08-11 10:16:35 -0400171 auto advanced_tab = create_scrolled_account_view(account_advanced_tab_new(account));
Stepan Salenikovichbdb0ecd2016-05-18 13:30:55 -0400172 gtk_notebook_append_page(GTK_NOTEBOOK(account_notebook),
Stepan Salenikovichb7e6a732015-05-04 17:13:22 -0400173 advanced_tab,
Stepan Salenikovicha1b8cb32015-09-11 14:58:35 -0400174 gtk_label_new(C_("Account settings", "Advanced")));
Stepan Salenikovichd45e6392015-08-11 10:16:35 -0400175 auto security_tab = create_scrolled_account_view(account_security_tab_new(account));
Stepan Salenikovichbdb0ecd2016-05-18 13:30:55 -0400176 gtk_notebook_append_page(GTK_NOTEBOOK(account_notebook),
Stepan Salenikovich7a1e71c2015-05-07 11:14:48 -0400177 security_tab,
Stepan Salenikovicha1b8cb32015-09-11 14:58:35 -0400178 gtk_label_new(C_("Account settings", "Security")));
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400179
Stepan Salenikovichad589082016-05-18 13:22:52 -0400180 gtk_widget_show_all(hbox_account);
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400181 /* set the tab displayed to the same as the prev account selected */
Stepan Salenikovichbdb0ecd2016-05-18 13:30:55 -0400182 gtk_notebook_set_current_page(GTK_NOTEBOOK(account_notebook), priv->current_page);
183 /* now connect to the tab changed signal */
184 g_signal_connect(account_notebook, "switch-page", G_CALLBACK(tab_selection_changed), self);
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400185
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400186 /* set the new account view as visible */
187 char *account_view_name = g_strdup_printf("%p_account", account);
188 gtk_stack_add_named(GTK_STACK(priv->stack_account), hbox_account, account_view_name);
189 gtk_stack_set_visible_child(GTK_STACK(priv->stack_account), hbox_account);
190 g_free(account_view_name);
Stepan Salenikovichbdb0ecd2016-05-18 13:30:55 -0400191
192 /* can delete and export accounts */
193 gtk_widget_set_sensitive(priv->button_remove_account, TRUE);
194 gtk_widget_set_sensitive(priv->button_export_account, TRUE);
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400195 }
196
Stepan Salenikovichbdb0ecd2016-05-18 13:30:55 -0400197 /* remove the old view */
198 if (old_view)
199 gtk_container_remove(GTK_CONTAINER(priv->stack_account), old_view);
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400200}
201
202static void
203account_active_toggled(GtkCellRendererToggle *renderer, gchar *path, AccountView *view)
204{
205 g_return_if_fail(IS_ACCOUNT_VIEW(view));
206 AccountViewPrivate *priv = ACCOUNT_VIEW_GET_PRIVATE(view);
207
208 /* we want to set it to the opposite of the current value */
209 gboolean toggle = !gtk_cell_renderer_toggle_get_active(renderer);
210
211 /* get iter which was clicked */
212 GtkTreePath *tree_path = gtk_tree_path_new_from_string(path);
213 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(priv->treeview_account_list));
214 GtkTreeIter iter;
215 gtk_tree_model_get_iter(model, &iter, tree_path);
216
217 /* get qmodelindex from iter and set the model data */
218 QModelIndex idx = gtk_q_tree_model_get_source_idx(GTK_Q_TREE_MODEL(model), &iter);
219 if (idx.isValid()) {
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400220 QVariant alias = idx.data(static_cast<int>(Account::Role::Alias));
Stepan Salenikovichfea84052016-05-13 16:32:51 -0400221 AccountModel::instance().setData(idx, QVariant(toggle), Qt::CheckStateRole);
222 /* save the account to apply the changed state right away */
223 AccountModel::instance().getAccountByModelIndex(idx)->performAction(Account::EditAction::SAVE);
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400224 }
225}
226
Stepan Salenikovichc3fede22015-03-20 17:01:47 -0400227static gboolean
Stepan Salenikovichbd029582015-03-24 11:00:56 -0400228remove_account_dialog(AccountView *view, Account *account)
Stepan Salenikovichc3fede22015-03-20 17:01:47 -0400229{
230 gboolean response = FALSE;
231 GtkWidget *dialog = gtk_message_dialog_new(NULL,
232 (GtkDialogFlags)(GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT),
233 GTK_MESSAGE_QUESTION, GTK_BUTTONS_OK_CANCEL,
Stepan Salenikovicha1b8cb32015-09-11 14:58:35 -0400234 _("Are you sure you want to delete account \"%s\"?"),
Stepan Salenikovichc3fede22015-03-20 17:01:47 -0400235 account->alias().toLocal8Bit().constData());
236
Stepan Salenikovichbd029582015-03-24 11:00:56 -0400237 gtk_window_set_destroy_with_parent(GTK_WINDOW(dialog), TRUE);
238
239 /* get parent window so we can center on it */
240 GtkWidget *parent = gtk_widget_get_toplevel(GTK_WIDGET(view));
241 if (gtk_widget_is_toplevel(parent)) {
242 gtk_window_set_transient_for(GTK_WINDOW(dialog), GTK_WINDOW(parent));
243 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER_ON_PARENT);
244 }
245
Stepan Salenikovichc3fede22015-03-20 17:01:47 -0400246 switch (gtk_dialog_run(GTK_DIALOG(dialog))) {
247 case GTK_RESPONSE_OK:
248 response = TRUE;
249 break;
250 default:
251 response = FALSE;
252 break;
253 }
254
255 gtk_widget_destroy(dialog);
256
257 return response;
258}
259
Stepan Salenikovichbd029582015-03-24 11:00:56 -0400260static gboolean
261save_account(GtkWidget *working_dialog)
262{
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400263 AccountModel::instance().save();
Stepan Salenikovichbd029582015-03-24 11:00:56 -0400264 if (working_dialog)
265 gtk_widget_destroy(working_dialog);
266
267 return G_SOURCE_REMOVE;
268}
269
Stepan Salenikovichc3fede22015-03-20 17:01:47 -0400270static void
271remove_account(G_GNUC_UNUSED GtkWidget *entry, AccountView *view)
272{
273 g_return_if_fail(IS_ACCOUNT_VIEW(view));
274 AccountViewPrivate *priv = ACCOUNT_VIEW_GET_PRIVATE(view);
275
276 GtkTreeSelection *selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_account_list));
277 QModelIndex idx = get_index_from_selection(selection);
278
279 if (idx.isValid()) {
280 /* this is a destructive operation, ask the user if they are sure */
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400281 Account *account = AccountModel::instance().getAccountByModelIndex(idx);
Stepan Salenikovichbd029582015-03-24 11:00:56 -0400282 if (remove_account_dialog(view, account)) {
283 /* show working dialog in case save operation takes time */
284 GtkWidget *working = ring_dialog_working(GTK_WIDGET(view), NULL);
285 gtk_window_present(GTK_WINDOW(working));
286
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400287 AccountModel::instance().remove(idx);
Stepan Salenikovichbd029582015-03-24 11:00:56 -0400288
289 /* now save the time it takes to transition the account view to the new account (300ms)
290 * the save doesn't happen before the "working" dialog is presented
291 * the timeout function should destroy the "working" dialog when done saving
292 */
Stepan Salenikovich12fee942015-03-25 18:38:47 -0400293 g_timeout_add_full(G_PRIORITY_DEFAULT, 300, (GSourceFunc)save_account, working, NULL);
Stepan Salenikovichc3fede22015-03-20 17:01:47 -0400294 }
295 }
296}
297
298static void
299add_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 GtkTreeIter protocol_iter;
305 if (gtk_combo_box_get_active_iter(GTK_COMBO_BOX(priv->combobox_account_type), &protocol_iter)) {
306 /* get the qmodelindex of the protocol */
307 GtkTreeModel *protocol_model = gtk_combo_box_get_model(GTK_COMBO_BOX(priv->combobox_account_type));
308 QModelIndex protocol_idx = gtk_q_sort_filter_tree_model_get_source_idx(
309 GTK_Q_SORT_FILTER_TREE_MODEL(protocol_model),
310 &protocol_iter);
311 if (protocol_idx.isValid()) {
312 protocol_idx = priv->active_protocols->mapToSource(protocol_idx);
Stepan Salenikovichbd029582015-03-24 11:00:56 -0400313
314 /* show working dialog in case save operation takes time */
315 GtkWidget *working = ring_dialog_working(GTK_WIDGET(view), NULL);
316 gtk_window_present(GTK_WINDOW(working));
317
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400318 auto account = AccountModel::instance().add(QString(_("New Account")), protocol_idx);
Stepan Salenikovich75a39172015-07-10 13:21:08 -0400319 if (account->protocol() == Account::Protocol::RING)
Stepan Salenikovicha1b8cb32015-09-11 14:58:35 -0400320 account->setDisplayName(_("New Account"));
Stepan Salenikovichbd029582015-03-24 11:00:56 -0400321
322 /* now save after a short timeout to make sure that
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 Salenikovich12fee942015-03-25 18:38:47 -0400326 g_timeout_add_full(G_PRIORITY_DEFAULT, 300, (GSourceFunc)save_account, working, NULL);
Stepan Salenikovichc3fede22015-03-20 17:01:47 -0400327 }
328 }
329}
330
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400331static void
Stepan Salenikovichd6a9ba92015-03-29 11:14:36 -0400332state_to_string(G_GNUC_UNUSED GtkTreeViewColumn *tree_column,
333 GtkCellRenderer *cell,
334 GtkTreeModel *tree_model,
335 GtkTreeIter *iter,
Stepan Salenikoviche4981b22015-10-22 15:22:59 -0400336 GtkTreeView *treeview)
Stepan Salenikovichd6a9ba92015-03-29 11:14:36 -0400337{
Stepan Salenikoviche4981b22015-10-22 15:22:59 -0400338 // check if this iter is selected
339 gboolean is_selected = FALSE;
340 if (GTK_IS_TREE_VIEW(treeview)) {
341 auto selection = gtk_tree_view_get_selection(treeview);
342 is_selected = gtk_tree_selection_iter_is_selected(selection, iter);
343 }
344
Stepan Salenikovichd6a9ba92015-03-29 11:14:36 -0400345 gchar *display_state = NULL;
Stepan Salenikoviche7a73142015-08-11 11:55:59 -0400346
347 /* get account */
348 QModelIndex idx = gtk_q_tree_model_get_source_idx(GTK_Q_TREE_MODEL(tree_model), iter);
349 if (idx.isValid()) {
350
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400351 auto account = AccountModel::instance().getAccountByModelIndex(idx);
Stepan Salenikoviche7a73142015-08-11 11:55:59 -0400352 auto humanState = account->toHumanStateName();
353
Stepan Salenikovich0731de02016-05-17 17:47:16 -0400354 auto escaped_state = g_markup_escape_text(humanState.toUtf8().constData(), -1);
Stepan Salenikoviche4981b22015-10-22 15:22:59 -0400355 /* we want the color of the status text to be the default color if this iter is
356 * selected so that the treeview is able to invert it against the selection color */
357 if (is_selected) {
Stepan Salenikovich0731de02016-05-17 17:47:16 -0400358 display_state = escaped_state;
Stepan Salenikoviche4981b22015-10-22 15:22:59 -0400359 } else {
360 switch (account->registrationState()) {
361 case Account::RegistrationState::READY:
Stepan Salenikovich0731de02016-05-17 17:47:16 -0400362 display_state = g_strdup_printf("<span fgcolor=\"green\">%s</span>", escaped_state);
Stepan Salenikoviche4981b22015-10-22 15:22:59 -0400363 break;
364 case Account::RegistrationState::UNREGISTERED:
Stepan Salenikovich0731de02016-05-17 17:47:16 -0400365 display_state = g_strdup_printf("<span fgcolor=\"gray\">%s</span>", escaped_state);
Stepan Salenikoviche4981b22015-10-22 15:22:59 -0400366 break;
367 case Account::RegistrationState::TRYING:
Stepan Salenikovich0731de02016-05-17 17:47:16 -0400368 display_state = g_strdup_printf("<span fgcolor=\"orange\">%s</span>", escaped_state);
Stepan Salenikoviche4981b22015-10-22 15:22:59 -0400369 break;
370 case Account::RegistrationState::ERROR:
Stepan Salenikovich0731de02016-05-17 17:47:16 -0400371 display_state = g_strdup_printf("<span fgcolor=\"red\">%s</span>", escaped_state);
Stepan Salenikoviche4981b22015-10-22 15:22:59 -0400372 break;
373 case Account::RegistrationState::COUNT__:
374 g_warning("registration state should never be \"count\"");
Stepan Salenikovich0731de02016-05-17 17:47:16 -0400375 display_state = g_strdup_printf("<span fgcolor=\"red\">%s</span>", escaped_state);
Stepan Salenikoviche4981b22015-10-22 15:22:59 -0400376 break;
377 }
Stepan Salenikovich0731de02016-05-17 17:47:16 -0400378 g_free(escaped_state);
Stepan Salenikoviche7a73142015-08-11 11:55:59 -0400379 }
Stepan Salenikovichd6a9ba92015-03-29 11:14:36 -0400380 }
Stepan Salenikoviche7a73142015-08-11 11:55:59 -0400381
Stepan Salenikovichd6a9ba92015-03-29 11:14:36 -0400382 g_object_set(G_OBJECT(cell), "markup", display_state, NULL);
383 g_free(display_state);
384}
385
386static void
Stepan Salenikovichbdb0ecd2016-05-18 13:30:55 -0400387close_import_export_view(AccountView *self)
388{
389 g_return_if_fail(IS_ACCOUNT_VIEW(self));
390 AccountViewPrivate *priv = ACCOUNT_VIEW_GET_PRIVATE(self);
391
392 auto selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_account_list));
393 account_selection_changed(selection, self);
394}
395
396static void
397import_account(AccountView* self)
398{
399 g_return_if_fail(IS_ACCOUNT_VIEW(self));
400 AccountViewPrivate *priv = ACCOUNT_VIEW_GET_PRIVATE(self);
401
402 auto old_view = gtk_stack_get_visible_child(GTK_STACK(priv->stack_account));
403
404 auto import_account = account_import_view_new();
405 g_signal_connect_swapped(import_account, "import-export-canceled", G_CALLBACK(close_import_export_view), self);
406 g_signal_connect_swapped(import_account, "import-export-completed", G_CALLBACK(close_import_export_view), self);
407 auto scrolled_view = create_scrolled_account_view(import_account);
408 gtk_widget_show_all(scrolled_view);
409 gtk_stack_add_named(GTK_STACK(priv->stack_account), scrolled_view, "import_account");
410 gtk_stack_set_visible_child(GTK_STACK(priv->stack_account), scrolled_view);
411
412 /* remove the old view */
413 if (old_view)
414 gtk_container_remove(GTK_CONTAINER(priv->stack_account), old_view);
415}
416
417static void
418export_account(AccountView* self)
419{
420 g_return_if_fail(IS_ACCOUNT_VIEW(self));
421 AccountViewPrivate *priv = ACCOUNT_VIEW_GET_PRIVATE(self);
422
423 auto selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_account_list));
424 auto idx = get_index_from_selection(selection);
425 if (idx.isValid()) {
426 auto old_view = gtk_stack_get_visible_child(GTK_STACK(priv->stack_account));
427
428 auto account = AccountModel::instance().getAccountByModelIndex(idx);
429 auto export_account = account_export_view_new();
430 g_signal_connect_swapped(export_account, "import-export-canceled", G_CALLBACK(close_import_export_view), self);
431 g_signal_connect_swapped(export_account, "import-export-completed", G_CALLBACK(close_import_export_view), self);
432 GList *account_list = nullptr;
433 account_list = g_list_append(account_list, account);
434 account_export_view_set_accounts(ACCOUNT_IMPORTEXPORT_VIEW(export_account), account_list);
435 g_list_free(account_list);
436 auto scrolled_view = create_scrolled_account_view(export_account);
437 gtk_widget_show_all(scrolled_view);
438 gtk_stack_add_named(GTK_STACK(priv->stack_account), scrolled_view, "export_account");
439 gtk_stack_set_visible_child(GTK_STACK(priv->stack_account), scrolled_view);
440
441 /* remove the old view */
442 if (old_view)
443 gtk_container_remove(GTK_CONTAINER(priv->stack_account), old_view);
444
445 }
446
447}
448
449static void
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400450account_view_init(AccountView *view)
451{
452 gtk_widget_init_template(GTK_WIDGET(view));
453
454 AccountViewPrivate *priv = ACCOUNT_VIEW_GET_PRIVATE(view);
455
456 /* account model */
457 GtkQTreeModel *account_model;
458 GtkCellRenderer *renderer;
459 GtkTreeViewColumn *column;
460
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400461 account_model = gtk_q_tree_model_new(&AccountModel::instance(), 4,
aviau9bbb19b2016-05-16 15:53:44 -0400462 0, Account::Role::Enabled, G_TYPE_BOOLEAN,
463 0, Account::Role::Alias, G_TYPE_STRING,
464 0, Account::Role::Proto, G_TYPE_STRING,
465 0, Account::Role::RegistrationState, G_TYPE_UINT);
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400466 gtk_tree_view_set_model(GTK_TREE_VIEW(priv->treeview_account_list), GTK_TREE_MODEL(account_model));
467
468 renderer = gtk_cell_renderer_toggle_new();
Stepan Salenikovicha1b8cb32015-09-11 14:58:35 -0400469 column = gtk_tree_view_column_new_with_attributes(C_("Account state column", "Enabled"), renderer, "active", 0, NULL);
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400470 gtk_tree_view_append_column(GTK_TREE_VIEW(priv->treeview_account_list), column);
471
472 g_signal_connect(renderer, "toggled", G_CALLBACK(account_active_toggled), view);
473
474 renderer = gtk_cell_renderer_text_new();
Stepan Salenikovicha1b8cb32015-09-11 14:58:35 -0400475 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 -0400476 gtk_tree_view_append_column(GTK_TREE_VIEW(priv->treeview_account_list), column);
Stepan Salenikovichab0f5be2016-02-05 15:16:35 -0500477 g_object_set(G_OBJECT(renderer), "ellipsize", PANGO_ELLIPSIZE_END, NULL);
478 gtk_tree_view_column_set_expand(column, TRUE);
479 // set a min width so most of the account name is visible
480 g_object_set(G_OBJECT(renderer), "width", 75, NULL);
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400481
482 renderer = gtk_cell_renderer_text_new();
Stepan Salenikovicha1b8cb32015-09-11 14:58:35 -0400483 column = gtk_tree_view_column_new_with_attributes(C_("Account status column", "Status"), renderer, "text", 3, NULL);
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400484 gtk_tree_view_append_column(GTK_TREE_VIEW(priv->treeview_account_list), column);
Stepan Salenikovichab0f5be2016-02-05 15:16:35 -0500485 g_object_set(G_OBJECT(renderer), "ellipsize", PANGO_ELLIPSIZE_END, NULL);
486 gtk_tree_view_column_set_expand(column, TRUE);
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400487
Stepan Salenikovichd6a9ba92015-03-29 11:14:36 -0400488 /* the registration state is an enum, we want to display it as a string */
489 gtk_tree_view_column_set_cell_data_func(
490 column,
491 renderer,
492 (GtkTreeCellDataFunc)state_to_string,
Stepan Salenikoviche4981b22015-10-22 15:22:59 -0400493 priv->treeview_account_list,
Stepan Salenikovichd6a9ba92015-03-29 11:14:36 -0400494 NULL);
495
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400496 /* add an empty box to the account stack initially, otherwise there will
497 * be no cool animation when the first account is selected */
498 GtkWidget *empty_box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
499 gtk_widget_show(empty_box);
500 gtk_stack_add_named(GTK_STACK(priv->stack_account), empty_box, "placeholder");
501 gtk_stack_set_visible_child(GTK_STACK(priv->stack_account), empty_box);
Stepan Salenikovichc3fede22015-03-20 17:01:47 -0400502
503 /* populate account type combo box */
504 /* TODO: when to delete this model? */
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400505 priv->active_protocols = new ActiveItemProxyModel((QAbstractItemModel *)AccountModel::instance().protocolModel());
Stepan Salenikovichc3fede22015-03-20 17:01:47 -0400506
507 GtkQSortFilterTreeModel *protocol_model = gtk_q_sort_filter_tree_model_new(
508 (QSortFilterProxyModel *)priv->active_protocols,
509 1,
aviau271bcc22016-05-27 17:25:19 -0400510 0, Qt::DisplayRole, G_TYPE_STRING);
Stepan Salenikovichc3fede22015-03-20 17:01:47 -0400511
512 gtk_combo_box_set_model(GTK_COMBO_BOX(priv->combobox_account_type), GTK_TREE_MODEL(protocol_model));
513
514 renderer = gtk_cell_renderer_text_new();
515 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(priv->combobox_account_type), renderer, FALSE);
516 gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(priv->combobox_account_type), renderer,
517 "text", 0, NULL);
518
519 /* connect signals to and from the selection model of the account model */
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400520 priv->protocol_selection_changed = QObject::connect(
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400521 AccountModel::instance().selectionModel(),
Stepan Salenikovichc3fede22015-03-20 17:01:47 -0400522 &QItemSelectionModel::currentChanged,
Stepan Salenikovich825bb0a2016-03-11 15:33:52 -0500523 [=](const QModelIndex & current, G_GNUC_UNUSED const QModelIndex & previous) {
Stepan Salenikovichc3fede22015-03-20 17:01:47 -0400524 GtkTreeSelection *selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_account_list));
525
Stepan Salenikovichc3fede22015-03-20 17:01:47 -0400526 /* select the current */
527 if (current.isValid()) {
528 GtkTreeIter new_iter;
529 if (gtk_q_tree_model_source_index_to_iter(account_model, current, &new_iter)) {
530 gtk_tree_selection_select_iter(selection, &new_iter);
531 } else {
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -0400532 g_warning("SelectionModel of AccountModel changed to invalid QModelIndex?");
Stepan Salenikovichc3fede22015-03-20 17:01:47 -0400533 }
534 }
535 }
536 );
537
538 GtkTreeSelection *account_selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_account_list));
539 g_signal_connect(account_selection, "changed", G_CALLBACK(update_account_model_selection), NULL);
540 g_signal_connect(account_selection, "changed", G_CALLBACK(account_selection_changed), view);
541
542 /* select the default protocol */
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400543 QModelIndex protocol_idx = AccountModel::instance().protocolModel()->selectionModel()->currentIndex();
Stepan Salenikovichc3fede22015-03-20 17:01:47 -0400544 if (protocol_idx.isValid()) {
545 protocol_idx = priv->active_protocols->mapFromSource(protocol_idx);
546 GtkTreeIter protocol_iter;
547 if (gtk_q_sort_filter_tree_model_source_index_to_iter(
548 (GtkQSortFilterTreeModel *)protocol_model,
549 protocol_idx,
550 &protocol_iter)) {
551 gtk_combo_box_set_active_iter(GTK_COMBO_BOX(priv->combobox_account_type), &protocol_iter);
552 }
553 }
554
555 /* connect signals to add/remove accounts */
556 g_signal_connect(priv->button_remove_account, "clicked", G_CALLBACK(remove_account), view);
557 g_signal_connect(priv->button_add_account, "clicked", G_CALLBACK(add_account), view);
Stepan Salenikovichbdb0ecd2016-05-18 13:30:55 -0400558
559 /* signals to import/export acounts */
560 g_signal_connect_swapped(priv->button_import_account, "clicked", G_CALLBACK(import_account), view);
561 g_signal_connect_swapped(priv->button_export_account, "clicked", G_CALLBACK(export_account), view);
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400562}
563
564static void
565account_view_class_init(AccountViewClass *klass)
566{
567 G_OBJECT_CLASS(klass)->dispose = account_view_dispose;
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400568 G_OBJECT_CLASS(klass)->finalize = account_view_finalize;
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400569
570 gtk_widget_class_set_template_from_resource(GTK_WIDGET_CLASS (klass),
571 "/cx/ring/RingGnome/accountview.ui");
572
573 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), AccountView, treeview_account_list);
574 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), AccountView, stack_account);
Stepan Salenikovichc3fede22015-03-20 17:01:47 -0400575 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), AccountView, button_remove_account);
576 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), AccountView, button_add_account);
577 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), AccountView, combobox_account_type);
Stepan Salenikovichbdb0ecd2016-05-18 13:30:55 -0400578 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), AccountView, button_import_account);
579 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), AccountView, button_export_account);
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400580}
581
582GtkWidget *
583account_view_new(void)
584{
585 return (GtkWidget *)g_object_new(ACCOUNT_VIEW_TYPE, NULL);
586}