totem-gallery.c 7.38 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/* 
 * Copyright (C) 2008 Philip Withnall <philip@tecnocode.co.uk>
 *
 * 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
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA.
 *
 * The Totem project hereby grant permission for non-gpl compatible GStreamer
 * plugins to be used and distributed together with GStreamer and Totem. This
 * permission are above and beyond the permissions granted by the GPL license
 * Totem is covered by.
 *
 * Monday 7th February 2005: Christian Schaller: Add exception clause.
 * See license_change file for details.
 */

#include "config.h"

#include <glib.h>
#include <gtk/gtk.h>
#include <glib/gi18n-lib.h>

34
#include "totem-dirs.h"
35 36 37 38 39 40
#include "totem-gallery.h"
#include "totem-gallery-progress.h"
#include "totem-screenshot-plugin.h"

static void dialog_response_callback (GtkDialog *dialog, gint response_id, TotemGallery *self);

41
static void default_screenshot_count_toggled_callback (GtkToggleButton *toggle_button, TotemGallery *self);
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65

struct _TotemGalleryPrivate {
	Totem *totem;
	GtkCheckButton *default_screenshot_count;
	GtkSpinButton *screenshot_count;
	GtkSpinButton *screenshot_width;
};

G_DEFINE_TYPE (TotemGallery, totem_gallery, GTK_TYPE_FILE_CHOOSER_DIALOG)
#define TOTEM_GALLERY_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), TOTEM_TYPE_GALLERY, TotemGalleryPrivate))

static void
totem_gallery_class_init (TotemGalleryClass *klass)
{
	g_type_class_add_private (klass, sizeof (TotemGalleryPrivate));
}

static void
totem_gallery_init (TotemGallery *self)
{
	self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, TOTEM_TYPE_GALLERY, TotemGalleryPrivate);
}

TotemGallery *
66
totem_gallery_new (Totem *totem)
67 68 69 70
{
	TotemGallery *gallery;
	GtkWidget *container;
	GtkBuilder *builder;
71
	gchar *movie_title, *uri, *suggested_name;
72
	GFile *file;
73 74 75 76

	/* Create the gallery and its interface */
	gallery = g_object_new (TOTEM_TYPE_GALLERY, NULL);

77
	builder = totem_plugin_load_interface ("screenshot", "gallery.ui", TRUE, NULL, gallery);
78 79 80 81 82 83 84 85 86
	if (builder == NULL) {
		g_object_unref (gallery);
		return NULL;
	}

	/* Grab the widgets */
	gallery->priv->default_screenshot_count = GTK_CHECK_BUTTON (gtk_builder_get_object (builder, "default_screenshot_count"));
	gallery->priv->screenshot_count = GTK_SPIN_BUTTON (gtk_builder_get_object (builder, "screenshot_count"));
	gallery->priv->screenshot_width = GTK_SPIN_BUTTON (gtk_builder_get_object (builder, "screenshot_width"));
87 88
	g_signal_connect (gtk_builder_get_object (builder, "default_screenshot_count"), "toggled",
			  G_CALLBACK (default_screenshot_count_toggled_callback), gallery);
89 90 91 92 93 94 95 96 97

	gallery->priv->totem = totem;

	gtk_window_set_title (GTK_WINDOW (gallery), _("Save Gallery"));
	gtk_file_chooser_set_action (GTK_FILE_CHOOSER (gallery), GTK_FILE_CHOOSER_ACTION_SAVE);
	gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (gallery), TRUE);
	/*gtk_window_set_resizable (GTK_WINDOW (gallery), FALSE);
	gtk_dialog_set_has_separator (GTK_DIALOG (gallery), TRUE);*/
	gtk_dialog_add_buttons (GTK_DIALOG (gallery),
98 99
			_("_Cancel"), GTK_RESPONSE_CANCEL,
			_("_Save"), GTK_RESPONSE_OK,
100 101 102 103 104 105 106 107 108
			NULL);
	gtk_dialog_set_default_response (GTK_DIALOG (gallery), GTK_RESPONSE_OK);

	g_signal_connect (G_OBJECT (gallery), "response",
			  G_CALLBACK (dialog_response_callback), gallery);

	container = GTK_WIDGET (gtk_builder_get_object (builder,
				"gallery_dialog_content"));
	gtk_file_chooser_set_extra_widget (GTK_FILE_CHOOSER (gallery), container);
