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 | bdb0ecd | 2016-05-18 13:30:55 -0400 | [diff] [blame^] | 39 | #include "accountimportexportview.h" |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 40 | |
| 41 | struct _AccountView |
| 42 | { |
Stepan Salenikovich | ab0f5be | 2016-02-05 15:16:35 -0500 | [diff] [blame] | 43 | GtkPaned parent; |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 44 | }; |
| 45 | |
| 46 | struct _AccountViewClass |
| 47 | { |
Stepan Salenikovich | ab0f5be | 2016-02-05 15:16:35 -0500 | [diff] [blame] | 48 | GtkPanedClass parent_class; |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 49 | }; |
| 50 | |
| 51 | typedef struct _AccountViewPrivate AccountViewPrivate; |
| 52 | |
| 53 | struct _AccountViewPrivate |
| 54 | { |
| 55 | GtkWidget *treeview_account_list; |
Stepan Salenikovich | bdb0ecd | 2016-05-18 13:30:55 -0400 | [diff] [blame^] | 56 | GtkWidget *account_import_view; |
| 57 | GtkWidget *account_export_view; |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 58 | GtkWidget *stack_account; |
Stepan Salenikovich | c3fede2 | 2015-03-20 17:01:47 -0400 | [diff] [blame] | 59 | GtkWidget *button_remove_account; |
| 60 | GtkWidget *button_add_account; |
| 61 | GtkWidget *combobox_account_type; |
Stepan Salenikovich | bdb0ecd | 2016-05-18 13:30:55 -0400 | [diff] [blame^] | 62 | GtkWidget *button_import_account; |
| 63 | GtkWidget *button_export_account; |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 64 | |
Stepan Salenikovich | 2d63d5e | 2015-03-22 23:23:54 -0400 | [diff] [blame] | 65 | gint current_page; /* keeps track of current notebook page displayed */ |
Stepan Salenikovich | c3fede2 | 2015-03-20 17:01:47 -0400 | [diff] [blame] | 66 | |
| 67 | ActiveItemProxyModel *active_protocols; |
Stepan Salenikovich | 2d63d5e | 2015-03-22 23:23:54 -0400 | [diff] [blame] | 68 | QMetaObject::Connection protocol_selection_changed; |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 69 | }; |
| 70 | |
Stepan Salenikovich | ab0f5be | 2016-02-05 15:16:35 -0500 | [diff] [blame] | 71 | G_DEFINE_TYPE_WITH_PRIVATE(AccountView, account_view, GTK_TYPE_PANED); |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 72 | |
| 73 | #define ACCOUNT_VIEW_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), ACCOUNT_VIEW_TYPE, AccountViewPrivate)) |
| 74 | |
| 75 | static void |
| 76 | account_view_dispose(GObject *object) |
| 77 | { |
| 78 | AccountView *view = ACCOUNT_VIEW(object); |
| 79 | AccountViewPrivate *priv = ACCOUNT_VIEW_GET_PRIVATE(view); |
| 80 | |
Stepan Salenikovich | 2d63d5e | 2015-03-22 23:23:54 -0400 | [diff] [blame] | 81 | QObject::disconnect(priv->protocol_selection_changed); |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 82 | |
| 83 | G_OBJECT_CLASS(account_view_parent_class)->dispose(object); |
| 84 | } |
| 85 | |
Stepan Salenikovich | 2d63d5e | 2015-03-22 23:23:54 -0400 | [diff] [blame] | 86 | static void |
| 87 | account_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 Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 97 | static void |
Stepan Salenikovich | c3fede2 | 2015-03-20 17:01:47 -0400 | [diff] [blame] | 98 | update_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 Roguez | 5d1514b | 2015-10-22 15:55:31 -0400 | [diff] [blame] | 102 | AccountModel::instance().selectionModel()->setCurrentIndex(current, QItemSelectionModel::ClearAndSelect); |
Stepan Salenikovich | c3fede2 | 2015-03-20 17:01:47 -0400 | [diff] [blame] | 103 | else |
Guillaume Roguez | 5d1514b | 2015-10-22 15:55:31 -0400 | [diff] [blame] | 104 | AccountModel::instance().selectionModel()->clearCurrentIndex(); |
Stepan Salenikovich | c3fede2 | 2015-03-20 17:01:47 -0400 | [diff] [blame] | 105 | } |
| 106 | |
Stepan Salenikovich | d45e639 | 2015-08-11 10:16:35 -0400 | [diff] [blame] | 107 | static GtkWidget * |
| 108 | create_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 Salenikovich | c3fede2 | 2015-03-20 17:01:47 -0400 | [diff] [blame] | 115 | static void |
Stepan Salenikovich | bdb0ecd | 2016-05-18 13:30:55 -0400 | [diff] [blame^] | 116 | tab_selection_changed(G_GNUC_UNUSED GtkNotebook *notebook, |
| 117 | G_GNUC_UNUSED GtkWidget *page, |
| 118 | guint page_num, |
| 119 | AccountView *self) |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 120 | { |
Stepan Salenikovich | bdb0ecd | 2016-05-18 13:30:55 -0400 | [diff] [blame^] | 121 | g_return_if_fail(IS_ACCOUNT_VIEW(self)); |
| 122 | AccountViewPrivate *priv = ACCOUNT_VIEW_GET_PRIVATE(self); |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 123 | |
Stepan Salenikovich | bdb0ecd | 2016-05-18 13:30:55 -0400 | [diff] [blame^] | 124 | priv->current_page = page_num; |
| 125 | } |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 126 | |
Stepan Salenikovich | bdb0ecd | 2016-05-18 13:30:55 -0400 | [diff] [blame^] | 127 | static void |
| 128 | account_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 Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 132 | |
Stepan Salenikovich | bdb0ecd | 2016-05-18 13:30:55 -0400 | [diff] [blame^] | 133 | auto old_view = gtk_stack_get_visible_child(GTK_STACK(priv->stack_account)); |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 134 | |
| 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 Salenikovich | bdb0ecd | 2016-05-18 13:30:55 -0400 | [diff] [blame^] | 142 | |
| 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 Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 146 | } else { |
Guillaume Roguez | 5d1514b | 2015-10-22 15:55:31 -0400 | [diff] [blame] | 147 | Account *account = AccountModel::instance().getAccountByModelIndex(account_idx); |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 148 | |
| 149 | /* build new account view */ |
| 150 | GtkWidget *hbox_account = gtk_box_new(GTK_ORIENTATION_VERTICAL, 10); |
| 151 | |
| 152 | /* create account notebook */ |
Stepan Salenikovich | bdb0ecd | 2016-05-18 13:30:55 -0400 | [diff] [blame^] | 153 | 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 Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 157 | |
| 158 | /* customize account view based on account */ |
Stepan Salenikovich | d45e639 | 2015-08-11 10:16:35 -0400 | [diff] [blame] | 159 | auto general_tab = create_scrolled_account_view(account_general_tab_new(account)); |
Stepan Salenikovich | bdb0ecd | 2016-05-18 13:30:55 -0400 | [diff] [blame^] | 160 | gtk_notebook_append_page(GTK_NOTEBOOK(account_notebook), |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 161 | general_tab, |
Stepan Salenikovich | a1b8cb3 | 2015-09-11 14:58:35 -0400 | [diff] [blame] | 162 | gtk_label_new(C_("Account settings", "General"))); |
Stepan Salenikovich | d45e639 | 2015-08-11 10:16:35 -0400 | [diff] [blame] | 163 | auto audio_tab = create_scrolled_account_view(account_audio_tab_new(account)); |
Stepan Salenikovich | bdb0ecd | 2016-05-18 13:30:55 -0400 | [diff] [blame^] | 164 | gtk_notebook_append_page(GTK_NOTEBOOK(account_notebook), |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 165 | audio_tab, |
Stepan Salenikovich | a1b8cb3 | 2015-09-11 14:58:35 -0400 | [diff] [blame] | 166 | gtk_label_new(C_("Account settings", "Audio"))); |
Stepan Salenikovich | d45e639 | 2015-08-11 10:16:35 -0400 | [diff] [blame] | 167 | auto video_tab = create_scrolled_account_view(account_video_tab_new(account)); |
Stepan Salenikovich | bdb0ecd | 2016-05-18 13:30:55 -0400 | [diff] [blame^] | 168 | gtk_notebook_append_page(GTK_NOTEBOOK(account_notebook), |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 169 | video_tab, |
Stepan Salenikovich | a1b8cb3 | 2015-09-11 14:58:35 -0400 | [diff] [blame] | 170 | gtk_label_new(C_("Account settings", "Video"))); |
Stepan Salenikovich | d45e639 | 2015-08-11 10:16:35 -0400 | [diff] [blame] | 171 | auto advanced_tab = create_scrolled_account_view(account_advanced_tab_new(account)); |
Stepan Salenikovich | bdb0ecd | 2016-05-18 13:30:55 -0400 | [diff] [blame^] | 172 | gtk_notebook_append_page(GTK_NOTEBOOK(account_notebook), |
Stepan Salenikovich | b7e6a73 | 2015-05-04 17:13:22 -0400 | [diff] [blame] | 173 | advanced_tab, |
Stepan Salenikovich | a1b8cb3 | 2015-09-11 14:58:35 -0400 | [diff] [blame] | 174 | gtk_label_new(C_("Account settings", "Advanced"))); |
Stepan Salenikovich | d45e639 | 2015-08-11 10:16:35 -0400 | [diff] [blame] | 175 | auto security_tab = create_scrolled_account_view(account_security_tab_new(account)); |
Stepan Salenikovich | bdb0ecd | 2016-05-18 13:30:55 -0400 | [diff] [blame^] | 176 | gtk_notebook_append_page(GTK_NOTEBOOK(account_notebook), |
Stepan Salenikovich | 7a1e71c | 2015-05-07 11:14:48 -0400 | [diff] [blame] | 177 | security_tab, |
Stepan Salenikovich | a1b8cb3 | 2015-09-11 14:58:35 -0400 | [diff] [blame] | 178 | gtk_label_new(C_("Account settings", "Security"))); |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 179 | |
Stepan Salenikovich | ad58908 | 2016-05-18 13:22:52 -0400 | [diff] [blame] | 180 | gtk_widget_show_all(hbox_account); |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 181 | /* set the tab displayed to the same as the prev account selected */ |
Stepan Salenikovich | bdb0ecd | 2016-05-18 13:30:55 -0400 | [diff] [blame^] | 182 | 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 Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 185 | |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 186 | /* 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 Salenikovich | bdb0ecd | 2016-05-18 13:30:55 -0400 | [diff] [blame^] | 191 | |
| 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 Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 195 | } |
| 196 | |
Stepan Salenikovich | bdb0ecd | 2016-05-18 13:30:55 -0400 | [diff] [blame^] | 197 | /* remove the old view */ |
| 198 | if (old_view) |
| 199 | gtk_container_remove(GTK_CONTAINER(priv->stack_account), old_view); |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | static void |
| 203 | account_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 Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 220 | QVariant alias = idx.data(static_cast<int>(Account::Role::Alias)); |
Stepan Salenikovich | fea8405 | 2016-05-13 16:32:51 -0400 | [diff] [blame] | 221 | 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 Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 224 | } |
| 225 | } |
| 226 | |
Stepan Salenikovich | c3fede2 | 2015-03-20 17:01:47 -0400 | [diff] [blame] | 227 | static gboolean |
Stepan Salenikovich | bd02958 | 2015-03-24 11:00:56 -0400 | [diff] [blame] | 228 | remove_account_dialog(AccountView *view, Account *account) |
Stepan Salenikovich | c3fede2 | 2015-03-20 17:01:47 -0400 | [diff] [blame] | 229 | { |
| 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 Salenikovich | a1b8cb3 | 2015-09-11 14:58:35 -0400 | [diff] [blame] | 234 | _("Are you sure you want to delete account \"%s\"?"), |
Stepan Salenikovich | c3fede2 | 2015-03-20 17:01:47 -0400 | [diff] [blame] | 235 | account->alias().toLocal8Bit().constData()); |
| 236 | |
Stepan Salenikovich | bd02958 | 2015-03-24 11:00:56 -0400 | [diff] [blame] | 237 | 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 Salenikovich | c3fede2 | 2015-03-20 17:01:47 -0400 | [diff] [blame] | 246 | 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 Salenikovich | bd02958 | 2015-03-24 11:00:56 -0400 | [diff] [blame] | 260 | static gboolean |
| 261 | save_account(GtkWidget *working_dialog) |
| 262 | { |
Guillaume Roguez | 5d1514b | 2015-10-22 15:55:31 -0400 | [diff] [blame] | 263 | AccountModel::instance().save(); |
Stepan Salenikovich | bd02958 | 2015-03-24 11:00:56 -0400 | [diff] [blame] | 264 | if (working_dialog) |
| 265 | gtk_widget_destroy(working_dialog); |
| 266 | |
| 267 | return G_SOURCE_REMOVE; |
| 268 | } |
| 269 | |
Stepan Salenikovich | c3fede2 | 2015-03-20 17:01:47 -0400 | [diff] [blame] | 270 | static void |
| 271 | remove_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 Roguez | 5d1514b | 2015-10-22 15:55:31 -0400 | [diff] [blame] | 281 | Account *account = AccountModel::instance().getAccountByModelIndex(idx); |
Stepan Salenikovich | bd02958 | 2015-03-24 11:00:56 -0400 | [diff] [blame] | 282 | 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 Roguez | 5d1514b | 2015-10-22 15:55:31 -0400 | [diff] [blame] | 287 | AccountModel::instance().remove(idx); |
Stepan Salenikovich | bd02958 | 2015-03-24 11:00:56 -0400 | [diff] [blame] | 288 | |
| 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 Salenikovich | 12fee94 | 2015-03-25 18:38:47 -0400 | [diff] [blame] | 293 | 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] | 294 | } |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | static void |
| 299 | add_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 Salenikovich | bd02958 | 2015-03-24 11:00:56 -0400 | [diff] [blame] | 313 | |
| 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 Roguez | 5d1514b | 2015-10-22 15:55:31 -0400 | [diff] [blame] | 318 | auto account = AccountModel::instance().add(QString(_("New Account")), protocol_idx); |
Stepan Salenikovich | 75a3917 | 2015-07-10 13:21:08 -0400 | [diff] [blame] | 319 | if (account->protocol() == Account::Protocol::RING) |
Stepan Salenikovich | a1b8cb3 | 2015-09-11 14:58:35 -0400 | [diff] [blame] | 320 | account->setDisplayName(_("New Account")); |
Stepan Salenikovich | bd02958 | 2015-03-24 11:00:56 -0400 | [diff] [blame] | 321 | |
| 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 Salenikovich | 12fee94 | 2015-03-25 18:38:47 -0400 | [diff] [blame] | 326 | g_timeout_add_full(G_PRIORITY_DEFAULT, 300, (GSourceFunc)save_account, working, NULL); |
Stepan Salenikovich | c3fede2 | 2015-03-20 17:01:47 -0400 | [diff] [blame] | 327 | } |
| 328 | } |
| 329 | } |
| 330 | |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 331 | static void |
Stepan Salenikovich | d6a9ba9 | 2015-03-29 11:14:36 -0400 | [diff] [blame] | 332 | state_to_string(G_GNUC_UNUSED GtkTreeViewColumn *tree_column, |
| 333 | GtkCellRenderer *cell, |
| 334 | GtkTreeModel *tree_model, |
| 335 | GtkTreeIter *iter, |
Stepan Salenikovich | e4981b2 | 2015-10-22 15:22:59 -0400 | [diff] [blame] | 336 | GtkTreeView *treeview) |
Stepan Salenikovich | d6a9ba9 | 2015-03-29 11:14:36 -0400 | [diff] [blame] | 337 | { |
Stepan Salenikovich | e4981b2 | 2015-10-22 15:22:59 -0400 | [diff] [blame] | 338 | // 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 Salenikovich | d6a9ba9 | 2015-03-29 11:14:36 -0400 | [diff] [blame] | 345 | gchar *display_state = NULL; |
Stepan Salenikovich | e7a7314 | 2015-08-11 11:55:59 -0400 | [diff] [blame] | 346 | |
| 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 Roguez | 5d1514b | 2015-10-22 15:55:31 -0400 | [diff] [blame] | 351 | auto account = AccountModel::instance().getAccountByModelIndex(idx); |
Stepan Salenikovich | e7a7314 | 2015-08-11 11:55:59 -0400 | [diff] [blame] | 352 | auto humanState = account->toHumanStateName(); |
| 353 | |
Stepan Salenikovich | 0731de0 | 2016-05-17 17:47:16 -0400 | [diff] [blame] | 354 | auto escaped_state = g_markup_escape_text(humanState.toUtf8().constData(), -1); |
Stepan Salenikovich | e4981b2 | 2015-10-22 15:22:59 -0400 | [diff] [blame] | 355 | /* 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 Salenikovich | 0731de0 | 2016-05-17 17:47:16 -0400 | [diff] [blame] | 358 | display_state = escaped_state; |
Stepan Salenikovich | e4981b2 | 2015-10-22 15:22:59 -0400 | [diff] [blame] | 359 | } else { |
| 360 | switch (account->registrationState()) { |
| 361 | case Account::RegistrationState::READY: |
Stepan Salenikovich | 0731de0 | 2016-05-17 17:47:16 -0400 | [diff] [blame] | 362 | display_state = g_strdup_printf("<span fgcolor=\"green\">%s</span>", escaped_state); |
Stepan Salenikovich | e4981b2 | 2015-10-22 15:22:59 -0400 | [diff] [blame] | 363 | break; |
| 364 | case Account::RegistrationState::UNREGISTERED: |
Stepan Salenikovich | 0731de0 | 2016-05-17 17:47:16 -0400 | [diff] [blame] | 365 | display_state = g_strdup_printf("<span fgcolor=\"gray\">%s</span>", escaped_state); |
Stepan Salenikovich | e4981b2 | 2015-10-22 15:22:59 -0400 | [diff] [blame] | 366 | break; |
| 367 | case Account::RegistrationState::TRYING: |
Stepan Salenikovich | 0731de0 | 2016-05-17 17:47:16 -0400 | [diff] [blame] | 368 | display_state = g_strdup_printf("<span fgcolor=\"orange\">%s</span>", escaped_state); |
Stepan Salenikovich | e4981b2 | 2015-10-22 15:22:59 -0400 | [diff] [blame] | 369 | break; |
| 370 | case Account::RegistrationState::ERROR: |
Stepan Salenikovich | 0731de0 | 2016-05-17 17:47:16 -0400 | [diff] [blame] | 371 | display_state = g_strdup_printf("<span fgcolor=\"red\">%s</span>", escaped_state); |
Stepan Salenikovich | e4981b2 | 2015-10-22 15:22:59 -0400 | [diff] [blame] | 372 | break; |
| 373 | case Account::RegistrationState::COUNT__: |
| 374 | g_warning("registration state should never be \"count\""); |
Stepan Salenikovich | 0731de0 | 2016-05-17 17:47:16 -0400 | [diff] [blame] | 375 | display_state = g_strdup_printf("<span fgcolor=\"red\">%s</span>", escaped_state); |
Stepan Salenikovich | e4981b2 | 2015-10-22 15:22:59 -0400 | [diff] [blame] | 376 | break; |
| 377 | } |
Stepan Salenikovich | 0731de0 | 2016-05-17 17:47:16 -0400 | [diff] [blame] | 378 | g_free(escaped_state); |
Stepan Salenikovich | e7a7314 | 2015-08-11 11:55:59 -0400 | [diff] [blame] | 379 | } |
Stepan Salenikovich | d6a9ba9 | 2015-03-29 11:14:36 -0400 | [diff] [blame] | 380 | } |
Stepan Salenikovich | e7a7314 | 2015-08-11 11:55:59 -0400 | [diff] [blame] | 381 | |
Stepan Salenikovich | d6a9ba9 | 2015-03-29 11:14:36 -0400 | [diff] [blame] | 382 | g_object_set(G_OBJECT(cell), "markup", display_state, NULL); |
| 383 | g_free(display_state); |
| 384 | } |
| 385 | |
| 386 | static void |
Stepan Salenikovich | bdb0ecd | 2016-05-18 13:30:55 -0400 | [diff] [blame^] | 387 | close_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 | |
| 396 | static void |
| 397 | import_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 | |
| 417 | static void |
| 418 | export_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 | |
| 449 | static void |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 450 | account_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 Roguez | 5d1514b | 2015-10-22 15:55:31 -0400 | [diff] [blame] | 461 | account_model = gtk_q_tree_model_new(&AccountModel::instance(), 4, |
aviau | 9bbb19b | 2016-05-16 15:53:44 -0400 | [diff] [blame] | 462 | 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 Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 466 | 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 Salenikovich | a1b8cb3 | 2015-09-11 14:58:35 -0400 | [diff] [blame] | 469 | 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] | 470 | 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 Salenikovich | a1b8cb3 | 2015-09-11 14:58:35 -0400 | [diff] [blame] | 475 | 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] | 476 | 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] | 477 | 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 Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 481 | |
| 482 | renderer = gtk_cell_renderer_text_new(); |
Stepan Salenikovich | a1b8cb3 | 2015-09-11 14:58:35 -0400 | [diff] [blame] | 483 | 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] | 484 | 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] | 485 | g_object_set(G_OBJECT(renderer), "ellipsize", PANGO_ELLIPSIZE_END, NULL); |
| 486 | gtk_tree_view_column_set_expand(column, TRUE); |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 487 | |
Stepan Salenikovich | d6a9ba9 | 2015-03-29 11:14:36 -0400 | [diff] [blame] | 488 | /* 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 Salenikovich | e4981b2 | 2015-10-22 15:22:59 -0400 | [diff] [blame] | 493 | priv->treeview_account_list, |
Stepan Salenikovich | d6a9ba9 | 2015-03-29 11:14:36 -0400 | [diff] [blame] | 494 | NULL); |
| 495 | |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 496 | /* 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 Salenikovich | c3fede2 | 2015-03-20 17:01:47 -0400 | [diff] [blame] | 502 | |
| 503 | /* populate account type combo box */ |
| 504 | /* TODO: when to delete this model? */ |
Guillaume Roguez | 5d1514b | 2015-10-22 15:55:31 -0400 | [diff] [blame] | 505 | priv->active_protocols = new ActiveItemProxyModel((QAbstractItemModel *)AccountModel::instance().protocolModel()); |
Stepan Salenikovich | c3fede2 | 2015-03-20 17:01:47 -0400 | [diff] [blame] | 506 | |
| 507 | GtkQSortFilterTreeModel *protocol_model = gtk_q_sort_filter_tree_model_new( |
| 508 | (QSortFilterProxyModel *)priv->active_protocols, |
| 509 | 1, |
aviau | 271bcc2 | 2016-05-27 17:25:19 -0400 | [diff] [blame] | 510 | 0, Qt::DisplayRole, G_TYPE_STRING); |
Stepan Salenikovich | c3fede2 | 2015-03-20 17:01:47 -0400 | [diff] [blame] | 511 | |
| 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 Salenikovich | 2d63d5e | 2015-03-22 23:23:54 -0400 | [diff] [blame] | 520 | priv->protocol_selection_changed = QObject::connect( |
Guillaume Roguez | 5d1514b | 2015-10-22 15:55:31 -0400 | [diff] [blame] | 521 | AccountModel::instance().selectionModel(), |
Stepan Salenikovich | c3fede2 | 2015-03-20 17:01:47 -0400 | [diff] [blame] | 522 | &QItemSelectionModel::currentChanged, |
Stepan Salenikovich | 825bb0a | 2016-03-11 15:33:52 -0500 | [diff] [blame] | 523 | [=](const QModelIndex & current, G_GNUC_UNUSED const QModelIndex & previous) { |
Stepan Salenikovich | c3fede2 | 2015-03-20 17:01:47 -0400 | [diff] [blame] | 524 | GtkTreeSelection *selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_account_list)); |
| 525 | |
Stepan Salenikovich | c3fede2 | 2015-03-20 17:01:47 -0400 | [diff] [blame] | 526 | /* 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 Salenikovich | f903d1b | 2015-03-25 14:51:45 -0400 | [diff] [blame] | 532 | g_warning("SelectionModel of AccountModel changed to invalid QModelIndex?"); |
Stepan Salenikovich | c3fede2 | 2015-03-20 17:01:47 -0400 | [diff] [blame] | 533 | } |
| 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 Roguez | 5d1514b | 2015-10-22 15:55:31 -0400 | [diff] [blame] | 543 | QModelIndex protocol_idx = AccountModel::instance().protocolModel()->selectionModel()->currentIndex(); |
Stepan Salenikovich | c3fede2 | 2015-03-20 17:01:47 -0400 | [diff] [blame] | 544 | 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 Salenikovich | bdb0ecd | 2016-05-18 13:30:55 -0400 | [diff] [blame^] | 558 | |
| 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 Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | static void |
| 565 | account_view_class_init(AccountViewClass *klass) |
| 566 | { |
| 567 | G_OBJECT_CLASS(klass)->dispose = account_view_dispose; |
Stepan Salenikovich | 2d63d5e | 2015-03-22 23:23:54 -0400 | [diff] [blame] | 568 | G_OBJECT_CLASS(klass)->finalize = account_view_finalize; |
Stepan Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 569 | |
| 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 Salenikovich | c3fede2 | 2015-03-20 17:01:47 -0400 | [diff] [blame] | 575 | 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 Salenikovich | bdb0ecd | 2016-05-18 13:30:55 -0400 | [diff] [blame^] | 578 | 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 Salenikovich | 61cbab0 | 2015-03-16 18:35:10 -0400 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | GtkWidget * |
| 583 | account_view_new(void) |
| 584 | { |
| 585 | return (GtkWidget *)g_object_new(ACCOUNT_VIEW_TYPE, NULL); |
| 586 | } |