Added configuration of temporary directory.
authorMikko Keinänen <mikko.keinanen@gmail.com>
Sun, 7 Nov 2010 00:56:32 +0000 (02:56 +0200)
committerMikko Keinänen <mikko.keinanen@gmail.com>
Sun, 7 Nov 2010 00:56:32 +0000 (02:56 +0200)
19 files changed:
src/dataobjects/filepathobject.cpp
src/dataobjects/filepathobject.h
src/db/dbconfig.cpp [new file with mode: 0644]
src/db/dbconfig.h [new file with mode: 0644]
src/db/dbcreator.cpp
src/db/dbfilepath.cpp
src/db/dbfilepath.h
src/dialogs/browsefilepathdialog.cpp [new file with mode: 0644]
src/dialogs/browsefilepathdialog.h [new file with mode: 0644]
src/dialogs/mediaimagepathdialog.cpp
src/dialogs/mediaimagepathdialog.h
src/dialogs/mediaimagepathmaindialog.cpp
src/emufront.pro
src/emulauncher.cpp
src/emulauncher.h
src/mainwindow.cpp
src/mainwindow.h
src/utils/emuhelper.cpp
src/utils/emuhelper.h

index 93f3da8..6619ddc 100644 (file)
@@ -21,7 +21,8 @@
 #include "filepathobject.h"
 #include "setup.h"
 
-FilePathObject::FilePathObject() : EmuFrontObject(), setup(0)
+FilePathObject::FilePathObject(int type)
+    : EmuFrontObject(), type(type), setup(0)
 { }
 
 FilePathObject::FilePathObject(int id, QString name, int filetype)
@@ -38,10 +39,7 @@ FilePathObject::~FilePathObject()
 FilePathObject::FilePathObject(const FilePathObject &fpobj)
     : EmuFrontObject(fpobj), type(fpobj.type)
 {
-    qDebug() << "FilePathObject copy constructor.";
     Setup *s = fpobj.setup;
-    qDebug() << "Setup name " << s->getName();
-    qDebug() << "Setup id " << s->getId();
     setup = new Setup(*s);
 }
 