109

110
	movie_title = totem_object_get_short_title (totem);
111 112 113 114 115 116 117 118

	/* Translators: The first argument is the movie title. The second
	 * argument is a number which is used to prevent overwriting files.
	 * Just translate "Gallery", and not the ".jpg". Example:
	 * "Galerie-%s-%d.jpg". */
	uri = totem_screenshot_plugin_setup_file_chooser (N_("Gallery-%s-%d.jpg"), movie_title);
	g_free (movie_title);

119 120 121 122 123 124
	file = g_file_new_for_uri (uri);
	/* We can use g_file_get_basename here and be sure that it's UTF-8
	 * because we provided the name. */
	suggested_name = g_file_get_basename (file);
	g_object_unref (file);

125 126
	gtk_file_chooser_set_uri (GTK_FILE_CHOOSER (gallery), uri);
	g_free (uri);
127 128
	gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (gallery), suggested_name);
	g_free (suggested_name);
129 130 131 132 133 134 135 136

	gtk_widget_show_all (GTK_WIDGET (gallery));

	g_object_unref (builder);

	return gallery;
}

137
static void
138 139 140 141 142 143 144 145 146 147
default_screenshot_count_toggled_callback (GtkToggleButton *toggle_button, TotemGallery *self)
{
	/* Only have the screenshot count spin button sensitive when the default screenshot count
	 * check button is unchecked. */
	gtk_widget_set_sensitive (GTK_WIDGET (self->priv->screenshot_count), !gtk_toggle_button_get_active (toggle_button));
}

static void
dialog_response_callback (GtkDialog *dialog, gint response_id, TotemGallery *self)
{
148
	gchar *filename, *video_mrl, *argv[6];
149 150 151 152
	guint screenshot_count, i;
	gint stdout_fd;
	GPid child_pid;
	GtkWidget *progress_dialog;
153
	gboolean ret;
154 155 156 157 158 159
	GError *error = NULL;

	if (response_id != GTK_RESPONSE_OK)
		return;
	gtk_widget_hide (GTK_WIDGET (dialog));

160 161 162
	/* Don't call in here again */
	g_signal_handlers_disconnect_by_func (G_OBJECT (self), dialog_response_callback, self);

163 164 165 166 167 168
	if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (self->priv->default_screenshot_count)) == TRUE)
		screenshot_count = 0;
	else
		screenshot_count = gtk_spin_button_get_value_as_int (self->priv->screenshot_count);

	filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (self));
169
	video_mrl = totem_object_get_current_mrl (self->priv->totem);
170
	totem_screenshot_plugin_update_file_chooser (gtk_file_chooser_get_uri (GTK_FILE_CHOOSER (self)));
171 172

	/* Build the command and arguments to pass it */
173
	argv[0] = (gchar*) LIBEXECDIR "/totem-gallery-thumbnailer"; /* a little hacky, but only the allocated stuff is freed below */
174 175 176 177 178
	argv[1] = g_strdup_printf ("--gallery=%u", screenshot_count); /* number of screenshots to output */
	argv[2] = g_strdup_printf ("--size=%u", gtk_spin_button_get_value_as_int (self->priv->screenshot_width)); /* screenshot width */
	argv[3] = video_mrl; /* video to thumbnail */
	argv[4] = filename; /* output filename */
	argv[5] = NULL;
179 180

	/* Run the command */
181 182 183 184
	ret = g_spawn_async_with_pipes (NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL,
					&child_pid, NULL, &stdout_fd, NULL, &error);

	/* Free argv, minus the filename */
185
	for (i = 1; i < G_N_ELEMENTS (argv) - 2; i++)
186 187 188
		g_free (argv[i]);

	if (ret == FALSE) {
189 190
		g_warning ("Error spawning totem-video-thumbnailer: %s", error->message);
		g_error_free (error);
191
		return;
192 193 194 195
	}

	/* Create the progress dialogue */
	progress_dialog = GTK_WIDGET (totem_gallery_progress_new (child_pid, filename));
196
	g_free (filename);
197
	totem_gallery_progress_run (TOTEM_GALLERY_PROGRESS (progress_dialog), stdout_fd);
198
	gtk_dialog_run (GTK_DIALOG (progress_dialog));
199 200
	gtk_widget_destroy (progress_dialog);

201
	gtk_dialog_response (GTK_DIALOG (self), 0);
202
}
203