2 * This file is part of jSpeed.
4 * jSpeed is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * jSpeed is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with jSpeed. If not, see <http://www.gnu.org/licenses/>.
19 #include <QtGui/QFileDialog>
20 #include <QtGui/QMessageBox>
21 #include <QtCore/QString>
22 #include <QtCore/QDir>
23 #include <QtCore/QStringList>
24 #include "fileselector.h"
26 FileSelector::FileSelector(QString const& text, QWidget* parent): ButtonSelector(text, parent)
30 bool FileSelector::loadFiles(QString const& dir, QStringList const& patterns)
34 if(directory.exists() && directory.isReadable())
36 directory.setNameFilters(patterns);
37 directory.setFilter(QDir::Files);
38 QStringList files = directory.entryList();
40 for(int i = 0; i < files.size(); i++)
51 bool FileSelector::loadFiles(QString const& dir, QString const& pattern)
53 return loadFiles(dir, QStringList() << pattern);
56 bool FileSelector::importFile(QString const& dir,
58 QString const& pattern,
62 QString filename = QFileDialog::getOpenFileName(this, tr("Open file"),
64 name + "(" + pattern + ")");
66 if(filename.isEmpty())
73 int i = filename.lastIndexOf(QDir::separator());
81 basename = filename.mid(i + 1);
84 QString targetFile = dir + basename;
88 if(!targetDir.exists())
90 if(!targetDir.mkpath(dir))
98 if(QFile::exists(targetFile))
100 ok = confirmCopy(basename);
104 QFile::remove(targetFile);
110 if(!QFile::copy(filename, targetFile))
117 *addedFile = basename;
123 selectByValue(basename);
134 bool FileSelector::confirmCopy(QString const& filename)
136 QMessageBox::StandardButton result = QMessageBox::question(this, tr("File exists"),
137 tr("File %1 already exists in directory, overwrite?").arg(filename),
138 QMessageBox::Yes | QMessageBox::No);
140 return (result == QMessageBox::Yes);