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