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