On Fri, May 30, 2003 at 01:49:28AM +0100, John Levon wrote:

> I believe we should *always* store an absolute path at
> runtime. At save time, we can convert it to a relative path.

Here's an attempt at a patch. Not tested

john


Index: insetgraphics.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/insets/insetgraphics.C,v
retrieving revision 1.176
diff -u -p -r1.176 insetgraphics.C
--- insetgraphics.C     28 May 2003 23:09:14 -0000      1.176
+++ insetgraphics.C     30 May 2003 01:21:21 -0000
@@ -182,7 +182,7 @@ void InsetGraphics::Cache::update(string
        lyx::Assert(!file_with_path.empty());
 
        string const path = OnlyPath(file_with_path);
-       loader.reset(file_with_path, parent_.params().as_grfxParams(path));
+       loader.reset(file_with_path, parent_.params().as_grfxParams());
 }
 
 
@@ -192,19 +192,20 @@ InsetGraphics::InsetGraphics()
 {}
 
 
-InsetGraphics::InsetGraphics(InsetGraphics const & ig,
-                            string const & filepath)
+#warning I have zero idea about the trackable()
+InsetGraphics::InsetGraphics(InsetGraphics const & ig)
        : Inset(ig),
+         boost::signals::trackable(ig),
          graphic_label(uniqueID()),
          cache_(new Cache(*this))
 {
-       setParams(ig.params(), filepath);
+       setParams(ig.params());
 }
 
 
-Inset * InsetGraphics::clone(Buffer const & buffer) const
+Inset * InsetGraphics::clone(Buffer const &) const
 {
-       return new InsetGraphics(*this, buffer.filePath());
+       return new InsetGraphics(*this);
 }
 
 
@@ -222,8 +223,7 @@ dispatch_result InsetGraphics::localDisp
                InsetGraphicsParams p;
                InsetGraphicsMailer::string2params(cmd.argument, p);
                if (!p.filename.empty()) {
-                       string const filepath = cmd.view()->buffer()->filePath();
-                       setParams(p, filepath);
+                       setParams(p);
                        cmd.view()->updateInset(this);
                }
                return DISPATCHED;
