blob: e9b4466bf43c7ddad8358c9f1d24b115ed91096e [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 "accountvideotab.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 _AccountVideoTab
41{
42 GtkBox parent;
43};
44
45struct _AccountVideoTabClass
46{
47 GtkBoxClass parent_class;
48};
49
50typedef struct _AccountVideoTabPrivate AccountVideoTabPrivate;
51
52struct _AccountVideoTabPrivate
53{
54 Account *account;
55 GtkWidget *treeview_codecs;
56 GtkWidget *checkbutton_enable;
57 GtkWidget *button_moveup;
58 GtkWidget *button_movedown;
59};
60
61G_DEFINE_TYPE_WITH_PRIVATE(AccountVideoTab, account_video_tab, GTK_TYPE_BOX);
62
63#define ACCOUNT_VIDEO_TAB_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), ACCOUNT_VIDEO_TAB_TYPE, AccountVideoTabPrivate))
64
65static void
66account_video_tab_dispose(GObject *object)
67{
68 G_OBJECT_CLASS(account_video_tab_parent_class)->dispose(object);
69}
70
71static void
72account_video_tab_init(AccountVideoTab *view)
73{
74 gtk_widget_init_template(GTK_WIDGET(view));
75}
76
77static void
78account_video_tab_class_init(AccountVideoTabClass *klass)
79{
80 G_OBJECT_CLASS(klass)->dispose = account_video_tab_dispose;
81
82 gtk_widget_class_set_template_from_resource(GTK_WIDGET_CLASS (klass),
83 "/cx/ring/RingGnome/accountvideotab.ui");
84
85 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), AccountVideoTab, treeview_codecs);
86 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), AccountVideoTab, checkbutton_enable);
87 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), AccountVideoTab, button_moveup);
88 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), AccountVideoTab, button_movedown);
89}
90
91static void
92video_enable(GtkToggleButton *checkbutton, AccountVideoTab *view)
93{
94 g_return_if_fail(IS_ACCOUNT_VIDEO_TAB(view));
95 AccountVideoTabPrivate *priv = ACCOUNT_VIDEO_TAB_GET_PRIVATE(view);
96 priv->account->setVideoEnabled(gtk_toggle_button_get_active(checkbutton));
97}
98
99static void
100codec_active_toggled(GtkCellRendererToggle *renderer, gchar *path, AccountVideoTab *view)
101{
102 g_return_if_fail(IS_ACCOUNT_VIDEO_TAB(view));
103 AccountVideoTabPrivate *priv = ACCOUNT_VIDEO_TAB_GET_PRIVATE(view);
104
105 /* we want to set it to the opposite of the current value */
106 gboolean toggle = !gtk_cell_renderer_toggle_get_active(renderer);
107
108 /* get iter which was clicked */
109 GtkTreePath *tree_path = gtk_tree_path_new_from_string(path);
110 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(priv->treeview_codecs));
111 GtkTreeIter iter;
112 gtk_tree_model_get_iter(model, &iter, tree_path);
113
114 /* get qmodelindex from iter and set the model data */
115 QModelIndex idx = gtk_q_sort_filter_tree_model_get_source_idx(GTK_Q_SORT_FILTER_TREE_MODEL(model), &iter);
116 if (idx.isValid()) {
117 priv->account->codecModel()->videoCodecs()->setData(idx, QVariant(toggle), Qt::CheckStateRole);
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400118 }
119}
120
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400121static void
122move_selected_codec(AccountVideoTab *view, int position_diff)
123{
124 g_return_if_fail(IS_ACCOUNT_VIDEO_TAB(view));
125 AccountVideoTabPrivate *priv = ACCOUNT_VIDEO_TAB_GET_PRIVATE(view);
126
127 GtkTreeSelection *selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_codecs));
128 QModelIndex idx = get_index_from_selection(selection);
129
130 if(!idx.isValid())
131 return;
132
133 QMimeData* mime = priv->account->codecModel()->videoCodecs()->mimeData(QModelIndexList() << idx);
134 priv->account->codecModel()->videoCodecs()->dropMimeData(
135 mime,
136 Qt::MoveAction,
137 idx.row() + position_diff,
138 0,
139 QModelIndex());
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400140
141 /* now make sure to select the same codec which was moved
142 * TODO: UGLY! this should be somehow done in the qt modle bindings,
143 * or via a selection model, not here
144 */
145 int new_row = idx.row() + position_diff;
146 int row_count = priv->account->codecModel()->videoCodecs()->rowCount(idx.parent());
147 if (new_row < 0)
148 new_row = row_count - 1;
149 else if (new_row >= row_count)
150 new_row = 0;
151
152 idx = idx.sibling(new_row, idx.column());
153 GtkTreeIter iter;
154 if (gtk_q_sort_filter_tree_model_source_index_to_iter(
155 GTK_Q_SORT_FILTER_TREE_MODEL(gtk_tree_view_get_model(GTK_TREE_VIEW(priv->treeview_codecs))),
156 idx,
157 &iter)) {
158 gtk_tree_selection_select_iter(selection, &iter);
159 }
160}
161
162static void
163move_codec_up(G_GNUC_UNUSED GtkButton *button, AccountVideoTab *view)
164{
165 move_selected_codec(view, -1);
166}
167
168static void
169move_codec_down(G_GNUC_UNUSED GtkButton *button, AccountVideoTab *view)
170{
171 move_selected_codec(view, +1);
172}
173
174static void
175build_tab_view(AccountVideoTab *view)
176{
177 g_return_if_fail(IS_ACCOUNT_VIDEO_TAB(view));
178 AccountVideoTabPrivate *priv = ACCOUNT_VIDEO_TAB_GET_PRIVATE(view);
179
180 /* codec model */
181 GtkQSortFilterTreeModel *codec_model;
182 GtkCellRenderer *renderer;
183 GtkTreeViewColumn *column;
184
185 codec_model = gtk_q_sort_filter_tree_model_new(
186 priv->account->codecModel()->videoCodecs(),
187 3,
188 Qt::CheckStateRole, G_TYPE_BOOLEAN,
189 CodecModel::Role::NAME, G_TYPE_STRING,
190 CodecModel::Role::BITRATE, G_TYPE_STRING);
191 gtk_tree_view_set_model(GTK_TREE_VIEW(priv->treeview_codecs), GTK_TREE_MODEL(codec_model));
192
193 renderer = gtk_cell_renderer_toggle_new();
Stepan Salenikovicha1b8cb32015-09-11 14:58:35 -0400194 column = gtk_tree_view_column_new_with_attributes(_("Enabled"), renderer, "active", 0, NULL);
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400195 gtk_tree_view_append_column(GTK_TREE_VIEW(priv->treeview_codecs), column);
196
197 g_signal_connect(renderer, "toggled", G_CALLBACK(codec_active_toggled), view);
198
199 renderer = gtk_cell_renderer_text_new();
Stepan Salenikovicha1b8cb32015-09-11 14:58:35 -0400200 column = gtk_tree_view_column_new_with_attributes(C_("The name of the codec", "Name"), renderer, "text", 1, NULL);
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400201 gtk_tree_view_append_column(GTK_TREE_VIEW(priv->treeview_codecs), column);
202
203 renderer = gtk_cell_renderer_text_new();
Stepan Salenikovicha1b8cb32015-09-11 14:58:35 -0400204 column = gtk_tree_view_column_new_with_attributes(_("Bitrate"), renderer, "text", 2, NULL);
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400205 gtk_tree_view_append_column(GTK_TREE_VIEW(priv->treeview_codecs), column);
206
207 /* enable video checkbutton */
208 gtk_toggle_button_set_active(
209 GTK_TOGGLE_BUTTON(priv->checkbutton_enable), priv->account->isVideoEnabled());
210 g_signal_connect(priv->checkbutton_enable, "toggled", G_CALLBACK(video_enable), view);
211
212 /* connect move codecs up/down signals */
213 g_signal_connect(priv->button_moveup, "clicked", G_CALLBACK(move_codec_up), view);
214 g_signal_connect(priv->button_movedown, "clicked", G_CALLBACK(move_codec_down), view);
215}
216
217GtkWidget *
218account_video_tab_new(Account *account)
219{
220 g_return_val_if_fail(account != NULL, NULL);
221
222 gpointer view = g_object_new(ACCOUNT_VIDEO_TAB_TYPE, NULL);
223
224 AccountVideoTabPrivate *priv = ACCOUNT_VIDEO_TAB_GET_PRIVATE(view);
225 priv->account = account;
226
227 build_tab_view(ACCOUNT_VIDEO_TAB(view));
228
229 return (GtkWidget *)view;
230}