callbacks.c 9.5 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */

/*
 *  GThumb
 *
 *  Copyright (C) 2009 Free Software Foundation, Inc.
 *
 *  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
19
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
21
22
23
24
25
26
27
 */


#include <config.h>
#include <glib/gi18n.h>
#include <glib-object.h>
#include <gthumb.h>
#include "actions.h"
Paolo Bacchilega's avatar
Paolo Bacchilega committed
28
#include "callbacks.h"
29
30
31
32
33


#define BROWSER_DATA_KEY "bookmarks-browser-data"


34
35
36
static const GActionEntry actions[] = {
	{ "bookmarks-add", gth_browser_activate_bookmarks_add },
	{ "bookmarks-edit", gth_browser_activate_bookmarks_edit }
37
38
39
40
};


typedef struct {
41
42
43
44
45
46
	GthBrowser *browser;
	guint       bookmarks_changed_id;
	guint       entry_points_changed_id;
	GMenu      *system_bookmarks_menu;
	GMenu      *entry_points_menu;
	GMenu      *bookmarks_menu;
47
48
49
50
51
52
53
54
55
56
57
} BrowserData;


static void
browser_data_free (BrowserData *data)
{
	if (data->bookmarks_changed_id != 0) {
		g_signal_handler_disconnect (gth_main_get_default_monitor (),
					     data->bookmarks_changed_id);
		data->bookmarks_changed_id = 0;
	}
58
59
60
61
62
	if (data->entry_points_changed_id != 0) {
		g_signal_handler_disconnect (gth_main_get_default_monitor (),
					     data->entry_points_changed_id);
		data->entry_points_changed_id = 0;
	}
63
64
65
	_g_object_unref (data->system_bookmarks_menu);
	_g_object_unref (data->entry_points_menu);
	_g_object_unref (data->bookmarks_menu);
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
	g_free (data);
}


#define BUFFER_SIZE 4096


typedef struct {
	GthBrowser   *browser;
	GInputStream *stream;
	char          buffer[BUFFER_SIZE];
	GString      *file_content;
} UpdateBookmarksData;


static void
update_bookmakrs_data_free (UpdateBookmarksData *data)
{
	g_input_stream_close (data->stream, NULL, NULL);
	g_object_unref (data->stream);
	g_string_free (data->file_content, TRUE);
	g_object_unref (data->browser);
	g_free (data);
}


static void
update_system_bookmark_list_from_content (GthBrowser *browser,
					  const char *content)
{
96
97
98
	BrowserData   *data;
	char        **lines;
	int           i;
99

100
101
	data = g_object_get_data (G_OBJECT (browser), BROWSER_DATA_KEY);
	g_return_if_fail (data != NULL);
102
103
104

	lines = g_strsplit (content, "\n", -1);
	for (i = 0; lines[i] != NULL; i++) {
105
106
107
108
109
		char      **line;
		char       *uri;
		GFile      *file;
		char       *name;
		GMenuItem  *item;
110
111
112

		line = g_strsplit (lines[i], " ", 2);
		uri = line[0];
113
114
		if (uri == NULL) {
			g_strfreev (line);
115
			continue;
116
		}
117
118
119
120
121
122
123

		file = g_file_new_for_uri (uri);
		name = g_strdup (strchr (lines[i], ' '));
		if (name == NULL)
			name = _g_file_get_display_name (file);
		if (name == NULL)
			name = g_file_get_parse_name (file);
124
125
126
		item = _g_menu_item_new_for_file (file, name);
		g_menu_item_set_action_and_target (item, "win.go-to-location", "s", uri);
		g_menu_append_item (data->system_bookmarks_menu, item);
127

Paolo Bacchilega's avatar
Paolo Bacchilega committed
128
		g_object_unref (item);
129
130
131
132
133
		g_free (name);
		g_object_unref (file);
		g_strfreev (line);
	}

134
	g_strfreev (lines);
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
}


