blob: 82b01f22dfe2bc5bd998db55fa27dfafe148c4d5 [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
22#include <gtk/gtk.h>
Stepan Salenikovicha1b8cb32015-09-11 14:58:35 -040023#include <glib/gi18n.h>
Stepan Salenikovichde896112015-05-11 16:46:33 -040024#include <categorizedhistorymodel.h>
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -040025#include "utils/files.h"
Stepan Salenikovichde896112015-05-11 16:46:33 -040026
27struct _GeneralSettingsView
28{
Stepan Salenikovichab0f5be2016-02-05 15:16:35 -050029 GtkScrolledWindow parent;
Stepan Salenikovichde896112015-05-11 16:46:33 -040030};
31
32struct _GeneralSettingsViewClass
33{
Stepan Salenikovichab0f5be2016-02-05 15:16:35 -050034 GtkScrolledWindowClass parent_class;
Stepan Salenikovichde896112015-05-11 16:46:33 -040035};
36
37typedef struct _GeneralSettingsViewPrivate GeneralSettingsViewPrivate;
38
39struct _GeneralSettingsViewPrivate
40{
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -040041 GSettings *settings;
42
43 /* Rint settings */
44 GtkWidget *checkbutton_autostart;
Stepan Salenikovichbc6c4be2015-07-31 16:07:54 -040045 GtkWidget *checkbutton_hideonclose;
46 GtkWidget *checkbutton_bringtofront;
Stepan Salenikovicha5e8e362015-11-05 16:50:48 -050047 GtkWidget *radiobutton_chatright;
48 GtkWidget *radiobutton_chatbottom;
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -040049
Stepan Salenikovichde896112015-05-11 16:46:33 -040050 /* history settings */
51 GtkWidget *adjustment_history_duration;
52 GtkWidget *button_clear_history;
53};
54
Stepan Salenikovichab0f5be2016-02-05 15:16:35 -050055G_DEFINE_TYPE_WITH_PRIVATE(GeneralSettingsView, general_settings_view, GTK_TYPE_SCROLLED_WINDOW);
Stepan Salenikovichde896112015-05-11 16:46:33 -040056
57#define GENERAL_SETTINGS_VIEW_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GENERAL_SETTINGS_VIEW_TYPE, GeneralSettingsViewPrivate))
58
59static void
60general_settings_view_dispose(GObject *object)
61{
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -040062 GeneralSettingsViewPrivate *priv = GENERAL_SETTINGS_VIEW_GET_PRIVATE(object);
63
64 g_clear_object(&priv->settings);
65
Stepan Salenikovichde896112015-05-11 16:46:33 -040066 G_OBJECT_CLASS(general_settings_view_parent_class)->dispose(object);
67}
68
69static void
70history_limit_changed(GtkAdjustment *adjustment, G_GNUC_UNUSED gpointer user_data)
71{
72 int limit = (int)gtk_adjustment_get_value(GTK_ADJUSTMENT(adjustment));
Guillaume Roguez5d1514b2015-10-22 15:55:31 -040073 CategorizedHistoryModel::instance().setHistoryLimit(limit);
Stepan Salenikovichde896112015-05-11 16:46:33 -040074}
75
76static gboolean
77clear_history_dialog(GeneralSettingsView *self)
78{
79 gboolean response = FALSE;
80 GtkWidget *dialog = gtk_message_dialog_new(NULL,
81 (GtkDialogFlags)(GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT),
82 GTK_MESSAGE_QUESTION, GTK_BUTTONS_OK_CANCEL,
Stepan Salenikovicha1b8cb32015-09-11 14:58:35 -040083 _("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 -040084
85 gtk_window_set_destroy_with_parent(GTK_WINDOW(dialog), TRUE);
86
87 /* get parent window so we can center on it */
88 GtkWidget *parent = gtk_widget_get_toplevel(GTK_WIDGET(self));
89 if (gtk_widget_is_toplevel(parent)) {
90 gtk_window_set_transient_for(GTK_WINDOW(dialog), GTK_WINDOW(parent));
91 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER_ON_PARENT);
92 }
93
94 switch (gtk_dialog_run(GTK_DIALOG(dialog))) {
95 case GTK_RESPONSE_OK:
96 response = TRUE;
97 break;
98 default:
99 response = FALSE;
100 break;
101 }
102
103 gtk_widget_destroy(dialog);
104
105 return response;
106}
107
108static void
109clear_history(G_GNUC_UNUSED GtkWidget *button, GeneralSettingsView *self)
110{
111 g_return_if_fail(IS_GENERAL_SETTINGS_VIEW(self));
112
113 if (clear_history_dialog(self) )
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400114 CategorizedHistoryModel::instance().clearAllCollections();
Stepan Salenikovichde896112015-05-11 16:46:33 -0400115}
116
117static void
118general_settings_view_init(GeneralSettingsView *self)
119{
120 gtk_widget_init_template(GTK_WIDGET(self));
121
122 GeneralSettingsViewPrivate *priv = GENERAL_SETTINGS_VIEW_GET_PRIVATE(self);
123
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -0400124 priv->settings = g_settings_new_full(get_ring_schema(), NULL, NULL);
125
Stepan Salenikovichbc6c4be2015-07-31 16:07:54 -0400126 /* bind client option to gsettings */
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -0400127 g_settings_bind(priv->settings, "start-on-login",
128 priv->checkbutton_autostart, "active",
129 G_SETTINGS_BIND_DEFAULT);
Stepan Salenikovichbc6c4be2015-07-31 16:07:54 -0400130 g_settings_bind(priv->settings, "hide-on-close",
131 priv->checkbutton_hideonclose, "active",
132 G_SETTINGS_BIND_DEFAULT);
133 g_settings_bind(priv->settings, "bring-window-to-front",
134 priv->checkbutton_bringtofront, "active",
135 G_SETTINGS_BIND_DEFAULT);
Stepan Salenikovicha5e8e362015-11-05 16:50:48 -0500136 g_settings_bind(priv->settings, "chat-pane-horizontal",
137 priv->radiobutton_chatright, "active",
138 G_SETTINGS_BIND_DEFAULT);
139 g_settings_bind(priv->settings, "chat-pane-horizontal",
140 priv->radiobutton_chatbottom, "active",
141 (GSettingsBindFlags) (G_SETTINGS_BIND_DEFAULT | G_SETTINGS_BIND_INVERT_BOOLEAN));
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -0400142
Stepan Salenikovichde896112015-05-11 16:46:33 -0400143 /* history limit */
144 gtk_adjustment_set_value(GTK_ADJUSTMENT(priv->adjustment_history_duration),
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400145 CategorizedHistoryModel::instance().historyLimit());
Stepan Salenikovichde896112015-05-11 16:46:33 -0400146 g_signal_connect(priv->adjustment_history_duration,
147 "value-changed", G_CALLBACK(history_limit_changed), NULL);
148
149 /* clear history */
150 g_signal_connect(priv->button_clear_history, "clicked", G_CALLBACK(clear_history), self);
151}
152
153static void
154general_settings_view_class_init(GeneralSettingsViewClass *klass)
155{
156 G_OBJECT_CLASS(klass)->dispose = general_settings_view_dispose;
157
158 gtk_widget_class_set_template_from_resource(GTK_WIDGET_CLASS (klass),
159 "/cx/ring/RingGnome/generalsettingsview.ui");
160
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -0400161 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), GeneralSettingsView, checkbutton_autostart);
Stepan Salenikovichbc6c4be2015-07-31 16:07:54 -0400162 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), GeneralSettingsView, checkbutton_hideonclose);
163 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), GeneralSettingsView, checkbutton_bringtofront);
Stepan Salenikovicha5e8e362015-11-05 16:50:48 -0500164 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), GeneralSettingsView, radiobutton_chatright);
165 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), GeneralSettingsView, radiobutton_chatbottom);
Stepan Salenikovichde896112015-05-11 16:46:33 -0400166 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), GeneralSettingsView, adjustment_history_duration);
167 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), GeneralSettingsView, button_clear_history);
168}
169
170GtkWidget *
171general_settings_view_new()
172{
173 gpointer view = g_object_new(GENERAL_SETTINGS_VIEW_TYPE, NULL);
174
175 return (GtkWidget *)view;
176}