callbacks.c 9.39 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
	GthBrowser *browser;
Paolo Bacchilega's avatar
Paolo Bacchilega committed
42
	GtkBuilder *builder;
43
44
45
46
47
	guint       bookmarks_changed_id;
	guint       entry_points_changed_id;
	GMenu      *system_bookmarks_menu;
	GMenu      *entry_points_menu;
	GMenu      *bookmarks_menu;
48
49
50
51
52
53
54
55
56
57
58
} 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;
	}
59
60
61
62
63
	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;
	}
Paolo Bacchilega's avatar
Paolo Bacchilega committed
64
	_g_object_unref (data->builder);
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
	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)
{
95
96
97
	BrowserData   *data;
	char        **lines;
	int           i;
98

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

	lines = g_strsplit (content, "\n", -1);
	for (i = 0; lines[i] != NULL; i++) {
104
105
106
		char      **line;
		char       *uri;
		GFile      *file;
107
		char       *first_space;
108
109
		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

		file = g_file_new_for_uri (uri);
119
120
		first_space = strchr (lines[i], ' ');
		name = (first_space != NULL) ? g_strdup (first_space + 1) : NULL;
121
122
123
124
		if (name == NULL)
			name = _g_file_get_display_name (file);
		if (name == NULL)
			name = g_file_get_parse_name (file);
125
126
127
		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);
128

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

135
	g_strfreev (lines);
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
174
}


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)
{
175
	BrowserData         *browser_data;
176
177
178
179
	GFile               *bookmark_file;
	GFileInputStream    *input_stream;
	UpdateBookmarksData *data;

180
181
182
183
184
	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);

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

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

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

		g_free (path);
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
222

	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)
{
223
	BrowserData    *data;
224
225
226
227
	GBookmarkFile  *bookmarks;
	char          **uris;
	int             i;

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

231
	g_menu_remove_all (data->bookmarks_menu);
232
233

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

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

		file = g_file_new_for_uri (uris[i]);
242
		name = g_bookmark_file_get_title (bookmarks, uris[i], NULL);
243
244
245
		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);
246

Paolo Bacchilega's avatar
Paolo Bacchilega committed
247
		g_object_unref (item);
248
		g_free (name);
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
		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);
}


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

274
275
276
277
	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);
278
279
280
281

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

285
286
287
288
289
290
291
		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);
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
	}

	_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);
}


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

	g_return_if_fail (GTH_IS_BROWSER (browser));

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

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;
324
		GMenuModel *menu;
325

326
		button = _gtk_menu_button_new_for_header_bar ("user-bookmarks-symbolic");
327
		gtk_widget_set_tooltip_text (button, _("Bookmarks"));
328

Paolo Bacchilega's avatar
Paolo Bacchilega committed
329
330
331
332
		data->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 (data->builder, "system-bookmarks"));
		data->entry_points_menu = G_MENU (gtk_builder_get_object (data->builder, "entry-points"));
		data->bookmarks_menu = G_MENU (gtk_builder_get_object (data->builder, "bookmarks"));
333

Paolo Bacchilega's avatar
Paolo Bacchilega committed
334
		menu = G_MENU_MODEL (gtk_builder_get_object (data->builder, "bookmarks-menu"));
335
336
		gtk_menu_button_set_menu_model (GTK_MENU_BUTTON (button), menu);
		_gtk_window_add_accelerators_from_menu ((GTK_WINDOW (browser)), menu);
337

338
		gtk_widget_show (button);
339
		gtk_box_pack_end (GTK_BOX (gth_browser_get_headerbar_section (browser, GTH_BROWSER_HEADER_SECTION_BROWSER_LOCATIONS)), button, FALSE, FALSE, 0);
340
341
	}

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


void
bookmarks__gth_browser_construct_idle_callback_cb (GthBrowser *browser)
{
	_gth_browser_update_bookmark_list (browser);
358
	_gth_browser_update_entry_point_list (browser);
359
}