namespace lyx {
-/** Dir name used for ".." in the bundled file.
-
-Under the lyx temp directory, content.lyx and its embedded files are usually
-saved as
-
-$temp/$embDirName/file.lyx
-$temp/$embDirName/figure1.png for ./figure1.png)
-$temp/$embDirName/sub/figure2.png for ./sub/figure2.png)
-
-This works fine for embedded files that are in the current or deeper directory
-of the document directory, but not for files such as ../figures/figure.png.
-A unique name $upDirName is chosen to represent .. in such filenames so that
-'up' directories can be stored 'down' the directory tree:
-
-$temp/$embDirName/$upDirName/figures/figure.png for ../figures/figure.png
-$temp/$embDirName/$upDirName/$upDirName/figure.png for ../../figure.png
-
-This name has to be fixed because it is used in lyx bundled .zip file.
-
-Using a similar trick, we use $absDirName for absolute path so that
-an absolute filename can be saved as
-
-$temp/$embDirName/$absDirName/a/absolute/path for /a/absolute/path
-
-*/
-const std::string embDirName = "LyX.Embedded.Files";
-const std::string upDirName = "LyX.Embed.Dir.Up";
-const std::string absDirName = "LyX.Embed.Dir.Abs";
-
namespace Alert = frontend::Alert;
EmbeddedFile::EmbeddedFile(string const & file, std::string const & buffer_path)
if (filename.empty())
return;
- inzip_name_ = to_utf8(makeRelPath(from_utf8(absFilename()),
- from_utf8(buffer_path)));
-
- if (FileName(inzip_name_).isAbsolute())
- inzip_name_ = absDirName + '/' + inzip_name_;
+ inzip_name_ = calcInzipName(buffer_path);
+}
- // replace .. by upDirName
- if (prefixIs(inzip_name_, "."))
- inzip_name_ = subst(inzip_name_, "..", upDirName);
- // to avoid name conflict between $docu_path/file and $temp_path/file
- // embedded files are in a subdirectory of $temp_path.
- inzip_name_ = embDirName + '/' + inzip_name_;
+void EmbeddedFile::setInzipName(std::string const & name)
+{
+ if (name.empty() || name == inzip_name_)
+ return;
+
+ // an enabled EmbeededFile should have this problem handled
+ BOOST_ASSERT(!enabled());
+ // file will be synced when it is enabled
+ inzip_name_ = name;
}
temp_path_ = buf->temppath();
if (!suffixIs(temp_path_, '/'))
temp_path_ += '/';
- if (embedded())
+ if (embedded()) {
+ if (inzip_name_ != calcInzipName(buf->filePath()))
+ syncInzipFile(buf->filePath());
updateFromExternalFile();
+ }
} else {
extract();
temp_path_ = "";
return availableFile().checksum();
}
+/**
+Under the lyx temp directory, content.lyx and its embedded files are usually
+saved as
+
+$temp/$embDirName/file.lyx
+$temp/$embDirName/figure1.png for ./figure1.png)
+$temp/$embDirName/sub/figure2.png for ./sub/figure2.png)
+
+This works fine for embedded files that are in the current or deeper directory
+of the document directory, but not for files such as ../figures/figure.png.
+A unique name $upDirName is chosen to represent .. in such filenames so that
+'up' directories can be stored 'down' the directory tree:
+
+$temp/$embDirName/$upDirName/figures/figure.png for ../figures/figure.png
+$temp/$embDirName/$upDirName/$upDirName/figure.png for ../../figure.png
+
+This name has to be fixed because it is used in lyx bundled .zip file.
+
+Using a similar trick, we use $absDirName for absolute path so that
+an absolute filename can be saved as
+
+$temp/$embDirName/$absDirName/a/absolute/path for /a/absolute/path
+
+*/
+const std::string embDirName = "LyX.Embedded.Files";
+const std::string upDirName = "LyX.Embed.Dir.Up";
+const std::string absDirName = "LyX.Embed.Dir.Abs";
+const std::string driveName = "LyX.Embed.Drive";
+const std::string spaceName = "LyX.Embed.Space";
+
+std::string EmbeddedFile::calcInzipName(std::string const & buffer_path)
+{
+ string inzipName = to_utf8(makeRelPath(from_utf8(absFilename()),
+ from_utf8(buffer_path)));
+
+ if (FileName(inzipName).isAbsolute())
+ inzipName = absDirName + '/' + inzipName;
+
+ // replace .. by upDirName
+ if (prefixIs(inzipName, "."))
+ inzipName = subst(inzipName, "..", upDirName);
+ // replace special characters by their value
+ inzipName = subst(inzipName, ":", driveName);
+ inzipName = subst(inzipName, " ", spaceName);
+
+ // to avoid name conflict between $docu_path/file and $temp_path/file
+ // embedded files are in a subdirectory of $temp_path.
+ inzipName = embDirName + '/' + inzipName;
+ return inzipName;
+}
+
+
+void EmbeddedFile::syncInzipFile(std::string const & buffer_path)
+{
+ BOOST_ASSERT(enabled());
+ string old_emb_file = temp_path_ + '/' + inzip_name_;
+ FileName old_emb(old_emb_file);
+
+ LYXERR(Debug::FILES, " OLD ZIP " << old_emb_file <<
+ " NEW ZIP " << calcInzipName(buffer_path));
+
+ //BOOST_ASSERT(old_emb.exists());
+
+ string new_inzip_name = calcInzipName(buffer_path);
+ string new_emb_file = temp_path_ + '/' + new_inzip_name;
+ FileName new_emb(new_emb_file);
+
+ // need to make directory?
+ FileName path = new_emb.onlyPath();
+ if (!path.createPath()) {
+ throw ExceptionMessage(ErrorException, _("Sync file failure"),
+ bformat(_("Cannot create file path '%1$s'.\n"
+ "Please check whether the path is writeable."),
+ from_utf8(path.absFilename())));
+ return;
+ }
+
+ if (old_emb.copyTo(new_emb)) {
+ LYXERR(Debug::FILES, "Sync inzip file from " << inzip_name_
+ << " to " << new_inzip_name);
+ inzip_name_ = new_inzip_name;
+ return;
+ }
+ throw ExceptionMessage(ErrorException, _("Sync file failure"),
+ bformat(_("Cannot copy file %1$s to %2$s.\n"
+ "Please check whether the directory exists and is writeable."),
+ from_utf8(old_emb_file), from_utf8(new_emb_file)));
+}
+
bool operator==(EmbeddedFile const & lhs, EmbeddedFile const & rhs)
{
std::string const & buffer_path = std::string());
/// set filename and inzipName.
+ /**
+ * NOTE: inzip_name_ is not unique across operation systems and is not
+ * guaranteed to be the same across different versions of lyx.
+ * inzip_name_ will be saved to the lyx file, and is used to indicate
+ * whether or not a file is embedded, and where the embedded file is in
+ * the bundled file. However, the embedded file will be renamed to the
+ * name set here when an EmbeddedFile is enabled. It is therefore
+ * safe to change the naming scheme here.
+ *
+ * NOTE that this treatment does not welcome an UUID solution because
+ * all embedded files will have to be renamed when an embedded file is
+ * opened. It is of course possible to use saved inzipname, but that is
+ * not easy. For example, when a new EmbeddedFile is created with the same
+ * file as an old one, it needs to be synced to the old inzipname...
+ **/
void set(std::string const & filename, std::string const & buffer_path);
+ /** Set the inzip name of an EmbeddedFile, which should be the name
+ * of an actual embedded file on disk. When an EmbeddedFile is enabled,
+ * this file will be renamed to the default inzipName if needed.
+ */
+ void setInzipName(std::string const & name);
/// filename in the zip file, which is the relative path
std::string inzipName() const { return inzip_name_; }
/// Calculate checksum of availableFile
unsigned long checksum() const;
+private:
+ // calculate inzip_name_ from filename
+ std::string calcInzipName(std::string const & buffer_path);
+ // move an embedded disk file with an existing inzip_name_ to
+ // an calculated inzip_name_
+ void syncInzipFile(std::string const & buffer_path);
+
private:
/// filename in zip file
std::string inzip_name_;
db->setFlags(db->flags() | Qt::ItemIsSelectable
| Qt::ItemIsUserCheckable);
db->setCheckState(Qt::Checked);
- databaseLW->addItem(f);
+ databaseLW->addItem(db);
}
}
QListWidgetItem * db = new QListWidgetItem(toqstr(bib));
db->setFlags(db->flags() | Qt::ItemIsSelectable
| Qt::ItemIsUserCheckable);
- db->setCheckState(emb == _("true") ? Qt::Checked : Qt::Unchecked);
+ db->setCheckState(emb.empty() ? Qt::Unchecked : Qt::Checked);
databaseLW->addItem(db);
}
}
for (vector<string>::const_iterator it = bib_str.begin();
it != bib_str.end(); ++it) {
string bibItem(changeExtension(*it, ""));
- QListWidgetItem * db = new QListWidgetItem(toqstr(bibItem));
- db->setFlags(db->flags() & ~Qt::ItemIsSelectable
- | Qt::ItemIsUserCheckable);
- db->setCheckState(Qt::Unchecked);
- add_->bibLW->addItem(db);
+ add_->bibLW->addItem(toqstr(bibItem));
}
string bibstyle = getStylefile();
void GuiInclude::updateContents()
{
filenameED->setText(toqstr(params_["filename"]));
- embedCB->setCheckState(params_["embed"] == _("true") ? Qt::Checked : Qt::Unchecked);
+ embedCB->setCheckState(params_["embed"].empty() ? Qt::Unchecked : Qt::Checked);
visiblespaceCB->setChecked(false);
visiblespaceCB->setEnabled(false);
}
//
InsetCommandParams orig = params();
+ // returned "embed" is composed of "true" or "false", which needs to be adjusted
+ string tmp;
+ string emb;
+
+ string newBibfiles;
+ string newEmbedStatus;
+
+ string bibfiles = to_utf8(p["bibfiles"]);
+ string embedStatus = to_utf8(p["embed"]);
+
+ bibfiles = split(bibfiles, tmp, ',');
+ embedStatus = split(embedStatus, emb, ',');
+ while (!tmp.empty()) {
+ EmbeddedFile file(changeExtension(tmp, "bib"), cur.buffer().filePath());
+ if (!file.exists())
+ continue;
+ if (!newBibfiles.empty())
+ newBibfiles += ",";
+ newBibfiles += tmp;
+ if (!newEmbedStatus.empty())
+ newEmbedStatus += ",";
+ if (emb == "true")
+ newEmbedStatus += file.inzipName();
+ // Get next file name
+ bibfiles = split(bibfiles, tmp, ',');
+ embedStatus = split(embedStatus, emb, ',');
+ }
+ LYXERR(Debug::FILES, "Update parameters from " << p["bibfiles"]
+ << " " << p["embed"] << " to " << newBibfiles << " "
+ << newEmbedStatus);
+ p["bibfiles"] = from_utf8(newBibfiles);
+ p["embed"] = from_utf8(newEmbedStatus);
+
setParams(p);
- // test parameter and copy files
try {
- // enable() in getFiles will try to copy files
+ // test parameter and copy files
getFiles(cur.buffer());
} catch (ExceptionMessage const & message) {
Alert::error(message.title_, message.details_);
bibfiles = split(bibfiles, tmp, ',');
embedStatus = split(embedStatus, emb, ',');
while (!tmp.empty()) {
- if (emb == "true") {
+ if (!emb.empty()) {
EmbeddedFile file(changeExtension(tmp, "bib"), buffer.filePath());
// If the file structure is correct, this should not fail.
file.setEmbed(true);
if (!first) {
bibfiles += ',';
embed += ',';
+ } else
first = false;
- }
bibfiles += from_utf8(it->outputFilename(buf.filePath()));
- embed += it->embedded() ? _("true") : _("false");
+ if (it->embedded())
+ embed += from_utf8(it->inzipName());
}
setParam("bibfiles", bibfiles);
setParam("embed", embed);
if (!filename.empty()) {
os << "\tfilename " << filename.outputFilename(buffer.filePath()) << '\n';
- os << "\tembed " << (filename.embedded() ? "true" : "false") << '\n';
+ os << "\tembed " << (filename.embedded() ? filename.inzipName() : "\"\"") << '\n';
}
if (display != defaultDisplayType)
os << "\tdisplay "
case EX_EMBED: {
lex.next();
- filename.setEmbed(lex.getBool());
+ string const name = lex.getString();
+ filename.setInzipName(name);
+ filename.setEmbed(!name.empty());
break;
}
// Do not write the default values
if (!filename.empty()) {
os << "\tfilename " << filename.outputFilename(buffer.filePath()) << '\n';
- os << "\tembed " << (filename.embedded() ? "true" : "false") << '\n';
+ os << "\tembed " << (filename.embedded() ? filename.inzipName() : "\"\"") << '\n';
}
if (lyxscale != 100)
os << "\tlyxscale " << lyxscale << '\n';
lex.eatLine();
} else if (token == "embed") {
lex.next();
- filename.setEmbed(lex.getBool());
+ string const name = lex.getString();
+ filename.setInzipName(name);
+ filename.setEmbed(!name.empty());
} else if (token == "lyxscale") {
lex.next();
lyxscale = lex.getInteger();
// each time, but there seems to be no easy way around.
EmbeddedFile file(to_utf8(params["filename"]),
onlyPath(parentFilename(buffer)));
- file.setEmbed(params["embed"] == _("true") ? true : false);
+ file.setEmbed(!params["embed"].empty());
file.enable(buffer.embedded(), &buffer);
return file;
}
REF_CODE);
}
try {
+ // the embed parameter passed back from the dialog
+ // is "true" or "false", we need to change it.
+ if (p["embed"] == _("false"))
+ p["embed"].clear();
+ else
+ p["embed"] = from_utf8(EmbeddedFile(to_utf8(p["filename"]),
+ onlyPath(parentFilename(cur.buffer()))).inzipName());
// test parameter
includedFilename(cur.buffer(), p);
} catch (ExceptionMessage const & message) {
else
temp += from_utf8(onlyFilename(to_utf8(params()["filename"])));
- if (params()["embed"] == _("true"))
+ if (!params()["embed"].empty())
temp += _(" (embedded)");
return temp;
}
{
InsetCommandParams p = params();
p["filename"] = from_utf8(file.outputFilename());
- p["embed"] = file.embedded() ? _("true") : _("false");
+ p["embed"] = file.embedded() ? from_utf8(file.inzipName()) : docstring();
set(p, buf);
}