callbacks.c 15.6 KB
Newer Older
1
2
3
4
5
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */

/*
 *  GThumb
 *
6
 *  Copyright (C) 2009-2010 Free Software Foundation, Inc.
7
8
9
10
11
12
13
14
15
16
17
18
 *
 *  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"
28
29
30
#include "callbacks.h"
#include "gth-slideshow-preferences.h"
#include "preferences.h"
31
32
33
34
35
36
37
38
39
40
41
42
43
44


#define BROWSER_DATA_KEY "slideshow-browser-data"


static const char *ui_info =
"<ui>"
"  <menubar name='MenuBar'>"
"    <menu name='View' action='ViewMenu'>"
"      <placeholder name='View_Actions'>"
"        <menuitem action='View_Slideshow'/>"
"      </placeholder>"
"    </menu>"
"  </menubar>"
45
46
47
48
49
50
"  <toolbar name='ToolBar'>"
"      <placeholder name='BrowserCommands'>"
"        <toolitem action='View_Fullscreen'/>"
"        <toolitem action='View_Slideshow'/>"
"      </placeholder>"
"  </toolbar>"
51
52
53
54
"</ui>";


static GtkActionEntry action_entries[] = {
55
	{ "View_Slideshow", "x-office-presentation",
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
	  "_Slideshow", "F5",
	  N_("View as a slideshow"),
	  G_CALLBACK (gth_browser_activate_action_view_slideshow) }
};


typedef struct {
	GtkActionGroup *action_group;
	guint           actions_merge_id;
} BrowserData;


static void
browser_data_free (BrowserData *data)
{
	g_free (data);
}


void
ss__gth_browser_construct_cb (GthBrowser *browser)
{
	BrowserData *data;
	GError      *error = NULL;

	g_return_if_fail (GTH_IS_BROWSER (browser));

	data = g_new0 (BrowserData, 1);

	data->action_group = gtk_action_group_new ("Slideshow Action");
	gtk_action_group_set_translation_domain (data->action_group, NULL);
	gtk_action_group_add_actions (data->action_group,
				      action_entries,
				      G_N_ELEMENTS (action_entries),
				      browser);
	gtk_ui_manager_insert_action_group (gth_browser_get_ui_manager (browser), data->action_group, 0);

	data->actions_merge_id = gtk_ui_manager_add_ui_from_string (gth_browser_get_ui_manager (browser), ui_info, -1, &error);
	if (data->actions_merge_id == 0) {
		g_warning ("building menus failed: %s", error->message);
		g_error_free (error);
	}

	g_object_set_data_full (G_OBJECT (browser), BROWSER_DATA_KEY, data, (GDestroyNotify) browser_data_free);
}


static void
set_action_sensitive (BrowserData *data,
		      const char  *action_name,
		      gboolean     sensitive)
{
	GtkAction *action;

	action = gtk_action_group_get_action (data->action_group, action_name);
	g_object_set (action, "sensitive", sensitive, NULL);
}


void
ss__gth_browser_update_sensitivity_cb (GthBrowser *browser)
{
	BrowserData  *data;
	GtkTreeModel *file_store;
	gboolean      sensitive;

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

	file_store = gth_file_view_get_model (GTH_FILE_VIEW (gth_browser_get_file_list_view (browser)));
	sensitive = (gth_file_store_n_visibles (GTH_FILE_STORE (file_store)) > 0);
	set_action_sensitive (data, "View_Slideshow", sensitive);
}
129
130
131
132
133
134
135


void
ss__slideshow_cb (GthBrowser *browser)
{
	gth_browser_activate_action_view_slideshow (NULL, browser);
}
136
137
138
139
140
141
142
143
144
145


void
ss__gth_catalog_read_metadata (GthCatalog  *catalog,
			       GthFileData *file_data)
{
	if (g_file_info_get_attribute_status (file_data->info, "slideshow::personalize") == G_FILE_ATTRIBUTE_STATUS_SET)
		g_value_hash_set_boolean (catalog->attributes,
					  "slideshow::personalize",
					  g_file_info_get_attribute_boolean (file_data->info, "slideshow::personalize"));
146
	if (g_file_info_get_attribute_status (file_data->info, "slideshow::automatic") == G_FILE_ATTRIBUTE_STATUS_SET)
147
		g_value_hash_set_boolean (catalog->attributes,
148
149
150
					  "slideshow::automatic",
					  g_file_info_get_attribute_boolean (file_data->info, "slideshow::automatic"));
	if (g_file_info_get_attribute_status (file_data->info, "slideshow::wrap-around") == G_FILE_ATTRIBUTE_STATUS_SET)
151
		g_value_hash_set_boolean (catalog->attributes,
152
153
					  "slideshow::wrap-around",
					  g_file_info_get_attribute_boolean (file_data->info, "slideshow::wrap-around"));
154
	if (g_file_info_get_attribute_status (file_data->info, "slideshow::delay") == G_FILE_ATTRIBUTE_STATUS_SET)
155
156
157
		g_value_hash_set_int (catalog->attributes,
				      "slideshow::delay",
				      g_file_info_get_attribute_int32 (file_data->info, "slideshow::delay"));
158
159
160
161
	if (g_file_info_get_attribute_status (file_data->info, "slideshow::transition") == G_FILE_ATTRIBUTE_STATUS_SET)
		g_value_hash_set_string (catalog->attributes,
					 "slideshow::transition",
				 	 g_file_info_get_attribute_string (file_data->info, "slideshow::transition"));
162
	if (g_file_info_get_attribute_status (file_data->info, "slideshow::playlist") == G_FILE_ATTRIBUTE_STATUS_SET)
163
		g_value_hash_set_stringv (catalog->attributes,
164
165
					  "slideshow::playlist",
					  g_file_info_get_attribute_stringv (file_data->info, "slideshow::playlist"));
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
}


void
ss__gth_catalog_write_metadata (GthCatalog  *catalog,
			        GthFileData *file_data)
{
	if (g_value_hash_is_set (catalog->attributes, "slideshow::personalize")) {
		g_file_info_set_attribute_boolean (file_data->info,
						   "slideshow::personalize",
						   g_value_hash_get_boolean (catalog->attributes, "slideshow::personalize"));
		g_file_info_set_attribute_status (file_data->info,
						  "slideshow::personalize",
						  G_FILE_ATTRIBUTE_STATUS_SET);
	}
181
	if (g_value_hash_is_set (catalog->attributes, "slideshow::automatic")) {
182
		g_file_info_set_attribute_boolean (file_data->info,
183
184
						   "slideshow::automatic",
						   g_value_hash_get_boolean (catalog->attributes, "slideshow::automatic"));
185
		g_file_info_set_attribute_status (file_data->info,
186
						  "slideshow::automatic",
187
188
						  G_FILE_ATTRIBUTE_STATUS_SET);
	}
189
	if (g_value_hash_is_set (catalog->attributes, "slideshow::wrap-around")) {
190
		g_file_info_set_attribute_boolean (file_data->info,
191
192
						   "slideshow::wrap-around",
						   g_value_hash_get_boolean (catalog->attributes, "slideshow::wrap-around"));
193
		g_file_info_set_attribute_status (file_data->info,
194
						  "slideshow::wrap-around",
195
196
197
198
199
						  G_FILE_ATTRIBUTE_STATUS_SET);
	}
	if (g_value_hash_is_set (catalog->attributes, "slideshow::delay")) {
		g_file_info_set_attribute_int32 (file_data->info,
						 "slideshow::delay",
200
						 g_value_hash_get_int (catalog->attributes, "slideshow::delay"));
201
202
203
204
205
206
207
208
209
210
211
212
		g_file_info_set_attribute_status (file_data->info,
						  "slideshow::delay",
						  G_FILE_ATTRIBUTE_STATUS_SET);
	}
	if (g_value_hash_is_set (catalog->attributes, "slideshow::transition")) {
		g_file_info_set_attribute_string (file_data->info,
						  "slideshow::transition",
						  g_value_hash_get_string (catalog->attributes, "slideshow::transition"));
		g_file_info_set_attribute_status (file_data->info,
						  "slideshow::transition",
						  G_FILE_ATTRIBUTE_STATUS_SET);
	}
213
	if (g_value_hash_is_set (catalog->attributes, "slideshow::playlist")) {
214
		g_file_info_set_attribute_stringv (file_data->info,
215
216
						   "slideshow::playlist",
						   g_value_hash_get_stringv (catalog->attributes, "slideshow::playlist"));
217
		g_file_info_set_attribute_status (file_data->info,
218
						  "slideshow::playlist",
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
						  G_FILE_ATTRIBUTE_STATUS_SET);
	}
}


void
ss__gth_catalog_read_from_doc (GthCatalog *catalog,
			       DomElement *root)
{
	DomElement *node;

	for (node = root->first_child; node; node = node->next_sibling) {
		DomElement *child;

		if (g_strcmp0 (node->tag_name, "slideshow") != 0)
			continue;

		g_value_hash_set_boolean (catalog->attributes,
					  "slideshow::personalize",
					  g_strcmp0 (dom_element_get_attribute (node, "personalize"), "true") == 0);
		g_value_hash_set_boolean (catalog->attributes,
240
241
					  "slideshow::automatic",
					  g_strcmp0 (dom_element_get_attribute (node, "automatic"), "true") == 0);
242
		g_value_hash_set_boolean (catalog->attributes,
243
244
					  "slideshow::wrap-around",
					  g_strcmp0 (dom_element_get_attribute (node, "wrap-around"), "true") == 0);
245
246
247

		for (child = node->first_child; child; child = child->next_sibling) {
			if (g_strcmp0 (child->tag_name, "delay") == 0) {
248
				int delay;
249

250
251
252
253
				sscanf (dom_element_get_inner_text (child), "%d", &delay);
				g_value_hash_set_int (catalog->attributes,
						      "slideshow::delay",
						      delay);
254
255
256
257
258
259
			}
			else if (g_strcmp0 (child->tag_name, "transition") == 0) {
				g_value_hash_set_string (catalog->attributes,
							 "slideshow::transition",
							 dom_element_get_inner_text (child));
			}
260
			else if (g_strcmp0 (child->tag_name, "playlist") == 0) {
261
262
263
264
265
266
267
268
269
				DomElement  *file;
				GList       *audio_files;

				audio_files = NULL;
				for (file = child->first_child; file; file = file->next_sibling) {
					if (g_strcmp0 (file->tag_name, "file") == 0)
						audio_files = g_list_prepend (audio_files, g_strdup (dom_element_get_attribute (file, "uri")));
				}
				audio_files = g_list_reverse (audio_files);
270

271
272
273
274
275
				if (audio_files != NULL) {
					char **audio_files_v;

					audio_files_v = _g_string_list_to_strv (audio_files);
					g_value_hash_set_stringv (catalog->attributes,
276
								  "slideshow::playlist",
277
278
279
280
281
								  audio_files_v);

					g_strfreev (audio_files_v);
				}
				else
282
					g_value_hash_unset (catalog->attributes, "slideshow::playlist");
283
284
285
286
287
288
289
290
291
292
293
294
295
296

				_g_string_list_free (audio_files);
			}
		}
	}
}


void
ss__gth_catalog_write_to_doc (GthCatalog  *catalog,
			      DomDocument *doc,
			      DomElement  *root)
{
	DomElement  *slideshow;
Paolo Bacchilega's avatar
Paolo Bacchilega committed
297
298
299

	if (! g_value_hash_is_set (catalog->attributes, "slideshow::personalize"))
		return;
300
301
302

	slideshow = dom_document_create_element (doc,
						 "slideshow",
Paolo Bacchilega's avatar
Paolo Bacchilega committed
303
304
305
						 "personalize", (g_value_hash_get_boolean_or_default (catalog->attributes, "slideshow::personalize", FALSE) ? "true" : "false"),
						 "automatic", (g_value_hash_get_boolean_or_default (catalog->attributes, "slideshow::automatic", FALSE) ? "true" : "false"),
						 "wrap-around", (g_value_hash_get_boolean_or_default (catalog->attributes, "slideshow::wrap-around", FALSE) ? "true" : "false"),
306
307
308
						 NULL);
	dom_element_append_child (root, slideshow);

Paolo Bacchilega's avatar
Paolo Bacchilega committed
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
	if (g_value_hash_is_set (catalog->attributes, "slideshow::delay")) {
		char *delay;

		delay = g_strdup_printf ("%d", g_value_hash_get_int (catalog->attributes, "slideshow::delay"));
		dom_element_append_child (slideshow,
					  dom_document_create_element_with_text (doc, delay, "delay", NULL));

		g_free (delay);
	}

	if (g_value_hash_is_set (catalog->attributes, "slideshow::transition"))
		dom_element_append_child (slideshow,
					  dom_document_create_element_with_text (doc,
										 g_value_hash_get_string (catalog->attributes, "slideshow::transition"),
										 "transition",
										 NULL));
325

Paolo Bacchilega's avatar
Paolo Bacchilega committed
326
327
	if (g_value_hash_is_set (catalog->attributes, "slideshow::playlist")) {
		char **playlist_files;
328

Paolo Bacchilega's avatar
Paolo Bacchilega committed
329
330
331
332
		playlist_files = g_value_hash_get_stringv (catalog->attributes, "slideshow::playlist");
		if (playlist_files[0] != NULL) {
			DomElement *playlist;
			int         i;
333

Paolo Bacchilega's avatar
Paolo Bacchilega committed
334
335
			playlist = dom_document_create_element (doc, "playlist", NULL);
			dom_element_append_child (slideshow, playlist);
336

Paolo Bacchilega's avatar
Paolo Bacchilega committed
337
338
339
			for (i = 0; playlist_files[i] != NULL; i++)
				dom_element_append_child (playlist, dom_document_create_element (doc, "file", "uri", playlist_files[i], NULL));
		}
340
	}
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
}


void
ss__dlg_catalog_properties (GtkBuilder  *builder,
			    GthFileData *file_data,
			    GthCatalog  *catalog)
{
	GtkWidget *slideshow_preferences;
	GtkWidget *label;

	if (! g_value_hash_is_set (catalog->attributes, "slideshow::personalize")
	    || ! g_value_hash_get_boolean (catalog->attributes, "slideshow::personalize"))
	{
		char *current_transition;

		current_transition = eel_gconf_get_string (PREF_SLIDESHOW_TRANSITION, DEFAULT_TRANSITION);
		slideshow_preferences = gth_slideshow_preferences_new (current_transition,
								       eel_gconf_get_boolean (PREF_SLIDESHOW_AUTOMATIC, TRUE),
360
								       (int) (1000.0 * eel_gconf_get_float (PREF_SLIDESHOW_CHANGE_DELAY, 5.0)),
361
362
363
364
365
366
367
								       eel_gconf_get_boolean (PREF_SLIDESHOW_WRAP_AROUND, FALSE));
		gtk_widget_set_sensitive (gth_slideshow_preferences_get_widget (GTH_SLIDESHOW_PREFERENCES (slideshow_preferences), "personalize_box"), FALSE);

		g_free (current_transition);
	}
	else {
		slideshow_preferences = gth_slideshow_preferences_new (g_value_hash_get_string (catalog->attributes, "slideshow::transition"),
368
								       g_value_hash_get_boolean (catalog->attributes, "slideshow::automatic"),
369
								       g_value_hash_get_int (catalog->attributes, "slideshow::delay"),
370
								       g_value_hash_get_boolean (catalog->attributes, "slideshow::wrap-around"));
371
372
373
374
		gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (gth_slideshow_preferences_get_widget (GTH_SLIDESHOW_PREFERENCES (slideshow_preferences), "personalize_checkbutton")), TRUE);
		gtk_widget_set_sensitive (gth_slideshow_preferences_get_widget (GTH_SLIDESHOW_PREFERENCES (slideshow_preferences), "personalize_box"), TRUE);
	}

375
	if (g_value_hash_is_set (catalog->attributes, "slideshow::playlist"))
376
		gth_slideshow_preferences_set_audio (GTH_SLIDESHOW_PREFERENCES (slideshow_preferences),
377
						     g_value_hash_get_stringv (catalog->attributes, "slideshow::playlist"));
378
379
380
381
382
383

	gtk_widget_show (gth_slideshow_preferences_get_widget (GTH_SLIDESHOW_PREFERENCES (slideshow_preferences), "personalize_checkbutton"));
	gtk_widget_hide (gth_slideshow_preferences_get_widget (GTH_SLIDESHOW_PREFERENCES (slideshow_preferences), "slideshow_label"));
	gtk_widget_show (gth_slideshow_preferences_get_widget (GTH_SLIDESHOW_PREFERENCES (slideshow_preferences), "playlist_box"));
	gtk_widget_show (slideshow_preferences);

384
385
386
387
#ifndef HAVE_CLUTTER
	gtk_widget_hide (gth_slideshow_preferences_get_widget (GTH_SLIDESHOW_PREFERENCES (slideshow_preferences), "transition_box"));
#endif /* ! HAVE_CLUTTER */

