Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 1 | /* |
Stepan Salenikovich | be87d2c | 2016-01-25 14:14:34 -0500 | [diff] [blame] | 2 | * Copyright (C) 2015-2016 Savoir-faire Linux Inc. |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 3 | * Author: Stepan Salenikovich <stepan.salenikovich@savoirfairelinux.com> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | #include "accountview.h" |
| 21 | |
| 22 | #include <gtk/gtk.h> |
Stepan Salenikovich | a1b8cb3 | 2015-09-11 14:58:35 -0400 | [diff] [blame] | 23 | #include <glib/gi18n.h> |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 24 | #include <accountmodel.h> |
Edric Milaret | 58e7107 | 2016-03-21 12:16:37 -0400 | [diff] [blame] | 25 | #include <codecmodel.h> |
Stepan Salenikovich | c3fede2 | 2015-03-20 17:01:47 -0400 | [diff] [blame] | 26 | #include <protocolmodel.h> |
| 27 | #include <QtCore/QItemSelectionModel> |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 28 | #include "models/gtkqtreemodel.h" |
Stepan Salenikovich | c3fede2 | 2015-03-20 17:01:47 -0400 | [diff] [blame] | 29 | #include "models/gtkqsortfiltertreemodel.h" |
| 30 | #include "models/activeitemproxymodel.h" |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 31 | #include "accountgeneraltab.h" |
| 32 | #include "accountaudiotab.h" |
| 33 | #include "accountvideotab.h" |
Stepan Salenikovich | b7e6a73 | 2015-05-04 17:13:22 -0400 | [diff] [blame] | 34 | #include "accountadvancedtab.h" |
Stepan Salenikovich | 7a1e71c | 2015-05-07 11:14:48 -0400 | [diff] [blame] | 35 | #include "accountsecuritytab.h" |
Stepan Salenikovich | bd02958 | 2015-03-24 11:00:56 -0400 | [diff] [blame] | 36 | #include "dialogs.h" |
Stepan Salenikovich | d6a9ba9 | 2015-03-29 11:14:36 -0400 | [diff] [blame] | 37 | #include <glib/gprintf.h> |
Stepan Salenikovich | 9816a94 | 2015-04-22 17:49:16 -0400 | [diff] [blame] | 38 | #include "utils/models.h" |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 39 | |
| 40 | struct _AccountView |
| 41 | { |
Stepan Salenikovich | ab0f5be | 2016-02-05 15:16:35 -0500 | [diff] [blame] | 42 | GtkPaned parent; |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 43 | }; |
| 44 | |
| 45 | struct _AccountViewClass |
| 46 | { |
Stepan Salenikovich | ab0f5be | 2016-02-05 15:16:35 -0500 | [diff] [blame] | 47 | GtkPanedClass parent_class; |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 48 | }; |
| 49 | |
| 50 | typedef struct _AccountViewPrivate AccountViewPrivate; |
| 51 | |
| 52 | struct _AccountViewPrivate |
| 53 | { |
| 54 | GtkWidget *treeview_account_list; |
| 55 | GtkWidget *stack_account; |
| 56 | GtkWidget *current_account_notebook; |
Stepan Salenikovich | c3fede2 | 2015-03-20 17:01:47 -0400 | [diff] [blame] | 57 | GtkWidget *button_remove_account; |
| 58 | GtkWidget *button_add_account; |
| 59 | GtkWidget *combobox_account_type; |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 60 | |
Stepan Salenikovich | 2d63d5e | 2015-03-22 23:23:54 -0400 | [diff] [blame] | 61 | gint current_page; /* keeps track of current notebook page displayed */ |
Stepan Salenikovich | c3fede2 | 2015-03-20 17:01:47 -0400 | [diff] [blame] | 62 | |
| 63 | ActiveItemProxyModel *active_protocols; |
Stepan Salenikovich | 2d63d5e | 2015-03-22 23:23:54 -0400 | [diff] [blame] | 64 | QMetaObject::Connection protocol_selection_changed; |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 65 | }; |
| 66 | |
Stepan Salenikovich | ab0f5be | 2016-02-05 15:16:35 -0500 | [diff] [blame] | 67 | G_DEFINE_TYPE_WITH_PRIVATE(AccountView, account_view, GTK_TYPE_PANED); |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 68 | |
| 69 | #define ACCOUNT_VIEW_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), ACCOUNT_VIEW_TYPE, AccountViewPrivate)) |
| 70 | |
| 71 | static void |
| 72 | account_view_dispose(GObject *object) |
| 73 | { |
| 74 | AccountView *view = ACCOUNT_VIEW(object); |
| 75 | AccountViewPrivate *priv = ACCOUNT_VIEW_GET_PRIVATE(view); |
| 76 | |
Stepan Salenikovich | 2d63d5e | 2015-03-22 23:23:54 -0400 | [diff] [blame] | 77 | QObject::disconnect(priv->protocol_selection_changed); |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 78 | |
| 79 | G_OBJECT_CLASS(account_view_parent_class)->dispose(object); |
| 80 | } |
| 81 | |
Stepan Salenikovich | 2d63d5e | 2015-03-22 23:23:54 -0400 | [diff] [blame] | 82 | static void |
| 83 | account_view_finalize(GObject *object) |
| 84 | { |
| 85 | AccountView *view = ACCOUNT_VIEW(object); |
| 86 | AccountViewPrivate *priv = ACCOUNT_VIEW_GET_PRIVATE(view); |
| 87 | |
| 88 | delete priv->active_protocols; |
| 89 | |
| 90 | G_OBJECT_CLASS(account_view_parent_class)->finalize(object); |
| 91 | } |
| 92 | |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 93 | static void |
Stepan Salenikovich | c3fede2 | 2015-03-20 17:01:47 -0400 | [diff] [blame] | 94 | update_account_model_selection(GtkTreeSelection *selection, G_GNUC_UNUSED gpointer user_data) |
| 95 | { |
| 96 | QModelIndex current = get_index_from_selection(selection); |
| 97 | if (current.isValid()) |
Guillaume Roguez | 5d1514b | 2015-10-22 15:55:31 -0400 | [diff] [blame] | 98 | AccountModel::instance().selectionModel()->setCurrentIndex(current, QItemSelectionModel::ClearAndSelect); |
Stepan Salenikovich | c3fede2 | 2015-03-20 17:01:47 -0400 | [diff] [blame] | 99 | else |
Guillaume Roguez | 5d1514b | 2015-10-22 15:55:31 -0400 | [diff] [blame] | 100 | AccountModel::instance().selectionModel()->clearCurrentIndex(); |
Stepan Salenikovich | c3fede2 | 2015-03-20 17:01:47 -0400 | [diff] [blame] | 101 | } |
| 102 | |
Stepan Salenikovich | d45e639 | 2015-08-11 10:16:35 -0400 | [diff] [blame] | 103 | static GtkWidget * |
| 104 | create_scrolled_account_view(GtkWidget *account_view) |
| 105 | { |
| 106 | auto scrolled = gtk_scrolled_window_new(NULL, NULL); |
| 107 | gtk_container_add(GTK_CONTAINER(scrolled), account_view); |
| 108 | return scrolled; |
| 109 | } |
| 110 | |
Stepan Salenikovich | c3fede2 | 2015-03-20 17:01:47 -0400 | [diff] [blame] | 111 | static void |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 112 | account_selection_changed(GtkTreeSelection *selection, AccountView *view) |
| 113 | { |
| 114 | g_return_if_fail(IS_ACCOUNT_VIEW(view)); |
| 115 | AccountViewPrivate *priv = ACCOUNT_VIEW_GET_PRIVATE(view); |
| 116 | |
| 117 | GtkWidget *old_account_view = gtk_stack_get_visible_child(GTK_STACK(priv->stack_account)); |
| 118 | |
| 119 | /* keep track of the last tab displayed */ |
| 120 | if (priv->current_account_notebook) |
| 121 | priv->current_page = gtk_notebook_get_current_page(GTK_NOTEBOOK(priv->current_account_notebook)); |
| 122 | |
| 123 | if (priv->current_page < 0) |
| 124 | priv->current_page = 0; |
| 125 | |
| 126 | QModelIndex account_idx = get_index_from_selection(selection); |
| 127 | if (!account_idx.isValid()) { |
| 128 | /* it nothing is slected, simply display something empty */ |
| 129 | GtkWidget *empty_box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0); |
| 130 | gtk_widget_show(empty_box); |
| 131 | gtk_stack_add_named(GTK_STACK(priv->stack_account), empty_box, "placeholder"); |
| 132 | gtk_stack_set_visible_child(GTK_STACK(priv->stack_account), empty_box); |
| 133 | priv->current_account_notebook = NULL; |
| 134 | } else { |
Guillaume Roguez | 5d1514b | 2015-10-22 15:55:31 -0400 | [diff] [blame] | 135 | Account *account = AccountModel::instance().getAccountByModelIndex(account_idx); |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 136 | |
| 137 | /* build new account view */ |
| 138 | GtkWidget *hbox_account = gtk_box_new(GTK_ORIENTATION_VERTICAL, 10); |
| 139 | |
| 140 | /* create account notebook */ |
| 141 | priv->current_account_notebook = gtk_notebook_new(); |
Stepan Salenikovich | ab0f5be | 2016-02-05 15:16:35 -0500 | [diff] [blame] | 142 | gtk_notebook_set_scrollable(GTK_NOTEBOOK(priv->current_account_notebook), TRUE); |
| 143 | gtk_notebook_set_show_border(GTK_NOTEBOOK(priv->current_account_notebook), FALSE); |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 144 | gtk_box_pack_start(GTK_BOX(hbox_account), priv->current_account_notebook, TRUE, TRUE, 0); |
| 145 | |
| 146 | /* customize account view based on account */ |
Stepan Salenikovich | d45e639 | 2015-08-11 10:16:35 -0400 | [diff] [blame] | 147 | auto general_tab = create_scrolled_account_view(account_general_tab_new(account)); |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 148 | gtk_notebook_append_page(GTK_NOTEBOOK(priv->current_account_notebook), |
| 149 | general_tab, |
Stepan Salenikovich | a1b8cb3 | 2015-09-11 14:58:35 -0400 | [diff] [blame] | 150 | gtk_label_new(C_("Account settings", "General"))); |
Stepan Salenikovich | d45e639 | 2015-08-11 10:16:35 -0400 | [diff] [blame] | 151 | auto audio_tab = create_scrolled_account_view(account_audio_tab_new(account)); |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 152 | gtk_notebook_append_page(GTK_NOTEBOOK(priv->current_account_notebook), |
| 153 | audio_tab, |
Stepan Salenikovich | a1b8cb3 | 2015-09-11 14:58:35 -0400 | [diff] [blame] | 154 | gtk_label_new(C_("Account settings", "Audio"))); |
Stepan Salenikovich | d45e639 | 2015-08-11 10:16:35 -0400 | [diff] [blame] | 155 | auto video_tab = create_scrolled_account_view(account_video_tab_new(account)); |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 156 | gtk_notebook_append_page(GTK_NOTEBOOK(priv->current_account_notebook), |
| 157 | video_tab, |
Stepan Salenikovich | a1b8cb3 | 2015-09-11 14:58:35 -0400 | [diff] [blame] | 158 | gtk_label_new(C_("Account settings", "Video"))); |
Stepan Salenikovich | d45e639 | 2015-08-11 10:16:35 -0400 | [diff] [blame] | 159 | auto advanced_tab = create_scrolled_account_view(account_advanced_tab_new(account)); |
Stepan Salenikovich | b7e6a73 | 2015-05-04 17:13:22 -0400 | [diff] [blame] | 160 | gtk_notebook_append_page(GTK_NOTEBOOK(priv->current_account_notebook), |
| 161 | advanced_tab, |
Stepan Salenikovich | a1b8cb3 | 2015-09-11 14:58:35 -0400 | [diff] [blame] | 162 | gtk_label_new(C_("Account settings", "Advanced"))); |
Stepan Salenikovich | d45e639 | 2015-08-11 10:16:35 -0400 | [diff] [blame] | 163 | auto security_tab = create_scrolled_account_view(account_security_tab_new(account)); |
Stepan Salenikovich | 7a1e71c | 2015-05-07 11:14:48 -0400 | [diff] [blame] | 164 | gtk_notebook_append_page(GTK_NOTEBOOK(priv->current_account_notebook), |
| 165 | security_tab, |
Stepan Salenikovich | a1b8cb3 | 2015-09-11 14:58:35 -0400 | [diff] [blame] | 166 | gtk_label_new(C_("Account settings", "Security"))); |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 167 | |
Stepan Salenikovich | ad58908 | 2016-05-18 13:22:52 -0400 | [diff] [blame^] | 168 | gtk_widget_show_all(hbox_account); |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 169 | /* set the tab displayed to the same as the prev account selected */ |
| 170 | gtk_notebook_set_current_page(GTK_NOTEBOOK(priv->current_account_notebook), priv->current_page); |
| 171 | |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 172 | /* set the new account view as visible */ |
| 173 | char *account_view_name = g_strdup_printf("%p_account", account); |
| 174 | gtk_stack_add_named(GTK_STACK(priv->stack_account), hbox_account, account_view_name); |
| 175 | gtk_stack_set_visible_child(GTK_STACK(priv->stack_account), hbox_account); |
| 176 | g_free(account_view_name); |
| 177 | } |
| 178 | |
| 179 | /* remove the old account view */ |
| 180 | if (old_account_view) |
| 181 | gtk_container_remove(GTK_CONTAINER(priv->stack_account), old_account_view); |
| 182 | } |
| 183 | |
| 184 | static void |
| 185 | account_active_toggled(GtkCellRendererToggle *renderer, gchar *path, AccountView *view) |
| 186 | { |
| 187 | g_return_if_fail(IS_ACCOUNT_VIEW(view)); |
| 188 | AccountViewPrivate *priv = ACCOUNT_VIEW_GET_PRIVATE(view); |
| 189 | |
| 190 | /* we want to set it to the opposite of the current value */ |
| 191 | gboolean toggle = !gtk_cell_renderer_toggle_get_active(renderer); |
| 192 | |
| 193 | /* get iter which was clicked */ |
| 194 | GtkTreePath *tree_path = gtk_tree_path_new_from_string(path); |
| 195 | GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(priv->treeview_account_list)); |
| 196 | GtkTreeIter iter; |
| 197 | gtk_tree_model_get_iter(model, &iter, tree_path); |
| 198 | |
| 199 | /* get qmodelindex from iter and set the model data */ |
| 200 | QModelIndex idx = gtk_q_tree_model_get_source_idx(GTK_Q_TREE_MODEL(model), &iter); |
| 201 | if (idx.isValid()) { |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 202 | QVariant alias = idx.data(static_cast<int>(Account::Role::Alias)); |
Stepan Salenikovich | fea8405 | 2016-05-13 16:32:51 -0400 | [diff] [blame] | 203 | AccountModel::instance().setData(idx, QVariant(toggle), Qt::CheckStateRole); |
| 204 | /* save the account to apply the changed state right away */ |
| 205 | AccountModel::instance().getAccountByModelIndex(idx)->performAction(Account::EditAction::SAVE); |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 206 | } |
| 207 | } |
| 208 | |
Stepan Salenikovich | c3fede2 | 2015-03-20 17:01:47 -0400 | [diff] [blame] | 209 | static gboolean |
Stepan Salenikovich | bd02958 | 2015-03-24 11:00:56 -0400 | [diff] [blame] | 210 | remove_account_dialog(AccountView *view, Account *account) |
Stepan Salenikovich | c3fede2 | 2015-03-20 17:01:47 -0400 | [diff] [blame] | 211 | { |
| 212 | gboolean response = FALSE; |
| 213 | GtkWidget *dialog = gtk_message_dialog_new(NULL, |
| 214 | (GtkDialogFlags)(GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT), |
| 215 | GTK_MESSAGE_QUESTION, GTK_BUTTONS_OK_CANCEL, |
Stepan Salenikovich | a1b8cb3 | 2015-09-11 14:58:35 -0400 | [diff] [blame] | 216 | _("Are you sure you want to delete account \"%s\"?"), |
Stepan Salenikovich | c3fede2 | 2015-03-20 17:01:47 -0400 | [diff] [blame] | 217 | account->alias().toLocal8Bit().constData()); |
| 218 | |
Stepan Salenikovich | bd02958 | 2015-03-24 11:00:56 -0400 | [diff] [blame] | 219 | gtk_window_set_destroy_with_parent(GTK_WINDOW(dialog), TRUE); |
| 220 | |
| 221 | /* get parent window so we can center on it */ |
| 222 | GtkWidget *parent = gtk_widget_get_toplevel(GTK_WIDGET(view)); |
| 223 | if (gtk_widget_is_toplevel(parent)) { |
| 224 | gtk_window_set_transient_for(GTK_WINDOW(dialog), GTK_WINDOW(parent)); |
| 225 | gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER_ON_PARENT); |
| 226 | } |
| 227 | |
Stepan Salenikovich | c3fede2 | 2015-03-20 17:01:47 -0400 | [diff] [blame] | 228 | switch (gtk_dialog_run(GTK_DIALOG(dialog))) { |
| 229 | case GTK_RESPONSE_OK: |
| 230 | response = TRUE; |
| 231 | break; |
| 232 | default: |
| 233 | response = FALSE; |
| 234 | break; |
| 235 | } |
| 236 | |
| 237 | gtk_widget_destroy(dialog); |
| 238 | |
| 239 | return response; |
| 240 | } |
| 241 | |
Stepan Salenikovich | bd02958 | 2015-03-24 11:00:56 -0400 | [diff] [blame] | 242 | static gboolean |
| 243 | save_account(GtkWidget *working_dialog) |
| 244 | { |
Guillaume Roguez | 5d1514b | 2015-10-22 15:55:31 -0400 | [diff] [blame] | 245 | AccountModel::instance().save(); |
Stepan Salenikovich | bd02958 | 2015-03-24 11:00:56 -0400 | [diff] [blame] | 246 | if (working_dialog) |
| 247 | gtk_widget_destroy(working_dialog); |
| 248 | |
| 249 | return G_SOURCE_REMOVE; |
| 250 | } |
| 251 | |
Stepan Salenikovich | c3fede2 | 2015-03-20 17:01:47 -0400 | [diff] [blame] | 252 | static void |
| 253 | remove_account(G_GNUC_UNUSED GtkWidget *entry, AccountView *view) |
| 254 | { |
| 255 | g_return_if_fail(IS_ACCOUNT_VIEW(view)); |
| 256 | AccountViewPrivate *priv = ACCOUNT_VIEW_GET_PRIVATE(view); |
| 257 | |
| 258 | GtkTreeSelection *selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_account_list)); |
| 259 | QModelIndex idx = get_index_from_selection(selection); |
| 260 | |
| 261 | if (idx.isValid()) { |
| 262 | /* this is a destructive operation, ask the user if they are sure */ |
Guillaume Roguez | 5d1514b | 2015-10-22 15:55:31 -0400 | [diff] [blame] | 263 | Account *account = AccountModel::instance().getAccountByModelIndex(idx); |
Stepan Salenikovich | bd02958 | 2015-03-24 11:00:56 -0400 | [diff] [blame] | 264 | if (remove_account_dialog(view, account)) { |
| 265 | /* show working dialog in case save operation takes time */ |
| 266 | GtkWidget *working = ring_dialog_working(GTK_WIDGET(view), NULL); |
| 267 | gtk_window_present(GTK_WINDOW(working)); |
| 268 | |
Guillaume Roguez | 5d1514b | 2015-10-22 15:55:31 -0400 | [diff] [blame] | 269 | AccountModel::instance().remove(idx); |
Stepan Salenikovich | bd02958 | 2015-03-24 11:00:56 -0400 | [diff] [blame] | 270 | |
| 271 | /* now save the time it takes to transition the account view to the new account (300ms) |
| 272 | * the save doesn't happen before the "working" dialog is presented |
| 273 | * the timeout function should destroy the "working" dialog when done saving |
| 274 | */ |
Stepan Salenikovich | 12fee94 | 2015-03-25 18:38:47 -0400 | [diff] [blame] | 275 | g_timeout_add_full(G_PRIORITY_DEFAULT, 300, (GSourceFunc)save_account, working, NULL); |
Stepan Salenikovich | c3fede2 | 2015-03-20 17:01:47 -0400 | [diff] [blame] | 276 | } |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | static void |
| 281 | add_account(G_GNUC_UNUSED GtkWidget *entry, AccountView *view) |
| 282 | { |
| 283 | g_return_if_fail(IS_ACCOUNT_VIEW(view)); |
| 284 | AccountViewPrivate *priv = ACCOUNT_VIEW_GET_PRIVATE(view); |
| 285 | |
| 286 | GtkTreeIter protocol_iter; |
| 287 | if (gtk_combo_box_get_active_iter(GTK_COMBO_BOX(priv->combobox_account_type), &protocol_iter)) { |
| 288 | /* get the qmodelindex of the protocol */ |
| 289 | GtkTreeModel *protocol_model = gtk_combo_box_get_model(GTK_COMBO_BOX(priv->combobox_account_type)); |
| 290 | QModelIndex protocol_idx = gtk_q_sort_filter_tree_model_get_source_idx( |
| 291 | GTK_Q_SORT_FILTER_TREE_MODEL(protocol_model), |
| 292 | &protocol_iter); |
| 293 | if (protocol_idx.isValid()) { |
| 294 | protocol_idx = priv->active_protocols->mapToSource(protocol_idx); |
Stepan Salenikovich | bd02958 | 2015-03-24 11:00:56 -0400 | [diff] [blame] | 295 | |
| 296 | /* show working dialog in case save operation takes time */ |
| 297 | GtkWidget *working = ring_dialog_working(GTK_WIDGET(view), NULL); |
| 298 | gtk_window_present(GTK_WINDOW(working)); |
| 299 | |
Guillaume Roguez | 5d1514b | 2015-10-22 15:55:31 -0400 | [diff] [blame] | 300 | auto account = AccountModel::instance().add(QString(_("New Account")), protocol_idx); |
Stepan Salenikovich | 75a3917 | 2015-07-10 13:21:08 -0400 | [diff] [blame] | 301 | if (account->protocol() == Account::Protocol::RING) |
Stepan Salenikovich | a1b8cb3 | 2015-09-11 14:58:35 -0400 | [diff] [blame] | 302 | account->setDisplayName(_("New Account")); |
Stepan Salenikovich | bd02958 | 2015-03-24 11:00:56 -0400 | [diff] [blame] | 303 | |
| 304 | /* now save after a short timeout to make sure that |
| 305 | * the save doesn't happen before the "working" dialog is presented |
| 306 | * the timeout function should destroy the "working" dialog when done saving |
| 307 | */ |
Stepan Salenikovich | 12fee94 | 2015-03-25 18:38:47 -0400 | [diff] [blame] | 308 | g_timeout_add_full(G_PRIORITY_DEFAULT, 300, (GSourceFunc)save_account, working, NULL); |
Stepan Salenikovich | c3fede2 | 2015-03-20 17:01:47 -0400 | [diff] [blame] | 309 | } |
| 310 | } |
| 311 | } |
| 312 | |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 313 | static void |
Stepan Salenikovich | d6a9ba9 | 2015-03-29 11:14:36 -0400 | [diff] [blame] | 314 | state_to_string(G_GNUC_UNUSED GtkTreeViewColumn *tree_column, |
| 315 | GtkCellRenderer *cell, |
| 316 | GtkTreeModel *tree_model, |
| 317 | GtkTreeIter *iter, |
Stepan Salenikovich | e4981b2 | 2015-10-22 15:22:59 -0400 | [diff] [blame] | 318 | GtkTreeView *treeview) |
Stepan Salenikovich | d6a9ba9 | 2015-03-29 11:14:36 -0400 | [diff] [blame] | 319 | { |
Stepan Salenikovich | e4981b2 | 2015-10-22 15:22:59 -0400 | [diff] [blame] | 320 | // check if this iter is selected |
| 321 | gboolean is_selected = FALSE; |
| 322 | if (GTK_IS_TREE_VIEW(treeview)) { |
| 323 | auto selection = gtk_tree_view_get_selection(treeview); |
| 324 | is_selected = gtk_tree_selection_iter_is_selected(selection, iter); |
| 325 | } |
| 326 | |
Stepan Salenikovich | d6a9ba9 | 2015-03-29 11:14:36 -0400 | [diff] [blame] | 327 | gchar *display_state = NULL; |
Stepan Salenikovich | e7a7314 | 2015-08-11 11:55:59 -0400 | [diff] [blame] | 328 | |
| 329 | /* get account */ |
| 330 | QModelIndex idx = gtk_q_tree_model_get_source_idx(GTK_Q_TREE_MODEL(tree_model), iter); |
| 331 | if (idx.isValid()) { |
| 332 | |
Guillaume Roguez | 5d1514b | 2015-10-22 15:55:31 -0400 | [diff] [blame] | 333 | auto account = AccountModel::instance().getAccountByModelIndex(idx); |
Stepan Salenikovich | e7a7314 | 2015-08-11 11:55:59 -0400 | [diff] [blame] | 334 | auto humanState = account->toHumanStateName(); |
| 335 | |
Stepan Salenikovich | 0731de0 | 2016-05-17 17:47:16 -0400 | [diff] [blame] | 336 | auto escaped_state = g_markup_escape_text(humanState.toUtf8().constData(), -1); |
Stepan Salenikovich | e4981b2 | 2015-10-22 15:22:59 -0400 | [diff] [blame] | 337 | /* we want the color of the status text to be the default color if this iter is |
| 338 | * selected so that the treeview is able to invert it against the selection color */ |
| 339 | if (is_selected) { |
Stepan Salenikovich | 0731de0 | 2016-05-17 17:47:16 -0400 | [diff] [blame] | 340 | display_state = escaped_state; |
Stepan Salenikovich | e4981b2 | 2015-10-22 15:22:59 -0400 | [diff] [blame] | 341 | } else { |
| 342 | switch (account->registrationState()) { |
| 343 | case Account::RegistrationState::READY: |
Stepan Salenikovich | 0731de0 | 2016-05-17 17:47:16 -0400 | [diff] [blame] | 344 | display_state = g_strdup_printf("<span fgcolor=\"green\">%s</span>", escaped_state); |
Stepan Salenikovich | e4981b2 | 2015-10-22 15:22:59 -0400 | [diff] [blame] | 345 | break; |
| 346 | case Account::RegistrationState::UNREGISTERED: |
Stepan Salenikovich | 0731de0 | 2016-05-17 17:47:16 -0400 | [diff] [blame] | 347 | display_state = g_strdup_printf("<span fgcolor=\"gray\">%s</span>", escaped_state); |
Stepan Salenikovich | e4981b2 | 2015-10-22 15:22:59 -0400 | [diff] [blame] | 348 | break; |
| 349 | case Account::RegistrationState::TRYING: |
Stepan Salenikovich | 0731de0 | 2016-05-17 17:47:16 -0400 | [diff] [blame] | 350 | display_state = g_strdup_printf("<span fgcolor=\"orange\">%s</span>", escaped_state); |
Stepan Salenikovich | e4981b2 | 2015-10-22 15:22:59 -0400 | [diff] [blame] | 351 | break; |
| 352 | case Account::RegistrationState::ERROR: |
Stepan Salenikovich | 0731de0 | 2016-05-17 17:47:16 -0400 | [diff] [blame] | 353 | display_state = g_strdup_printf("<span fgcolor=\"red\">%s</span>", escaped_state); |
Stepan Salenikovich | e4981b2 | 2015-10-22 15:22:59 -0400 | [diff] [blame] | 354 | break; |
| 355 | case Account::RegistrationState::COUNT__: |
| 356 | g_warning("registration state should never be \"count\""); |
Stepan Salenikovich | 0731de0 | 2016-05-17 17:47:16 -0400 | [diff] [blame] | 357 | display_state = g_strdup_printf("<span fgcolor=\"red\">%s</span>", escaped_state); |
Stepan Salenikovich | e4981b2 | 2015-10-22 15:22:59 -0400 | [diff] [blame] | 358 | break; |
| 359 | } |
Stepan Salenikovich | 0731de0 | 2016-05-17 17:47:16 -0400 | [diff] [blame] | 360 | g_free(escaped_state); |
Stepan Salenikovich | e7a7314 | 2015-08-11 11:55:59 -0400 | [diff] [blame] | 361 | } |
Stepan Salenikovich | d6a9ba9 | 2015-03-29 11:14:36 -0400 | [diff] [blame] | 362 | } |
Stepan Salenikovich | e7a7314 | 2015-08-11 11:55:59 -0400 | [diff] [blame] | 363 | |
Stepan Salenikovich | d6a9ba9 | 2015-03-29 11:14:36 -0400 | [diff] [blame] | 364 | g_object_set(G_OBJECT(cell), "markup", display_state, NULL); |
| 365 | g_free(display_state); |
| 366 | } |
| 367 | |
| 368 | static void |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 369 | account_view_init(AccountView *view) |
| 370 | { |
| 371 | gtk_widget_init_template(GTK_WIDGET(view)); |
| 372 | |
| 373 | AccountViewPrivate *priv = ACCOUNT_VIEW_GET_PRIVATE(view); |
| 374 | |
| 375 | /* account model */ |
| 376 | GtkQTreeModel *account_model; |
| 377 | GtkCellRenderer *renderer; |
| 378 | GtkTreeViewColumn *column; |
| 379 | |
Guillaume Roguez | 5d1514b | 2015-10-22 15:55:31 -0400 | [diff] [blame] | 380 | account_model = gtk_q_tree_model_new(&AccountModel::instance(), 4, |
aviau | 9bbb19b | 2016-05-16 15:53:44 -0400 | [diff] [blame] | 381 | 0, Account::Role::Enabled, G_TYPE_BOOLEAN, |
| 382 | 0, Account::Role::Alias, G_TYPE_STRING, |
| 383 | 0, Account::Role::Proto, G_TYPE_STRING, |
| 384 | 0, Account::Role::RegistrationState, G_TYPE_UINT); |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 385 | gtk_tree_view_set_model(GTK_TREE_VIEW(priv->treeview_account_list), GTK_TREE_MODEL(account_model)); |
| 386 | |
| 387 | renderer = gtk_cell_renderer_toggle_new(); |
Stepan Salenikovich | a1b8cb3 | 2015-09-11 14:58:35 -0400 | [diff] [blame] | 388 | column = gtk_tree_view_column_new_with_attributes(C_("Account state column", "Enabled"), renderer, "active", 0, NULL); |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 389 | gtk_tree_view_append_column(GTK_TREE_VIEW(priv->treeview_account_list), column); |
| 390 | |
| 391 | g_signal_connect(renderer, "toggled", G_CALLBACK(account_active_toggled), view); |
| 392 | |
| 393 | renderer = gtk_cell_renderer_text_new(); |
Stepan Salenikovich | a1b8cb3 | 2015-09-11 14:58:35 -0400 | [diff] [blame] | 394 | column = gtk_tree_view_column_new_with_attributes(C_("Account alias (name) column", "Alias"), renderer, "text", 1, NULL); |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 395 | gtk_tree_view_append_column(GTK_TREE_VIEW(priv->treeview_account_list), column); |
Stepan Salenikovich | ab0f5be | 2016-02-05 15:16:35 -0500 | [diff] [blame] | 396 | g_object_set(G_OBJECT(renderer), "ellipsize", PANGO_ELLIPSIZE_END, NULL); |
| 397 | gtk_tree_view_column_set_expand(column, TRUE); |
| 398 | // set a min width so most of the account name is visible |
| 399 | g_object_set(G_OBJECT(renderer), "width", 75, NULL); |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 400 | |
| 401 | renderer = gtk_cell_renderer_text_new(); |
Stepan Salenikovich | a1b8cb3 | 2015-09-11 14:58:35 -0400 | [diff] [blame] | 402 | column = gtk_tree_view_column_new_with_attributes(C_("Account status column", "Status"), renderer, "text", 3, NULL); |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 403 | gtk_tree_view_append_column(GTK_TREE_VIEW(priv->treeview_account_list), column); |
Stepan Salenikovich | ab0f5be | 2016-02-05 15:16:35 -0500 | [diff] [blame] | 404 | g_object_set(G_OBJECT(renderer), "ellipsize", PANGO_ELLIPSIZE_END, NULL); |
| 405 | gtk_tree_view_column_set_expand(column, TRUE); |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 406 | |
Stepan Salenikovich | d6a9ba9 | 2015-03-29 11:14:36 -0400 | [diff] [blame] | 407 | /* the registration state is an enum, we want to display it as a string */ |
| 408 | gtk_tree_view_column_set_cell_data_func( |
| 409 | column, |
| 410 | renderer, |
| 411 | (GtkTreeCellDataFunc)state_to_string, |
Stepan Salenikovich | e4981b2 | 2015-10-22 15:22:59 -0400 | [diff] [blame] | 412 | priv->treeview_account_list, |
Stepan Salenikovich | d6a9ba9 | 2015-03-29 11:14:36 -0400 | [diff] [blame] | 413 | NULL); |
| 414 | |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 415 | /* add an empty box to the account stack initially, otherwise there will |
| 416 | * be no cool animation when the first account is selected */ |
| 417 | GtkWidget *empty_box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0); |
| 418 | gtk_widget_show(empty_box); |
| 419 | gtk_stack_add_named(GTK_STACK(priv->stack_account), empty_box, "placeholder"); |
| 420 | gtk_stack_set_visible_child(GTK_STACK(priv->stack_account), empty_box); |
Stepan Salenikovich | c3fede2 | 2015-03-20 17:01:47 -0400 | [diff] [blame] | 421 | |
| 422 | /* populate account type combo box */ |
| 423 | /* TODO: when to delete this model? */ |
Guillaume Roguez | 5d1514b | 2015-10-22 15:55:31 -0400 | [diff] [blame] | 424 | priv->active_protocols = new ActiveItemProxyModel((QAbstractItemModel *)AccountModel::instance().protocolModel()); |
Stepan Salenikovich | c3fede2 | 2015-03-20 17:01:47 -0400 | [diff] [blame] | 425 | |
| 426 | GtkQSortFilterTreeModel *protocol_model = gtk_q_sort_filter_tree_model_new( |
| 427 | (QSortFilterProxyModel *)priv->active_protocols, |
| 428 | 1, |
aviau | 271bcc2 | 2016-05-27 17:25:19 -0400 | [diff] [blame] | 429 | 0, Qt::DisplayRole, G_TYPE_STRING); |
Stepan Salenikovich | c3fede2 | 2015-03-20 17:01:47 -0400 | [diff] [blame] | 430 | |
| 431 | gtk_combo_box_set_model(GTK_COMBO_BOX(priv->combobox_account_type), GTK_TREE_MODEL(protocol_model)); |
| 432 | |
| 433 | renderer = gtk_cell_renderer_text_new(); |
| 434 | gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(priv->combobox_account_type), renderer, FALSE); |
| 435 | gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(priv->combobox_account_type), renderer, |
| 436 | "text", 0, NULL); |
| 437 | |
| 438 | /* connect signals to and from the selection model of the account model */ |
Stepan Salenikovich | 2d63d5e | 2015-03-22 23:23:54 -0400 | [diff] [blame] | 439 | priv->protocol_selection_changed = QObject::connect( |
Guillaume Roguez | 5d1514b | 2015-10-22 15:55:31 -0400 | [diff] [blame] | 440 | AccountModel::instance().selectionModel(), |
Stepan Salenikovich | c3fede2 | 2015-03-20 17:01:47 -0400 | [diff] [blame] | 441 | &QItemSelectionModel::currentChanged, |
Stepan Salenikovich | 825bb0a | 2016-03-11 15:33:52 -0500 | [diff] [blame] | 442 | [=](const QModelIndex & current, G_GNUC_UNUSED const QModelIndex & previous) { |
Stepan Salenikovich | c3fede2 | 2015-03-20 17:01:47 -0400 | [diff] [blame] | 443 | GtkTreeSelection *selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_account_list)); |
| 444 | |
Stepan Salenikovich | c3fede2 | 2015-03-20 17:01:47 -0400 | [diff] [blame] | 445 | /* select the current */ |
| 446 | if (current.isValid()) { |
| 447 | GtkTreeIter new_iter; |
| 448 | if (gtk_q_tree_model_source_index_to_iter(account_model, current, &new_iter)) { |
| 449 | gtk_tree_selection_select_iter(selection, &new_iter); |
| 450 | } else { |
Stepan Salenikovich | f903d1b | 2015-03-25 14:51:45 -0400 | [diff] [blame] | 451 | g_warning("SelectionModel of AccountModel changed to invalid QModelIndex?"); |
Stepan Salenikovich | c3fede2 | 2015-03-20 17:01:47 -0400 | [diff] [blame] | 452 | } |
| 453 | } |
| 454 | } |
| 455 | ); |
| 456 | |
| 457 | GtkTreeSelection *account_selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_account_list)); |
| 458 | g_signal_connect(account_selection, "changed", G_CALLBACK(update_account_model_selection), NULL); |
| 459 | g_signal_connect(account_selection, "changed", G_CALLBACK(account_selection_changed), view); |
| 460 | |
| 461 | /* select the default protocol */ |
Guillaume Roguez | 5d1514b | 2015-10-22 15:55:31 -0400 | [diff] [blame] | 462 | QModelIndex protocol_idx = AccountModel::instance().protocolModel()->selectionModel()->currentIndex(); |
Stepan Salenikovich | c3fede2 | 2015-03-20 17:01:47 -0400 | [diff] [blame] | 463 | if (protocol_idx.isValid()) { |
| 464 | protocol_idx = priv->active_protocols->mapFromSource(protocol_idx); |
| 465 | GtkTreeIter protocol_iter; |
| 466 | if (gtk_q_sort_filter_tree_model_source_index_to_iter( |
| 467 | (GtkQSortFilterTreeModel *)protocol_model, |
| 468 | protocol_idx, |
| 469 | &protocol_iter)) { |
| 470 | gtk_combo_box_set_active_iter(GTK_COMBO_BOX(priv->combobox_account_type), &protocol_iter); |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | /* connect signals to add/remove accounts */ |
| 475 | g_signal_connect(priv->button_remove_account, "clicked", G_CALLBACK(remove_account), view); |
| 476 | g_signal_connect(priv->button_add_account, "clicked", G_CALLBACK(add_account), view); |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | static void |
| 480 | account_view_class_init(AccountViewClass *klass) |
| 481 | { |
| 482 | G_OBJECT_CLASS(klass)->dispose = account_view_dispose; |
Stepan Salenikovich | 2d63d5e | 2015-03-22 23:23:54 -0400 | [diff] [blame] | 483 | G_OBJECT_CLASS(klass)->finalize = account_view_finalize; |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 484 | |
| 485 | gtk_widget_class_set_template_from_resource(GTK_WIDGET_CLASS (klass), |
| 486 | "/cx/ring/RingGnome/accountview.ui"); |
| 487 | |
| 488 | gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), AccountView, treeview_account_list); |
| 489 | gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), AccountView, stack_account); |
Stepan Salenikovich | c3fede2 | 2015-03-20 17:01:47 -0400 | [diff] [blame] | 490 | gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), AccountView, button_remove_account); |
| 491 | gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), AccountView, button_add_account); |
| 492 | gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), AccountView, combobox_account_type); |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | GtkWidget * |
| 496 | account_view_new(void) |
| 497 | { |
| 498 | return (GtkWidget *)g_object_new(ACCOUNT_VIEW_TYPE, NULL); |
| 499 | } |