file.cpp revision b9002edd77d18ae04b2f7f1130ee385e51f39500
/**
* @file
*/
/* Authors:
* Lauris Kaplinski <lauris@kaplinski.com>
* Chema Celorio <chema@celorio.com>
* bulia byak <buliabyak@users.sf.net>
* Bruno Dilly <bruno.dilly@gmail.com>
* Stephen Silver <sasilver@users.sourceforge.net>
* Jon A. Cruz <jon@joncruz.org>
* Abhishek Sharma
* David Xiong
*
* Copyright (C) 2006 Johan Engelen <johan@shouraizou.nl>
* Copyright (C) 1999-2012 Authors
* Copyright (C) 2004 David Turner
* Copyright (C) 2001-2002 Ximian, Inc.
*
* Released under GNU GPL, read the file 'COPYING' for more information
*/
/** @file
* @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 "ui/dialog/ocaldialogs.h"
#include "desktop.h"
#include "desktop-handles.h"
#include "dir-util.h"
#include "document-private.h"
#include "document-undo.h"
#include "file.h"
#include "helper/png-write.h"
#include "id-clash.h"
#include "inkscape.h"
#include "inkscape.h"
#include "interface.h"
#include "message.h"
#include "message-stack.h"
#include "path-prefix.h"
#include "preferences.h"
#include "print.h"
#include "resource-manager.h"
#include "rdf.h"
#include "selection-chemistry.h"
#include "selection.h"
#include "sp-namedview.h"
#include "style.h"
#include "ui/view/view-widget.h"
#include "uri.h"
#include "xml/rebase-hrefs.h"
#include "verbs.h"
#include "event-log.h"
#include <glib.h>
using Inkscape::DocumentUndo;
#ifdef WITH_GNOME_VFS
# include <libgnomevfs/gnome-vfs.h>
#endif
#ifdef WITH_DBUS
#include "extension/dbus/dbus-init.h"
#endif
//#ifdef WITH_INKBOARD
//#include "jabber_whiteboard/session-manager.h"
//#endif
#ifdef WIN32
#include <windows.h>
#endif
//#define INK_DUMP_FILENAME_CONV 1
//#define INK_DUMP_FOPEN 1
// what gets passed here is not actually an URI... it is an UTF-8 encoded filename (!)
{
g_warning("sp_file_add_recent: uri == NULL");
return;
}
if (fn) {
if (uri_to_add) {
}
}
}
/*######################
## N E W
######################*/
/**
* Create a blank document and add it to the desktop
*/
{
#ifdef WITH_DBUS
#endif
return dt;
}
{
}
gchar *foundTemplate = 0;
for (std::list<gchar const*>::iterator nameIt = baseNames.begin(); (nameIt != baseNames.end()) && !foundTemplate; ++nameIt) {
for (std::list<gchar *>::iterator it = sources.begin(); (it != sources.end()) && !foundTemplate; ++it) {
// 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.
foundTemplate = tmp;
} else {
}
}
}
}
}
if (foundTemplate) {
foundTemplate = 0;
}
return desk;
}
/*######################
## 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 add_to_recent, bool replace_empty)
{
if (desktop) {
}
try {
}
if (desktop) {
}
if (doc) {
// If the current desktop is empty, open the document there
} else {
// create a whole new desktop and window
SPViewWidget *dtw = sp_desktop_widget_new(sp_document_namedview(doc, NULL)); // TODO this will trigger broken link warnings, etc.
}
// everyone who cares now has a reference, get rid of ours
// resize the window to match the document properties
if (add_to_recent) {
}
if ( inkscape_use_gui() ) {
// Perform a fixup pass for hrefs.
}
}
return TRUE;
} else {
return FALSE;
}
}
/**
* Handle prompting user for "do you want to revert"? Revert on "OK"
*/
void sp_file_revert_dialog()
{
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
{
}
//# Test if the open_path directory exists
open_path = "";
#ifdef WIN32
//# If no open path, default to our win32 documents folder
{
// The path to the My Documents folder is read from the
// value "HKEY_CURRENT_USER\Software\Windows\CurrentVersion\Explorer\Shell Folders\Personal"
"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders",
{
{
if(utf8path)
{
}
}
}
}
#endif
//# If no open path, default to our home directory
{
open_path = g_get_home_dir();
}
//# Create a dialog
_("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 sp_file_vacuum()
{
_("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;
gchar *text = g_strdup_printf(_("File %s is write protected. Please remove write protection and try again."), safeUri);
return FALSE;
return FALSE;
return FALSE;
} catch (...) {
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
}
/**
* Check if a string ends with another string.
* \todo Find a better code file to put this general purpose method
*/
{
} else {
return false;
}
}
/**
* Display a SaveAs dialog. Save the document if OK pressed.
*/
bool
sp_file_save_dialog(Gtk::Window &parentWindow, SPDocument *doc, Inkscape::Extension::FileSaveMethod save_method)
{
// Note: default_extension has the format "org.inkscape.output.svg.inkscape", whereas
// filename_extension only uses ".svg"
//g_message("%s: extension name: '%s'", __FUNCTION__, default_extension);
if (extension)
save_path = "";
save_path = g_get_home_dir();
// TODO fixed buffer is bad:
char formatBuf[256];
int i = 1;
// We are saving for the first time; create a unique default filename
}
} 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;
}
// set new title here (call RDF to ensure metadata and title element are updated)
// free up old string
delete saveDialog;
saveDialog = 0;
if ( newFileName.size()>0 )
else
g_warning( "Error converting save filename to UTF-8." );
}
// FIXME: does the argument !is_copy really convey the correct meaning here?
}
return success;
}
return false;
}
/**
* Save a document, displaying a SaveAs dialog if necessary.
*/
bool
{
bool success = true;
if (doc->isModifiedSinceSave()) {
{
// Hier sollte in Argument mitgegeben werden, das anzeigt, da� das Dokument das erste
// Mal gespeichert wird, so da� als default .svg ausgew�hlt wird und nicht die zuletzt
// benutzte "Save as ..."-Endung
} else {
Glib::ustring extension = Inkscape::Extension::get_file_save_extension(Inkscape::Extension::FILE_SAVE_METHOD_SAVE_AS);
// Try to determine the extension from the uri; this may not lead to a valid extension,
// but this case is caught in the file_save method below (or rather in Extension::save()
// further down the line).
// but hopefully it's a reasonable workaround for now
}
success = file_save(parentWindow, doc, fn, Inkscape::Extension::db.get(ext.c_str()), FALSE, TRUE, Inkscape::Extension::FILE_SAVE_METHOD_SAVE_AS);
if (success == false) {
// give the user the chance to change filename or extension
}
}
} 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;
return sp_file_save_dialog(parentWindow, SP_ACTIVE_DOCUMENT, Inkscape::Extension::FILE_SAVE_METHOD_SAVE_AS);
}
/**
* Save a copy of a document, always displaying a sort of SaveAs dialog.
*/
bool
{
if (!SP_ACTIVE_DOCUMENT)
return false;
return sp_file_save_dialog(parentWindow, SP_ACTIVE_DOCUMENT, Inkscape::Extension::FILE_SAVE_METHOD_SAVE_COPY);
}
/*######################
## I M P O R T
######################*/
/**
* Import a resource. Called by sp_file_import()
*/
SPObject *
{
//DEBUG_MESSAGE( fileImport, "file_import( in_doc:%p uri:[%s], key:%p", in_doc, uri, key );
try {
}
// Count the number of top-level items in the imported document.
guint items_count = 0;
if (SP_IS_ITEM(child)) {
items_count++;
}
}
// Create a new group if necessary.
}
// Determine the place to insert the new object.
// This will be the current layer, if possible.
// FIXME: If there's no desktop (command line run?) we need
// a document:: method to return the current layer.
// For now, we just use the root in this case.
if (desktop) {
} else {
}
// Construct a new object representing the imported image,
// and insert it into the current document.
if (SP_IS_ITEM(child)) {
// convert layers to groups, and make sure they are unlocked
// FIXME: add "preserve layers" mode where each layer from
// import is copied to the same-named layer in host
}
// don't lose top-level defs or style elements
}
}
}
}
}
// release some stuff
// select and move the imported item
// preserve parent and viewBox transformations
// c2p is identity matrix at this point unless ensureUpToDate is called
doc->ensureUpToDate();
// move to mouse pointer
{
if (sel_bbox) {
sp_selection_move_relative(selection, m, false);
}
}
}
_("Import"));
return new_obj;
} else {
}
return NULL;
}
/**
* Display an Open dialog, import a resource if OK pressed.
*/
void
{
if (!doc)
return;
if(import_path.empty())
{
}
//# Test if the import_path directory exists
import_path = "";
//# If no open path, default to our home directory
if (import_path.empty())
{
}
// Create new dialog (don't use an old one, because parentWindow has probably changed)
(char const *)_("Select file to import"));
if (!success) {
delete importDialogInstance;
return;
}
// Get file name and extension type
delete importDialogInstance;
if ( newFileName.size() > 0)
else
g_warning( "ERROR CONVERTING OPEN FILENAME TO UTF-8" );
}
return;
}
/*######################
## E X P O R T
######################*/
#ifdef NEW_EXPORT_DIALOG
/**
* Display an Export dialog, export as the selected type if OK pressed
*/
bool
{
//# temp hack for 'doc' until we can switch to this dialog
//# Get the default extension name
if(default_extension.empty()) {
// FIXME: Is this a good default? Should there be a macro for the string?
}
//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)
export_path = attr3;
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 Export dialog
(char const *) _("Select file to export to"),
);
if (!success) {
delete exportDialogInstance;
return success;
}
delete exportDialogInstance;
if ( newFileName.size()>0 )
else
g_warning( "Error converting save filename to UTF-8." );
success = file_save(parentWindow, doc, fileName, selectionType, TRUE, FALSE, Inkscape::Extension::FILE_SAVE_METHOD_EXPORT);
if (success) {
}
return success;
}
return false;
}
#else
/**
* TODO Delete
* This is now a dialog called from dialog manager
*
bool
sp_file_export_dialog(Gtk::Window &parentWindow)
{
// sp_export_dialog();
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
sp_file_export_to_ocal_dialog(Gtk::Window &parentWindow)
{
if (!SP_ACTIVE_DOCUMENT)
return false;
SPDocument *doc = SP_ACTIVE_DOCUMENT;
Glib::ustring export_path;
Glib::ustring export_loc;
Glib::ustring fileName;
Inkscape::Extension::Extension *selectionType;
bool success = false;
static bool gotSuccess = false;
Inkscape::XML::Node *repr = doc->getReprRoot();
(void)repr;
if (!doc->uri && !doc->isModifiedSinceSave())
return false;
// Get the default extension name
Glib::ustring default_extension = "org.inkscape.output.svg.inkscape";
char formatBuf[256];
Glib::ustring filename_extension = ".svg";
selectionType = Inkscape::Extension::db.get(default_extension.c_str());
export_path = Glib::get_tmp_dir ();
export_loc = export_path;
export_loc.append(G_DIR_SEPARATOR_S);
snprintf(formatBuf, 255, _("drawing%s"), filename_extension.c_str());
export_loc.append(formatBuf);
// convert save_loc from utf-8 to locale
// is this needed any more, now that everything is handled in
// Inkscape::IO?
Glib::ustring export_path_local = Glib::filename_from_utf8(export_path);
if ( export_path_local.size() > 0)
export_path = export_path_local;
// Show the Export To OCAL dialog
Inkscape::UI::Dialog::OCAL:ExportDialog *exportDialogInstance =
new Inkscape::UI::Dialog::OCAL:ExportDialog
parentWindow,
Inkscape::UI::Dialog::EXPORT_TYPES,
(char const *) _("Select file to export to")
);
success = exportDialogInstance->show();
if (!success) {
delete exportDialogInstance;
return success;
}
fileName = exportDialogInstance->getFilename();
delete exportDialogInstance;
exportDialogInstance = NULL;;
fileName.append(filename_extension.c_str());
if (fileName.size() > 0) {
Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
if ( newFileName.size()>0 )
fileName = newFileName;
else
g_warning( "Error converting save filename to UTF-8." );
}
Glib::ustring filePath = export_path;
filePath.append(G_DIR_SEPARATOR_S);
filePath.append(Glib::path_get_basename(fileName));
fileName = filePath;
success = file_save(parentWindow, doc, filePath, selectionType, FALSE, FALSE, Inkscape::Extension::FILE_SAVE_METHOD_EXPORT);
if (!success){
gchar *text = g_strdup_printf(_("Error saving a temporary copy"));
sp_ui_error_dialog(text);
return success;
}
// Start now the submition
// Create the uri
Inkscape::Preferences *prefs = Inkscape::Preferences::get();
Glib::ustring uri = "dav://";
Glib::ustring username = prefs->getString("/options/ocalusername/str");
Glib::ustring password = prefs->getString("/options/ocalpassword/str");
if (username.empty() || password.empty())
{
Inkscape::UI::Dialog::FileExportToOCALPasswordDialog *exportPasswordDialogInstance = NULL;
if(!gotSuccess)
{
exportPasswordDialogInstance = new Inkscape::UI::Dialog::FileExportToOCALPasswordDialog(
parentWindow,
(char const *) _("Open Clip Art Login"));
success = exportPasswordDialogInstance->show();
if (!success) {
delete exportPasswordDialogInstance;
return success;
}
}
username = exportPasswordDialogInstance->getUsername();
password = exportPasswordDialogInstance->getPassword();
delete exportPasswordDialogInstance;
exportPasswordDialogInstance = NULL;
}
uri.append(username);
uri.append(":");
uri.append(password);
uri.append("@");
uri.append("/dav.php/");
uri.append(Glib::path_get_basename(fileName));
// Save as a remote file using the dav protocol.
success = file_save_remote(doc, uri, selectionType, FALSE, FALSE);
remove(fileName.c_str());
if (!success)
{
gchar *text = g_strdup_printf(_("Error exporting the document. Verify if the server name, username and password are correct, if the server has support for webdav and verify if you didn't forget to choose a license."));
sp_ui_error_dialog(text);
}
else
gotSuccess = true;
return success;
}
*/
/**
* Export the current document to OCAL
*/
/*
void
sp_file_export_to_ocal(Gtk::Window &parentWindow)
{
// Try to execute the new code and return;
if (!SP_ACTIVE_DOCUMENT)
return;
bool success = sp_file_export_to_ocal_dialog(parentWindow);
if (success)
SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Document exported..."));
}
*/
/*######################
## I M P O R T F R O M O C A L
######################*/
/**
* Display an ImportFromOcal Dialog, and the selected document from OCAL
*/
{
}
}
void
{
if (!doc)
return;
if (import_ocal_dialog == NULL) {
import_ocal_dialog = new
(char const *)_("Import Clip Art"));
}
}
/*######################
## P R I N T
######################*/
/**
* Print the current document, if any.
*/
void
{
if (doc)
}
/*
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 :