388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
	label = gtk_label_new (_("Slideshow"));
	gtk_widget_show (label);

	gtk_notebook_append_page (GTK_NOTEBOOK (_gtk_builder_get_widget (builder, "properties_notebook")), slideshow_preferences, label);
	g_object_set_data (G_OBJECT (builder), "slideshow_preferences", slideshow_preferences);
}


void
ss__dlg_catalog_properties_save (GtkBuilder  *builder,
				 GthFileData *file_data,
				 GthCatalog  *catalog)
{
	GtkWidget  *slideshow_preferences;
	char       *transition_id;
	char      **files;

	slideshow_preferences = g_object_get_data (G_OBJECT (builder), "slideshow_preferences");

	g_value_hash_set_boolean (catalog->attributes,
				  "slideshow::personalize",
				  gth_slideshow_preferences_get_personalize (GTH_SLIDESHOW_PREFERENCES (slideshow_preferences)));

	transition_id = gth_slideshow_preferences_get_transition_id (GTH_SLIDESHOW_PREFERENCES (slideshow_preferences));
	g_value_hash_set_string (catalog->attributes,
				 "slideshow::transition",
				 transition_id);
	g_free (transition_id);

	g_value_hash_set_boolean (catalog->attributes,
418
				  "slideshow::automatic",
419
				  gth_slideshow_preferences_get_automatic (GTH_SLIDESHOW_PREFERENCES (slideshow_preferences)));
420
421
422
	g_value_hash_set_int (catalog->attributes,
			      "slideshow::delay",
			      gth_slideshow_preferences_get_delay (GTH_SLIDESHOW_PREFERENCES (slideshow_preferences)));
423
	g_value_hash_set_boolean (catalog->attributes,
424
				  "slideshow::wrap-around",
425
426
427
428
				  gth_slideshow_preferences_get_wrap_around (GTH_SLIDESHOW_PREFERENCES (slideshow_preferences)));

	files = gth_slideshow_preferences_get_audio_files (GTH_SLIDESHOW_PREFERENCES (slideshow_preferences));
	g_value_hash_set_stringv (catalog->attributes,
429
				  "slideshow::playlist",
430
431
432
				  files);
	g_strfreev (files);
}