blob: 294b3d3fb2e1bf348d9fc0d98bd3ffb62cba827e [file] [log] [blame]
Stepan Salenikovichde896112015-05-11 16:46:33 -04001/*
Stepan Salenikovichbe87d2c2016-01-25 14:14:34 -05002 * Copyright (C) 2015-2016 Savoir-faire Linux Inc.
Stepan Salenikovichde896112015-05-11 16:46:33 -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 Salenikovichde896112015-05-11 16:46:33 -040018 */
19
20#include "generalsettingsview.h"
21
Stepan Salenikovich5a127672016-09-13 11:19:50 -040022// GTK+ related
Stepan Salenikovichde896112015-05-11 16:46:33 -040023#include <gtk/gtk.h>
Stepan Salenikovicha1b8cb32015-09-11 14:58:35 -040024#include <glib/gi18n.h>
Nicolas Jager6f5e04a2016-05-18 15:04:59 -040025
Stepan Salenikovich5a127672016-09-13 11:19:50 -040026// LRC
Nicolas Jager6f5e04a2016-05-18 15:04:59 -040027#include <person.h>
28#include <profile.h>
29#include <profilemodel.h>
Stepan Salenikovich5a127672016-09-13 11:19:50 -040030#include <categorizedhistorymodel.h>
31
32// Ring client
33#include "utils/files.h"
34#include "avatarmanipulation.h"
Stepan Salenikovichde896112015-05-11 16:46:33 -040035
36struct _GeneralSettingsView
37{
Stepan Salenikovichab0f5be2016-02-05 15:16:35 -050038 GtkScrolledWindow parent;
Stepan Salenikovichde896112015-05-11 16:46:33 -040039};
40
41struct _GeneralSettingsViewClass
42{
Stepan Salenikovichab0f5be2016-02-05 15:16:35 -050043 GtkScrolledWindowClass parent_class;
Stepan Salenikovichde896112015-05-11 16:46:33 -040044};
45
46typedef struct _GeneralSettingsViewPrivate GeneralSettingsViewPrivate;
47
48struct _GeneralSettingsViewPrivate
49{
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -040050 GSettings *settings;
51
52 /* Rint settings */
53 GtkWidget *checkbutton_autostart;
Stepan Salenikovich982b2882016-06-15 13:13:37 -040054 GtkWidget *checkbutton_showstatusicon;
Stepan Salenikovichbc6c4be2015-07-31 16:07:54 -040055 GtkWidget *checkbutton_bringtofront;
Stepan Salenikovich5a127672016-09-13 11:19:50 -040056 GtkWidget *checkbutton_callnotifications;
57 GtkWidget *checkbutton_chatnotifications;
Stepan Salenikoviche9933242016-06-21 18:08:48 -040058 GtkWidget *checkbutton_searchentryplacescall;
Stepan Salenikovicha5e8e362015-11-05 16:50:48 -050059 GtkWidget *radiobutton_chatright;
60 GtkWidget *radiobutton_chatbottom;
Nicolas Jager6f5e04a2016-05-18 15:04:59 -040061 GtkWidget *box_profil_settings;
62 GtkWidget *avatarmanipulation;
63 GtkWidget *profile_name;
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -040064
Stepan Salenikovichde896112015-05-11 16:46:33 -040065 /* history settings */
66 GtkWidget *adjustment_history_duration;
67 GtkWidget *button_clear_history;
68};
69
Stepan Salenikovichab0f5be2016-02-05 15:16:35 -050070G_DEFINE_TYPE_WITH_PRIVATE(GeneralSettingsView, general_settings_view, GTK_TYPE_SCROLLED_WINDOW);
Stepan Salenikovichde896112015-05-11 16:46:33 -040071
72#define GENERAL_SETTINGS_VIEW_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GENERAL_SETTINGS_VIEW_TYPE, GeneralSettingsViewPrivate))
73
74static void
75general_settings_view_dispose(GObject *object)
76{
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -040077 GeneralSettingsViewPrivate *priv = GENERAL_SETTINGS_VIEW_GET_PRIVATE(object);
78
79 g_clear_object(&priv->settings);
80
Nicolas Jager6f5e04a2016-05-18 15:04:59 -040081 //make sure the VideoWidget is destroyed
82 general_settings_view_show_profile(GENERAL_SETTINGS_VIEW(object), FALSE);
83
Stepan Salenikovichde896112015-05-11 16:46:33 -040084 G_OBJECT_CLASS(general_settings_view_parent_class)->dispose(object);
85}
86
87static void
88history_limit_changed(GtkAdjustment *adjustment, G_GNUC_UNUSED gpointer user_data)
89{
90 int limit = (int)gtk_adjustment_get_value(GTK_ADJUSTMENT(adjustment));
Guillaume Roguez5d1514b2015-10-22 15:55:31 -040091 CategorizedHistoryModel::instance().setHistoryLimit(limit);
Stepan Salenikovichde896112015-05-11 16:46:33 -040092}
93
94static gboolean
95clear_history_dialog(GeneralSettingsView *self)
96{
97 gboolean response = FALSE;
98 GtkWidget *dialog = gtk_message_dialog_new(NULL,
99 (GtkDialogFlags)(GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT),
100 GTK_MESSAGE_QUESTION, GTK_BUTTONS_OK_CANCEL,
Stepan Salenikovicha1b8cb32015-09-11 14:58:35 -0400101 _("Are you sure you want to clear all your history?\nThis operation will also reset the Frequent Contacts list"));
Stepan Salenikovichde896112015-05-11 16:46:33 -0400102
103 gtk_window_set_destroy_with_parent(GTK_WINDOW(dialog), TRUE);
104
105 /* get parent window so we can center on it */
106 GtkWidget *parent = gtk_widget_get_toplevel(GTK_WIDGET(self));
107 if (gtk_widget_is_toplevel(parent)) {
108 gtk_window_set_transient_for(GTK_WINDOW(dialog), GTK_WINDOW(parent));
109 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER_ON_PARENT);
110 }
111
112 switch (gtk_dialog_run(GTK_DIALOG(dialog))) {
113 case GTK_RESPONSE_OK:
114 response = TRUE;
115 break;
116 default:
117 response = FALSE;
118 break;
119 }
120
121 gtk_widget_destroy(dialog);
122
123 return response;
124}
125
126static void
127clear_history(G_GNUC_UNUSED GtkWidget *button, GeneralSettingsView *self)
128{
129 g_return_if_fail(IS_GENERAL_SETTINGS_VIEW(self));
130
131 if (clear_history_dialog(self) )
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400132 CategorizedHistoryModel::instance().clearAllCollections();
Stepan Salenikovichde896112015-05-11 16:46:33 -0400133}
134
135static void
136general_settings_view_init(GeneralSettingsView *self)
137{
138 gtk_widget_init_template(GTK_WIDGET(self));
139
140 GeneralSettingsViewPrivate *priv = GENERAL_SETTINGS_VIEW_GET_PRIVATE(self);
141
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -0400142 priv->settings = g_settings_new_full(get_ring_schema(), NULL, NULL);
143
Stepan Salenikovichbc6c4be2015-07-31 16:07:54 -0400144 /* bind client option to gsettings */
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -0400145 g_settings_bind(priv->settings, "start-on-login",
146 priv->checkbutton_autostart, "active",
147 G_SETTINGS_BIND_DEFAULT);
Stepan Salenikovich982b2882016-06-15 13:13:37 -0400148 g_settings_bind(priv->settings, "show-status-icon",
149 priv->checkbutton_showstatusicon, "active",
Stepan Salenikovichbc6c4be2015-07-31 16:07:54 -0400150 G_SETTINGS_BIND_DEFAULT);
151 g_settings_bind(priv->settings, "bring-window-to-front",
152 priv->checkbutton_bringtofront, "active",
153 G_SETTINGS_BIND_DEFAULT);
Stepan Salenikovich5a127672016-09-13 11:19:50 -0400154 g_settings_bind(priv->settings, "enable-call-notifications",
155 priv->checkbutton_callnotifications, "active",
156 G_SETTINGS_BIND_DEFAULT);
157 g_settings_bind(priv->settings, "enable-chat-notifications",
158 priv->checkbutton_chatnotifications, "active",
159 G_SETTINGS_BIND_DEFAULT);
Stepan Salenikoviche9933242016-06-21 18:08:48 -0400160 g_settings_bind(priv->settings, "search-entry-places-call",
161 priv->checkbutton_searchentryplacescall, "active",
162 G_SETTINGS_BIND_DEFAULT);
Stepan Salenikovicha5e8e362015-11-05 16:50:48 -0500163 g_settings_bind(priv->settings, "chat-pane-horizontal",
164 priv->radiobutton_chatright, "active",
165 G_SETTINGS_BIND_DEFAULT);
166 g_settings_bind(priv->settings, "chat-pane-horizontal",
167 priv->radiobutton_chatbottom, "active",
168 (GSettingsBindFlags) (G_SETTINGS_BIND_DEFAULT | G_SETTINGS_BIND_INVERT_BOOLEAN));
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -0400169
Stepan Salenikovichde896112015-05-11 16:46:33 -0400170 /* history limit */
171 gtk_adjustment_set_value(GTK_ADJUSTMENT(priv->adjustment_history_duration),
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400172 CategorizedHistoryModel::instance().historyLimit());
Stepan Salenikovichde896112015-05-11 16:46:33 -0400173 g_signal_connect(priv->adjustment_history_duration,
174 "value-changed", G_CALLBACK(history_limit_changed), NULL);
175
176 /* clear history */
177 g_signal_connect(priv->button_clear_history, "clicked", G_CALLBACK(clear_history), self);
178}
179
180static void
181general_settings_view_class_init(GeneralSettingsViewClass *klass)
182{
183 G_OBJECT_CLASS(klass)->dispose = general_settings_view_dispose;
184
185 gtk_widget_class_set_template_from_resource(GTK_WIDGET_CLASS (klass),
186 "/cx/ring/RingGnome/generalsettingsview.ui");
187
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -0400188 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), GeneralSettingsView, checkbutton_autostart);
Stepan Salenikovich982b2882016-06-15 13:13:37 -0400189 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), GeneralSettingsView, checkbutton_showstatusicon);
Stepan Salenikovichbc6c4be2015-07-31 16:07:54 -0400190 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), GeneralSettingsView, checkbutton_bringtofront);
Stepan Salenikovich5a127672016-09-13 11:19:50 -0400191 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), GeneralSettingsView, checkbutton_callnotifications);
192 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), GeneralSettingsView, checkbutton_chatnotifications);
Stepan Salenikoviche9933242016-06-21 18:08:48 -0400193 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), GeneralSettingsView, checkbutton_searchentryplacescall);
Stepan Salenikovicha5e8e362015-11-05 16:50:48 -0500194 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), GeneralSettingsView, radiobutton_chatright);
195 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), GeneralSettingsView, radiobutton_chatbottom);
Stepan Salenikovichde896112015-05-11 16:46:33 -0400196 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), GeneralSettingsView, adjustment_history_duration);
197 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), GeneralSettingsView, button_clear_history);
Nicolas Jager6f5e04a2016-05-18 15:04:59 -0400198 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), GeneralSettingsView, box_profil_settings);
Stepan Salenikovichde896112015-05-11 16:46:33 -0400199}
200
201GtkWidget *
202general_settings_view_new()
203{
204 gpointer view = g_object_new(GENERAL_SETTINGS_VIEW_TYPE, NULL);
205
206 return (GtkWidget *)view;
207}
Nicolas Jager6f5e04a2016-05-18 15:04:59 -0400208
209static void
210change_profile_name(GtkEntry *entry)
211{
212 auto profile = ProfileModel::instance().selectedProfile();
213 profile->person()->setFormattedName(gtk_entry_get_text(entry));
214 profile->save();
215}
216
217void
218general_settings_view_show_profile(GeneralSettingsView *self, gboolean show_profile)
219{
220 g_return_if_fail(GENERAL_SETTINGS_VIEW(self));
221 GeneralSettingsViewPrivate *priv = GENERAL_SETTINGS_VIEW_GET_PRIVATE(self);
222
223 /* We will construct and destroy the profile (AvatarManipulation widget) each time the profile
224 * should be visible and hidden, respectively. It is not the "prettiest" way of doing things,
225 * but this way we ensure 1. that the profile is updated correctly when it is shown and 2. that
226 * the VideoWidget inside is destroyed when it is not being shown.
227 */
228 if (show_profile) {
229 /* avatar manipulation widget */
230 priv->avatarmanipulation = avatar_manipulation_new();
231 gtk_box_pack_start(GTK_BOX(priv->box_profil_settings), priv->avatarmanipulation, true, true, 0);
232 gtk_widget_set_visible(priv->avatarmanipulation, true);
233
234 /* print the profile name. as long as we have only one profil, profil name = person name (a.k.a formatedName) */
235 priv->profile_name = gtk_entry_new();
236 gtk_entry_set_text (GTK_ENTRY(priv->profile_name),
237 ProfileModel::instance().selectedProfile()->person()->formattedName().toUtf8().constData());
238 gtk_widget_set_visible(priv->profile_name, true);
239 gtk_entry_set_alignment(GTK_ENTRY(priv->profile_name), 0.5);
240 gtk_box_pack_start(GTK_BOX(priv->box_profil_settings), priv->profile_name, true, true, 0);
241 g_signal_connect(priv->profile_name, "changed", G_CALLBACK(change_profile_name), NULL);
242 } else {
243 if (priv->avatarmanipulation) {
244 gtk_container_remove(GTK_CONTAINER(priv->box_profil_settings), priv->avatarmanipulation);
245 priv->avatarmanipulation = nullptr;
246 }
247 if (priv->profile_name) {
248 gtk_container_remove(GTK_CONTAINER(priv->box_profil_settings), priv->profile_name);
249 priv->profile_name = nullptr;
250 }
251 }
252}