]> git.lyx.org Git - lyx.git/blobdiff - src/insets/InsetGraphics.cpp
This should be the last of the commits refactoring the InsetLayout code.
[lyx.git] / src / insets / InsetGraphics.cpp
index a5d6c61a4271b4976c4e1903d784bb2148702d76..421dcf1a6b3e31c7b26896275b73008415afb9ba 100644 (file)
@@ -15,7 +15,6 @@ TODO
     * What advanced features the users want to do?
       Implement them in a non latex dependent way, but a logical way.
       LyX should translate it to latex or any other fitting format.
-    * Add a way to roll the image file into the file format.
     * When loading, if the image is not found in the expected place, try
       to find it in the clipart, or in the same directory with the image.
     * The image choosing dialog could show thumbnails of the image formats
@@ -56,14 +55,12 @@ TODO
 #include "BufferView.h"
 #include "Converter.h"
 #include "Cursor.h"
-#include "support/debug.h"
 #include "DispatchResult.h"
 #include "ErrorList.h"
 #include "Exporter.h"
 #include "Format.h"
 #include "FuncRequest.h"
 #include "FuncStatus.h"
-#include "support/gettext.h"
 #include "LaTeXFeatures.h"
 #include "Length.h"
 #include "Lexer.h"
@@ -76,8 +73,11 @@ TODO
 #include "frontends/alert.h"
 
 #include "support/convert.h"
+#include "support/debug.h"
 #include "support/docstream.h"
+#include "support/ExceptionMessage.h"
 #include "support/filetools.h"
+#include "support/gettext.h"
 #include "support/lyxlib.h"
 #include "support/lstrings.h"
 #include "support/os.h"
@@ -89,34 +89,12 @@ TODO
 #include <algorithm>
 #include <sstream>
 
+using namespace std;
+using namespace lyx::support;
 
 namespace lyx {
 
-using support::bformat;
-using support::changeExtension;
-using support::compare_timestamps;
-using support::contains;
-using support::DocFileName;
-using support::FileName;
-using support::float_equal;
-using support::getExtension;
-using support::isValidLaTeXFilename;
-using support::latex_path;
-using support::onlyFilename;
-using support::removeExtension;
-using support::rtrim;
-using support::subst;
-using support::suffixIs;
-using support::Systemcall;
-using support::unzipFile;
-using support::unzippedFileName;
-
-using std::endl;
-using std::string;
-using std::istringstream;
-using std::ostream;
-using std::ostringstream;
-
+namespace Alert = frontend::Alert;
 
 namespace {
 
@@ -192,9 +170,16 @@ void InsetGraphics::doDispatch(Cursor & cur, FuncRequest & cmd)
                Buffer const & buffer = cur.buffer();
                InsetGraphicsParams p;
                InsetGraphicsMailer::string2params(to_utf8(cmd.argument()), buffer, p);
-               if (!p.filename.empty())
+               if (!p.filename.empty()) {
+                       try {
+                               p.filename.enable(buffer.embedded(), &buffer);
+                       } catch (ExceptionMessage const & message) {
+                               Alert::error(message.title_, message.details_);
+                               // do not set parameter if an error happens
+                               break;
+                       }
                        setParams(p);
-               else
+               else
                        cur.noUpdate();
                break;
        }
@@ -231,26 +216,30 @@ bool InsetGraphics::getStatus(Cursor & cur, FuncRequest const & cmd,
 }
 
 
-void InsetGraphics::registerEmbeddedFiles(Buffer const &
-       EmbeddedFiles & files) const
+void InsetGraphics::registerEmbeddedFiles(Buffer const & buffer,
+       EmbeddedFileList & files) const
 {
-       files.registerFile(params().filename.absFilename(), 
-               false, this);
+       files.registerFile(params().filename, this, buffer);
 }
 
 
 void InsetGraphics::updateEmbeddedFile(Buffer const & buf,
        EmbeddedFile const & file)
 {
-       BOOST_ASSERT(buf.embeddedFiles().enabled());
-       params_.filename = file;
+       // when embedding is enabled, change of embedding status leads to actions
+       EmbeddedFile temp = file;
+       temp.enable(buf.embedded(), &buf);
+       // this will not be set if an exception is thorwn in enable()
+       params_.filename = temp;
+
        LYXERR(Debug::FILES, "Update InsetGraphic with File " 
                << params_.filename.toFilesystemEncoding() 
-               << ", embedding status: " << params_.filename.embedded());
+               << ", embedding status: " << params_.filename.embedded()
+               << ", enabled: " << params_.filename.enabled());
 }
 
 
-void InsetGraphics::edit(Cursor & cur, bool)
+void InsetGraphics::edit(Cursor & cur, bool, EntryDirection)
 {
        InsetGraphicsMailer(*this).showDialog(&cur.bv());
 }
@@ -290,16 +279,7 @@ void InsetGraphics::read(Buffer const & buf, Lexer & lex)
        else
                LYXERR(Debug::GRAPHICS, "Not a Graphics inset!");
 
-       // InsetGraphics is read, with filename in params_. We do not know if this file actually
-       // exists or is embedded so we need to get the 'availableFile' from buf.embeddedFiles()
-       if (buf.embeddedFiles().enabled()) {
-               EmbeddedFiles::EmbeddedFileList::const_iterator it = 
-                       buf.embeddedFiles().find(params_.filename.toFilesystemEncoding());
-               if (it != buf.embeddedFiles().end())
-                       // using available file, embedded or external, depending on file availability and
-                       // embedding status.
-                       params_.filename = *it;
-       }
+       params_.filename.enable(buf.embedded(), &buf);
        graphic_->update(params().as_grfxParams());
 }
 
