Commit 67a923a1 authored by Nickolay V. Shmyrev's avatar Nickolay V. Shmyrev

Rework factory functions so they are placed now in a single file


	* backend/ev-document-factory.c:
	(ev_document_factory_get_from_mime),
	(ev_document_factory_get_all_mime_types), (get_document_from_uri),
	(ev_document_factory_get_document),
	(file_filter_add_mime_list_and_free),
	(ev_document_factory_add_filters):
	* backend/ev-document-factory.h:
	* properties/ev-properties-main.c: (ev_properties_get_pages):
	* shell/Makefile.am:
	* shell/ev-application.c:
	* shell/ev-job-xfer.c: (ev_job_xfer_run):
	* shell/ev-window.c: (ev_window_cmd_file_open),
	(ev_window_cmd_save_as):
	* thumbnailer/evince-thumbnailer.c: (evince_thumbnail_pngenc_get):

	Rework factory functions so they are placed now in a single
	file ev-document-factory.c
parent dda75bc2
2006-03-31 Nickolay V. Shmyrev <nshmyrev@yandex.ru>
* backend/ev-document-factory.c:
(ev_document_factory_get_from_mime),
(ev_document_factory_get_all_mime_types), (get_document_from_uri),
(ev_document_factory_get_document),
(file_filter_add_mime_list_and_free),
(ev_document_factory_add_filters):
* backend/ev-document-factory.h:
* properties/ev-properties-main.c: (ev_properties_get_pages):
* shell/Makefile.am:
* shell/ev-application.c:
* shell/ev-job-xfer.c: (ev_job_xfer_run):
* shell/ev-window.c: (ev_window_cmd_file_open),
(ev_window_cmd_save_as):
* thumbnailer/evince-thumbnailer.c: (evince_thumbnail_pngenc_get):
Rework factory functions so they are placed now in a single
file ev-document-factory.c
2006-03-28 Nickolay V. Shmyrev <nshmyrev@yandex.ru>
* shell/ev-window.c: (update_chrome_visibility):
......
......@@ -42,6 +42,11 @@
#endif
#include <string.h>
#include <glib/gi18n.h>
#include <libgnomevfs/gnome-vfs-mime-utils.h>
#include <libgnomevfs/gnome-vfs-file-info.h>
#include <libgnomevfs/gnome-vfs-ops.h>
#include <gtk/gtkfilechooserdialog.h>
typedef struct _EvDocumentType EvDocumentType;
struct _EvDocumentType
......@@ -137,40 +142,32 @@ mime_type_supported_by_gdk_pixbuf (const gchar *mime_type)
}
#endif
static GType
ev_document_type_get_from_mime (const char *mime_type)
EvDocument*
ev_document_factory_get_from_mime (const char *mime_type)
{
int i;
GType type = G_TYPE_INVALID;
EvDocument *document = NULL;
g_return_val_if_fail (mime_type, G_TYPE_INVALID);
for (i = 0; i < G_N_ELEMENTS (document_types); i++) {
if (strcmp (mime_type, document_types[i].mime_type) == 0) {
g_assert (document_types[i].document_type_factory_callback != NULL);
return document_types[i].document_type_factory_callback();
type = document_types[i].document_type_factory_callback();
break;
}
}
#ifdef ENABLE_PIXBUF
if (mime_type_supported_by_gdk_pixbuf (mime_type)) {
return pixbuf_document_get_type ();
if (type == G_TYPE_INVALID && mime_type_supported_by_gdk_pixbuf (mime_type)) {
type = pixbuf_document_get_type ();
}
#endif
return G_TYPE_INVALID;
}
EvDocument *
ev_document_factory_get_document (const char *mime_type)
{
GType type = G_TYPE_INVALID;
type = ev_document_type_get_from_mime (mime_type);
if (type != G_TYPE_INVALID) {
return g_object_new (type, NULL);
}
return NULL;
document = g_object_new (type, NULL);
}
return document;
}
EvBackend
......@@ -194,7 +191,7 @@ ev_document_factory_get_backend (EvDocument *document)
return 0;
}
GList *
static GList *
ev_document_factory_get_mime_types (EvBackend backend)
{
GList *types = NULL;
......@@ -215,7 +212,7 @@ ev_document_factory_get_mime_types (EvBackend backend)
return types;
}
GList *
static GList *
ev_document_factory_get_all_mime_types (void)
{
GList *types = NULL;
......@@ -231,3 +228,174 @@ ev_document_factory_get_all_mime_types (void)
return types;
}
static EvDocument *
get_document_from_uri (const char *uri, gboolean slow, gchar **mime_type, GError **error)
{
EvDocument *document = NULL;
GnomeVFSFileInfo *info;
GnomeVFSResult result;
info = gnome_vfs_file_info_new ();
result = gnome_vfs_get_file_info (uri, info,
GNOME_VFS_FILE_INFO_GET_MIME_TYPE |
GNOME_VFS_FILE_INFO_FOLLOW_LINKS |
(slow ? GNOME_VFS_FILE_INFO_FORCE_SLOW_MIME_TYPE : 0));
if (result != GNOME_VFS_OK) {
g_set_error (error,
EV_DOCUMENT_ERROR,
0,
gnome_vfs_result_to_string (result));
gnome_vfs_file_info_unref (info);
return NULL;
}
if (info->mime_type == NULL) {
g_set_error (error,
EV_DOCUMENT_ERROR,
0,
_("Unknown MIME Type"));
gnome_vfs_file_info_unref (info);
return NULL;
}
document = ev_document_factory_get_from_mime (info->mime_type);
if (document == NULL) {
g_set_error (error,
EV_DOCUMENT_ERROR,
0,
_("Unhandled MIME type: '%s'"), info->mime_type);
gnome_vfs_file_info_unref (info);
return NULL;
}
if (mime_type != NULL) {
*mime_type = g_strdup (info->mime_type);
}
gnome_vfs_file_info_unref (info);
return document;
}
EvDocument *
ev_document_factory_get_document (const char *uri, gchar **mime_type, GError **error)
{
EvDocument *document;
document = get_document_from_uri (uri, FALSE, mime_type, error);
if (document != NULL) {
return document;
}
if (error) {
g_error_free (*error);
*error = NULL;
}
document = get_document_from_uri (uri, TRUE, mime_type, error);
return document;
}
static void
file_filter_add_mime_list_and_free (GtkFileFilter *filter, GList *mime_types)
{
GList *l;
for (l = mime_types; l != NULL; l = l->next) {
gtk_file_filter_add_mime_type (filter, l->data);
}
g_list_foreach (mime_types, (GFunc)g_free, NULL);
g_list_free (mime_types);
}
void
ev_document_factory_add_filters (GtkWidget *chooser, EvDocument *document)
{
EvBackend backend = 0;
GList *mime_types;
GtkFileFilter *filter;
GtkFileFilter *default_filter;
GtkFileFilter *document_filter;
if (document != NULL) {
backend = ev_document_factory_get_backend (document);
}
default_filter = document_filter = filter = gtk_file_filter_new ();
gtk_file_filter_set_name (filter, _("All Documents"));
mime_types = ev_document_factory_get_all_mime_types ();
file_filter_add_mime_list_and_free (filter, mime_types);
gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter);
#ifdef ENABLE_PS
if (document == NULL || backend == EV_BACKEND_PS) {
default_filter = filter = gtk_file_filter_new ();
gtk_file_filter_set_name (filter, _("PostScript Documents"));
mime_types = ev_document_factory_get_mime_types (EV_BACKEND_PS);
file_filter_add_mime_list_and_free (filter, mime_types);
gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter);
}
#endif
if (document == NULL || backend == EV_BACKEND_PDF) {
default_filter = filter = gtk_file_filter_new ();
gtk_file_filter_set_name (filter, _("PDF Documents"));
mime_types = ev_document_factory_get_mime_types (EV_BACKEND_PDF);
file_filter_add_mime_list_and_free (filter, mime_types);
gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter);
}
#ifdef ENABLE_PIXBUF
if (document == NULL || backend == EV_BACKEND_PIXBUF) {
default_filter = filter = gtk_file_filter_new ();
gtk_file_filter_set_name (filter, _("Images"));
mime_types = ev_document_factory_get_mime_types (EV_BACKEND_PIXBUF);
file_filter_add_mime_list_and_free (filter, mime_types);
gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter);
}
#endif
#ifdef ENABLE_DVI
if (document == NULL || backend == EV_BACKEND_DVI) {
default_filter = filter = gtk_file_filter_new ();
gtk_file_filter_set_name (filter, _("DVI Documents"));
mime_types = ev_document_factory_get_mime_types (EV_BACKEND_DVI);
file_filter_add_mime_list_and_free (filter, mime_types);
gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter);
}
#endif
#ifdef ENABLE_DJVU
if (document == NULL || backend == EV_BACKEND_DJVU) {
default_filter = filter = gtk_file_filter_new ();
gtk_file_filter_set_name (filter, _("Djvu Documents"));
mime_types = ev_document_factory_get_mime_types (EV_BACKEND_DJVU);
file_filter_add_mime_list_and_free (filter, mime_types);
gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter);
}
#endif
#ifdef ENABLE_COMICS
if (document == NULL || backend == EV_BACKEND_COMICS) {
default_filter = filter = gtk_file_filter_new ();
gtk_file_filter_set_name (filter, _("Comic Books"));
mime_types = ev_document_factory_get_mime_types (EV_BACKEND_COMICS);
file_filter_add_mime_list_and_free (filter, mime_types);
gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter);
}
#endif
filter = gtk_file_filter_new ();
gtk_file_filter_set_name (filter, _("All Files"));
gtk_file_filter_add_pattern (filter, "*");
gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter);
gtk_file_chooser_set_filter (GTK_FILE_CHOOSER (chooser),
document == NULL ? document_filter : default_filter);
}
......@@ -21,6 +21,7 @@
#ifndef EV_DOCUMENT_FACTORY_H
#define EV_DOCUMENT_FACTORY_H
#include <gtk/gtk.h>
#include "ev-document.h"
G_BEGIN_DECLS
......@@ -35,10 +36,10 @@ typedef enum {
EV_BACKEND_COMICS
} EvBackend;
EvDocument *ev_document_factory_get_document (const char *mime_type);
EvBackend ev_document_factory_get_backend (EvDocument *document);
GList *ev_document_factory_get_mime_types (EvBackend backend);
GList *ev_document_factory_get_all_mime_types (void);
EvDocument* ev_document_factory_get_from_mime (const char *mime_type);
EvDocument* ev_document_factory_get_document (const char *uri, gchar **mime_type, GError **error);
EvBackend ev_document_factory_get_backend (EvDocument *document);
void ev_document_factory_add_filters (GtkWidget *chooser, EvDocument *document);
G_END_DECLS
......
......@@ -90,6 +90,7 @@ ev_properties_get_pages (NautilusPropertyPageProvider *provider,
char *uri = NULL;
GtkWidget *page, *label;
NautilusPropertyPage *property_page;
GType type = G_TYPE_INVALID;
/* only add properties page if a single file is selected */
if (files == NULL || files->next != NULL)
......@@ -98,7 +99,7 @@ ev_properties_get_pages (NautilusPropertyPageProvider *provider,
/* okay, make the page */
mime = nautilus_file_info_get_mime_type (file);
document = ev_document_factory_get_document (mime);
document = ev_document_factory_get_from_mime (mime);
g_free (mime);
if (document == NULL)
......
......@@ -20,8 +20,6 @@ evince_SOURCES= \
eggfindbar.h \
ev-application.c \
ev-application.h \
ev-document-types.c \
ev-document-types.h \
ev-job-queue.h \
ev-job-queue.c \
ev-jobs.h \
......
......@@ -26,8 +26,8 @@
#include "ev-application.h"
#include "ev-utils.h"
#include "ev-document-types.h"
#include "ev-file-helpers.h"
#include "ev-document-factory.h"
#include <glib.h>
#include <glib/gi18n.h>
......
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; c-indent-level: 8 -*- */
/*
* Copyright (C) 2005, Red Hat, 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, 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "ev-document-types.h"
#include "ev-document-factory.h"
#include <string.h>
#include <glib/gi18n.h>
#include <libgnomevfs/gnome-vfs-mime-utils.h>
#include <libgnomevfs/gnome-vfs-file-info.h>
#include <libgnomevfs/gnome-vfs-ops.h>
#include <gtk/gtkfilechooserdialog.h>
static EvDocument *
get_document_from_uri (const char *uri, gboolean slow, gchar **mime_type, GError **error)
{
EvDocument *document;
GnomeVFSFileInfo *info;
GnomeVFSResult result;
info = gnome_vfs_file_info_new ();
result = gnome_vfs_get_file_info (uri, info,
GNOME_VFS_FILE_INFO_GET_MIME_TYPE |
GNOME_VFS_FILE_INFO_FOLLOW_LINKS |
(slow ? GNOME_VFS_FILE_INFO_FORCE_SLOW_MIME_TYPE : 0));
if (result != GNOME_VFS_OK) {
g_set_error (error,
EV_DOCUMENT_ERROR,
0,
gnome_vfs_result_to_string (result));
gnome_vfs_file_info_unref (info);
return NULL;
}
if (info->mime_type == NULL) {
g_set_error (error,
EV_DOCUMENT_ERROR,
0,
_("Unknown MIME Type"));
gnome_vfs_file_info_unref (info);
return NULL;
}
document = ev_document_factory_get_document (info->mime_type);
if (document == NULL) {
g_set_error (error,
EV_DOCUMENT_ERROR,
0,
_("Unhandled MIME type: '%s'"), info->mime_type);
gnome_vfs_file_info_unref (info);
return NULL;
}
if (mime_type != NULL) {
*mime_type = g_strdup (info->mime_type);
}
gnome_vfs_file_info_unref (info);
return document;
}
EvDocument *
ev_document_types_get_document (const char *uri, gchar **mime_type, GError **error)
{
EvDocument *document;
document = get_document_from_uri (uri, FALSE, mime_type, error);
if (document != NULL) {
return document;
}
if (error) {
g_error_free (*error);
*error = NULL;
}
document = get_document_from_uri (uri, TRUE, mime_type, error);
return document;
}
static void
file_filter_add_mime_list_and_free (GtkFileFilter *filter, GList *mime_types)
{
GList *l;
for (l = mime_types; l != NULL; l = l->next) {
gtk_file_filter_add_mime_type (filter, l->data);
}
g_list_foreach (mime_types, (GFunc)g_free, NULL);
g_list_free (mime_types);
}
void
ev_document_types_add_filters (GtkWidget *chooser, EvDocument *document)
{
EvBackend backend = 0;
GList *mime_types;
GtkFileFilter *filter;
GtkFileFilter *default_filter;
GtkFileFilter *document_filter;
if (document != NULL) {
backend = ev_document_factory_get_backend (document);
}
default_filter = document_filter = filter = gtk_file_filter_new ();
gtk_file_filter_set_name (filter, _("All Documents"));
mime_types = ev_document_factory_get_all_mime_types ();
file_filter_add_mime_list_and_free (filter, mime_types);
gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter);
#ifdef ENABLE_PS
if (document == NULL || backend == EV_BACKEND_PS) {
default_filter = filter = gtk_file_filter_new ();
gtk_file_filter_set_name (filter, _("PostScript Documents"));
mime_types = ev_document_factory_get_mime_types (EV_BACKEND_PS);
file_filter_add_mime_list_and_free (filter, mime_types);
gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter);
}
#endif
if (document == NULL || backend == EV_BACKEND_PDF) {
default_filter = filter = gtk_file_filter_new ();
gtk_file_filter_set_name (filter, _("PDF Documents"));
mime_types = ev_document_factory_get_mime_types (EV_BACKEND_PDF);
file_filter_add_mime_list_and_free (filter, mime_types);
gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter);
}
#ifdef ENABLE_PIXBUF
if (document == NULL || backend == EV_BACKEND_PIXBUF) {
default_filter = filter = gtk_file_filter_new ();
gtk_file_filter_set_name (filter, _("Images"));
mime_types = ev_document_factory_get_mime_types (EV_BACKEND_PIXBUF);
file_filter_add_mime_list_and_free (filter, mime_types);
gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter);
}
#endif
#ifdef ENABLE_DVI
if (document == NULL || backend == EV_BACKEND_DVI) {
default_filter = filter = gtk_file_filter_new ();
gtk_file_filter_set_name (filter, _("DVI Documents"));
mime_types = ev_document_factory_get_mime_types (EV_BACKEND_DVI);
file_filter_add_mime_list_and_free (filter, mime_types);
gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter);
}
#endif
#ifdef ENABLE_DJVU
if (document == NULL || backend == EV_BACKEND_DJVU) {
default_filter = filter = gtk_file_filter_new ();
gtk_file_filter_set_name (filter, _("Djvu Documents"));
mime_types = ev_document_factory_get_mime_types (EV_BACKEND_DJVU);
file_filter_add_mime_list_and_free (filter, mime_types);
gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter);
}
#endif
#ifdef ENABLE_COMICS
if (document == NULL || backend == EV_BACKEND_COMICS) {
default_filter = filter = gtk_file_filter_new ();
gtk_file_filter_set_name (filter, _("Comic Books"));
mime_types = ev_document_factory_get_mime_types (EV_BACKEND_COMICS);
file_filter_add_mime_list_and_free (filter, mime_types);
gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter);
}
#endif
filter = gtk_file_filter_new ();
gtk_file_filter_set_name (filter, _("All Files"));
gtk_file_filter_add_pattern (filter, "*");
gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter);
gtk_file_chooser_set_filter (GTK_FILE_CHOOSER (chooser),
document == NULL ? document_filter : default_filter);
}
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; c-indent-level: 8 -*- */
/*
* Copyright (C) 2005, Red Hat, 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, 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*/
#ifndef EV_DOCUMENT_TYPES_H
#define EV_DOCUMENT_TYPES_H
#include "ev-document.h"
#include <gtk/gtk.h>
G_BEGIN_DECLS
EvDocument *ev_document_types_get_document (const char *uri,
gchar **mime_type,
GError **error);
void ev_document_types_add_filters (GtkWidget *chooser,
EvDocument *document);
G_END_DECLS
#endif
......@@ -23,8 +23,8 @@
#include "ev-job-xfer.h"
#include "ev-document-types.h"
#include "ev-file-helpers.h"
#include "ev-document-factory.h"
#include <glib/gi18n.h>
#include <glib.h>
......@@ -102,7 +102,7 @@ ev_job_xfer_run (EvJobXfer *job)
job->error = NULL;
}
document = ev_document_types_get_document (job->uri, NULL, &error);
document = ev_document_factory_get_document (job->uri, NULL, &error);
if (document != NULL) {
EV_JOB (job)->document = document;
......
......@@ -44,7 +44,7 @@
#include "ev-document-fonts.h"
#include "ev-document-find.h"
#include "ev-document-security.h"
#include "ev-document-types.h"
#include "ev-document-factory.h"
#include "ev-job-queue.h"
#include "ev-jobs.h"
#include "ev-sidebar-page.h"
......@@ -1162,7 +1162,7 @@ ev_window_cmd_file_open (GtkAction *action, EvWindow *window)
GTK_STOCK_OPEN, GTK_RESPONSE_OK,
NULL);
ev_document_types_add_filters (chooser, NULL);
ev_document_factory_add_filters (chooser, NULL);
gtk_file_chooser_set_select_multiple (GTK_FILE_CHOOSER (chooser), TRUE);
gtk_file_chooser_set_local_only (GTK_FILE_CHOOSER (chooser), FALSE);
if (ev_application_get_chooser_uri (EV_APP) != NULL)
......@@ -1283,7 +1283,7 @@ ev_window_cmd_save_as (GtkAction *action, EvWindow *ev_window)
GTK_STOCK_SAVE, GTK_RESPONSE_OK,
NULL);
ev_document_types_add_filters (fc, ev_window->priv->document);
ev_document_factory_add_filters (fc, ev_window->priv->document);
gtk_dialog_set_default_response (GTK_DIALOG (fc), GTK_RESPONSE_OK);
#ifdef HAVE_GTK_FILE_CHOOSER_SET_DO_OVERWRITE_CONFIRMATION
......
......@@ -30,32 +30,6 @@
#define THUMBNAIL_SIZE 128
static EvDocument *
get_document_from_uri (const char *uri, gboolean slow, gchar **mime_type)
{
EvDocument *document = NULL;
GnomeVFSFileInfo *info;
GnomeVFSResult result;
info = gnome_vfs_file_info_new ();
result = gnome_vfs_get_file_info (uri, info,
GNOME_VFS_FILE_INFO_GET_MIME_TYPE |
GNOME_VFS_FILE_INFO_FOLLOW_LINKS |
(slow ? GNOME_VFS_FILE_INFO_FORCE_SLOW_MIME_TYPE : 0));
if (result != GNOME_VFS_OK || info->mime_type == NULL) {
goto end;
}
document = ev_document_factory_get_document (info->mime_type);
if (mime_type != NULL) {
*mime_type = info->mime_type ? g_strdup (info->mime_type) : NULL;
}
end:
gnome_vfs_file_info_unref (info);
return document;
}
static gboolean
evince_thumbnail_pngenc_get (const char *uri, const char *thumbnail, int size)
{
......@@ -64,11 +38,9 @@ evince_thumbnail_pngenc_get (const char *uri, const char *thumbnail, int size)
GdkPixbuf *pixbuf;
char *mime_type = NULL;
document = get_document_from_uri (uri, FALSE, &mime_type);
if (document == NULL) {
document = get_document_from_uri (uri, TRUE, &mime_type);
}
if (document == NULL) {
document = ev_document_factory_get_document (uri, &mime_type, &error);
if (document == NULL || error) {
return FALSE;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment