Renamed some private slots.
[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
22 #include <QtGui>
23 #include "mainwindow.h"
24 #include "emulauncher.h"
25 #include "platformeditview.h"
26 #include "mediatypeeditview.h"
27 #include "filepatheditview.h"
28 #include "setupeditview.h"
29 #include "externalexecutableeditview.h"
30 #include "externalexecutablemodel.h"
31 #include "datfileutil.h"
32 #include "databasemanager.h"
33 #include "dbcreator.h"
34 #include "dbconfig.h"
35 #include "setupmodel.h"
36 #include "platformmodel.h"
37 #include "mediatypemodel.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     supModel = new SetupModel(this);
60     plfModel = new PlatformModel(this);
61     mdtModel = new MediaTypeModel(this);
62         emuModel = new ExternalExecutableModel(this);
63     launcher = new EmuLauncher(errorMessage, supModel, emuModel, this, tmpDirFilePath);
64     setCentralWidget(launcher);
65     createActions();
66     createMenus();
67     createStatusBar();
68     readSettings();
69     plfDialog = 0;
70     mdtDialog = 0;
71     mediaImagePathView = 0;
72     setupMainView = 0;
73     emulatorEditView = 0;
74 }
75
76 void MainWindow::connectSignals()
77 {
78 }
79
80 void MainWindow::createActions()
81 {
82     configPlatformsAction = new QAction(tr("&Set Platforms"), this);
83     configPlatformsAction->setStatusTip(tr("Add, edit and delete platforms"));
84     connect(configPlatformsAction, SIGNAL(triggered()),
85         this, SLOT(configurePlatforms()));
86
87     configMediaTypesAction = new QAction(tr("&Set Media Types"), this);
88     configMediaTypesAction->setStatusTip(tr("Add, edit and delete media types"));
89     connect(configMediaTypesAction, SIGNAL(triggered()), this, SLOT(configureMediaTypes()));
90
91     configMediaImagePathsAction = new QAction(tr("Set media &image paths"), this);
92     configMediaImagePathsAction->setStatusTip(tr("Add, edit and delete media image file paths."));
93     connect(configMediaImagePathsAction, SIGNAL(triggered()),
94         this, SLOT(configureMediaImagePaths()));
95
96     configSetupsAction = new QAction(tr("Configure S&etups"), this);
97     configSetupsAction->setStatusTip(tr("Add, edit and delete setups"));
98     connect(configSetupsAction, SIGNAL(triggered()), this, SLOT(configureSetups()));
99
100     configEmulatorsAction = new QAction(tr("Configure Em&ulators"), this);
101     configEmulatorsAction->setStatusTip(tr("Add, edit and delete emulator configurations"));
102     connect(configEmulatorsAction, SIGNAL(triggered()), this, SLOT(configureEmulators()));
103
104     configTmpDirAction = new QAction(tr("&Temp dir"), this);
105     configTmpDirAction->setStatusTip(tr("Configure directory for temporary files."));
106     connect(configTmpDirAction, SIGNAL(triggered()), this, SLOT(configureTmpDir()));
107
108     manageDatFilesAction = new QAction(tr("&Manage dats"), this);
109     manageDatFilesAction->setStatusTip(tr("Read dat files to database."));
110     connect(manageDatFilesAction, SIGNAL(triggered()), this, SLOT(manageDatFiles()));
111
112     exitAction = new QAction(tr("&Exit"), this);
113     exitAction->setShortcut(tr("Ctrl+Q"));
114     exitAction->setStatusTip(tr("Exit EmuFront"));
115     connect(exitAction, SIGNAL(triggered()), this, SLOT(close()));
116
117     resetDbAction = new QAction( tr("Reset database"), this);
118     resetDbAction->setStatusTip(tr("Deletes all the current data and create a new database."));
119     connect(resetDbAction, SIGNAL(triggered()), this, SLOT(resetDb()));
120
121     aboutAction = new QAction(tr("&About"), this);
122     aboutAction->setStatusTip(tr("About EmuFront"));
123     connect(aboutAction, SIGNAL(triggered()), this, SLOT(about()));
124 }
125
126 void MainWindow::configurePlatforms()
127 {
128     if (!plfDialog) {
129         plfDialog = new PlatformEditView(plfModel, this);
130         connect(plfDialog, SIGNAL(finished(int)), this, SLOT(updateData()));
131     }
132     activateDialog(plfDialog);
133 }
134
135 void MainWindow::configureMediaTypes()
136 {
137     if (!mdtDialog)
138     {
139         mdtDialog = new MediaTypeEditView(mdtModel, this);
140         connect(mdtDialog, SIGNAL(finished(int)), this, SLOT(updateData()));
141    }
142    activateDialog(mdtDialog);
143 }
144
145 void MainWindow::configureMediaImagePaths()
146 {
147     if (!mediaImagePathView)
148     {
149         mediaImagePathView = new FilePathEditView(supModel, this);
150     }
151     activateDialog(mediaImagePathView);
152 }
153
154 void MainWindow::configureSetups()
155 {
156     if (!setupMainView) {
157         setupMainView = new SetupEditView(plfModel, mdtModel, supModel, this);
158     }
159     activateDialog(setupMainView);
160 }
161
162 void MainWindow::configureEmulators()
163 {
164     if (!emulatorEditView) {
165         emulatorEditView = new ExternalExecutableEditView(supModel, emuModel, this);
166     }
167     activateDialog(emulatorEditView);
168 }
169
170 void MainWindow::configureTmpDir()
171 {
172     QString fpath = QFileDialog::getExistingDirectory(this,
173         tr("Select a directory"), tmpDirFilePath,
174         QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
175     QDir d(fpath);
176     if (d.exists() && d.isReadable()) {
177         tmpDirFilePath = fpath;
178         if (DbConfig::setTmpDir(tmpDirFilePath)) {
179             QMessageBox::information(
180                 this,
181                 tr("Temp dir updated"),
182                 tr("The directory for temporary files "
183                     "has been updated to '%1'.")
184                     .arg(tmpDirFilePath)
185             );
186             launcher->setTmpDirPath(tmpDirFilePath);
187         }
188     }
189 }
190
191 void MainWindow::resetDb()
192 {
193     if (QMessageBox::question(this, "Reset database?",
194         "Are you REALLY SURE you want to do this? "
195         "All the current data WILL BE LOST!",
196         QMessageBox::No,
197         QMessageBox::Yes) == QMessageBox::No) {
198         return;
199     }
200     try {
201         createDB();
202     }
203     catch (EmuFrontException e) {
204         errorMessage->showMessage(e.what());
205     }
206 }
207
208 void MainWindow::manageDatFiles()
209 {
210     DatFileUtil dfu;
211     dfu.open();
212 }
213
214 void MainWindow::activateDialog(EmuFrontDialog* dia) const
215 {
216     dia->show();
217     dia->raise();
218     dia->activateWindow();
219 }
220
221 void MainWindow::createMenus()
222 {
223     fileMenu = menuBar()->addMenu(tr("&File"));
224     fileMenu->addAction(resetDbAction);
225     fileMenu->addSeparator();
226     fileMenu->addAction(exitAction);
227
228     configMenu = menuBar()->addMenu(tr("&Config"));
229     configMenu->addAction(configTmpDirAction);
230     configMenu->addSeparator();
231     configMenu->addAction(configPlatformsAction);
232     configMenu->addAction(configMediaTypesAction);
233     configMenu->addAction(configSetupsAction);
234     configMenu->addAction(configMediaImagePathsAction);
235     configMenu->addAction(configEmulatorsAction);
236     configMenu->addSeparator();
237     configMenu->addAction(manageDatFilesAction);
238
239     helpMenu = menuBar()->addMenu(tr("&Help"));
240     helpMenu->addAction(aboutAction);
241 }
242
243 void MainWindow::createStatusBar()
244 {
245     messageLabel = new QLabel;
246     statusBar()->addWidget(messageLabel);
247 }
248
249 void MainWindow::readSettings()
250 {
251 }
252
253 void MainWindow::writeSettings()
254 {
255 }
256
257 void MainWindow::closeEvent(QCloseEvent *event)
258 {
259     if (okToContinue())
260         event->accept();
261     else event->ignore();
262 }
263
264 bool MainWindow::okToContinue()
265 {
266     return true;
267 }
268
269 void MainWindow::updateData()
270 {
271     qDebug() << "MainWindow::updateData()";
272     launcher->updateData();
273 }
274
275 void MainWindow::about()
276 {
277     QMessageBox::about(this, aboutTitle, aboutStr);
278 }
279
280 bool MainWindow::testDB(bool reset)
281 {
282     try {
283         if (DatabaseManager::openDB()) {
284             qDebug() << " Database opened succesfully!";
285         }
286         else {
287             throw EmuFrontException("Database connection failed!");
288         }
289
290         int dbVer = DbCreator::dbExists();
291         if (dbVer == 0) reset = true;
292         if (!reset && dbVer != DbCreator::DB_VERSION) {
293             QString msg("Database is not compatible "
294                         "with current version of EmuFront!"
295                         "Do you want to continue to recreate the database?"
296                         "ALL THE CURRENT DATA WILL BE LOST!!!");
297             if (QMessageBox::question(this, "Database not compatible!", msg,
298                                       QMessageBox::Yes, QMessageBox::No) == QMessageBox::Yes) {
299                 reset = true;
300             }
301             else throw EmuFrontException("The current database is not compatible!"
302                                          " Cannot continue.");
303         }
304     
305         if (reset) {
306             createDB();
307         }
308         return true;
309     }
310     catch (EmuFrontException e) {
311         qDebug() << e.what();
312         errorMessage->showMessage(e.what());
313         return false;
314     }
315 }
316
317 void MainWindow::createDB() const
318 {
319     try
320     {
321         DbCreator dbCreator;
322         dbCreator.createDB();
323     }
324     catch (QString str) {
325         QString msg(tr("Exception while trying to create"
326                        " EmuFront database: %s").arg(str));
327         errorMessage->showMessage(msg);
328     }
329 }
330