RaiiMyList is a "threadsafe" way of using LocalMyList.
#include "../../mylistnode.h"
-
--- /dev/null
+#include "../../raiimylist.h"
messagehandler.cpp \
asyncquerytask.cpp \
service.cpp \
+ raiimylist.cpp \
servicemanager.cpp \
dynamicmodel/data.cpp \
dynamicmodel/node.cpp \
sqlresultiteratorinterface.h \
service.h \
servicemanager.h \
+ raiimylist.h \
dynamicmodel/data.h \
dynamicmodel/node.h \
dynamicmodel/model.h \
include/LocalMyList/UnknownFileLookupTask \
include/LocalMyList/FileLocationCheckTask \
include/LocalMyList/RequestHandler \
- include/LocalMyList/DirectoryWatcher
+ include/LocalMyList/DirectoryWatcher \
+ include/LocalMyList/RaiiMyList
!noscript {
QT *= script
--- /dev/null
+#include "raiimylist.h"
+
+namespace LocalMyList {
+
+RaiiMyList::RaiiMyList() : mutex{}, locker{&mutex}, ok{true}, m_connected{false}
+{
+ if (MyList::instance()->thread() != QThread::currentThread())
+ {
+ qWarning("MyList instance created from a different thread");
+ ok = false;
+ return;
+ }
+ MyList::instance()->loadLocalSettings();
+ m_connected = MyList::instance()->database()->connect();
+}
+
+RaiiMyList::~RaiiMyList()
+{
+ if (!ok) return;
+
+ MyList::instance()->database()->disconnect();
+ MyList::instance()->destroy();
+}
+
+} // namespace LocalMyList
--- /dev/null
+#ifndef RAIIMYLIST_H
+#define RAIIMYLIST_H
+
+#include "localmylist_global.h"
+#include <QMutex>
+#include <QMutexLocker>
+#include <QThread>
+#include "mylist.h"
+
+namespace LocalMyList {
+
+/**
+ * @brief A "threadsafe" way of using LocalMyList in threaded environments where there
+ * is no control over the thread from which MyList and Database are used.
+ *
+ * This is mostly a hack.
+ *
+ * Example use:
+ * {
+ * RaiiMyList rml;
+ * if (!rml || !rml.connected()) return ERROR;
+ * // do something useful here, databaseconnection is open
+ * }
+ */
+class LOCALMYLISTSHARED_EXPORT RaiiMyList
+{
+ public:
+ RaiiMyList();
+ ~RaiiMyList();
+
+ operator bool() const { return ok; }
+ bool connected() const { return m_connected; }
+
+ private:
+ QMutex mutex;
+ QMutexLocker locker;
+ bool ok;
+ bool m_connected;
+};
+
+}
+
+#endif // RAIIMYLIST_H