xed/xedit/xedit-document-saver.c

360 lines
9.2 KiB
C
Raw Permalink Normal View History

2011-11-07 13:46:58 -06:00
/*
2016-01-25 08:13:49 -06:00
* xedit-document-saver.c
* This file is part of xedit
2011-11-07 13:46:58 -06:00
*
* Copyright (C) 2005-2006 - Paolo Borelli and Paolo Maggi
* Copyright (C) 2007 - Paolo Borelli, Paolo Maggi, Steve Frécinaux
*
* 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
*/
/*
2016-01-25 08:13:49 -06:00
* Modified by the xedit Team, 2005-2006. See the AUTHORS file for a
* list of people on the xedit Team.
2011-11-07 13:46:58 -06:00
* See the ChangeLog files for a list of changes.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <glib/gi18n.h>
2016-01-25 08:13:49 -06:00
#include "xedit-document-saver.h"
#include "xedit-debug.h"
#include "xedit-prefs-manager.h"
#include "xedit-marshal.h"
#include "xedit-utils.h"
#include "xedit-enum-types.h"
#include "xedit-gio-document-saver.h"
2011-11-07 13:46:58 -06:00
2016-01-25 08:13:49 -06:00
G_DEFINE_ABSTRACT_TYPE(XeditDocumentSaver, xedit_document_saver, G_TYPE_OBJECT)
2011-11-07 13:46:58 -06:00
/* Signals */
enum {
SAVING,
LAST_SIGNAL
};
static guint signals[LAST_SIGNAL] = { 0 };
/* Properties */
enum {
PROP_0,
PROP_DOCUMENT,
PROP_URI,
PROP_ENCODING,
PROP_NEWLINE_TYPE,
PROP_FLAGS
};
static void
2016-01-25 08:13:49 -06:00
xedit_document_saver_set_property (GObject *object,
2011-11-07 13:46:58 -06:00
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
2016-01-25 08:13:49 -06:00
XeditDocumentSaver *saver = XEDIT_DOCUMENT_SAVER (object);
2011-11-07 13:46:58 -06:00
switch (prop_id)
{
case PROP_DOCUMENT:
g_return_if_fail (saver->document == NULL);
saver->document = g_value_get_object (value);
break;
case PROP_URI:
g_return_if_fail (saver->uri == NULL);
saver->uri = g_value_dup_string (value);
break;
case PROP_ENCODING:
g_return_if_fail (saver->encoding == NULL);
saver->encoding = g_value_get_boxed (value);
break;
case PROP_NEWLINE_TYPE:
saver->newline_type = g_value_get_enum (value);
break;
case PROP_FLAGS:
saver->flags = g_value_get_flags (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
2016-01-25 08:13:49 -06:00
xedit_document_saver_get_property (GObject *object,
2011-11-07 13:46:58 -06:00
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
2016-01-25 08:13:49 -06:00
XeditDocumentSaver *saver = XEDIT_DOCUMENT_SAVER (object);
2011-11-07 13:46:58 -06:00
switch (prop_id)
{
case PROP_DOCUMENT:
g_value_set_object (value, saver->document);
break;
case PROP_URI:
g_value_set_string (value, saver->uri);
break;
case PROP_ENCODING:
g_value_set_boxed (value, saver->encoding);
break;
case PROP_NEWLINE_TYPE:
g_value_set_enum (value, saver->newline_type);
break;
case PROP_FLAGS:
g_value_set_flags (value, saver->flags);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
2016-01-25 08:13:49 -06:00
xedit_document_saver_finalize (GObject *object)
2011-11-07 13:46:58 -06:00
{
2016-01-25 08:13:49 -06:00
XeditDocumentSaver *saver = XEDIT_DOCUMENT_SAVER (object);
2011-11-07 13:46:58 -06:00
g_free (saver->uri);
2016-01-25 08:13:49 -06:00
G_OBJECT_CLASS (xedit_document_saver_parent_class)->finalize (object);
2011-11-07 13:46:58 -06:00
}
static void
2016-01-25 08:13:49 -06:00
xedit_document_saver_dispose (GObject *object)
2011-11-07 13:46:58 -06:00
{
2016-01-25 08:13:49 -06:00
XeditDocumentSaver *saver = XEDIT_DOCUMENT_SAVER (object);
2011-11-07 13:46:58 -06:00
if (saver->info != NULL)
{
g_object_unref (saver->info);
saver->info = NULL;
}
2016-01-25 08:13:49 -06:00
G_OBJECT_CLASS (xedit_document_saver_parent_class)->dispose (object);
2011-11-07 13:46:58 -06:00
}
static void
2016-01-25 08:13:49 -06:00
xedit_document_saver_class_init (XeditDocumentSaverClass *klass)
2011-11-07 13:46:58 -06:00
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
2016-01-25 08:13:49 -06:00
object_class->finalize = xedit_document_saver_finalize;
object_class->dispose = xedit_document_saver_dispose;
object_class->set_property = xedit_document_saver_set_property;
object_class->get_property = xedit_document_saver_get_property;
2011-11-07 13:46:58 -06:00
g_object_class_install_property (object_class,
PROP_DOCUMENT,
g_param_spec_object ("document",
"Document",
2016-01-25 08:13:49 -06:00
"The XeditDocument this XeditDocumentSaver is associated with",
XEDIT_TYPE_DOCUMENT,
2011-11-07 13:46:58 -06:00
G_PARAM_READWRITE |
G_PARAM_CONSTRUCT_ONLY |
G_PARAM_STATIC_STRINGS));
g_object_class_install_property (object_class,
PROP_URI,
g_param_spec_string ("uri",
"URI",
2016-01-25 08:13:49 -06:00
"The URI this XeditDocumentSaver saves the document to",
2011-11-07 13:46:58 -06:00
"",
G_PARAM_READWRITE |
G_PARAM_CONSTRUCT_ONLY |
G_PARAM_STATIC_STRINGS));
g_object_class_install_property (object_class,
PROP_ENCODING,
g_param_spec_boxed ("encoding",
"URI",
"The encoding of the saved file",
2016-01-25 08:13:49 -06:00
XEDIT_TYPE_ENCODING,
2011-11-07 13:46:58 -06:00
G_PARAM_READWRITE |
G_PARAM_CONSTRUCT_ONLY |
G_PARAM_STATIC_STRINGS));
g_object_class_install_property (object_class,
PROP_NEWLINE_TYPE,
g_param_spec_enum ("newline-type",
"Newline type",
"The accepted types of line ending",
2016-01-25 08:13:49 -06:00
XEDIT_TYPE_DOCUMENT_NEWLINE_TYPE,
XEDIT_DOCUMENT_NEWLINE_TYPE_LF,
2011-11-07 13:46:58 -06:00
G_PARAM_READWRITE |
G_PARAM_STATIC_NAME |
G_PARAM_STATIC_BLURB |
G_PARAM_CONSTRUCT_ONLY));
g_object_class_install_property (object_class,
PROP_FLAGS,
g_param_spec_flags ("flags",
"Flags",
"The flags for the saving operation",
2016-01-25 08:13:49 -06:00
XEDIT_TYPE_DOCUMENT_SAVE_FLAGS,
2011-11-07 13:46:58 -06:00
0,
G_PARAM_READWRITE |
G_PARAM_CONSTRUCT_ONLY));
signals[SAVING] =
g_signal_new ("saving",
G_OBJECT_CLASS_TYPE (object_class),
G_SIGNAL_RUN_LAST,
2016-01-25 08:13:49 -06:00
G_STRUCT_OFFSET (XeditDocumentSaverClass, saving),
2011-11-07 13:46:58 -06:00
NULL, NULL,
2016-01-25 08:13:49 -06:00
xedit_marshal_VOID__BOOLEAN_POINTER,
2011-11-07 13:46:58 -06:00
G_TYPE_NONE,
2,
G_TYPE_BOOLEAN,
G_TYPE_POINTER);
}
static void
2016-01-25 08:13:49 -06:00
xedit_document_saver_init (XeditDocumentSaver *saver)
2011-11-07 13:46:58 -06:00
{
saver->used = FALSE;
}
2016-01-25 08:13:49 -06:00
XeditDocumentSaver *
xedit_document_saver_new (XeditDocument *doc,
2011-11-07 13:46:58 -06:00
const gchar *uri,
2016-01-25 08:13:49 -06:00
const XeditEncoding *encoding,
XeditDocumentNewlineType newline_type,
XeditDocumentSaveFlags flags)
2011-11-07 13:46:58 -06:00
{
2016-01-25 08:13:49 -06:00
XeditDocumentSaver *saver;
2011-11-07 13:46:58 -06:00
GType saver_type;
2016-01-25 08:13:49 -06:00
g_return_val_if_fail (XEDIT_IS_DOCUMENT (doc), NULL);
2011-11-07 13:46:58 -06:00
2016-01-25 08:13:49 -06:00
saver_type = XEDIT_TYPE_GIO_DOCUMENT_SAVER;
2011-11-07 13:46:58 -06:00
if (encoding == NULL)
2016-01-25 08:13:49 -06:00
encoding = xedit_encoding_get_utf8 ();
2011-11-07 13:46:58 -06:00
2016-01-25 08:13:49 -06:00
saver = XEDIT_DOCUMENT_SAVER (g_object_new (saver_type,
2011-11-07 13:46:58 -06:00
"document", doc,
"uri", uri,
"encoding", encoding,
"newline_type", newline_type,
"flags", flags,
NULL));
return saver;
}
void
2016-01-25 08:13:49 -06:00
xedit_document_saver_saving (XeditDocumentSaver *saver,
2011-11-07 13:46:58 -06:00
gboolean completed,
GError *error)
{
/* the object will be unrefed in the callback of the saving
* signal, so we need to prevent finalization.
*/
if (completed)
{
g_object_ref (saver);
}
g_signal_emit (saver, signals[SAVING], 0, completed, error);
if (completed)
{
if (error == NULL)
2016-01-25 08:13:49 -06:00
xedit_debug_message (DEBUG_SAVER, "save completed");
2011-11-07 13:46:58 -06:00
else
2016-01-25 08:13:49 -06:00
xedit_debug_message (DEBUG_SAVER, "save failed");
2011-11-07 13:46:58 -06:00
g_object_unref (saver);
}
}
void
2016-01-25 08:13:49 -06:00
xedit_document_saver_save (XeditDocumentSaver *saver,
2011-11-07 13:46:58 -06:00
GTimeVal *old_mtime)
{
2016-01-25 08:13:49 -06:00
xedit_debug (DEBUG_SAVER);
2011-11-07 13:46:58 -06:00
2016-01-25 08:13:49 -06:00
g_return_if_fail (XEDIT_IS_DOCUMENT_SAVER (saver));
2011-11-07 13:46:58 -06:00
g_return_if_fail (saver->uri != NULL && strlen (saver->uri) > 0);
g_return_if_fail (saver->used == FALSE);
saver->used = TRUE;
// CHECK:
// - sanity check a max len for the uri?
// report async (in an idle handler) or sync (bool ret)
// async is extra work here, sync is special casing in the caller
/* never keep backup of autosaves */
2016-01-25 08:13:49 -06:00
if ((saver->flags & XEDIT_DOCUMENT_SAVE_PRESERVE_BACKUP) != 0)
2011-11-07 13:46:58 -06:00
saver->keep_backup = FALSE;
else
2016-01-25 08:13:49 -06:00
saver->keep_backup = xedit_prefs_manager_get_create_backup_copy ();
2011-11-07 13:46:58 -06:00
2016-01-25 08:13:49 -06:00
XEDIT_DOCUMENT_SAVER_GET_CLASS (saver)->save (saver, old_mtime);
2011-11-07 13:46:58 -06:00
}
2016-01-25 08:13:49 -06:00
XeditDocument *
xedit_document_saver_get_document (XeditDocumentSaver *saver)
2011-11-07 13:46:58 -06:00
{
2016-01-25 08:13:49 -06:00
g_return_val_if_fail (XEDIT_IS_DOCUMENT_SAVER (saver), NULL);
2011-11-07 13:46:58 -06:00
return saver->document;
}
const gchar *
2016-01-25 08:13:49 -06:00
xedit_document_saver_get_uri (XeditDocumentSaver *saver)
2011-11-07 13:46:58 -06:00
{
2016-01-25 08:13:49 -06:00
g_return_val_if_fail (XEDIT_IS_DOCUMENT_SAVER (saver), NULL);
2011-11-07 13:46:58 -06:00
return saver->uri;
}
/* Returns 0 if file size is unknown */
goffset
2016-01-25 08:13:49 -06:00
xedit_document_saver_get_file_size (XeditDocumentSaver *saver)
2011-11-07 13:46:58 -06:00
{
2016-01-25 08:13:49 -06:00
g_return_val_if_fail (XEDIT_IS_DOCUMENT_SAVER (saver), 0);
2011-11-07 13:46:58 -06:00
2016-01-25 08:13:49 -06:00
return XEDIT_DOCUMENT_SAVER_GET_CLASS (saver)->get_file_size (saver);
2011-11-07 13:46:58 -06:00
}
goffset
2016-01-25 08:13:49 -06:00
xedit_document_saver_get_bytes_written (XeditDocumentSaver *saver)
2011-11-07 13:46:58 -06:00
{
2016-01-25 08:13:49 -06:00
g_return_val_if_fail (XEDIT_IS_DOCUMENT_SAVER (saver), 0);
2011-11-07 13:46:58 -06:00
2016-01-25 08:13:49 -06:00
return XEDIT_DOCUMENT_SAVER_GET_CLASS (saver)->get_bytes_written (saver);
2011-11-07 13:46:58 -06:00
}
GFileInfo *
2016-01-25 08:13:49 -06:00
xedit_document_saver_get_info (XeditDocumentSaver *saver)
2011-11-07 13:46:58 -06:00
{
2016-01-25 08:13:49 -06:00
g_return_val_if_fail (XEDIT_IS_DOCUMENT_SAVER (saver), NULL);
2011-11-07 13:46:58 -06:00
return saver->info;
}