54b07f619690c9d409057daf5ee49aa63cc5f60d
[emufront] / src / dialogs / mediaimagepathmaindialog.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
23 #include "filepathobject.h"
24 #include "emufrontfileobject.h"
25 #include "dbfilepath.h"
26 #include "fileutil.h"
27 #include "mediaimagepathmaindialog.h"
28 #include "mediaimagepathdialog.h"
29
30 MediaImagePathMainDialog::MediaImagePathMainDialog(QWidget *parent)
31     : DbObjectDialog(parent)
32 {
33     setWindowTitle(tr("Set media image paths"));
34     dbManager = new DbFilePath(this);
35     dbMediaImageContainer = new DbMediaImageContainer(this);
36     initDataTable();
37
38     scanButton = new QPushButton(tr("&Scan"));
39     buttonBox->addButton(scanButton, QDialogButtonBox::ActionRole);
40
41     initEditDialog();
42     hiddenColumns << DbFilePath::FilePath_Id;
43     hiddenColumns << DbFilePath::FilePath_SetupId;
44     hideColumns();
45
46     fileUtil = new FileUtil(this);
47     initProgressDialog();
48
49     // do not move to parent class:
50     connectSignals();
51 }
52
53 void MediaImagePathMainDialog::initProgressDialog()
54 {
55     progressDialog = new QProgressDialog(this);
56     progressDialog->setWindowTitle(tr("Scanning files"));
57     progressDialog->setCancelButtonText(tr("Abort"));
58     progressDialog->setWindowModality(Qt::WindowModal);
59 }
60
61 void MediaImagePathMainDialog::connectSignals()
62 {
63     DbObjectDialog::connectSignals();
64     connect(scanButton, SIGNAL(clicked()), this, SLOT(beginScanFilePath()));
65 }
66
67 void MediaImagePathMainDialog::initEditDialog()
68 {
69     nameDialog = new MediaImagePathDialog(this, dynamic_cast<FilePathObject*>(dbObject));
70     connectNameDialogSignals();
71 }
72
73 void MediaImagePathMainDialog::beginScanFilePath()
74 {
75     QModelIndex index = objectList->currentIndex();
76     if (!index.isValid()) return;
77     if (QMessageBox::question(this,
78         tr("Confirm"),
79         tr("Do you want to continue? "
80         "If you have tons of huge files this may take even hours! "
81         "If you are low on battery power, consider carefully!"),
82         QMessageBox::Yes, QMessageBox::No, QMessageBox::NoButton ) == QMessageBox::No) {
83             return;
84         }
85     FilePathObject *fpo = 0;
86     try
87     {
88         EmuFrontObject *ob = dbManager->getDataObjectFromModel(&index); // throws EmuFrontException
89         if (!ob) return;
90         fpo = dynamic_cast<FilePathObject*>(ob);
91         if (!fpo) return;
92         QStringList l;
93         l << "*.zip"; // TODO set filters in a global constant class
94
95         dbMediaImageContainer->removeFromFilePath(fpo->getId());
96
97         progressDialog->show();
98
99         setUIEnabled(false);
100         int count = fileUtil->scanFilePath(fpo, l, dbMediaImageContainer, progressDialog);
101         progressDialog->hide();
102
103         QMessageBox msgBox;
104         msgBox.setText(tr("Scanned %1 files to database.").arg(count));
105         msgBox.exec();
106         DbFilePath *dbfp = dynamic_cast<DbFilePath*>(dbManager);
107         if (!(dbfp && dbfp->setScanned(fpo)))
108             throw EmuFrontException(tr("Failed updating the last scanned time stamp for selected file path!"));
109         else updateList();
110     }
111     catch (EmuFrontException s)
112     {
113         errorMessage->showMessage( s.what() );
114     }
115     setUIEnabled(true);
116     delete fpo;
117     fpo = 0;
118 }
119
120 EmuFrontObject* MediaImagePathMainDialog::createObject()
121 {
122     return new FilePathObject(FilePathObject::FilePathType_MediaImageDir);
123 }
124
125 MediaImagePathMainDialog::~MediaImagePathMainDialog()
126 {
127     deleteCurrentObject();
128 }
129
130 void MediaImagePathMainDialog::deleteCurrentObject()
131 {
132     if (dbObject) {
133         FilePathObject* fpo =  dynamic_cast<FilePathObject*>(dbObject);
134         if (fpo) delete fpo;
135         else qDebug() << "Failed deleteing FilePathObject";
136         dbObject = 0;
137     }
138 }
139
140 void MediaImagePathMainDialog::cleanUp()
141 {
142     deleteCurrentObject();
143     if (nameDialog) {
144         MediaImagePathDialog *pnd =
145             dynamic_cast<MediaImagePathDialog*>(nameDialog);
146         if (pnd) delete pnd;
147         else qDebug() << "Failed to delete MediaImagePathDialog";
148         nameDialog = 0;
149     }
150 }