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 "dialogs/platformdialog.h"
25 #include "dialogs/platformmaindialog.h"
26 #include "dialogs/mediatypedialog.h"
28 #include "dialogs/mediatypemaindialog.h"
29 #include "dialogs/mediaimagepathmaindialog.h"
30 #include "dialogs/setupmaindialog.h"
31 #include "dialogs/executablemaindialog.h"
32 #include "utils/datfileutil.h"
33 #include "db/databasemanager.h"
34 #include "db/dbcreator.h"
35 #include "db/dbconfig.h"
37 QString MainWindow::aboutStr = trUtf8(
39 "<p>© 2010 Mikko Keinänen</p>"
40 "<p>mikko.keinanen@gmail.com</p>"
41 "<p>EmuFront is free software: you can redistribute it and/or modify "
42 "it under the terms of the GNU General Public License version 2 as published by "
43 "the Free Software Foundation.</p>"
46 QString MainWindow::aboutTitle = tr("About EmuFront");
48 MainWindow::MainWindow(bool reset)
50 if (!testDB(reset)) close();
51 errorMessage = new QErrorMessage(this);
52 setWindowTitle("EmuFront");
53 tmpDirFilePath = DbConfig::getTmpDir();
54 if (tmpDirFilePath.isEmpty())
55 tmpDirFilePath = QDir::homePath();
56 qDebug() << "Temporary dir is " << tmpDirFilePath;
57 launcher = new EmuLauncher(errorMessage, this, tmpDirFilePath);
58 setCentralWidget(launcher);
69 mediaImagePathDialog = 0;
71 executableMainDialog = 0;
74 void MainWindow::connectSignals()
78 void MainWindow::createActions()
81 configPlatformAction = new QAction(tr("&Platforms"), this);
82 configPlatformAction->setStatusTip(tr("Configure platforms"));
83 connect(configPlatformAction, SIGNAL(triggered()),
84 this, SLOT(configurePlatforms()));
86 configPlatformsAction = new QAction(tr("&Set Platforms"), this);
87 configPlatformsAction->setStatusTip(tr("Add, edit and delete platforms"));
88 connect(configPlatformsAction, SIGNAL(triggered()),
89 this, SLOT(configurePlatformss()));
92 configMediaTypeAction = new QAction(tr("&Media Types"), this);
93 configMediaTypeAction->setStatusTip(tr("Configure media types"));
94 connect(configMediaTypeAction, SIGNAL(triggered()), this, SLOT(configureMediaTypes()));
96 configMediaTypesAction = new QAction(tr("&Set Media Types"), this);
97 configMediaTypeAction->setStatusTip(tr("Add, edit and delete media types"));
98 connect(configMediaTypesAction, SIGNAL(triggered()), this, SLOT(configureMediaTypess()));
100 configMediaImagePathAction = new QAction(tr("Media &Image Paths"), this);
101 configMediaImagePathAction->setStatusTip(tr("Configure media image file paths."));
102 connect(configMediaImagePathAction, SIGNAL(triggered()),
103 this, SLOT(configureMediaImagePaths()));
105 configSetupAction = new QAction(tr("S&etups"), this);
106 configSetupAction->setStatusTip(tr("Configure set ups"));
107 connect(configSetupAction, SIGNAL(triggered()), this, SLOT(configureSetups()));
109 configEmulatorAction = new QAction(tr("Em&ulators"), this);
110 configEmulatorAction->setStatusTip(tr("Configure emulators"));
111 connect(configEmulatorAction, SIGNAL(triggered()), this, SLOT(configureEmulators()));
113 configTmpDirAction = new QAction(tr("&Temp dir"), this);
114 configTmpDirAction->setStatusTip(tr("Configure directory for temporary files."));
115 connect(configTmpDirAction, SIGNAL(triggered()), this, SLOT(configureTmpDir()));
117 manageDatFilesAction = new QAction(tr("&Manage dats"), this);
118 manageDatFilesAction->setStatusTip(tr("Read dat files to database."));
119 connect(manageDatFilesAction, SIGNAL(triggered()), this, SLOT(manageDatFiles()));
121 exitAction = new QAction(tr("&Exit"), this);
122 exitAction->setShortcut(tr("Ctrl+Q"));
123 exitAction->setStatusTip(tr("Exit EmuFront"));
124 connect(exitAction, SIGNAL(triggered()), this, SLOT(close()));
126 resetDbAction = new QAction( tr("Reset database"), this);
127 resetDbAction->setStatusTip(tr("Deletes all the current data and create a new database."));
128 connect(resetDbAction, SIGNAL(triggered()), this, SLOT(resetDb()));
130 aboutAction = new QAction(tr("&About"), this);
131 aboutAction->setStatusTip(tr("About EmuFront"));
132 connect(aboutAction, SIGNAL(triggered()), this, SLOT(about()));
136 void MainWindow::configurePlatforms()
140 platformDialog = new PlatformDialog(this);
141 connect(platformDialog, SIGNAL(finished(int)), this, SLOT(updateData()));
143 activateDialog(platformDialog);
146 void MainWindow::configurePlatformss()
149 plfDialog = new PlatformMainDialog(this);
150 connect(plfDialog, SIGNAL(finished(int)), this, SLOT(updateData()));
152 activateDialog(plfDialog);
156 void MainWindow::configureMediaTypes()
158 if (!mediaTypeDialog)
160 mediaTypeDialog = new MediaTypeDialog(this);
161 connect(mediaTypeDialog, SIGNAL(finished(int)), this, SLOT(updateData()));
163 activateDialog(mediaTypeDialog);
166 void MainWindow::configureMediaTypess()
170 mdtDialog = new MediaTypeMainDialog(this);
171 connect(mdtDialog, SIGNAL(finished(int)), this, SLOT(updateData()));
173 activateDialog(mdtDialog);
177 void MainWindow::configureMediaImagePaths()
179 if (!mediaImagePathDialog)
181 mediaImagePathDialog = new MediaImagePathMainDialog(this);
183 activateDialog(mediaImagePathDialog);
186 void MainWindow::configureSetups()
188 if (!setupMainDialog)
190 qDebug() << "MainWindow: Creating a setup main dialog.";
191 setupMainDialog = new SetupMainDialog(this);
193 activateDialog(setupMainDialog);
194 setupMainDialog->refreshDataModel();
197 void MainWindow::configureEmulators()
199 if (!executableMainDialog) {
200 executableMainDialog = new ExecutableMainDialog(this);
201 connect(executableMainDialog, SIGNAL(finished(int)), this, SLOT(updateData()));
203 activateDialog(executableMainDialog);
204 executableMainDialog->refreshDataModel();
207 void MainWindow::configureTmpDir()
209 /*if (!tmpFolderDialog) {
210 tmpFolderDialog = new TmpFolderEditDialog(this, tmpDirFilePath);
212 activateDialog(tmpFolderDialog);*/
214 QString fpath = QFileDialog::getExistingDirectory(this,
215 tr("Select a directory"), tmpDirFilePath,
216 QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
218 if (d.exists() && d.isReadable()) {
219 tmpDirFilePath = fpath;
220 DbConfig::setTmpDir(tmpDirFilePath);
221 launcher->setTmpDirPath(tmpDirFilePath);
225 void MainWindow::resetDb()
227 if (QMessageBox::question(this, "Reset database?",
228 "Are you REALLY SURE you want to do this? "
229 "All the current data WILL BE LOST!",
231 QMessageBox::Yes) == QMessageBox::No) {
237 catch (EmuFrontException e) {
238 errorMessage->showMessage(e.what());
242 void MainWindow::manageDatFiles()
248 void MainWindow::activateDialog(EmuFrontDialog* dia) const
252 dia->activateWindow();
255 void MainWindow::createMenus()
257 fileMenu = menuBar()->addMenu(tr("&File"));
258 fileMenu->addAction(resetDbAction);
259 fileMenu->addSeparator();
260 fileMenu->addAction(exitAction);
262 configMenu = menuBar()->addMenu(tr("&Config"));
263 configMenu->addAction(configTmpDirAction);
264 configMenu->addSeparator();
266 configMenu->addAction(configPlatformAction);
267 configMenu->addAction(configPlatformsAction);
269 configMenu->addAction(configMediaTypeAction);
270 configMenu->addAction(configMediaTypesAction);
271 configMenu->addAction(configSetupAction);
272 configMenu->addAction(configMediaImagePathAction);
273 configMenu->addAction(configEmulatorAction);
274 configMenu->addSeparator();
275 configMenu->addAction(manageDatFilesAction);
277 helpMenu = menuBar()->addMenu(tr("&Help"));
278 helpMenu->addAction(aboutAction);
281 void MainWindow::createStatusBar()
283 messageLabel = new QLabel;
284 statusBar()->addWidget(messageLabel);
287 void MainWindow::readSettings()
291 void MainWindow::writeSettings()
295 void MainWindow::closeEvent(QCloseEvent *event)
299 else event->ignore();
302 bool MainWindow::okToContinue()
307 void MainWindow::updateData()
309 qDebug() << "MainWindow::updateData()";
310 launcher->updateData();
313 void MainWindow::about()
315 QMessageBox::about(this, aboutTitle, aboutStr);
318 bool MainWindow::testDB(bool reset)
321 if (DatabaseManager::openDB()) {
322 qDebug() << " Database opened succesfully!";
325 throw EmuFrontException("Database connection failed!");
328 int dbVer = DbCreator::dbExists();
329 if (dbVer == 0) reset = true;
330 if (!reset && dbVer != DbCreator::DB_VERSION) {
331 QString msg("Database is not compatible "
332 "with current version of EmuFront!"
333 "Do you want to continue to recreate the database?"
334 "ALL THE CURRENT DATA WILL BE LOST!!!");
335 if (QMessageBox::question(this, "Database not compatible!", msg,
336 QMessageBox::Yes, QMessageBox::No) == QMessageBox::Yes) {
339 else throw EmuFrontException("The current database is not compatible!"
340 " Cannot continue.");
348 catch (EmuFrontException e) {
349 qDebug() << e.what();
350 errorMessage->showMessage(e.what());
355 void MainWindow::createDB() const
360 dbCreator.createDB();
362 catch (QString str) {
363 QString msg(tr("Exception while trying to create"
364 " EmuFront database: %s").arg(str));
365 errorMessage->showMessage(msg);