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/platformmaindialog.h"
25 #include "dialogs/mediatypedialog.h"
26 #include "dialogs/mediaimagepathmaindialog.h"
27 #include "dialogs/setupmaindialog.h"
28 #include "dialogs/executablemaindialog.h"
29 #include "utils/datfileutil.h"
30 #include "db/databasemanager.h"
31 #include "db/dbcreator.h"
32 #include "db/dbconfig.h"
34 QString MainWindow::aboutStr = trUtf8(
36 "<p>© 2010 Mikko Keinänen</p>"
37 "<p>mikko.keinanen@gmail.com</p>"
38 "<p>EmuFront is free software: you can redistribute it and/or modify "
39 "it under the terms of the GNU General Public License version 2 as published by "
40 "the Free Software Foundation.</p>"
43 QString MainWindow::aboutTitle = tr("About EmuFront");
45 MainWindow::MainWindow(bool reset)
47 if (!testDB(reset)) close();
48 errorMessage = new QErrorMessage(this);
49 setWindowTitle("EmuFront");
50 tmpDirFilePath = DbConfig::getTmpDir();
51 if (tmpDirFilePath.isEmpty())
52 tmpDirFilePath = QDir::homePath();
53 qDebug() << "Temporary dir is " << tmpDirFilePath;
54 launcher = new EmuLauncher(errorMessage, this, tmpDirFilePath);
55 setCentralWidget(launcher);
62 mediaImagePathDialog = 0;
64 executableMainDialog = 0;
67 void MainWindow::connectSignals()
71 void MainWindow::createActions()
73 configPlatformAction = new QAction(tr("&Platforms"), this);
74 configPlatformAction->setStatusTip(tr("Configure platforms"));
75 connect(configPlatformAction, SIGNAL(triggered()),
76 this, SLOT(configurePlatforms()));
78 configPlatformsAction = new QAction(tr("&Set Platforms"), this);
79 configPlatformsAction->setStatusTip(tr("Add, edit and delete platforms"));
80 connect(configPlatformsAction, SIGNAL(triggered()),
81 this, SLOT(configurePlatformss()));
83 configMediaTypeAction = new QAction(tr("&Media Types"), this);
84 configMediaTypeAction->setStatusTip(tr("Configure media types"));
85 connect(configMediaTypeAction, SIGNAL(triggered()), this, SLOT(configureMediaTypes()));
87 configMediaImagePathAction = new QAction(tr("Media &Image Paths"), this);
88 configMediaImagePathAction->setStatusTip(tr("Configure media image file paths."));
89 connect(configMediaImagePathAction, SIGNAL(triggered()),
90 this, SLOT(configureMediaImagePaths()));
92 configSetupAction = new QAction(tr("S&etups"), this);
93 configSetupAction->setStatusTip(tr("Configure set ups"));
94 connect(configSetupAction, SIGNAL(triggered()), this, SLOT(configureSetups()));
96 configEmulatorAction = new QAction(tr("Em&ulators"), this);
97 configEmulatorAction->setStatusTip(tr("Configure emulators"));
98 connect(configEmulatorAction, SIGNAL(triggered()), this, SLOT(configureEmulators()));
100 configTmpDirAction = new QAction(tr("&Temp dir"), this);
101 configTmpDirAction->setStatusTip(tr("Configure directory for temporary files."));
102 connect(configTmpDirAction, SIGNAL(triggered()), this, SLOT(configureTmpDir()));
104 manageDatFilesAction = new QAction(tr("&Manage dats"), this);
105 manageDatFilesAction->setStatusTip(tr("Read dat files to database."));
106 connect(manageDatFilesAction, SIGNAL(triggered()), this, SLOT(manageDatFiles()));
108 exitAction = new QAction(tr("&Exit"), this);
109 exitAction->setShortcut(tr("Ctrl+Q"));
110 exitAction->setStatusTip(tr("Exit EmuFront"));
111 connect(exitAction, SIGNAL(triggered()), this, SLOT(close()));
113 resetDbAction = new QAction( tr("Reset database"), this);
114 resetDbAction->setStatusTip(tr("Deletes all the current data and create a new database."));
115 connect(resetDbAction, SIGNAL(triggered()), this, SLOT(resetDb()));
117 aboutAction = new QAction(tr("&About"), this);
118 aboutAction->setStatusTip(tr("About EmuFront"));
119 connect(aboutAction, SIGNAL(triggered()), this, SLOT(about()));
122 void MainWindow::configurePlatforms()
126 platformDialog = new PlatformDialog(this);
127 connect(platformDialog, SIGNAL(finished(int)), this, SLOT(updateData()));
129 activateDialog(platformDialog);
132 void MainWindow::configurePlatformss()
134 plfDialog = new PlatformMainDialog(this);
135 connect(plfDialog, SIGNAL(finished(int)), this, SLOT(updateData()));
136 activateDialog(plfDialog);
139 void MainWindow::configureMediaTypes()
141 if (!mediaTypeDialog)
143 mediaTypeDialog = new MediaTypeDialog(this);
144 connect(mediaTypeDialog, SIGNAL(finished(int)), this, SLOT(updateData()));
146 activateDialog(mediaTypeDialog);
149 void MainWindow::configureMediaImagePaths()
151 if (!mediaImagePathDialog)
153 mediaImagePathDialog = new MediaImagePathMainDialog(this);
155 activateDialog(mediaImagePathDialog);
158 void MainWindow::configureSetups()
160 if (!setupMainDialog)
162 qDebug() << "MainWindow: Creating a setup main dialog.";
163 setupMainDialog = new SetupMainDialog(this);
165 activateDialog(setupMainDialog);
166 setupMainDialog->refreshDataModel();
169 void MainWindow::configureEmulators()
171 if (!executableMainDialog) {
172 executableMainDialog = new ExecutableMainDialog(this);
173 connect(executableMainDialog, SIGNAL(finished(int)), this, SLOT(updateData()));
175 activateDialog(executableMainDialog);
176 executableMainDialog->refreshDataModel();
179 void MainWindow::configureTmpDir()
181 /*if (!tmpFolderDialog) {
182 tmpFolderDialog = new TmpFolderEditDialog(this, tmpDirFilePath);
184 activateDialog(tmpFolderDialog);*/
186 QString fpath = QFileDialog::getExistingDirectory(this,
187 tr("Select a directory"), tmpDirFilePath,
188 QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
190 if (d.exists() && d.isReadable()) {
191 tmpDirFilePath = fpath;
192 DbConfig::setTmpDir(tmpDirFilePath);
193 launcher->setTmpDirPath(tmpDirFilePath);
197 void MainWindow::resetDb()
199 if (QMessageBox::question(this, "Reset database?",
200 "Are you REALLY SURE you want to do this? "
201 "All the current data WILL BE LOST!",
203 QMessageBox::Yes) == QMessageBox::No) {
209 catch (EmuFrontException e) {
210 errorMessage->showMessage(e.what());
214 void MainWindow::manageDatFiles()
220 void MainWindow::activateDialog(EmuFrontDialog* dia) const
224 dia->activateWindow();
227 void MainWindow::createMenus()
229 fileMenu = menuBar()->addMenu(tr("&File"));
230 fileMenu->addAction(resetDbAction);
231 fileMenu->addSeparator();
232 fileMenu->addAction(exitAction);
234 configMenu = menuBar()->addMenu(tr("&Config"));
235 configMenu->addAction(configTmpDirAction);
236 configMenu->addSeparator();
237 configMenu->addAction(configPlatformAction);
238 configMenu->addAction(configPlatformsAction);
239 configMenu->addAction(configMediaTypeAction);
240 configMenu->addAction(configSetupAction);
241 configMenu->addAction(configMediaImagePathAction);
242 configMenu->addAction(configEmulatorAction);
243 configMenu->addSeparator();
244 configMenu->addAction(manageDatFilesAction);
246 helpMenu = menuBar()->addMenu(tr("&Help"));
247 helpMenu->addAction(aboutAction);
250 void MainWindow::createStatusBar()
252 messageLabel = new QLabel;
253 statusBar()->addWidget(messageLabel);
256 void MainWindow::readSettings()
260 void MainWindow::writeSettings()
264 void MainWindow::closeEvent(QCloseEvent *event)
268 else event->ignore();
271 bool MainWindow::okToContinue()
276 void MainWindow::updateData()
278 qDebug() << "MainWindow::updateData()";
279 launcher->updateData();
282 void MainWindow::about()
284 QMessageBox::about(this, aboutTitle, aboutStr);
287 bool MainWindow::testDB(bool reset)
290 if (DatabaseManager::openDB()) {
291 qDebug() << " Database opened succesfully!";
294 throw EmuFrontException("Database connection failed!");
297 int dbVer = DbCreator::dbExists();
298 if (dbVer == 0) reset = true;
299 if (!reset && dbVer != DbCreator::DB_VERSION) {
300 QString msg("Database is not compatible "
301 "with current version of EmuFront!"
302 "Do you want to continue to recreate the database?"
303 "ALL THE CURRENT DATA WILL BE LOST!!!");
304 if (QMessageBox::question(this, "Database not compatible!", msg,
305 QMessageBox::Yes, QMessageBox::No) == QMessageBox::Yes) {
308 else throw EmuFrontException("The current database is not compatible!"
309 " Cannot continue.");
317 catch (EmuFrontException e) {
318 qDebug() << e.what();
319 errorMessage->showMessage(e.what());
324 void MainWindow::createDB() const
329 dbCreator.createDB();
331 catch (QString str) {
332 QString msg(tr("Exception while trying to create"
333 " EmuFront database: %s").arg(str));
334 errorMessage->showMessage(msg);