blob: e58667175cfee6ec4c69e88276cebf95d0f91fe8 [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// GTK
21#include <gtk/gtk.h>
22#include <glib/gi18n.h>
23
24// LRC
25#include <accountmodel.h>
Nicolas Jagerad4ae042017-04-26 07:50:26 -040026#include <bannedcontactmodel.h>
27#include <contactmethod.h>
Nicolas Jager005fc552017-04-26 07:50:26 -040028#include <personmodel.h>
Nicolas Jager005fc552017-04-26 07:50:26 -040029
30// Ring Client
31#include "accountbanstab.h"
32#include "models/gtkqtreemodel.h"
33#include "bannedcontactsview.h"
34#include "utils/models.h"
35
36// Qt
37#include <QItemSelectionModel>
38#include <QSortFilterProxyModel>
39
40/**
41 * gtk structure
42 */
43struct _AccountBansTab
44{
45 GtkBox parent;
46};
47
48/**
49 * gtk class structure
50 */
51struct _AccountBansTabClass
52{
53 GtkBoxClass parent_class;
54};
55
56typedef struct _AccountBansTabPrivate AccountBansTabPrivate;
57
58/**
59 * gtk private structure
60 */
61struct _AccountBansTabPrivate
62{
63 Account *account;
64 GtkWidget *scrolled_window_bans_tab;
65 GtkWidget *treeview_bans;
Nicolas Jagerad4ae042017-04-26 07:50:26 -040066 GtkWidget *button_unban;
Nicolas Jager005fc552017-04-26 07:50:26 -040067 QMetaObject::Connection account_state_changed;
68
69};
70
71G_DEFINE_TYPE_WITH_PRIVATE(AccountBansTab, account_bans_tab, GTK_TYPE_BOX);
72
73#define ACCOUNT_BANS_TAB_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), ACCOUNT_BANS_TAB_TYPE, AccountBansTabPrivate))
74
75/**
76 * gtk dispose function
77 */
78static void
79account_bans_tab_dispose(GObject *object)
80{
81 AccountBansTab *self = ACCOUNT_BANS_TAB(object);
82 AccountBansTabPrivate *priv = ACCOUNT_BANS_TAB_GET_PRIVATE(self);
83
84 G_OBJECT_CLASS(account_bans_tab_parent_class)->dispose(object);
85 QObject::disconnect(priv->account_state_changed);
86
87}
88
89/**
Nicolas Jagerad4ae042017-04-26 07:50:26 -040090 * gtk callback function called when the selection in the contact request list changed
91 */
92static void
93selection_bans_changed(GtkTreeSelection* selection, AccountBansTab* self)
94{
95 auto priv = ACCOUNT_BANS_TAB_GET_PRIVATE(self);
96 auto has_selection = (gtk_tree_selection_count_selected_rows(selection) > 0);
97
98 gtk_widget_set_sensitive(priv->button_unban, has_selection);
99}
100
101/**
102 * gtk callback function called when button_unban is clicked.
103 */
104static void
105button_unban_clicked(AccountBansTab* view)
106{
107 auto priv = ACCOUNT_BANS_TAB_GET_PRIVATE(view);
108 auto treeview = GTK_TREE_VIEW(priv->treeview_bans);
109 auto tsel = gtk_tree_view_get_selection(treeview);
110
111 GtkTreeModel* tm;
112 GtkTreeIter iter;
113
114 if (gtk_tree_selection_get_selected(tsel , &tm , &iter)) {
115 /* get Account */
116 const auto idx_account = AccountModel::instance().selectionModel()->currentIndex();
117 auto account = idx_account.data(static_cast<int>(Account::Role::Object)).value<Account*>();
118
119 /* get ContactMethod */
120 const auto idx_cm = gtk_q_tree_model_get_source_idx(GTK_Q_TREE_MODEL(gtk_tree_view_get_model(treeview)), &iter);
121 auto cm = idx_cm.data(static_cast<int>(ContactMethod::Role::Object)).value<ContactMethod*>();
122
123 if (not cm or not account) {
124 g_error("cannot unban, invalid pointer(s). cm(%p), account(%p)", cm, account);
125 return;
126 }
127
128 account->bannedContactModel()->remove(cm);
129 }
130}
131
132/**
Nicolas Jager005fc552017-04-26 07:50:26 -0400133 * gtk finalize function
134 */
135static void
136account_bans_tab_finalize(GObject *object)
137{
138 G_OBJECT_CLASS(account_bans_tab_parent_class)->finalize(object);
139}
140
141/**
142 * gtk init function
143 */
144static void
145account_bans_tab_init(AccountBansTab *view)
146{
147 gtk_widget_init_template(GTK_WIDGET(view));
148 AccountBansTabPrivate *priv = ACCOUNT_BANS_TAB_GET_PRIVATE(view);
149
150 priv->treeview_bans = banned_contacts_view_new();
151 gtk_container_add(GTK_CONTAINER(priv->scrolled_window_bans_tab), priv->treeview_bans);
Nicolas Jagerad4ae042017-04-26 07:50:26 -0400152
153 auto selection_bans = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_bans));
154
155 g_signal_connect_swapped(priv->button_unban, "clicked", G_CALLBACK(button_unban_clicked), view);
156 g_signal_connect(selection_bans, "changed", G_CALLBACK(selection_bans_changed), view);
Nicolas Jager005fc552017-04-26 07:50:26 -0400157}
158
159/**
160 * gtk class init function
161 */
162static void
163account_bans_tab_class_init(AccountBansTabClass *klass)
164{
165 G_OBJECT_CLASS(klass)->finalize = account_bans_tab_finalize;
166 G_OBJECT_CLASS(klass)->dispose = account_bans_tab_dispose;
167
168 gtk_widget_class_set_template_from_resource(GTK_WIDGET_CLASS (klass), "/cx/ring/RingGnome/accountbanstab.ui");
169
170 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), AccountBansTab, scrolled_window_bans_tab);
Nicolas Jagerad4ae042017-04-26 07:50:26 -0400171 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), AccountBansTab, button_unban);
Nicolas Jager005fc552017-04-26 07:50:26 -0400172}
173
174/**
175 * gtk new function
176 */
177GtkWidget *
178account_bans_tab_new(Account *account)
179{
180 g_return_val_if_fail(account != NULL, NULL);
181
182 gpointer view = g_object_new(ACCOUNT_BANS_TAB_TYPE, NULL);
183
184 AccountBansTabPrivate *priv = ACCOUNT_BANS_TAB_GET_PRIVATE(view);
185 priv->account = account;
186
187 return (GtkWidget *)view;
188}