--- /dev/null
+#include "databasemanager.h"
+#include <QObject>
+#include <QSqlDatabase>
+#include <QSqlError>
+#include <QSqlQuery>
+#include <QFile>
+#include <QDir>
+#include <QVariant>
+#include <iostream>
+
+const QString DatabaseManager::DB_FILENAME = QString("my.db.sqlite");
+
+DatabaseManager::DatabaseManager(QObject *parent)
+ : QObject(parent)
+{}
+
+DatabaseManager::~DatabaseManager()
+{}
+
+bool DatabaseManager::openDB()
+{
+ db = QSqlDatabase::addDatabase("QSQLITE");
+ db.setDatabaseName(getDbPath());
+ return db.open();
+}
+
+QSqlError DatabaseManager::lastError()
+{
+ return db.lastError();
+}
+
+bool DatabaseManager::deleteDB()
+{
+ db.close();
+ return QFile::remove(getDbPath());
+}
+
+QString DatabaseManager::getDbPath() const
+{
+ QString path;
+#ifdef Q_OS_LINUX
+ path.append(QDir::home().path());
+ path.append(QDir::separator()).append(DB_FILENAME);
+#else
+ path.append(DB_FILENAME);
+#endif
+ return path;
+}
+
+/**
+ * Check if database already exists.
+ * Returns false if doesn't or we don't have a connection.
+*/
+bool DatabaseManager::dbExists() const
+{
+ using namespace std;
+ if (!db.isOpen())
+ {
+ cerr << "Database is not connected!" << endl;
+ return false;
+ }
+ QSqlQuery query;
+ query.exec("SELECT name FROM sqlite_master WHERE name='person'");
+ return query.next();
+}
+
+bool DatabaseManager::createDB() const
+{
+ bool ret = false;
+ if (db.isOpen())
+ {
+ QSqlQuery query;
+ ret = query.exec("create table platform "
+ "(id integer primary key, "
+ "name varchar(30), "
+ "filename varchar(125))");
+ /*ret = query.exec("create table media "
+ "(id integer primary key, "
+ "name varchar(30), "
+ "filename varchar(125))");*/
+ }
+ return ret;
+}
+
+int DatabaseManager::insertPlatform(QString name, QString filename) const
+{
+ int newId = -1;
+ bool ret = false;
+ if (db.isOpen())
+ {
+ //http://www.sqlite.org/autoinc.html
+ // NULL = is the keyword for the autoincrement to generate next value
+ QSqlQuery query;
+ query.prepare("insert into platform (id, name, filename) "
+ "values (NULL, :name, :filename)");
+ query.bindValue(":name", name);
+ query.bindValue(":filename", filename);
+ ret = query.exec();
+
+ /*ret = query.exec(QString("insert into person values(NULL,'%1','%2',%3)")
+ .arg(firstname).arg(lastname).arg(age));*/
+ // Get database given autoincrement value
+ if (ret)
+ {
+ // http://www.sqlite.org/c3ref/last_insert_rowid.html
+ QVariant var = query.lastInsertId();
+ if (var.isValid()) newId = var.toInt();
+ }
+ }
+ return newId;
+}
+
+QString DatabaseManager::getPlatform(int id) const
+{
+ QString name;
+ QSqlQuery query(QString("select firstname, lastname from person where id = %1").arg(id));
+
+ if (query.next())
+ {
+ name.append(query.value(0).toString());
+ name.append(query.value(1).toString());
+ }
+ return name;
+}
+
--- /dev/null
+#ifndef DATABASEMANAGER_H
+#define DATABASEMANAGER_H
+
+#include <QObject>
+#include <QSqlDatabase>
+
+class QSqlError;
+class QFile;
+
+class DatabaseManager : public QObject
+{
+public:
+ DatabaseManager(QObject *parent = 0);
+ ~DatabaseManager();
+
+ bool openDB();
+ bool deleteDB();
+ bool dbExists() const;
+ QSqlError lastError();
+ bool createDB() const;
+ int insertPlatform(QString name, QString filename = 0) const;
+ QString getPlatform(int id) const;
+
+private:
+ QSqlDatabase db;
+ static const QString DB_FILENAME;
+ QString getDbPath() const;
+};
+
+#endif
--- /dev/null
+#include <QtGui>
+#include "dbobjectdialog.h"
+
+DbObjectDialog::DbObjectDialog(QWidget *parent)
+ : QDialog(parent)
+{
+ editButton = new QPushButton(tr("&Edit"));
+ addButton = new QPushButton(tr("&Add"));
+ deleteButton = new QPushButton(tr("&Delete"));
+ objectList = new QListView();
+ buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok, Qt::Vertical);
+ buttonBox->addButton(editButton, QDialogButtonBox::ActionRole);
+ buttonBox->addButton(addButton, QDialogButtonBox::ActionRole);
+ buttonBox->addButton(deleteButton, QDialogButtonBox::ActionRole);
+ // nameDialog will be created on request
+
+ connectSignals();
+ layout();
+}
+
+void DbObjectDialog::connectSignals()
+{
+ connect(buttonBox, SIGNAL(accepted()), this, SLOT(close()));
+ connect(objectList, SIGNAL(clicked(const QModelIndex &)),
+ this, SLOT(listObjectClicked(const QModelIndex &)));
+ connect(editButton, SIGNAL(clicked()), this, SLOT(editButtonClicked()));
+ connect(addButton, SIGNAL(clicked()), this, SLOT(addButtonClicked()));
+ connect(deleteButton, SIGNAL(clicked()), this, SLOT(deleteButtonClicked()));
+}
+
+void DbObjectDialog::addButtonClicked()
+{
+ disableSelection();
+}
+
+void DbObjectDialog::editButtonClicked()
+{
+ disableSelection();
+}
+
+void DbObjectDialog::deleteButtonClicked()
+{
+ disableSelection();
+}
+
+void DbObjectDialog::layout()
+{
+ QHBoxLayout *mainLayout = new QHBoxLayout;
+ mainLayout->addWidget(objectList);
+ mainLayout->addWidget(buttonBox);
+ setLayout(mainLayout);
+}
+
+void DbObjectDialog::listObjectClicked(const QModelIndex &index)
+{
+ setButtonsEnabled(index.isValid());
+ if(!index.isValid())
+ return;
+}
+
+void DbObjectDialog::setButtonsEnabled(bool enabled)
+{
+ editButton->setEnabled(enabled);
+ deleteButton->setEnabled(enabled);
+}
+
+void DbObjectDialog::disableSelection()
+{
+ setButtonsEnabled(false);
+}
--- /dev/null
+#ifndef DBOBJECTDIALOG_H
+#define DBOBJECTDIALOG_H
+
+#include "namedialog.h"
+
+class QPushButton;
+class QModelIndex;
+class QDialogButtonBox;
+class QListView;
+
+class DbObjectDialog : public QDialog
+{
+ Q_OBJECT
+
+ public:
+ DbObjectDialog(QWidget *parent = 0);
+
+ protected slots:
+ void editButtonClicked();
+ void addButtonClicked();
+ void deleteButtonClicked();
+ //void enableEditButton();
+ //void enableDeleteButton();
+ void listObjectClicked(const QModelIndex &);
+
+ private:
+ QDialogButtonBox *buttonBox;
+ QPushButton *editButton;
+ QPushButton *addButton;
+ QPushButton *deleteButton;
+ QListView *objectList;
+ NameDialog *nameDialog;
+
+ void setButtonsEnabled(bool);
+ int deleteObject();
+ void connectSignals();
+ void layout();
+ void disableSelection();
+};
+
+#endif
--- /dev/null
+#include <QtGui>
+#include "namedialog.h"
+
+NameDialog::NameDialog(QWidget *parent, bool edit)
+ : QDialog(parent), edit(edit)
+{
+ nameLabel = new QLabel(tr("&Name: "));
+ nameEdit = new QLineEdit;
+ nameLabel->setBuddy(nameEdit);
+ saveButton = new QPushButton(tr("&Save"));
+ saveButton->setDefault(true);
+ saveButton->setEnabled(false);
+ closeButton = new QPushButton(tr("Close"));
+ connectSignals();
+ layout();
+ setWindowTitle(tr("Set names"));
+}
+
+NameDialog::~NameDialog()
+{
+ /* deleting objects in heap is not needed here
+ * because when deleting a parent widget
+ * the child widgets will be also deleted:
+ * delete nameLabel;
+ * delete nameEdit;
+ * delete saveButton;
+ * delete closeButton;
+ */
+}
+
+void NameDialog::setEdit(bool edit)
+{
+ this->edit = edit;
+}
+void NameDialog::connectSignals()
+{
+ connect(nameEdit, SIGNAL(textChanged(const QString &)),
+ this, SLOT(enableSaveButton(const QString &)));
+ connect(saveButton, SIGNAL(clicked()), this, SLOT(saveButtonClicked()));
+ connect(closeButton, SIGNAL(clicked()), this, SLOT(close()));
+}
+
+void NameDialog::layout()
+{
+ QHBoxLayout *topLayout = new QHBoxLayout;
+ topLayout->addWidget(nameLabel);
+ topLayout->addWidget(nameEdit);
+
+ QHBoxLayout *bottomLayout = new QHBoxLayout;
+ bottomLayout->addStretch();
+ bottomLayout->addWidget(saveButton);
+ bottomLayout->addWidget(closeButton);
+
+ QVBoxLayout *mainLayout = new QVBoxLayout;
+ mainLayout->addLayout(topLayout);
+ mainLayout->addLayout(bottomLayout);
+ setLayout(mainLayout);
+}
+
+void NameDialog::saveButtonClicked()
+{
+ if (nameEdit->text() == 0 || nameEdit->text().trimmed().isEmpty())
+ return;
+
+ QString name = nameEdit->text().simplified();
+ /*if (edit) updateDb(name);
+ else insertDb(name);*/
+}
+
+void NameDialog::enableSaveButton(const QString &text)
+{
+ saveButton->setEnabled(!text.isEmpty());
+}
--- /dev/null
+#ifndef NAMEDIALOG_H
+#define NAMEDIALOG_H
+
+#include <QDialog>
+
+class QLabel;
+class QLineEdit;
+class QPushButton;
+
+class NameDialog : public QDialog
+{
+ Q_OBJECT
+
+public:
+ NameDialog(QWidget *parent = 0, bool edit = false);
+ ~NameDialog();
+ void setEdit(bool edit);
+
+signals:
+ void insertName(const QString &name, int id = 0);
+
+protected slots:
+ void saveButtonClicked();
+ void enableSaveButton(const QString &);
+
+protected:
+ QLabel *nameLabel;
+ QLineEdit *nameEdit;
+ QPushButton *saveButton;
+ QPushButton *closeButton;
+private:
+ void connectSignals();
+ void layout();
+
+ bool edit;
+};
+
+#endif
--- /dev/null
+#include "platformdialog.h"
+
+PlatformDialog::PlatformDialog(QWidget *parent)
+ : DbObjectDialog(parent)
+{
+ setWindowTitle(tr("Set emulated platforms"));
+}
--- /dev/null
+#ifndef PLATFORMDIALOG_H
+#define PLATFORMDIALOG_H
+
+#include "dbobjectdialog.h"
+
+class PlatformDialog : public DbObjectDialog
+{
+ Q_OBJECT
+
+ public:
+ PlatformDialog(QWidget *parent = 0);
+};
+
+#endif
--- /dev/null
+#include "platformnamedialog.h"
+
+PlatformNameDialog::PlatformNameDialog(QWidget *parent, bool edit)
+ : NameDialog(parent, edit)
+{
+ setWindowTitle(tr("Set platform name"));
+}
--- /dev/null
+#ifndef PLATFORMDIALOG_H
+#define PLATFORMDIALOG_H
+
+#include "namedialog.h"
+
+class PlatformNameDialog : public NameDialog
+{
+ Q_OBJECT
+
+public:
+ PlatformNameDialog(QWidget *parent = 0, bool edit = false);
+};
+
+#endif
--- /dev/null
+######################################################################
+# Automatically generated by qmake (2.01a) Fri May 14 22:39:13 2010
+######################################################################
+
+TEMPLATE = app
+TARGET =
+DEPENDPATH += . db dialogs
+INCLUDEPATH += . db dialogs
+QT += sql
+
+# Input
+HEADERS += mainwindow.h \
+ db/databasemanager.h \
+ dialogs/dbobjectdialog.h \
+ dialogs/namedialog.h \
+ dialogs/platformdialog.h \
+ dialogs/platformnamedialog.h
+SOURCES += main.cpp \
+ mainwindow.cpp \
+ db/databasemanager.cpp \
+ dialogs/dbobjectdialog.cpp \
+ dialogs/namedialog.cpp \
+ dialogs/platformdialog.cpp \
+ dialogs/platformnamedialog.cpp
--- /dev/null
+#include <QApplication>
+#include <QTextStream>
+#include <iostream>
+#include "mainwindow.h"
+//#include "db/databasemanager.h"
+//#include "dialogs/platformnamedialog.h"
+
+int main(int argc, char *argv[])
+{
+ QApplication app(argc, argv);
+ QTextStream cout(stdout, QIODevice::WriteOnly);
+
+ /*DatabaseManager dbm;
+ if (dbm.openDB())
+ cout << " Database opened succesfully!" << endl;
+ else cout << " Database connection failed!" << endl;
+
+ if (dbm.dbExists())
+ cout << " Database exists!" << endl;
+ else
+ {
+ cout << " Database is missing!" << endl;
+ if (dbm.createDB())
+ cout << " Database created succesfully!" << endl;
+ else {
+ cout << "Failed creating database!" << endl;
+ exit(1);
+ }
+ }
+
+ if (dbm.insertPerson("Testi","Tapaus",1) > 0)
+ cout << "Database insert successfull!" << endl;
+ else cout << "Database insert failed!" << endl;
+
+
+ cout << "Trying to select from database: " << dbm.getName(1) << endl;
+ */
+
+ /*PlatformNameDialog *nameDialog = new PlatformNameDialog;
+ nameDialog->show();*/
+ MainWindow *mw = new MainWindow;
+ mw->show();
+ return app.exec();
+}
--- /dev/null
+#include <QtGui>
+#include "mainwindow.h"
+#include "dialogs/platformdialog.h"
+
+MainWindow::MainWindow()
+{
+ setWindowTitle("EmuFront");
+ createActions();
+ createMenus();
+ createStatusBar();
+ readSettings();
+ platformDialog = 0;
+}
+
+void MainWindow::createActions()
+{
+ configPlatformAction = new QAction(tr("&Platforms"), this);
+ configPlatformAction->setStatusTip(tr("Configure platforms"));
+ connect(configPlatformAction, SIGNAL(triggered()),
+ this, SLOT(configurePlatforms()));
+
+ exitAction = new QAction(tr("&Exit"), this);
+ exitAction->setShortcut(tr("Ctrl+Q"));
+ exitAction->setStatusTip(tr("Exit EmuFront"));
+ connect(exitAction, SIGNAL(triggered()), this, SLOT(close()));
+}
+
+void MainWindow::configurePlatforms()
+{
+ if (!platformDialog)
+ {
+ platformDialog = new PlatformDialog(this);
+ }
+ platformDialog->show();
+ platformDialog->raise();
+ platformDialog->activateWindow();
+}
+
+void MainWindow::createMenus()
+{
+ fileMenu = menuBar()->addMenu(tr("&File"));
+ fileMenu->addAction(exitAction);
+
+ configMenu = menuBar()->addMenu(tr("&Config"));
+ configMenu->addAction(configPlatformAction);
+}
+
+void MainWindow::createStatusBar()
+{
+ messageLabel = new QLabel;
+ statusBar()->addWidget(messageLabel);
+}
+
+void MainWindow::readSettings()
+{
+}
+
+void MainWindow::writeSettings()
+{
+}
+
+void MainWindow::closeEvent(QCloseEvent *event)
+{
+ if (okToContinue())
+ event->accept();
+ else event->ignore();
+}
+
+bool MainWindow::okToContinue()
+{
+ return true;
+}
+
+
--- /dev/null
+#ifndef MAINWINDOW_H
+#define MAINWINDOW_H
+
+#include <QMainWindow>
+
+class QAction;
+class PlatformDialog;
+class QLabel;
+
+class MainWindow : public QMainWindow
+{
+ Q_OBJECT
+
+public:
+ MainWindow();
+ //~MainWindow();
+
+protected:
+ void closeEvent(QCloseEvent *event);
+
+private slots:
+ void configurePlatforms();
+
+private:
+ void createActions();
+ void createMenus();
+ void createStatusBar();
+ void readSettings();
+ void writeSettings();
+ bool okToContinue();
+ PlatformDialog *platformDialog;
+ QMenu *configMenu;
+ QMenu *fileMenu;
+ QAction *configPlatformAction;
+ QAction *exitAction;
+ QLabel *messageLabel;
+
+ // todo ks. s. 46 jatkoa...
+};
+
+#endif