Stepan Salenikovich | de89611 | 2015-05-11 16:46:33 -0400 | [diff] [blame] | 1 | /* |
| 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 Salenikovich | fb7f295 | 2015-05-25 16:44:19 -0400 | [diff] [blame] | 35 | #include "utils/files.h" |
Stepan Salenikovich | de89611 | 2015-05-11 16:46:33 -0400 | [diff] [blame] | 36 | |
| 37 | struct _GeneralSettingsView |
| 38 | { |
| 39 | GtkBox parent; |
| 40 | }; |
| 41 | |
| 42 | struct _GeneralSettingsViewClass |
| 43 | { |
| 44 | GtkBoxClass parent_class; |
| 45 | }; |
| 46 | |
| 47 | typedef struct _GeneralSettingsViewPrivate GeneralSettingsViewPrivate; |
| 48 | |
| 49 | struct _GeneralSettingsViewPrivate |
| 50 | { |
Stepan Salenikovich | fb7f295 | 2015-05-25 16:44:19 -0400 | [diff] [blame] | 51 | GSettings *settings; |
| 52 | |
| 53 | /* Rint settings */ |
| 54 | GtkWidget *checkbutton_autostart; |
Stepan Salenikovich | bc6c4be | 2015-07-31 16:07:54 -0400 | [diff] [blame] | 55 | GtkWidget *checkbutton_hideonclose; |
| 56 | GtkWidget *checkbutton_bringtofront; |
Stepan Salenikovich | fb7f295 | 2015-05-25 16:44:19 -0400 | [diff] [blame] | 57 | |
Stepan Salenikovich | de89611 | 2015-05-11 16:46:33 -0400 | [diff] [blame] | 58 | /* history settings */ |
| 59 | GtkWidget *adjustment_history_duration; |
| 60 | GtkWidget *button_clear_history; |
| 61 | }; |
| 62 | |
| 63 | G_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 | |
| 67 | static void |
| 68 | general_settings_view_dispose(GObject *object) |
| 69 | { |
Stepan Salenikovich | fb7f295 | 2015-05-25 16:44:19 -0400 | [diff] [blame] | 70 | GeneralSettingsViewPrivate *priv = GENERAL_SETTINGS_VIEW_GET_PRIVATE(object); |
| 71 | |
| 72 | g_clear_object(&priv->settings); |
| 73 | |
Stepan Salenikovich | de89611 | 2015-05-11 16:46:33 -0400 | [diff] [blame] | 74 | G_OBJECT_CLASS(general_settings_view_parent_class)->dispose(object); |
| 75 | } |
| 76 | |
| 77 | static void |
| 78 | history_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 | |
| 84 | static gboolean |
| 85 | clear_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 | |
| 117 | static void |
| 118 | clear_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 | |
| 126 | static void |
| 127 | general_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 Salenikovich | fb7f295 | 2015-05-25 16:44:19 -0400 | [diff] [blame] | 133 | priv->settings = g_settings_new_full(get_ring_schema(), NULL, NULL); |
| 134 | |
Stepan Salenikovich | bc6c4be | 2015-07-31 16:07:54 -0400 | [diff] [blame] | 135 | /* bind client option to gsettings */ |
Stepan Salenikovich | fb7f295 | 2015-05-25 16:44:19 -0400 | [diff] [blame] | 136 | g_settings_bind(priv->settings, "start-on-login", |
| 137 | priv->checkbutton_autostart, "active", |
| 138 | G_SETTINGS_BIND_DEFAULT); |
Stepan Salenikovich | bc6c4be | 2015-07-31 16:07:54 -0400 | [diff] [blame] | 139 | 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 Salenikovich | fb7f295 | 2015-05-25 16:44:19 -0400 | [diff] [blame] | 145 | |
Stepan Salenikovich | de89611 | 2015-05-11 16:46:33 -0400 | [diff] [blame] | 146 | /* 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 | |
| 156 | static void |
| 157 | general_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 Salenikovich | fb7f295 | 2015-05-25 16:44:19 -0400 | [diff] [blame] | 164 | gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), GeneralSettingsView, checkbutton_autostart); |
Stepan Salenikovich | bc6c4be | 2015-07-31 16:07:54 -0400 | [diff] [blame] | 165 | 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 Salenikovich | de89611 | 2015-05-11 16:46:33 -0400 | [diff] [blame] | 167 | 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 | |
| 171 | GtkWidget * |
| 172 | general_settings_view_new() |
| 173 | { |
| 174 | gpointer view = g_object_new(GENERAL_SETTINGS_VIEW_TYPE, NULL); |
| 175 | |
| 176 | return (GtkWidget *)view; |
| 177 | } |