blob: c32dbc2545bbf6d2ff4d5e52d545317aa0569384 [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 Salenikovicha5e8e362015-11-05 16:50:48 -050058 GtkWidget *radiobutton_chatright;
59 GtkWidget *radiobutton_chatbottom;
Nicolas Jager6f5e04a2016-05-18 15:04:59 -040060 GtkWidget *box_profil_settings;
61 GtkWidget *avatarmanipulation;
62 GtkWidget *profile_name;
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -040063
Stepan Salenikovichde896112015-05-11 16:46:33 -040064 /* history settings */
65 GtkWidget *adjustment_history_duration;
66 GtkWidget *button_clear_history;
67};
68
Stepan Salenikovichab0f5be2016-02-05 15:16:35 -050069G_DEFINE_TYPE_WITH_PRIVATE(GeneralSettingsView, general_settings_view, GTK_TYPE_SCROLLED_WINDOW);
Stepan Salenikovichde896112015-05-11 16:46:33 -040070
71#define GENERAL_SETTINGS_VIEW_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GENERAL_SETTINGS_VIEW_TYPE, GeneralSettingsViewPrivate))
72
73static void
74general_settings_view_dispose(GObject *object)
75{
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -040076 GeneralSettingsViewPrivate *priv = GENERAL_SETTINGS_VIEW_GET_PRIVATE(object);
77
78 g_clear_object(&priv->settings);
79
Nicolas Jager6f5e04a2016-05-18 15:04:59 -040080 //make sure the VideoWidget is destroyed
81 general_settings_view_show_profile(GENERAL_SETTINGS_VIEW(object), FALSE);
82
Stepan Salenikovichde896112015-05-11 16:46:33 -040083 G_OBJECT_CLASS(general_settings_view_parent_class)->dispose(object);
84}
85
86static void
87history_limit_changed(GtkAdjustment *adjustment, G_GNUC_UNUSED gpointer user_data)
88{
89 int limit = (int)gtk_adjustment_get_value(GTK_ADJUSTMENT(adjustment));
Guillaume Roguez5d1514b2015-10-22 15:55:31 -040090 CategorizedHistoryModel::instance().setHistoryLimit(limit);
Stepan Salenikovichde896112015-05-11 16:46:33 -040091}
92
93static gboolean
94clear_history_dialog(GeneralSettingsView *self)
95{
96 gboolean response = FALSE;
97 GtkWidget *dialog = gtk_message_dialog_new(NULL,
98 (GtkDialogFlags)(GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT),
99 GTK_MESSAGE_QUESTION, GTK_BUTTONS_OK_CANCEL,
Stepan Salenikovicha1b8cb32015-09-11 14:58:35 -0400100 _("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 -0400101
102 gtk_window_set_destroy_with_parent(GTK_WINDOW(dialog), TRUE);
103
104 /* get parent window so we can center on it */
105 GtkWidget *parent = gtk_widget_get_toplevel(GTK_WIDGET(self));
106 if (gtk_widget_is_toplevel(parent)) {
107 gtk_window_set_transient_for(GTK_WINDOW(dialog), GTK_WINDOW(parent));
108 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER_ON_PARENT);
109 }
110
111 switch (gtk_dialog_run(GTK_DIALOG(dialog))) {
112 case GTK_RESPONSE_OK:
113 response = TRUE;
114 break;
115 default:
116 response = FALSE;
117 break;
118 }
119
120 gtk_widget_destroy(dialog);
121
122 return response;
123}
124
125static void
126clear_history(G_GNUC_UNUSED GtkWidget *button, GeneralSettingsView *self)
127{
128 g_return_if_fail(IS_GENERAL_SETTINGS_VIEW(self));
129
130 if (clear_history_dialog(self) )
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400131 CategorizedHistoryModel::instance().clearAllCollections();
Stepan Salenikovichde896112015-05-11 16:46:33 -0400132}
133
134static void
135general_settings_view_init(GeneralSettingsView *self)
136{
137 gtk_widget_init_template(GTK_WIDGET(self));
138
139 GeneralSettingsViewPrivate *priv = GENERAL_SETTINGS_VIEW_GET_PRIVATE(self);
140
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -0400141 priv->settings = g_settings_new_full(get_ring_schema(), NULL, NULL);
142
Stepan Salenikovichbc6c4be2015-07-31 16:07:54 -0400143 /* bind client option to gsettings */
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -0400144 g_settings_bind(priv->settings, "start-on-login",
145 priv->checkbutton_autostart, "active",
146 G_SETTINGS_BIND_DEFAULT);
Stepan Salenikovich982b2882016-06-15 13:13:37 -0400147 g_settings_bind(priv->settings, "show-status-icon",
148 priv->checkbutton_showstatusicon, "active",
Stepan Salenikovichbc6c4be2015-07-31 16:07:54 -0400149 G_SETTINGS_BIND_DEFAULT);
150 g_settings_bind(priv->settings, "bring-window-to-front",
151 priv->checkbutton_bringtofront, "active",
152 G_SETTINGS_BIND_DEFAULT);
Stepan Salenikovich5a127672016-09-13 11:19:50 -0400153 g_settings_bind(priv->settings, "enable-call-notifications",
154 priv->checkbutton_callnotifications, "active",
155 G_SETTINGS_BIND_DEFAULT);
156 g_settings_bind(priv->settings, "enable-chat-notifications",
157 priv->checkbutton_chatnotifications, "active",
158 G_SETTINGS_BIND_DEFAULT);
Stepan Salenikovicha5e8e362015-11-05 16:50:48 -0500159 g_settings_bind(priv->settings, "chat-pane-horizontal",
160 priv->radiobutton_chatright, "active",
161 G_SETTINGS_BIND_DEFAULT);
162 g_settings_bind(priv->settings, "chat-pane-horizontal",
163 priv->radiobutton_chatbottom, "active",
164 (GSettingsBindFlags) (G_SETTINGS_BIND_DEFAULT | G_SETTINGS_BIND_INVERT_BOOLEAN));
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -0400165
Stepan Salenikovichde896112015-05-11 16:46:33 -0400166 /* history limit */
167 gtk_adjustment_set_value(GTK_ADJUSTMENT(priv->adjustment_history_duration),
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400168 CategorizedHistoryModel::instance().historyLimit());
Stepan Salenikovichde896112015-05-11 16:46:33 -0400169 g_signal_connect(priv->adjustment_history_duration,
170 "value-changed", G_CALLBACK(history_limit_changed), NULL);
171
172 /* clear history */
173 g_signal_connect(priv->button_clear_history, "clicked", G_CALLBACK(clear_history), self);
174}
175
176static void
177general_settings_view_class_init(GeneralSettingsViewClass *klass)
178{
179 G_OBJECT_CLASS(klass)->dispose = general_settings_view_dispose;
180
181 gtk_widget_class_set_template_from_resource(GTK_WIDGET_CLASS (klass),
182 "/cx/ring/RingGnome/generalsettingsview.ui");
183
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -0400184 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), GeneralSettingsView, checkbutton_autostart);
Stepan Salenikovich982b2882016-06-15 13:13:37 -0400185 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), GeneralSettingsView, checkbutton_showstatusicon);
Stepan Salenikovichbc6c4be2015-07-31 16:07:54 -0400186 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), GeneralSettingsView, checkbutton_bringtofront);
Stepan Salenikovich5a127672016-09-13 11:19:50 -0400187 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), GeneralSettingsView, checkbutton_callnotifications);
188 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), GeneralSettingsView, checkbutton_chatnotifications);
Stepan Salenikovicha5e8e362015-11-05 16:50:48 -0500189 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), GeneralSettingsView, radiobutton_chatright);
190 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), GeneralSettingsView, radiobutton_chatbottom);
Stepan Salenikovichde896112015-05-11 16:46:33 -0400191 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), GeneralSettingsView, adjustment_history_duration);
192 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), GeneralSettingsView, button_clear_history);
Nicolas Jager6f5e04a2016-05-18 15:04:59 -0400193 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), GeneralSettingsView, box_profil_settings);
Stepan Salenikovichde896112015-05-11 16:46:33 -0400194}
195
196GtkWidget *
197general_settings_view_new()
198{
199 gpointer view = g_object_new(GENERAL_SETTINGS_VIEW_TYPE, NULL);
200
201 return (GtkWidget *)view;
202}
Nicolas Jager6f5e04a2016-05-18 15:04:59 -0400203
204static void
205change_profile_name(GtkEntry *entry)
206{
207 auto profile = ProfileModel::instance().selectedProfile();
208 profile->person()->setFormattedName(gtk_entry_get_text(entry));
209 profile->save();
210}
211
212void
213general_settings_view_show_profile(GeneralSettingsView *self, gboolean show_profile)
214{
215 g_return_if_fail(GENERAL_SETTINGS_VIEW(self));
216 GeneralSettingsViewPrivate *priv = GENERAL_SETTINGS_VIEW_GET_PRIVATE(self);
217
218 /* We will construct and destroy the profile (AvatarManipulation widget) each time the profile
219 * should be visible and hidden, respectively. It is not the "prettiest" way of doing things,
220 * but this way we ensure 1. that the profile is updated correctly when it is shown and 2. that
221 * the VideoWidget inside is destroyed when it is not being shown.
222 */
223 if (show_profile) {
224 /* avatar manipulation widget */
225 priv->avatarmanipulation = avatar_manipulation_new();
226 gtk_box_pack_start(GTK_BOX(priv->box_profil_settings), priv->avatarmanipulation, true, true, 0);
227 gtk_widget_set_visible(priv->avatarmanipulation, true);
228
229 /* print the profile name. as long as we have only one profil, profil name = person name (a.k.a formatedName) */
230 priv->profile_name = gtk_entry_new();
231 gtk_entry_set_text (GTK_ENTRY(priv->profile_name),
232 ProfileModel::instance().selectedProfile()->person()->formattedName().toUtf8().constData());
233 gtk_widget_set_visible(priv->profile_name, true);
234 gtk_entry_set_alignment(GTK_ENTRY(priv->profile_name), 0.5);
235 gtk_box_pack_start(GTK_BOX(priv->box_profil_settings), priv->profile_name, true, true, 0);
236 g_signal_connect(priv->profile_name, "changed", G_CALLBACK(change_profile_name), NULL);
237 } else {
238 if (priv->avatarmanipulation) {
239 gtk_container_remove(GTK_CONTAINER(priv->box_profil_settings), priv->avatarmanipulation);
240 priv->avatarmanipulation = nullptr;
241 }
242 if (priv->profile_name) {
243 gtk_container_remove(GTK_CONTAINER(priv->box_profil_settings), priv->profile_name);
244 priv->profile_name = nullptr;
245 }
246 }
247}