blob: 83224653db8a2254c3aadb0571408e9f7a13973d [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 Salenikovichf6078222016-10-03 17:31:16 -040029#include "models/gtkqtreemodel.h"
Stepan Salenikovichc3fede22015-03-20 17:01:47 -040030#include "models/activeitemproxymodel.h"
Stepan Salenikovich61cbab02015-03-16 18:35:10 -040031#include "accountgeneraltab.h"
aviau6aeb4852016-08-18 16:01:09 -040032#include "accountcreationwizard.h"
Stepan Salenikovich61cbab02015-03-16 18:35:10 -040033#include "accountaudiotab.h"
34#include "accountvideotab.h"
Stepan Salenikovichb7e6a732015-05-04 17:13:22 -040035#include "accountadvancedtab.h"
Stepan Salenikovich7a1e71c2015-05-07 11:14:48 -040036#include "accountsecuritytab.h"
aviau6aeb4852016-08-18 16:01:09 -040037#include "accountdevicestab.h"
Stepan Salenikovichbd029582015-03-24 11:00:56 -040038#include "dialogs.h"
Stepan Salenikovichd6a9ba92015-03-29 11:14:36 -040039#include <glib/gprintf.h>
Stepan Salenikovich9816a942015-04-22 17:49:16 -040040#include "utils/models.h"
Stepan Salenikovichbdb0ecd2016-05-18 13:30:55 -040041#include "accountimportexportview.h"
Stepan Salenikovich61cbab02015-03-16 18:35:10 -040042
aviau6aeb4852016-08-18 16:01:09 -040043static constexpr const char* ACCOUNT_CREATION_WIZARD_VIEW_NAME = "account-creation-wizard";
44
Stepan Salenikovich61cbab02015-03-16 18:35:10 -040045struct _AccountView
46{
Stepan Salenikovichab0f5be2016-02-05 15:16:35 -050047 GtkPaned parent;
Stepan Salenikovich61cbab02015-03-16 18:35:10 -040048};
49
50struct _AccountViewClass
51{
Stepan Salenikovichab0f5be2016-02-05 15:16:35 -050052 GtkPanedClass parent_class;
Stepan Salenikovich61cbab02015-03-16 18:35:10 -040053};
54
55typedef struct _AccountViewPrivate AccountViewPrivate;
56
57struct _AccountViewPrivate
58{
59 GtkWidget *treeview_account_list;
Stepan Salenikovichbdb0ecd2016-05-18 13:30:55 -040060 GtkWidget *account_import_view;
61 GtkWidget *account_export_view;
Stepan Salenikovich61cbab02015-03-16 18:35:10 -040062 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;
Stepan Salenikovichbdb0ecd2016-05-18 13:30:55 -040066 GtkWidget *button_import_account;
67 GtkWidget *button_export_account;
Stepan Salenikovich61cbab02015-03-16 18:35:10 -040068
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -040069 gint current_page; /* keeps track of current notebook page displayed */
Stepan Salenikovichc3fede22015-03-20 17:01:47 -040070
71 ActiveItemProxyModel *active_protocols;
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -040072 QMetaObject::Connection protocol_selection_changed;
Stepan Salenikovich61cbab02015-03-16 18:35:10 -040073};
74
Stepan Salenikovichab0f5be2016-02-05 15:16:35 -050075G_DEFINE_TYPE_WITH_PRIVATE(AccountView, account_view, GTK_TYPE_PANED);
Stepan Salenikovich61cbab02015-03-16 18:35:10 -040076
77#define ACCOUNT_VIEW_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), ACCOUNT_VIEW_TYPE, AccountViewPrivate))
78
79static void
80account_view_dispose(GObject *object)
81{
82 AccountView *view = ACCOUNT_VIEW(object);
83 AccountViewPrivate *priv = ACCOUNT_VIEW_GET_PRIVATE(view);
84
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -040085 QObject::disconnect(priv->protocol_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
91account_view_finalize(GObject *object)
92{
93 AccountView *view = ACCOUNT_VIEW(object);
94 AccountViewPrivate *priv = ACCOUNT_VIEW_GET_PRIVATE(view);
95
96 delete priv->active_protocols;
97
98 G_OBJECT_CLASS(account_view_parent_class)->finalize(object);
99}
100
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400101static void
Stepan Salenikovichc3fede22015-03-20 17:01:47 -0400102update_account_model_selection(GtkTreeSelection *selection, G_GNUC_UNUSED gpointer user_data)
103{
104 QModelIndex current = get_index_from_selection(selection);
105 if (current.isValid())
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400106 AccountModel::instance().selectionModel()->setCurrentIndex(current, QItemSelectionModel::ClearAndSelect);
Stepan Salenikovichc3fede22015-03-20 17:01:47 -0400107 else
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400108 AccountModel::instance().selectionModel()->clearCurrentIndex();
Stepan Salenikovichc3fede22015-03-20 17:01:47 -0400109}
110
Stepan Salenikovichd45e6392015-08-11 10:16:35 -0400111static GtkWidget *
112create_scrolled_account_view(GtkWidget *account_view)
113{
114 auto scrolled = gtk_scrolled_window_new(NULL, NULL);
115 gtk_container_add(GTK_CONTAINER(scrolled), account_view);
116 return scrolled;
117}
118
Stepan Salenikovichc3fede22015-03-20 17:01:47 -0400119static void
Stepan Salenikovichbdb0ecd2016-05-18 13:30:55 -0400120tab_selection_changed(G_GNUC_UNUSED GtkNotebook *notebook,
121 G_GNUC_UNUSED GtkWidget *page,
122 guint page_num,
123 AccountView *self)
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400124{
Stepan Salenikovichbdb0ecd2016-05-18 13:30:55 -0400125 g_return_if_fail(IS_ACCOUNT_VIEW(self));
126 AccountViewPrivate *priv = ACCOUNT_VIEW_GET_PRIVATE(self);
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400127
Stepan Salenikovichbdb0ecd2016-05-18 13:30:55 -0400128 priv->current_page = page_num;
129}
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400130
Stepan Salenikovichbdb0ecd2016-05-18 13:30:55 -0400131static void
132account_selection_changed(GtkTreeSelection *selection, AccountView *self)
133{
134 g_return_if_fail(IS_ACCOUNT_VIEW(self));
135 AccountViewPrivate *priv = ACCOUNT_VIEW_GET_PRIVATE(self);
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400136
Stepan Salenikovichbdb0ecd2016-05-18 13:30:55 -0400137 auto old_view = gtk_stack_get_visible_child(GTK_STACK(priv->stack_account));
aviau6aeb4852016-08-18 16:01:09 -0400138 if(IS_ACCOUNT_CREATION_WIZARD(old_view))
139 {
140 /* When using the account creation wizard, The user might create
141 * accounts that will be deleted by the daemon. The fact that the
142 * selection has changed should be ignored. The account creation wizard
143 * should be responsible to remove itself of the stack and then call
144 * account_selection_changed.
145 */
146 return;
147 }
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400148
149 QModelIndex account_idx = get_index_from_selection(selection);
150 if (!account_idx.isValid()) {
151 /* it nothing is slected, simply display something empty */
152 GtkWidget *empty_box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
153 gtk_widget_show(empty_box);
154 gtk_stack_add_named(GTK_STACK(priv->stack_account), empty_box, "placeholder");
155 gtk_stack_set_visible_child(GTK_STACK(priv->stack_account), empty_box);
Stepan Salenikovichbdb0ecd2016-05-18 13:30:55 -0400156
157 /* cannot delete nor export accounts */
158 gtk_widget_set_sensitive(priv->button_remove_account, FALSE);
159 gtk_widget_set_sensitive(priv->button_export_account, FALSE);
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400160 } else {
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400161 Account *account = AccountModel::instance().getAccountByModelIndex(account_idx);
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400162
163 /* build new account view */
164 GtkWidget *hbox_account = gtk_box_new(GTK_ORIENTATION_VERTICAL, 10);
165
166 /* create account notebook */
Stepan Salenikovichbdb0ecd2016-05-18 13:30:55 -0400167 auto account_notebook = gtk_notebook_new();
168 gtk_notebook_set_scrollable(GTK_NOTEBOOK(account_notebook), TRUE);
169 gtk_notebook_set_show_border(GTK_NOTEBOOK(account_notebook), FALSE);
170 gtk_box_pack_start(GTK_BOX(hbox_account), account_notebook, TRUE, TRUE, 0);
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400171
172 /* customize account view based on account */
Stepan Salenikovichd45e6392015-08-11 10:16:35 -0400173 auto general_tab = create_scrolled_account_view(account_general_tab_new(account));
Stepan Salenikovichbdb0ecd2016-05-18 13:30:55 -0400174 gtk_notebook_append_page(GTK_NOTEBOOK(account_notebook),
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400175 general_tab,
Stepan Salenikovicha1b8cb32015-09-11 14:58:35 -0400176 gtk_label_new(C_("Account settings", "General")));
Stepan Salenikovichd45e6392015-08-11 10:16:35 -0400177 auto audio_tab = create_scrolled_account_view(account_audio_tab_new(account));
Stepan Salenikovichbdb0ecd2016-05-18 13:30:55 -0400178 gtk_notebook_append_page(GTK_NOTEBOOK(account_notebook),
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400179 audio_tab,
Stepan Salenikovicha1b8cb32015-09-11 14:58:35 -0400180 gtk_label_new(C_("Account settings", "Audio")));
Stepan Salenikovichd45e6392015-08-11 10:16:35 -0400181 auto video_tab = create_scrolled_account_view(account_video_tab_new(account));
Stepan Salenikovichbdb0ecd2016-05-18 13:30:55 -0400182 gtk_notebook_append_page(GTK_NOTEBOOK(account_notebook),
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400183 video_tab,
Stepan Salenikovicha1b8cb32015-09-11 14:58:35 -0400184 gtk_label_new(C_("Account settings", "Video")));
Stepan Salenikovichd45e6392015-08-11 10:16:35 -0400185 auto advanced_tab = create_scrolled_account_view(account_advanced_tab_new(account));
Stepan Salenikovichbdb0ecd2016-05-18 13:30:55 -0400186 gtk_notebook_append_page(GTK_NOTEBOOK(account_notebook),
Stepan Salenikovichb7e6a732015-05-04 17:13:22 -0400187 advanced_tab,
Stepan Salenikovicha1b8cb32015-09-11 14:58:35 -0400188 gtk_label_new(C_("Account settings", "Advanced")));
Stepan Salenikovichd45e6392015-08-11 10:16:35 -0400189 auto security_tab = create_scrolled_account_view(account_security_tab_new(account));
Stepan Salenikovichbdb0ecd2016-05-18 13:30:55 -0400190 gtk_notebook_append_page(GTK_NOTEBOOK(account_notebook),
Stepan Salenikovich7a1e71c2015-05-07 11:14:48 -0400191 security_tab,
Stepan Salenikovicha1b8cb32015-09-11 14:58:35 -0400192 gtk_label_new(C_("Account settings", "Security")));
aviau6aeb4852016-08-18 16:01:09 -0400193 auto devices_tab = create_scrolled_account_view(account_devices_tab_new(account));
194 gtk_notebook_append_page(GTK_NOTEBOOK(account_notebook),
195 devices_tab,
196 gtk_label_new(C_("Account settings", "Devices")));
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400197
Stepan Salenikovichad589082016-05-18 13:22:52 -0400198 gtk_widget_show_all(hbox_account);
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400199 /* set the tab displayed to the same as the prev account selected */
Stepan Salenikovichbdb0ecd2016-05-18 13:30:55 -0400200 gtk_notebook_set_current_page(GTK_NOTEBOOK(account_notebook), priv->current_page);
201 /* now connect to the tab changed signal */
202 g_signal_connect(account_notebook, "switch-page", G_CALLBACK(tab_selection_changed), self);
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400203
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400204 /* set the new account view as visible */
205 char *account_view_name = g_strdup_printf("%p_account", account);
206 gtk_stack_add_named(GTK_STACK(priv->stack_account), hbox_account, account_view_name);
207 gtk_stack_set_visible_child(GTK_STACK(priv->stack_account), hbox_account);
208 g_free(account_view_name);
Stepan Salenikovichbdb0ecd2016-05-18 13:30:55 -0400209
210 /* can delete and export accounts */
211 gtk_widget_set_sensitive(priv->button_remove_account, TRUE);
212 gtk_widget_set_sensitive(priv->button_export_account, TRUE);
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400213 }
214
Stepan Salenikovichbdb0ecd2016-05-18 13:30:55 -0400215 /* remove the old view */
216 if (old_view)
217 gtk_container_remove(GTK_CONTAINER(priv->stack_account), old_view);
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400218}
219
220static void
221account_active_toggled(GtkCellRendererToggle *renderer, gchar *path, AccountView *view)
222{
223 g_return_if_fail(IS_ACCOUNT_VIEW(view));
224 AccountViewPrivate *priv = ACCOUNT_VIEW_GET_PRIVATE(view);
225
226 /* we want to set it to the opposite of the current value */
227 gboolean toggle = !gtk_cell_renderer_toggle_get_active(renderer);
228
229 /* get iter which was clicked */
230 GtkTreePath *tree_path = gtk_tree_path_new_from_string(path);
231 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(priv->treeview_account_list));
232 GtkTreeIter iter;
233 gtk_tree_model_get_iter(model, &iter, tree_path);
234
235 /* get qmodelindex from iter and set the model data */
236 QModelIndex idx = gtk_q_tree_model_get_source_idx(GTK_Q_TREE_MODEL(model), &iter);
237 if (idx.isValid()) {
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400238 QVariant alias = idx.data(static_cast<int>(Account::Role::Alias));
Stepan Salenikovichfea84052016-05-13 16:32:51 -0400239 AccountModel::instance().setData(idx, QVariant(toggle), Qt::CheckStateRole);
240 /* save the account to apply the changed state right away */
241 AccountModel::instance().getAccountByModelIndex(idx)->performAction(Account::EditAction::SAVE);
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400242 }
243}
244
Stepan Salenikovichc3fede22015-03-20 17:01:47 -0400245static gboolean
Stepan Salenikovichbd029582015-03-24 11:00:56 -0400246remove_account_dialog(AccountView *view, Account *account)
Stepan Salenikovichc3fede22015-03-20 17:01:47 -0400247{
248 gboolean response = FALSE;
249 GtkWidget *dialog = gtk_message_dialog_new(NULL,
250 (GtkDialogFlags)(GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT),
251 GTK_MESSAGE_QUESTION, GTK_BUTTONS_OK_CANCEL,
Stepan Salenikovicha1b8cb32015-09-11 14:58:35 -0400252 _("Are you sure you want to delete account \"%s\"?"),
Stepan Salenikovichc3fede22015-03-20 17:01:47 -0400253 account->alias().toLocal8Bit().constData());
254
Stepan Salenikovichbd029582015-03-24 11:00:56 -0400255 gtk_window_set_destroy_with_parent(GTK_WINDOW(dialog), TRUE);
256
257 /* get parent window so we can center on it */
258 GtkWidget *parent = gtk_widget_get_toplevel(GTK_WIDGET(view));
259 if (gtk_widget_is_toplevel(parent)) {
260 gtk_window_set_transient_for(GTK_WINDOW(dialog), GTK_WINDOW(parent));
261 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER_ON_PARENT);
262 }
263
Stepan Salenikovichc3fede22015-03-20 17:01:47 -0400264 switch (gtk_dialog_run(GTK_DIALOG(dialog))) {
265 case GTK_RESPONSE_OK:
266 response = TRUE;
267 break;
268 default:
269 response = FALSE;
270 break;
271 }
272
273 gtk_widget_destroy(dialog);
274
275 return response;
276}
277
Stepan Salenikovichbd029582015-03-24 11:00:56 -0400278static gboolean
279save_account(GtkWidget *working_dialog)
280{
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400281 AccountModel::instance().save();
Stepan Salenikovichbd029582015-03-24 11:00:56 -0400282 if (working_dialog)
283 gtk_widget_destroy(working_dialog);
284
285 return G_SOURCE_REMOVE;
286}
287
Stepan Salenikovichc3fede22015-03-20 17:01:47 -0400288static void
289remove_account(G_GNUC_UNUSED GtkWidget *entry, AccountView *view)
290{
291 g_return_if_fail(IS_ACCOUNT_VIEW(view));
292 AccountViewPrivate *priv = ACCOUNT_VIEW_GET_PRIVATE(view);
293
294 GtkTreeSelection *selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_account_list));
295 QModelIndex idx = get_index_from_selection(selection);
296
297 if (idx.isValid()) {
298 /* this is a destructive operation, ask the user if they are sure */
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400299 Account *account = AccountModel::instance().getAccountByModelIndex(idx);
Stepan Salenikovichbd029582015-03-24 11:00:56 -0400300 if (remove_account_dialog(view, account)) {
301 /* show working dialog in case save operation takes time */
302 GtkWidget *working = ring_dialog_working(GTK_WIDGET(view), NULL);
303 gtk_window_present(GTK_WINDOW(working));
304
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400305 AccountModel::instance().remove(idx);
Stepan Salenikovichbd029582015-03-24 11:00:56 -0400306
307 /* now save the time it takes to transition the account view to the new account (300ms)
308 * the save doesn't happen before the "working" dialog is presented
309 * the timeout function should destroy the "working" dialog when done saving
310 */
Stepan Salenikovich12fee942015-03-25 18:38:47 -0400311 g_timeout_add_full(G_PRIORITY_DEFAULT, 300, (GSourceFunc)save_account, working, NULL);
Stepan Salenikovichc3fede22015-03-20 17:01:47 -0400312 }
313 }
314}
315
316static void
aviau6aeb4852016-08-18 16:01:09 -0400317hide_account_creation_wizard(AccountView *view)
318{
319 g_return_if_fail(IS_ACCOUNT_VIEW(view));
320 AccountViewPrivate *priv = ACCOUNT_VIEW_GET_PRIVATE(view);
321
322 auto old_view = gtk_stack_get_visible_child(GTK_STACK(priv->stack_account));
323 if (IS_ACCOUNT_CREATION_WIZARD(old_view))
324 {
325 gtk_container_remove(GTK_CONTAINER(priv->stack_account), old_view);
326 }
327
328 auto selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_account_list));
329 account_selection_changed(selection, view);
330}
331
332static void
333show_account_creation_wizard(AccountView *view)
334{
335 g_return_if_fail(IS_ACCOUNT_VIEW(view));
336 AccountViewPrivate *priv = ACCOUNT_VIEW_GET_PRIVATE(view);
337
338 auto old_view = gtk_stack_get_visible_child(GTK_STACK(priv->stack_account));
339
340 auto account_creation_wizard = gtk_stack_get_child_by_name(
341 GTK_STACK(priv->stack_account),
342 ACCOUNT_CREATION_WIZARD_VIEW_NAME
343 );
344
345 if (!account_creation_wizard)
346 {
347 account_creation_wizard = account_creation_wizard_new(true);
348 gtk_stack_add_named(GTK_STACK(priv->stack_account),
349 account_creation_wizard,
350 ACCOUNT_CREATION_WIZARD_VIEW_NAME);
351 g_signal_connect_swapped(account_creation_wizard, "account-creation-completed", G_CALLBACK(hide_account_creation_wizard), view);
352 g_signal_connect_swapped(account_creation_wizard, "account-creation-canceled", G_CALLBACK(hide_account_creation_wizard), view);
353 }
354
355 gtk_widget_show(account_creation_wizard);
356
357 gtk_stack_set_visible_child_name(GTK_STACK(priv->stack_account), ACCOUNT_CREATION_WIZARD_VIEW_NAME);
358
359 /* remove the old view */
360 if (old_view)
361 gtk_container_remove(GTK_CONTAINER(priv->stack_account), old_view);
362}
363
364static void
Stepan Salenikovichc3fede22015-03-20 17:01:47 -0400365add_account(G_GNUC_UNUSED GtkWidget *entry, AccountView *view)
366{
367 g_return_if_fail(IS_ACCOUNT_VIEW(view));
368 AccountViewPrivate *priv = ACCOUNT_VIEW_GET_PRIVATE(view);
369
370 GtkTreeIter protocol_iter;
371 if (gtk_combo_box_get_active_iter(GTK_COMBO_BOX(priv->combobox_account_type), &protocol_iter)) {
372 /* get the qmodelindex of the protocol */
373 GtkTreeModel *protocol_model = gtk_combo_box_get_model(GTK_COMBO_BOX(priv->combobox_account_type));
Stepan Salenikovichf6078222016-10-03 17:31:16 -0400374 QModelIndex protocol_idx = gtk_q_tree_model_get_source_idx(
375 GTK_Q_TREE_MODEL(protocol_model),
Stepan Salenikovichc3fede22015-03-20 17:01:47 -0400376 &protocol_iter);
377 if (protocol_idx.isValid()) {
378 protocol_idx = priv->active_protocols->mapToSource(protocol_idx);
Stepan Salenikovichbd029582015-03-24 11:00:56 -0400379
aviau6aeb4852016-08-18 16:01:09 -0400380 Account::Protocol protocol = qvariant_cast<Account::Protocol>(protocol_idx.data((int)ProtocolModel::Role::Protocol));
381 if (protocol == Account::Protocol::RING)
382 {
383 show_account_creation_wizard(view);
384 }
385 else
386 {
387 /* show working dialog in case save operation takes time */
388 GtkWidget *working = ring_dialog_working(GTK_WIDGET(view), NULL);
389 gtk_window_present(GTK_WINDOW(working));
Stepan Salenikovichbd029582015-03-24 11:00:56 -0400390
aviau6aeb4852016-08-18 16:01:09 -0400391 AccountModel::instance().add(QString(_("New Account")), protocol_idx);
Stepan Salenikovichbd029582015-03-24 11:00:56 -0400392
aviau6aeb4852016-08-18 16:01:09 -0400393 /* now save after a short timeout to make sure that
394 * the save doesn't happen before the "working" dialog is presented
395 * the timeout function should destroy the "working" dialog when done saving
396 */
397 g_timeout_add_full(G_PRIORITY_DEFAULT, 300, (GSourceFunc)save_account, working, NULL);
398 }
399
Stepan Salenikovichc3fede22015-03-20 17:01:47 -0400400 }
401 }
402}
403
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400404static void
Stepan Salenikovichd6a9ba92015-03-29 11:14:36 -0400405state_to_string(G_GNUC_UNUSED GtkTreeViewColumn *tree_column,
406 GtkCellRenderer *cell,
407 GtkTreeModel *tree_model,
408 GtkTreeIter *iter,
Stepan Salenikoviche4981b22015-10-22 15:22:59 -0400409 GtkTreeView *treeview)
Stepan Salenikovichd6a9ba92015-03-29 11:14:36 -0400410{
Stepan Salenikoviche4981b22015-10-22 15:22:59 -0400411 // check if this iter is selected
412 gboolean is_selected = FALSE;
413 if (GTK_IS_TREE_VIEW(treeview)) {
414 auto selection = gtk_tree_view_get_selection(treeview);
415 is_selected = gtk_tree_selection_iter_is_selected(selection, iter);
416 }
417
Stepan Salenikovichd6a9ba92015-03-29 11:14:36 -0400418 gchar *display_state = NULL;
Stepan Salenikoviche7a73142015-08-11 11:55:59 -0400419
420 /* get account */
421 QModelIndex idx = gtk_q_tree_model_get_source_idx(GTK_Q_TREE_MODEL(tree_model), iter);
422 if (idx.isValid()) {
423
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400424 auto account = AccountModel::instance().getAccountByModelIndex(idx);
Stepan Salenikoviche7a73142015-08-11 11:55:59 -0400425 auto humanState = account->toHumanStateName();
426
Stepan Salenikovich0731de02016-05-17 17:47:16 -0400427 auto escaped_state = g_markup_escape_text(humanState.toUtf8().constData(), -1);
Stepan Salenikoviche4981b22015-10-22 15:22:59 -0400428 /* we want the color of the status text to be the default color if this iter is
429 * selected so that the treeview is able to invert it against the selection color */
430 if (is_selected) {
Stepan Salenikovich0731de02016-05-17 17:47:16 -0400431 display_state = escaped_state;
Stepan Salenikoviche4981b22015-10-22 15:22:59 -0400432 } else {
433 switch (account->registrationState()) {
434 case Account::RegistrationState::READY:
Stepan Salenikovich0731de02016-05-17 17:47:16 -0400435 display_state = g_strdup_printf("<span fgcolor=\"green\">%s</span>", escaped_state);
Stepan Salenikoviche4981b22015-10-22 15:22:59 -0400436 break;
437 case Account::RegistrationState::UNREGISTERED:
Stepan Salenikovich0731de02016-05-17 17:47:16 -0400438 display_state = g_strdup_printf("<span fgcolor=\"gray\">%s</span>", escaped_state);
Stepan Salenikoviche4981b22015-10-22 15:22:59 -0400439 break;
440 case Account::RegistrationState::TRYING:
aviau6aeb4852016-08-18 16:01:09 -0400441 case Account::RegistrationState::INITIALIZING:
Stepan Salenikovich0731de02016-05-17 17:47:16 -0400442 display_state = g_strdup_printf("<span fgcolor=\"orange\">%s</span>", escaped_state);
Stepan Salenikoviche4981b22015-10-22 15:22:59 -0400443 break;
444 case Account::RegistrationState::ERROR:
Stepan Salenikovich0731de02016-05-17 17:47:16 -0400445 display_state = g_strdup_printf("<span fgcolor=\"red\">%s</span>", escaped_state);
Stepan Salenikoviche4981b22015-10-22 15:22:59 -0400446 break;
447 case Account::RegistrationState::COUNT__:
448 g_warning("registration state should never be \"count\"");
Stepan Salenikovich0731de02016-05-17 17:47:16 -0400449 display_state = g_strdup_printf("<span fgcolor=\"red\">%s</span>", escaped_state);
Stepan Salenikoviche4981b22015-10-22 15:22:59 -0400450 break;
451 }
Stepan Salenikovich0731de02016-05-17 17:47:16 -0400452 g_free(escaped_state);
Stepan Salenikoviche7a73142015-08-11 11:55:59 -0400453 }
Stepan Salenikovichd6a9ba92015-03-29 11:14:36 -0400454 }
Stepan Salenikoviche7a73142015-08-11 11:55:59 -0400455
Stepan Salenikovichd6a9ba92015-03-29 11:14:36 -0400456 g_object_set(G_OBJECT(cell), "markup", display_state, NULL);
457 g_free(display_state);
458}
459
460static void
Stepan Salenikovichbdb0ecd2016-05-18 13:30:55 -0400461close_import_export_view(AccountView *self)
462{
463 g_return_if_fail(IS_ACCOUNT_VIEW(self));
464 AccountViewPrivate *priv = ACCOUNT_VIEW_GET_PRIVATE(self);
465
466 auto selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_account_list));
467 account_selection_changed(selection, self);
468}
469
470static void
471import_account(AccountView* self)
472{
473 g_return_if_fail(IS_ACCOUNT_VIEW(self));
474 AccountViewPrivate *priv = ACCOUNT_VIEW_GET_PRIVATE(self);
475
476 auto old_view = gtk_stack_get_visible_child(GTK_STACK(priv->stack_account));
477
478 auto import_account = account_import_view_new();
479 g_signal_connect_swapped(import_account, "import-export-canceled", G_CALLBACK(close_import_export_view), self);
480 g_signal_connect_swapped(import_account, "import-export-completed", G_CALLBACK(close_import_export_view), self);
481 auto scrolled_view = create_scrolled_account_view(import_account);
482 gtk_widget_show_all(scrolled_view);
483 gtk_stack_add_named(GTK_STACK(priv->stack_account), scrolled_view, "import_account");
484 gtk_stack_set_visible_child(GTK_STACK(priv->stack_account), scrolled_view);
485
486 /* remove the old view */
487 if (old_view)
488 gtk_container_remove(GTK_CONTAINER(priv->stack_account), old_view);
489}
490
491static void
492export_account(AccountView* self)
493{
494 g_return_if_fail(IS_ACCOUNT_VIEW(self));
495 AccountViewPrivate *priv = ACCOUNT_VIEW_GET_PRIVATE(self);
496
497 auto selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_account_list));
498 auto idx = get_index_from_selection(selection);
499 if (idx.isValid()) {
500 auto old_view = gtk_stack_get_visible_child(GTK_STACK(priv->stack_account));
501
502 auto account = AccountModel::instance().getAccountByModelIndex(idx);
503 auto export_account = account_export_view_new();
504 g_signal_connect_swapped(export_account, "import-export-canceled", G_CALLBACK(close_import_export_view), self);
505 g_signal_connect_swapped(export_account, "import-export-completed", G_CALLBACK(close_import_export_view), self);
506 GList *account_list = nullptr;
507 account_list = g_list_append(account_list, account);
508 account_export_view_set_accounts(ACCOUNT_IMPORTEXPORT_VIEW(export_account), account_list);
509 g_list_free(account_list);
510 auto scrolled_view = create_scrolled_account_view(export_account);
511 gtk_widget_show_all(scrolled_view);
512 gtk_stack_add_named(GTK_STACK(priv->stack_account), scrolled_view, "export_account");
513 gtk_stack_set_visible_child(GTK_STACK(priv->stack_account), scrolled_view);
514
515 /* remove the old view */
516 if (old_view)
517 gtk_container_remove(GTK_CONTAINER(priv->stack_account), old_view);
518
519 }
520
521}
522
523static void
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400524account_view_init(AccountView *view)
525{
526 gtk_widget_init_template(GTK_WIDGET(view));
527
528 AccountViewPrivate *priv = ACCOUNT_VIEW_GET_PRIVATE(view);
529
530 /* account model */
531 GtkQTreeModel *account_model;
532 GtkCellRenderer *renderer;
533 GtkTreeViewColumn *column;
534
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400535 account_model = gtk_q_tree_model_new(&AccountModel::instance(), 4,
aviau9bbb19b2016-05-16 15:53:44 -0400536 0, Account::Role::Enabled, G_TYPE_BOOLEAN,
537 0, Account::Role::Alias, G_TYPE_STRING,
538 0, Account::Role::Proto, G_TYPE_STRING,
539 0, Account::Role::RegistrationState, G_TYPE_UINT);
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400540 gtk_tree_view_set_model(GTK_TREE_VIEW(priv->treeview_account_list), GTK_TREE_MODEL(account_model));
541
542 renderer = gtk_cell_renderer_toggle_new();
Stepan Salenikovicha1b8cb32015-09-11 14:58:35 -0400543 column = gtk_tree_view_column_new_with_attributes(C_("Account state column", "Enabled"), renderer, "active", 0, NULL);
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400544 gtk_tree_view_append_column(GTK_TREE_VIEW(priv->treeview_account_list), column);
545
546 g_signal_connect(renderer, "toggled", G_CALLBACK(account_active_toggled), view);
547
548 renderer = gtk_cell_renderer_text_new();
Stepan Salenikovicha1b8cb32015-09-11 14:58:35 -0400549 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 -0400550 gtk_tree_view_append_column(GTK_TREE_VIEW(priv->treeview_account_list), column);
Stepan Salenikovichab0f5be2016-02-05 15:16:35 -0500551 g_object_set(G_OBJECT(renderer), "ellipsize", PANGO_ELLIPSIZE_END, NULL);
552 gtk_tree_view_column_set_expand(column, TRUE);
553 // set a min width so most of the account name is visible
554 g_object_set(G_OBJECT(renderer), "width", 75, NULL);
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400555
556 renderer = gtk_cell_renderer_text_new();
Stepan Salenikovicha1b8cb32015-09-11 14:58:35 -0400557 column = gtk_tree_view_column_new_with_attributes(C_("Account status column", "Status"), renderer, "text", 3, NULL);
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400558 gtk_tree_view_append_column(GTK_TREE_VIEW(priv->treeview_account_list), column);
Stepan Salenikovichab0f5be2016-02-05 15:16:35 -0500559 g_object_set(G_OBJECT(renderer), "ellipsize", PANGO_ELLIPSIZE_END, NULL);
560 gtk_tree_view_column_set_expand(column, TRUE);
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400561
Stepan Salenikovichd6a9ba92015-03-29 11:14:36 -0400562 /* the registration state is an enum, we want to display it as a string */
563 gtk_tree_view_column_set_cell_data_func(
564 column,
565 renderer,
566 (GtkTreeCellDataFunc)state_to_string,
Stepan Salenikoviche4981b22015-10-22 15:22:59 -0400567 priv->treeview_account_list,
Stepan Salenikovichd6a9ba92015-03-29 11:14:36 -0400568 NULL);
569
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400570 /* add an empty box to the account stack initially, otherwise there will
571 * be no cool animation when the first account is selected */
572 GtkWidget *empty_box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
573 gtk_widget_show(empty_box);
574 gtk_stack_add_named(GTK_STACK(priv->stack_account), empty_box, "placeholder");
575 gtk_stack_set_visible_child(GTK_STACK(priv->stack_account), empty_box);
Stepan Salenikovichc3fede22015-03-20 17:01:47 -0400576
577 /* populate account type combo box */
578 /* TODO: when to delete this model? */
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400579 priv->active_protocols = new ActiveItemProxyModel((QAbstractItemModel *)AccountModel::instance().protocolModel());
Stepan Salenikovichc3fede22015-03-20 17:01:47 -0400580
Stepan Salenikovichf6078222016-10-03 17:31:16 -0400581 GtkQTreeModel *protocol_model = gtk_q_tree_model_new(
Stepan Salenikovichc3fede22015-03-20 17:01:47 -0400582 (QSortFilterProxyModel *)priv->active_protocols,
583 1,
aviau271bcc22016-05-27 17:25:19 -0400584 0, Qt::DisplayRole, G_TYPE_STRING);
Stepan Salenikovichc3fede22015-03-20 17:01:47 -0400585
586 gtk_combo_box_set_model(GTK_COMBO_BOX(priv->combobox_account_type), GTK_TREE_MODEL(protocol_model));
587
588 renderer = gtk_cell_renderer_text_new();
589 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(priv->combobox_account_type), renderer, FALSE);
590 gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(priv->combobox_account_type), renderer,
591 "text", 0, NULL);
592
593 /* connect signals to and from the selection model of the account model */
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400594 priv->protocol_selection_changed = QObject::connect(
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400595 AccountModel::instance().selectionModel(),
Stepan Salenikovichc3fede22015-03-20 17:01:47 -0400596 &QItemSelectionModel::currentChanged,
Stepan Salenikovich825bb0a2016-03-11 15:33:52 -0500597 [=](const QModelIndex & current, G_GNUC_UNUSED const QModelIndex & previous) {
Stepan Salenikovichc3fede22015-03-20 17:01:47 -0400598 GtkTreeSelection *selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_account_list));
599
Stepan Salenikovichc3fede22015-03-20 17:01:47 -0400600 /* select the current */
601 if (current.isValid()) {
602 GtkTreeIter new_iter;
603 if (gtk_q_tree_model_source_index_to_iter(account_model, current, &new_iter)) {
604 gtk_tree_selection_select_iter(selection, &new_iter);
605 } else {
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -0400606 g_warning("SelectionModel of AccountModel changed to invalid QModelIndex?");
Stepan Salenikovichc3fede22015-03-20 17:01:47 -0400607 }
608 }
609 }
610 );
611
612 GtkTreeSelection *account_selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_account_list));
613 g_signal_connect(account_selection, "changed", G_CALLBACK(update_account_model_selection), NULL);
614 g_signal_connect(account_selection, "changed", G_CALLBACK(account_selection_changed), view);
615
616 /* select the default protocol */
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400617 QModelIndex protocol_idx = AccountModel::instance().protocolModel()->selectionModel()->currentIndex();
Stepan Salenikovichc3fede22015-03-20 17:01:47 -0400618 if (protocol_idx.isValid()) {
619 protocol_idx = priv->active_protocols->mapFromSource(protocol_idx);
620 GtkTreeIter protocol_iter;
Stepan Salenikovichf6078222016-10-03 17:31:16 -0400621 if (gtk_q_tree_model_source_index_to_iter(
622 (GtkQTreeModel *)protocol_model,
Stepan Salenikovichc3fede22015-03-20 17:01:47 -0400623 protocol_idx,
624 &protocol_iter)) {
625 gtk_combo_box_set_active_iter(GTK_COMBO_BOX(priv->combobox_account_type), &protocol_iter);
626 }
627 }
628
629 /* connect signals to add/remove accounts */
630 g_signal_connect(priv->button_remove_account, "clicked", G_CALLBACK(remove_account), view);
631 g_signal_connect(priv->button_add_account, "clicked", G_CALLBACK(add_account), view);
Stepan Salenikovichbdb0ecd2016-05-18 13:30:55 -0400632
633 /* signals to import/export acounts */
634 g_signal_connect_swapped(priv->button_import_account, "clicked", G_CALLBACK(import_account), view);
635 g_signal_connect_swapped(priv->button_export_account, "clicked", G_CALLBACK(export_account), view);
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400636}
637
638static void
639account_view_class_init(AccountViewClass *klass)
640{
641 G_OBJECT_CLASS(klass)->dispose = account_view_dispose;
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400642 G_OBJECT_CLASS(klass)->finalize = account_view_finalize;
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400643
644 gtk_widget_class_set_template_from_resource(GTK_WIDGET_CLASS (klass),
645 "/cx/ring/RingGnome/accountview.ui");
646
647 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), AccountView, treeview_account_list);
648 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), AccountView, stack_account);
Stepan Salenikovichc3fede22015-03-20 17:01:47 -0400649 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), AccountView, button_remove_account);
650 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), AccountView, button_add_account);
651 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), AccountView, combobox_account_type);
Stepan Salenikovichbdb0ecd2016-05-18 13:30:55 -0400652 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), AccountView, button_import_account);
653 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), AccountView, button_export_account);
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400654}
655
656GtkWidget *
657account_view_new(void)
658{
659 return (GtkWidget *)g_object_new(ACCOUNT_VIEW_TYPE, NULL);
660}