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/>.
23 #include "mainwindow.h"
24 #include "emulauncher.h"
25 #include "platformeditview.h"
26 #include "mediatypeeditview.h"
27 #include "filepatheditview.h"
28 #include "setupeditview.h"
29 #include "externalexecutableeditview.h"
30 #include "externalexecutablemodel.h"
31 #include "datfileutil.h"
32 #include "databasemanager.h"
33 #include "dbcreator.h"
35 #include "setupmodel.h"
36 #include "platformmodel.h"
37 #include "mediatypemodel.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 supModel = new SetupModel(this);
60 plfModel = new PlatformModel(this);
61 mdtModel = new MediaTypeModel(this);
62 emuModel = new ExternalExecutableModel(this);
63 launcher = new EmuLauncher(errorMessage, supModel, emuModel, this, tmpDirFilePath);
64 setCentralWidget(launcher);
71 mediaImagePathView = 0;
76 void MainWindow::connectSignals()
80 void MainWindow::createActions()
82 configPlatformsAction = new QAction(tr("&Set Platforms"), this);
83 configPlatformsAction->setStatusTip(tr("Add, edit and delete platforms"));
84 connect(configPlatformsAction, SIGNAL(triggered()),
85 this, SLOT(configurePlatforms()));
87 configMediaTypesAction = new QAction(tr("&Set Media Types"), this);
88 configMediaTypesAction->setStatusTip(tr("Add, edit and delete media types"));
89 connect(configMediaTypesAction, SIGNAL(triggered()), this, SLOT(configureMediaTypes()));
91 configMediaImagePathsAction = new QAction(tr("Set media &image paths"), this);
92 configMediaImagePathsAction->setStatusTip(tr("Add, edit and delete media image file paths."));
93 connect(configMediaImagePathsAction, SIGNAL(triggered()),
94 this, SLOT(configureMediaImagePaths()));
96 configSetupsAction = new QAction(tr("Configure S&etups"), this);
97 configSetupsAction->setStatusTip(tr("Add, edit and delete setups"));
98 connect(configSetupsAction, SIGNAL(triggered()), this, SLOT(configureSetups()));
100 configEmulatorsAction = new QAction(tr("Configure Em&ulators"), this);
101 configEmulatorsAction->setStatusTip(tr("Add, edit and delete emulator configurations"));
102 connect(configEmulatorsAction, SIGNAL(triggered()), this, SLOT(configureEmulators()));
104 configTmpDirAction = new QAction(tr("&Temp dir"), this);
105 configTmpDirAction->setStatusTip(tr("Configure directory for temporary files."));
106 connect(configTmpDirAction, SIGNAL(triggered()), this, SLOT(configureTmpDir()));
108 manageDatFilesAction = new QAction(tr("&Manage dats"), this);
109 manageDatFilesAction->setStatusTip(tr("Read dat files to database."));
110 connect(manageDatFilesAction, SIGNAL(triggered()), this, SLOT(manageDatFiles()));
112 exitAction = new QAction(tr("&Exit"), this);
113 exitAction->setShortcut(tr("Ctrl+Q"));
114 exitAction->setStatusTip(tr("Exit EmuFront"));
115 connect(exitAction, SIGNAL(triggered()), this, SLOT(close()));
117 resetDbAction = new QAction( tr("Reset database"), this);
118 resetDbAction->setStatusTip(tr("Deletes all the current data and create a new database."));
119 connect(resetDbAction, SIGNAL(triggered()), this, SLOT(resetDb()));
121 aboutAction = new QAction(tr("&About"), this);
122 aboutAction->setStatusTip(tr("About EmuFront"));
123 connect(aboutAction, SIGNAL(triggered()), this, SLOT(about()));
126 void MainWindow::configurePlatforms()
129 plfDialog = new PlatformEditView(plfModel, this);
130 connect(plfDialog, SIGNAL(finished(int)), this, SLOT(updateData()));
132 activateDialog(plfDialog);
135 void MainWindow::configureMediaTypes()
139 mdtDialog = new MediaTypeEditView(mdtModel, this);
140 connect(mdtDialog, SIGNAL(finished(int)), this, SLOT(updateData()));
142 activateDialog(mdtDialog);
145 void MainWindow::configureMediaImagePaths()
147 if (!mediaImagePathView)
149 mediaImagePathView = new FilePathEditView(supModel, this);
151 activateDialog(mediaImagePathView);
154 void MainWindow::configureSetups()
156 if (!setupMainView) {
157 setupMainView = new SetupEditView(plfModel, mdtModel, supModel, this);
159 activateDialog(setupMainView);
162 void MainWindow::configureEmulators()
164 if (!emulatorEditView) {
165 emulatorEditView = new ExternalExecutableEditView(supModel, emuModel, this);
167 activateDialog(emulatorEditView);
170 void MainWindow::configureTmpDir()
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 if (DbConfig::setTmpDir(tmpDirFilePath)) {
179 QMessageBox::information(
181 tr("Temp dir updated"),
182 tr("The directory for temporary files "
183 "has been updated to '%1'.")
186 launcher->setTmpDirPath(tmpDirFilePath);
191 void MainWindow::resetDb()
193 if (QMessageBox::question(this, "Reset database?",
194 "Are you REALLY SURE you want to do this? "
195 "All the current data WILL BE LOST!",
197 QMessageBox::Yes) == QMessageBox::No) {
203 catch (EmuFrontException e) {
204 errorMessage->showMessage(e.what());
208 void MainWindow::manageDatFiles()
214 void MainWindow::activateDialog(EmuFrontDialog* dia) const
218 dia->activateWindow();
221 void MainWindow::createMenus()
223 fileMenu = menuBar()->addMenu(tr("&File"));
224 fileMenu->addAction(resetDbAction);
225 fileMenu->addSeparator();
226 fileMenu->addAction(exitAction);
228 configMenu = menuBar()->addMenu(tr("&Config"));
229 configMenu->addAction(configTmpDirAction);
230 configMenu->addSeparator();
231 configMenu->addAction(configPlatformsAction);
232 configMenu->addAction(configMediaTypesAction);
233 configMenu->addAction(configSetupsAction);
234 configMenu->addAction(configMediaImagePathsAction);
235 configMenu->addAction(configEmulatorsAction);
236 configMenu->addSeparator();
237 configMenu->addAction(manageDatFilesAction);
239 helpMenu = menuBar()->addMenu(tr("&Help"));
240 helpMenu->addAction(aboutAction);
243 void MainWindow::createStatusBar()
245 messageLabel = new QLabel;
246 statusBar()->addWidget(messageLabel);
249 void MainWindow::readSettings()
253 void MainWindow::writeSettings()
257 void MainWindow::closeEvent(QCloseEvent *event)
261 else event->ignore();
264 bool MainWindow::okToContinue()
269 void MainWindow::updateData()
271 qDebug() << "MainWindow::updateData()";
272 launcher->updateData();
275 void MainWindow::about()
277 QMessageBox::about(this, aboutTitle, aboutStr);
280 bool MainWindow::testDB(bool reset)
283 if (DatabaseManager::openDB()) {
284 qDebug() << " Database opened succesfully!";
287 throw EmuFrontException("Database connection failed!");
290 int dbVer = DbCreator::dbExists();
291 if (dbVer == 0) reset = true;
292 if (!reset && dbVer != DbCreator::DB_VERSION) {
293 QString msg("Database is not compatible "
294 "with current version of EmuFront!"
295 "Do you want to continue to recreate the database?"
296 "ALL THE CURRENT DATA WILL BE LOST!!!");
297 if (QMessageBox::question(this, "Database not compatible!", msg,
298 QMessageBox::Yes, QMessageBox::No) == QMessageBox::Yes) {
301 else throw EmuFrontException("The current database is not compatible!"
302 " Cannot continue.");
310 catch (EmuFrontException e) {
311 qDebug() << e.what();
312 errorMessage->showMessage(e.what());
317 void MainWindow::createDB() const
322 dbCreator.createDB();
324 catch (QString str) {
325 QString msg(tr("Exception while trying to create"
326 " EmuFront database: %s").arg(str));
327 errorMessage->showMessage(msg);