Started implementing better MVC support: thanks Petro for the idea! ;)
[emufront] / src / mainwindow.cpp
1 // EmuFront
2 // Copyright 2010 Mikko Keinänen
3 //
4 // This file is part of EmuFront.
5 //
6 //
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.
11 //
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.
16 //
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/>.
19
20 #include <QtGui>
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"
33
34 QString MainWindow::aboutStr = trUtf8(
35         "<h2>EmuFront</h2>"
36         "<p>&copy; 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>"
41 );
42
43 QString MainWindow::aboutTitle = tr("About EmuFront");
44
45 MainWindow::MainWindow(bool reset)
46 {
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);
56     createActions();
57     createMenus();
58     createStatusBar();
59     readSettings();
60     platformDialog = 0;
61     mediaTypeDialog = 0;
62     mediaImagePathDialog = 0;
63     setupMainDialog = 0;
64     executableMainDialog = 0;
65 }
66
67 void MainWindow::connectSignals()
68 {
69 }
70
71 void MainWindow::createActions()
72 {
73     configPlatformAction = new QAction(tr("&Platforms"), this);
74     configPlatformAction->setStatusTip(tr("Configure platforms"));
75     connect(configPlatformAction, SIGNAL(triggered()),
76         this, SLOT(configurePlatforms()));
77
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()));
82
83     configMediaTypeAction = new QAction(tr("&Media Types"), this);
84     configMediaTypeAction->setStatusTip(tr("Configure media types"));
85     connect(configMediaTypeAction, SIGNAL(triggered()), this, SLOT(configureMediaTypes()));
86
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()));
91
92     configSetupAction = new QAction(tr("S&etups"), this);
93     configSetupAction->setStatusTip(tr("Configure set ups"));
94     connect(configSetupAction, SIGNAL(triggered()), this, SLOT(configureSetups()));
95
96     configEmulatorAction = new QAction(tr("Em&ulators"), this);
97     configEmulatorAction->setStatusTip(tr("Configure emulators"));
98     connect(configEmulatorAction, SIGNAL(triggered()), this, SLOT(configureEmulators()));
99
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()));
103
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()));
107
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()));
112
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()));
116
117     aboutAction = new QAction(tr("&About"), this);
118     aboutAction->setStatusTip(tr("About EmuFront"));
119     connect(aboutAction, SIGNAL(triggered()), this, SLOT(about()));
120 }
121
122 void MainWindow::configurePlatforms()
123 {
124    if (!platformDialog)
125    {
126        platformDialog = new PlatformDialog(this);
127        connect(platformDialog, SIGNAL(finished(int)), this, SLOT(updateData()));
128    }
129    activateDialog(platformDialog);
130 }
131
132 void MainWindow::configurePlatformss()
133 {
134     plfDialog = new PlatformMainDialog(this);
135     connect(plfDialog, SIGNAL(finished(int)), this, SLOT(updateData()));
136     activateDialog(plfDialog);
137 }
138
139 void MainWindow::configureMediaTypes()
140 {
141     if (!mediaTypeDialog)
142     {
143         mediaTypeDialog = new MediaTypeDialog(this);
144         connect(mediaTypeDialog, SIGNAL(finished(int)), this, SLOT(updateData()));
145    }
146    activateDialog(mediaTypeDialog);
147 }
148
149 void MainWindow::configureMediaImagePaths()
150 {
151     if (!mediaImagePathDialog)
152     {
153         mediaImagePathDialog = new MediaImagePathMainDialog(this);
154     }
155     activateDialog(mediaImagePathDialog);
156 }
157
158 void MainWindow::configureSetups()
159 {
160     if (!setupMainDialog)
161     {
162         qDebug() << "MainWindow: Creating a setup main dialog.";
163         setupMainDialog = new SetupMainDialog(this);
164     }
165     activateDialog(setupMainDialog);
166     setupMainDialog->refreshDataModel();
167 }
168
169 void MainWindow::configureEmulators()
170 {
171     if (!executableMainDialog) {
172         executableMainDialog = new ExecutableMainDialog(this);
173         connect(executableMainDialog, SIGNAL(finished(int)), this, SLOT(updateData()));
174     }
175     activateDialog(executableMainDialog);
176     executableMainDialog->refreshDataModel();
177 }
178
179 void MainWindow::configureTmpDir()
180 {
181     /*if (!tmpFolderDialog) {
182         tmpFolderDialog = new TmpFolderEditDialog(this, tmpDirFilePath);
183     }
184     activateDialog(tmpFolderDialog);*/
185
186     QString fpath = QFileDialog::getExistingDirectory(this,
187         tr("Select a directory"), tmpDirFilePath,
188         QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
189     QDir d(fpath);
190     if (d.exists() && d.isReadable()) {
191         tmpDirFilePath = fpath;
192         DbConfig::setTmpDir(tmpDirFilePath);
193         launcher->setTmpDirPath(tmpDirFilePath);
194     }
195 }
196
197 void MainWindow::resetDb()
198 {
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!",
202         QMessageBox::No,
203         QMessageBox::Yes) == QMessageBox::No) {
204         return;
205     }
206     try {
207         createDB();
208     }
209     catch (EmuFrontException e) {
210         errorMessage->showMessage(e.what());
211     }
212 }
213
214 void MainWindow::manageDatFiles()
215 {
216     DatFileUtil dfu;
217     dfu.open();
218 }
219
220 void MainWindow::activateDialog(EmuFrontDialog* dia) const
221 {
222     dia->show();
223     dia->raise();
224     dia->activateWindow();
225 }
226
227 void MainWindow::createMenus()
228 {
229     fileMenu = menuBar()->addMenu(tr("&File"));
230     fileMenu->addAction(resetDbAction);
231     fileMenu->addSeparator();
232     fileMenu->addAction(exitAction);
233
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);
245
246     helpMenu = menuBar()->addMenu(tr("&Help"));
247     helpMenu->addAction(aboutAction);
248 }
249
250 void MainWindow::createStatusBar()
251 {
252     messageLabel = new QLabel;
253     statusBar()->addWidget(messageLabel);
254 }
255
256 void MainWindow::readSettings()
257 {
258 }
259
260 void MainWindow::writeSettings()
261 {
262 }
263
264 void MainWindow::closeEvent(QCloseEvent *event)
265 {
266     if (okToContinue())
267         event->accept();
268     else event->ignore();
269 }
270
271 bool MainWindow::okToContinue()
272 {
273     return true;
274 }
275
276 void MainWindow::updateData()
277 {
278     qDebug() << "MainWindow::updateData()";
279     launcher->updateData();
280 }
281
282 void MainWindow::about()
283 {
284     QMessageBox::about(this, aboutTitle, aboutStr);
285 }
286
287 bool MainWindow::testDB(bool reset)
288 {
289     try {
290         if (DatabaseManager::openDB()) {
291             qDebug() << " Database opened succesfully!";
292         }
293         else {
294             throw EmuFrontException("Database connection failed!");
295         }
296
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) {
306                 reset = true;
307             }
308             else throw EmuFrontException("The current database is not compatible!"
309                                          " Cannot continue.");
310         }
311
312         if (reset) {
313             createDB();
314         }
315         return true;
316     }
317     catch (EmuFrontException e) {
318         qDebug() << e.what();
319         errorMessage->showMessage(e.what());
320         return false;
321     }
322 }
323
324 void MainWindow::createDB() const
325 {
326     try
327     {
328         DbCreator dbCreator;
329         dbCreator.createDB();
330     }
331     catch (QString str) {
332         QString msg(tr("Exception while trying to create"
333                        " EmuFront database: %s").arg(str));
334         errorMessage->showMessage(msg);
335     }
336 }
337