b502eac6995833322810d2eb5f37a9945747d039
[emufront] / src / views / filepatheditview.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 "filepatheditview.h"
23 #include "filepathmodel.h"
24 #include "fileutil.h"
25 #include "setupmodel.h"
26 #include "comboboxdelegate.h"
27 #include "filesystembrowsedelegate.h"
28 #include <QtGui>
29
30 FilePathEditView::FilePathEditView(SetupModel *supModel, QWidget *parent) :
31     EmuFrontEditView(parent)
32 {
33     setWindowTitle(tr("Set media image paths"));
34     scanButton = new QPushButton(tr("&Scan"));
35     buttonBox->addButton(scanButton, QDialogButtonBox::ActionRole);
36     fileUtil = new FileUtil(this);
37     initProgressDialog();
38
39     model = new FilePathModel(this);
40     objectList->setModel(model);
41     ComboBoxDelegate *setupDelegate = new ComboBoxDelegate(
42         supModel,
43         SetupModel::Setup_Id,
44         SetupModel::Setup_Name,
45         this
46     );
47     objectList->setItemDelegateForColumn(FilePathModel::FilePath_SetupId, setupDelegate);
48     FileSystemBrowseDelegate *fsBrowseDelegate = new FileSystemBrowseDelegate(this);
49     objectList->setItemDelegateForColumn(FilePathModel::FilePath_Name, fsBrowseDelegate);
50     postInit();
51 }
52
53 void FilePathEditView::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 FilePathEditView::connectSignals()
62 {
63     EmuFrontEditView::connectSignals();
64     connect(scanButton, SIGNAL(clicked()), this, SLOT(beginScanFilePath()));
65 }
66
67 void FilePathEditView::beginScanFilePath()
68 {
69     QModelIndex index = objectList->currentIndex();
70     if (!index.isValid()) return;
71
72     if (QMessageBox::question(this,
73         tr("Confirm"),
74         tr("Do you want to continue? "
75         "Re-scanning file path removes old entries for selected path. "
76         "If you have tons of huge files this may take even hours! "
77         "If you are low on battery power, consider carefully!"),
78         QMessageBox::Yes, QMessageBox::No, QMessageBox::NoButton ) == QMessageBox::No)
79     { return; }
80
81     FilePathModel *fpModel = qobject_cast<FilePathModel*>(model);
82     FilePathObject *fpo = fpModel->getFilePathObject(index);
83     if (!fpo) {
84         errorMessage->showMessage(tr("Failed creating a file path object of selected file path."));
85         return;
86     }
87     try
88     {
89         QStringList l;
90         l << "*.zip"; // TODO set filters in a global constant class
91
92         progressDialog->show();
93         //setUIEnabled(false);
94         int count = fileUtil->scanFilePath(fpo, l, progressDialog);
95         progressDialog->hide();
96
97         QMessageBox msgBox;
98         msgBox.setText(tr("Scanned %1 files to database.").arg(count)); msgBox.exec();
99         fpModel->setScanned(fpo->getId());
100         //updateList();
101     }
102     catch (EmuFrontException s)
103     {
104         errorMessage->showMessage( s.what() );
105     }
106     //setUIEnabled(true);
107     delete fpo;
108     fpo = 0;
109 }
110