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"
24 #include "platformdialog.h"
25 #include "platformeditview.h"
26 #include "mediatypedialog.h"
28 #include "mediatypeeditview.h"
30 #include "mediaimagepathmaindialog.h"
31 #include "filepatheditview.h"
33 #include "setupmaindialog.h"
34 #include "setupeditview.h"
35 #include "executablemaindialog.h"
36 #include "datfileutil.h"
37 #include "databasemanager.h"
38 #include "dbcreator.h"
41 QString MainWindow::aboutStr = trUtf8(
43 "<p>© 2010 Mikko Keinänen</p>"
44 "<p>mikko.keinanen@gmail.com</p>"
45 "<p>EmuFront is free software: you can redistribute it and/or modify "
46 "it under the terms of the GNU General Public License version 2 as published by "
47 "the Free Software Foundation.</p>"
50 QString MainWindow::aboutTitle = tr("About EmuFront");
52 MainWindow::MainWindow(bool reset)
54 if (!testDB(reset)) close();
55 errorMessage = new QErrorMessage(this);
56 setWindowTitle("EmuFront");
57 tmpDirFilePath = DbConfig::getTmpDir();
58 if (tmpDirFilePath.isEmpty())
59 tmpDirFilePath = QDir::homePath();
60 qDebug() << "Temporary dir is " << tmpDirFilePath;
61 launcher = new EmuLauncher(errorMessage, this, tmpDirFilePath);
62 setCentralWidget(launcher);
74 mediaImagePathDialog = 0;
75 mediaImagePathView = 0;
79 executableMainDialog = 0;
82 void MainWindow::connectSignals()
86 void MainWindow::createActions()
89 configPlatformAction = new QAction(tr("&Platforms"), this);
90 configPlatformAction->setStatusTip(tr("Configure platforms"));
91 connect(configPlatformAction, SIGNAL(triggered()),
92 this, SLOT(configurePlatforms()));
94 configPlatformsAction = new QAction(tr("&Set Platforms"), this);
95 configPlatformsAction->setStatusTip(tr("Add, edit and delete platforms"));
96 connect(configPlatformsAction, SIGNAL(triggered()),
97 this, SLOT(configurePlatformss()));
100 configMediaTypeAction = new QAction(tr("&Media Types"), this);
101 configMediaTypeAction->setStatusTip(tr("Configure media types"));
102 connect(configMediaTypeAction, SIGNAL(triggered()), this, SLOT(configureMediaTypes()));
104 configMediaTypesAction = new QAction(tr("&Set Media Types"), this);
105 configMediaTypeAction->setStatusTip(tr("Add, edit and delete media types"));
106 connect(configMediaTypesAction, SIGNAL(triggered()), this, SLOT(configureMediaTypess()));
109 configMediaImagePathAction = new QAction(tr("Media &Image Paths"), this);
110 configMediaImagePathAction->setStatusTip(tr("Configure media image file paths."));
111 connect(configMediaImagePathAction, SIGNAL(triggered()),
112 this, SLOT(configureMediaImagePaths()));
114 configMediaImagePathsAction = new QAction(tr("Set media &image paths"), this);
115 configMediaImagePathsAction->setStatusTip(tr("Add, edit and delete media image file paths."));
116 connect(configMediaImagePathsAction, SIGNAL(triggered()),
117 this, SLOT(configureMediaImagePathss()));
120 configSetupAction = new QAction(tr("S&etups"), this);
121 configSetupAction->setStatusTip(tr("Configure set ups"));
122 connect(configSetupAction, SIGNAL(triggered()), this, SLOT(configureSetups()));
124 configSetupsAction = new QAction(tr("Configure S&etups"), this);
125 configSetupsAction->setStatusTip(tr("Add, edit and delete setups"));
126 connect(configSetupsAction, SIGNAL(triggered()), this, SLOT(configureSetupss()));
128 configEmulatorAction = new QAction(tr("Em&ulators"), this);
129 configEmulatorAction->setStatusTip(tr("Configure emulators"));
130 connect(configEmulatorAction, SIGNAL(triggered()), this, SLOT(configureEmulators()));
132 configTmpDirAction = new QAction(tr("&Temp dir"), this);
133 configTmpDirAction->setStatusTip(tr("Configure directory for temporary files."));
134 connect(configTmpDirAction, SIGNAL(triggered()), this, SLOT(configureTmpDir()));
136 manageDatFilesAction = new QAction(tr("&Manage dats"), this);
137 manageDatFilesAction->setStatusTip(tr("Read dat files to database."));
138 connect(manageDatFilesAction, SIGNAL(triggered()), this, SLOT(manageDatFiles()));
140 exitAction = new QAction(tr("&Exit"), this);
141 exitAction->setShortcut(tr("Ctrl+Q"));
142 exitAction->setStatusTip(tr("Exit EmuFront"));
143 connect(exitAction, SIGNAL(triggered()), this, SLOT(close()));
145 resetDbAction = new QAction( tr("Reset database"), this);
146 resetDbAction->setStatusTip(tr("Deletes all the current data and create a new database."));
147 connect(resetDbAction, SIGNAL(triggered()), this, SLOT(resetDb()));
149 aboutAction = new QAction(tr("&About"), this);
150 aboutAction->setStatusTip(tr("About EmuFront"));
151 connect(aboutAction, SIGNAL(triggered()), this, SLOT(about()));
155 void MainWindow::configurePlatforms()
159 platformDialog = new PlatformDialog(this);
160 connect(platformDialog, SIGNAL(finished(int)), this, SLOT(updateData()));
162 activateDialog(platformDialog);
165 void MainWindow::configurePlatformss()
168 plfDialog = new PlatformEditView(this);
169 connect(plfDialog, SIGNAL(finished(int)), this, SLOT(updateData()));
171 activateDialog(plfDialog);
175 void MainWindow::configureMediaTypes()
177 if (!mediaTypeDialog)
179 mediaTypeDialog = new MediaTypeDialog(this);
180 connect(mediaTypeDialog, SIGNAL(finished(int)), this, SLOT(updateData()));
182 activateDialog(mediaTypeDialog);
185 void MainWindow::configureMediaTypess()
189 mdtDialog = new MediaTypeEditView(this);
190 connect(mdtDialog, SIGNAL(finished(int)), this, SLOT(updateData()));
192 activateDialog(mdtDialog);
197 void MainWindow::configureMediaImagePaths()
199 if (!mediaImagePathDialog)
201 mediaImagePathDialog = new MediaImagePathMainDialog(this);
203 activateDialog(mediaImagePathDialog);
206 void MainWindow::configureMediaImagePathss()
208 if (!mediaImagePathView)
210 mediaImagePathView = new FilePathEditView(this);
212 activateDialog(mediaImagePathView);
216 void MainWindow::configureSetups()
218 if (!setupMainDialog)
220 setupMainDialog = new SetupMainDialog(this);
222 activateDialog(setupMainDialog);
223 setupMainDialog->refreshDataModel();
226 void MainWindow::configureSetupss()
228 if (!setupMainView) {
229 setupMainView = new SetupEditView(this);
231 activateDialog(setupMainView);
235 void MainWindow::configureEmulators()
237 if (!executableMainDialog) {
238 executableMainDialog = new ExecutableMainDialog(this);
239 connect(executableMainDialog, SIGNAL(finished(int)), this, SLOT(updateData()));
241 activateDialog(executableMainDialog);
242 executableMainDialog->refreshDataModel();
245 void MainWindow::configureTmpDir()
247 /*if (!tmpFolderDialog) {
248 tmpFolderDialog = new TmpFolderEditDialog(this, tmpDirFilePath);
250 activateDialog(tmpFolderDialog);*/
252 QString fpath = QFileDialog::getExistingDirectory(this,
253 tr("Select a directory"), tmpDirFilePath,
254 QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
256 if (d.exists() && d.isReadable()) {
257 tmpDirFilePath = fpath;
258 DbConfig::setTmpDir(tmpDirFilePath);
259 launcher->setTmpDirPath(tmpDirFilePath);
263 void MainWindow::resetDb()
265 if (QMessageBox::question(this, "Reset database?",
266 "Are you REALLY SURE you want to do this? "
267 "All the current data WILL BE LOST!",
269 QMessageBox::Yes) == QMessageBox::No) {
275 catch (EmuFrontException e) {
276 errorMessage->showMessage(e.what());
280 void MainWindow::manageDatFiles()
286 void MainWindow::activateDialog(EmuFrontDialog* dia) const
290 dia->activateWindow();
293 void MainWindow::createMenus()
295 fileMenu = menuBar()->addMenu(tr("&File"));
296 fileMenu->addAction(resetDbAction);
297 fileMenu->addSeparator();
298 fileMenu->addAction(exitAction);
300 configMenu = menuBar()->addMenu(tr("&Config"));
301 configMenu->addAction(configTmpDirAction);
302 configMenu->addSeparator();
304 configMenu->addAction(configPlatformAction);
305 configMenu->addAction(configPlatformsAction);
307 configMenu->addAction(configMediaTypeAction);
308 configMenu->addAction(configMediaTypesAction);
310 configMenu->addAction(configSetupAction);
311 configMenu->addAction(configSetupsAction);
313 configMenu->addAction(configMediaImagePathAction);
314 configMenu->addAction(configMediaImagePathsAction);
315 configMenu->addAction(configEmulatorAction);
316 configMenu->addSeparator();
317 configMenu->addAction(manageDatFilesAction);
319 helpMenu = menuBar()->addMenu(tr("&Help"));
320 helpMenu->addAction(aboutAction);
323 void MainWindow::createStatusBar()
325 messageLabel = new QLabel;
326 statusBar()->addWidget(messageLabel);
329 void MainWindow::readSettings()
333 void MainWindow::writeSettings()
337 void MainWindow::closeEvent(QCloseEvent *event)
341 else event->ignore();
344 bool MainWindow::okToContinue()
349 void MainWindow::updateData()
351 qDebug() << "MainWindow::updateData()";
352 launcher->updateData();
355 void MainWindow::about()
357 QMessageBox::about(this, aboutTitle, aboutStr);
360 bool MainWindow::testDB(bool reset)
363 if (DatabaseManager::openDB()) {
364 qDebug() << " Database opened succesfully!";
367 throw EmuFrontException("Database connection failed!");
370 int dbVer = DbCreator::dbExists();
371 if (dbVer == 0) reset = true;
372 if (!reset && dbVer != DbCreator::DB_VERSION) {
373 QString msg("Database is not compatible "
374 "with current version of EmuFront!"
375 "Do you want to continue to recreate the database?"
376 "ALL THE CURRENT DATA WILL BE LOST!!!");
377 if (QMessageBox::question(this, "Database not compatible!", msg,
378 QMessageBox::Yes, QMessageBox::No) == QMessageBox::Yes) {
381 else throw EmuFrontException("The current database is not compatible!"
382 " Cannot continue.");
390 catch (EmuFrontException e) {
391 qDebug() << e.what();
392 errorMessage->showMessage(e.what());
397 void MainWindow::createDB() const
402 dbCreator.createDB();
404 catch (QString str) {
405 QString msg(tr("Exception while trying to create"
406 " EmuFront database: %s").arg(str));
407 errorMessage->showMessage(msg);