bool LyXVC::registrer() { FileName const filename = owner_->fileName(); // there must be a file to save if (!filename.isReadableFile()) { Alert::error(_("Document not saved"), _("You must save the document " "before it can be registered.")); return false; } // it is very likely here that the vcs is not created yet... if (!vcs) { //check in the root directory of the document FileName const cvs_entries(onlyPath(filename.absFileName()) + "/CVS/Entries"); FileName const svn_entries(onlyPath(filename.absFileName()) + "/.svn/entries"); FileName const git_index(onlyPath(filename.absFileName()) + "/.git/index"); if (git_index.isReadableFile()) { LYXERR(Debug::LYXVC, "LyXVC: registering " << to_utf8(filename.displayName()) << " with GIT"); vcs.reset(new GIT(git_index, owner_)); } else if (svn_entries.isReadableFile()) { LYXERR(Debug::LYXVC, "LyXVC: registering " << to_utf8(filename.displayName()) << " with SVN"); vcs.reset(new SVN(svn_entries, owner_)); } else if (cvs_entries.isReadableFile()) { LYXERR(Debug::LYXVC, "LyXVC: registering " << to_utf8(filename.displayName()) << " with CVS"); vcs.reset(new CVS(cvs_entries, owner_)); } else { LYXERR(Debug::LYXVC, "LyXVC: registering " << to_utf8(filename.displayName()) << " with RCS"); vcs.reset(new RCS(FileName(), owner_)); } } LYXERR(Debug::LYXVC, "LyXVC: registrer"); docstring response; bool ok = Alert::askForText(response, _("LyX VC: Initial description"), _("(no initial description)")); if (!ok) { LYXERR(Debug::LYXVC, "LyXVC: user cancelled"); vcs.reset(0); return false; } if (response.empty()) response = _("(no initial description)"); vcs->registrer(to_utf8(response)); return true; }
bool tex2lyx(string const & infilename, FileName const & outfilename, string const & encoding) { if (outfilename.isReadableFile()) { if (overwrite_files) { cerr << "Overwriting existing file " << outfilename << endl; } else { cerr << "Not overwriting existing file " << outfilename << endl; return false; } } else { cerr << "Creating file " << outfilename << endl; } ofstream os(outfilename.toFilesystemEncoding().c_str()); if (!os.good()) { cerr << "Could not open output file \"" << outfilename << "\" for writing." << endl; return false; } #ifdef FILEDEBUG cerr << "Input file: " << infilename << "\n"; cerr << "Output file: " << outfilename << "\n"; #endif return tex2lyx(FileName(infilename), os, encoding); }
TextClass::ReturnValues TextClass::readWithoutConv(FileName const & filename, ReadType rt) { if (!filename.isReadableFile()) { lyxerr << "Cannot read layout file `" << filename << "'." << endl; return ERROR; } LYXERR(Debug::TCLASS, "Reading " + translateReadType(rt) + ": " + to_utf8(makeDisplayPath(filename.absFileName()))); // Define the plain layout used in table cells, ert, etc. Note that // we do this before loading any layout file, so that classes can // override features of this layout if they should choose to do so. if (rt == BASECLASS && !hasLayout(plain_layout_)) layoutlist_.push_back(createBasicLayout(plain_layout_)); Lexer lexrc(textClassTags); lexrc.setFile(filename); ReturnValues retval = read(lexrc, rt); LYXERR(Debug::TCLASS, "Finished reading " + translateReadType(rt) + ": " + to_utf8(makeDisplayPath(filename.absFileName()))); return retval; }
FileName GuiClipboard::getPastedGraphicsFileName(Cursor const & cur, Clipboard::GraphicsType & type) const { // create file dialog filter according to the existing types in the clipboard vector<Clipboard::GraphicsType> types; if (hasGraphicsContents(Clipboard::EmfGraphicsType)) types.push_back(Clipboard::EmfGraphicsType); if (hasGraphicsContents(Clipboard::WmfGraphicsType)) types.push_back(Clipboard::WmfGraphicsType); if (hasGraphicsContents(Clipboard::LinkBackGraphicsType)) types.push_back(Clipboard::LinkBackGraphicsType); if (hasGraphicsContents(Clipboard::PdfGraphicsType)) types.push_back(Clipboard::PdfGraphicsType); if (hasGraphicsContents(Clipboard::PngGraphicsType)) types.push_back(Clipboard::PngGraphicsType); if (hasGraphicsContents(Clipboard::JpegGraphicsType)) types.push_back(Clipboard::JpegGraphicsType); LASSERT(!types.empty(), /**/); // select prefered type if AnyGraphicsType was passed if (type == Clipboard::AnyGraphicsType) type = types.front(); // which extension? map<Clipboard::GraphicsType, string> extensions; map<Clipboard::GraphicsType, docstring> typeNames; extensions[Clipboard::EmfGraphicsType] = "emf"; extensions[Clipboard::WmfGraphicsType] = "wmf"; extensions[Clipboard::LinkBackGraphicsType] = "linkback"; extensions[Clipboard::PdfGraphicsType] = "pdf"; extensions[Clipboard::PngGraphicsType] = "png"; extensions[Clipboard::JpegGraphicsType] = "jpeg"; typeNames[Clipboard::EmfGraphicsType] = _("Enhanced Metafile"); typeNames[Clipboard::WmfGraphicsType] = _("Windows Metafile"); typeNames[Clipboard::LinkBackGraphicsType] = _("LinkBack PDF"); typeNames[Clipboard::PdfGraphicsType] = _("PDF"); typeNames[Clipboard::PngGraphicsType] = _("PNG"); typeNames[Clipboard::JpegGraphicsType] = _("JPEG"); // find unused filename with primary extension string document_path = cur.buffer()->fileName().onlyPath().absFileName(); unsigned newfile_number = 0; FileName filename; do { ++newfile_number; filename = FileName(addName(document_path, to_utf8(_("pasted")) + convert<string>(newfile_number) + "." + extensions[type])); } while (filename.isReadableFile()); while (true) { // create file type filter, putting the prefered on to the front QStringList filter; for (size_t i = 0; i != types.size(); ++i) { docstring s = bformat(_("%1$s Files"), typeNames[types[i]]) + " (*." + from_ascii(extensions[types[i]]) + ")"; if (types[i] == type) filter.prepend(toqstr(s)); else filter.append(toqstr(s)); } filter = fileFilters(filter.join(";;")); // show save dialog for the graphic FileDialog dlg(qt_("Choose a filename to save the pasted graphic as")); FileDialog::Result result = dlg.save(toqstr(filename.onlyPath().absFileName()), filter, toqstr(filename.onlyFileName())); if (result.first == FileDialog::Later) return FileName(); string newFilename = fromqstr(result.second); if (newFilename.empty()) { cur.bv().message(_("Canceled.")); return FileName(); } filename.set(newFilename); // check the extension (the user could have changed it) if (!suffixIs(ascii_lowercase(filename.absFileName()), "." + extensions[type])) { // the user changed the extension. Check if the type is available size_t i; for (i = 1; i != types.size(); ++i) { if (suffixIs(ascii_lowercase(filename.absFileName()), "." + extensions[types[i]])) { type = types[i]; break; } } // invalid extension found, or none at all. In the latter // case set the default extensions. if (i == types.size() && filename.onlyFileName().find('.') == string::npos) { filename.changeExtension("." + extensions[type]); } } // check whether the file exists and warn the user if (!filename.exists()) break; int ret = frontend::Alert::prompt( _("Overwrite external file?"), bformat(_("File %1$s already exists, do you want to overwrite it?"), from_utf8(filename.absFileName())), 1, 1, _("&Overwrite"), _("&Cancel")); if (ret == 0) // overwrite, hence break the dialog loop break; // not overwrite, hence show the dialog again (i.e. loop) } return filename; }
string const doSubstitution(InsetExternalParams const & params, Buffer const & buffer, string const & s, bool use_latex_path, bool external_in_tmpdir, Substitute what) { Buffer const * masterBuffer = buffer.masterBuffer(); string const parentpath = external_in_tmpdir ? masterBuffer->temppath() : buffer.filePath(); string const filename = external_in_tmpdir ? params.filename.mangledFileName() : params.filename.outputFileName(parentpath); string const basename = changeExtension( onlyFileName(filename), string()); string const absname = makeAbsPath(filename, parentpath).absFileName(); string result = s; if (what != ALL_BUT_PATHS) { string const filepath = onlyPath(filename); string const abspath = onlyPath(absname); string const masterpath = external_in_tmpdir ? masterBuffer->temppath() : masterBuffer->filePath(); // FIXME UNICODE string relToMasterPath = onlyPath( to_utf8(makeRelPath(from_utf8(absname), from_utf8(masterpath)))); if (relToMasterPath == "./") relToMasterPath.clear(); // FIXME UNICODE string relToParentPath = onlyPath( to_utf8(makeRelPath(from_utf8(absname), from_utf8(parentpath)))); if (relToParentPath == "./") relToParentPath.clear(); result = subst_path(result, "$$FPath", filepath, use_latex_path, PROTECT_EXTENSION, ESCAPE_DOTS); result = subst_path(result, "$$AbsPath", abspath, use_latex_path, PROTECT_EXTENSION, ESCAPE_DOTS); result = subst_path(result, "$$RelPathMaster", relToMasterPath, use_latex_path, PROTECT_EXTENSION, ESCAPE_DOTS); result = subst_path(result, "$$RelPathParent", relToParentPath, use_latex_path, PROTECT_EXTENSION, ESCAPE_DOTS); if (FileName::isAbsolute(filename)) { result = subst_path(result, "$$AbsOrRelPathMaster", abspath, use_latex_path, PROTECT_EXTENSION, ESCAPE_DOTS); result = subst_path(result, "$$AbsOrRelPathParent", abspath, use_latex_path, PROTECT_EXTENSION, ESCAPE_DOTS); } else { result = subst_path(result, "$$AbsOrRelPathMaster", relToMasterPath, use_latex_path, PROTECT_EXTENSION, ESCAPE_DOTS); result = subst_path(result, "$$AbsOrRelPathParent", relToParentPath, use_latex_path, PROTECT_EXTENSION, ESCAPE_DOTS); } } if (what == PATHS) return result; result = subst_path(result, "$$FName", filename, use_latex_path, EXCLUDE_EXTENSION); result = subst_path(result, "$$Basename", basename, use_latex_path, PROTECT_EXTENSION, ESCAPE_DOTS); result = subst_path(result, "$$Extension", '.' + getExtension(filename), use_latex_path); result = subst_path(result, "$$Tempname", params.tempname().absFileName(), use_latex_path); result = subst_path(result, "$$Sysdir", package().system_support().absFileName(), use_latex_path); // Handle the $$Contents(filename) syntax if (contains(result, "$$Contents(\"")) { // Since use_latex_path may be true we must extract the file // name from s instead of result and do the substitutions // again, this time with use_latex_path false. size_t const spos = s.find("$$Contents(\""); size_t const send = s.find("\")", spos); string const file_template = s.substr(spos + 12, send - (spos + 12)); string const file = doSubstitution(params, buffer, file_template, false, external_in_tmpdir, what); string contents; FileName const absfile( makeAbsPath(file, masterBuffer->temppath())); if (absfile.isReadableFile()) // FIXME UNICODE contents = to_utf8(absfile.fileContents("UTF-8")); size_t const pos = result.find("$$Contents(\""); size_t const end = result.find("\")", pos); result.replace(pos, end + 2, contents); } return result; }