blob: 5ea52c1e9bab20b0e4e857f6ea69a0c3c8e6fab1 [file] [log] [blame]
Stepan Salenikovich61cbab02015-03-16 18:35:10 -04001/*
Guillaume Roguez2a6150d2017-07-19 18:24:47 -04002 * Copyright (C) 2015-2017 Savoir-faire Linux Inc.
Stepan Salenikovich61cbab02015-03-16 18:35:10 -04003 * 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 Salenikovich61cbab02015-03-16 18:35:10 -040018 */
19
20#include "accountvideotab.h"
21
22#include <gtk/gtk.h>
Stepan Salenikovicha1b8cb32015-09-11 14:58:35 -040023#include <glib/gi18n.h>
Stepan Salenikovich61cbab02015-03-16 18:35:10 -040024#include <account.h>
Edric Milaret58e71072016-03-21 12:16:37 -040025#include <codecmodel.h>
Stepan Salenikovichf6078222016-10-03 17:31:16 -040026#include "models/gtkqtreemodel.h"
27#include <QtCore/QSortFilterProxyModel>
Stepan Salenikovich9816a942015-04-22 17:49:16 -040028#include "utils/models.h"
Stepan Salenikovich61cbab02015-03-16 18:35:10 -040029
30struct _AccountVideoTab
31{
32 GtkBox parent;
33};
34
35struct _AccountVideoTabClass
36{
37 GtkBoxClass parent_class;
38};
39
40typedef struct _AccountVideoTabPrivate AccountVideoTabPrivate;
41
42struct _AccountVideoTabPrivate
43{
44 Account *account;
45 GtkWidget *treeview_codecs;
46 GtkWidget *checkbutton_enable;
47 GtkWidget *button_moveup;
48 GtkWidget *button_movedown;
49};
50
51G_DEFINE_TYPE_WITH_PRIVATE(AccountVideoTab, account_video_tab, GTK_TYPE_BOX);
52
53#define ACCOUNT_VIDEO_TAB_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), ACCOUNT_VIDEO_TAB_TYPE, AccountVideoTabPrivate))
54
55static void
56account_video_tab_dispose(GObject *object)
57{
58 G_OBJECT_CLASS(account_video_tab_parent_class)->dispose(object);
59}
60
61static void
62account_video_tab_init(AccountVideoTab *view)
63{
64 gtk_widget_init_template(GTK_WIDGET(view));
65}
66
67static void
68account_video_tab_class_init(AccountVideoTabClass *klass)
69{
70 G_OBJECT_CLASS(klass)->dispose = account_video_tab_dispose;
71
72 gtk_widget_class_set_template_from_resource(GTK_WIDGET_CLASS (klass),
73 "/cx/ring/RingGnome/accountvideotab.ui");
74
75 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), AccountVideoTab, treeview_codecs);
76 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), AccountVideoTab, checkbutton_enable);
77 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), AccountVideoTab, button_moveup);
78 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), AccountVideoTab, button_movedown);
79}
80
81static void
82video_enable(GtkToggleButton *checkbutton, AccountVideoTab *view)
83{
84 g_return_if_fail(IS_ACCOUNT_VIDEO_TAB(view));
85 AccountVideoTabPrivate *priv = ACCOUNT_VIDEO_TAB_GET_PRIVATE(view);
86 priv->account->setVideoEnabled(gtk_toggle_button_get_active(checkbutton));
87}
88
89static void
90codec_active_toggled(GtkCellRendererToggle *renderer, gchar *path, AccountVideoTab *view)
91{
92 g_return_if_fail(IS_ACCOUNT_VIDEO_TAB(view));
93 AccountVideoTabPrivate *priv = ACCOUNT_VIDEO_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 */
Stepan Salenikovichf6078222016-10-03 17:31:16 -0400105 QModelIndex idx = gtk_q_tree_model_get_source_idx(GTK_Q_TREE_MODEL(model), &iter);
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400106 if (idx.isValid()) {
107 priv->account->codecModel()->videoCodecs()->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(AccountVideoTab *view, int position_diff)
113{
114 g_return_if_fail(IS_ACCOUNT_VIDEO_TAB(view));
115 AccountVideoTabPrivate *priv = ACCOUNT_VIDEO_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()->videoCodecs()->mimeData(QModelIndexList() << idx);
124 priv->account->codecModel()->videoCodecs()->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()->videoCodecs()->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;
Stepan Salenikovichf6078222016-10-03 17:31:16 -0400144 if (gtk_q_tree_model_source_index_to_iter(
145 GTK_Q_TREE_MODEL(gtk_tree_view_get_model(GTK_TREE_VIEW(priv->treeview_codecs))),
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400146 idx,
147 &iter)) {
148 gtk_tree_selection_select_iter(selection, &iter);
149 }
150}
151
152static void
153move_codec_up(G_GNUC_UNUSED GtkButton *button, AccountVideoTab *view)
154{
155 move_selected_codec(view, -1);
156}
157
158static void
159move_codec_down(G_GNUC_UNUSED GtkButton *button, AccountVideoTab *view)
160{
161 move_selected_codec(view, +1);
162}
163
164static void
165build_tab_view(AccountVideoTab *view)
166{
167 g_return_if_fail(IS_ACCOUNT_VIDEO_TAB(view));
168 AccountVideoTabPrivate *priv = ACCOUNT_VIDEO_TAB_GET_PRIVATE(view);
169
170 /* codec model */
Stepan Salenikovichf6078222016-10-03 17:31:16 -0400171 GtkQTreeModel *codec_model;
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400172 GtkCellRenderer *renderer;
173 GtkTreeViewColumn *column;
174
Stepan Salenikovichf6078222016-10-03 17:31:16 -0400175 codec_model = gtk_q_tree_model_new(
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400176 priv->account->codecModel()->videoCodecs(),
177 3,
aviau271bcc22016-05-27 17:25:19 -0400178 0, Qt::CheckStateRole, G_TYPE_BOOLEAN,
179 0, CodecModel::Role::NAME, G_TYPE_STRING,
180 0, CodecModel::Role::BITRATE, G_TYPE_STRING);
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400181 gtk_tree_view_set_model(GTK_TREE_VIEW(priv->treeview_codecs), GTK_TREE_MODEL(codec_model));
182
183 renderer = gtk_cell_renderer_toggle_new();
Stepan Salenikovicha1b8cb32015-09-11 14:58:35 -0400184 column = gtk_tree_view_column_new_with_attributes(_("Enabled"), renderer, "active", 0, NULL);
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400185 gtk_tree_view_append_column(GTK_TREE_VIEW(priv->treeview_codecs), column);
186
187 g_signal_connect(renderer, "toggled", G_CALLBACK(codec_active_toggled), view);
188
189 renderer = gtk_cell_renderer_text_new();
Stepan Salenikovicha1b8cb32015-09-11 14:58:35 -0400190 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 -0400191 gtk_tree_view_append_column(GTK_TREE_VIEW(priv->treeview_codecs), column);
192
193 renderer = gtk_cell_renderer_text_new();
Stepan Salenikovicha1b8cb32015-09-11 14:58:35 -0400194 column = gtk_tree_view_column_new_with_attributes(_("Bitrate"), renderer, "text", 2, NULL);
Stepan Salenikovich61cbab02015-03-16 18:35:10 -0400195 gtk_tree_view_append_column(GTK_TREE_VIEW(priv->treeview_codecs), column);
196
197 /* enable video checkbutton */
198 gtk_toggle_button_set_active(
199 GTK_TOGGLE_BUTTON(priv->checkbutton_enable), priv->account->isVideoEnabled());
200 g_signal_connect(priv->checkbutton_enable, "toggled", G_CALLBACK(video_enable), view);
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_video_tab_new(Account *account)
209{
210 g_return_val_if_fail(account != NULL, NULL);
211
212 gpointer view = g_object_new(ACCOUNT_VIDEO_TAB_TYPE, NULL);
213
214 AccountVideoTabPrivate *priv = ACCOUNT_VIDEO_TAB_GET_PRIVATE(view);
215 priv->account = account;
216
217 build_tab_view(ACCOUNT_VIDEO_TAB(view));
218
219 return (GtkWidget *)view;
220}