file.cpp revision 416d7c3dbd94794cd4c3005b5bf2c9b51073b726
#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 "ui/dialog/ocaldialogs.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;
if (doc->isModifiedSinceSave()) {
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.
// remember current zoom and view
if (reverted) {
// restore zoom and view
}
} 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
_("Select file to open"));
//# Show the dialog
//# Save the folder the user selected for later
if (!success)
{
delete openDialogInstance;
return;
}
//# User selected something. Get name and type
//# Code to check & open if multiple files.
//# We no longer need the file dialog object - delete it
delete openDialogInstance;
//# Iterate through filenames if more than 1
{
{
if ( newFileName.size() > 0 )
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
*
* \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;
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
#else
#endif
{
#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;
}
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;
}
while (1) {
return true;
}
if (result != GNOME_VFS_OK) {
return false;
}
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");
}
);
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;
if (doc->isModifiedSinceSave()) {
{
} 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
######################*/
/**
* Display an Export dialog, export as the selected type if OK pressed
*/
bool
{
if (!SP_ACTIVE_DOCUMENT)
return false;
bool success = false;
static bool gotSuccess = false;
return false;
// Get the default extension name
char formatBuf[256];
// 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
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;
}
// Start now the submition
// Create the uri
{
if(!gotSuccess)
{
(char const *) _("Open Clip Art Login"));
if (!success)
return success;
}
}
// Save as a remote file using the dav protocol.
if (!success)
{
gchar *text = g_strdup_printf(_("Error exporting the document. Verify if the server name, username and password are correct. If the server have support for webdav and verify if you didn't forget to choose a license too."));
}
else
gotSuccess = true;
return success;
}
/**
* Export the current document to OCAL
*/
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
######################*/
/**
* Display an ImportToOcal Dialog, and the selected document from OCAL
*/
void
{
if (!doc)
return;
if (!importDialogInstance) {
importDialogInstance = new
(char const *)_("Import From Open Clip Art Library"));
}
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;
}
/*######################
## P R I N T
######################*/
/**
* Print the current document, if any.
*/
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 :