blob: 95354be92a9c2394fcc5af062b3265449b43bacf [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>
34#include <categorizedhistorymodel.h>
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -040035#include "utils/files.h"
Stepan Salenikovichde896112015-05-11 16:46:33 -040036
37struct _GeneralSettingsView
38{
39 GtkBox parent;
40};
41
42struct _GeneralSettingsViewClass
43{
44 GtkBoxClass parent_class;
45};
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 Salenikovichbc6c4be2015-07-31 16:07:54 -040055 GtkWidget *checkbutton_hideonclose;
56 GtkWidget *checkbutton_bringtofront;
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -040057
Stepan Salenikovichde896112015-05-11 16:46:33 -040058 /* history settings */
59 GtkWidget *adjustment_history_duration;
60 GtkWidget *button_clear_history;
61};
62
63G_DEFINE_TYPE_WITH_PRIVATE(GeneralSettingsView, general_settings_view, GTK_TYPE_BOX);
64
65#define GENERAL_SETTINGS_VIEW_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GENERAL_SETTINGS_VIEW_TYPE, GeneralSettingsViewPrivate))
66
67static void
68general_settings_view_dispose(GObject *object)
69{
Stepan Salenikovichfb7f2952015-05-25 16:44:19 -040070 GeneralSettingsViewPrivate *priv = GENERAL_SETTINGS_VIEW_GET_PRIVATE(object);
71
72 g_clear_object(&priv->settings);
73
Stepan Salenikovichde896112015-05-11 16:46:33 -040074 G_OBJECT_CLASS(general_settings_view_parent_class)->dispose(object);
75}
76
77static void
78history_limit_changed(GtkAdjustment *adjustment, G_GNUC_UNUSED gpointer user_data)
79{
80 int limit = (int)gtk_adjustment_get_value(GTK_ADJUSTMENT(adjustment));
81 CategorizedHistoryModel::instance()->setHistoryLimit(limit);
82}
83
84static gboolean
85clear_history_dialog(GeneralSettingsView *self)
86{
87 gboolean response = FALSE;
88 GtkWidget *dialog = gtk_message_dialog_new(NULL,
89 (GtkDialogFlags)(GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT),
90 GTK_MESSAGE_QUESTION, GTK_BUTTONS_OK_CANCEL,
91 "Are you sure you want to clear all your history?\n" \
92 "This operation will also reset the Frequen Contacts list");
93
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) )
123 CategorizedHistoryModel::instance()->clearAllCollections();
124}
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),
148 CategorizedHistoryModel::instance()->historyLimit());
149 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}