blob: 395c00db280b4204265419b989b9dba87e4a53cf [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>
Stepan Salenikovich738e59a2016-09-16 15:36:11 -040031#include <media/recordingmodel.h>
Stepan Salenikovich5a127672016-09-13 11:19:50 -040032
33// Ring client
34#include "utils/files.h"
35#include "avatarmanipulation.h"
Stepan Salenikovichde896112015-05-11 16:46:33 -040036
37struct _GeneralSettingsView
38{
Stepan Salenikovichab0f5be2016-02-05 15:16:35 -050039 GtkScrolledWindow parent;
Stepan Salenikovichde896112015-05-11 16:46:33 -040040};
41
42struct _GeneralSettingsViewClass
43{
Stepan Salenikovichab0f5be2016-02-05 15:16:35 -050044 GtkScrolledWindowClass parent_class;
Stepan Salenikovichde896112015-05-11 16:46:33 -040045};
46
47typedef struct _GeneralSettingsViewPrivate GeneralSettingsViewPrivate;
48
49struct _GeneralSettingsViewPrivate
50{
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -040051 GSettings *settings;
52
53 /* Rint settings */
54 GtkWidget *checkbutton_autostart;
Stepan Salenikovich982b2882016-06-15 13:13:37 -040055 GtkWidget *checkbutton_showstatusicon;
Stepan Salenikovichbc6c4be2015-07-31 16:07:54 -040056 GtkWidget *checkbutton_bringtofront;
Stepan Salenikovich5a127672016-09-13 11:19:50 -040057 GtkWidget *checkbutton_callnotifications;
58 GtkWidget *checkbutton_chatnotifications;
Stepan Salenikoviche9933242016-06-21 18:08:48 -040059 GtkWidget *checkbutton_searchentryplacescall;
Stepan Salenikovicha5e8e362015-11-05 16:50:48 -050060 GtkWidget *radiobutton_chatright;
61 GtkWidget *radiobutton_chatbottom;
Nicolas Jager6f5e04a2016-05-18 15:04:59 -040062 GtkWidget *box_profil_settings;
63 GtkWidget *avatarmanipulation;
64 GtkWidget *profile_name;
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -040065
Stepan Salenikovichde896112015-05-11 16:46:33 -040066 /* history settings */
67 GtkWidget *adjustment_history_duration;
68 GtkWidget *button_clear_history;
69};
70
Stepan Salenikovichab0f5be2016-02-05 15:16:35 -050071G_DEFINE_TYPE_WITH_PRIVATE(GeneralSettingsView, general_settings_view, GTK_TYPE_SCROLLED_WINDOW);
Stepan Salenikovichde896112015-05-11 16:46:33 -040072
73#define GENERAL_SETTINGS_VIEW_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GENERAL_SETTINGS_VIEW_TYPE, GeneralSettingsViewPrivate))
74
75static void
76general_settings_view_dispose(GObject *object)
77{
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -040078 GeneralSettingsViewPrivate *priv = GENERAL_SETTINGS_VIEW_GET_PRIVATE(object);
79
80 g_clear_object(&priv->settings);
81
Nicolas Jager6f5e04a2016-05-18 15:04:59 -040082 //make sure the VideoWidget is destroyed
83 general_settings_view_show_profile(GENERAL_SETTINGS_VIEW(object), FALSE);
84
Stepan Salenikovichde896112015-05-11 16:46:33 -040085 G_OBJECT_CLASS(general_settings_view_parent_class)->dispose(object);
86}
87
88static void
89history_limit_changed(GtkAdjustment *adjustment, G_GNUC_UNUSED gpointer user_data)
90{
91 int limit = (int)gtk_adjustment_get_value(GTK_ADJUSTMENT(adjustment));
Guillaume Roguez5d1514b2015-10-22 15:55:31 -040092 CategorizedHistoryModel::instance().setHistoryLimit(limit);
Stepan Salenikovichde896112015-05-11 16:46:33 -040093}
94
95static gboolean
96clear_history_dialog(GeneralSettingsView *self)
97{
98 gboolean response = FALSE;
99 GtkWidget *dialog = gtk_message_dialog_new(NULL,
100 (GtkDialogFlags)(GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT),
101 GTK_MESSAGE_QUESTION, GTK_BUTTONS_OK_CANCEL,
Stepan Salenikovich738e59a2016-09-16 15:36:11 -0400102 _("This is a destructive operation. Are you sure you want to delete all of your chat and call history?"));
Stepan Salenikovichde896112015-05-11 16:46:33 -0400103
104 gtk_window_set_destroy_with_parent(GTK_WINDOW(dialog), TRUE);
105
106 /* get parent window so we can center on it */
107 GtkWidget *parent = gtk_widget_get_toplevel(GTK_WIDGET(self));
108 if (gtk_widget_is_toplevel(parent)) {
109 gtk_window_set_transient_for(GTK_WINDOW(dialog), GTK_WINDOW(parent));
110 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER_ON_PARENT);
111 }
112
113 switch (gtk_dialog_run(GTK_DIALOG(dialog))) {
114 case GTK_RESPONSE_OK:
115 response = TRUE;
116 break;
117 default:
118 response = FALSE;
119 break;
120 }
121
122 gtk_widget_destroy(dialog);
123
124 return response;
125}
126
127static void
128clear_history(G_GNUC_UNUSED GtkWidget *button, GeneralSettingsView *self)
129{
130 g_return_if_fail(IS_GENERAL_SETTINGS_VIEW(self));
131
Stepan Salenikovich738e59a2016-09-16 15:36:11 -0400132 if (clear_history_dialog(self) ) {
133 CategorizedHistoryModel::instance().clear();
134 Media::RecordingModel::instance().clear();
135 }
Stepan Salenikovichde896112015-05-11 16:46:33 -0400136}
137
138static void
139general_settings_view_init(GeneralSettingsView *self)
140{
141 gtk_widget_init_template(GTK_WIDGET(self));
142
143 GeneralSettingsViewPrivate *priv = GENERAL_SETTINGS_VIEW_GET_PRIVATE(self);
144
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -0400145 priv->settings = g_settings_new_full(get_ring_schema(), NULL, NULL);
146
Stepan Salenikovichbc6c4be2015-07-31 16:07:54 -0400147 /* bind client option to gsettings */
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -0400148 g_settings_bind(priv->settings, "start-on-login",
149 priv->checkbutton_autostart, "active",
150 G_SETTINGS_BIND_DEFAULT);
Stepan Salenikovich982b2882016-06-15 13:13:37 -0400151 g_settings_bind(priv->settings, "show-status-icon",
152 priv->checkbutton_showstatusicon, "active",
Stepan Salenikovichbc6c4be2015-07-31 16:07:54 -0400153 G_SETTINGS_BIND_DEFAULT);
154 g_settings_bind(priv->settings, "bring-window-to-front",
155 priv->checkbutton_bringtofront, "active",
156 G_SETTINGS_BIND_DEFAULT);
Stepan Salenikovich5a127672016-09-13 11:19:50 -0400157 g_settings_bind(priv->settings, "enable-call-notifications",
158 priv->checkbutton_callnotifications, "active",
159 G_SETTINGS_BIND_DEFAULT);
160 g_settings_bind(priv->settings, "enable-chat-notifications",
161 priv->checkbutton_chatnotifications, "active",
162 G_SETTINGS_BIND_DEFAULT);
Stepan Salenikoviche9933242016-06-21 18:08:48 -0400163 g_settings_bind(priv->settings, "search-entry-places-call",
164 priv->checkbutton_searchentryplacescall, "active",
165 G_SETTINGS_BIND_DEFAULT);
Stepan Salenikovicha5e8e362015-11-05 16:50:48 -0500166 g_settings_bind(priv->settings, "chat-pane-horizontal",
167 priv->radiobutton_chatright, "active",
168 G_SETTINGS_BIND_DEFAULT);
169 g_settings_bind(priv->settings, "chat-pane-horizontal",
170 priv->radiobutton_chatbottom, "active",
171 (GSettingsBindFlags) (G_SETTINGS_BIND_DEFAULT | G_SETTINGS_BIND_INVERT_BOOLEAN));
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -0400172
Stepan Salenikovichde896112015-05-11 16:46:33 -0400173 /* history limit */
174 gtk_adjustment_set_value(GTK_ADJUSTMENT(priv->adjustment_history_duration),
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400175 CategorizedHistoryModel::instance().historyLimit());
Stepan Salenikovichde896112015-05-11 16:46:33 -0400176 g_signal_connect(priv->adjustment_history_duration,
177 "value-changed", G_CALLBACK(history_limit_changed), NULL);
178
179 /* clear history */
180 g_signal_connect(priv->button_clear_history, "clicked", G_CALLBACK(clear_history), self);
181}
182
183static void
184general_settings_view_class_init(GeneralSettingsViewClass *klass)
185{
186 G_OBJECT_CLASS(klass)->dispose = general_settings_view_dispose;
187
188 gtk_widget_class_set_template_from_resource(GTK_WIDGET_CLASS (klass),
189 "/cx/ring/RingGnome/generalsettingsview.ui");
190
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -0400191 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), GeneralSettingsView, checkbutton_autostart);
Stepan Salenikovich982b2882016-06-15 13:13:37 -0400192 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), GeneralSettingsView, checkbutton_showstatusicon);
Stepan Salenikovichbc6c4be2015-07-31 16:07:54 -0400193 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), GeneralSettingsView, checkbutton_bringtofront);
Stepan Salenikovich5a127672016-09-13 11:19:50 -0400194 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), GeneralSettingsView, checkbutton_callnotifications);
195 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), GeneralSettingsView, checkbutton_chatnotifications);
Stepan Salenikoviche9933242016-06-21 18:08:48 -0400196 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), GeneralSettingsView, checkbutton_searchentryplacescall);
Stepan Salenikovicha5e8e362015-11-05 16:50:48 -0500197 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), GeneralSettingsView, radiobutton_chatright);
198 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), GeneralSettingsView, radiobutton_chatbottom);
Stepan Salenikovichde896112015-05-11 16:46:33 -0400199 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), GeneralSettingsView, adjustment_history_duration);
200 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), GeneralSettingsView, button_clear_history);
Nicolas Jager6f5e04a2016-05-18 15:04:59 -0400201 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), GeneralSettingsView, box_profil_settings);
Stepan Salenikovichde896112015-05-11 16:46:33 -0400202}
203
204GtkWidget *
205general_settings_view_new()
206{
207 gpointer view = g_object_new(GENERAL_SETTINGS_VIEW_TYPE, NULL);
208
209 return (GtkWidget *)view;
210}
Nicolas Jager6f5e04a2016-05-18 15:04:59 -0400211
212static void
213change_profile_name(GtkEntry *entry)
214{
215 auto profile = ProfileModel::instance().selectedProfile();
216 profile->person()->setFormattedName(gtk_entry_get_text(entry));
217 profile->save();
218}
219
220void
221general_settings_view_show_profile(GeneralSettingsView *self, gboolean show_profile)
222{
223 g_return_if_fail(GENERAL_SETTINGS_VIEW(self));
224 GeneralSettingsViewPrivate *priv = GENERAL_SETTINGS_VIEW_GET_PRIVATE(self);
225
226 /* We will construct and destroy the profile (AvatarManipulation widget) each time the profile
227 * should be visible and hidden, respectively. It is not the "prettiest" way of doing things,
228 * but this way we ensure 1. that the profile is updated correctly when it is shown and 2. that
229 * the VideoWidget inside is destroyed when it is not being shown.
230 */
231 if (show_profile) {
232 /* avatar manipulation widget */
233 priv->avatarmanipulation = avatar_manipulation_new();
AmarOk436bd9b2017-07-13 15:02:48 -0400234 gtk_widget_set_halign(priv->box_profil_settings, GtkAlign::GTK_ALIGN_CENTER);
Nicolas Jager6f5e04a2016-05-18 15:04:59 -0400235 gtk_box_pack_start(GTK_BOX(priv->box_profil_settings), priv->avatarmanipulation, true, true, 0);
236 gtk_widget_set_visible(priv->avatarmanipulation, true);
237
238 /* print the profile name. as long as we have only one profil, profil name = person name (a.k.a formatedName) */
239 priv->profile_name = gtk_entry_new();
240 gtk_entry_set_text (GTK_ENTRY(priv->profile_name),
241 ProfileModel::instance().selectedProfile()->person()->formattedName().toUtf8().constData());
242 gtk_widget_set_visible(priv->profile_name, true);
243 gtk_entry_set_alignment(GTK_ENTRY(priv->profile_name), 0.5);
244 gtk_box_pack_start(GTK_BOX(priv->box_profil_settings), priv->profile_name, true, true, 0);
245 g_signal_connect(priv->profile_name, "changed", G_CALLBACK(change_profile_name), NULL);
246 } else {
247 if (priv->avatarmanipulation) {
248 gtk_container_remove(GTK_CONTAINER(priv->box_profil_settings), priv->avatarmanipulation);
249 priv->avatarmanipulation = nullptr;
250 }
251 if (priv->profile_name) {
252 gtk_container_remove(GTK_CONTAINER(priv->box_profil_settings), priv->profile_name);
253 priv->profile_name = nullptr;
254 }
255 }
256}