-
Notifications
You must be signed in to change notification settings - Fork 263
/
lo_prefs.c
131 lines (100 loc) · 3.67 KB
/
lo_prefs.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
* lo_prefs.c - Line operations, remove duplicate lines, empty lines,
* lines with only whitespace, sort lines.
*
* Copyright 2015 Sylvan Mostert <smostert.dev@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifdef HAVE_CONFIG_H
#include "config.h" /* for the gettext domain */
#endif
#include "lo_prefs.h"
static struct
{
GtkWidget *collation_cb;
} config_widgets;
LineOpsInfo *lo_info = NULL;
/* handle button presses in the preferences dialog box */
void
lo_configure_response_cb(GtkDialog *dialog, gint response, gpointer user_data)
{
if (response == GTK_RESPONSE_OK || response == GTK_RESPONSE_APPLY)
{
GKeyFile *config = g_key_file_new();
gchar *config_dir = g_path_get_dirname(lo_info->config_file);
gchar *data;
/* Grabbing options that has been set */
lo_info->use_collation_compare =
gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(config_widgets.collation_cb));
/* Write preference to file */
g_key_file_load_from_file(config, lo_info->config_file, G_KEY_FILE_NONE, NULL);
g_key_file_set_boolean(config, "general", "use_collation_compare",
lo_info->use_collation_compare);
if (!g_file_test(config_dir, G_FILE_TEST_IS_DIR)
&& utils_mkdir(config_dir, TRUE) != 0)
{
dialogs_show_msgbox(GTK_MESSAGE_ERROR,
_("Plugin configuration directory could not be created."));
}
else
{
/* write config to file */
data = g_key_file_to_data(config, NULL, NULL);
utils_write_file(lo_info->config_file, data);
g_free(data);
}
g_free(config_dir);
g_key_file_free(config);
}
}
/* Configure the preferences GUI and callbacks */
GtkWidget *
lo_configure(G_GNUC_UNUSED GeanyPlugin *plugin, GtkDialog *dialog, G_GNUC_UNUSED gpointer pdata)
{
GtkWidget *vbox;
vbox = gtk_vbox_new(FALSE, 0);
config_widgets.collation_cb = gtk_check_button_new_with_label(
_("Use collation based string compare"));
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(config_widgets.collation_cb),
lo_info->use_collation_compare);
gtk_box_pack_start(GTK_BOX(vbox), config_widgets.collation_cb, FALSE, FALSE, 2);
gtk_widget_show_all(vbox);
g_signal_connect(dialog, "response", G_CALLBACK(lo_configure_response_cb), NULL);
return vbox;
}
/* Initialize preferences */
void
lo_init_prefs(GeanyPlugin *plugin)
{
GeanyData *geany_data = plugin->geany_data;
GKeyFile *config = g_key_file_new();
/* load preferences from file into lo_info */
lo_info = g_new0(LineOpsInfo, 1);
lo_info->config_file = g_strconcat(geany->app->configdir,
G_DIR_SEPARATOR_S, "plugins", G_DIR_SEPARATOR_S,
"lineoperations", G_DIR_SEPARATOR_S, "general.conf", NULL);
g_key_file_load_from_file(config, lo_info->config_file, G_KEY_FILE_NONE, NULL);
lo_info->use_collation_compare = utils_get_setting_boolean(config,
"general", "use_collation_compare", FALSE);
printf("VALUE: %d\n", lo_info->use_collation_compare);
g_key_file_free(config);
}
/* Free config */
void
lo_free_info() {
g_free(lo_info->config_file);
g_free(lo_info);
}