Small change to text element.
[jspeed] / src / fileselector.cpp
1 /*
2  * This file is part of jSpeed.
3  *
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.
8  *
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.
13  *
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/>.
16  *
17  */
18
19 #include <QtGui/QFileDialog>
20 #include <QtGui/QMessageBox>
21 #include <QtCore/QString>
22 #include <QtCore/QDir>
23 #include <QtCore/QStringList>
24 #include "fileselector.h"
25
26 FileSelector::FileSelector(QString const& text, QWidget* parent): ButtonSelector(text, parent)
27 {
28 }
29
30 bool FileSelector::loadFiles(QString const& dir, QStringList const& patterns, bool stripType)
31 {
32     QDir directory(dir);
33
34     if(directory.exists() && directory.isReadable())
35     {
36         directory.setNameFilters(patterns);
37         directory.setFilter(QDir::Files);
38         QStringList files = directory.entryList();
39
40         for(int i = 0; i < files.size(); i++)
41         {
42             if(stripType)
43             {
44                 int pos = files.at(i).lastIndexOf('.');
45                 QString name;
46
47                 if(pos != -1)
48                 {
49                     name = files.at(i).left(pos);
50                 }
51                 else
52                 {
53                     name = files.at(i);
54                 }
55
56                 addItem(name, files.at(i));
57             }
58             else
59             {
60                 addItem(files.at(i), files.at(i));
61             }
62         }
63
64         return true;
65     }
66
67     return false;
68 }
69
70 bool FileSelector::loadFiles(QString const& dir, QString const& pattern, bool stripType)
71 {
72     return loadFiles(dir, QStringList() << pattern, stripType);
73 }
74
75 bool FileSelector::importFile(QString const& dir,
76                               QString const& name,
77                               QString const& pattern,
78                               bool add,
79                               QString* addedFile)
80 {
81     QString filename = QFileDialog::getOpenFileName(this, tr("Open file"),
82                                                     QDir::home().path(),
83                                                     name + "(" + pattern + ")");
84
85     if(filename.isEmpty())
86     {
87         return true;
88     }
89
90     QString basename;
91
92     int i = filename.lastIndexOf(QDir::separator());
93
94     if(i == -1)
95     {
96         basename = filename;
97     }
98     else
99     {
100         basename = filename.mid(i + 1);
101     }
102
103     QString targetFile = dir + basename;
104
105     QDir targetDir(dir);
106
107     if(!targetDir.exists())
108     {
109         if(!targetDir.mkpath(dir))
110         {
111             return false;
112         }
113     }
114
115     bool ok = true;
116
117     if(QFile::exists(targetFile))
118     {
119         ok = confirmCopy(basename);
120
121         if(ok)
122         {
123             QFile::remove(targetFile);
124         }
125     }
126
127     if(ok)
128     {
129         if(!QFile::copy(filename, targetFile))
130         {
131             return false;
132         }
133
134         if(addedFile)
135         {
136             *addedFile = basename;
137         }
138
139         if(add)
140         {
141             addItem(basename);
142             selectByValue(basename);
143         }
144
145         return true;
146     }
147     else
148     {
149         return false;
150     }
151 }
152
153 bool FileSelector::confirmCopy(QString const& filename)
154 {
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);
158
159     return (result == QMessageBox::Yes);
160 }
161