index 53d82bd..d0bb80f 100644 (file)
@@ -27,7 +27,7 @@ class Setup;
 class FilePathObject : public EmuFrontObject
 {
 public:
-    FilePathObject();
+    FilePathObject(int type);
     ~FilePathObject();
     FilePathObject(int id, QString name, int filetype);
     FilePathObject(int id, QString name, int filetype, Setup*);
@@ -39,6 +39,13 @@ public:
     int getType() const;
     void setType(int);
 
+    enum {
+        FilePathType_MediaImageDir = 1,
+        FilePathType_TmpDir,
+        FilePathType_PlatformIconsDir,
+        FilePathType_MediaIconsDir
+    };
+
 private:
     int type;
     Setup *setup;
diff --git a/src/db/dbconfig.cpp b/src/db/dbconfig.cpp
new file mode 100644 (file)
index 0000000..1ae9f52
--- /dev/null
@@ -0,0 +1,33 @@
+#include "dbconfig.h"
+#include <QSqlRelationalTableModel>
+#include <QSqlQuery>
+#include <QSqlRecord>
+
+DbConfig::DbConfig(QObject *parent) :
+    QObject(parent)
+{
+}
+
+QString DbConfig::getTmpDir()
+{
+    QString r = "";
+    QSqlQuery q("SELECT tmpdirpath FROM config");
+    if (q.first())
+        r = q.value(DbConfig::DbConfig_TmpDirPath).toString();
+    return r;
+}
+
+bool DbConfig::setTmpDir(QString path)
+{
+    QSqlQuery q("SELECT COUNT(*) FROM config");
+    int rows = 0;
+    if (q.first())
+        rows = q.value(0).toInt();
+    if (rows == 0)
+        q.prepare("INSERT INTO config (tmpdirpath) "
+        "VALUES (:path)");
+    else
+        q.prepare("UPDATE CONFIG SET tmpdirpath=:path");
+    q.bindValue(":path", path);
+    return q.exec();
+}
diff --git a/src/db/dbconfig.h b/src/db/dbconfig.h
new file mode 100644 (file)
index 0000000..6246407
--- /dev/null
@@ -0,0 +1,18 @@
+#ifndef DBCONFIG_H
+#define DBCONFIG_H
+
+#include <QObject>
+
+class DbConfig : public QObject
+{
+    Q_OBJECT
+public:
+    DbConfig(QObject *parent = 0);
+    static QString getTmpDir();
+    static bool setTmpDir(QString);
+    enum {
+        DbConfig_TmpDirPath = 0
+    };
+};
+
+#endif // DBCONFIG_H
index 5fd76b6..5b686af 100644 (file)
@@ -53,6 +53,10 @@ bool DbCreator::createDB()
 
         qDebug() << "Creating TABLE file";
 
+        ret = query.exec("CREATE TABLE IF NOT EXISTS config"
+                "(tmpdirpath TEXT)"
+            );
+
         ret = query.exec("CREATE TABLE IF NOT EXISTS file"
                         "(id INTEGER PRIMARY KEY, "
                         "name TEXT, "
index 13b5778..cc37e94 100644 (file)
@@ -36,11 +36,11 @@ EmuFrontObject* DbFilePath::recordToDataObject(const QSqlRecord *rec)
     int id = rec->value(FilePath_Id).toInt();
     QString fpath = rec->value(FilePath_Name).toString();
     int setupId = rec->value(FilePath_SetupId).toInt();
-    //int fileType = rec->value(FilePath_FileTypeId).toInt();
+    int fileType = rec->value(FilePath_FileTypeId).toInt();
     Setup *sup = dynamic_cast<Setup*>(dbSetup->getDataObject(setupId));
        // TODO
     //int lastScanned = 0;
-    return new FilePathObject(id, fpath, /* TODO */ 0, sup);
+    return new FilePathObject(id, fpath, fileType, sup);
 }
 
 bool DbFilePath::updateDataObjectToModel(const EmuFrontObject *ob)
@@ -103,7 +103,8 @@ QString DbFilePath::constructSelect(QString where) const
             "filepath.name AS Name, "
             "filepath.lastscanned AS LastScanned, "
             "setup.id AS SetupId, "
-            "platform.name || ' ' || mediatype.name AS SetupName "
+            "platform.name || ' ' || mediatype.name AS SetupName, "
+            "filepath.filetypeid "
             "FROM filepath "
             "INNER JOIN setup ON filepath.setupid=setup.id  "
             "INNER JOIN platform ON setup.platformid=platform.id "
index 682d393..4d0f9d4 100644 (file)
@@ -35,7 +35,9 @@ public:
            FilePath_Name,
            FilePath_LastScanned,
            FilePath_SetupId,
-           FilePath_SetupName };
+           FilePath_SetupName,
+           FilePath_FileTypeId
+           };
 
 protected:
     virtual EmuFrontObject* recordToDataObject(const QSqlRecord* );
