2011-11-07 13:46:58 -06:00
|
|
|
/*
|
2011-11-07 16:52:18 -06:00
|
|
|
* pluma-gio-document-loader.c
|
|
|
|
* This file is part of pluma
|
2011-11-07 13:46:58 -06:00
|
|
|
*
|
|
|
|
* Copyright (C) 2005 - Paolo Maggi
|
|
|
|
* Copyright (C) 2007 - Paolo Maggi, Steve Frécinaux
|
|
|
|
* Copyright (C) 2008 - Jesse van den Kieboom
|
|
|
|
*
|
|
|
|
* 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
|
2012-11-18 19:54:49 -06:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
2011-11-07 13:46:58 -06:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2011-11-07 16:52:18 -06:00
|
|
|
* Modified by the pluma Team, 2005-2008. See the AUTHORS file for a
|
|
|
|
* list of people on the pluma Team.
|
2011-11-07 13:46:58 -06:00
|
|
|
* See the ChangeLog files for a list of changes.
|
|
|
|
*
|
|
|
|
* $Id$
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include <config.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <glib/gi18n.h>
|
|
|
|
#include <glib/gstdio.h>
|
|
|
|
#include <gio/gio.h>
|
|
|
|
|
2011-11-07 16:52:18 -06:00
|
|
|
#include "pluma-gio-document-loader.h"
|
|
|
|
#include "pluma-document-output-stream.h"
|
|
|
|
#include "pluma-smart-charset-converter.h"
|
|
|
|
#include "pluma-prefs-manager.h"
|
|
|
|
#include "pluma-debug.h"
|
|
|
|
#include "pluma-utils.h"
|
2011-11-07 13:46:58 -06:00
|
|
|
|
|
|
|
#ifndef ENABLE_GVFS_METADATA
|
2011-11-07 16:52:18 -06:00
|
|
|
#include "pluma-metadata-manager.h"
|
2011-11-07 13:46:58 -06:00
|
|
|
#endif
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
2011-11-07 16:52:18 -06:00
|
|
|
PlumaGioDocumentLoader *loader;
|
2011-11-07 13:46:58 -06:00
|
|
|
GCancellable *cancellable;
|
|
|
|
|
|
|
|
gssize read;
|
|
|
|
gboolean tried_mount;
|
|
|
|
} AsyncData;
|
|
|
|
|
|
|
|
#define READ_CHUNK_SIZE 8192
|
|
|
|
#define REMOTE_QUERY_ATTRIBUTES G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE "," \
|
|
|
|
G_FILE_ATTRIBUTE_STANDARD_TYPE "," \
|
|
|
|
G_FILE_ATTRIBUTE_TIME_MODIFIED "," \
|
|
|
|
G_FILE_ATTRIBUTE_STANDARD_SIZE "," \
|
|
|
|
G_FILE_ATTRIBUTE_ACCESS_CAN_WRITE "," \
|
2011-11-07 16:52:18 -06:00
|
|
|
PLUMA_METADATA_ATTRIBUTE_ENCODING
|
2011-11-07 13:46:58 -06:00
|
|
|
|
2011-11-07 16:52:18 -06:00
|
|
|
#define PLUMA_GIO_DOCUMENT_LOADER_GET_PRIVATE(object) \
|
2011-11-07 13:46:58 -06:00
|
|
|
(G_TYPE_INSTANCE_GET_PRIVATE ((object), \
|
2011-11-07 16:52:18 -06:00
|
|
|
PLUMA_TYPE_GIO_DOCUMENT_LOADER, \
|
|
|
|
PlumaGioDocumentLoaderPrivate))
|
2011-11-07 13:46:58 -06:00
|
|
|
|
2011-11-07 16:52:18 -06:00
|
|
|
static void pluma_gio_document_loader_load (PlumaDocumentLoader *loader);
|
|
|
|
static gboolean pluma_gio_document_loader_cancel (PlumaDocumentLoader *loader);
|
|
|
|
static goffset pluma_gio_document_loader_get_bytes_read (PlumaDocumentLoader *loader);
|
2011-11-07 13:46:58 -06:00
|
|
|
|
|
|
|
static void open_async_read (AsyncData *async);
|
|
|
|
|
2011-11-07 16:52:18 -06:00
|
|
|
struct _PlumaGioDocumentLoaderPrivate
|
2011-11-07 13:46:58 -06:00
|
|
|
{
|
|
|
|
/* Info on the current file */
|
|
|
|
GFile *gfile;
|
|
|
|
|
|
|
|
goffset bytes_read;
|
|
|
|
|
|
|
|
/* Handle for remote files */
|
|
|
|
GCancellable *cancellable;
|
|
|
|
GInputStream *stream;
|
|
|
|
GOutputStream *output;
|
2011-11-07 16:52:18 -06:00
|
|
|
PlumaSmartCharsetConverter *converter;
|
2011-11-07 13:46:58 -06:00
|
|
|
|
|
|
|
gchar buffer[READ_CHUNK_SIZE];
|
|
|
|
|
|
|
|
GError *error;
|
|
|
|
};
|
|
|
|
|
2011-11-07 16:52:18 -06:00
|
|
|
G_DEFINE_TYPE(PlumaGioDocumentLoader, pluma_gio_document_loader, PLUMA_TYPE_DOCUMENT_LOADER)
|
2011-11-07 13:46:58 -06:00
|
|
|
|
|
|
|
static void
|
2011-11-07 16:52:18 -06:00
|
|
|
pluma_gio_document_loader_dispose (GObject *object)
|
2011-11-07 13:46:58 -06:00
|
|
|
{
|
2011-11-07 16:52:18 -06:00
|
|
|
PlumaGioDocumentLoaderPrivate *priv;
|
2011-11-07 13:46:58 -06:00
|
|
|
|
2011-11-07 16:52:18 -06:00
|
|
|
priv = PLUMA_GIO_DOCUMENT_LOADER (object)->priv;
|
2011-11-07 13:46:58 -06:00
|
|
|
|
|
|
|
if (priv->cancellable != NULL)
|
|
|
|
{
|
|
|
|
g_cancellable_cancel (priv->cancellable);
|
|
|
|
g_object_unref (priv->cancellable);
|
|
|
|
priv->cancellable = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (priv->stream != NULL)
|
|
|
|
{
|
|
|
|
g_object_unref (priv->stream);
|
|
|
|
priv->stream = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (priv->output != NULL)
|
|
|
|
{
|
|
|
|
g_object_unref (priv->output);
|
|
|
|
priv->output = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (priv->converter != NULL)
|
|
|
|
{
|
|
|
|
g_object_unref (priv->converter);
|
|
|
|
priv->converter = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (priv->gfile != NULL)
|
|
|
|
{
|
|
|
|
g_object_unref (priv->gfile);
|
|
|
|
priv->gfile = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (priv->error != NULL)
|
|
|
|
{
|
|
|
|
g_error_free (priv->error);
|
|
|
|
priv->error = NULL;
|
|
|
|
}
|
|
|
|
|
2011-11-07 16:52:18 -06:00
|
|
|
G_OBJECT_CLASS (pluma_gio_document_loader_parent_class)->dispose (object);
|
2011-11-07 13:46:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-11-07 16:52:18 -06:00
|
|
|
pluma_gio_document_loader_class_init (PlumaGioDocumentLoaderClass *klass)
|
2011-11-07 13:46:58 -06:00
|
|
|
{
|
|
|
|
GObjectClass *object_class = G_OBJECT_CLASS (klass);
|
2011-11-07 16:52:18 -06:00
|
|
|
PlumaDocumentLoaderClass *loader_class = PLUMA_DOCUMENT_LOADER_CLASS (klass);
|
2011-11-07 13:46:58 -06:00
|
|
|
|
2011-11-07 16:52:18 -06:00
|
|
|
object_class->dispose = pluma_gio_document_loader_dispose;
|
2011-11-07 13:46:58 -06:00
|
|
|
|
2011-11-07 16:52:18 -06:00
|
|
|
loader_class->load = pluma_gio_document_loader_load;
|
|
|
|
loader_class->cancel = pluma_gio_document_loader_cancel;
|
|
|
|
loader_class->get_bytes_read = pluma_gio_document_loader_get_bytes_read;
|
2011-11-07 13:46:58 -06:00
|
|
|
|
2011-11-07 16:52:18 -06:00
|
|
|
g_type_class_add_private (object_class, sizeof(PlumaGioDocumentLoaderPrivate));
|
2011-11-07 13:46:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-11-07 16:52:18 -06:00
|
|
|
pluma_gio_document_loader_init (PlumaGioDocumentLoader *gvloader)
|
2011-11-07 13:46:58 -06:00
|
|
|
{
|
2011-11-07 16:52:18 -06:00
|
|
|
gvloader->priv = PLUMA_GIO_DOCUMENT_LOADER_GET_PRIVATE (gvloader);
|
2011-11-07 13:46:58 -06:00
|
|
|
|
|
|
|
gvloader->priv->converter = NULL;
|
|
|
|
gvloader->priv->error = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static AsyncData *
|
2011-11-07 16:52:18 -06:00
|
|
|
async_data_new (PlumaGioDocumentLoader *gvloader)
|
2011-11-07 13:46:58 -06:00
|
|
|
{
|
|
|
|
AsyncData *async;
|
|
|
|
|
|
|
|
async = g_slice_new (AsyncData);
|
|
|
|
async->loader = gvloader;
|
|
|
|
async->cancellable = g_object_ref (gvloader->priv->cancellable);
|
|
|
|
async->tried_mount = FALSE;
|
|
|
|
|
|
|
|
return async;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
async_data_free (AsyncData *async)
|
|
|
|
{
|
|
|
|
g_object_unref (async->cancellable);
|
|
|
|
g_slice_free (AsyncData, async);
|
|
|
|
}
|
|
|
|
|
2011-11-07 16:52:18 -06:00
|
|
|
static const PlumaEncoding *
|
|
|
|
get_metadata_encoding (PlumaDocumentLoader *loader)
|
2011-11-07 13:46:58 -06:00
|
|
|
{
|
2011-11-07 16:52:18 -06:00
|
|
|
const PlumaEncoding *enc = NULL;
|
2011-11-07 13:46:58 -06:00
|
|
|
|
|
|
|
#ifndef ENABLE_GVFS_METADATA
|
|
|
|
gchar *charset;
|
|
|
|
const gchar *uri;
|
|
|
|
|
2011-11-07 16:52:18 -06:00
|
|
|
uri = pluma_document_loader_get_uri (loader);
|
2011-11-07 13:46:58 -06:00
|
|
|
|
2011-11-07 16:52:18 -06:00
|
|
|
charset = pluma_metadata_manager_get (uri, "encoding");
|
2011-11-07 13:46:58 -06:00
|
|
|
|
|
|
|
if (charset == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2011-11-07 16:52:18 -06:00
|
|
|
enc = pluma_encoding_get_from_charset (charset);
|
2011-11-07 13:46:58 -06:00
|
|
|
|
|
|
|
g_free (charset);
|
|
|
|
#else
|
|
|
|
GFileInfo *info;
|
|
|
|
|
2011-11-07 16:52:18 -06:00
|
|
|
info = pluma_document_loader_get_info (loader);
|
2011-11-07 13:46:58 -06:00
|
|
|
|
|
|
|
/* check if the encoding was set in the metadata */
|
2011-11-07 16:52:18 -06:00
|
|
|
if (g_file_info_has_attribute (info, PLUMA_METADATA_ATTRIBUTE_ENCODING))
|
2011-11-07 13:46:58 -06:00
|
|
|
{
|
|
|
|
const gchar *charset;
|
|
|
|
|
|
|
|
charset = g_file_info_get_attribute_string (info,
|
2011-11-07 16:52:18 -06:00
|
|
|
PLUMA_METADATA_ATTRIBUTE_ENCODING);
|
2011-11-07 13:46:58 -06:00
|
|
|
|
|
|
|
if (charset == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2011-11-07 16:52:18 -06:00
|
|
|
enc = pluma_encoding_get_from_charset (charset);
|
2011-11-07 13:46:58 -06:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return enc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-11-07 16:52:18 -06:00
|
|
|
remote_load_completed_or_failed (PlumaGioDocumentLoader *loader, AsyncData *async)
|
2011-11-07 13:46:58 -06:00
|
|
|
{
|
2011-11-07 16:52:18 -06:00
|
|
|
pluma_document_loader_loading (PLUMA_DOCUMENT_LOADER (loader),
|
2011-11-07 13:46:58 -06:00
|
|
|
TRUE,
|
|
|
|
loader->priv->error);
|
|
|
|
|
|
|
|
if (async)
|
|
|
|
async_data_free (async);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
async_failed (AsyncData *async, GError *error)
|
|
|
|
{
|
|
|
|
g_propagate_error (&async->loader->priv->error, error);
|
|
|
|
remote_load_completed_or_failed (async->loader, async);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
close_input_stream_ready_cb (GInputStream *stream,
|
|
|
|
GAsyncResult *res,
|
|
|
|
AsyncData *async)
|
|
|
|
{
|
|
|
|
GError *error = NULL;
|
|
|
|
|
2011-11-07 16:52:18 -06:00
|
|
|
pluma_debug (DEBUG_LOADER);
|
2011-11-07 13:46:58 -06:00
|
|
|
|
|
|
|
/* check cancelled state manually */
|
|
|
|
if (g_cancellable_is_cancelled (async->cancellable))
|
|
|
|
{
|
|
|
|
async_data_free (async);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-11-07 16:52:18 -06:00
|
|
|
pluma_debug_message (DEBUG_SAVER, "Finished closing input stream");
|
2011-11-07 13:46:58 -06:00
|
|
|
|
|
|
|
if (!g_input_stream_close_finish (stream, res, &error))
|
|
|
|
{
|
2011-11-07 16:52:18 -06:00
|
|
|
pluma_debug_message (DEBUG_SAVER, "Closing input stream error: %s", error->message);
|
2011-11-07 13:46:58 -06:00
|
|
|
|
|
|
|
async_failed (async, error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-11-07 16:52:18 -06:00
|
|
|
pluma_debug_message (DEBUG_SAVER, "Close output stream");
|
2011-11-07 13:46:58 -06:00
|
|
|
if (!g_output_stream_close (async->loader->priv->output,
|
|
|
|
async->cancellable, &error))
|
|
|
|
{
|
|
|
|
async_failed (async, error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
remote_load_completed_or_failed (async->loader, async);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
write_complete (AsyncData *async)
|
|
|
|
{
|
2011-11-07 16:52:18 -06:00
|
|
|
PlumaDocumentLoader *loader;
|
2011-11-07 13:46:58 -06:00
|
|
|
|
2011-11-07 16:52:18 -06:00
|
|
|
loader = PLUMA_DOCUMENT_LOADER (async->loader);
|
2011-11-07 13:46:58 -06:00
|
|
|
|
|
|
|
if (async->loader->priv->stream)
|
|
|
|
g_input_stream_close_async (G_INPUT_STREAM (async->loader->priv->stream),
|
|
|
|
G_PRIORITY_HIGH,
|
|
|
|
async->cancellable,
|
|
|
|
(GAsyncReadyCallback)close_input_stream_ready_cb,
|
|
|
|
async);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* prototype, because they call each other... isn't C lovely */
|
|
|
|
static void read_file_chunk (AsyncData *async);
|
|
|
|
|
|
|
|
static void
|
|
|
|
write_file_chunk (AsyncData *async)
|
|
|
|
{
|
2011-11-07 16:52:18 -06:00
|
|
|
PlumaGioDocumentLoader *gvloader;
|
2011-11-07 13:46:58 -06:00
|
|
|
gssize bytes_written;
|
|
|
|
GError *error = NULL;
|
|
|
|
|
|
|
|
gvloader = async->loader;
|
|
|
|
|
|
|
|
/* we use sync methods on doc stream since it is in memory. Using async
|
|
|
|
would be racy and we can endup with invalidated iters */
|
|
|
|
bytes_written = g_output_stream_write (G_OUTPUT_STREAM (gvloader->priv->output),
|
|
|
|
gvloader->priv->buffer,
|
|
|
|
async->read,
|
|
|
|
async->cancellable,
|
|
|
|
&error);
|
|
|
|
|
2011-11-07 16:52:18 -06:00
|
|
|
pluma_debug_message (DEBUG_SAVER, "Written: %" G_GSSIZE_FORMAT, bytes_written);
|
2011-11-07 13:46:58 -06:00
|
|
|
if (bytes_written == -1)
|
|
|
|
{
|
2011-11-07 16:52:18 -06:00
|
|
|
pluma_debug_message (DEBUG_SAVER, "Write error: %s", error->message);
|
2011-11-07 13:46:58 -06:00
|
|
|
async_failed (async, error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* note that this signal blocks the read... check if it isn't
|
|
|
|
* a performance problem
|
|
|
|
*/
|
2011-11-07 16:52:18 -06:00
|
|
|
pluma_document_loader_loading (PLUMA_DOCUMENT_LOADER (gvloader),
|
2011-11-07 13:46:58 -06:00
|
|
|
FALSE,
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
read_file_chunk (async);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
async_read_cb (GInputStream *stream,
|
|
|
|
GAsyncResult *res,
|
|
|
|
AsyncData *async)
|
|
|
|
{
|
2011-11-07 16:52:18 -06:00
|
|
|
pluma_debug (DEBUG_LOADER);
|
|
|
|
PlumaGioDocumentLoader *gvloader;
|
2011-11-07 13:46:58 -06:00
|
|
|
GError *error = NULL;
|
|
|
|
|
2011-11-07 16:52:18 -06:00
|
|
|
pluma_debug (DEBUG_LOADER);
|
2011-11-07 13:46:58 -06:00
|
|
|
|
|
|
|
/* manually check cancelled state */
|
|
|
|
if (g_cancellable_is_cancelled (async->cancellable))
|
|
|
|
{
|
|
|
|
async_data_free (async);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
gvloader = async->loader;
|
|
|
|
|
|
|
|
async->read = g_input_stream_read_finish (stream, res, &error);
|
|
|
|
|
|
|
|
/* error occurred */
|
|
|
|
if (async->read == -1)
|
|
|
|
{
|
|
|
|
async_failed (async, error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for the extremely unlikely case where the file size overflows. */
|
|
|
|
if (gvloader->priv->bytes_read + async->read < gvloader->priv->bytes_read)
|
|
|
|
{
|
|
|
|
g_set_error (&gvloader->priv->error,
|
2011-11-07 16:52:18 -06:00
|
|
|
PLUMA_DOCUMENT_ERROR,
|
|
|
|
PLUMA_DOCUMENT_ERROR_TOO_BIG,
|
2011-11-07 13:46:58 -06:00
|
|
|
"File too big");
|
|
|
|
|
|
|
|
async_failed (async, gvloader->priv->error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Bump the size. */
|
|
|
|
gvloader->priv->bytes_read += async->read;
|
|
|
|
|
|
|
|
/* end of the file, we are done! */
|
|
|
|
if (async->read == 0)
|
|
|
|
{
|
2011-11-07 16:52:18 -06:00
|
|
|
PlumaDocumentLoader *loader;
|
2011-11-07 13:46:58 -06:00
|
|
|
|
2011-11-07 16:52:18 -06:00
|
|
|
loader = PLUMA_DOCUMENT_LOADER (gvloader);
|
2011-11-07 13:46:58 -06:00
|
|
|
|
2015-10-09 06:57:06 -05:00
|
|
|
g_output_stream_flush (gvloader->priv->output,
|
|
|
|
NULL,
|
|
|
|
&gvloader->priv->error);
|
|
|
|
|
2011-11-07 13:46:58 -06:00
|
|
|
loader->auto_detected_encoding =
|
2011-11-07 16:52:18 -06:00
|
|
|
pluma_smart_charset_converter_get_guessed (gvloader->priv->converter);
|
2011-11-07 13:46:58 -06:00
|
|
|
|
|
|
|
loader->auto_detected_newline_type =
|
2011-11-07 16:52:18 -06:00
|
|
|
pluma_document_output_stream_detect_newline_type (PLUMA_DOCUMENT_OUTPUT_STREAM (gvloader->priv->output));
|
2011-11-07 13:46:58 -06:00
|
|
|
|
|
|
|
/* Check if we needed some fallback char, if so, check if there was
|
|
|
|
a previous error and if not set a fallback used error */
|
|
|
|
/* FIXME Uncomment this when we want to manage conversion fallback */
|
2011-11-07 16:52:18 -06:00
|
|
|
/*if ((pluma_smart_charset_converter_get_num_fallbacks (gvloader->priv->converter) != 0) &&
|
2011-11-07 13:46:58 -06:00
|
|
|
gvloader->priv->error == NULL)
|
|
|
|
{
|
|
|
|
g_set_error_literal (&gvloader->priv->error,
|
2011-11-07 16:52:18 -06:00
|
|
|
PLUMA_DOCUMENT_ERROR,
|
|
|
|
PLUMA_DOCUMENT_ERROR_CONVERSION_FALLBACK,
|
2011-11-07 13:46:58 -06:00
|
|
|
"There was a conversion error and it was "
|
|
|
|
"needed to use a fallback char");
|
|
|
|
}*/
|
|
|
|
|
|
|
|
write_complete (async);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
write_file_chunk (async);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
read_file_chunk (AsyncData *async)
|
|
|
|
{
|
2011-11-07 16:52:18 -06:00
|
|
|
PlumaGioDocumentLoader *gvloader;
|
2011-11-07 13:46:58 -06:00
|
|
|
|
|
|
|
gvloader = async->loader;
|
|
|
|
|
|
|
|
g_input_stream_read_async (G_INPUT_STREAM (gvloader->priv->stream),
|
|
|
|
gvloader->priv->buffer,
|
|
|
|
READ_CHUNK_SIZE,
|
|
|
|
G_PRIORITY_HIGH,
|
|
|
|
async->cancellable,
|
|
|
|
(GAsyncReadyCallback) async_read_cb,
|
|
|
|
async);
|
|
|
|
}
|
|
|
|
|
|
|
|
static GSList *
|
2011-11-07 16:52:18 -06:00
|
|
|
get_candidate_encodings (PlumaGioDocumentLoader *gvloader)
|
2011-11-07 13:46:58 -06:00
|
|
|
{
|
2011-11-07 16:52:18 -06:00
|
|
|
const PlumaEncoding *metadata;
|
2011-11-07 13:46:58 -06:00
|
|
|
GSList *encodings = NULL;
|
|
|
|
|
2011-11-07 16:52:18 -06:00
|
|
|
encodings = pluma_prefs_manager_get_auto_detected_encodings ();
|
2011-11-07 13:46:58 -06:00
|
|
|
|
2011-11-07 16:52:18 -06:00
|
|
|
metadata = get_metadata_encoding (PLUMA_DOCUMENT_LOADER (gvloader));
|
2011-11-07 13:46:58 -06:00
|
|
|
if (metadata != NULL)
|
|
|
|
{
|
|
|
|
encodings = g_slist_prepend (encodings, (gpointer)metadata);
|
|
|
|
}
|
|
|
|
|
|
|
|
return encodings;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
finish_query_info (AsyncData *async)
|
|
|
|
{
|
2011-11-07 16:52:18 -06:00
|
|
|
PlumaGioDocumentLoader *gvloader;
|
|
|
|
PlumaDocumentLoader *loader;
|
2011-11-07 13:46:58 -06:00
|
|
|
GInputStream *conv_stream;
|
|
|
|
GFileInfo *info;
|
|
|
|
GSList *candidate_encodings;
|
|
|
|
|
|
|
|
gvloader = async->loader;
|
2011-11-07 16:52:18 -06:00
|
|
|
loader = PLUMA_DOCUMENT_LOADER (gvloader);
|
2011-11-07 13:46:58 -06:00
|
|
|
info = loader->info;
|
|
|
|
|
|
|
|
/* if it's not a regular file, error out... */
|
|
|
|
if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_TYPE) &&
|
|
|
|
g_file_info_get_file_type (info) != G_FILE_TYPE_REGULAR)
|
|
|
|
{
|
|
|
|
g_set_error (&gvloader->priv->error,
|
|
|
|
G_IO_ERROR,
|
|
|
|
G_IO_ERROR_NOT_REGULAR_FILE,
|
|
|
|
"Not a regular file");
|
|
|
|
|
|
|
|
remote_load_completed_or_failed (gvloader, async);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the candidate encodings */
|
|
|
|
if (loader->encoding == NULL)
|
|
|
|
{
|
|
|
|
candidate_encodings = get_candidate_encodings (gvloader);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
candidate_encodings = g_slist_prepend (NULL, (gpointer) loader->encoding);
|
|
|
|
}
|
|
|
|
|
2011-11-07 16:52:18 -06:00
|
|
|
gvloader->priv->converter = pluma_smart_charset_converter_new (candidate_encodings);
|
2011-11-07 13:46:58 -06:00
|
|
|
g_slist_free (candidate_encodings);
|
|
|
|
|
|
|
|
conv_stream = g_converter_input_stream_new (gvloader->priv->stream,
|
|
|
|
G_CONVERTER (gvloader->priv->converter));
|
|
|
|
g_object_unref (gvloader->priv->stream);
|
|
|
|
|
|
|
|
gvloader->priv->stream = conv_stream;
|
|
|
|
|
|
|
|
/* Output stream */
|
2011-11-07 16:52:18 -06:00
|
|
|
gvloader->priv->output = pluma_document_output_stream_new (loader->document);
|
2011-11-07 13:46:58 -06:00
|
|
|
|
|
|
|
/* start reading */
|
|
|
|
read_file_chunk (async);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
query_info_cb (GFile *source,
|
|
|
|
GAsyncResult *res,
|
|
|
|
AsyncData *async)
|
|
|
|
{
|
2011-11-07 16:52:18 -06:00
|
|
|
PlumaGioDocumentLoader *gvloader;
|
2011-11-07 13:46:58 -06:00
|
|
|
GFileInfo *info;
|
|
|
|
GError *error = NULL;
|
|
|
|
|
2011-11-07 16:52:18 -06:00
|
|
|
pluma_debug (DEBUG_LOADER);
|
2011-11-07 13:46:58 -06:00
|
|
|
|
|
|
|
/* manually check the cancelled state */
|
|
|
|
if (g_cancellable_is_cancelled (async->cancellable))
|
|
|
|
{
|
|
|
|
async_data_free (async);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
gvloader = async->loader;
|
|
|
|
|
|
|
|
/* finish the info query */
|
|
|
|
info = g_file_query_info_finish (gvloader->priv->gfile,
|
|
|
|
res,
|
|
|
|
&error);
|
|
|
|
|
|
|
|
if (info == NULL)
|
|
|
|
{
|
|
|
|
/* propagate the error and clean up */
|
|
|
|
async_failed (async, error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-11-07 16:52:18 -06:00
|
|
|
PLUMA_DOCUMENT_LOADER (gvloader)->info = info;
|
2011-11-07 13:46:58 -06:00
|
|
|
|
|
|
|
finish_query_info (async);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
mount_ready_callback (GFile *file,
|
|
|
|
GAsyncResult *res,
|
|
|
|
AsyncData *async)
|
|
|
|
{
|
|
|
|
GError *error = NULL;
|
|
|
|
gboolean mounted;
|
|
|
|
|
2011-11-07 16:52:18 -06:00
|
|
|
pluma_debug (DEBUG_LOADER);
|
2011-11-07 13:46:58 -06:00
|
|
|
|
|
|
|
/* manual check for cancelled state */
|
|
|
|
if (g_cancellable_is_cancelled (async->cancellable))
|
|
|
|
{
|
|
|
|
async_data_free (async);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mounted = g_file_mount_enclosing_volume_finish (file, res, &error);
|
|
|
|
|
|
|
|
if (!mounted)
|
|
|
|
{
|
|
|
|
async_failed (async, error);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* try again to open the file for reading */
|
|
|
|
open_async_read (async);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
recover_not_mounted (AsyncData *async)
|
|
|
|
{
|
2011-11-07 16:52:18 -06:00
|
|
|
PlumaDocument *doc;
|
2011-11-07 13:46:58 -06:00
|
|
|
GMountOperation *mount_operation;
|
|
|
|
|
2011-11-07 16:52:18 -06:00
|
|
|
pluma_debug (DEBUG_LOADER);
|
2011-11-07 13:46:58 -06:00
|
|
|
|
2011-11-07 16:52:18 -06:00
|
|
|
doc = pluma_document_loader_get_document (PLUMA_DOCUMENT_LOADER (async->loader));
|
|
|
|
mount_operation = _pluma_document_create_mount_operation (doc);
|
2011-11-07 13:46:58 -06:00
|
|
|
|
|
|
|
async->tried_mount = TRUE;
|
|
|
|
g_file_mount_enclosing_volume (async->loader->priv->gfile,
|
|
|
|
G_MOUNT_MOUNT_NONE,
|
|
|
|
mount_operation,
|
|
|
|
async->cancellable,
|
|
|
|
(GAsyncReadyCallback) mount_ready_callback,
|
|
|
|
async);
|
|
|
|
|
|
|
|
g_object_unref (mount_operation);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
async_read_ready_callback (GObject *source,
|
|
|
|
GAsyncResult *res,
|
|
|
|
AsyncData *async)
|
|
|
|
{
|
|
|
|
GError *error = NULL;
|
2011-11-07 16:52:18 -06:00
|
|
|
PlumaGioDocumentLoader *gvloader;
|
2011-11-07 13:46:58 -06:00
|
|
|
|
2011-11-07 16:52:18 -06:00
|
|
|
pluma_debug (DEBUG_LOADER);
|
2011-11-07 13:46:58 -06:00
|
|
|
|
|
|
|
/* manual check for cancelled state */
|
|
|
|
if (g_cancellable_is_cancelled (async->cancellable))
|
|
|
|
{
|
|
|
|
async_data_free (async);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
gvloader = async->loader;
|
|
|
|
|
|
|
|
gvloader->priv->stream = G_INPUT_STREAM (g_file_read_finish (gvloader->priv->gfile,
|
|
|
|
res, &error));
|
|
|
|
|
|
|
|
if (!gvloader->priv->stream)
|
|
|
|
{
|
|
|
|
if (error->code == G_IO_ERROR_NOT_MOUNTED && !async->tried_mount)
|
|
|
|
{
|
|
|
|
recover_not_mounted (async);
|
|
|
|
g_error_free (error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Propagate error */
|
|
|
|
g_propagate_error (&gvloader->priv->error, error);
|
2011-11-07 16:52:18 -06:00
|
|
|
pluma_document_loader_loading (PLUMA_DOCUMENT_LOADER (gvloader),
|
2011-11-07 13:46:58 -06:00
|
|
|
TRUE,
|
|
|
|
gvloader->priv->error);
|
|
|
|
|
|
|
|
async_data_free (async);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get the file info: note we cannot use
|
|
|
|
* g_file_input_stream_query_info_async since it is not able to get the
|
|
|
|
* content type etc, beside it is not supported by gvfs.
|
|
|
|
* Using the file instead of the stream is slightly racy, but for
|
|
|
|
* loading this is not too bad...
|
|
|
|
*/
|
|
|
|
g_file_query_info_async (gvloader->priv->gfile,
|
|
|
|
REMOTE_QUERY_ATTRIBUTES,
|
|
|
|
G_FILE_QUERY_INFO_NONE,
|
|
|
|
G_PRIORITY_HIGH,
|
|
|
|
async->cancellable,
|
|
|
|
(GAsyncReadyCallback) query_info_cb,
|
|
|
|
async);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
open_async_read (AsyncData *async)
|
|
|
|
{
|
|
|
|
g_file_read_async (async->loader->priv->gfile,
|
|
|
|
G_PRIORITY_HIGH,
|
|
|
|
async->cancellable,
|
|
|
|
(GAsyncReadyCallback) async_read_ready_callback,
|
|
|
|
async);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-11-07 16:52:18 -06:00
|
|
|
pluma_gio_document_loader_load (PlumaDocumentLoader *loader)
|
2011-11-07 13:46:58 -06:00
|
|
|
{
|
2011-11-07 16:52:18 -06:00
|
|
|
PlumaGioDocumentLoader *gvloader = PLUMA_GIO_DOCUMENT_LOADER (loader);
|
2011-11-07 13:46:58 -06:00
|
|
|
AsyncData *async;
|
|
|
|
|
2011-11-07 16:52:18 -06:00
|
|
|
pluma_debug (DEBUG_LOADER);
|
2011-11-07 13:46:58 -06:00
|
|
|
|
|
|
|
/* make sure no load operation is currently running */
|
|
|
|
g_return_if_fail (gvloader->priv->cancellable == NULL);
|
|
|
|
|
|
|
|
gvloader->priv->gfile = g_file_new_for_uri (loader->uri);
|
|
|
|
|
|
|
|
/* loading start */
|
2011-11-07 16:52:18 -06:00
|
|
|
pluma_document_loader_loading (PLUMA_DOCUMENT_LOADER (gvloader),
|
2011-11-07 13:46:58 -06:00
|
|
|
FALSE,
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
gvloader->priv->cancellable = g_cancellable_new ();
|
|
|
|
async = async_data_new (gvloader);
|
|
|
|
|
|
|
|
open_async_read (async);
|
|
|
|
}
|
|
|
|
|
|
|
|
static goffset
|
2011-11-07 16:52:18 -06:00
|
|
|
pluma_gio_document_loader_get_bytes_read (PlumaDocumentLoader *loader)
|
2011-11-07 13:46:58 -06:00
|
|
|
{
|
2011-11-07 16:52:18 -06:00
|
|
|
return PLUMA_GIO_DOCUMENT_LOADER (loader)->priv->bytes_read;
|
2011-11-07 13:46:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2011-11-07 16:52:18 -06:00
|
|
|
pluma_gio_document_loader_cancel (PlumaDocumentLoader *loader)
|
2011-11-07 13:46:58 -06:00
|
|
|
{
|
2011-11-07 16:52:18 -06:00
|
|
|
PlumaGioDocumentLoader *gvloader = PLUMA_GIO_DOCUMENT_LOADER (loader);
|
2011-11-07 13:46:58 -06:00
|
|
|
|
|
|
|
if (gvloader->priv->cancellable == NULL)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
g_cancellable_cancel (gvloader->priv->cancellable);
|
|
|
|
|
|
|
|
g_set_error (&gvloader->priv->error,
|
|
|
|
G_IO_ERROR,
|
|
|
|
G_IO_ERROR_CANCELLED,
|
|
|
|
"Operation cancelled");
|
|
|
|
|
|
|
|
remote_load_completed_or_failed (gvloader, NULL);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|