static void
update_system_bookmark_list_ready (GObject      *source_object,
				   GAsyncResult *result,
				   gpointer      user_data)
{
	UpdateBookmarksData *data = user_data;
	gssize               size;

	size = g_input_stream_read_finish (data->stream, result, NULL);
	if (size < 0) {
		update_bookmakrs_data_free (data);
		return;
	}

	if (size > 0) {
		data->buffer[size + 1] = '\0';
		g_string_append (data->file_content, data->buffer);

		g_input_stream_read_async (data->stream,
				   	   data->buffer,
				   	   BUFFER_SIZE - 1,
					   G_PRIORITY_DEFAULT,
					   NULL,
					   update_system_bookmark_list_ready,
					   data);
		return;
	}

	update_system_bookmark_list_from_content (data->browser, data->file_content->str);
	update_bookmakrs_data_free (data);
}


static void
_gth_browser_update_system_bookmark_list (GthBrowser *browser)
{
174
	BrowserData         *browser_data;
175
176
177
178
	GFile               *bookmark_file;
	GFileInputStream    *input_stream;
	UpdateBookmarksData *data;

179
180
181
182
183
	browser_data = g_object_get_data (G_OBJECT (browser), BROWSER_DATA_KEY);
	g_return_if_fail (browser_data != NULL);

	g_menu_remove_all (browser_data->system_bookmarks_menu);

184
185
	/* give priority to XDG_CONFIG_HOME/gtk-3.0/bookmarks if not found
	 * try the old ~/.gtk-bookmarks */
186

187
	bookmark_file = gth_user_dir_get_file_for_read (GTH_DIR_CONFIG, "gtk-3.0", "bookmarks", NULL);
188
	if (! g_file_query_exists (bookmark_file, NULL)) {
189
190
		char *path;

191
		g_object_unref (bookmark_file);
192
193
194
195
		path = g_build_filename (g_get_home_dir (), ".gtk-bookmarks", NULL);
		bookmark_file = g_file_new_for_path (path);

		g_free (path);
196
	}
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221

	input_stream = g_file_read (bookmark_file, NULL, NULL);
	g_object_unref (bookmark_file);

	if (input_stream == NULL)
		return;

	data = g_new0 (UpdateBookmarksData, 1);
	data->browser = g_object_ref (browser);
	data->stream = (GInputStream*) input_stream;
	data->file_content = g_string_new ("");

	g_input_stream_read_async (data->stream,
				   data->buffer,
				   BUFFER_SIZE - 1,
				   G_PRIORITY_DEFAULT,
				   NULL,
				   update_system_bookmark_list_ready,
				   data);
}


static void
_gth_browser_update_bookmark_list (GthBrowser *browser)
{
222
	BrowserData    *data;
223
224
225
226
	GBookmarkFile  *bookmarks;
	char          **uris;
	int             i;

227
228
	data = g_object_get_data (G_OBJECT (browser), BROWSER_DATA_KEY);
	g_return_if_fail (data != NULL);
229

230
	g_menu_remove_all (data->bookmarks_menu);
231
232

	bookmarks = gth_main_get_default_bookmarks ();
233
	uris = g_bookmark_file_get_uris (bookmarks, NULL);
234
235

	for (i = 0; uris[i] != NULL; i++) {
236
237
238
		GFile     *file;
		char      *name;
		GMenuItem *item;
239
240

		file = g_file_new_for_uri (uris[i]);
241
		name = g_bookmark_file_get_title (bookmarks, uris[i], NULL);
242
243
244
		item = _g_menu_item_new_for_file (file, name);
		g_menu_item_set_action_and_target (item, "win.go-to-location", "s", uris[i]);
		g_menu_append_item (data->bookmarks_menu, item);
245

Paolo Bacchilega's avatar
Paolo Bacchilega committed
246
		g_object_unref (item);
247
		g_free (name);
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
		g_object_unref (file);
	}

	_gth_browser_update_system_bookmark_list (browser);

	g_strfreev (uris);
}


static void
bookmarks_changed_cb (GthMonitor *monitor,
		      gpointer    user_data)
{
	BrowserData *data = user_data;
	_gth_browser_update_bookmark_list (data->browser);
}


266
267
268
static void
_gth_browser_update_entry_point_list (GthBrowser *browser)
{
269
	BrowserData  *data;
270
271
272
	GList        *entry_points;
	GList        *scan;

273
274
275
276
	data = g_object_get_data (G_OBJECT (browser), BROWSER_DATA_KEY);
	g_return_if_fail (data != NULL);

	g_menu_remove_all (data->entry_points_menu);
277
278
279
280

	entry_points = gth_main_get_all_entry_points ();
	for (scan = entry_points; scan; scan = scan->next) {
		GthFileData *file_data = scan->data;
281
282
		GMenuItem   *item;
		char        *uri;
283

284
285
286
287
288
289
290
		item = _g_menu_item_new_for_file (file_data->file, NULL);
		uri = g_file_get_uri (file_data->file);
		g_menu_item_set_action_and_target (item, "win.go-to-location", "s", uri);
		g_menu_append_item (data->entry_points_menu, item);

		g_free (uri);
		g_object_unref (item);
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
	}

	_g_object_list_unref (entry_points);
}


static void
entry_points_changed_cb (GthMonitor *monitor,
			 gpointer    user_data)
{
	BrowserData *data = user_data;
	call_when_idle ((DataFunc) _gth_browser_update_entry_point_list, data->browser);
}


306
307
308
309
310
311
312
313
void
bookmarks__gth_browser_construct_cb (GthBrowser *browser)
{
	BrowserData *data;

	g_return_if_fail (GTH_IS_BROWSER (browser));

	data = g_new0 (BrowserData, 1);
314
315
	g_object_set_data_full (G_OBJECT (browser), BROWSER_DATA_KEY, data, (GDestroyNotify) browser_data_free);

316
317
318
319
320
321
322
323
	g_action_map_add_action_entries (G_ACTION_MAP (browser),
					 actions,
					 G_N_ELEMENTS (actions),
					 browser);

	{
		GtkWidget  *button;
		GtkBuilder *builder;
324
		GMenuModel *menu;
325
326
327

		button = _gtk_menu_button_new_for_header_bar ();
		gtk_widget_set_tooltip_text (button, _("Bookmarks"));
328
		gtk_container_add (GTK_CONTAINER (button), gtk_image_new_from_icon_name ("user-bookmarks-symbolic", GTK_ICON_SIZE_MENU));
329

330
331
332
333
334
		builder = gtk_builder_new_from_resource ("/org/gnome/gThumb/bookmarks/data/ui/bookmarks-menu.ui");
		data->system_bookmarks_menu = G_MENU (gtk_builder_get_object (builder, "system-bookmarks"));
		data->entry_points_menu = G_MENU (gtk_builder_get_object (builder, "entry-points"));
		data->bookmarks_menu = G_MENU (gtk_builder_get_object (builder, "bookmarks"));

335
336
337
		menu = G_MENU_MODEL (gtk_builder_get_object (builder, "bookmarks-menu"));
		gtk_menu_button_set_menu_model (GTK_MENU_BUTTON (button), menu);
		_gtk_window_add_accelerators_from_menu ((GTK_WINDOW (browser)), menu);
338
339

		gtk_widget_show_all (button);
340
		gtk_box_pack_start (GTK_BOX (gth_browser_get_headerbar_section (browser, GTH_BROWSER_HEADER_SECTION_BROWSER_LOCATIONS)), button, FALSE, FALSE, 0);
341
342

		g_object_unref (builder);
343
344
	}

345
	data->browser = browser;
346
347
348
349
	data->bookmarks_changed_id = g_signal_connect (gth_main_get_default_monitor (),
						       "bookmarks-changed",
						       G_CALLBACK (bookmarks_changed_cb),
						       data);
350
351
352
353
	data->entry_points_changed_id = g_signal_connect (gth_main_get_default_monitor (),
							  "entry-points-changed",
							  G_CALLBACK (entry_points_changed_cb),
							  data);
354
355
356
357
358
359
360
}


void
bookmarks__gth_browser_construct_idle_callback_cb (GthBrowser *browser)
{
	_gth_browser_update_bookmark_list (browser);
361
	_gth_browser_update_entry_point_list (browser);
362
}