+ return FileName(fromqstr(result.second));
+}
+
+
+Buffer * GuiView::loadDocument(FileName const & filename, bool tolastfiles)
+{
+ setBusy(true);
+
+ Buffer * newBuffer = checkAndLoadLyXFile(filename);
+
+ if (!newBuffer) {
+ message(_("Document not loaded."));
+ setBusy(false);
+ return 0;
+ }
+
+ setBuffer(newBuffer);
+
+ // scroll to the position when the file was last closed
+ if (lyxrc.use_lastfilepos) {
+ LastFilePosSection::FilePos filepos =
+ LyX::ref().session().lastFilePos().load(filename);
+ view()->moveToPosition(filepos.pit, filepos.pos, 0, 0);
+ }
+
+ if (tolastfiles)
+ LyX::ref().session().lastFiles().add(filename);
+
+ setBusy(false);
+ return newBuffer;
+}
+
+
+void GuiView::openDocument(string const & fname)
+{
+ string initpath = lyxrc.document_path;
+
+ if (buffer()) {
+ string const trypath = buffer()->filePath();
+ // If directory is writeable, use this as default.
+ if (FileName(trypath).isDirWritable())
+ initpath = trypath;
+ }
+
+ string filename;
+
+ if (fname.empty()) {
+ FileDialog dlg(qt_("Select document to open"), LFUN_FILE_OPEN);
+ dlg.setButton1(qt_("Documents|#o#O"), toqstr(lyxrc.document_path));
+ dlg.setButton2(qt_("Examples|#E#e"),
+ toqstr(addPath(package().system_support().absFilename(), "examples")));
+
+ FileDialog::Result result =
+ dlg.open(toqstr(initpath), FileFilterList(_("LyX Documents (*.lyx)")));
+
+ if (result.first == FileDialog::Later)
+ return;
+
+ filename = fromqstr(result.second);
+
+ // check selected filename
+ if (filename.empty()) {
+ message(_("Canceled."));
+ return;
+ }
+ } else
+ filename = fname;
+
+ // get absolute path of file and add ".lyx" to the filename if
+ // necessary.
+ FileName const fullname =
+ fileSearch(string(), filename, "lyx", support::may_not_exist);
+ if (!fullname.empty())
+ filename = fullname.absFilename();
+
+ // if the file doesn't exist, let the user create one
+ if (!fullname.exists()) {
+ // the user specifically chose this name. Believe him.
+ Buffer * const b = newFile(filename, string(), true);
+ if (b)
+ setBuffer(b);
+ return;
+ }
+
+ docstring const disp_fn = makeDisplayPath(filename);
+ message(bformat(_("Opening document %1$s..."), disp_fn));
+
+ docstring str2;
+ Buffer * buf = loadDocument(fullname);
+ if (buf) {
+ updateLabels(*buf);
+
+ setBuffer(buf);
+ buf->errors("Parse");
+ str2 = bformat(_("Document %1$s opened."), disp_fn);
+ } else {
+ str2 = bformat(_("Could not open document %1$s"), disp_fn);
+ }
+ message(str2);
+}
+
+// FIXME: clean that
+static bool import(GuiView * lv, FileName const & filename,
+ string const & format, ErrorList & errorList)
+{
+ FileName const lyxfile(support::changeExtension(filename.absFilename(), ".lyx"));
+
+ string loader_format;
+ vector<string> loaders = theConverters().loaders();
+ if (find(loaders.begin(), loaders.end(), format) == loaders.end()) {
+ for (vector<string>::const_iterator it = loaders.begin();
+ it != loaders.end(); ++it) {
+ if (!theConverters().isReachable(format, *it))
+ continue;
+
+ string const tofile =
+ support::changeExtension(filename.absFilename(),
+ formats.extension(*it));
+ if (!theConverters().convert(0, filename, FileName(tofile),
+ filename, format, *it, errorList))
+ return false;
+ loader_format = *it;
+ break;
+ }
+ if (loader_format.empty()) {
+ frontend::Alert::error(_("Couldn't import file"),
+ bformat(_("No information for importing the format %1$s."),
+ formats.prettyName(format)));
+ return false;
+ }
+ } else
+ loader_format = format;
+
+ if (loader_format == "lyx") {
+ Buffer * buf = lv->loadDocument(lyxfile);
+ if (!buf)
+ return false;
+ updateLabels(*buf);
+ lv->setBuffer(buf);
+ buf->errors("Parse");
+ } else {
+ Buffer * const b = newFile(lyxfile.absFilename(), string(), true);
+ if (!b)
+ return false;
+ lv->setBuffer(b);
+ bool as_paragraphs = loader_format == "textparagraph";
+ string filename2 = (loader_format == format) ? filename.absFilename()
+ : support::changeExtension(filename.absFilename(),
+ formats.extension(loader_format));
+ lv->view()->insertPlaintextFile(FileName(filename2), as_paragraphs);
+ theLyXFunc().setLyXView(lv);
+ lyx::dispatch(FuncRequest(LFUN_MARK_OFF));
+ }
+
+ return true;
+}
+
+
+void GuiView::importDocument(string const & argument)
+{
+ string format;
+ string filename = split(argument, format, ' ');
+
+ LYXERR(Debug::INFO, format << " file: " << filename);
+
+ // need user interaction
+ if (filename.empty()) {
+ string initpath = lyxrc.document_path;
+
+ Buffer const * buf = buffer();
+ if (buf) {
+ string const trypath = buf->filePath();
+ // If directory is writeable, use this as default.
+ if (FileName(trypath).isDirWritable())
+ initpath = trypath;
+ }
+
+ docstring const text = bformat(_("Select %1$s file to import"),
+ formats.prettyName(format));
+
+ FileDialog dlg(toqstr(text), LFUN_BUFFER_IMPORT);
+ dlg.setButton1(qt_("Documents|#o#O"), toqstr(lyxrc.document_path));
+ dlg.setButton2(qt_("Examples|#E#e"),
+ toqstr(addPath(package().system_support().absFilename(), "examples")));
+
+ docstring filter = formats.prettyName(format);
+ filter += " (*.";
+ // FIXME UNICODE
+ filter += from_utf8(formats.extension(format));
+ filter += ')';
+
+ FileDialog::Result result =
+ dlg.open(toqstr(initpath), FileFilterList(filter));
+
+ if (result.first == FileDialog::Later)
+ return;
+
+ filename = fromqstr(result.second);
+
+ // check selected filename
+ if (filename.empty())
+ message(_("Canceled."));
+ }
+
+ if (filename.empty())
+ return;
+
+ // get absolute path of file
+ FileName const fullname(makeAbsPath(filename));
+
+ FileName const lyxfile(support::changeExtension(fullname.absFilename(), ".lyx"));
+
+ // Check if the document already is open
+ Buffer * buf = theBufferList().getBuffer(lyxfile.absFilename());
+ if (buf) {
+ setBuffer(buf);
+ if (!closeBuffer()) {
+ message(_("Canceled."));
+ return;
+ }
+ }
+
+ docstring const displaypath = makeDisplayPath(lyxfile.absFilename(), 30);
+
+ // if the file exists already, and we didn't do
+ // -i lyx thefile.lyx, warn
+ if (lyxfile.exists() && fullname != lyxfile) {
+
+ docstring text = bformat(_("The document %1$s already exists.\n\n"
+ "Do you want to overwrite that document?"), displaypath);
+ int const ret = Alert::prompt(_("Overwrite document?"),
+ text, 0, 1, _("&Overwrite"), _("&Cancel"));
+
+ if (ret == 1) {
+ message(_("Canceled."));
+ return;
+ }
+ }
+
+ message(bformat(_("Importing %1$s..."), displaypath));
+ ErrorList errorList;
+ if (import(this, fullname, format, errorList))
+ message(_("imported."));
+ else
+ message(_("file not imported!"));
+
+ // FIXME (Abdel 12/08/06): Is there a need to display the error list here?