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 setWindowTitle("EmuFront");
48 tmpDirFilePath = DbConfig::getTmpDir();
49 if (tmpDirFilePath.isEmpty())
50 tmpDirFilePath = QDir::homePath();
51 qDebug() << "Temporary dir is " << tmpDirFilePath;
52 launcher = new EmuLauncher(this, tmpDirFilePath);
53 setCentralWidget(launcher);
60 mediaImagePathDialog = 0;
62 executableMainDialog = 0;
65 void MainWindow::connectSignals()
69 void MainWindow::createActions()
71 configPlatformAction = new QAction(tr("&Platforms"), this);
72 configPlatformAction->setStatusTip(tr("Configure platforms"));
73 connect(configPlatformAction, SIGNAL(triggered()),
74 this, SLOT(configurePlatforms()));
76 configMediaTypeAction = new QAction(tr("&Media Types"), this);
77 configMediaTypeAction->setStatusTip(tr("Configure media types"));
78 connect(configMediaTypeAction, SIGNAL(triggered()), this, SLOT(configureMediaTypes()));
80 configMediaImagePathAction = new QAction(tr("Media &Image Paths"), this);
81 configMediaImagePathAction->setStatusTip(tr("Configure media image file paths."));
82 connect(configMediaImagePathAction, SIGNAL(triggered()),
83 this, SLOT(configureMediaImagePaths()));
85 configSetupAction = new QAction(tr("S&etups"), this);
86 configSetupAction->setStatusTip(tr("Configure set ups"));
87 connect(configSetupAction, SIGNAL(triggered()), this, SLOT(configureSetups()));
89 configEmulatorAction = new QAction(tr("Em&ulators"), this);
90 configEmulatorAction->setStatusTip(tr("Configure emulators"));
91 connect(configEmulatorAction, SIGNAL(triggered()), this, SLOT(configureEmulators()));
93 configTmpDirAction = new QAction(tr("&Temp dir"), this);
94 configTmpDirAction->setStatusTip(tr("Configure directory for temporary files."));
95 connect(configTmpDirAction, SIGNAL(triggered()), this, SLOT(configureTmpDir()));
97 manageDatFilesAction = new QAction(tr("&Manage dats"), this);
98 manageDatFilesAction->setStatusTip(tr("Read dat files to database."));
99 connect(manageDatFilesAction, SIGNAL(triggered()), this, SLOT(manageDatFiles()));
101 exitAction = new QAction(tr("&Exit"), this);
102 exitAction->setShortcut(tr("Ctrl+Q"));
103 exitAction->setStatusTip(tr("Exit EmuFront"));
104 connect(exitAction, SIGNAL(triggered()), this, SLOT(close()));
106 resetDbAction = new QAction( tr("Reset database"), this);
107 resetDbAction->setStatusTip(tr("Deletes all the current data and create a new database."));
108 connect(resetDbAction, SIGNAL(triggered()), this, SLOT(resetDb()));
110 aboutAction = new QAction(tr("&About"), this);
111 aboutAction->setStatusTip(tr("About EmuFront"));
112 connect(aboutAction, SIGNAL(triggered()), this, SLOT(about()));
115 void MainWindow::configurePlatforms()
119 platformDialog = new PlatformDialog(this);
120 connect(platformDialog, SIGNAL(finished(int)), this, SLOT(updateData()));
122 activateDialog(platformDialog);
125 void MainWindow::configureMediaTypes()
127 if (!mediaTypeDialog)
129 mediaTypeDialog = new MediaTypeDialog(this);
130 connect(mediaTypeDialog, SIGNAL(finished(int)), this, SLOT(updateData()));
132 activateDialog(mediaTypeDialog);
135 void MainWindow::configureMediaImagePaths()
137 if (!mediaImagePathDialog)
139 mediaImagePathDialog = new MediaImagePathMainDialog(this);
141 activateDialog(mediaImagePathDialog);
144 void MainWindow::configureSetups()
146 if (!setupMainDialog)
148 qDebug() << "MainWindow: Creating a setup main dialog.";
149 setupMainDialog = new SetupMainDialog(this);
151 activateDialog(setupMainDialog);
152 setupMainDialog->refreshDataModel();
155 void MainWindow::configureEmulators()
157 if (!executableMainDialog) {
158 executableMainDialog = new ExecutableMainDialog(this);
159 connect(executableMainDialog, SIGNAL(finished(int)), this, SLOT(updateData()));
161 activateDialog(executableMainDialog);
162 executableMainDialog->refreshDataModel();
165 void MainWindow::configureTmpDir()
167 /*if (!tmpFolderDialog) {
168 tmpFolderDialog = new TmpFolderEditDialog(this, tmpDirFilePath);
170 activateDialog(tmpFolderDialog);*/
172 QString fpath = QFileDialog::getExistingDirectory(this,
173 tr("Select a directory"), tmpDirFilePath,
174 QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
176 if (d.exists() && d.isReadable()) {
177 tmpDirFilePath = fpath;
178 DbConfig::setTmpDir(tmpDirFilePath);
179 launcher->setTmpDirPath(tmpDirFilePath);
183 void MainWindow::resetDb()
185 if (QMessageBox::question(this, "Reset database?",
186 "Are you REALLY SURE you want to do this? "
187 "All the current data WILL BE LOST!",
189 QMessageBox::Yes) == QMessageBox::No) {
195 catch (EmuFrontException e) {
196 qDebug() << e.what();
197 QMessageBox::critical(this, "Exception", 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 QMessageBox::critical(this, "Exception", e.what());
310 /* Throws EmuFrontException */
311 void MainWindow::createDB() const
316 dbCreator.createDB();
318 catch (QString str) {
319 QString msg(tr("Exception while trying to create"
320 " EmuFront database: %s").arg(str));
321 throw EmuFrontException(msg);