988160f8cd48387299ccfef80f1bec1aa5eba9b1
[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(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     SetupModel *stupMdl = new SetupModel(this);
42     ComboBoxDelegate *setupDelegate = new ComboBoxDelegate(
43         stupMdl,
44         SetupModel::Setup_Id,
45         SetupModel::Setup_Name,
46         this
47     );
48     objectList->setItemDelegateForColumn(FilePathModel::FilePath_SetupId, setupDelegate);
49     FileSystemBrowseDelegate *fsBrowseDelegate = new FileSystemBrowseDelegate(this);
50     objectList->setItemDelegateForColumn(FilePathModel::FilePath_Name, fsBrowseDelegate);
51     postInit();
52 }
53
54 void FilePathEditView::initProgressDialog()
55 {
56     progressDialog = new QProgressDialog(this);
57     progressDialog->setWindowTitle(tr("Scanning files"));
58     progressDialog->setCancelButtonText(tr("Abort"));
59     progressDialog->setWindowModality(Qt::WindowModal);
60 }
61
62 void FilePathEditView::connectSignals()
63 {
64     EmuFrontEditView::connectSignals();
65     connect(scanButton, SIGNAL(clicked()), this, SLOT(beginScanFilePath()));
66 }
67
68 void FilePathEditView::beginScanFilePath()
69 {
70     QModelIndex index = objectList->currentIndex();
71     if (!index.isValid()) return;
72
73     if (QMessageBox::question(this,
74         tr("Confirm"),
75         tr("Do you want to continue? "
76         "Re-scanning file path removes old entries for selected path. "
77         "If you have tons of huge files this may take even hours! "
78         "If you are low on battery power, consider carefully!"),
79         QMessageBox::Yes, QMessageBox::No, QMessageBox::NoButton ) == QMessageBox::No)
80     { return; }
81
82     FilePathModel *fpModel = qobject_cast<FilePathModel*>(model);
83     FilePathObject *fpo = fpModel->getFilePathObject(index);
84     if (!fpo) {
85         errorMessage->showMessage(tr("Failed creating a file path object of selected file path."));
86         return;
87     }
88     try
89     {
90         QStringList l;
91         l << "*.zip"; // TODO set filters in a global constant class
92
93         progressDialog->show();
94         //setUIEnabled(false);
95         int count = fileUtil->scanFilePath(fpo, l, progressDialog);
96         progressDialog->hide();
97
98         QMessageBox msgBox;
99         msgBox.setText(tr("Scanned %1 files to database.").arg(count)); msgBox.exec();
100         fpModel->setScanned(fpo->getId());
101         //updateList();
102     }
103     catch (EmuFrontException s)
104     {
105         errorMessage->showMessage( s.what() );
106     }
107     //setUIEnabled(true);
108     delete fpo;
109     fpo = 0;
110 }
111