]> git.lyx.org Git - features.git/commitdiff
For Qt4.4 and up: Detach Buffer autosave into a new thread.
authorAbdelrazak Younes <younes@lyx.org>
Sun, 13 Dec 2009 21:00:46 +0000 (21:00 +0000)
committerAbdelrazak Younes <younes@lyx.org>
Sun, 13 Dec 2009 21:00:46 +0000 (21:00 +0000)
* Buffer: new clone() method. When this new autosave method is used the old autoSave() is not of course.

* GuiView: clone the current document buffer and save it in a new thread.

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@32512 a592a061-630c-0410-9148-cb99ea01b6c8

src/Buffer.cpp
src/Buffer.h
src/frontends/qt4/GuiView.cpp
src/frontends/qt4/GuiView.h

index 1a37dcc2102d32f85b69a8cfafd3b4ebb9c1d450..90512f0b63cbab0329fd588e93584ff600bbbd96 100644 (file)
@@ -356,6 +356,17 @@ Buffer::~Buffer()
 }
 
 
+Buffer * Buffer::clone() const
+{
+       Buffer * clone = new Buffer(fileName().absFilename(), false);
+       clone->d->file_fully_loaded = true;
+       clone->d->params = d->params;
+       clone->d->inset = static_cast<InsetText *>(d->inset->clone());
+       clone->d->inset->setBuffer(*clone);
+       return clone;
+}
+
+
 void Buffer::changed() const
 {
        if (d->wa_)
index 05050fe7af6ec013ce5acf6341d6c3a8f89fa72a..f745aa04b8563cc2fb43eff35324156c2f5966dd 100644 (file)
@@ -130,6 +130,9 @@ public:
        /// Destructor
        ~Buffer();
 
+       ///
+       Buffer * clone() const;
+
        /** High-level interface to buffer functionality.
            This function parses a command string and executes it.
        */
index 1ec7a2f13e03f3ac63211bf3f736cf6b7b36a80d..1967eacf41a320a92362fa680076d77a1279b513 100644 (file)
 #include <QUrl>
 #include <QScrollBar>
 
+// QtConcurrent was introduced in Qt 4.4
+#if (QT_VERSION >= 0x040400)
+#include <QFuture>
+#include <QFutureWatcher>
+#include <QtConcurrentRun>
+#endif
+
 #include <boost/bind.hpp>
 
 #include <sstream>
@@ -290,6 +297,11 @@ public:
 
        ///
        TocModels toc_models_;
+
+#if (QT_VERSION >= 0x040400)
+       ///
+       QFutureWatcher<bool> autosave_watcher_;
+#endif
 };
 
 
@@ -350,6 +362,11 @@ GuiView::GuiView(int id)
        // clear session data if any.
        QSettings settings;
        settings.remove("views");
+
+#if (QT_VERSION >= 0x040400)
+       connect(&d.autosave_watcher_, SIGNAL(finished()), this,
+               SLOT(autoSaveFinished()));
+#endif
 }
 
 
@@ -359,6 +376,16 @@ GuiView::~GuiView()
 }
 
 
+void GuiView::autoSaveFinished()
+{
+#if (QT_VERSION >= 0x040400)
+       docstring const msg = d.autosave_watcher_.result()
+               ? _("Automatic save done.") : _("Automatic save failed!");
+       message(msg);
+#endif
+}
+
+
 void GuiView::saveLayout() const
 {
        QSettings settings;
@@ -1195,12 +1222,39 @@ BufferView const * GuiView::currentBufferView() const
 }
 
 
+static bool saveAndDestroyBuffer(Buffer * buffer, FileName const & fname)
+{
+       bool failed = true;
+       FileName const tmp_ret = FileName::tempName("lyxauto");
+       if (!tmp_ret.empty()) {
+               if (buffer->writeFile(tmp_ret))
+                       failed = !tmp_ret.moveTo(fname);
+       }
+       if (failed) {
+               // failed to write/rename tmp_ret so try writing direct
+               failed = buffer->writeFile(fname);
+       }
+       delete buffer;
+       return !failed;
+}
+
+
 void GuiView::autoSave()
 {
        LYXERR(Debug::INFO, "Running autoSave()");
 
-       if (documentBufferView())
-               documentBufferView()->buffer().autoSave();
+       Buffer * buffer = documentBufferView()
+               ? &documentBufferView()->buffer() : 0;
+       if (!buffer)
+               return;
+
+#if (QT_VERSION >= 0x040400)
+       QFuture<bool> f = QtConcurrent::run(saveAndDestroyBuffer, buffer->clone(),
+               buffer->getAutosaveFilename());
+       d.autosave_watcher_.setFuture(f);
+#else
+       buffer->autoSave();
+#endif
 }
 
 
index 869107b0f80058b13a2c0c5c9605bc3ffc95e624..7a610f0199d76da9f40ed28b2c0e6b4fad68751c 100644 (file)
@@ -180,6 +180,9 @@ private Q_SLOTS:
        void normalSizedIcons();
        void bigSizedIcons();
 
+       /// For completion of Buffer autosave thread.
+       void autoSaveFinished();
+
 private:
        /// Open given child document in current buffer directory.
        void openChildDocument(std::string const & filename);