@@ -327,18 +327,6 @@ BufferView * InsetGraphics::view() const
 void InsetGraphics::draw(BufferView * bv, LyXFont const & font,
                         int baseline, float & x) const
 {
-       // MakeAbsPath returns params().filename unchanged if it absolute
-       // already.
-       string const file_with_path =
-               MakeAbsPath(params().filename, bv->buffer()->filePath());
-
-       // A 'paste' operation creates a new inset with the correct filepath,
-       // but then the 'old' inset stored in the 'copy' operation is actually
-       // added to the buffer.
-       // Thus, we should ensure that the filepath is correct.
-       if (file_with_path != cache_->loader.filename())
-               cache_->update(file_with_path);
-
        cache_->view = bv->owner()->view();
        int oasc = cache_->old_ascent;
 
@@ -408,10 +396,10 @@ Inset::EDITABLE InsetGraphics::editable(
 }
 
 
-void InsetGraphics::write(Buffer const *, ostream & os) const
+void InsetGraphics::write(Buffer const * buf, ostream & os) const
 {
        os << "Graphics\n";
-       params().Write(os);
+       params().Write(os, buf->filePath());
 }
 
 
@@ -420,15 +408,15 @@ void InsetGraphics::read(Buffer const * 
        string const token = lex.getString();
 
        if (token == "Graphics")
-               readInsetGraphics(lex);
+               readInsetGraphics(lex, buf->filePath());
        else
                lyxerr[Debug::GRAPHICS] << "Not a Graphics inset!\n";
 
-       cache_->update(MakeAbsPath(params().filename, buf->filePath()));
+       cache_->update(params().filename);
 }
 
 
-void InsetGraphics::readInsetGraphics(LyXLex & lex)
+void InsetGraphics::readInsetGraphics(LyXLex & lex, string const & bufpath)
 {
        bool finished = false;
 
@@ -455,7 +443,7 @@ void InsetGraphics::readInsetGraphics(Ly
                        // TODO: Possibly open up a dialog?
                }
                else {
-                       if (! params_.Read(lex, token))
+                       if (!params_.Read(lex, token, bufpath))
                                lyxerr << "Unknown token, " << token << ", skipping."
                                        << std::endl;
                }
@@ -518,17 +506,13 @@ string const InsetGraphics::prepareFile(
 {
        // LaTeX can cope if the graphics file doesn't exist, so just return the
        // filename.
-       string const orig_file = params().filename;
-       string orig_file_with_path =
-               MakeAbsPath(orig_file, buf->filePath());
-       lyxerr[Debug::GRAPHICS] << "[InsetGraphics::prepareFile] orig_file = "
-                   << orig_file << "\n\twith path: "
-                   << orig_file_with_path << endl;
+       string orig_file = params().filename;
+       string const rel_file = MakeRelPath(orig_file, buf->filePath());
 
-       if (!IsFileReadable(orig_file_with_path))
-               return orig_file;
+       if (!IsFileReadable(rel_file))
+               return rel_file;
 
-       bool const zipped = zippedFile(orig_file_with_path);
+       bool const zipped = zippedFile(orig_file);
 
        // If the file is compressed and we have specified that it
        // should not be uncompressed, then just return its name and
@@ -536,9 +520,9 @@ string const InsetGraphics::prepareFile(
        if (zipped && params().noUnzip) {
                lyxerr[Debug::GRAPHICS]
                        << "\tpass zipped file to LaTeX but with full path.\n";
-               // LaTeX needs an absolue path, otherwise the
+               // LaTeX needs an absolute path, otherwise the
                // coresponding *.eps.bb file isn't found
-               return orig_file_with_path;
+               return orig_file;
        }
 
        // Ascertain whether the file has changed.
@@ -559,22 +543,21 @@ string const InsetGraphics::prepareFile(
                lyxerr[Debug::GRAPHICS]
                        << "\ttemp_file: " << temp_file << endl;
                if (file_has_changed || !IsFileReadable(temp_file)) {
-                       bool const success = lyx::copy(orig_file_with_path,
-                                                      temp_file);
+                       bool const success = lyx::copy(orig_file, temp_file);
                        lyxerr[Debug::GRAPHICS]
                                << "\tCopying zipped file from "
-                               << orig_file_with_path << " to " << temp_file
+                               << orig_file << " to " << temp_file
                                << (success ? " succeeded\n" : " failed\n");
                } else
                        lyxerr[Debug::GRAPHICS]
                                << "\tzipped file " << temp_file
                                << " exists! Maybe no tempdir ...\n";
-               orig_file_with_path = unzipFile(temp_file);
+               orig_file = unzipFile(temp_file);
                lyxerr[Debug::GRAPHICS]
-                       << "\tunzipped to " << orig_file_with_path << endl;
+                       << "\tunzipped to " << orig_file << endl;
        }
 
-       string const from = getExtFromContents(orig_file_with_path);
+       string const from = getExtFromContents(orig_file);
        string const to   = findTargetFormat(from, runparams);
        lyxerr[Debug::GRAPHICS]
                << "\t we have: from " << from << " to " << to << '\n';
@@ -584,8 +567,8 @@ string const InsetGraphics::prepareFile(
                // graphic file as is.
                // This is true even if the orig_file is compressed.
                if (formats.getFormat(to)->extension() == GetExtension(orig_file))
-                       return RemoveExtension(orig_file_with_path);
-               return orig_file_with_path;
+                       return RemoveExtension(orig_file);
+               return orig_file;
        }
 
        // We're going to be running the exported buffer through the LaTeX
@@ -600,13 +583,13 @@ string const InsetGraphics::prepareFile(
        // to "any_dir_file.ext"! changing the dots in the
        // dirname is important for the use of ChangeExtension
        lyxerr[Debug::GRAPHICS]
-               << "\tthe orig file is: " << orig_file_with_path << endl;
+               << "\tthe orig file is: " << orig_file << endl;
 
        if (lyxrc.use_tempdir) {
-               string const ext_tmp = GetExtension(orig_file_with_path);
+               string const ext_tmp = GetExtension(orig_file);
                // without ext and /
                temp_file = subst(
-                       ChangeExtension(orig_file_with_path, string()), "/", "_");
+                       ChangeExtension(orig_file, string()), "/", "_");
                // without dots and again with ext
                temp_file = ChangeExtension(
                        subst(temp_file, ".", "_"), ext_tmp);
@@ -617,14 +600,14 @@ string const InsetGraphics::prepareFile(
 
                // if the file doen't exists, copy it into the tempdir
                if (file_has_changed || !IsFileReadable(temp_file)) {
-                       bool const success = lyx::copy(orig_file_with_path, temp_file);
+                       bool const success = lyx::copy(orig_file, temp_file);
                        lyxerr[Debug::GRAPHICS]
-                               << "\tcopying from " << orig_file_with_path << " to "
+                               << "\tcopying from " << orig_file << " to "
                                << temp_file
                                << (success ? " succeeded\n" : " failed\n");
                        if (!success) {
                                string str = bformat(_("Could not copy the 
file\n%1$s\n"
-                                       "into the temporary directory."), 
orig_file_with_path);
+                                       "into the temporary directory."), orig_file);
                                Alert::error(_("Graphics display failed"), str);
                                return orig_file;
                        }
@@ -680,9 +663,12 @@ int InsetGraphics::latex(Buffer const * 
                << "insetgraphics::latex: Filename = "
                << params().filename << endl;
 
+       string const relative_file = MakeRelPath(params().filename, buf->filePath());
+
        // A missing (e)ps-extension is no problem for LaTeX, so
        // we have to test three different cases
-       string const file_ = MakeAbsPath(params().filename, buf->filePath());
+#warning uh, but can our cache handle it ? no.
+       string const file_ = params().filename;
        bool const file_exists =
                !file_.empty() &&
                (IsFileReadable(file_) ||               // original
@@ -726,7 +712,7 @@ int InsetGraphics::latex(Buffer const * 
        // "nice" means that the buffer is exported to LaTeX format but not
        //        run through the LaTeX compiler.
        if (runparams.nice) {
-               os << before <<'{' << params().filename << '}' << after;
+               os << before <<'{' << relative_file << '}' << after;
                return 1;
        }
 
@@ -735,7 +721,7 @@ int InsetGraphics::latex(Buffer const * 
        // appropriate (when there are several versions in different formats)
        string const latex_str = message.empty() ?
                (before + '{' + os::external_path(prepareFile(buf, runparams)) + '}' + 
after) :
-               (before + '{' + params().filename + " not found!}" + after);
+               (before + '{' + relative_file + " not found!}" + after);
        os << latex_str;
 
        // Return how many newlines we issued.
@@ -798,8 +784,7 @@ void InsetGraphics::statusChanged()
 }
 
 
-bool InsetGraphics::setParams(InsetGraphicsParams const & p,
-                             string const & filepath)
+bool InsetGraphics::setParams(InsetGraphicsParams const & p)
 {
        // If nothing is changed, just return and say so.
        if (params() == p && !p.filename.empty())
@@ -809,7 +794,7 @@ bool InsetGraphics::setParams(InsetGraph
        params_ = p;
 
        // Update the inset with the new parameters.
-       cache_->update(MakeAbsPath(params().filename, filepath));
+       cache_->update(params().filename);
 
        // We have changed data, report it.
        return true;
@@ -856,7 +841,8 @@ void InsetGraphicsMailer::string2params(
 
        if (lex.isOK()) {
                InsetGraphics inset;
-               inset.readInsetGraphics(lex);
+#warning FIXME not setting bufpath is dubious
+               inset.readInsetGraphics(lex, string());
                params = inset.params();
        }
 }
@@ -867,7 +853,8 @@ InsetGraphicsMailer::params2string(Inset
 {
        ostringstream data;
        data << name_ << ' ';
-       params.Write(data);
+#warning FIXME not setting bufpath is dubious
+       params.Write(data, string());
        data << "\\end_inset\n";
        return STRCONV(data.str());
 }
Index: insetgraphics.h
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/insets/insetgraphics.h,v
retrieving revision 1.68
diff -u -p -r1.68 insetgraphics.h
--- insetgraphics.h     28 May 2003 23:09:14 -0000      1.68
+++ insetgraphics.h     30 May 2003 01:21:22 -0000
@@ -28,7 +28,7 @@ public:
        ///
        InsetGraphics();
        ///
-       InsetGraphics(InsetGraphics const &, string const & filepath);
+       InsetGraphics(InsetGraphics const &);
        ///
        ~InsetGraphics();
        ///
@@ -71,8 +71,7 @@ public:
        /** Set the inset parameters, used by the GUIndependent dialog.
            Return true of new params are different from what was so far.
        */
-       bool setParams(InsetGraphicsParams const & params,
-                      string const & filepath);
+       bool setParams(InsetGraphicsParams const & params);
 
        /// Get the inset parameters, used by the GUIndependent dialog.
        InsetGraphicsParams const & params() const;
@@ -93,7 +92,7 @@ private:
        void statusChanged();
 
        /// Read the inset native format
-       void readInsetGraphics(LyXLex & lex);
+       void readInsetGraphics(LyXLex & lex, string const & bufpath);
 
        /// Get the status message, depends on the image loading status.
        string const statusMessage() const;
Index: insetgraphicsParams.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/insets/insetgraphicsParams.C,v
retrieving revision 1.58
diff -u -p -r1.58 insetgraphicsParams.C
--- insetgraphicsParams.C       19 May 2003 17:03:08 -0000      1.58
+++ insetgraphicsParams.C       30 May 2003 01:21:22 -0000
@@ -49,7 +49,7 @@ InsetGraphicsParams::operator=(InsetGrap
 {
        // Are we assigning the object into itself?
        if (this == &params)
-               return * this;
+               return *this;
        copy(params);
        return *this;
 }
@@ -137,12 +137,12 @@ bool operator!=(InsetGraphicsParams cons
 }
 
 
-void InsetGraphicsParams::Write(ostream & os) const
+void InsetGraphicsParams::Write(ostream & os, string const & bufpath) const
 {
        // Do not write the default values
 
        if (!filename.empty()) {
-               os << "\tfilename " << filename << '\n';
+               os << "\tfilename " << MakeRelPath(filename, bufpath) << '\n';
        }
        if (lyxscale != 100)
                os << "\tlyxscale " << lyxscale << '\n';
@@ -183,11 +183,11 @@ void InsetGraphicsParams::Write(ostream 
 }
 
 
-bool InsetGraphicsParams::Read(LyXLex & lex, string const & token)
+bool InsetGraphicsParams::Read(LyXLex & lex, string const & token, string const & 
bufpath)
 {
        if (token == "filename") {
                lex.eatLine();
-               filename = lex.getString();
+               filename = MakeAbsPath(lex.getString(), bufpath);
        } else if (token == "lyxscale") {
                lex.next();
                lyxscale = lex.getInteger();
@@ -254,15 +254,12 @@ bool InsetGraphicsParams::Read(LyXLex & 
 }
 
 
-grfx::Params InsetGraphicsParams::as_grfxParams(string const & filepath) const
+grfx::Params InsetGraphicsParams::as_grfxParams() const
 {
        grfx::Params pars;
        pars.filename = filename;
        pars.scale = lyxscale;
        pars.angle = rotateAngle;
-
-       if (!filepath.empty())
-               pars.filename = MakeAbsPath(pars.filename, filepath);
 
        if (clip) {
                pars.bb = bb;
Index: insetgraphicsParams.h
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/insets/insetgraphicsParams.h,v
retrieving revision 1.31
diff -u -p -r1.31 insetgraphicsParams.h
--- insetgraphicsParams.h       25 Feb 2003 12:32:48 -0000      1.31
+++ insetgraphicsParams.h       30 May 2003 01:21:22 -0000
@@ -69,14 +69,14 @@ struct InsetGraphicsParams
        ///
        InsetGraphicsParams & operator=(InsetGraphicsParams const &);
        /// Save the parameters in the LyX format stream.
-       void Write(std::ostream & os) const;
+       void Write(std::ostream & os, string const & bufpath) const;
        /// If the token belongs to our parameters, read it.
-       bool Read(LyXLex & lex, string const & token);
+       bool Read(LyXLex & lex, string const & token, string const & bufpath);
        /// convert
   // Only a subset of InsetGraphicsParams is needed for display purposes.
   // This function also interrogates lyxrc to ascertain whether
   // to display or not.
-       grfx::Params as_grfxParams(string const & filepath = string()) const;
+       grfx::Params as_grfxParams() const;
 
 private:
        /// Initialize the object to a default status.

Reply via email to