blob: 3bf2e86b6754be1a2941021ed51d1d061db2f806 [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 Salenikovichfb7f2952015-05-25 16:44:19 -040058
Stepan Salenikovichde896112015-05-11 16:46:33 -040059 /* history settings */
60 GtkWidget *adjustment_history_duration;
61 GtkWidget *button_clear_history;
62};
63
64G_DEFINE_TYPE_WITH_PRIVATE(GeneralSettingsView, general_settings_view, GTK_TYPE_BOX);
65
66#define GENERAL_SETTINGS_VIEW_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GENERAL_SETTINGS_VIEW_TYPE, GeneralSettingsViewPrivate))
67
68static void
69general_settings_view_dispose(GObject *object)
70{
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -040071 GeneralSettingsViewPrivate *priv = GENERAL_SETTINGS_VIEW_GET_PRIVATE(object);
72
73 g_clear_object(&priv->settings);
74
Stepan Salenikovichde896112015-05-11 16:46:33 -040075 G_OBJECT_CLASS(general_settings_view_parent_class)->dispose(object);
76}
77
78static void
79history_limit_changed(GtkAdjustment *adjustment, G_GNUC_UNUSED gpointer user_data)
80{
81 int limit = (int)gtk_adjustment_get_value(GTK_ADJUSTMENT(adjustment));
Guillaume Roguez5d1514b2015-10-22 15:55:31 -040082 CategorizedHistoryModel::instance().setHistoryLimit(limit);
Stepan Salenikovichde896112015-05-11 16:46:33 -040083}
84
85static gboolean
86clear_history_dialog(GeneralSettingsView *self)
87{
88 gboolean response = FALSE;
89 GtkWidget *dialog = gtk_message_dialog_new(NULL,
90 (GtkDialogFlags)(GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT),
91 GTK_MESSAGE_QUESTION, GTK_BUTTONS_OK_CANCEL,
Stepan Salenikovicha1b8cb32015-09-11 14:58:35 -040092 _("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 -040093
94 gtk_window_set_destroy_with_parent(GTK_WINDOW(dialog), TRUE);
95
96 /* get parent window so we can center on it */
97 GtkWidget *parent = gtk_widget_get_toplevel(GTK_WIDGET(self));
98 if (gtk_widget_is_toplevel(parent)) {
99 gtk_window_set_transient_for(GTK_WINDOW(dialog), GTK_WINDOW(parent));
100 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER_ON_PARENT);
101 }
102
103 switch (gtk_dialog_run(GTK_DIALOG(dialog))) {
104 case GTK_RESPONSE_OK:
105 response = TRUE;
106 break;
107 default:
108 response = FALSE;
109 break;
110 }
111
112 gtk_widget_destroy(dialog);
113
114 return response;
115}
116
117static void
118clear_history(G_GNUC_UNUSED GtkWidget *button, GeneralSettingsView *self)
119{
120 g_return_if_fail(IS_GENERAL_SETTINGS_VIEW(self));
121
122 if (clear_history_dialog(self) )
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400123 CategorizedHistoryModel::instance().clearAllCollections();
Stepan Salenikovichde896112015-05-11 16:46:33 -0400124}
125
126static void
127general_settings_view_init(GeneralSettingsView *self)
128{
129 gtk_widget_init_template(GTK_WIDGET(self));
130
131 GeneralSettingsViewPrivate *priv = GENERAL_SETTINGS_VIEW_GET_PRIVATE(self);
132
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -0400133 priv->settings = g_settings_new_full(get_ring_schema(), NULL, NULL);
134
Stepan Salenikovichbc6c4be2015-07-31 16:07:54 -0400135 /* bind client option to gsettings */
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -0400136 g_settings_bind(priv->settings, "start-on-login",
137 priv->checkbutton_autostart, "active",
138 G_SETTINGS_BIND_DEFAULT);
Stepan Salenikovichbc6c4be2015-07-31 16:07:54 -0400139 g_settings_bind(priv->settings, "hide-on-close",
140 priv->checkbutton_hideonclose, "active",
141 G_SETTINGS_BIND_DEFAULT);
142 g_settings_bind(priv->settings, "bring-window-to-front",
143 priv->checkbutton_bringtofront, "active",
144 G_SETTINGS_BIND_DEFAULT);
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -0400145
Stepan Salenikovichde896112015-05-11 16:46:33 -0400146 /* history limit */
147 gtk_adjustment_set_value(GTK_ADJUSTMENT(priv->adjustment_history_duration),
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400148 CategorizedHistoryModel::instance().historyLimit());
Stepan Salenikovichde896112015-05-11 16:46:33 -0400149 g_signal_connect(priv->adjustment_history_duration,
150 "value-changed", G_CALLBACK(history_limit_changed), NULL);
151
152 /* clear history */
153 g_signal_connect(priv->button_clear_history, "clicked", G_CALLBACK(clear_history), self);
154}
155
156static void
157general_settings_view_class_init(GeneralSettingsViewClass *klass)
158{
159 G_OBJECT_CLASS(klass)->dispose = general_settings_view_dispose;
160
161 gtk_widget_class_set_template_from_resource(GTK_WIDGET_CLASS (klass),
162 "/cx/ring/RingGnome/generalsettingsview.ui");
163
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -0400164 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), GeneralSettingsView, checkbutton_autostart);
Stepan Salenikovichbc6c4be2015-07-31 16:07:54 -0400165 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), GeneralSettingsView, checkbutton_hideonclose);
166 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), GeneralSettingsView, checkbutton_bringtofront);
Stepan Salenikovichde896112015-05-11 16:46:33 -0400167 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), GeneralSettingsView, adjustment_history_duration);
168 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), GeneralSettingsView, button_clear_history);
169}
170
171GtkWidget *
172general_settings_view_new()
173{
174 gpointer view = g_object_new(GENERAL_SETTINGS_VIEW_TYPE, NULL);
175
176 return (GtkWidget *)view;
177}