2 // Copyright 2010 Mikko Keinänen
4 // This file is part of EmuFront.
7 // EmuFront is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License version 2 as published by
9 // the Free Software Foundation and appearing in the file gpl.txt included in the
10 // packaging of this file.
12 // EmuFront is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with EmuFront. If not, see <http://www.gnu.org/licenses/>.
21 #include "mainwindow.h"
22 #include "emulauncher.h"
23 #include "dialogs/platformdialog.h"
24 #include "dialogs/mediatypedialog.h"
25 #include "dialogs/mediaimagepathmaindialog.h"
26 #include "dialogs/setupmaindialog.h"
27 #include "dialogs/executablemaindialog.h"
28 #include "utils/datfileutil.h"
29 #include "db/databasemanager.h"
30 #include "db/dbcreator.h"
31 #include "db/dbconfig.h"
33 QString MainWindow::aboutStr = trUtf8(
35 "<p>© 2010 Mikko Keinänen</p>"
36 "<p>mikko.keinanen@gmail.com</p>"
37 "<p>EmuFront is free software: you can redistribute it and/or modify "
38 "it under the terms of the GNU General Public License version 2 as published by "
39 "the Free Software Foundation.</p>"
42 QString MainWindow::aboutTitle = tr("About EmuFront");
44 MainWindow::MainWindow(bool reset)
46 if (!testDB(reset)) close();
47 errorMessage = new QErrorMessage(this);
48 setWindowTitle("EmuFront");
49 tmpDirFilePath = DbConfig::getTmpDir();
50 if (tmpDirFilePath.isEmpty())
51 tmpDirFilePath = QDir::homePath();
52 qDebug() << "Temporary dir is " << tmpDirFilePath;
53 launcher = new EmuLauncher(this, tmpDirFilePath);
54 setCentralWidget(launcher);
61 mediaImagePathDialog = 0;
63 executableMainDialog = 0;
66 void MainWindow::connectSignals()
70 void MainWindow::createActions()
72 configPlatformAction = new QAction(tr("&Platforms"), this);
73 configPlatformAction->setStatusTip(tr("Configure platforms"));
74 connect(configPlatformAction, SIGNAL(triggered()),
75 this, SLOT(configurePlatforms()));
77 configMediaTypeAction = new QAction(tr("&Media Types"), this);
78 configMediaTypeAction->setStatusTip(tr("Configure media types"));
79 connect(configMediaTypeAction, SIGNAL(triggered()), this, SLOT(configureMediaTypes()));
81 configMediaImagePathAction = new QAction(tr("Media &Image Paths"), this);
82 configMediaImagePathAction->setStatusTip(tr("Configure media image file paths."));
83 connect(configMediaImagePathAction, SIGNAL(triggered()),
84 this, SLOT(configureMediaImagePaths()));
86 configSetupAction = new QAction(tr("S&etups"), this);
87 configSetupAction->setStatusTip(tr("Configure set ups"));
88 connect(configSetupAction, SIGNAL(triggered()), this, SLOT(configureSetups()));
90 configEmulatorAction = new QAction(tr("Em&ulators"), this);
91 configEmulatorAction->setStatusTip(tr("Configure emulators"));
92 connect(configEmulatorAction, SIGNAL(triggered()), this, SLOT(configureEmulators()));
94 configTmpDirAction = new QAction(tr("&Temp dir"), this);
95 configTmpDirAction->setStatusTip(tr("Configure directory for temporary files."));
96 connect(configTmpDirAction, SIGNAL(triggered()), this, SLOT(configureTmpDir()));
98 manageDatFilesAction = new QAction(tr("&Manage dats"), this);
99 manageDatFilesAction->setStatusTip(tr("Read dat files to database."));
100 connect(manageDatFilesAction, SIGNAL(triggered()), this, SLOT(manageDatFiles()));
102 exitAction = new QAction(tr("&Exit"), this);
103 exitAction->setShortcut(tr("Ctrl+Q"));
104 exitAction->setStatusTip(tr("Exit EmuFront"));
105 connect(exitAction, SIGNAL(triggered()), this, SLOT(close()));
107 resetDbAction = new QAction( tr("Reset database"), this);
108 resetDbAction->setStatusTip(tr("Deletes all the current data and create a new database."));
109 connect(resetDbAction, SIGNAL(triggered()), this, SLOT(resetDb()));
111 aboutAction = new QAction(tr("&About"), this);
112 aboutAction->setStatusTip(tr("About EmuFront"));
113 connect(aboutAction, SIGNAL(triggered()), this, SLOT(about()));
116 void MainWindow::configurePlatforms()
120 platformDialog = new PlatformDialog(this);
121 connect(platformDialog, SIGNAL(finished(int)), this, SLOT(updateData()));
123 activateDialog(platformDialog);
126 void MainWindow::configureMediaTypes()
128 if (!mediaTypeDialog)
130 mediaTypeDialog = new MediaTypeDialog(this);
131 connect(mediaTypeDialog, SIGNAL(finished(int)), this, SLOT(updateData()));
133 activateDialog(mediaTypeDialog);
136 void MainWindow::configureMediaImagePaths()
138 if (!mediaImagePathDialog)
140 mediaImagePathDialog = new MediaImagePathMainDialog(this);
142 activateDialog(mediaImagePathDialog);
145 void MainWindow::configureSetups()
147 if (!setupMainDialog)
149 qDebug() << "MainWindow: Creating a setup main dialog.";
150 setupMainDialog = new SetupMainDialog(this);
152 activateDialog(setupMainDialog);
153 setupMainDialog->refreshDataModel();
156 void MainWindow::configureEmulators()
158 if (!executableMainDialog) {
159 executableMainDialog = new ExecutableMainDialog(this);
160 connect(executableMainDialog, SIGNAL(finished(int)), this, SLOT(updateData()));
162 activateDialog(executableMainDialog);
163 executableMainDialog->refreshDataModel();
166 void MainWindow::configureTmpDir()
168 /*if (!tmpFolderDialog) {
169 tmpFolderDialog = new TmpFolderEditDialog(this, tmpDirFilePath);
171 activateDialog(tmpFolderDialog);*/
173 QString fpath = QFileDialog::getExistingDirectory(this,
174 tr("Select a directory"), tmpDirFilePath,
175 QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
177 if (d.exists() && d.isReadable()) {
178 tmpDirFilePath = fpath;
179 DbConfig::setTmpDir(tmpDirFilePath);
180 launcher->setTmpDirPath(tmpDirFilePath);
184 void MainWindow::resetDb()
186 if (QMessageBox::question(this, "Reset database?",
187 "Are you REALLY SURE you want to do this? "
188 "All the current data WILL BE LOST!",
190 QMessageBox::Yes) == QMessageBox::No) {
196 catch (EmuFrontException e) {
197 errorMessage->showMessage(e.what());
201 void MainWindow::manageDatFiles()
207 void MainWindow::activateDialog(EmuFrontDialog* dia) const
211 dia->activateWindow();
214 void MainWindow::createMenus()
216 fileMenu = menuBar()->addMenu(tr("&File"));
217 fileMenu->addAction(resetDbAction);
218 fileMenu->addSeparator();
219 fileMenu->addAction(exitAction);
221 configMenu = menuBar()->addMenu(tr("&Config"));
222 configMenu->addAction(configTmpDirAction);
223 configMenu->addSeparator();
224 configMenu->addAction(configPlatformAction);
225 configMenu->addAction(configMediaTypeAction);
226 configMenu->addAction(configSetupAction);
227 configMenu->addAction(configMediaImagePathAction);
228 configMenu->addAction(configEmulatorAction);
229 configMenu->addSeparator();
230 configMenu->addAction(manageDatFilesAction);
232 helpMenu = menuBar()->addMenu(tr("&Help"));
233 helpMenu->addAction(aboutAction);
236 void MainWindow::createStatusBar()
238 messageLabel = new QLabel;
239 statusBar()->addWidget(messageLabel);
242 void MainWindow::readSettings()
246 void MainWindow::writeSettings()
250 void MainWindow::closeEvent(QCloseEvent *event)
254 else event->ignore();
257 bool MainWindow::okToContinue()
262 void MainWindow::updateData()
264 qDebug() << "MainWindow::updateData()";
265 launcher->updateData();
268 void MainWindow::about()
270 QMessageBox::about(this, aboutTitle, aboutStr );
273 bool MainWindow::testDB(bool reset)
276 if (DatabaseManager::openDB()) {
277 qDebug() << " Database opened succesfully!";
280 throw EmuFrontException("Database connection failed!");
283 int dbVer = DbCreator::dbExists();
284 if (dbVer == 0) reset = true;
285 if (!reset && dbVer != DbCreator::DB_VERSION) {
286 QString msg("Database is not compatible "
287 "with current version of EmuFront!"
288 "Do you want to continue to recreate the database?"
289 "ALL THE CURRENT DATA WILL BE LOST!!!");
290 if (QMessageBox::question(this, "Database not compatible!", msg,
291 QMessageBox::Yes, QMessageBox::No) == QMessageBox::Yes) {
294 else throw EmuFrontException("The current database is not compatible!"
295 " Cannot continue.");
303 catch (EmuFrontException e) {
304 qDebug() << e.what();
305 errorMessage->showMessage(e.what());
310 void MainWindow::createDB() const
315 dbCreator.createDB();
317 catch (QString str) {
318 QString msg(tr("Exception while trying to create"
319 " EmuFront database: %s").arg(str));
320 errorMessage->showMessage(msg);