Refactoring the project, new folders for view and model classes. Added
[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 // TODO: deprecated
24 #include "platformdialog.h"
25 #include "platformeditview.h"
26 #include "mediatypedialog.h"
27 // TODO: deprecated
28 #include "mediatypeeditview.h"
29 #include "mediaimagepathmaindialog.h"
30 // TODO: deprecated
31 #include "setupmaindialog.h"
32 #include "setupeditview.h"
33 #include "executablemaindialog.h"
34 #include "datfileutil.h"
35 #include "databasemanager.h"
36 #include "dbcreator.h"
37 #include "dbconfig.h"
38
39 QString MainWindow::aboutStr = trUtf8(
40         "<h2>EmuFront</h2>"
41         "<p>&copy; 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>"
46 );
47
48 QString MainWindow::aboutTitle = tr("About EmuFront");
49
50 MainWindow::MainWindow(bool reset)
51 {
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     launcher = new EmuLauncher(errorMessage, this, tmpDirFilePath);
60     setCentralWidget(launcher);
61     createActions();
62     createMenus();
63     createStatusBar();
64     readSettings();
65     // TODO: deprecated
66     platformDialog = 0;
67     plfDialog = 0;
68     // TODO: deprecated
69     mediaTypeDialog = 0;
70     mdtDialog = 0;
71     mediaImagePathDialog = 0;
72     // TODO: deprecated
73     setupMainDialog = 0;
74     setupMainView = 0;
75     executableMainDialog = 0;
76 }
77
78 void MainWindow::connectSignals()
79 {
80 }
81
82 void MainWindow::createActions()
83 {
84     // TODO: deprecated
85     configPlatformAction = new QAction(tr("&Platforms"), this);
86     configPlatformAction->setStatusTip(tr("Configure platforms"));
87     connect(configPlatformAction, SIGNAL(triggered()),
88         this, SLOT(configurePlatforms()));
89
90     configPlatformsAction = new QAction(tr("&Set Platforms"), this);
91     configPlatformsAction->setStatusTip(tr("Add, edit and delete platforms"));
92     connect(configPlatformsAction, SIGNAL(triggered()),
93         this, SLOT(configurePlatformss()));
94
95     // TODO: deprecated
96     configMediaTypeAction = new QAction(tr("&Media Types"), this);
97     configMediaTypeAction->setStatusTip(tr("Configure media types"));
98     connect(configMediaTypeAction, SIGNAL(triggered()), this, SLOT(configureMediaTypes()));
99
100     configMediaTypesAction = new QAction(tr("&Set Media Types"), this);
101     configMediaTypeAction->setStatusTip(tr("Add, edit and delete media types"));
102     connect(configMediaTypesAction, SIGNAL(triggered()), this, SLOT(configureMediaTypess()));
103
104     configMediaImagePathAction = new QAction(tr("Media &Image Paths"), this);
105     configMediaImagePathAction->setStatusTip(tr("Configure media image file paths."));
106     connect(configMediaImagePathAction, SIGNAL(triggered()),
107         this, SLOT(configureMediaImagePaths()));
108
109     // TODO: deprecated
110     configSetupAction = new QAction(tr("S&etups"), this);
111     configSetupAction->setStatusTip(tr("Configure set ups"));
112     connect(configSetupAction, SIGNAL(triggered()), this, SLOT(configureSetups()));
113
114     configSetupsAction = new QAction(tr("Configure S&etups"), this);
115     configSetupsAction->setStatusTip(tr("Add, edit and delete setups"));
116     connect(configSetupsAction, SIGNAL(triggered()), this, SLOT(configureSetupss()));
117
118     configEmulatorAction = new QAction(tr("Em&ulators"), this);
119     configEmulatorAction->setStatusTip(tr("Configure emulators"));
120     connect(configEmulatorAction, SIGNAL(triggered()), this, SLOT(configureEmulators()));
121
122     configTmpDirAction = new QAction(tr("&Temp dir"), this);
123     configTmpDirAction->setStatusTip(tr("Configure directory for temporary files."));
124     connect(configTmpDirAction, SIGNAL(triggered()), this, SLOT(configureTmpDir()));
125
126     manageDatFilesAction = new QAction(tr("&Manage dats"), this);
127     manageDatFilesAction->setStatusTip(tr("Read dat files to database."));
128     connect(manageDatFilesAction, SIGNAL(triggered()), this, SLOT(manageDatFiles()));
129
130     exitAction = new QAction(tr("&Exit"), this);
131     exitAction->setShortcut(tr("Ctrl+Q"));
132     exitAction->setStatusTip(tr("Exit EmuFront"));
133     connect(exitAction, SIGNAL(triggered()), this, SLOT(close()));
134
135     resetDbAction = new QAction( tr("Reset database"), this);
136     resetDbAction->setStatusTip(tr("Deletes all the current data and create a new database."));
137     connect(resetDbAction, SIGNAL(triggered()), this, SLOT(resetDb()));
138
139     aboutAction = new QAction(tr("&About"), this);
140     aboutAction->setStatusTip(tr("About EmuFront"));
141     connect(aboutAction, SIGNAL(triggered()), this, SLOT(about()));
142 }
143
144 // TODO: deprecated
145 void MainWindow::configurePlatforms()
146 {
147    if (!platformDialog)
148    {
149        platformDialog = new PlatformDialog(this);
150        connect(platformDialog, SIGNAL(finished(int)), this, SLOT(updateData()));
151    }
152    activateDialog(platformDialog);
153 }
154
155 void MainWindow::configurePlatformss()
156 {
157     if (!plfDialog) {
158         plfDialog = new PlatformEditView(this);
159         connect(plfDialog, SIGNAL(finished(int)), this, SLOT(updateData()));
160     }
161     activateDialog(plfDialog);
162 }
163
164 // TODO: deprecated
165 void MainWindow::configureMediaTypes()
166 {
167     if (!mediaTypeDialog)
168     {
169         mediaTypeDialog = new MediaTypeDialog(this);
170         connect(mediaTypeDialog, SIGNAL(finished(int)), this, SLOT(updateData()));
171    }
172    activateDialog(mediaTypeDialog);
173 }
174
175 void MainWindow::configureMediaTypess()
176 {
177     if (!mdtDialog)
178     {
179         mdtDialog = new MediaTypeEditView(this);
180         connect(mdtDialog, SIGNAL(finished(int)), this, SLOT(updateData()));
181    }
182    activateDialog(mdtDialog);
183 }
184
185
186 void MainWindow::configureMediaImagePaths()
187 {
188     if (!mediaImagePathDialog)
189     {
190         mediaImagePathDialog = new MediaImagePathMainDialog(this);
191     }
192     activateDialog(mediaImagePathDialog);
193 }
194
195 // TODO: deprecated
196 void MainWindow::configureSetups()
197 {
198     if (!setupMainDialog)
199     {
200         setupMainDialog = new SetupMainDialog(this);
201     }
202     activateDialog(setupMainDialog);
203     setupMainDialog->refreshDataModel();
204 }
205
206 void MainWindow::configureSetupss()
207 {
208     if (!setupMainView) {
209         setupMainView = new SetupEditView(this);
210     }
211     activateDialog(setupMainView);
212 }
213
214
215 void MainWindow::configureEmulators()
216 {
217     if (!executableMainDialog) {
218         executableMainDialog = new ExecutableMainDialog(this);
219         connect(executableMainDialog, SIGNAL(finished(int)), this, SLOT(updateData()));
220     }
221     activateDialog(executableMainDialog);
222     executableMainDialog->refreshDataModel();
223 }
224
225 void MainWindow::configureTmpDir()
226 {
227     /*if (!tmpFolderDialog) {
228         tmpFolderDialog = new TmpFolderEditDialog(this, tmpDirFilePath);
229     }
230     activateDialog(tmpFolderDialog);*/
231
232     QString fpath = QFileDialog::getExistingDirectory(this,
233         tr("Select a directory"), tmpDirFilePath,
234         QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
235     QDir d(fpath);
236     if (d.exists() && d.isReadable()) {
237         tmpDirFilePath = fpath;
238         DbConfig::setTmpDir(tmpDirFilePath);
239         launcher->setTmpDirPath(tmpDirFilePath);
240     }
241 }
242
243 void MainWindow::resetDb()
244 {
245     if (QMessageBox::question(this, "Reset database?",
246         "Are you REALLY SURE you want to do this? "
247         "All the current data WILL BE LOST!",
248         QMessageBox::No,
249         QMessageBox::Yes) == QMessageBox::No) {
250         return;
251     }
252     try {
253         createDB();
254     }
255     catch (EmuFrontException e) {
256         errorMessage->showMessage(e.what());
257     }
258 }
259
260 void MainWindow::manageDatFiles()
261 {
262     DatFileUtil dfu;
263     dfu.open();
264 }
265
266 void MainWindow::activateDialog(EmuFrontDialog* dia) const
267 {
268     dia->show();
269     dia->raise();
270     dia->activateWindow();
271 }
272
273 void MainWindow::createMenus()
274 {
275     fileMenu = menuBar()->addMenu(tr("&File"));
276     fileMenu->addAction(resetDbAction);
277     fileMenu->addSeparator();
278     fileMenu->addAction(exitAction);
279
280     configMenu = menuBar()->addMenu(tr("&Config"));
281     configMenu->addAction(configTmpDirAction);
282     configMenu->addSeparator();
283     // TODO: deprecated
284     configMenu->addAction(configPlatformAction);
285     configMenu->addAction(configPlatformsAction);
286     // TODO: deprecated
287     configMenu->addAction(configMediaTypeAction);
288     configMenu->addAction(configMediaTypesAction);
289     // TODO: deprecated
290     configMenu->addAction(configSetupAction);
291     configMenu->addAction(configSetupsAction);
292     configMenu->addAction(configMediaImagePathAction);
293     configMenu->addAction(configEmulatorAction);
294     configMenu->addSeparator();
295     configMenu->addAction(manageDatFilesAction);
296
297     helpMenu = menuBar()->addMenu(tr("&Help"));
298     helpMenu->addAction(aboutAction);
299 }
300
301 void MainWindow::createStatusBar()
302 {
303     messageLabel = new QLabel;
304     statusBar()->addWidget(messageLabel);
305 }
306
307 void MainWindow::readSettings()
308 {
309 }
310
311 void MainWindow::writeSettings()
312 {
313 }
314
315 void MainWindow::closeEvent(QCloseEvent *event)
316 {
317     if (okToContinue())
318         event->accept();
319     else event->ignore();
320 }
321
322 bool MainWindow::okToContinue()
323 {
324     return true;
325 }
326
327 void MainWindow::updateData()
328 {
329     qDebug() << "MainWindow::updateData()";
330     launcher->updateData();
331 }
332
333 void MainWindow::about()
334 {
335     QMessageBox::about(this, aboutTitle, aboutStr);
336 }
337
338 bool MainWindow::testDB(bool reset)
339 {
340     try {
341         if (DatabaseManager::openDB()) {
342             qDebug() << " Database opened succesfully!";
343         }
344         else {
345             throw EmuFrontException("Database connection failed!");
346         }
347
348         int dbVer = DbCreator::dbExists();
349         if (dbVer == 0) reset = true;
350         if (!reset && dbVer != DbCreator::DB_VERSION) {
351             QString msg("Database is not compatible "
352                         "with current version of EmuFront!"
353                         "Do you want to continue to recreate the database?"
354                         "ALL THE CURRENT DATA WILL BE LOST!!!");
355             if (QMessageBox::question(this, "Database not compatible!", msg,
356                                       QMessageBox::Yes, QMessageBox::No) == QMessageBox::Yes) {
357                 reset = true;
358             }
359             else throw EmuFrontException("The current database is not compatible!"
360                                          " Cannot continue.");
361         }
362
363         if (reset) {
364             createDB();
365         }
366         return true;
367     }
368     catch (EmuFrontException e) {
369         qDebug() << e.what();
370         errorMessage->showMessage(e.what());
371         return false;
372     }
373 }
374
375 void MainWindow::createDB() const
376 {
377     try
378     {
379         DbCreator dbCreator;
380         dbCreator.createDB();
381     }
382     catch (QString str) {
383         QString msg(tr("Exception while trying to create"
384                        " EmuFront database: %s").arg(str));
385         errorMessage->showMessage(msg);
386     }
387 }
388