blob: 58fa68aa3f7e5ab7265643f3d2b2648869ac5a08 [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 "accountaudiotab.h"
32
33#include <gtk/gtk.h>
Stepan Salenikovicha1b8cb32015-09-11 14:58:35 -040034#include <glib/gi18n.h>
Stepan Salenikovich61cbab02015-03-16 18:35:10 -040035#include <account.h>
36#include <audio/codecmodel.h>
37#include "models/gtkqsortfiltertreemodel.h"
Stepan Salenikovich9816a942015-04-22 17:49:16 -040038#include "utils/models.h"
Stepan Salenikovich61cbab02015-03-16 18:35:10 -040039
40struct _AccountAudioTab
41{
42 GtkBox parent;
43};
44
45struct _AccountAudioTabClass
46{
47 GtkBoxClass parent_class;
48};
49
50typedef struct _AccountAudioTabPrivate AccountAudioTabPrivate;
51
52struct _AccountAudioTabPrivate
53{
54 Account *account;
55 GtkWidget *treeview_codecs;
56 GtkWidget *button_moveup;
57 GtkWidget *button_movedown;
58};
59
60G_DEFINE_TYPE_WITH_PRIVATE(AccountAudioTab, account_audio_tab, GTK_TYPE_BOX);
61
62#define ACCOUNT_AUDIO_TAB_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), ACCOUNT_AUDIO_TAB_TYPE, AccountAudioTabPrivate))
63
64static void
65account_audio_tab_dispose(GObject *object)
66{
67 G_OBJECT_CLASS(account_audio_tab_parent_class)->dispose(object);
68}
69
70static void
71account_audio_tab_init(AccountAudioTab *view)
72{
73 gtk_widget_init_template(GTK_WIDGET(view));
74}
75
76static void
77account_audio_tab_class_init(AccountAudioTabClass *klass)
78{
79 G_OBJECT_CLASS(klass)->dispose = account_audio_tab_dispose;
80
81 gtk_widget_class_set_template_from_resource(GTK_WIDGET_CLASS (klass),
82 "/cx/ring/RingGnome/accountaudiotab.ui");
83
84 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), AccountAudioTab, treeview_codecs);
85 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), AccountAudioTab, button_moveup);
86 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), AccountAudioTab, button_movedown);
87}
88
89static void
90codec_active_toggled(GtkCellRendererToggle *renderer, gchar *path, AccountAudioTab *view)
91{
92 g_return_if_fail(IS_ACCOUNT_AUDIO_TAB(view));
93 AccountAudioTabPrivate *priv = ACCOUNT_AUDIO_TAB_GET_PRIVATE(view);
94
95 /* we want to set it to the opposite of the current value */
96 gboolean toggle = !gtk_cell_renderer_toggle_get_active(renderer);
97
98 /* get iter which was clicked */
99 GtkTreePath *tree_path = gtk_tree_path_new_from_string(path);
100 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(priv->treeview_codecs));
101 GtkTreeIter iter;
102 gtk_tree_model_get_iter(model, &iter, tree_path);
103
104 /* get qmodelindex from iter and set the model data */
105 QModelIndex idx = gtk_q_sort_filter_tree_model_get_source_idx(GTK_Q_SORT_FILTER_TREE_MODEL(model), &iter);
106 if (idx.isValid()) {
107 priv->account->codecModel()->audioCodecs()->setData(idx, QVariant(toggle), Qt::CheckStateRole);
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400108 }
109}
110
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400111static void
112move_selected_codec(AccountAudioTab *view, int position_diff)
113{
114 g_return_if_fail(IS_ACCOUNT_AUDIO_TAB(view));
115 AccountAudioTabPrivate *priv = ACCOUNT_AUDIO_TAB_GET_PRIVATE(view);
116
117 GtkTreeSelection *selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_codecs));
118 QModelIndex idx = get_index_from_selection(selection);
119
120 if(!idx.isValid())
121 return;
122
123 QMimeData* mime = priv->account->codecModel()->audioCodecs()->mimeData(QModelIndexList() << idx);
124 priv->account->codecModel()->audioCodecs()->dropMimeData(
125 mime,
126 Qt::MoveAction,
127 idx.row() + position_diff,
128 0,
129 QModelIndex());
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400130
131 /* now make sure to select the same codec which was moved
132 * TODO: UGLY! this should be somehow done in the qt modle bindings,
133 * or via a selection model, not here
134 */
135 int new_row = idx.row() + position_diff;
136 int row_count = priv->account->codecModel()->audioCodecs()->rowCount(idx.parent());
137 if (new_row < 0)
138 new_row = row_count - 1;
139 else if (new_row >= row_count)
140 new_row = 0;
141
142 idx = idx.sibling(new_row, idx.column());
143 GtkTreeIter iter;
144 if (gtk_q_sort_filter_tree_model_source_index_to_iter(
145 GTK_Q_SORT_FILTER_TREE_MODEL(gtk_tree_view_get_model(GTK_TREE_VIEW(priv->treeview_codecs))),
146 idx,
147 &iter)) {
148 gtk_tree_selection_select_iter(selection, &iter);
149 }
150}
151
152static void
153move_codec_up(G_GNUC_UNUSED GtkButton *button, AccountAudioTab *view)
154{
155 move_selected_codec(view, -1);
156}
157
158static void
159move_codec_down(G_GNUC_UNUSED GtkButton *button, AccountAudioTab *view)
160{
161 move_selected_codec(view, +1);
162}
163
164static void
165build_tab_view(AccountAudioTab *view)
166{
167 g_return_if_fail(IS_ACCOUNT_AUDIO_TAB(view));
168 AccountAudioTabPrivate *priv = ACCOUNT_AUDIO_TAB_GET_PRIVATE(view);
169
170 /* codec model */
171 GtkQSortFilterTreeModel *codec_model;
172 GtkCellRenderer *renderer;
173 GtkTreeViewColumn *column;
174
175 codec_model = gtk_q_sort_filter_tree_model_new(
176 priv->account->codecModel()->audioCodecs(),
177 4,
178 Qt::CheckStateRole, G_TYPE_BOOLEAN,
179 CodecModel::Role::NAME, G_TYPE_STRING,
180 CodecModel::Role::BITRATE, G_TYPE_STRING,
181 CodecModel::Role::SAMPLERATE, G_TYPE_STRING);
182 gtk_tree_view_set_model(GTK_TREE_VIEW(priv->treeview_codecs), GTK_TREE_MODEL(codec_model));
183
184 renderer = gtk_cell_renderer_toggle_new();
Stepan Salenikovicha1b8cb32015-09-11 14:58:35 -0400185 column = gtk_tree_view_column_new_with_attributes(_("Enabled"), renderer, "active", 0, NULL);
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400186 gtk_tree_view_append_column(GTK_TREE_VIEW(priv->treeview_codecs), column);
187
188 g_signal_connect(renderer, "toggled", G_CALLBACK(codec_active_toggled), view);
189
190 renderer = gtk_cell_renderer_text_new();
Stepan Salenikovicha1b8cb32015-09-11 14:58:35 -0400191 column = gtk_tree_view_column_new_with_attributes(C_("Name of the codec", "Name"), renderer, "text", 1, NULL);
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400192 gtk_tree_view_append_column(GTK_TREE_VIEW(priv->treeview_codecs), column);
193
194 renderer = gtk_cell_renderer_text_new();
Stepan Salenikovicha1b8cb32015-09-11 14:58:35 -0400195 column = gtk_tree_view_column_new_with_attributes(_("Bitrate"), renderer, "text", 2, NULL);
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400196 gtk_tree_view_append_column(GTK_TREE_VIEW(priv->treeview_codecs), column);
197
198 renderer = gtk_cell_renderer_text_new();
Stepan Salenikovicha1b8cb32015-09-11 14:58:35 -0400199 column = gtk_tree_view_column_new_with_attributes(_("Samplerate"), renderer, "text", 3, NULL);
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400200 gtk_tree_view_append_column(GTK_TREE_VIEW(priv->treeview_codecs), column);
201
202 /* connect move codecs up/down signals */
203 g_signal_connect(priv->button_moveup, "clicked", G_CALLBACK(move_codec_up), view);
204 g_signal_connect(priv->button_movedown, "clicked", G_CALLBACK(move_codec_down), view);
205}
206
207GtkWidget *
208account_audio_tab_new(Account *account)
209{
210 g_return_val_if_fail(account != NULL, NULL);
211
212 gpointer view = g_object_new(ACCOUNT_AUDIO_TAB_TYPE, NULL);
213
214 AccountAudioTabPrivate *priv = ACCOUNT_AUDIO_TAB_GET_PRIVATE(view);
215 priv->account = account;
216
217 build_tab_view(ACCOUNT_AUDIO_TAB(view));
218
219 return (GtkWidget *)view;
220}