@@ -322,7 +302,7 @@ void InsetGraphics::readInsetGraphics(Lexer & lex, string const & bufpath)
                } else {
                        if (!params_.Read(lex, token, bufpath))
                                lyxerr << "Unknown token, " << token << ", skipping."
-                                       << std::endl;
+                                       << endl;
                }
        }
 }
@@ -492,40 +472,39 @@ enum GraphicsCopyStatus {
 };
 
 
-std::pair<GraphicsCopyStatus, FileName> const
+pair<GraphicsCopyStatus, FileName> const
 copyFileIfNeeded(FileName const & file_in, FileName const & file_out)
 {
+       LYXERR(Debug::FILES, "Comparing " << file_in << " and " << file_out);
        unsigned long const checksum_in  = file_in.checksum();
        unsigned long const checksum_out = file_out.checksum();
 
        if (checksum_in == checksum_out)
                // Nothing to do...
-               return std::make_pair(IDENTICAL_CONTENTS, file_out);
+               return make_pair(IDENTICAL_CONTENTS, file_out);
 
        Mover const & mover = getMover(formats.getFormatFromFile(file_in));
        bool const success = mover.copy(file_in, file_out);
        if (!success) {
                // FIXME UNICODE
                LYXERR(Debug::GRAPHICS,
-                       to_utf8(support::bformat(_("Could not copy the file\n%1$s\n"
+                       to_utf8(bformat(_("Could not copy the file\n%1$s\n"
                                                           "into the temporary directory."),
                                                from_utf8(file_in.absFilename()))));
        }
 
        GraphicsCopyStatus status = success ? SUCCESS : FAILURE;
-       return std::make_pair(status, file_out);
+       return make_pair(status, file_out);
 }
 
 
-std::pair<GraphicsCopyStatus, FileName> const
+pair<GraphicsCopyStatus, FileName> const
 copyToDirIfNeeded(DocFileName const & file, string const & dir)
 {
-       using support::rtrim;
-
        string const file_in = file.absFilename();
-       string const only_path = support::onlyPath(file_in);
-       if (rtrim(support::onlyPath(file_in) , "/") == rtrim(dir, "/"))
-               return std::make_pair(IDENTICAL_PATHS, file_in);
+       string const only_path = onlyPath(file_in);
+       if (rtrim(onlyPath(file_in) , "/") == rtrim(dir, "/"))
+               return make_pair(IDENTICAL_PATHS, file_in);
 
        string mangled = file.mangledFilename();
        if (file.isZipped()) {
@@ -539,7 +518,7 @@ copyToDirIfNeeded(DocFileName const & file, string const & dir)
                string::size_type const ext_len = file_in.length() - base.length();
                mangled[mangled.length() - ext_len] = '.';
        }
-       FileName const file_out(support::makeAbsPath(mangled, dir));
+       FileName const file_out(makeAbsPath(mangled, dir));
 
        return copyFileIfNeeded(file, file_out);
 }
@@ -558,13 +537,10 @@ string const stripExtensionIfPossible(string const & file, bool nice)
        // dots with a macro whose definition is just a dot ;-)
        // The automatic format selection does not work if the file
        // name is escaped.
-       string const latex_name = latex_path(file,
-                                            support::EXCLUDE_EXTENSION);
+       string const latex_name = latex_path(file, EXCLUDE_EXTENSION);
        if (!nice || contains(latex_name, '"'))
                return latex_name;
-       return latex_path(removeExtension(file),
-                         support::PROTECT_EXTENSION,
-                         support::ESCAPE_DOTS);
+       return latex_path(removeExtension(file), PROTECT_EXTENSION, ESCAPE_DOTS);
 }
 
 
@@ -579,7 +555,7 @@ string const stripExtensionIfPossible(string const & file, string const & to, bo
            (to_format == "eps" && file_format ==  "ps") ||
            (to_format ==  "ps" && file_format == "eps"))
                return stripExtensionIfPossible(file, nice);
-       return latex_path(file, support::EXCLUDE_EXTENSION);
+       return latex_path(file, EXCLUDE_EXTENSION);
 }
 
 } // namespace anon
