initial commit Base app, core lib, plugin system

This commit is contained in:
Zdenek Jonak
2015-10-28 19:21:55 +01:00
commit 53d56f2e13
30 changed files with 1004 additions and 0 deletions
+51
View File
@@ -0,0 +1,51 @@
#include <QDir>
#include <QApplication>
#include <QPluginLoader>
#include "context.h"
#include "iplugin.h"
#include <odb/sqlite/database.hxx>
Context &Context::instance()
{
static Context ctx;
return ctx;
}
QList<IPlugin *> Context::plugins()
{
return m_plugins;
}
void Context::loadPlugins()
{
QDir pluginsDir(qApp->applicationDirPath() + "/../plugins");
foreach (QString fileName, pluginsDir.entryList(QStringList() << "*.so")) {
QPluginLoader pluginLoader(pluginsDir.absoluteFilePath(fileName));
QObject *p = pluginLoader.instance();
if (p != NULL) {
IPlugin *plugin = qobject_cast<IPlugin*>(p);
if (plugin != NULL) {
plugin->init();
m_plugins.append(plugin);
}
}
}
}
void Context::openDb(const QString &path)
{
if (m_db != NULL) {
delete m_db;
}
m_db = new odb::sqlite::database(path.toStdString());
}
Context::Context()
{
m_db = NULL;
}
+25
View File
@@ -0,0 +1,25 @@
#ifndef CONTEXT_H
#define CONTEXT_H
#include <QList>
#include <odb/database.hxx>
class IPlugin;
class Context
{
public:
static Context &instance();
QList<IPlugin*> plugins();
void loadPlugins();
void openDb(const QString &path);
odb::database *db();
private:
Context();
QList<IPlugin*> m_plugins;
odb::database *m_db;
};
#endif // CONTEXT_H
+6
View File
@@ -0,0 +1,6 @@
#include "core.h"
Core::Core()
{
}
+13
View File
@@ -0,0 +1,13 @@
#ifndef CORE_H
#define CORE_H
#include "core_global.h"
class CORESHARED_EXPORT Core
{
public:
Core();
};
#endif // CORE_H
+32
View File
@@ -0,0 +1,32 @@
#-------------------------------------------------
#
# Project created by QtCreator 2015-10-28T15:25:33
#
#-------------------------------------------------
QT += widgets
TARGET = core
TEMPLATE = lib
DEFINES += CORE_LIBRARY
SOURCES += core.cpp \
data/user.cpp \
context.cpp
HEADERS += core.h\
core_global.h \
iplugin.h \
service.h \
data/user.h \
context.h
unix {
target.path = /usr/lib
INSTALLS += target
}
ODB_FILES = core/data/user.h
H_DIR = $$PWD/data/*.h
include(../odb.pri)
+12
View File
@@ -0,0 +1,12 @@
#ifndef CORE_GLOBAL_H
#define CORE_GLOBAL_H
#include <QtCore/qglobal.h>
#if defined(CORE_LIBRARY)
# define CORESHARED_EXPORT Q_DECL_EXPORT
#else
# define CORESHARED_EXPORT Q_DECL_IMPORT
#endif
#endif // CORE_GLOBAL_H
+38
View File
@@ -0,0 +1,38 @@
#include "user.h"
User::User()
{
}
int User::getId() const
{
return id;
}
void User::setId(int value)
{
id = value;
}
QString User::getLogin() const
{
return login;
}
void User::setLogin(const QString &value)
{
login = value;
}
QString User::getPassword() const
{
return password;
}
void User::setPassword(const QString &value)
{
password = value;
}
+34
View File
@@ -0,0 +1,34 @@
#ifndef USER_H
#define USER_H
#include <QObject>
#include <QString>
#include <odb/core.hxx>
#pragma db object
class User : public QObject
{
Q_OBJECT
public:
User();
int getId() const;
void setId(int value);
QString getLogin() const;
void setLogin(const QString &value);
QString getPassword() const;
void setPassword(const QString &value);
private:
friend class odb::access;
#pragma db id auto
int id;
QString login;
QString password;
};
#endif // USER_H
+39
View File
@@ -0,0 +1,39 @@
#ifndef IPLUGIN_H
#define IPLUGIN_H
#include <QString>
#include <QWidget>
#include <QtPlugin>
#include "service.h"
class IPlugin
{
public:
IPlugin() {
m_ui = NULL;
}
virtual ~IPlugin() { }
virtual QString pluginName() = 0;
virtual QString pluginId() = 0;
virtual void init() = 0;
virtual QWidget *ui() {
return m_ui;
}
template<class T>
Service<T> *service() {
(Service<T>*)m_service;
}
protected:
QWidget *m_ui;
void *m_service;
};
#define PluginInterface_iid "cz.itsolved.prodejna.IPlugin"
Q_DECLARE_INTERFACE(IPlugin, PluginInterface_iid)
#endif // IPLUGIN_H
+48
View File
@@ -0,0 +1,48 @@
#ifndef SERVICE_H
#define SERVICE_H
#include <QList>
#include <QSharedPointer>
#include <odb/core.hxx>
#include <odb/transaction.hxx>
#include <odb/database.hxx>
#include <odb/result.hxx>
template<class T>
class Service
{
public:
Service() { }
QList<QSharedPointer<T> > all() {
odb::database *db = Context::instance().db();
odb::transaction tx(db->begin());
odb::result<T> res = db->template query<T>();
QList<QSharedPointer<T> > ret;
for (typename odb::result<T>::iterator it = res.begin(); it != res.end(); it++) {
QSharedPointer<T> entity = db->template load<T>(((T*)*it)->id());
ret.append(entity);
}
tx.commit();
return ret;
}
void save(QSharedPointer<T> entity) {
odb::database *db = Context::instance().db();
odb::transaction tx(db->begin());
db->persist(entity);
tx.commit();
}
QSharedPointer<T> loadById(int id) {
odb::database *db = Context::instance().db();
odb::transaction tx(db->begin());
db->template load<T>(id);
tx.commit();
}
};
#endif // SERVICE_H