blob: d150b4bf5836151e3052b29624f670ddbaa3e7f8 [file] [log] [blame]
Stepan Salenikovich61cbab02015-03-16 18:35:10 -04001/*
2 * Copyright (C) 2015 Savoir-Faire Linux Inc.
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.
18 *
19 * Additional permission under GNU GPL version 3 section 7:
20 *
21 * If you modify this program, or any covered work, by linking or
22 * combining it with the OpenSSL project's OpenSSL library (or a
23 * modified version of that library), containing parts covered by the
24 * terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
25 * grants you additional permission to convey the resulting work.
26 * Corresponding Source for a non-source form of such a combination
27 * shall include the source code for the parts of OpenSSL used as well
28 * as that of the covered work.
29 */
30
31#include "accountview.h"
32
33#include <gtk/gtk.h>
34#include <accountmodel.h>
35#include <audio/codecmodel.h>
Stepan Salenikovichc3fede22015-03-20 17:01:47 -040036#include <protocolmodel.h>
37#include <QtCore/QItemSelectionModel>
Stepan Salenikovich61cbab02015-03-16 18:35:10 -040038#include "models/gtkqtreemodel.h"
Stepan Salenikovichc3fede22015-03-20 17:01:47 -040039#include "models/gtkqsortfiltertreemodel.h"
40#include "models/activeitemproxymodel.h"
Stepan Salenikovich61cbab02015-03-16 18:35:10 -040041#include "accountgeneraltab.h"
42#include "accountaudiotab.h"
43#include "accountvideotab.h"
44
45struct _AccountView
46{
47 GtkBox parent;
48};
49
50struct _AccountViewClass
51{
52 GtkBoxClass parent_class;
53};
54
55typedef struct _AccountViewPrivate AccountViewPrivate;
56
57struct _AccountViewPrivate
58{
59 GtkWidget *treeview_account_list;
60 GtkWidget *stack_account;
61 GtkWidget *current_account_notebook;
Stepan Salenikovichc3fede22015-03-20 17:01:47 -040062 GtkWidget *button_remove_account;
63 GtkWidget *button_add_account;
64 GtkWidget *combobox_account_type;
Stepan Salenikovich61cbab02015-03-16 18:35:10 -040065
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -040066 gint current_page; /* keeps track of current notebook page displayed */
Stepan Salenikovichc3fede22015-03-20 17:01:47 -040067
68 ActiveItemProxyModel *active_protocols;
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -040069 QMetaObject::Connection protocol_selection_changed;
Stepan Salenikovich61cbab02015-03-16 18:35:10 -040070};
71
72G_DEFINE_TYPE_WITH_PRIVATE(AccountView, account_view, GTK_TYPE_BOX);
73
74#define ACCOUNT_VIEW_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), ACCOUNT_VIEW_TYPE, AccountViewPrivate))
75
76static void
77account_view_dispose(GObject *object)
78{
79 AccountView *view = ACCOUNT_VIEW(object);
80 AccountViewPrivate *priv = ACCOUNT_VIEW_GET_PRIVATE(view);
81
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -040082 QObject::disconnect(priv->protocol_selection_changed);
Stepan Salenikovich61cbab02015-03-16 18:35:10 -040083
84 G_OBJECT_CLASS(account_view_parent_class)->dispose(object);
85}
86
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -040087static void
88account_view_finalize(GObject *object)
89{
90 AccountView *view = ACCOUNT_VIEW(object);
91 AccountViewPrivate *priv = ACCOUNT_VIEW_GET_PRIVATE(view);
92
93 delete priv->active_protocols;
94
95 G_OBJECT_CLASS(account_view_parent_class)->finalize(object);
96}
97
Stepan Salenikovich61cbab02015-03-16 18:35:10 -040098static QModelIndex
99get_index_from_selection(GtkTreeSelection *selection)
100{
101 GtkTreeIter iter;
102 GtkTreeModel *model = NULL;
103
104 if (gtk_tree_selection_get_selected(selection, &model, &iter)) {
105 return gtk_q_tree_model_get_source_idx(GTK_Q_TREE_MODEL(model), &iter);
106 } else {
107 return QModelIndex();
108 }
109}
110
111static void
Stepan Salenikovichc3fede22015-03-20 17:01:47 -0400112update_account_model_selection(GtkTreeSelection *selection, G_GNUC_UNUSED gpointer user_data)
113{
114 QModelIndex current = get_index_from_selection(selection);
115 if (current.isValid())
116 AccountModel::instance()->selectionModel()->setCurrentIndex(current, QItemSelectionModel::ClearAndSelect);
117 else
118 AccountModel::instance()->selectionModel()->clearCurrentIndex();
119}
120
121static void
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400122account_selection_changed(GtkTreeSelection *selection, AccountView *view)
123{
124 g_return_if_fail(IS_ACCOUNT_VIEW(view));
125 AccountViewPrivate *priv = ACCOUNT_VIEW_GET_PRIVATE(view);
126
127 GtkWidget *old_account_view = gtk_stack_get_visible_child(GTK_STACK(priv->stack_account));
128
129 /* keep track of the last tab displayed */
130 if (priv->current_account_notebook)
131 priv->current_page = gtk_notebook_get_current_page(GTK_NOTEBOOK(priv->current_account_notebook));
132
133 if (priv->current_page < 0)
134 priv->current_page = 0;
135
136 QModelIndex account_idx = get_index_from_selection(selection);
137 if (!account_idx.isValid()) {
138 /* it nothing is slected, simply display something empty */
139 GtkWidget *empty_box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
140 gtk_widget_show(empty_box);
141 gtk_stack_add_named(GTK_STACK(priv->stack_account), empty_box, "placeholder");
142 gtk_stack_set_visible_child(GTK_STACK(priv->stack_account), empty_box);
143 priv->current_account_notebook = NULL;
144 } else {
145 Account *account = AccountModel::instance()->getAccountByModelIndex(account_idx);
146
147 /* build new account view */
148 GtkWidget *hbox_account = gtk_box_new(GTK_ORIENTATION_VERTICAL, 10);
149
150 /* create account notebook */
151 priv->current_account_notebook = gtk_notebook_new();
152 gtk_box_pack_start(GTK_BOX(hbox_account), priv->current_account_notebook, TRUE, TRUE, 0);
153
154 /* customize account view based on account */
155 GtkWidget *general_tab = account_general_tab_new(account);
156 gtk_notebook_append_page(GTK_NOTEBOOK(priv->current_account_notebook),
157 general_tab,
158 gtk_label_new("General"));
159 GtkWidget *audio_tab = account_audio_tab_new(account);
160 gtk_notebook_append_page(GTK_NOTEBOOK(priv->current_account_notebook),
161 audio_tab,
162 gtk_label_new("Audio"));
163 GtkWidget *video_tab = account_video_tab_new(account);
164 gtk_notebook_append_page(GTK_NOTEBOOK(priv->current_account_notebook),
165 video_tab,
166 gtk_label_new("Video"));
167
168 /* set the tab displayed to the same as the prev account selected */
169 gtk_notebook_set_current_page(GTK_NOTEBOOK(priv->current_account_notebook), priv->current_page);
170
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400171 gtk_widget_show_all(hbox_account);
172
173 /* set the new account view as visible */
174 char *account_view_name = g_strdup_printf("%p_account", account);
175 gtk_stack_add_named(GTK_STACK(priv->stack_account), hbox_account, account_view_name);
176 gtk_stack_set_visible_child(GTK_STACK(priv->stack_account), hbox_account);
177 g_free(account_view_name);
178 }
179
180 /* remove the old account view */
181 if (old_account_view)
182 gtk_container_remove(GTK_CONTAINER(priv->stack_account), old_account_view);
183}
184
185static void
186account_active_toggled(GtkCellRendererToggle *renderer, gchar *path, AccountView *view)
187{
188 g_return_if_fail(IS_ACCOUNT_VIEW(view));
189 AccountViewPrivate *priv = ACCOUNT_VIEW_GET_PRIVATE(view);
190
191 /* we want to set it to the opposite of the current value */
192 gboolean toggle = !gtk_cell_renderer_toggle_get_active(renderer);
193
194 /* get iter which was clicked */
195 GtkTreePath *tree_path = gtk_tree_path_new_from_string(path);
196 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(priv->treeview_account_list));
197 GtkTreeIter iter;
198 gtk_tree_model_get_iter(model, &iter, tree_path);
199
200 /* get qmodelindex from iter and set the model data */
201 QModelIndex idx = gtk_q_tree_model_get_source_idx(GTK_Q_TREE_MODEL(model), &iter);
202 if (idx.isValid()) {
203 /* check if it is the IP2IP account, as we don't want to be able to disable it */
204 QVariant alias = idx.data(static_cast<int>(Account::Role::Alias));
205 if (strcmp(alias.value<QString>().toLocal8Bit().constData(), "IP2IP") != 0)
206 AccountModel::instance()->setData(idx, QVariant(toggle), Qt::CheckStateRole);
207 }
208}
209
Stepan Salenikovichc3fede22015-03-20 17:01:47 -0400210static gboolean
211remove_account_dialog(Account *account)
212{
213 gboolean response = FALSE;
214 GtkWidget *dialog = gtk_message_dialog_new(NULL,
215 (GtkDialogFlags)(GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT),
216 GTK_MESSAGE_QUESTION, GTK_BUTTONS_OK_CANCEL,
217 "Are you sure you want to delete account \"%s\"?",
218 account->alias().toLocal8Bit().constData());
219
Stepan Salenikovichc3fede22015-03-20 17:01:47 -0400220 switch (gtk_dialog_run(GTK_DIALOG(dialog))) {
221 case GTK_RESPONSE_OK:
222 response = TRUE;
223 break;
224 default:
225 response = FALSE;
226 break;
227 }
228
229 gtk_widget_destroy(dialog);
230
231 return response;
232}
233
234static void
235remove_account(G_GNUC_UNUSED GtkWidget *entry, AccountView *view)
236{
237 g_return_if_fail(IS_ACCOUNT_VIEW(view));
238 AccountViewPrivate *priv = ACCOUNT_VIEW_GET_PRIVATE(view);
239
240 GtkTreeSelection *selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_account_list));
241 QModelIndex idx = get_index_from_selection(selection);
242
243 if (idx.isValid()) {
244 /* this is a destructive operation, ask the user if they are sure */
245 Account *account = AccountModel::instance()->getAccountByModelIndex(idx);
246 if (remove_account_dialog(account)) {
247 AccountModel::instance()->remove(idx);
Stepan Salenikovichc3fede22015-03-20 17:01:47 -0400248 }
249 }
250}
251
252static void
253add_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 GtkTreeIter protocol_iter;
259 if (gtk_combo_box_get_active_iter(GTK_COMBO_BOX(priv->combobox_account_type), &protocol_iter)) {
260 /* get the qmodelindex of the protocol */
261 GtkTreeModel *protocol_model = gtk_combo_box_get_model(GTK_COMBO_BOX(priv->combobox_account_type));
262 QModelIndex protocol_idx = gtk_q_sort_filter_tree_model_get_source_idx(
263 GTK_Q_SORT_FILTER_TREE_MODEL(protocol_model),
264 &protocol_iter);
265 if (protocol_idx.isValid()) {
266 protocol_idx = priv->active_protocols->mapToSource(protocol_idx);
267 AccountModel::instance()->add(QString("New Account"), protocol_idx);
Stepan Salenikovichc3fede22015-03-20 17:01:47 -0400268 }
269 }
270}
271
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400272static void
273account_view_init(AccountView *view)
274{
275 gtk_widget_init_template(GTK_WIDGET(view));
276
277 AccountViewPrivate *priv = ACCOUNT_VIEW_GET_PRIVATE(view);
278
279 /* account model */
280 GtkQTreeModel *account_model;
281 GtkCellRenderer *renderer;
282 GtkTreeViewColumn *column;
283
284 account_model = gtk_q_tree_model_new(AccountModel::instance(), 4,
285 Account::Role::Enabled, G_TYPE_BOOLEAN,
286 Account::Role::Alias, G_TYPE_STRING,
287 Account::Role::Proto, G_TYPE_INT,
288 Account::Role::RegistrationState, G_TYPE_STRING);
289 gtk_tree_view_set_model(GTK_TREE_VIEW(priv->treeview_account_list), GTK_TREE_MODEL(account_model));
290
291 renderer = gtk_cell_renderer_toggle_new();
292 column = gtk_tree_view_column_new_with_attributes("Enabled", renderer, "active", 0, NULL);
293 gtk_tree_view_append_column(GTK_TREE_VIEW(priv->treeview_account_list), column);
294
295 g_signal_connect(renderer, "toggled", G_CALLBACK(account_active_toggled), view);
296
297 renderer = gtk_cell_renderer_text_new();
298 column = gtk_tree_view_column_new_with_attributes("Alias", renderer, "text", 1, NULL);
299 gtk_tree_view_append_column(GTK_TREE_VIEW(priv->treeview_account_list), column);
300
301 renderer = gtk_cell_renderer_text_new();
302 column = gtk_tree_view_column_new_with_attributes("Status", renderer, "text", 3, NULL);
303 gtk_tree_view_append_column(GTK_TREE_VIEW(priv->treeview_account_list), column);
304
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400305 /* add an empty box to the account stack initially, otherwise there will
306 * be no cool animation when the first account is selected */
307 GtkWidget *empty_box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
308 gtk_widget_show(empty_box);
309 gtk_stack_add_named(GTK_STACK(priv->stack_account), empty_box, "placeholder");
310 gtk_stack_set_visible_child(GTK_STACK(priv->stack_account), empty_box);
Stepan Salenikovichc3fede22015-03-20 17:01:47 -0400311
312 /* populate account type combo box */
313 /* TODO: when to delete this model? */
314 priv->active_protocols = new ActiveItemProxyModel((QAbstractItemModel *)AccountModel::instance()->protocolModel());
315
316 GtkQSortFilterTreeModel *protocol_model = gtk_q_sort_filter_tree_model_new(
317 (QSortFilterProxyModel *)priv->active_protocols,
318 1,
319 Qt::DisplayRole, G_TYPE_STRING);
320
321 gtk_combo_box_set_model(GTK_COMBO_BOX(priv->combobox_account_type), GTK_TREE_MODEL(protocol_model));
322
323 renderer = gtk_cell_renderer_text_new();
324 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(priv->combobox_account_type), renderer, FALSE);
325 gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(priv->combobox_account_type), renderer,
326 "text", 0, NULL);
327
328 /* connect signals to and from the selection model of the account model */
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400329 priv->protocol_selection_changed = QObject::connect(
Stepan Salenikovichc3fede22015-03-20 17:01:47 -0400330 AccountModel::instance()->selectionModel(),
331 &QItemSelectionModel::currentChanged,
332 [=](const QModelIndex & current, const QModelIndex & previous) {
333 GtkTreeSelection *selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_account_list));
334
335 /* first unselect the previous */
336 if (previous.isValid()) {
337 GtkTreeIter old_iter;
338 if (gtk_q_tree_model_source_index_to_iter(account_model, previous, &old_iter)) {
339 gtk_tree_selection_unselect_iter(selection, &old_iter);
340 } else {
341 g_warning("Trying to unselect invalid GtkTreeIter");
342 }
343 }
344
345 /* select the current */
346 if (current.isValid()) {
347 GtkTreeIter new_iter;
348 if (gtk_q_tree_model_source_index_to_iter(account_model, current, &new_iter)) {
349 gtk_tree_selection_select_iter(selection, &new_iter);
350 } else {
351 g_warning("SelectionModel of CallModel changed to invalid QModelIndex?");
352 }
353 }
354 }
355 );
356
357 GtkTreeSelection *account_selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_account_list));
358 g_signal_connect(account_selection, "changed", G_CALLBACK(update_account_model_selection), NULL);
359 g_signal_connect(account_selection, "changed", G_CALLBACK(account_selection_changed), view);
360
361 /* select the default protocol */
362 QModelIndex protocol_idx = AccountModel::instance()->protocolModel()->selectionModel()->currentIndex();
363 if (protocol_idx.isValid()) {
364 protocol_idx = priv->active_protocols->mapFromSource(protocol_idx);
365 GtkTreeIter protocol_iter;
366 if (gtk_q_sort_filter_tree_model_source_index_to_iter(
367 (GtkQSortFilterTreeModel *)protocol_model,
368 protocol_idx,
369 &protocol_iter)) {
370 gtk_combo_box_set_active_iter(GTK_COMBO_BOX(priv->combobox_account_type), &protocol_iter);
371 }
372 }
373
374 /* connect signals to add/remove accounts */
375 g_signal_connect(priv->button_remove_account, "clicked", G_CALLBACK(remove_account), view);
376 g_signal_connect(priv->button_add_account, "clicked", G_CALLBACK(add_account), view);
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400377}
378
379static void
380account_view_class_init(AccountViewClass *klass)
381{
382 G_OBJECT_CLASS(klass)->dispose = account_view_dispose;
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400383 G_OBJECT_CLASS(klass)->finalize = account_view_finalize;
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400384
385 gtk_widget_class_set_template_from_resource(GTK_WIDGET_CLASS (klass),
386 "/cx/ring/RingGnome/accountview.ui");
387
388 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), AccountView, treeview_account_list);
389 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), AccountView, stack_account);
Stepan Salenikovichc3fede22015-03-20 17:01:47 -0400390 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), AccountView, button_remove_account);
391 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), AccountView, button_add_account);
392 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), AccountView, combobox_account_type);
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400393}
394
395GtkWidget *
396account_view_new(void)
397{
398 return (GtkWidget *)g_object_new(ACCOUNT_VIEW_TYPE, NULL);
399}