@@ -592,7 +568,8 @@ string const InsetGraphics::prepareFile(Buffer const & buf,
        if (params().filename.empty())
                return string();
 
-       string const orig_file = params().filename.absFilename();
+       string const orig_file = params().filename.availableFile().absFilename();
+       // this is for dryrun and display purposes, do not use latexFilename
        string const rel_file = params().filename.relFilename(buf.filePath());
 
        // previewing source code, no file copying or file format conversion
@@ -601,7 +578,7 @@ string const InsetGraphics::prepareFile(Buffer const & buf,
 
        // temp_file will contain the file for LaTeX to act on if, for example,
        // we move it to a temp dir or uncompress it.
-       FileName temp_file = params().filename;
+       FileName temp_file = params().filename.availableFile();
 
        // The master buffer. This is useful when there are multiple levels
        // of include files
@@ -621,7 +598,7 @@ string const InsetGraphics::prepareFile(Buffer const & buf,
 
        GraphicsCopyStatus status;
        boost::tie(status, temp_file) =
-                       copyToDirIfNeeded(params().filename, temp_path);
+                       copyToDirIfNeeded(params().filename.availableFile(), temp_path);
 
        if (status == FAILURE)
                return orig_file;
@@ -677,8 +654,7 @@ string const InsetGraphics::prepareFile(Buffer const & buf,
                                        source_file, output_file);
                        // We can't strip the extension, because we don't know
                        // the unzipped file format
-                       return latex_path(output_file,
-                                         support::EXCLUDE_EXTENSION);
+                       return latex_path(output_file, EXCLUDE_EXTENSION);
                }
 
                FileName const unzipped_temp_file =
@@ -717,7 +693,7 @@ string const InsetGraphics::prepareFile(Buffer const & buf,
                        // the file format from the extension, so we must
                        // change it.
                        FileName const new_file = FileName(changeExtension(temp_file.absFilename(), ext));
-                       if (support::rename(temp_file, new_file)) {
+                       if (temp_file.moveTo(new_file)) {
                                temp_file = new_file;
                                output_file = changeExtension(output_file, ext);
                                source_file = FileName(changeExtension(source_file.absFilename(), ext));
@@ -779,9 +755,6 @@ int InsetGraphics::latex(Buffer const & buf, odocstream & os,
        LYXERR(Debug::GRAPHICS, "insetgraphics::latex: Filename = "
                << params().filename.absFilename());
 
-       string const relative_file =
-               params().filename.relFilename(buf.filePath());
-
        bool const file_exists = !params().filename.empty()
                        && params().filename.isReadableFile();
        string const message = file_exists ?
@@ -832,7 +805,7 @@ int InsetGraphics::latex(Buffer const & buf, odocstream & os,
 
        LYXERR(Debug::GRAPHICS, "InsetGraphics::latex outputting:\n" << latex_str);
        // Return how many newlines we issued.
-       return int(std::count(latex_str.begin(), latex_str.end(),'\n'));
+       return int(count(latex_str.begin(), latex_str.end(),'\n'));
 }
 
 
@@ -861,7 +834,7 @@ static int writeImageObject(char const * format, odocstream & os,
 {
        if (runparams.flavor != OutputParams::XML)
                os << "<![ %output.print." << format
-                        << "; [" << std::endl;
+                        << "; [" << endl;
 
        os <<"<imageobject><imagedata fileref=\"&"
                 << graphic_label
@@ -878,7 +851,7 @@ static int writeImageObject(char const * format, odocstream & os,
        os << "</imageobject>";
 
        if (runparams.flavor != OutputParams::XML)
-               os << std::endl << "]]>" ;
+               os << endl << "]]>" ;
 
        return runparams.flavor == OutputParams::XML ? 0 : 2;
 }