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 aboutAction = new QAction(tr("&About"), this);
107 aboutAction->setStatusTip(tr("About EmuFront"));
108 connect(aboutAction, SIGNAL(triggered()), this, SLOT(about()));
111 void MainWindow::configurePlatforms()
115 platformDialog = new PlatformDialog(this);
116 connect(platformDialog, SIGNAL(finished(int)), this, SLOT(updateData()));
118 activateDialog(platformDialog);
121 void MainWindow::configureMediaTypes()
123 if (!mediaTypeDialog)
125 mediaTypeDialog = new MediaTypeDialog(this);
126 connect(mediaTypeDialog, SIGNAL(finished(int)), this, SLOT(updateData()));
128 activateDialog(mediaTypeDialog);
131 void MainWindow::configureMediaImagePaths()
133 if (!mediaImagePathDialog)
135 mediaImagePathDialog = new MediaImagePathMainDialog(this);
137 activateDialog(mediaImagePathDialog);
140 void MainWindow::configureSetups()
142 if (!setupMainDialog)
144 qDebug() << "MainWindow: Creating a setup main dialog.";
145 setupMainDialog = new SetupMainDialog(this);
147 activateDialog(setupMainDialog);
148 setupMainDialog->refreshDataModel();
151 void MainWindow::configureEmulators()
153 if (!executableMainDialog) {
154 executableMainDialog = new ExecutableMainDialog(this);
155 connect(executableMainDialog, SIGNAL(finished(int)), this, SLOT(updateData()));
157 activateDialog(executableMainDialog);
158 executableMainDialog->refreshDataModel();
161 void MainWindow::configureTmpDir()
163 /*if (!tmpFolderDialog) {
164 tmpFolderDialog = new TmpFolderEditDialog(this, tmpDirFilePath);
166 activateDialog(tmpFolderDialog);*/
168 QString fpath = QFileDialog::getExistingDirectory(this,
169 tr("Select a directory"), tmpDirFilePath,
170 QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
172 if (d.exists() && d.isReadable()) {
173 tmpDirFilePath = fpath;
174 DbConfig::setTmpDir(tmpDirFilePath);
175 launcher->setTmpDirPath(tmpDirFilePath);
179 void MainWindow::manageDatFiles()
185 void MainWindow::activateDialog(EmuFrontDialog* dia) const
189 dia->activateWindow();
192 void MainWindow::createMenus()
194 fileMenu = menuBar()->addMenu(tr("&File"));
195 fileMenu->addAction(exitAction);
197 configMenu = menuBar()->addMenu(tr("&Config"));
198 configMenu->addAction(configTmpDirAction);
199 configMenu->addSeparator();
200 configMenu->addAction(configPlatformAction);
201 configMenu->addAction(configMediaTypeAction);
202 configMenu->addAction(configSetupAction);
203 configMenu->addAction(configMediaImagePathAction);
204 configMenu->addAction(configEmulatorAction);
205 configMenu->addSeparator();
206 configMenu->addAction(manageDatFilesAction);
208 helpMenu = menuBar()->addMenu(tr("&Help"));
209 helpMenu->addAction(aboutAction);
212 void MainWindow::createStatusBar()
214 messageLabel = new QLabel;
215 statusBar()->addWidget(messageLabel);
218 void MainWindow::readSettings()
222 void MainWindow::writeSettings()
226 void MainWindow::closeEvent(QCloseEvent *event)
230 else event->ignore();
233 bool MainWindow::okToContinue()
238 void MainWindow::updateData()
240 qDebug() << "MainWindow::updateData()";
241 launcher->updateData();
244 void MainWindow::about()
246 QMessageBox::about(this, aboutTitle, aboutStr );
249 bool MainWindow::testDB(bool reset)
252 if (DatabaseManager::openDB()) {
253 qDebug() << " Database opened succesfully!";
256 throw EmuFrontException("Database connection failed!");
259 int dbVer = DbCreator::dbExists();
260 if (dbVer == 0) reset = true;
261 if (!reset && dbVer != DbCreator::DB_VERSION) {
262 QString msg("Database is not compatible "
263 "with current version of EmuFront!"
264 "Do you want to continue to recreate the database?"
265 "ALL THE CURRENT DATA WILL BE LOST!!!");
266 if (QMessageBox::question(this, "Database not compatible!", msg,
267 QMessageBox::Yes, QMessageBox::No) == QMessageBox::Yes) {
270 else throw EmuFrontException("The current database is not compatible!"
271 " Cannot continue.");
279 dbCreator.createDB();
281 catch (QString str) {
282 QString msg(tr("Exception while trying to create"
283 " EmuFront database: %s").arg(str));
284 throw EmuFrontException(msg);
289 catch (EmuFrontException e) {
290 qDebug() << e.what();
291 QMessageBox::critical(this, "Exception", e.what());