file.cpp revision 297cd3386afc582bfa0d638513c89315434dd6ea
#define __SP_FILE_C__
/*
*
* Authors:
* Lauris Kaplinski <lauris@kaplinski.com>
* Chema Celorio <chema@celorio.com>
* bulia byak <buliabyak@users.sf.net>
* Bruno Dilly <bruno.dilly@gmail.com>
*
* Copyright (C) 2006 Johan Engelen <johan@shouraizou.nl>
* Copyright (C) 1999-2005 Authors
* Copyright (C) 2004 David Turner
* Copyright (C) 2001-2002 Ximian, Inc.
*
* Released under GNU GPL, read the file 'COPYING' for more information
*/
/**
* Note: This file needs to be cleaned up extensively.
* What it probably needs is to have one .h file for
* the API, and two or more .cpp files for the implementations.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <libnr/nr-pixops.h>
#include "document-private.h"
#include "selection-chemistry.h"
#include "ui/view/view-widget.h"
#include "dir-util.h"
#include "helper/png-write.h"
#include "inkscape.h"
#include "desktop.h"
#include "selection.h"
#include "interface.h"
#include "style.h"
#include "print.h"
#include "file.h"
#include "message.h"
#include "message-stack.h"
#include "ui/dialog/filedialog.h"
#include "prefs-utils.h"
#include "path-prefix.h"
#include "sp-namedview.h"
#include "desktop-handles.h"
#include "application/application.h"
#include "application/editor.h"
#include "inkscape.h"
#include "uri.h"
#ifdef WITH_GNOME_VFS
# include <libgnomevfs/gnome-vfs.h>
#endif
#ifdef WITH_INKBOARD
#include "jabber_whiteboard/session-manager.h"
#endif
//#define INK_DUMP_FILENAME_CONV 1
//#define INK_DUMP_FOPEN 1
/*######################
## N E W
######################*/
/**
* Create a blank document and add it to the desktop
*/
{
{
} else {
}
return dt;
}
{
// TRANSLATORS: default.svg is localizable - this is the name of the default document
// template. This way you can localize the default pagesize, translate the name of
// the default layer, etc. If you wish to localize this file, please create a
// localized share/templates/default.xx.svg file, where xx is your language code.
return sp_file_new(default_template);
}
}
}
return sp_file_new("");
}
/*######################
## D E L E T E
######################*/
/**
* Perform document closures preceding an exit()
*/
void
{
// no need to call inkscape_exit here; last document being closed will take care of that
}
/*######################
## O P E N
######################*/
/**
* Open a file, add the document to the desktop
*
* \param replace_empty if true, and the current desktop is empty, this document
* will replace the empty one.
*/
bool
bool add_to_recent, bool replace_empty)
{
try {
}
if (doc) {
// If the current desktop is empty, open the document there
} else {
// create a whole new desktop and window
} else {
}
}
// everyone who cares now has a reference, get rid of ours
// resize the window to match the document properties
if (add_to_recent) {
}
return TRUE;
} else {
return FALSE;
}
}
/**
* Handle prompting user for "do you want to revert"? Revert on "OK"
*/
void
{
if (!uri) {
desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Document not saved yet. Cannot revert."));
return;
}
bool do_revert = true;
gchar *text = g_strdup_printf(_("Changes will be lost! Are you sure you want to reload document %s?"), uri);
if (!response) {
do_revert = false;
}
}
bool reverted;
if (do_revert) {
// Allow overwriting of current document.
} else {
reverted = false;
}
if (reverted) {
} else {
}
}
{
tmp += " [";
for (unsigned i = 0; i < total; i++) {
}
tmp += "]";
}
{
g_message(" size: %lu\n length: %lu\n bytes: %lu\n clen: %lu",
try {
tmp = " ";
if (i < dataLen) {
} else {
tmp += " ";
}
if (i < byteLen) {
} else {
tmp += " . ";
}
} else {
tmp += " ";
}
if ( i < cstrLen ) {
} else {
tmp += " . ";
}
} else {
tmp += " ";
}
}
} catch (...) {
g_message("XXXXXXXXXXXXXXXXXX Exception" );
}
g_message("---------------");
}
/**
* Display an file Open selector. Open a document if OK is pressed.
* Can select single or multiple files for opening.
*/
void
{
//# Get the current directory for finding files
if (attr)
//# Test if the open_path directory exists
open_path = "";
//# If no open path, default to our home directory
{
open_path = g_get_home_dir();
}
//# Create a dialog if we don't already have one
if (!openDialogInstance) {
(char const *)_("Select file to open"));
// allow easy access to our examples folder
if (Inkscape::IO::file_test(INKSCAPE_EXAMPLESDIR, (GFileTest)(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR))) {
}
}
//# Show the dialog
if (!success)
return;
//# User selected something. Get name and type
//# Code to check & open iff multiple files.
//# Iterate through filenames if more than 1
{
{
if ( newFileName.size() > 0 )
fName = newFileName;
else
g_warning( "ERROR CONVERTING OPEN FILENAME TO UTF-8" );
#ifdef INK_DUMP_FILENAME_CONV
#endif
}
}
return;
}
if ( newFileName.size() > 0)
else
g_warning( "ERROR CONVERTING OPEN FILENAME TO UTF-8" );
}
return;
}
/*######################
## V A C U U M
######################*/
/**
* Remove unreferenced defs from the defs section of the document.
*/
void
{
_("Vacuum <defs>"));
if (diff > 0) {
ngettext("Removed <b>%i</b> unused definition in <defs>.",
"Removed <b>%i</b> unused definitions in <defs>.",
diff),
diff);
} else {
}
}
/*######################
## S A V E
######################*/
/**
* This 'save' function called by the others below
* It was divided in file_save_local and file_save_remote
* to support remote saving too.
* Now file_save is calling only local saving, but it will be solved.
*
* \param official whether to set :output_module and :modified in the
* document; is true for normal save, false for temporary saves
*/
static bool
{
return false;
#ifdef WITH_GNOME_VFS
// if (gnome_vfs_initialized() && !gnome_vfs_uri_is_local(gnome_vfs_uri_new(uri.c_str()))) {
// // Use VFS for this
// bool success = file_save_remote(doc, uri, key, saveas, official);
// if (!success) {
// g_warning("Error: Could not save file '%s' with VFS\n", uri.c_str());
// return false;
// }
// else
// return true;
// }
// else
#else
#endif
}
bool
{
try {
false,
gchar *text = g_strdup_printf(_("No Inkscape extension found to save document (%s). This may have been caused by an unknown filename extension."), safeUri);
return FALSE;
return FALSE;
}
return true;
}
/*
* Used only for remote saving using VFS and a specific uri. Gets the file at the /tmp.
*/
bool
{
#ifdef WITH_GNOME_VFS
#define BUF_SIZE 8192
g_warning( "Error converting filename to locale encoding.");
}
// Gets the temp file name.
// Open the temp file to send.
if (result != GNOME_VFS_OK) {
g_warning("Could not find the temp saving.");
return false;
}
if (result == GNOME_VFS_ERROR_NOT_FOUND){
result = gnome_vfs_create (&to_handle, uri_local, GNOME_VFS_OPEN_WRITE, FALSE, GNOME_VFS_PERM_USER_ALL);
}
if (result != GNOME_VFS_OK) {
return false;
}
///g_warning("file_save_remote: temp dir: %s",fileName.c_str());
while (1) {
//g_warning("bytes lidos: %d",bytes_read);
return true;
}
//g_warning("while: %s", gnome_vfs_result_to_string(result));
if (result != GNOME_VFS_OK) {
return false;
}
//g_warning("%s",buffer);
//g_warning("escritos: %d",bytes_written);
if (result != GNOME_VFS_OK) {
return false;
}
if (bytes_read != bytes_written){
return false;
}
}
return true;
#else
// in case we do not have GNOME_VFS
return false;
#endif
}
/**
* Display a SaveAs dialog. Save the document if OK pressed.
*
* \param ascopy (optional) wether to set the documents->uri to the new filename or not
*/
bool
{
//# Get the default extension name
if (!attr)
if (attr)
//g_message("%s: extension name: '%s'", __FUNCTION__, default_extension);
char formatBuf[256];
int i = 1;
//g_warning("%s: extension ptr: 0x%x", __FUNCTION__, (unsigned int)extension);
if (extension)
if (attr)
save_path = "";
save_path = g_get_home_dir();
}
} else {
}
// convert save_loc from utf-8 to locale
// is this needed any more, now that everything is handled in
// Inkscape::IO?
if ( save_loc_local.size() > 0)
//# Show the SaveAs dialog
char const * dialog_title;
if (is_copy) {
dialog_title = (char const *) _("Select file to save a copy to");
} else {
dialog_title = (char const *) _("Select file to save to");
}
(char const *) _("Select file to save to"),
);
// allow easy access to the user's own templates folder
}
if (!success) {
delete saveDialog;
return success;
}
delete saveDialog;
saveDialog = 0;
if ( newFileName.size()>0 )
else
g_warning( "Error converting save filename to UTF-8." );
if (success)
return success;
}
return false;
}
/**
* Save a document, displaying a SaveAs dialog if necessary.
*/
bool
{
bool success = true;
{
} else {
}
} else {
SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("No changes need to be saved."));
}
return success;
}
/**
* Save a document.
*/
bool
{
if (!SP_ACTIVE_DOCUMENT)
return false;
}
/**
* Save a document, always displaying the SaveAs dialog.
*/
bool
{
if (!SP_ACTIVE_DOCUMENT)
return false;
}
/**
* Save a copy of a document, always displaying a sort of SaveAs dialog.
*/
bool
{
if (!SP_ACTIVE_DOCUMENT)
return false;
}
/*######################
## I M P O R T
######################*/
/**
* Import a resource. Called by sp_file_import()
*/
void
{
//DEBUG_MESSAGE( fileImport, "file_import( in_doc:%p uri:[%s], key:%p", in_doc, uri, key );
try {
}
// move imported defs to our document's defs
{
// FIXME: in case of id conflict, newly added thing will be re-ided and thus likely break a reference to it from imported stuff
}
guint items_count = 0;
if (SP_IS_ITEM(child))
items_count ++;
}
// create group
for (SPObject *child = sp_object_first_child(SP_DOCUMENT_ROOT(doc)); child != NULL; child = SP_OBJECT_NEXT(child) ) {
if (SP_IS_ITEM(child)) {
// convert layers to groups; FIXME: add "preserve layers" mode where each layer
// from impot is copied to the same-named layer in host
}
}
if (desktop) {
// Add it to the current layer
} else {
// There's no desktop (command line run?)
// FIXME: For such cases we need a document:: method to return the current layer
}
} else {
// just add one item
for (SPObject *child = sp_object_first_child(SP_DOCUMENT_ROOT(doc)); child != NULL; child = SP_OBJECT_NEXT(child) ) {
if (SP_IS_ITEM(child)) {
if (desktop) {
// Add it to the current layer
} else {
// There's no desktop (command line run?)
// FIXME: For such cases we need a document:: method to return the current layer
}
}
}
}
// select and move the imported item
// To move the imported object, we must temporarily set the "transform pattern with
// object" option.
{
if (sel_bbox) {
}
}
}
_("Import"));
} else {
}
return;
}
/**
* Display an Open dialog, import a resource if OK pressed.
*/
void
{
if (!doc)
return;
if (!importDialogInstance) {
(char const *)_("Select file to import"));
}
if (!success)
return;
//# Get file name and extension type
if ( newFileName.size() > 0)
else
g_warning( "ERROR CONVERTING OPEN FILENAME TO UTF-8" );
if (import_path.size()>0)
}
return;
}
/*######################
## E X P O R T
######################*/
//#define NEW_EXPORT_DIALOG
#ifdef NEW_EXPORT_DIALOG
/**
* Display an Export dialog, export as the selected type if OK pressed
*/
bool
sp_file_export_dialog(void *widget)
{
//# temp hack for 'doc' until we can switch to this dialog
//# Get the default extension name
if (!attr)
if (attr)
//g_message("%s: extension name: '%s'", __FUNCTION__, default_extension);
{
char formatBuf[256];
//g_warning("%s: extension ptr: 0x%x", __FUNCTION__, (unsigned int)extension);
if (extension)
if (attr)
export_path = attr;
export_path = "";
}
else
{
}
// convert save_loc from utf-8 to locale
// is this needed any more, now that everything is handled in
// Inkscape::IO?
if ( export_path_local.size() > 0)
//# Show the SaveAs dialog
if (!exportDialogInstance)
(char const *) _("Select file to export to"),
);
if (!success)
return success;
if ( newFileName.size()>0 )
else
g_warning( "Error converting save filename to UTF-8." );
if (success)
return success;
}
return false;
}
#else
/**
*
*/
bool
sp_file_export_dialog(void *widget)
{
return true;
}
#endif
/*######################
## E X P O R T T O O C A L
######################*/
/**
* Export the current document to OCAL
*/
/**
* Display an Export dialog, export as the selected type if OK pressed
*/
bool
{
//# temp hack for 'doc' until we can switch to this dialog
if (!SP_ACTIVE_DOCUMENT)
return false;
bool success = false;
// Verify whether the document is saved, so save this as temporary
return false;
//# Get the default extension name
if (!attr)
if (attr)
char formatBuf[256];
if (extension)
// convert save_loc from utf-8 to locale
// is this needed any more, now that everything is handled in
// Inkscape::IO?
if ( export_path_local.size() > 0)
//# Show the Export To OCAL dialog
// MADE A CHANGE TO SUBMIT CHANGES. IN THE FUTURE WILL CALL FileExportToOCALDialog
if (!exportDialogInstance)
(char const *) _("Select file to export to"),
);
if (!success)
return success;
if ( newFileName.size()>0 )
else
g_warning( "Error converting save filename to UTF-8." );
}
if (!success){
g_warning( "Error saving a temporary copy." );
return success;
}
/* Start now the submition */
// Create the uri
}
// Save as a remote file using the dav protocol.
//remove(fileName.c_str());
if (!success)
g_warning( "Error exporting the document." );
return success;
}
void
{
// Try to execute the new code and return;
if (!SP_ACTIVE_DOCUMENT)
return;
if (success)
}
/*######################
## I M P O R T F R O M O C A L
######################*/
/**
* Import a document from OCAL
*/
void
{
// Try to execute the new code and return;
if (!SP_ACTIVE_DOCUMENT)
return;
//bool success = sp_file_import_from_ocal_dialog(parentWindow);
//if (success)
// SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Document imported..."));
}
/*######################
## P R I N T
######################*/
/**
* Print the current document, if any.
*/
void
{
if (doc)
}
/**
* Print the current document, if any. Do not use
* the machine's print drivers.
*/
void
{
if (doc)
}
/**
* Display what the drawing would look like, if
* printed.
*/
void
{
if (doc)
}
{
//g_message("Inkscape::IO::fixupHrefs( , [%s], )", base );
if ( 0 ) {
"data:foo,bar",
"foo.svg",
"file:foo.svg",
"bar\xe1\x84\x92.svg",
"bar%e1%84%92.svg",
};
g_message("+------");
for ( int i = 0; things[i]; i++ )
{
try
{
(int)uri.isRelative(),
things[i],
str );
}
catch ( MalformedURIException err )
{
{
(int)again.isRelative(),
things[i],
str );
g_message(" ----");
}
}
}
g_message("+------");
}
// First try to figure out an absolute path to the asset
//g_message("image href [%s]", href );
//g_message(" absr [%s]", absref );
if ( absref && Inkscape::IO::file_test(absref, G_FILE_TEST_EXISTS) && !Inkscape::IO::file_test(base_href, G_FILE_TEST_EXISTS))
{
// only switch over if the absref is valid while href is not
//g_message(" copied absref to href");
}
}
// Once we have an absolute path, convert it relative to the new location
//g_message(" setting to [%s]", relname );
}
// TODO next refinement is to make the first choice keeping the relative path as-is if
// based on the new location it gives us a valid file.
}
}
/*
Local Variables:
mode:c++
c-file-style:"stroustrup"
c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
indent-tabs-mode:nil
fill-column:99
End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :