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