diff --git a/src/dialogs/browsefilepathdialog.cpp b/src/dialogs/browsefilepathdialog.cpp
new file mode 100644 (file)
index 0000000..525a83c
--- /dev/null
@@ -0,0 +1,54 @@
+// EmuFront
+// Copyright 2010 Mikko Keinänen
+//
+// This file is part of EmuFront.
+//
+//
+// EmuFront is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License version 2 as published by
+// the Free Software Foundation and appearing in the file gpl.txt included in the
+// packaging of this file.
+//
+// EmuFront is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with EmuFront.  If not, see <http://www.gnu.org/licenses/>.
+
+#include <QtGui>
+#include "browsefilepathdialog.h"
+
+BrowseFilePathDialog::BrowseFilePathDialog(QWidget *parent, EmuFrontObject *efo, Qt::Orientation orientation) :
+    DataObjectEditDialog(parent, efo, orientation)
+{
+}
+
+void BrowseFilePathDialog::connectSignals()
+{
+    DataObjectEditDialog::connectSignals();
+    connect(filePathButton, SIGNAL(clicked()), this, SLOT(browseFilePath()));
+}
+
+void BrowseFilePathDialog::initWidgets()
+{
+    filePathLabel = new QLabel;
+    filePathButton = new QPushButton(tr("&Browse filepath"));
+}
+
+void BrowseFilePathDialog::browseFilePath()
+{
+    QString startPath = (efObject && !efObject->getName().isEmpty())
+        ? efObject->getName()
+        : QDir::homePath();
+
+    QString fpath = QFileDialog::getExistingDirectory(this,
+        tr("Select a directory"), startPath,
+        QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
+    QDir d(fpath);
+    if (d.exists() && d.isReadable()) {
+        filePathLabel->setText(d.path());
+        if (d.path() != startPath) emit filePathUpdated();
+    }
+}
diff --git a/src/dialogs/browsefilepathdialog.h b/src/dialogs/browsefilepathdialog.h
new file mode 100644 (file)
index 0000000..ffdab6e
--- /dev/null
@@ -0,0 +1,48 @@
+// EmuFront
+// Copyright 2010 Mikko Keinänen
+//
+// This file is part of EmuFront.
+//
+//
+// EmuFront is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License version 2 as published by
+// the Free Software Foundation and appearing in the file gpl.txt included in the
+// packaging of this file.
+//
+// EmuFront is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with EmuFront.  If not, see <http://www.gnu.org/licenses/>.
+
+#ifndef BROWSEFILEPATHDIALOG_H
+#define BROWSEFILEPATHDIALOG_H
+
+#include "dataobjecteditdialog.h"
+
+class QLabel;
+class QPushButton;
+
+class BrowseFilePathDialog : public DataObjectEditDialog
+{
+    Q_OBJECT
+public:
+    BrowseFilePathDialog(QWidget *parent, EmuFrontObject*,
+        Qt::Orientation orientation = Qt::Horizontal);
+
+signals:
+    void filePathUpdated();
+
+protected slots:
+    void browseFilePath();
+
+protected:
+    void connectSignals();
+    void initWidgets();
+    QLabel *filePathLabel;
+    QPushButton *filePathButton;
+};
+
+#endif // BROWSEFILEPATHDIALOG_H
index 62673e6..3838657 100644 (file)
@@ -26,7 +26,7 @@
 #include "../widgets/setupcombobox.h"
 
 MediaImagePathDialog::MediaImagePathDialog(QWidget *parent, EmuFrontObject *efObject)
-    : DataObjectEditDialog(parent, efObject, Qt::Horizontal)
+    : BrowseFilePathDialog(parent, efObject, Qt::Horizontal)
 {
     initWidgets();
     dbPlatform = 0;
@@ -40,13 +40,13 @@ MediaImagePathDialog::~MediaImagePathDialog()
 {
 }
 
-void MediaImagePathDialog::connectSignals()
+/*void MediaImagePathDialog::connectSignals()
 {
     DataObjectEditDialog::connectSignals();
     connect(filePathButton, SIGNAL(clicked()), this, SLOT(browseFilePath()));
-}
+}*/
 
-void MediaImagePathDialog::browseFilePath()
+/*void MediaImagePathDialog::browseFilePath()
 {
     QString fpath = QFileDialog::getExistingDirectory(this, tr("Select a directory"), ".",
         QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
@@ -55,13 +55,14 @@ void MediaImagePathDialog::browseFilePath()
     {
         filePathLabel->setText(d.path());
     }
-}
+}*/
 
 void MediaImagePathDialog::initWidgets()
 {
+    BrowseFilePathDialog::initWidgets();
     // these widgets will be automatically parented using layout components
-    filePathLabel = new QLabel;
-    filePathButton = new QPushButton(tr("&Browse filepath"));
+    //filePathLabel = new QLabel;
+    //filePathButton = new QPushButton(tr("&Browse filepath"));
     dbSetup = new DbSetup(this);
     setupComBox = new SetupComboBox(dbSetup, this);
 }
index 1ec5413..b1f3692 100644 (file)
 #ifndef MEDIAIMAGEPATHDIALOG_H
 #define MEDIAIMAGEPATHDIALOG_H
 
-#include "dataobjecteditdialog.h"
+#include "browsefilepathdialog.h"
 
-class QDialogButtonBox;
-class QLabel;
-class QPushButton;
-class QSqlTableModel;
+//class QDialogButtonBox;
+//class QLabel;
+//class QPushButton;
+//class QSqlTableModel;
 class DbSetup;
 class Setup;
 class DbMediaType;
 class DbPlatform;
 class SetupComboBox;
 
-class MediaImagePathDialog : public DataObjectEditDialog
+class MediaImagePathDialog : public BrowseFilePathDialog
 {
     Q_OBJECT
 
@@ -44,19 +44,17 @@ public:
 
 protected slots:
     virtual void acceptChanges();
-    void browseFilePath();
+    //void browseFilePath();
 
 private:
     SetupComboBox *setupComBox;
-    QLabel *filePathLabel;
-    QPushButton *filePathButton;
     DbSetup *dbSetup;
     DbMediaType *dbMediaType;
     DbPlatform *dbPlatform;
 
     void initWidgets();
     void layout();
-    void connectSignals();
+    //void connectSignals();
     void populateSetupComBox();
     void setSelectedSetup(const Setup *);
     Setup* getSelectedSetup();
index 2ec1f60..ff31fc6 100644 (file)
@@ -93,7 +93,7 @@ void MediaImagePathMainDialog::beginScanFilePath()
 
 EmuFrontObject* MediaImagePathMainDialog::createObject()
 {
-    return new FilePathObject;
+    return new FilePathObject(FilePathObject::FilePathType_MediaImageDir);
 }
 
 MediaImagePathMainDialog::~MediaImagePathMainDialog()
index 1c6e173..a11a3a5 100644 (file)
@@ -65,7 +65,9 @@ HEADERS += mainwindow.h \
     utils/emuhelper.h \
     dialogs/listdialog.h \
     dialogs/emufrontinputdialog.h \
-    dialogs/emufrontfileobjectdialog.h
+    dialogs/emufrontfileobjectdialog.h \
+    dialogs/browsefilepathdialog.h \
+    db/dbconfig.h
 SOURCES += main.cpp \
     mainwindow.cpp \
     db/databasemanager.cpp \
@@ -119,11 +121,11 @@ SOURCES += main.cpp \
     utils/emuhelper.cpp \
     dialogs/listdialog.cpp \
     dialogs/emufrontinputdialog.cpp \
-    dialogs/emufrontfileobjectdialog.cpp
+    dialogs/emufrontfileobjectdialog.cpp \
+    dialogs/browsefilepathdialog.cpp \
+    db/dbconfig.cpp
 OTHER_FILES += 
 
 CONFIG += mobility
 
 MOBILITY = 
-
-
index d2aca49..11418a6 100644 (file)
@@ -32,8 +32,8 @@
 #include "utils/emuhelper.h"
 #include "dialogs/emufrontinputdialog.h"
 
-EmuLauncher::EmuLauncher(QWidget *parent) :
-    QWidget(parent)
+EmuLauncher::EmuLauncher(QWidget *parent, QString tmp) :
+    QWidget(parent), tmpDirPath(tmp)
 {
     dbPlatform = new DbPlatform(this);
     dbMediaType = new DbMediaType(this);
@@ -224,7 +224,7 @@ void EmuLauncher::launchEmu()
         if (selectedImages.count() < 1)
             throw EmuFrontException(tr("No media images selected"));
 
-        emuHelper->launch(exe, mediaImageContainers, selectedImages, list.count());
+        emuHelper->launch(exe, mediaImageContainers, selectedImages, list.count(), tmpDirPath);
         micTable->clearSelection();
     } catch (EmuFrontException efe) {
         if (exe) delete exe;
@@ -261,3 +261,8 @@ void EmuLauncher::cleanTmp()
 {
     // TODO
 }
+
+void EmuLauncher::setTmpDirPath(QString tmp)
+{
+    tmpDirPath = tmp;
+}
index 6921f9a..1e88136 100644 (file)
@@ -39,9 +39,10 @@ class EmuLauncher : public QWidget
 {
 Q_OBJECT
 public:
-    explicit EmuLauncher(QWidget *parent = 0);
+    EmuLauncher(QWidget *parent = 0, QString tmp = ".");
     ~EmuLauncher();
     void updateData();
+    void setTmpDirPath(QString);
 
 signals:
 
@@ -70,6 +71,7 @@ private:
     void populateMediaTypeSelectBox();
     void launch(const Executable*, const MediaImageContainer*);
     void cleanTmp();
+    QString tmpDirPath;
 };
 
 #endif // EMULAUNCHER_H
index f5bbfb1..7d474d8 100644 (file)
 #include "dialogs/mediaimagepathmaindialog.h"
 #include "dialogs/setupmaindialog.h"
 #include "dialogs/executablemaindialog.h"
+#include "dialogs/tmpfoldereditdialog.h"
 #include "db/databasemanager.h"
+#include "db/dbconfig.h"
 
 MainWindow::MainWindow()
 {
     setWindowTitle("EmuFront");
-    launcher = new EmuLauncher(this);
+    tmpDirFilePath = DbConfig::getTmpDir();
+    if (tmpDirFilePath.isEmpty())
+        tmpDirFilePath = QDir::tempPath();
+    launcher = new EmuLauncher(this, tmpDirFilePath);
     setCentralWidget(launcher);
     createActions();
     createMenus();
@@ -41,7 +46,6 @@ MainWindow::MainWindow()
     mediaImagePathDialog = 0;
     setupMainDialog = 0;
     executableMainDialog = 0;
-    connectSignals();
 }
 
 void MainWindow::connectSignals()
@@ -72,6 +76,10 @@ void MainWindow::createActions()
     configEmulatorAction->setStatusTip(tr("Configure emulators"));
     connect(configEmulatorAction, SIGNAL(triggered()), this, SLOT(configureEmulators()));
 
+    configTmpDirAction = new QAction(tr("&Temp dir"), this);
+    configTmpDirAction->setStatusTip(tr("Configure directory for temporary files."));
+    connect(configTmpDirAction, SIGNAL(triggered()), this, SLOT(configureTmpDir()));
+
     exitAction = new QAction(tr("&Exit"), this);
     exitAction->setShortcut(tr("Ctrl+Q"));
     exitAction->setStatusTip(tr("Exit EmuFront"));
@@ -132,6 +140,24 @@ void MainWindow::configureEmulators()
     executableMainDialog->refreshDataModel();
 }
 
+void MainWindow::configureTmpDir()
+{
+    /*if (!tmpFolderDialog) {
+        tmpFolderDialog = new TmpFolderEditDialog(this, tmpDirFilePath);
+    }
+    activateDialog(tmpFolderDialog);*/
+
+    QString fpath = QFileDialog::getExistingDirectory(this,
+        tr("Select a directory"), tmpDirFilePath,
+        QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
+    QDir d(fpath);
+    if (d.exists() && d.isReadable()) {
+        tmpDirFilePath = fpath;
+        DbConfig::setTmpDir(tmpDirFilePath);
+        launcher->setTmpDirPath(tmpDirFilePath);
+    }
+}
+
 void MainWindow::activateDialog(EmuFrontDialog* dia) const
 {
     dia->show();
@@ -150,6 +176,7 @@ void MainWindow::createMenus()
     configMenu->addAction(configMediaImagePathAction);
     configMenu->addAction(configSetupAction);
     configMenu->addAction(configEmulatorAction);
+    configMenu->addAction(configTmpDirAction);
 
     helpMenu = menuBar()->addMenu(tr("&Help"));
     helpMenu->addAction(aboutAction);
index 7f8ba29..eca5089 100644 (file)
@@ -28,6 +28,7 @@ class MediaTypeDialog;
 class MediaImagePathMainDialog;
 class SetupMainDialog;
 class ExecutableMainDialog;
+//class TmpFolderEditDialog;
 class QLabel;
 class DatabaseManager;
 class EmuFrontDialog;
@@ -50,6 +51,7 @@ private slots:
     void configureMediaImagePaths();
     void configureSetups();
     void configureEmulators();
+    void configureTmpDir();
     void updateData();
     void about();
 
@@ -67,6 +69,7 @@ private:
     MediaImagePathMainDialog *mediaImagePathDialog;
     SetupMainDialog *setupMainDialog;
     ExecutableMainDialog *executableMainDialog;
+    //TmpFolderEditDialog *tmpFolderDialog;
        QMenu *configMenu;
     QMenu *fileMenu;
     QMenu *helpMenu;
@@ -77,9 +80,11 @@ private:
     QAction *configEmulatorAction;
     QAction *exitAction;
     QAction *aboutAction;
+    QAction *configTmpDirAction;
        QLabel *messageLabel;
     DatabaseManager *dbManager;
     EmuLauncher *launcher;
+    QString tmpDirFilePath;
 };
 
 #endif
index e98a888..03f84a4 100644 (file)
@@ -33,7 +33,7 @@ EmuHelper::EmuHelper(QObject *parent) :
 }
 
 void EmuHelper::launch(const Executable * ex, QList<MediaImageContainer *> micList,
-    QList<EmuFrontObject *> miList, int mediaCount)
+    QList<EmuFrontObject *> miList, int mediaCount, QString tmp)
 {
     if (miList.count() < 1) {
         throw EmuFrontException(tr("No media images available!"));
@@ -42,10 +42,9 @@ void EmuHelper::launch(const Executable * ex, QList<MediaImageContainer *> micLi
         throw EmuFrontException(tr("No media image containers available!"));
     }
 
-    QString tmp = "/tmp/"; // TODO: do this configurable!
+    if (!tmp.endsWith('/')) tmp.append("/");
 
     // extract the media image container to tmp folder
-    // (TODO: tmp folder configuration)
     foreach(MediaImageContainer *mic, micList) {
         QString fp;
         fp.append(mic->getFilePath()->getName());
@@ -60,7 +59,8 @@ void EmuHelper::launch(const Executable * ex, QList<MediaImageContainer *> micLi
     // fill in the media image slots in the command line options ($1, $2, ...)
     QString opts = ex->getOptions();
     for(int i = 0; i < mediaCount && i < miList.size(); i++) {
-        QString tmpfp = " \"/tmp/";
+        QString tmpfp = " \"";
+        tmpfp.append(tmp);
         tmpfp.append (miList.at(i)->getName());
         tmpfp.append("\"");
         opts.replace(QString("$%1").arg(i+1), tmpfp);
@@ -78,10 +78,11 @@ void EmuHelper::launch(const Executable * ex, QList<MediaImageContainer *> micLi
 
     // clean the temp dir
     foreach(EmuFrontObject *ob, miList) {
-        QString fp = tmp;
+        QString fp = " \"";
+         fp.append(tmp);
         fp.append(ob->getName());
-        QFile f(fp);
-        if (!f.remove())
+        fp.append("\"");
+        if (!QFile::remove(fp))
             qDebug() << "Removing " << fp << " failed.";
     }
     delete ex;
index de80679..dfdc81c 100644 (file)
@@ -33,7 +33,7 @@ class EmuHelper : public ProcessHelper
     Q_OBJECT
 public:
     explicit EmuHelper(QObject *parent = 0);
-    void launch(const Executable * ex, QList<MediaImageContainer *> micList, QList<EmuFrontObject*> miList, int mediaCount = 1);
+    void launch(const Executable * ex, QList<MediaImageContainer *> micList, QList<EmuFrontObject*> miList, int mediaCount = 1, QString tmp = ".");
 private slots:
     void processError(QProcess::ProcessError);
     void processFinished(int);