3 ** Copyright 2010 Mikko Keinänen
5 ** This file is part of EmuFront.
8 ** EmuFront is free software: you can redistribute it and/or modify
9 ** it under the terms of the GNU General Public License version 2 as published by
10 ** the Free Software Foundation and appearing in the file gpl.txt included in the
11 ** packaging of this file.
13 ** EmuFront is distributed in the hope that it will be useful,
14 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ** GNU General Public License for more details.
18 ** You should have received a copy of the GNU General Public License
19 ** along with EmuFront. If not, see <http://www.gnu.org/licenses/>.
22 #include "mainwindow.h"
23 #include "emulauncher.h"
25 #include "platformdialog.h"
26 #include "platformeditview.h"
27 #include "mediatypedialog.h"
29 #include "mediatypeeditview.h"
31 #include "mediaimagepathmaindialog.h"
32 #include "filepatheditview.h"
34 #include "setupmaindialog.h"
35 #include "setupeditview.h"
36 #include "executablemaindialog.h"
37 #include "datfileutil.h"
38 #include "databasemanager.h"
39 #include "dbcreator.h"
42 QString MainWindow::aboutStr = trUtf8(
44 "<p>© 2010 Mikko Keinänen</p>"
45 "<p>mikko.keinanen@gmail.com</p>"
46 "<p>EmuFront is free software: you can redistribute it and/or modify "
47 "it under the terms of the GNU General Public License version 2 as published by "
48 "the Free Software Foundation.</p>"
51 QString MainWindow::aboutTitle = tr("About EmuFront");
53 MainWindow::MainWindow(bool reset)
55 if (!testDB(reset)) close();
56 errorMessage = new QErrorMessage(this);
57 setWindowTitle("EmuFront");
58 tmpDirFilePath = DbConfig::getTmpDir();
59 if (tmpDirFilePath.isEmpty())
60 tmpDirFilePath = QDir::homePath();
61 qDebug() << "Temporary dir is " << tmpDirFilePath;
62 launcher = new EmuLauncher(errorMessage, this, tmpDirFilePath);
63 setCentralWidget(launcher);
75 mediaImagePathDialog = 0;
76 mediaImagePathView = 0;
80 executableMainDialog = 0;
83 void MainWindow::connectSignals()
87 void MainWindow::createActions()
90 configPlatformAction = new QAction(tr("&Platforms"), this);
91 configPlatformAction->setStatusTip(tr("Configure platforms"));
92 connect(configPlatformAction, SIGNAL(triggered()),
93 this, SLOT(configurePlatforms()));
95 configPlatformsAction = new QAction(tr("&Set Platforms"), this);
96 configPlatformsAction->setStatusTip(tr("Add, edit and delete platforms"));
97 connect(configPlatformsAction, SIGNAL(triggered()),
98 this, SLOT(configurePlatformss()));
101 configMediaTypeAction = new QAction(tr("&Media Types"), this);
102 configMediaTypeAction->setStatusTip(tr("Configure media types"));
103 connect(configMediaTypeAction, SIGNAL(triggered()), this, SLOT(configureMediaTypes()));
105 configMediaTypesAction = new QAction(tr("&Set Media Types"), this);
106 configMediaTypeAction->setStatusTip(tr("Add, edit and delete media types"));
107 connect(configMediaTypesAction, SIGNAL(triggered()), this, SLOT(configureMediaTypess()));
110 configMediaImagePathAction = new QAction(tr("Media &Image Paths"), this);
111 configMediaImagePathAction->setStatusTip(tr("Configure media image file paths."));
112 connect(configMediaImagePathAction, SIGNAL(triggered()),
113 this, SLOT(configureMediaImagePaths()));
115 configMediaImagePathsAction = new QAction(tr("Set media &image paths"), this);
116 configMediaImagePathsAction->setStatusTip(tr("Add, edit and delete media image file paths."));
117 connect(configMediaImagePathsAction, SIGNAL(triggered()),
118 this, SLOT(configureMediaImagePathss()));
121 configSetupAction = new QAction(tr("S&etups"), this);
122 configSetupAction->setStatusTip(tr("Configure set ups"));
123 connect(configSetupAction, SIGNAL(triggered()), this, SLOT(configureSetups()));
125 configSetupsAction = new QAction(tr("Configure S&etups"), this);
126 configSetupsAction->setStatusTip(tr("Add, edit and delete setups"));
127 connect(configSetupsAction, SIGNAL(triggered()), this, SLOT(configureSetupss()));
129 configEmulatorAction = new QAction(tr("Em&ulators"), this);
130 configEmulatorAction->setStatusTip(tr("Configure emulators"));
131 connect(configEmulatorAction, SIGNAL(triggered()), this, SLOT(configureEmulators()));
133 configTmpDirAction = new QAction(tr("&Temp dir"), this);
134 configTmpDirAction->setStatusTip(tr("Configure directory for temporary files."));
135 connect(configTmpDirAction, SIGNAL(triggered()), this, SLOT(configureTmpDir()));
137 manageDatFilesAction = new QAction(tr("&Manage dats"), this);
138 manageDatFilesAction->setStatusTip(tr("Read dat files to database."));
139 connect(manageDatFilesAction, SIGNAL(triggered()), this, SLOT(manageDatFiles()));
141 exitAction = new QAction(tr("&Exit"), this);
142 exitAction->setShortcut(tr("Ctrl+Q"));
143 exitAction->setStatusTip(tr("Exit EmuFront"));
144 connect(exitAction, SIGNAL(triggered()), this, SLOT(close()));
146 resetDbAction = new QAction( tr("Reset database"), this);
147 resetDbAction->setStatusTip(tr("Deletes all the current data and create a new database."));
148 connect(resetDbAction, SIGNAL(triggered()), this, SLOT(resetDb()));
150 aboutAction = new QAction(tr("&About"), this);
151 aboutAction->setStatusTip(tr("About EmuFront"));
152 connect(aboutAction, SIGNAL(triggered()), this, SLOT(about()));
156 void MainWindow::configurePlatforms()
160 platformDialog = new PlatformDialog(this);
161 connect(platformDialog, SIGNAL(finished(int)), this, SLOT(updateData()));
163 activateDialog(platformDialog);
166 void MainWindow::configurePlatformss()
169 plfDialog = new PlatformEditView(this);
170 connect(plfDialog, SIGNAL(finished(int)), this, SLOT(updateData()));
172 activateDialog(plfDialog);
176 void MainWindow::configureMediaTypes()
178 if (!mediaTypeDialog)
180 mediaTypeDialog = new MediaTypeDialog(this);
181 connect(mediaTypeDialog, SIGNAL(finished(int)), this, SLOT(updateData()));
183 activateDialog(mediaTypeDialog);
186 void MainWindow::configureMediaTypess()
190 mdtDialog = new MediaTypeEditView(this);
191 connect(mdtDialog, SIGNAL(finished(int)), this, SLOT(updateData()));
193 activateDialog(mdtDialog);
198 void MainWindow::configureMediaImagePaths()
200 if (!mediaImagePathDialog)
202 mediaImagePathDialog = new MediaImagePathMainDialog(this);
204 activateDialog(mediaImagePathDialog);
207 void MainWindow::configureMediaImagePathss()
209 if (!mediaImagePathView)
211 mediaImagePathView = new FilePathEditView(this);
213 activateDialog(mediaImagePathView);
217 void MainWindow::configureSetups()
219 if (!setupMainDialog)
221 setupMainDialog = new SetupMainDialog(this);
223 activateDialog(setupMainDialog);
224 setupMainDialog->refreshDataModel();
227 void MainWindow::configureSetupss()
229 if (!setupMainView) {
230 setupMainView = new SetupEditView(this);
232 activateDialog(setupMainView);
236 void MainWindow::configureEmulators()
238 if (!executableMainDialog) {
239 executableMainDialog = new ExecutableMainDialog(this);
240 connect(executableMainDialog, SIGNAL(finished(int)), this, SLOT(updateData()));
242 activateDialog(executableMainDialog);
243 executableMainDialog->refreshDataModel();
246 void MainWindow::configureTmpDir()
248 /*if (!tmpFolderDialog) {
249 tmpFolderDialog = new TmpFolderEditDialog(this, tmpDirFilePath);
251 activateDialog(tmpFolderDialog);*/
253 QString fpath = QFileDialog::getExistingDirectory(this,
254 tr("Select a directory"), tmpDirFilePath,
255 QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
257 if (d.exists() && d.isReadable()) {
258 tmpDirFilePath = fpath;
259 DbConfig::setTmpDir(tmpDirFilePath);
260 launcher->setTmpDirPath(tmpDirFilePath);
264 void MainWindow::resetDb()
266 if (QMessageBox::question(this, "Reset database?",
267 "Are you REALLY SURE you want to do this? "
268 "All the current data WILL BE LOST!",
270 QMessageBox::Yes) == QMessageBox::No) {
276 catch (EmuFrontException e) {
277 errorMessage->showMessage(e.what());
281 void MainWindow::manageDatFiles()
287 void MainWindow::activateDialog(EmuFrontDialog* dia) const
291 dia->activateWindow();
294 void MainWindow::createMenus()
296 fileMenu = menuBar()->addMenu(tr("&File"));
297 fileMenu->addAction(resetDbAction);
298 fileMenu->addSeparator();
299 fileMenu->addAction(exitAction);
301 configMenu = menuBar()->addMenu(tr("&Config"));
302 configMenu->addAction(configTmpDirAction);
303 configMenu->addSeparator();
305 configMenu->addAction(configPlatformAction);
306 configMenu->addAction(configPlatformsAction);
308 configMenu->addAction(configMediaTypeAction);
309 configMenu->addAction(configMediaTypesAction);
311 configMenu->addAction(configSetupAction);
312 configMenu->addAction(configSetupsAction);
314 configMenu->addAction(configMediaImagePathAction);
315 configMenu->addAction(configMediaImagePathsAction);
316 configMenu->addAction(configEmulatorAction);
317 configMenu->addSeparator();
318 configMenu->addAction(manageDatFilesAction);
320 helpMenu = menuBar()->addMenu(tr("&Help"));
321 helpMenu->addAction(aboutAction);
324 void MainWindow::createStatusBar()
326 messageLabel = new QLabel;
327 statusBar()->addWidget(messageLabel);
330 void MainWindow::readSettings()
334 void MainWindow::writeSettings()
338 void MainWindow::closeEvent(QCloseEvent *event)
342 else event->ignore();
345 bool MainWindow::okToContinue()
350 void MainWindow::updateData()
352 qDebug() << "MainWindow::updateData()";
353 launcher->updateData();
356 void MainWindow::about()
358 QMessageBox::about(this, aboutTitle, aboutStr);
361 bool MainWindow::testDB(bool reset)
364 if (DatabaseManager::openDB()) {
365 qDebug() << " Database opened succesfully!";
368 throw EmuFrontException("Database connection failed!");
371 int dbVer = DbCreator::dbExists();
372 if (dbVer == 0) reset = true;
373 if (!reset && dbVer != DbCreator::DB_VERSION) {
374 QString msg("Database is not compatible "
375 "with current version of EmuFront!"
376 "Do you want to continue to recreate the database?"
377 "ALL THE CURRENT DATA WILL BE LOST!!!");
378 if (QMessageBox::question(this, "Database not compatible!", msg,
379 QMessageBox::Yes, QMessageBox::No) == QMessageBox::Yes) {
382 else throw EmuFrontException("The current database is not compatible!"
383 " Cannot continue.");
391 catch (EmuFrontException e) {
392 qDebug() << e.what();
393 errorMessage->showMessage(e.what());
398 void MainWindow::createDB() const
403 dbCreator.createDB();
405 catch (QString str) {
406 QString msg(tr("Exception while trying to create"
407 " EmuFront database: %s").arg(str));
408 errorMessage->showMessage(msg);