blob: 9bf1e067131d4ec821c2a1430e66896fc8780d53 [file] [log] [blame]
Stepan Salenikovichde896112015-05-11 16:46:33 -04001/*
2 * Copyright (C) 2015 Savoir-Faire Linux Inc.
3 * 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.
18 *
19 * Additional permission under GNU GPL version 3 section 7:
20 *
21 * If you modify this program, or any covered work, by linking or
22 * combining it with the OpenSSL project's OpenSSL library (or a
23 * modified version of that library), containing parts covered by the
24 * terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
25 * grants you additional permission to convey the resulting work.
26 * Corresponding Source for a non-source form of such a combination
27 * shall include the source code for the parts of OpenSSL used as well
28 * as that of the covered work.
29 */
30
31#include "generalsettingsview.h"
32
33#include <gtk/gtk.h>
Stepan Salenikovicha1b8cb32015-09-11 14:58:35 -040034#include <glib/gi18n.h>
Stepan Salenikovichde896112015-05-11 16:46:33 -040035#include <categorizedhistorymodel.h>
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -040036#include "utils/files.h"
Stepan Salenikovichde896112015-05-11 16:46:33 -040037
38struct _GeneralSettingsView
39{
40 GtkBox parent;
41};
42
43struct _GeneralSettingsViewClass
44{
45 GtkBoxClass parent_class;
46};
47
48typedef struct _GeneralSettingsViewPrivate GeneralSettingsViewPrivate;
49
50struct _GeneralSettingsViewPrivate
51{
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -040052 GSettings *settings;
53
54 /* Rint settings */
55 GtkWidget *checkbutton_autostart;
Stepan Salenikovichbc6c4be2015-07-31 16:07:54 -040056 GtkWidget *checkbutton_hideonclose;
57 GtkWidget *checkbutton_bringtofront;
Stepan Salenikovicha5e8e362015-11-05 16:50:48 -050058 GtkWidget *radiobutton_chatright;
59 GtkWidget *radiobutton_chatbottom;
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -040060
Stepan Salenikovichde896112015-05-11 16:46:33 -040061 /* history settings */
62 GtkWidget *adjustment_history_duration;
63 GtkWidget *button_clear_history;
64};
65
66G_DEFINE_TYPE_WITH_PRIVATE(GeneralSettingsView, general_settings_view, GTK_TYPE_BOX);
67
68#define GENERAL_SETTINGS_VIEW_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GENERAL_SETTINGS_VIEW_TYPE, GeneralSettingsViewPrivate))
69
70static void
71general_settings_view_dispose(GObject *object)
72{
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -040073 GeneralSettingsViewPrivate *priv = GENERAL_SETTINGS_VIEW_GET_PRIVATE(object);
74
75 g_clear_object(&priv->settings);
76
Stepan Salenikovichde896112015-05-11 16:46:33 -040077 G_OBJECT_CLASS(general_settings_view_parent_class)->dispose(object);
78}
79
80static void
81history_limit_changed(GtkAdjustment *adjustment, G_GNUC_UNUSED gpointer user_data)
82{
83 int limit = (int)gtk_adjustment_get_value(GTK_ADJUSTMENT(adjustment));
Guillaume Roguez5d1514b2015-10-22 15:55:31 -040084 CategorizedHistoryModel::instance().setHistoryLimit(limit);
Stepan Salenikovichde896112015-05-11 16:46:33 -040085}
86
87static gboolean
88clear_history_dialog(GeneralSettingsView *self)
89{
90 gboolean response = FALSE;
91 GtkWidget *dialog = gtk_message_dialog_new(NULL,
92 (GtkDialogFlags)(GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT),
93 GTK_MESSAGE_QUESTION, GTK_BUTTONS_OK_CANCEL,
Stepan Salenikovicha1b8cb32015-09-11 14:58:35 -040094 _("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 -040095
96 gtk_window_set_destroy_with_parent(GTK_WINDOW(dialog), TRUE);
97
98 /* get parent window so we can center on it */
99 GtkWidget *parent = gtk_widget_get_toplevel(GTK_WIDGET(self));
100 if (gtk_widget_is_toplevel(parent)) {
101 gtk_window_set_transient_for(GTK_WINDOW(dialog), GTK_WINDOW(parent));
102 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER_ON_PARENT);
103 }
104
105 switch (gtk_dialog_run(GTK_DIALOG(dialog))) {
106 case GTK_RESPONSE_OK:
107 response = TRUE;
108 break;
109 default:
110 response = FALSE;
111 break;
112 }
113
114 gtk_widget_destroy(dialog);
115
116 return response;
117}
118
119static void
120clear_history(G_GNUC_UNUSED GtkWidget *button, GeneralSettingsView *self)
121{
122 g_return_if_fail(IS_GENERAL_SETTINGS_VIEW(self));
123
124 if (clear_history_dialog(self) )
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400125 CategorizedHistoryModel::instance().clearAllCollections();
Stepan Salenikovichde896112015-05-11 16:46:33 -0400126}
127
128static void
129general_settings_view_init(GeneralSettingsView *self)
130{
131 gtk_widget_init_template(GTK_WIDGET(self));
132
133 GeneralSettingsViewPrivate *priv = GENERAL_SETTINGS_VIEW_GET_PRIVATE(self);
134
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -0400135 priv->settings = g_settings_new_full(get_ring_schema(), NULL, NULL);
136
Stepan Salenikovichbc6c4be2015-07-31 16:07:54 -0400137 /* bind client option to gsettings */
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -0400138 g_settings_bind(priv->settings, "start-on-login",
139 priv->checkbutton_autostart, "active",
140 G_SETTINGS_BIND_DEFAULT);
Stepan Salenikovichbc6c4be2015-07-31 16:07:54 -0400141 g_settings_bind(priv->settings, "hide-on-close",
142 priv->checkbutton_hideonclose, "active",
143 G_SETTINGS_BIND_DEFAULT);
144 g_settings_bind(priv->settings, "bring-window-to-front",
145 priv->checkbutton_bringtofront, "active",
146 G_SETTINGS_BIND_DEFAULT);
Stepan Salenikovicha5e8e362015-11-05 16:50:48 -0500147 g_settings_bind(priv->settings, "chat-pane-horizontal",
148 priv->radiobutton_chatright, "active",
149 G_SETTINGS_BIND_DEFAULT);
150 g_settings_bind(priv->settings, "chat-pane-horizontal",
151 priv->radiobutton_chatbottom, "active",
152 (GSettingsBindFlags) (G_SETTINGS_BIND_DEFAULT | G_SETTINGS_BIND_INVERT_BOOLEAN));
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -0400153
Stepan Salenikovichde896112015-05-11 16:46:33 -0400154 /* history limit */
155 gtk_adjustment_set_value(GTK_ADJUSTMENT(priv->adjustment_history_duration),
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400156 CategorizedHistoryModel::instance().historyLimit());
Stepan Salenikovichde896112015-05-11 16:46:33 -0400157 g_signal_connect(priv->adjustment_history_duration,
158 "value-changed", G_CALLBACK(history_limit_changed), NULL);
159
160 /* clear history */
161 g_signal_connect(priv->button_clear_history, "clicked", G_CALLBACK(clear_history), self);
162}
163
164static void
165general_settings_view_class_init(GeneralSettingsViewClass *klass)
166{
167 G_OBJECT_CLASS(klass)->dispose = general_settings_view_dispose;
168
169 gtk_widget_class_set_template_from_resource(GTK_WIDGET_CLASS (klass),
170 "/cx/ring/RingGnome/generalsettingsview.ui");
171
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -0400172 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), GeneralSettingsView, checkbutton_autostart);
Stepan Salenikovichbc6c4be2015-07-31 16:07:54 -0400173 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), GeneralSettingsView, checkbutton_hideonclose);
174 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), GeneralSettingsView, checkbutton_bringtofront);
Stepan Salenikovicha5e8e362015-11-05 16:50:48 -0500175 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), GeneralSettingsView, radiobutton_chatright);
176 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), GeneralSettingsView, radiobutton_chatbottom);
Stepan Salenikovichde896112015-05-11 16:46:33 -0400177 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), GeneralSettingsView, adjustment_history_duration);
178 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), GeneralSettingsView, button_clear_history);
179}
180
181GtkWidget *
182general_settings_view_new()
183{
184 gpointer view = g_object_new(GENERAL_SETTINGS_VIEW_TYPE, NULL);
185
186 return (GtkWidget *)view;
187}