[gthumb] media viewer: save the screenshot without showing the dialog
- From: Paolo Bacchilega <paobac src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gthumb] media viewer: save the screenshot without showing the dialog
- Date: Thu, 14 Aug 2014 15:30:47 +0000 (UTC)
commit 5f2d5d9ec18d6115e1f98486e693380c8a2abdf9
Author: Paolo Bacchilega <paobac src gnome org>
Date: Thu Aug 14 17:18:16 2014 +0200
media viewer: save the screenshot without showing the dialog
allow to set the screenshots location in the extension preferences
dialog
extensions/gstreamer_tools/Makefile.am | 2 +
extensions/gstreamer_tools/actions.c | 154 +++++++------------
extensions/gstreamer_tools/data/ui/Makefile.am | 2 +-
.../data/ui/media-viewer-preferences.ui | 92 ++++++++++++
.../gstreamer_tools/dlg-media-viewer-preferences.c | 100 +++++++++++++
.../gstreamer_tools/dlg-media-viewer-preferences.h | 29 ++++
extensions/gstreamer_tools/main.c | 4 +-
gthumb/glib-utils.c | 33 ++++-
gthumb/glib-utils.h | 4 +
9 files changed, 320 insertions(+), 100 deletions(-)
---
diff --git a/extensions/gstreamer_tools/Makefile.am b/extensions/gstreamer_tools/Makefile.am
index 944fa2c..b9344a7 100644
--- a/extensions/gstreamer_tools/Makefile.am
+++ b/extensions/gstreamer_tools/Makefile.am
@@ -8,6 +8,8 @@ extension_LTLIBRARIES = libgstreamer_tools.la
libgstreamer_tools_la_SOURCES = \
actions.c \
actions.h \
+ dlg-media-viewer-preferences.c \
+ dlg-media-viewer-preferences.h \
gth-media-viewer-page.c \
gth-media-viewer-page.h \
gth-metadata-provider-gstreamer.c \
diff --git a/extensions/gstreamer_tools/actions.c b/extensions/gstreamer_tools/actions.c
index 68220aa..4280f58 100644
--- a/extensions/gstreamer_tools/actions.c
+++ b/extensions/gstreamer_tools/actions.c
@@ -73,51 +73,45 @@ save_screenshot_task_completed_cb (GthTask *task,
}
-static void
-save_as_response_cb (GtkDialog *file_sel,
- int response,
- SaveData *save_data)
+static GFile *
+get_screenshot_file (SaveData *save_data,
+ GError **error)
{
- GFile *file;
- const char *mime_type;
- GFile *folder;
- char *folder_uri;
- GthTask *task;
-
- if (response != GTK_RESPONSE_OK) {
- GthMediaViewerPage *page = save_data->page;
-
- if (save_data->playing_before_screenshot)
- gst_element_set_state (gth_media_viewer_page_get_playbin (page), GST_STATE_PLAYING);
- save_date_free (save_data);
- gtk_widget_destroy (GTK_WIDGET (file_sel));
- return;
+ GFile *file = NULL;
+ char *uri;
+ GFile *folder;
+ GthFileData *file_data;
+ char *prefix;
+ int attempt;
+
+ uri = _g_settings_get_uri_or_special_dir (save_data->settings,
PREF_GSTREAMER_TOOLS_SCREESHOT_LOCATION, G_USER_DIRECTORY_PICTURES);
+ folder = g_file_new_for_uri (uri);
+ file_data = gth_media_viewer_page_get_file_data (save_data->page);
+ prefix = _g_utf8_remove_extension (g_file_info_get_display_name (file_data->info));
+ if (prefix == NULL)
+ prefix = g_strdup (C_("Filename", "Screenshot"));
+
+ for (attempt = 1; (file == NULL) && (attempt < MAX_ATTEMPTS); attempt++) {
+ char *display_name;
+ GFile *proposed_file;
+
+ display_name = g_strdup_printf ("%s-%02d.jpeg", prefix, attempt);
+ proposed_file = g_file_get_child_for_display_name (folder, display_name, NULL);
+ if ((proposed_file != NULL) && ! g_file_query_exists (proposed_file, NULL))
+ file = g_object_ref (proposed_file);
+
+ _g_object_unref (proposed_file);
+ g_free (display_name);
}
- if (! gth_file_chooser_dialog_get_file (GTH_FILE_CHOOSER_DIALOG (file_sel), &file, &mime_type))
- return;
+ if (file == NULL)
+ g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_FILENAME, "Invalid filename");
- folder = g_file_get_parent (file);
- folder_uri = g_file_get_uri (folder);
- g_settings_set_string (save_data->settings, PREF_GSTREAMER_TOOLS_SCREESHOT_LOCATION, folder_uri);
+ g_free (prefix);
+ _g_object_unref (folder);
+ g_free (uri);
- save_data->file_data = gth_file_data_new (file, NULL);
- gth_file_data_set_mime_type (save_data->file_data, mime_type);
- task = gth_save_image_task_new (save_data->image,
- mime_type,
- save_data->file_data,
- GTH_OVERWRITE_RESPONSE_YES);
- g_signal_connect (task,
- "completed",
- G_CALLBACK (save_screenshot_task_completed_cb),
- save_data);
- gth_browser_exec_task (GTH_BROWSER (save_data->browser), task, FALSE);
-
- gtk_widget_destroy (GTK_WIDGET (file_sel));
-
- g_free (folder_uri);
- g_object_unref (folder);
- g_object_unref (file);
+ return file;
}
@@ -125,8 +119,10 @@ static void
screenshot_ready_cb (GdkPixbuf *pixbuf,
gpointer user_data)
{
- SaveData *save_data = user_data;
- GtkWidget *file_sel;
+ SaveData *save_data = user_data;
+ GFile *file;
+ GError *error = NULL;
+ GthTask *task;
if (pixbuf == NULL) {
_gtk_error_dialog_from_gerror_show (GTK_WINDOW (save_data->browser), _("Could not take a
screenshot"), NULL);
@@ -135,65 +131,29 @@ screenshot_ready_cb (GdkPixbuf *pixbuf,
}
save_data->image = gth_image_new_for_pixbuf (pixbuf);
- file_sel = gth_file_chooser_dialog_new (_("Save Image"), GTK_WINDOW (save_data->browser),
"image-saver");
- gtk_window_set_modal (GTK_WINDOW (file_sel), TRUE);
-
- {
- char *last_uri;
- GFile *last_folder;
- GthFileData *file_data;
- char *prefix;
- char *display_name;
- int attempt;
-
- last_uri = g_settings_get_string (save_data->settings,
PREF_GSTREAMER_TOOLS_SCREESHOT_LOCATION);
- if ((last_uri == NULL) || (strcmp (last_uri, "~") == 0) || (strcmp (last_uri, "file://~") ==
0)) {
- const char *dir;
-
- dir = g_get_user_special_dir (G_USER_DIRECTORY_PICTURES);
- if (dir != NULL)
- last_folder = g_file_new_for_path (dir);
- else
- last_folder = g_file_new_for_uri (get_home_uri ());
- }
- else
- last_folder = g_file_new_for_uri (last_uri);
- gtk_file_chooser_set_current_folder_file (GTK_FILE_CHOOSER (file_sel), last_folder, NULL);
-
- file_data = gth_media_viewer_page_get_file_data (save_data->page);
- prefix = _g_utf8_remove_extension (g_file_info_get_display_name (file_data->info));
- if (prefix == NULL)
- prefix = g_strdup (C_("Filename", "Screenshot"));
- display_name = NULL;
- for (attempt = 1; attempt < MAX_ATTEMPTS; attempt++) {
- GFile *proposed_file;
-
- g_free (display_name);
-
- display_name = g_strdup_printf ("%s-%02d.jpeg", prefix, attempt);
- proposed_file = g_file_get_child_for_display_name (last_folder, display_name, NULL);
- if ((proposed_file != NULL) && ! g_file_query_exists (proposed_file, NULL)) {
- g_object_unref (proposed_file);
- break;
- }
- }
-
- if (display_name != NULL) {
- gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (file_sel), display_name);
- g_free (display_name);
- }
-
- g_free (prefix);
- g_object_unref (last_folder);
- g_free (last_uri);
+
+ /* save the image */
+
+ file = get_screenshot_file (save_data, &error);
+ if (file == NULL) {
+ _gtk_error_dialog_from_gerror_show (GTK_WINDOW (save_data->browser), _("Could not take a
screenshot"), error);
+ save_date_free (save_data);
+ g_clear_error (&error);
+ return;
}
- g_signal_connect (GTK_DIALOG (file_sel),
- "response",
- G_CALLBACK (save_as_response_cb),
+ save_data->file_data = gth_file_data_new (file, NULL);
+ gth_file_data_set_mime_type (save_data->file_data, "image/jpeg");
+ task = gth_save_image_task_new (save_data->image,
+ "image/jpeg",
+ save_data->file_data,
+ GTH_OVERWRITE_RESPONSE_YES);
+ g_signal_connect (task,
+ "completed",
+ G_CALLBACK (save_screenshot_task_completed_cb),
save_data);
+ gth_browser_exec_task (GTH_BROWSER (save_data->browser), task, FALSE);
- gtk_widget_show (file_sel);
}
diff --git a/extensions/gstreamer_tools/data/ui/Makefile.am b/extensions/gstreamer_tools/data/ui/Makefile.am
index aeceb4e..472f279 100644
--- a/extensions/gstreamer_tools/data/ui/Makefile.am
+++ b/extensions/gstreamer_tools/data/ui/Makefile.am
@@ -1,5 +1,5 @@
uidir = $(pkgdatadir)/ui
-ui_DATA = mediabar.ui
+ui_DATA = mediabar.ui media-viewer-preferences.ui
EXTRA_DIST = $(ui_DATA)
-include $(top_srcdir)/git.mk
diff --git a/extensions/gstreamer_tools/data/ui/media-viewer-preferences.ui
b/extensions/gstreamer_tools/data/ui/media-viewer-preferences.ui
new file mode 100644
index 0000000..ac6d9cf
--- /dev/null
+++ b/extensions/gstreamer_tools/data/ui/media-viewer-preferences.ui
@@ -0,0 +1,92 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!-- Generated with glade 3.18.3 -->
+<interface>
+ <requires lib="gtk+" version="3.0"/>
+ <object class="GtkDialog" id="preferences_dialog">
+ <property name="can_focus">False</property>
+ <property name="border_width">5</property>
+ <property name="title" translatable="yes">Preferences</property>
+ <property name="type_hint">dialog</property>
+ <child internal-child="vbox">
+ <object class="GtkBox" id="dialog-vbox7">
+ <property name="visible">True</property>
+ <property name="can_focus">False</property>
+ <property name="orientation">vertical</property>
+ <child internal-child="action_area">
+ <object class="GtkButtonBox" id="dialog-action_area7">
+ <property name="visible">True</property>
+ <property name="can_focus">False</property>
+ <property name="layout_style">end</property>
+ <child>
+ <object class="GtkButton" id="close_button">
+ <property name="label">gtk-close</property>
+ <property name="visible">True</property>
+ <property name="can_focus">True</property>
+ <property name="receives_default">True</property>
+ <property name="use_stock">True</property>
+ </object>
+ <packing>
+ <property name="expand">False</property>
+ <property name="fill">False</property>
+ <property name="position">0</property>
+ </packing>
+ </child>
+ </object>
+ <packing>
+ <property name="expand">False</property>
+ <property name="fill">True</property>
+ <property name="pack_type">end</property>
+ <property name="position">0</property>
+ </packing>
+ </child>
+ <child>
+ <object class="GtkBox" id="box1">
+ <property name="visible">True</property>
+ <property name="can_focus">False</property>
+ <property name="border_width">5</property>
+ <property name="orientation">vertical</property>
+ <property name="spacing">5</property>
+ <child>
+ <object class="GtkLabel" id="label1">
+ <property name="visible">True</property>
+ <property name="can_focus">False</property>
+ <property name="xalign">0</property>
+ <property name="label" translatable="yes">_Screenshots location:</property>
+ <property name="use_underline">True</property>
+ <property name="mnemonic_widget">screenshots_filechooserbutton</property>
+ </object>
+ <packing>
+ <property name="expand">False</property>
+ <property name="fill">False</property>
+ <property name="position">0</property>
+ </packing>
+ </child>
+ <child>
+ <object class="GtkFileChooserButton" id="screenshots_filechooserbutton">
+ <property name="width_request">300</property>
+ <property name="visible">True</property>
+ <property name="can_focus">False</property>
+ <property name="action">select-folder</property>
+ <property name="local_only">False</property>
+ <property name="title" translatable="yes"/>
+ </object>
+ <packing>
+ <property name="expand">True</property>
+ <property name="fill">True</property>
+ <property name="position">1</property>
+ </packing>
+ </child>
+ </object>
+ <packing>
+ <property name="expand">False</property>
+ <property name="fill">True</property>
+ <property name="position">1</property>
+ </packing>
+ </child>
+ </object>
+ </child>
+ <action-widgets>
+ <action-widget response="0">close_button</action-widget>
+ </action-widgets>
+ </object>
+</interface>
diff --git a/extensions/gstreamer_tools/dlg-media-viewer-preferences.c
b/extensions/gstreamer_tools/dlg-media-viewer-preferences.c
new file mode 100644
index 0000000..b7ecdb5
--- /dev/null
+++ b/extensions/gstreamer_tools/dlg-media-viewer-preferences.c
@@ -0,0 +1,100 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
+
+/*
+ * GThumb
+ *
+ * Copyright (C) 2014 The 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
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <config.h>
+#include <gtk/gtk.h>
+#include <gthumb.h>
+#include "dlg-media-viewer-preferences.h"
+#include "preferences.h"
+
+
+typedef struct {
+ GtkBuilder *builder;
+ GSettings *settings;
+ GtkWidget *dialog;
+} DialogData;
+
+
+static void
+update_settings (DialogData *data)
+{
+ char *uri;
+
+ uri = gtk_file_chooser_get_uri (GTK_FILE_CHOOSER (gtk_builder_get_object (data->builder,
"screenshots_filechooserbutton")));
+ if (uri == NULL)
+ return;
+
+ _g_settings_set_uri (data->settings,
+ PREF_GSTREAMER_TOOLS_SCREESHOT_LOCATION,
+ uri);
+
+ g_free (uri);
+}
+
+
+static void
+destroy_cb (GtkWidget *widget,
+ DialogData *data)
+{
+ update_settings (data);
+ g_object_unref (data->builder);
+ g_object_unref (data->settings);
+ g_free (data);
+}
+
+
+void
+dlg_media_viewer_preferences (GtkWindow *parent)
+{
+ DialogData *data;
+ char *uri;
+
+ data = g_new0 (DialogData, 1);
+ data->builder = _gtk_builder_new_from_file ("media-viewer-preferences.ui", "gstreamer_tools");
+ data->settings = g_settings_new (GTHUMB_GSTREAMER_TOOLS_SCHEMA);
+
+ /* Get the widgets. */
+
+ data->dialog = _gtk_builder_get_widget (data->builder, "preferences_dialog");
+
+ /* Set widgets data. */
+
+ uri = _g_settings_get_uri_or_special_dir (data->settings, PREF_GSTREAMER_TOOLS_SCREESHOT_LOCATION,
G_USER_DIRECTORY_PICTURES);
+ gtk_file_chooser_set_uri (GTK_FILE_CHOOSER (gtk_builder_get_object (data->builder,
"screenshots_filechooserbutton")), uri);
+ g_free (uri);
+
+ /* Set the signals handlers. */
+
+ g_signal_connect (G_OBJECT (data->dialog),
+ "destroy",
+ G_CALLBACK (destroy_cb),
+ data);
+ g_signal_connect_swapped (gtk_builder_get_object (data->builder, "close_button"),
+ "clicked",
+ G_CALLBACK (gtk_widget_destroy),
+ G_OBJECT (data->dialog));
+
+ /* run dialog. */
+
+ gtk_window_set_transient_for (GTK_WINDOW (data->dialog), parent);
+ gtk_window_set_modal (GTK_WINDOW (data->dialog), TRUE);
+ gtk_widget_show (data->dialog);
+}
diff --git a/extensions/gstreamer_tools/dlg-media-viewer-preferences.h
b/extensions/gstreamer_tools/dlg-media-viewer-preferences.h
new file mode 100644
index 0000000..0bebf45
--- /dev/null
+++ b/extensions/gstreamer_tools/dlg-media-viewer-preferences.h
@@ -0,0 +1,29 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
+
+/*
+ * GThumb
+ *
+ * Copyright (C) 2014 The 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
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef DLG_MEDIA_VIEWER_PREFERENCES_H
+#define DLG_MEDIA_VIEWER_PREFERENCES_H
+
+#include <gthumb.h>
+
+void dlg_media_viewer_preferences (GtkWindow *parent);
+
+#endif /* DLG_MEDIA_VIEWER_PREFERENCES_H */
diff --git a/extensions/gstreamer_tools/main.c b/extensions/gstreamer_tools/main.c
index d16982c..f8077f6 100644
--- a/extensions/gstreamer_tools/main.c
+++ b/extensions/gstreamer_tools/main.c
@@ -23,6 +23,7 @@
#include <config.h>
#include <gtk/gtk.h>
#include <gthumb.h>
+#include "dlg-media-viewer-preferences.h"
#include "gth-metadata-provider-gstreamer.h"
#include "gth-media-viewer-page.h"
@@ -73,11 +74,12 @@ gthumb_extension_deactivate (void)
G_MODULE_EXPORT gboolean
gthumb_extension_is_configurable (void)
{
- return FALSE;
+ return TRUE;
}
G_MODULE_EXPORT void
gthumb_extension_configure (GtkWindow *parent)
{
+ dlg_media_viewer_preferences (parent);
}
diff --git a/gthumb/glib-utils.c b/gthumb/glib-utils.c
index c792979..897413f 100644
--- a/gthumb/glib-utils.c
+++ b/gthumb/glib-utils.c
@@ -3008,13 +3008,44 @@ _g_settings_get_uri (GSettings *settings,
return NULL;
}
- uri = _g_replace (value, "~", g_get_home_dir ());
+ uri = _g_replace (value, "file://~", get_home_uri ());
+
g_free (value);
return uri;
}
+char *
+_g_settings_get_uri_or_special_dir (GSettings *settings,
+ const char *key,
+ GUserDirectory directory)
+{
+ char *uri;
+
+ uri = g_settings_get_string (settings, key);
+ if ((uri == NULL) || (strcmp (uri, "~") == 0) || (strcmp (uri, "file://~") == 0)) {
+ const char *dir;
+
+ g_free (uri);
+ uri = NULL;
+
+ dir = g_get_user_special_dir (directory);
+ if (dir != NULL)
+ uri = g_filename_to_uri (dir, NULL, NULL);
+ if (uri == NULL)
+ uri = g_strdup (get_home_uri ());
+ }
+ else {
+ char *tmp = uri;
+ uri = _g_replace (tmp, "file://~", get_home_uri ());
+ g_free (tmp);
+ }
+
+ return uri;
+}
+
+
void
_g_settings_set_uri (GSettings *settings,
const char *key,
diff --git a/gthumb/glib-utils.h b/gthumb/glib-utils.h
index 0ae5b8c..3c3ee61 100644
--- a/gthumb/glib-utils.h
+++ b/gthumb/glib-utils.h
@@ -327,6 +327,10 @@ gboolean _g_mime_type_is_audio (const char *mime_type);
char * _g_settings_get_uri (GSettings *settings,
const char *key);
+char * _g_settings_get_uri_or_special_dir
+ (GSettings *settings,
+ const char *key,
+ GUserDirectory directory);
void _g_settings_set_uri (GSettings *settings,
const char *key,
const char *uri);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]