From 91bfdfe57cc5558e8c0e6b4ed2720e074d699d4b Mon Sep 17 00:00:00 2001 From: Abdelrazak Younes Date: Sat, 12 Sep 2009 20:41:24 +0000 Subject: [PATCH] Move LFUN_BUFFER_RELOAD and LFUN_VC_* to GuiView. Try to use the document buffer correctly. git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@31376 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/LyXFunc.cpp | 205 ------------------------------ src/LyXFunc.h | 5 - src/frontends/qt4/GuiView.cpp | 229 +++++++++++++++++++++++++++++++++- src/frontends/qt4/GuiView.h | 4 + 4 files changed, 231 insertions(+), 212 deletions(-) diff --git a/src/LyXFunc.cpp b/src/LyXFunc.cpp index 0025ac5b81..4a32f01ed1 100644 --- a/src/LyXFunc.cpp +++ b/src/LyXFunc.cpp @@ -475,30 +475,6 @@ FuncStatus LyXFunc::getStatus(FuncRequest const & cmd) const enable = buf->isExportable("program"); break; - case LFUN_VC_REGISTER: - enable = !buf->lyxvc().inUse(); - break; - case LFUN_VC_CHECK_IN: - enable = buf->lyxvc().checkInEnabled(); - break; - case LFUN_VC_CHECK_OUT: - enable = buf->lyxvc().checkOutEnabled(); - break; - case LFUN_VC_LOCKING_TOGGLE: - enable = !buf->isReadonly() && buf->lyxvc().lockingToggleEnabled(); - flag.setOnOff(enable && !buf->lyxvc().locker().empty()); - break; - case LFUN_VC_REVERT: - enable = buf->lyxvc().inUse(); - break; - case LFUN_VC_UNDO_LAST: - enable = buf->lyxvc().undoLastEnabled(); - break; - case LFUN_BUFFER_RELOAD: - enable = !buf->isUnnamed() && buf->fileName().exists() - && (!buf->isClean() || buf->isExternallyModified(Buffer::timestamp_method)); - break; - case LFUN_CITATION_INSERT: { FuncRequest fr(LFUN_INSET_INSERT, "citation"); enable = getStatus(fr).enabled(); @@ -590,15 +566,6 @@ FuncStatus LyXFunc::getStatus(FuncRequest const & cmd) const break; } - case LFUN_VC_COMMAND: { - if (cmd.argument().empty()) - enable = false; - - if (!buf && contains(cmd.getArg(0), 'D')) - enable = false; - break; - } - case LFUN_MASTER_BUFFER_UPDATE: case LFUN_MASTER_BUFFER_VIEW: if (!buf->parent()) { @@ -731,36 +698,6 @@ FuncStatus LyXFunc::getStatus(FuncRequest const & cmd) const } -bool LyXFunc::ensureBufferClean(BufferView * bv) -{ - Buffer & buf = bv->buffer(); - if (buf.isClean() && !buf.isUnnamed()) - return true; - - docstring const file = buf.fileName().displayName(30); - docstring title; - docstring text; - if (!buf.isUnnamed()) { - text = bformat(_("The document %1$s has unsaved " - "changes.\n\nDo you want to save " - "the document?"), file); - title = _("Save changed document?"); - - } else { - text = bformat(_("The document %1$s has not been " - "saved yet.\n\nDo you want to save " - "the document?"), file); - title = _("Save new document?"); - } - int const ret = Alert::prompt(title, text, 0, 1, _("&Save"), _("&Cancel")); - - if (ret == 0) - lyx_view_->dispatch(FuncRequest(LFUN_BUFFER_WRITE)); - - return buf.isClean() && !buf.isUnnamed(); -} - - namespace { bool loadLayoutFile(string const & name, string const & buf_path) @@ -861,19 +798,6 @@ void LyXFunc::dispatch(FuncRequest const & cmd) updateFlags = Update::None; break; - case LFUN_BUFFER_RELOAD: { - LASSERT(lyx_view_ && buffer, /**/); - docstring const file = makeDisplayPath(buffer->absFileName(), 20); - docstring text = bformat(_("Any changes will be lost. Are you sure " - "you want to revert to the saved version of the document %1$s?"), file); - int const ret = Alert::prompt(_("Revert to saved document?"), - text, 1, 1, _("&Revert"), _("&Cancel")); - - if (ret == 0) - reloadBuffer(); - break; - } - case LFUN_BUFFER_UPDATE: { LASSERT(lyx_view_ && lyx_view_->documentBufferView(), /**/); Buffer & doc_buffer = lyx_view_->documentBufferView()->buffer(); @@ -1017,68 +941,6 @@ void LyXFunc::dispatch(FuncRequest const & cmd) break; } - // --- version control ------------------------------- - case LFUN_VC_REGISTER: - LASSERT(lyx_view_ && buffer, /**/); - if (!ensureBufferClean(lyx_view_->documentBufferView())) - break; - if (!buffer->lyxvc().inUse()) { - if (buffer->lyxvc().registrer()) - reloadBuffer(); - } - updateFlags = Update::Force; - break; - - case LFUN_VC_CHECK_IN: - LASSERT(lyx_view_ && buffer, /**/); - if (!ensureBufferClean(lyx_view_->documentBufferView())) - break; - if (buffer->lyxvc().inUse() - && !buffer->isReadonly()) { - setMessage(from_utf8(buffer->lyxvc().checkIn())); - reloadBuffer(); - } - break; - - case LFUN_VC_CHECK_OUT: - LASSERT(lyx_view_ && buffer, /**/); - if (!ensureBufferClean(lyx_view_->documentBufferView())) - break; - if (buffer->lyxvc().inUse()) { - setMessage(from_utf8(buffer->lyxvc().checkOut())); - reloadBuffer(); - } - break; - - case LFUN_VC_LOCKING_TOGGLE: - LASSERT(lyx_view_ && buffer, /**/); - if (!ensureBufferClean(lyx_view_->documentBufferView()) - || buffer->isReadonly()) - break; - if (buffer->lyxvc().inUse()) { - string res = buffer->lyxvc().lockingToggle(); - if (res.empty()) - frontend::Alert::error(_("Revision control error."), - _("Error when setting the locking property.")); - else { - setMessage(from_utf8(res)); - reloadBuffer(); - } - } - break; - - case LFUN_VC_REVERT: - LASSERT(lyx_view_ && buffer, /**/); - buffer->lyxvc().revert(); - reloadBuffer(); - break; - - case LFUN_VC_UNDO_LAST: - LASSERT(lyx_view_ && buffer, /**/); - buffer->lyxvc().undoLast(); - reloadBuffer(); - break; - // --- lyxserver commands ---------------------------- case LFUN_SERVER_GET_FILENAME: LASSERT(lyx_view_ && buffer, /**/); @@ -1595,49 +1457,6 @@ void LyXFunc::dispatch(FuncRequest const & cmd) theSession().bookmarks().clear(); break; - case LFUN_VC_COMMAND: { - string flag = cmd.getArg(0); - if (buffer && contains(flag, 'R') - && !ensureBufferClean(lyx_view_->documentBufferView())) - break; - docstring message; - if (contains(flag, 'M')) - if (!Alert::askForText(message, _("LyX VC: Log Message"))) - break; - - string path = cmd.getArg(1); - if (contains(path, "$$p") && buffer) - path = subst(path, "$$p", buffer->filePath()); - LYXERR(Debug::LYXVC, "Directory: " << path); - FileName pp(path); - if (!pp.isReadableDirectory()) { - lyxerr << _("Directory is not accessible.") << endl; - break; - } - support::PathChanger p(pp); - - string command = cmd.getArg(2); - if (command.empty()) - break; - if (buffer) { - command = subst(command, "$$i", buffer->absFileName()); - command = subst(command, "$$p", buffer->filePath()); - } - command = subst(command, "$$m", to_utf8(message)); - LYXERR(Debug::LYXVC, "Command: " << command); - Systemcall one; - one.startscript(Systemcall::Wait, command); - - if (!buffer) - break; - if (contains(flag, 'I')) - buffer->markDirty(); - if (contains(flag, 'R')) - reloadBuffer(); - - break; - } - default: LASSERT(theApp(), /**/); // Let the frontend dispatch its own actions. @@ -1805,30 +1624,6 @@ void LyXFunc::sendDispatchMessage(docstring const & msg, FuncRequest const & cmd } -void LyXFunc::reloadBuffer() -{ - Buffer * buf = &lyx_view_->documentBufferView()->buffer(); - FileName filename = buf->fileName(); - // The user has already confirmed that the changes, if any, should - // be discarded. So we just release the Buffer and don't call closeBuffer(); - theBufferList().release(buf); - // if the lyx_view_ has been destroyed, create a new one - if (!lyx_view_) - theApp()->dispatch(FuncRequest(LFUN_WINDOW_NEW)); - buf = lyx_view_->loadDocument(filename); - docstring const disp_fn = makeDisplayPath(filename.absFilename()); - docstring str; - if (buf) { - buf->updateLabels(); - lyx_view_->setBuffer(buf); - buf->errors("Parse"); - str = bformat(_("Document %1$s reloaded."), disp_fn); - } else { - str = bformat(_("Could not reload document %1$s"), disp_fn); - } - lyx_view_->message(str); -} - // Each "lyx_view_" should have it's own message method. lyxview and // the minibuffer would use the minibuffer, but lyxserver would // send an ERROR signal to its client. Alejandro 970603 diff --git a/src/LyXFunc.h b/src/LyXFunc.h index c301a461ed..20c3da9477 100644 --- a/src/LyXFunc.h +++ b/src/LyXFunc.h @@ -121,11 +121,6 @@ private: /// send a post-dispatch status message void sendDispatchMessage(docstring const & msg, FuncRequest const & ev); - - /// - void reloadBuffer(); - /// make sure the document is saved - bool ensureBufferClean(BufferView * bv); /// void updateLayout(DocumentClass const * const oldlayout, Buffer * buffer); }; diff --git a/src/frontends/qt4/GuiView.cpp b/src/frontends/qt4/GuiView.cpp index 76961e95eb..31688d0e7f 100644 --- a/src/frontends/qt4/GuiView.cpp +++ b/src/frontends/qt4/GuiView.cpp @@ -71,6 +71,8 @@ #include "support/lstrings.h" #include "support/os.h" #include "support/Package.h" +#include "support/Path.h" +#include "support/Systemcall.h" #include "support/Timeout.h" #include @@ -1175,6 +1177,8 @@ bool GuiView::getStatus(FuncRequest const & cmd, FuncStatus & flag) bool enable = true; Buffer * buf = currentBufferView() ? ¤tBufferView()->buffer() : 0; + Buffer * doc_buffer = documentBufferView() + ? &(documentBufferView()->buffer()) : 0; if (cmd.origin == FuncRequest::TOC) { GuiToc * toc = static_cast(findOrBuild("toc", false)); @@ -1187,6 +1191,13 @@ bool GuiView::getStatus(FuncRequest const & cmd, FuncStatus & flag) } switch(cmd.action) { + case LFUN_BUFFER_RELOAD: + enable = doc_buffer && !doc_buffer->isUnnamed() + && doc_buffer->fileName().exists() + && (!doc_buffer->isClean() + || doc_buffer->isExternallyModified(Buffer::timestamp_method)); + break; + case LFUN_BUFFER_WRITE: enable = buf && (buf->isUnnamed() || !buf->isClean()); break; @@ -1327,11 +1338,39 @@ bool GuiView::getStatus(FuncRequest const & cmd, FuncStatus & flag) case LFUN_BUFFER_SWITCH: // toggle on the current buffer, but do not toggle off // the other ones (is that a good idea?) - buf = &documentBufferView()->buffer(); - if (buf && to_utf8(cmd.argument()) == buf->absFileName()) + if (doc_buffer + && to_utf8(cmd.argument()) == doc_buffer->absFileName()) flag.setOnOff(true); break; + case LFUN_VC_REGISTER: + enable = doc_buffer && !doc_buffer->lyxvc().inUse(); + break; + case LFUN_VC_CHECK_IN: + enable = doc_buffer && doc_buffer->lyxvc().checkInEnabled(); + break; + case LFUN_VC_CHECK_OUT: + enable = doc_buffer && doc_buffer->lyxvc().checkOutEnabled(); + break; + case LFUN_VC_LOCKING_TOGGLE: + enable = doc_buffer && !doc_buffer->isReadonly() + && doc_buffer->lyxvc().lockingToggleEnabled(); + flag.setOnOff(enable && !doc_buffer->lyxvc().locker().empty()); + break; + case LFUN_VC_REVERT: + enable = doc_buffer && doc_buffer->lyxvc().inUse(); + break; + case LFUN_VC_UNDO_LAST: + enable = doc_buffer && doc_buffer->lyxvc().undoLastEnabled(); + break; + case LFUN_VC_COMMAND: { + if (cmd.argument().empty()) + enable = false; + if (!doc_buffer && contains(cmd.getArg(0), 'D')) + enable = false; + break; + } + default: return false; } @@ -2121,12 +2160,175 @@ void GuiView::gotoNextOrPreviousBuffer(NextOrPrevious np) } +/// make sure the document is saved +static bool ensureBufferClean(Buffer * buffer) +{ + LASSERT(buffer, return false); + if (buffer->isClean() && !buffer->isUnnamed()) + return true; + + docstring const file = buffer->fileName().displayName(30); + docstring title; + docstring text; + if (!buffer->isUnnamed()) { + text = bformat(_("The document %1$s has unsaved " + "changes.\n\nDo you want to save " + "the document?"), file); + title = _("Save changed document?"); + + } else { + text = bformat(_("The document %1$s has not been " + "saved yet.\n\nDo you want to save " + "the document?"), file); + title = _("Save new document?"); + } + int const ret = Alert::prompt(title, text, 0, 1, _("&Save"), _("&Cancel")); + + if (ret == 0) + dispatch(FuncRequest(LFUN_BUFFER_WRITE)); + + return buffer->isClean() && !buffer->isUnnamed(); +} + + +void GuiView::reloadBuffer() +{ + Buffer * buf = &documentBufferView()->buffer(); + FileName filename = buf->fileName(); + // The user has already confirmed that the changes, if any, should + // be discarded. So we just release the Buffer and don't call closeBuffer(); + theBufferList().release(buf); + buf = loadDocument(filename); + docstring const disp_fn = makeDisplayPath(filename.absFilename()); + docstring str; + if (buf) { + buf->updateLabels(); + setBuffer(buf); + buf->errors("Parse"); + str = bformat(_("Document %1$s reloaded."), disp_fn); + } else { + str = bformat(_("Could not reload document %1$s"), disp_fn); + } + message(str); +} + + +void GuiView::dispatchVC(FuncRequest const & cmd) +{ + Buffer * buffer = documentBufferView() + ? &(documentBufferView()->buffer()) : 0; + + switch (cmd.action) { + case LFUN_VC_REGISTER: + if (!buffer || !ensureBufferClean(buffer)) + break; + if (!buffer->lyxvc().inUse()) { + if (buffer->lyxvc().registrer()) + reloadBuffer(); + } + break; + + case LFUN_VC_CHECK_IN: + if (!buffer || !ensureBufferClean(buffer)) + break; + if (buffer->lyxvc().inUse() && !buffer->isReadonly()) { + message(from_utf8(buffer->lyxvc().checkIn())); + reloadBuffer(); + } + break; + + case LFUN_VC_CHECK_OUT: + if (!buffer || !ensureBufferClean(buffer)) + break; + if (buffer->lyxvc().inUse()) { + message(from_utf8(buffer->lyxvc().checkOut())); + reloadBuffer(); + } + break; + + case LFUN_VC_LOCKING_TOGGLE: + LASSERT(buffer, return); + if (!ensureBufferClean(buffer) || buffer->isReadonly()) + break; + if (buffer->lyxvc().inUse()) { + string res = buffer->lyxvc().lockingToggle(); + if (res.empty()) { + frontend::Alert::error(_("Revision control error."), + _("Error when setting the locking property.")); + } else { + message(from_utf8(res)); + reloadBuffer(); + } + } + break; + + case LFUN_VC_REVERT: + LASSERT(buffer, return); + buffer->lyxvc().revert(); + reloadBuffer(); + break; + + case LFUN_VC_UNDO_LAST: + LASSERT(buffer, return); + buffer->lyxvc().undoLast(); + reloadBuffer(); + break; + + case LFUN_VC_COMMAND: { + string flag = cmd.getArg(0); + if (buffer && contains(flag, 'R') && !ensureBufferClean(buffer)) + break; + docstring message; + if (contains(flag, 'M')) { + if (!Alert::askForText(message, _("LyX VC: Log Message"))) + break; + } + string path = cmd.getArg(1); + if (contains(path, "$$p") && buffer) + path = subst(path, "$$p", buffer->filePath()); + LYXERR(Debug::LYXVC, "Directory: " << path); + FileName pp(path); + if (!pp.isReadableDirectory()) { + lyxerr << _("Directory is not accessible.") << endl; + break; + } + support::PathChanger p(pp); + + string command = cmd.getArg(2); + if (command.empty()) + break; + if (buffer) { + command = subst(command, "$$i", buffer->absFileName()); + command = subst(command, "$$p", buffer->filePath()); + } + command = subst(command, "$$m", to_utf8(message)); + LYXERR(Debug::LYXVC, "Command: " << command); + Systemcall one; + one.startscript(Systemcall::Wait, command); + + if (!buffer) + break; + if (contains(flag, 'I')) + buffer->markDirty(); + if (contains(flag, 'R')) + reloadBuffer(); + + break; + } + } +} + + bool GuiView::dispatch(FuncRequest const & cmd) { BufferView * bv = currentBufferView(); // By default we won't need any update. if (bv) bv->cursor().updateFlags(Update::None); + + Buffer * doc_buffer = documentBufferView() + ? &(documentBufferView()->buffer()) : 0; + bool dispatched = true; if (cmd.origin == FuncRequest::TOC) { @@ -2190,6 +2392,19 @@ bool GuiView::dispatch(FuncRequest const & cmd) insertPlaintextFile(cmd.argument(), false); break; + case LFUN_BUFFER_RELOAD: { + LASSERT(doc_buffer, /**/); + docstring const file = makeDisplayPath(doc_buffer->absFileName(), 20); + docstring text = bformat(_("Any changes will be lost. Are you sure " + "you want to revert to the saved version of the document %1$s?"), file); + int const ret = Alert::prompt(_("Revert to saved document?"), + text, 1, 1, _("&Revert"), _("&Cancel")); + + if (ret == 0) + reloadBuffer(); + break; + } + case LFUN_BUFFER_WRITE: if (bv) saveBuffer(bv->buffer()); @@ -2400,6 +2615,16 @@ bool GuiView::dispatch(FuncRequest const & cmd) lyx::dispatch(FuncRequest(LFUN_SCREEN_FONT_UPDATE)); break; + case LFUN_VC_REGISTER: + case LFUN_VC_CHECK_IN: + case LFUN_VC_CHECK_OUT: + case LFUN_VC_LOCKING_TOGGLE: + case LFUN_VC_REVERT: + case LFUN_VC_UNDO_LAST: + case LFUN_VC_COMMAND: + dispatchVC(cmd); + break; + default: dispatched = false; break; diff --git a/src/frontends/qt4/GuiView.h b/src/frontends/qt4/GuiView.h index 7e46f5b958..f55ed40abc 100644 --- a/src/frontends/qt4/GuiView.h +++ b/src/frontends/qt4/GuiView.h @@ -327,6 +327,10 @@ private: Dialog * findOrBuild(std::string const & name, bool hide_it); /// Dialog * build(std::string const & name); + /// + void reloadBuffer(); + /// + void dispatchVC(FuncRequest const & cmd); /// This view ID. int id_; -- 2.39.5