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