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"
29 #include "mediaimagepathmaindialog.h"
31 #include "setupmaindialog.h"
32 #include "setupeditview.h"
33 #include "executablemaindialog.h"
34 #include "datfileutil.h"
35 #include "databasemanager.h"
36 #include "dbcreator.h"
39 QString MainWindow::aboutStr = trUtf8(
41 "<p>© 2010 Mikko Keinänen</p>"
42 "<p>mikko.keinanen@gmail.com</p>"
43 "<p>EmuFront is free software: you can redistribute it and/or modify "
44 "it under the terms of the GNU General Public License version 2 as published by "
45 "the Free Software Foundation.</p>"
48 QString MainWindow::aboutTitle = tr("About EmuFront");
50 MainWindow::MainWindow(bool reset)
52 if (!testDB(reset)) close();
53 errorMessage = new QErrorMessage(this);
54 setWindowTitle("EmuFront");
55 tmpDirFilePath = DbConfig::getTmpDir();
56 if (tmpDirFilePath.isEmpty())
57 tmpDirFilePath = QDir::homePath();
58 qDebug() << "Temporary dir is " << tmpDirFilePath;
59 launcher = new EmuLauncher(errorMessage, this, tmpDirFilePath);
60 setCentralWidget(launcher);
71 mediaImagePathDialog = 0;
75 executableMainDialog = 0;
78 void MainWindow::connectSignals()
82 void MainWindow::createActions()
85 configPlatformAction = new QAction(tr("&Platforms"), this);
86 configPlatformAction->setStatusTip(tr("Configure platforms"));
87 connect(configPlatformAction, SIGNAL(triggered()),
88 this, SLOT(configurePlatforms()));
90 configPlatformsAction = new QAction(tr("&Set Platforms"), this);
91 configPlatformsAction->setStatusTip(tr("Add, edit and delete platforms"));
92 connect(configPlatformsAction, SIGNAL(triggered()),
93 this, SLOT(configurePlatformss()));
96 configMediaTypeAction = new QAction(tr("&Media Types"), this);
97 configMediaTypeAction->setStatusTip(tr("Configure media types"));
98 connect(configMediaTypeAction, SIGNAL(triggered()), this, SLOT(configureMediaTypes()));
100 configMediaTypesAction = new QAction(tr("&Set Media Types"), this);
101 configMediaTypeAction->setStatusTip(tr("Add, edit and delete media types"));
102 connect(configMediaTypesAction, SIGNAL(triggered()), this, SLOT(configureMediaTypess()));
104 configMediaImagePathAction = new QAction(tr("Media &Image Paths"), this);
105 configMediaImagePathAction->setStatusTip(tr("Configure media image file paths."));
106 connect(configMediaImagePathAction, SIGNAL(triggered()),
107 this, SLOT(configureMediaImagePaths()));
110 configSetupAction = new QAction(tr("S&etups"), this);
111 configSetupAction->setStatusTip(tr("Configure set ups"));
112 connect(configSetupAction, SIGNAL(triggered()), this, SLOT(configureSetups()));
114 configSetupsAction = new QAction(tr("Configure S&etups"), this);
115 configSetupsAction->setStatusTip(tr("Add, edit and delete setups"));
116 connect(configSetupsAction, SIGNAL(triggered()), this, SLOT(configureSetupss()));
118 configEmulatorAction = new QAction(tr("Em&ulators"), this);
119 configEmulatorAction->setStatusTip(tr("Configure emulators"));
120 connect(configEmulatorAction, SIGNAL(triggered()), this, SLOT(configureEmulators()));
122 configTmpDirAction = new QAction(tr("&Temp dir"), this);
123 configTmpDirAction->setStatusTip(tr("Configure directory for temporary files."));
124 connect(configTmpDirAction, SIGNAL(triggered()), this, SLOT(configureTmpDir()));
126 manageDatFilesAction = new QAction(tr("&Manage dats"), this);
127 manageDatFilesAction->setStatusTip(tr("Read dat files to database."));
128 connect(manageDatFilesAction, SIGNAL(triggered()), this, SLOT(manageDatFiles()));
130 exitAction = new QAction(tr("&Exit"), this);
131 exitAction->setShortcut(tr("Ctrl+Q"));
132 exitAction->setStatusTip(tr("Exit EmuFront"));
133 connect(exitAction, SIGNAL(triggered()), this, SLOT(close()));
135 resetDbAction = new QAction( tr("Reset database"), this);
136 resetDbAction->setStatusTip(tr("Deletes all the current data and create a new database."));
137 connect(resetDbAction, SIGNAL(triggered()), this, SLOT(resetDb()));
139 aboutAction = new QAction(tr("&About"), this);
140 aboutAction->setStatusTip(tr("About EmuFront"));
141 connect(aboutAction, SIGNAL(triggered()), this, SLOT(about()));
145 void MainWindow::configurePlatforms()
149 platformDialog = new PlatformDialog(this);
150 connect(platformDialog, SIGNAL(finished(int)), this, SLOT(updateData()));
152 activateDialog(platformDialog);
155 void MainWindow::configurePlatformss()
158 plfDialog = new PlatformEditView(this);
159 connect(plfDialog, SIGNAL(finished(int)), this, SLOT(updateData()));
161 activateDialog(plfDialog);
165 void MainWindow::configureMediaTypes()
167 if (!mediaTypeDialog)
169 mediaTypeDialog = new MediaTypeDialog(this);
170 connect(mediaTypeDialog, SIGNAL(finished(int)), this, SLOT(updateData()));
172 activateDialog(mediaTypeDialog);
175 void MainWindow::configureMediaTypess()
179 mdtDialog = new MediaTypeEditView(this);
180 connect(mdtDialog, SIGNAL(finished(int)), this, SLOT(updateData()));
182 activateDialog(mdtDialog);
186 void MainWindow::configureMediaImagePaths()
188 if (!mediaImagePathDialog)
190 mediaImagePathDialog = new MediaImagePathMainDialog(this);
192 activateDialog(mediaImagePathDialog);
196 void MainWindow::configureSetups()
198 if (!setupMainDialog)
200 setupMainDialog = new SetupMainDialog(this);
202 activateDialog(setupMainDialog);
203 setupMainDialog->refreshDataModel();
206 void MainWindow::configureSetupss()
208 if (!setupMainView) {
209 setupMainView = new SetupEditView(this);
211 activateDialog(setupMainView);
215 void MainWindow::configureEmulators()
217 if (!executableMainDialog) {
218 executableMainDialog = new ExecutableMainDialog(this);
219 connect(executableMainDialog, SIGNAL(finished(int)), this, SLOT(updateData()));
221 activateDialog(executableMainDialog);
222 executableMainDialog->refreshDataModel();
225 void MainWindow::configureTmpDir()
227 /*if (!tmpFolderDialog) {
228 tmpFolderDialog = new TmpFolderEditDialog(this, tmpDirFilePath);
230 activateDialog(tmpFolderDialog);*/
232 QString fpath = QFileDialog::getExistingDirectory(this,
233 tr("Select a directory"), tmpDirFilePath,
234 QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
236 if (d.exists() && d.isReadable()) {
237 tmpDirFilePath = fpath;
238 DbConfig::setTmpDir(tmpDirFilePath);
239 launcher->setTmpDirPath(tmpDirFilePath);
243 void MainWindow::resetDb()
245 if (QMessageBox::question(this, "Reset database?",
246 "Are you REALLY SURE you want to do this? "
247 "All the current data WILL BE LOST!",
249 QMessageBox::Yes) == QMessageBox::No) {
255 catch (EmuFrontException e) {
256 errorMessage->showMessage(e.what());
260 void MainWindow::manageDatFiles()
266 void MainWindow::activateDialog(EmuFrontDialog* dia) const
270 dia->activateWindow();
273 void MainWindow::createMenus()
275 fileMenu = menuBar()->addMenu(tr("&File"));
276 fileMenu->addAction(resetDbAction);
277 fileMenu->addSeparator();
278 fileMenu->addAction(exitAction);
280 configMenu = menuBar()->addMenu(tr("&Config"));
281 configMenu->addAction(configTmpDirAction);
282 configMenu->addSeparator();
284 configMenu->addAction(configPlatformAction);
285 configMenu->addAction(configPlatformsAction);
287 configMenu->addAction(configMediaTypeAction);
288 configMenu->addAction(configMediaTypesAction);
290 configMenu->addAction(configSetupAction);
291 configMenu->addAction(configSetupsAction);
292 configMenu->addAction(configMediaImagePathAction);
293 configMenu->addAction(configEmulatorAction);
294 configMenu->addSeparator();
295 configMenu->addAction(manageDatFilesAction);
297 helpMenu = menuBar()->addMenu(tr("&Help"));
298 helpMenu->addAction(aboutAction);
301 void MainWindow::createStatusBar()
303 messageLabel = new QLabel;
304 statusBar()->addWidget(messageLabel);
307 void MainWindow::readSettings()
311 void MainWindow::writeSettings()
315 void MainWindow::closeEvent(QCloseEvent *event)
319 else event->ignore();
322 bool MainWindow::okToContinue()
327 void MainWindow::updateData()
329 qDebug() << "MainWindow::updateData()";
330 launcher->updateData();
333 void MainWindow::about()
335 QMessageBox::about(this, aboutTitle, aboutStr);
338 bool MainWindow::testDB(bool reset)
341 if (DatabaseManager::openDB()) {
342 qDebug() << " Database opened succesfully!";
345 throw EmuFrontException("Database connection failed!");
348 int dbVer = DbCreator::dbExists();
349 if (dbVer == 0) reset = true;
350 if (!reset && dbVer != DbCreator::DB_VERSION) {
351 QString msg("Database is not compatible "
352 "with current version of EmuFront!"
353 "Do you want to continue to recreate the database?"
354 "ALL THE CURRENT DATA WILL BE LOST!!!");
355 if (QMessageBox::question(this, "Database not compatible!", msg,
356 QMessageBox::Yes, QMessageBox::No) == QMessageBox::Yes) {
359 else throw EmuFrontException("The current database is not compatible!"
360 " Cannot continue.");
368 catch (EmuFrontException e) {
369 qDebug() << e.what();
370 errorMessage->showMessage(e.what());
375 void MainWindow::createDB() const
380 dbCreator.createDB();
382 catch (QString str) {
383 QString msg(tr("Exception while trying to create"
384 " EmuFront database: %s").arg(str));
385 errorMessage->showMessage(msg);