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