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

/*
 *  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
static const GActionEntry actions[] = {
	{ "slideshow", gth_browser_activate_slideshow }
};


38 39 40 41 42
static const GthShortcut shortcuts[] = {
	{ "slideshow", N_("Presentation"), GTH_SHORTCUT_CONTEXT_BROWSER_VIEWER, GTH_SHORTCUT_CATEGORY_FILE_MANAGER, "F5" },
};


43 44 45 46 47
void
ss__gth_browser_construct_cb (GthBrowser *browser)
{
	g_return_if_fail (GTH_IS_BROWSER (browser));

48 49 50 51 52
	g_action_map_add_action_entries (G_ACTION_MAP (browser),
					 actions,
					 G_N_ELEMENTS (actions),
					 browser);

53 54 55 56
	gth_window_add_shortcuts (GTH_WINDOW (browser),
				  shortcuts,
				  G_N_ELEMENTS (shortcuts));

57 58 59
	gth_browser_add_header_bar_button (browser,
					   GTH_BROWSER_HEADER_SECTION_BROWSER_VIEW,
					   "view-presentation-symbolic",
60
					   _("Presentation"),
61
					   "win.slideshow",
62
					   NULL);
63 64 65 66 67 68 69 70 71 72 73
}


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

	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);
74
	g_object_set (g_action_map_lookup_action (G_ACTION_MAP (browser), "slideshow"), "enabled", sensitive, NULL);
75
}
76 77 78 79 80


void
ss__slideshow_cb (GthBrowser *browser)
{
81
	gth_browser_activate_slideshow (NULL, NULL, browser);
82
}
83 84 85 86 87 88 89 90 91 92


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"));
93
	if (g_file_info_get_attribute_status (file_data->info, "slideshow::automatic") == G_FILE_ATTRIBUTE_STATUS_SET)
94
		g_value_hash_set_boolean (catalog->attributes,
95 96 97
					  "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)
98
		g_value_hash_set_boolean (catalog->attributes,
99 100
					  "slideshow::wrap-around",
					  g_file_info_get_attribute_boolean (file_data->info, "slideshow::wrap-around"));
101 102 103 104
	if (g_file_info_get_attribute_status (file_data->info, "slideshow::random-order") == G_FILE_ATTRIBUTE_STATUS_SET)
		g_value_hash_set_boolean (catalog->attributes,
					  "slideshow::random-order",
					  g_file_info_get_attribute_boolean (file_data->info, "slideshow::random-order"));
105
	if (g_file_info_get_attribute_status (file_data->info, "slideshow::delay") == G_FILE_ATTRIBUTE_STATUS_SET)
106 107 108
		g_value_hash_set_int (catalog->attributes,
				      "slideshow::delay",
				      g_file_info_get_attribute_int32 (file_data->info, "slideshow::delay"));
109 110 111 112
	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"));
113
	if (g_file_info_get_attribute_status (file_data->info, "slideshow::playlist") == G_FILE_ATTRIBUTE_STATUS_SET)
114
		g_value_hash_set_stringv (catalog->attributes,
115 116
					  "slideshow::playlist",
					  g_file_info_get_attribute_stringv (file_data->info, "slideshow::playlist"));
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
}


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);
	}
132
	if (g_value_hash_is_set (catalog->attributes, "slideshow::automatic")) {
133
		g_file_info_set_attribute_boolean (file_data->info,
134 135
						   "slideshow::automatic",
						   g_value_hash_get_boolean (catalog->attributes, "slideshow::automatic"));
136
		g_file_info_set_attribute_status (file_data->info,
137
						  "slideshow::automatic",
138 139
						  G_FILE_ATTRIBUTE_STATUS_SET);
	}
140
	if (g_value_hash_is_set (catalog->attributes, "slideshow::wrap-around")) {
141
		g_file_info_set_attribute_boolean (file_data->info,
142 143
						   "slideshow::wrap-around",
						   g_value_hash_get_boolean (catalog->attributes, "slideshow::wrap-around"));
144
		g_file_info_set_attribute_status (file_data->info,
145
						  "slideshow::wrap-around",
146 147
						  G_FILE_ATTRIBUTE_STATUS_SET);
	}
148 149 150 151 152 153 154 155
	if (g_value_hash_is_set (catalog->attributes, "slideshow::random-order")) {
		g_file_info_set_attribute_boolean (file_data->info,
						   "slideshow::random-order",
						   g_value_hash_get_boolean (catalog->attributes, "slideshow::random-order"));
		g_file_info_set_attribute_status (file_data->info,
						  "slideshow::random-order",
						  G_FILE_ATTRIBUTE_STATUS_SET);
	}
156 157 158
	if (g_value_hash_is_set (catalog->attributes, "slideshow::delay")) {
		g_file_info_set_attribute_int32 (file_data->info,
						 "slideshow::delay",
159
						 g_value_hash_get_int (catalog->attributes, "slideshow::delay"));
160 161 162 163 164 165 166 167 168 169 170 171
		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);
	}
172
	if (g_value_hash_is_set (catalog->attributes, "slideshow::playlist")) {
173
		g_file_info_set_attribute_stringv (file_data->info,
174 175
						   "slideshow::playlist",
						   g_value_hash_get_stringv (catalog->attributes, "slideshow::playlist"));
176
		g_file_info_set_attribute_status (file_data->info,
177
						  "slideshow::playlist",
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
						  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,
199 200
					  "slideshow::automatic",
					  g_strcmp0 (dom_element_get_attribute (node, "automatic"), "true") == 0);
201
		g_value_hash_set_boolean (catalog->attributes,
202 203
					  "slideshow::wrap-around",
					  g_strcmp0 (dom_element_get_attribute (node, "wrap-around"), "true") == 0);
204 205 206
		g_value_hash_set_boolean (catalog->attributes,
					  "slideshow::random-order",
					  g_strcmp0 (dom_element_get_attribute (node, "random-order"), "true") == 0);
207 208 209

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

212 213 214 215
				sscanf (dom_element_get_inner_text (child), "%d", &delay);
				g_value_hash_set_int (catalog->attributes,
						      "slideshow::delay",
						      delay);
216 217 218 219 220 221
			}
			else if (g_strcmp0 (child->tag_name, "transition") == 0) {
				g_value_hash_set_string (catalog->attributes,
							 "slideshow::transition",
							 dom_element_get_inner_text (child));
			}
222
			else if (g_strcmp0 (child->tag_name, "playlist") == 0) {
223 224 225 226 227 228 229 230 231
				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);
232

233 234 235 236 237
				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,
238
								  "slideshow::playlist",
239 240 241 242 243
								  audio_files_v);

					g_strfreev (audio_files_v);
				}
				else
244
					g_value_hash_unset (catalog->attributes, "slideshow::playlist");
245 246 247 248 249 250 251 252 253 254 255 256 257 258

				_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
259 260 261

	if (! g_value_hash_is_set (catalog->attributes, "slideshow::personalize"))
		return;
262 263 264

	slideshow = dom_document_create_element (doc,
						 "slideshow",
Paolo Bacchilega's avatar
Paolo Bacchilega committed
265 266 267
						 "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"),
268
						 "random-order", (g_value_hash_get_boolean_or_default (catalog->attributes, "slideshow::random-order", FALSE) ? "true" : "false"),
269 270 271
						 NULL);
	dom_element_append_child (root, slideshow);

Paolo Bacchilega's avatar
Paolo Bacchilega committed
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
	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));
288

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

Paolo Bacchilega's avatar
Paolo Bacchilega committed
292 293 294 295
		playlist_files = g_value_hash_get_stringv (catalog->attributes, "slideshow::playlist");
		if (playlist_files[0] != NULL) {
			DomElement *playlist;
			int         i;
296

Paolo Bacchilega's avatar
Paolo Bacchilega committed
297 298
			playlist = dom_document_create_element (doc, "playlist", NULL);
			dom_element_append_child (slideshow, playlist);
299

Paolo Bacchilega's avatar
Paolo Bacchilega committed
300 301 302
			for (i = 0; playlist_files[i] != NULL; i++)
				dom_element_append_child (playlist, dom_document_create_element (doc, "file", "uri", playlist_files[i], NULL));
		}
303
	}
304 305 306 307 308 309 310 311 312 313 314 315 316 317
}


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"))
	{
318 319
		GSettings *settings;
		char      *current_transition;
320

321 322
		settings = g_settings_new (GTHUMB_SLIDESHOW_SCHEMA);
		current_transition = g_settings_get_string (settings, PREF_SLIDESHOW_TRANSITION);
323
		slideshow_preferences = gth_slideshow_preferences_new (current_transition,
324 325 326 327
								       g_settings_get_boolean (settings, PREF_SLIDESHOW_AUTOMATIC),
								       (int) (1000.0 * g_settings_get_double (settings, PREF_SLIDESHOW_CHANGE_DELAY)),
								       g_settings_get_boolean (settings, PREF_SLIDESHOW_WRAP_AROUND),
								       g_settings_get_boolean (settings, PREF_SLIDESHOW_RANDOM_ORDER));
328 329 330
		gtk_widget_set_sensitive (gth_slideshow_preferences_get_widget (GTH_SLIDESHOW_PREFERENCES (slideshow_preferences), "personalize_box"), FALSE);

		g_free (current_transition);
331
		g_object_unref (settings);
332 333 334
	}
	else {
		slideshow_preferences = gth_slideshow_preferences_new (g_value_hash_get_string (catalog->attributes, "slideshow::transition"),
335
								       g_value_hash_get_boolean (catalog->attributes, "slideshow::automatic"),
336
								       g_value_hash_get_int (catalog->attributes, "slideshow::delay"),
337 338
								       g_value_hash_get_boolean (catalog->attributes, "slideshow::wrap-around"),
								       g_value_hash_get_boolean (catalog->attributes, "slideshow::random-order"));
339 340 341 342
		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);
	}

343
	if (g_value_hash_is_set (catalog->attributes, "slideshow::playlist"))
344
		gth_slideshow_preferences_set_audio (GTH_SLIDESHOW_PREFERENCES (slideshow_preferences),
345
						     g_value_hash_get_stringv (catalog->attributes, "slideshow::playlist"));
346

347
	gtk_container_set_border_width (GTK_CONTAINER (slideshow_preferences), 12);
348 349 350 351 352
	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);

353 354 355 356
#ifndef HAVE_CLUTTER
	gtk_widget_hide (gth_slideshow_preferences_get_widget (GTH_SLIDESHOW_PREFERENCES (slideshow_preferences), "transition_box"));
#endif /* ! HAVE_CLUTTER */

357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
	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,
387
				  "slideshow::automatic",
388
				  gth_slideshow_preferences_get_automatic (GTH_SLIDESHOW_PREFERENCES (slideshow_preferences)));
389 390 391
	g_value_hash_set_int (catalog->attributes,
			      "slideshow::delay",
			      gth_slideshow_preferences_get_delay (GTH_SLIDESHOW_PREFERENCES (slideshow_preferences)));
392
	g_value_hash_set_boolean (catalog->attributes,
393
				  "slideshow::wrap-around",
394
				  gth_slideshow_preferences_get_wrap_around (GTH_SLIDESHOW_PREFERENCES (slideshow_preferences)));
395 396 397
	g_value_hash_set_boolean (catalog->attributes,
				  "slideshow::random-order",
				  gth_slideshow_preferences_get_random_order (GTH_SLIDESHOW_PREFERENCES (slideshow_preferences)));
398 399 400

	files = gth_slideshow_preferences_get_audio_files (GTH_SLIDESHOW_PREFERENCES (slideshow_preferences));
	g_value_hash_set_stringv (catalog->attributes,
401
				  "slideshow::playlist",
402 403 404
				  files);
	g_strfreev (files);
}