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, bool stripType)
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++)
44 int pos = files.at(i).lastIndexOf('.');
49 name = files.at(i).left(pos);
56 addItem(name, files.at(i));
60 addItem(files.at(i), files.at(i));
70 bool FileSelector::loadFiles(QString const& dir, QString const& pattern, bool stripType)
72 return loadFiles(dir, QStringList() << pattern, stripType);
75 bool FileSelector::importFile(QString const& dir,
77 QString const& pattern,
81 QString filename = QFileDialog::getOpenFileName(this, tr("Open file"),
83 name + "(" + pattern + ")");
85 if(filename.isEmpty())
92 int i = filename.lastIndexOf(QDir::separator());
100 basename = filename.mid(i + 1);
103 QString targetFile = dir + basename;
107 if(!targetDir.exists())
109 if(!targetDir.mkpath(dir))
117 if(QFile::exists(targetFile))
119 ok = confirmCopy(basename);
123 QFile::remove(targetFile);
129 if(!QFile::copy(filename, targetFile))
136 *addedFile = basename;
142 selectByValue(basename);
153 bool FileSelector::confirmCopy(QString const& filename)
155 QMessageBox::StandardButton result = QMessageBox::question(this, tr("File exists"),
156 tr("File %1 already exists in directory, overwrite?").arg(filename),
157 QMessageBox::Yes | QMessageBox::No);
159 return (result == QMessageBox::Yes);