blob: d289ce3d27aee9094970a71d8607a11d3b717c9d [file] [log] [blame]
Nicolas Jager005fc552017-04-26 07:50:26 -04001/*
2 * Copyright (C) 2017 Savoir-faire Linux Inc.
3 * Author: Nicolas Jäger <nicolas.jager@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
20#include "bannedcontactsview.h"
21
22// GTK
23#include <gtk/gtk.h>
24
25// Ring Client
26#include "models/gtkqtreemodel.h"
27#include "native/pixbufmanipulator.h"
28
29// Std
30#include <memory>
31
32// LRC
33#include <accountmodel.h>
34#include <bannedcontactmodel.h>
35#include <globalinstances.h>
36#include <personmodel.h>
37
38// Qt
39#include <QItemSelectionModel>
40#include <QSize>
41
42/**
43 * gtk structure
44 */
45struct _BannedContactsView
46{
47 GtkTreeView parent;
48};
49
50/**
51 * gtk class structure
52 */
53struct _BannedContactsViewClass
54{
55 GtkTreeViewClass parent_class;
56};
57
58typedef struct _BannedContactsViewPrivate BannedContactsViewPrivate;
59
60/**
61 * gtk private structure
62 */
63struct _BannedContactsViewPrivate
64{
65};
66
67G_DEFINE_TYPE_WITH_PRIVATE(BannedContactsView, banned_contacts_view, GTK_TYPE_TREE_VIEW);
68
69#define BANNED_CONTACTS_VIEW_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), BANNED_CONTACTS_VIEW_TYPE, BannedContactsViewPrivate))
70
71/**
72 * callback function to render the peer id
73 */
74static void
75render_peer_id(G_GNUC_UNUSED GtkTreeViewColumn *tree_column,
76 GtkCellRenderer *cell,
77 GtkTreeModel *model,
78 GtkTreeIter *iter,
79 G_GNUC_UNUSED GtkTreeView *treeview)
80{
81 QModelIndex idx = gtk_q_tree_model_get_source_idx(GTK_Q_TREE_MODEL(model), iter);
82
83 auto uri_qt = idx.data(Qt::DisplayRole).toString();
84 auto uri_std = uri_qt.toStdString();
85
86 g_object_set(G_OBJECT(cell), "markup", uri_std.c_str(), NULL);
87}
88
89/**
90 * gtk init function
91 */
92static void
93banned_contacts_view_init(BannedContactsView *self)
94{
95 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(self), FALSE);
96 /* no need to show the expander since it will always be expanded */
97 gtk_tree_view_set_show_expanders(GTK_TREE_VIEW(self), FALSE);
98 /* disable default search otherwise the search steals input focus on key presses */
99 gtk_tree_view_set_enable_search(GTK_TREE_VIEW(self), FALSE);
100
101 const auto idx_account = AccountModel::instance().selectionModel()->currentIndex();
102 auto account = idx_account.data(static_cast<int>(Account::Role::Object)).value<Account*>();
103
104 if(not account)
105 return;
106
107 GtkQTreeModel *recent_model = gtk_q_tree_model_new(
108 account->bannedContactModel(),
109 1,
110 BannedContactModel::Columns::PEER_ID,
111 Qt::DisplayRole, G_TYPE_STRING);
112
113 gtk_tree_view_set_model(GTK_TREE_VIEW(self),
114 GTK_TREE_MODEL(recent_model));
115
116 /* peer id renderer */
117 GtkCellArea *area = gtk_cell_area_box_new();
118 GtkTreeViewColumn *column = gtk_tree_view_column_new_with_area(area);
119 GtkCellRenderer *renderer = gtk_cell_renderer_text_new();
120 g_object_set(G_OBJECT(renderer), "ellipsize", PANGO_ELLIPSIZE_END, NULL);
121 gtk_cell_area_box_pack_start(GTK_CELL_AREA_BOX(area), renderer, FALSE, FALSE, FALSE);
122
123 gtk_tree_view_column_set_cell_data_func(
124 column,
125 renderer,
126 (GtkTreeCellDataFunc)render_peer_id,
127 self,
128 NULL);
129
130 gtk_tree_view_append_column(GTK_TREE_VIEW(self), column);
131 gtk_tree_view_column_set_resizable(column, TRUE);
132 gtk_tree_view_column_set_expand(column, TRUE);
133 gtk_tree_view_expand_all(GTK_TREE_VIEW(self));
134
135 gtk_widget_show_all(GTK_WIDGET(self));
136}
137
138/**
139 * gtk dispose function
140 */
141static void
142banned_contacts_view_dispose(GObject *object)
143{
144 G_OBJECT_CLASS(banned_contacts_view_parent_class)->dispose(object);
145}
146
147/**
148 * gtk finalize function
149 */
150static void
151banned_contacts_view_finalize(GObject *object)
152{
153 G_OBJECT_CLASS(banned_contacts_view_parent_class)->finalize(object);
154}
155
156/**
157 * gtk class init function
158 */
159static void
160banned_contacts_view_class_init(BannedContactsViewClass *klass)
161{
162 G_OBJECT_CLASS(klass)->finalize = banned_contacts_view_finalize;
163 G_OBJECT_CLASS(klass)->dispose = banned_contacts_view_dispose;
164}
165
166/**
167 * gtk new function
168 */
169GtkWidget *
170banned_contacts_view_new()
171{
172 gpointer self = g_object_new(BANNED_CONTACTS_VIEW_TYPE, NULL);
173
174 return (GtkWidget *)self;
175}