24504ed39e100a0b51b30165b2baaee59f5a4bdc
[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 #include "filepatheditview.h"
22 #include "filepathmodel.h"
23 #include "fileutil.h"
24 #include "setupmodel.h"
25 #include "comboboxdelegate.h"
26 #include "filesystembrowsedelegate.h"
27 #include "dbmediaimagecontainer.h"
28 #include <QtGui>
29
30 FilePathEditView::FilePathEditView(QWidget *parent) :
31     EmuFrontEditView(parent)
32 {
33     scanButton = new QPushButton(tr("&Scan"));
34     buttonBox->addButton(scanButton, QDialogButtonBox::ActionRole);
35     fileUtil = new FileUtil(this);
36     initProgressDialog();
37
38     dbMediaImageContainer = new DbMediaImageContainer(this);
39
40     model = new FilePathModel(this);
41     objectList->setModel(model);
42     SetupModel *stupMdl = new SetupModel(this);
43     ComboBoxDelegate *setupDelegate = new ComboBoxDelegate(
44         stupMdl,
45         SetupModel::Setup_Id,
46         SetupModel::Setup_Name,
47         this
48     );
49     objectList->setItemDelegateForColumn(FilePathModel::FilePath_SetupId, setupDelegate);
50     FileSystemBrowseDelegate *fsBrowseDelegate = new FileSystemBrowseDelegate(this);
51     objectList->setItemDelegateForColumn(FilePathModel::FilePath_Name, fsBrowseDelegate);
52     postInit();
53 }
54
55 void FilePathEditView::initProgressDialog()
56 {
57     progressDialog = new QProgressDialog(this);
58     progressDialog->setWindowTitle(tr("Scanning files"));
59     progressDialog->setCancelButtonText(tr("Abort"));
60     progressDialog->setWindowModality(Qt::WindowModal);
61 }
62
63 void FilePathEditView::connectSignals()
64 {
65     EmuFrontEditView::connectSignals();
66     connect(scanButton, SIGNAL(clicked()), this, SLOT(beginScanFilePath()));
67 }
68
69 void FilePathEditView::beginScanFilePath()
70 {
71     QModelIndex index = objectList->currentIndex();
72     if (!index.isValid()) return;
73
74     if (QMessageBox::question(this,
75         tr("Confirm"),
76         tr("Do you want to continue? "
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         // Remove old instances scanned from this file path
94         dbMediaImageContainer->removeFromFilePath(fpo->getId());
95
96         progressDialog->show();
97
98         //setUIEnabled(false);
99         int count = fileUtil->scanFilePath(fpo, l, dbMediaImageContainer, progressDialog);
100         progressDialog->hide();
101
102         QMessageBox msgBox;
103         msgBox.setText(tr("Scanned %1 files to database.").arg(count)); msgBox.exec();
104         fpModel->setScanned(fpo->getId());
105         //updateList();
106     }
107     catch (EmuFrontException s)
108     {
109         errorMessage->showMessage( s.what() );
110     }
111     //setUIEnabled(true);
112     delete fpo;
113     fpo = 0;
114 }
115