Added configuration of temporary directory.
[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 "dialogs/tmpfoldereditdialog.h"
29 #include "db/databasemanager.h"
30 #include "db/dbconfig.h"
31
32 MainWindow::MainWindow()
33 {
34     setWindowTitle("EmuFront");
35     tmpDirFilePath = DbConfig::getTmpDir();
36     if (tmpDirFilePath.isEmpty())
37         tmpDirFilePath = QDir::tempPath();
38     launcher = new EmuLauncher(this, tmpDirFilePath);
39     setCentralWidget(launcher);
40     createActions();
41     createMenus();
42     createStatusBar();
43     readSettings();
44     platformDialog = 0;
45     mediaTypeDialog = 0;
46     mediaImagePathDialog = 0;
47     setupMainDialog = 0;
48     executableMainDialog = 0;
49 }
50
51 void MainWindow::connectSignals()
52 {
53 }
54
55 void MainWindow::createActions()
56 {
57     configPlatformAction = new QAction(tr("&Platforms"), this);
58     configPlatformAction->setStatusTip(tr("Configure platforms"));
59     connect(configPlatformAction, SIGNAL(triggered()),
60             this, SLOT(configurePlatforms()));
61
62     configMediaTypeAction = new QAction(tr("&Media Types"), this);
63     configMediaTypeAction->setStatusTip(tr("Configure media types"));
64     connect(configMediaTypeAction, SIGNAL(triggered()), this, SLOT(configureMediaTypes()));
65
66     configMediaImagePathAction = new QAction(tr("Media &Image Paths"), this);
67     configMediaImagePathAction->setStatusTip(tr("Configure media image file paths."));
68     connect(configMediaImagePathAction, SIGNAL(triggered()),
69         this, SLOT(configureMediaImagePaths()));
70
71     configSetupAction = new QAction(tr("S&etups"), this);
72     configSetupAction->setStatusTip(tr("Configure set ups"));
73     connect(configSetupAction, SIGNAL(triggered()), this, SLOT(configureSetups()));
74
75     configEmulatorAction = new QAction(tr("Em&ulators"), this);
76     configEmulatorAction->setStatusTip(tr("Configure emulators"));
77     connect(configEmulatorAction, SIGNAL(triggered()), this, SLOT(configureEmulators()));
78
79     configTmpDirAction = new QAction(tr("&Temp dir"), this);
80     configTmpDirAction->setStatusTip(tr("Configure directory for temporary files."));
81     connect(configTmpDirAction, SIGNAL(triggered()), this, SLOT(configureTmpDir()));
82
83     exitAction = new QAction(tr("&Exit"), this);
84     exitAction->setShortcut(tr("Ctrl+Q"));
85     exitAction->setStatusTip(tr("Exit EmuFront"));
86     connect(exitAction, SIGNAL(triggered()), this, SLOT(close()));
87
88     aboutAction = new QAction(tr("&About"), this);
89     aboutAction->setStatusTip(tr("About EmuFront"));
90     connect(aboutAction, SIGNAL(triggered()), this, SLOT(about()));
91 }
92
93 void MainWindow::configurePlatforms()
94 {
95    if (!platformDialog)
96    {
97        platformDialog = new PlatformDialog(this);
98        connect(platformDialog, SIGNAL(finished(int)), this, SLOT(updateData()));
99    }
100    activateDialog(platformDialog);
101 }
102
103 void MainWindow::configureMediaTypes()
104 {
105     if (!mediaTypeDialog)
106     {
107         mediaTypeDialog = new MediaTypeDialog(this);
108         connect(mediaTypeDialog, SIGNAL(finished(int)), this, SLOT(updateData()));
109    }
110    activateDialog(mediaTypeDialog);
111 }
112
113 void MainWindow::configureMediaImagePaths()
114 {
115     if (!mediaImagePathDialog)
116     {
117         mediaImagePathDialog = new MediaImagePathMainDialog(this);
118     }
119     activateDialog(mediaImagePathDialog);
120 }
121
122 void MainWindow::configureSetups()
123 {
124     if (!setupMainDialog)
125     {
126         qDebug() << "MainWindow: Creating a setup main dialog.";
127         setupMainDialog = new SetupMainDialog(this);
128     }
129     activateDialog(setupMainDialog);
130     setupMainDialog->refreshDataModel();
131 }
132
133 void MainWindow::configureEmulators()
134 {
135     if (!executableMainDialog) {
136         executableMainDialog = new ExecutableMainDialog(this);
137         connect(executableMainDialog, SIGNAL(finished(int)), this, SLOT(updateData()));
138     }
139     activateDialog(executableMainDialog);
140     executableMainDialog->refreshDataModel();
141 }
142
143 void MainWindow::configureTmpDir()
144 {
145     /*if (!tmpFolderDialog) {
146         tmpFolderDialog = new TmpFolderEditDialog(this, tmpDirFilePath);
147     }
148     activateDialog(tmpFolderDialog);*/
149
150     QString fpath = QFileDialog::getExistingDirectory(this,
151         tr("Select a directory"), tmpDirFilePath,
152         QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
153     QDir d(fpath);
154     if (d.exists() && d.isReadable()) {
155         tmpDirFilePath = fpath;
156         DbConfig::setTmpDir(tmpDirFilePath);
157         launcher->setTmpDirPath(tmpDirFilePath);
158     }
159 }
160
161 void MainWindow::activateDialog(EmuFrontDialog* dia) const
162 {
163     dia->show();
164     dia->raise();
165     dia->activateWindow();
166 }
167
168 void MainWindow::createMenus()
169 {
170     fileMenu = menuBar()->addMenu(tr("&File"));
171     fileMenu->addAction(exitAction);
172
173     configMenu = menuBar()->addMenu(tr("&Config"));
174     configMenu->addAction(configPlatformAction);
175     configMenu->addAction(configMediaTypeAction);
176     configMenu->addAction(configMediaImagePathAction);
177     configMenu->addAction(configSetupAction);
178     configMenu->addAction(configEmulatorAction);
179     configMenu->addAction(configTmpDirAction);
180
181     helpMenu = menuBar()->addMenu(tr("&Help"));
182     helpMenu->addAction(aboutAction);
183 }
184
185 void MainWindow::createStatusBar()
186 {
187     messageLabel = new QLabel;
188     statusBar()->addWidget(messageLabel);
189 }
190
191 void MainWindow::readSettings()
192 {
193 }
194
195 void MainWindow::writeSettings()
196 {
197 }
198
199 void MainWindow::closeEvent(QCloseEvent *event)
200 {
201     if (okToContinue())
202         event->accept();
203     else event->ignore();
204 }
205
206 bool MainWindow::okToContinue()
207 {
208     return true;
209 }
210
211 void MainWindow::updateData()
212 {
213     qDebug() << "MainWindow::updateData()";
214     launcher->updateData();
215 }
216
217 void MainWindow::about()
218 {
219     QMessageBox::about(this, tr("About EmuFront"),
220         "<h2>EmuFront</h2>"
221         "<p>&copy; 2010 Mikko Keinänen</p>"
222         "<p>EmuFront is free software: you can redistribute it and/or modify "
223         "it under the terms of the GNU General Public License version 2 as published by "
224         "the Free Software Foundation.</p>"
225         );
226 }