1 /*
2 * overviewplugin.c - This file is part of the Geany Overview plugin
3 *
4 * Copyright (c) 2015 Matthew Brush <mbrush@codebrainz.ca>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19 * MA 02110-1301, USA.
20 *
21 */
22
23 #ifdef HAVE_CONFIG_H
24 # include "config.h"
25 #endif
26
27 #include "overviewplugin.h"
28 #include "overviewscintilla.h"
29 #include "overviewprefs.h"
30 #include "overviewprefspanel.h"
31 #include "overviewui.h"
32 #include <errno.h>
33
34 GeanyPlugin *geany_plugin;
35 GeanyData *geany_data;
36
37 PLUGIN_VERSION_CHECK (224)
38
39 PLUGIN_SET_TRANSLATABLE_INFO (
40 LOCALEDIR,
41 GETTEXT_PACKAGE,
42 "Overview",
43 _("Provides an overview of the active document"),
44 "0.01",
45 "Matthew Brush <matt@geany.org>")
46
47 static OverviewPrefs *overview_prefs = NULL;
48
49 static void write_config (void);
50
51 static void
on_visible_pref_notify(OverviewPrefs * prefs,GParamSpec * pspec,gpointer user_data)52 on_visible_pref_notify (OverviewPrefs *prefs,
53 GParamSpec *pspec,
54 gpointer user_data)
55 {
56 write_config ();
57 }
58
59 static void
toggle_visibility(void)60 toggle_visibility (void)
61 {
62 gboolean visible = TRUE;
63 g_object_get (overview_prefs, "visible", &visible, NULL);
64 g_object_set (overview_prefs, "visible", !visible, NULL);
65 }
66
67 enum
68 {
69 KB_TOGGLE_VISIBLE,
70 KB_TOGGLE_POSITION,
71 KB_TOGGLE_INVERTED,
72 NUM_KB
73 };
74
75 static gboolean
on_kb_activate(guint keybinding_id)76 on_kb_activate (guint keybinding_id)
77 {
78 switch (keybinding_id)
79 {
80 case KB_TOGGLE_VISIBLE:
81 toggle_visibility ();
82 break;
83 case KB_TOGGLE_POSITION:
84 {
85 GtkPositionType pos;
86 g_object_get (overview_prefs, "position", &pos, NULL);
87 pos = (pos == GTK_POS_LEFT) ? GTK_POS_RIGHT : GTK_POS_LEFT;
88 g_object_set (overview_prefs, "position", pos, NULL);
89 break;
90 }
91 case KB_TOGGLE_INVERTED:
92 {
93 gboolean inv = FALSE;
94 g_object_get (overview_prefs, "overlay-inverted", &inv, NULL);
95 g_object_set (overview_prefs, "overlay-inverted", !inv, NULL);
96 break;
97 }
98 default:
99 break;
100 }
101 return TRUE;
102 }
103
104 static gchar *
get_config_file(void)105 get_config_file (void)
106 {
107 gchar *dir;
108 gchar *fn;
109 static const gchar *def_config = OVERVIEW_PREFS_DEFAULT_CONFIG;
110
111 dir = g_build_filename (geany_data->app->configdir, "plugins", "overview", NULL);
112 fn = g_build_filename (dir, "prefs.conf", NULL);
113
114 if (! g_file_test (fn, G_FILE_TEST_IS_DIR))
115 {
116 if (g_mkdir_with_parents (dir, 0755) != 0)
117 {
118 g_critical ("failed to create config dir '%s': %s", dir, g_strerror (errno));
119 g_free (dir);
120 g_free (fn);
121 return NULL;
122 }
123 }
124
125 g_free (dir);
126
127 if (! g_file_test (fn, G_FILE_TEST_EXISTS))
128 {
129 GError *error = NULL;
130 if (!g_file_set_contents (fn, def_config, -1, &error))
131 {
132 g_critical ("failed to save default config to file '%s': %s",
133 fn, error->message);
134 g_error_free (error);
135 g_free (fn);
136 return NULL;
137 }
138 }
139
140 return fn;
141 }
142
143 static void
write_config(void)144 write_config (void)
145 {
146 gchar *conf_file;
147 GError *error = NULL;
148 conf_file = get_config_file ();
149 if (! overview_prefs_save (overview_prefs, conf_file, &error))
150 {
151 g_critical ("failed to save preferences to file '%s': %s", conf_file, error->message);
152 g_error_free (error);
153 }
154 g_free (conf_file);
155 }
156
157 void
plugin_init(G_GNUC_UNUSED GeanyData * data)158 plugin_init (G_GNUC_UNUSED GeanyData *data)
159 {
160 gchar *conf_file;
161 GError *error = NULL;
162 GeanyKeyGroup *key_group;
163
164 plugin_module_make_resident (geany_plugin);
165
166 overview_prefs = overview_prefs_new ();
167 conf_file = get_config_file ();
168 if (! overview_prefs_load (overview_prefs, conf_file, &error))
169 {
170 g_critical ("failed to load preferences file '%s': %s", conf_file, error->message);
171 g_error_free (error);
172 }
173 g_free (conf_file);
174
175 overview_ui_init (overview_prefs);
176
177 key_group = plugin_set_key_group (geany_plugin,
178 "overview",
179 NUM_KB,
180 on_kb_activate);
181
182 keybindings_set_item (key_group,
183 KB_TOGGLE_VISIBLE,
184 NULL, 0, 0,
185 "toggle-visibility",
186 _("Toggle Visibility"),
187 overview_ui_get_menu_item ());
188
189 keybindings_set_item (key_group,
190 KB_TOGGLE_POSITION,
191 NULL, 0, 0,
192 "toggle-position",
193 _("Toggle Left/Right Position"),
194 NULL);
195
196 keybindings_set_item (key_group,
197 KB_TOGGLE_INVERTED,
198 NULL, 0, 0,
199 "toggle-inverted",
200 _("Toggle Overlay Inversion"),
201 NULL);
202
203 g_signal_connect (overview_prefs, "notify::visible", G_CALLBACK (on_visible_pref_notify), NULL);
204 }
205
206 void
plugin_cleanup(void)207 plugin_cleanup (void)
208 {
209 write_config ();
210 overview_ui_deinit ();
211
212 if (OVERVIEW_IS_PREFS (overview_prefs))
213 g_object_unref (overview_prefs);
214 overview_prefs = NULL;
215 }
216
217 static void
on_prefs_stored(OverviewPrefsPanel * panel,OverviewPrefs * prefs,gpointer user_data)218 on_prefs_stored (OverviewPrefsPanel *panel,
219 OverviewPrefs *prefs,
220 gpointer user_data)
221 {
222 write_config ();
223 overview_ui_queue_update ();
224 }
225
226 GtkWidget *
plugin_configure(GtkDialog * dialog)227 plugin_configure (GtkDialog *dialog)
228 {
229 GtkWidget *panel;
230 panel = overview_prefs_panel_new (overview_prefs, dialog);
231 g_signal_connect (panel, "prefs-stored", G_CALLBACK (on_prefs_stored), NULL);
232 return panel;
233 }
234