Added theme scheduler, poi support and speed alarm features.
[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, QString const& pattern)
31 {
32     QDir directory(dir);
33
34     if(directory.exists() && directory.isReadable())
35     {
36         QStringList filters;
37         filters << pattern;
38         directory.setNameFilters(filters);
39         directory.setFilter(QDir::Files);
40         QStringList files = directory.entryList();
41
42         for(int i = 0; i < files.size(); i++)
43         {
44             addItem(files.at(i));
45         }
46
47         return true;
48     }
49
50     return false;
51 }
52
53 bool FileSelector::importFile(QString const& dir,
54                               QString const& name,
55                               QString const& pattern,
56                               bool add,
57                               QString* addedFile)
58 {
59     QString filename = QFileDialog::getOpenFileName(this, tr("Open file"),
60                                                     QDir::home().path(),
61                                                     name + "(" + pattern + ")");
62
63     if(filename.isEmpty())
64     {
65         return true;
66     }
67
68     QString basename;
69
70     int i = filename.lastIndexOf(QDir::separator());
71
72     if(i == -1)
73     {
74         basename = filename;
75     }
76     else
77     {
78         basename = filename.mid(i + 1);
79     }
80
81     QString targetFile = dir + basename;
82
83     QDir targetDir(dir);
84
85     if(!targetDir.exists())
86     {
87         if(!targetDir.mkpath(dir))
88         {
89             return false;
90         }
91     }
92
93     bool ok = true;
94
95     if(QFile::exists(targetFile))
96     {
97         ok = confirmCopy(basename);
98
99         if(ok)
100         {
101             QFile::remove(targetFile);
102         }
103     }
104
105     if(ok)
106     {
107         if(!QFile::copy(filename, targetFile))
108         {
109             return false;
110         }
111
112         if(addedFile)
113         {
114             *addedFile = basename;
115         }
116
117         if(add)
118         {
119             addItem(basename);
120             selectByValue(basename);
121         }
122
123         return true;
124     }
125     else
126     {
127         return false;
128     }
129 }
130
131 bool FileSelector::confirmCopy(QString const& filename)
132 {
133     QMessageBox::StandardButton result =  QMessageBox::question(this, tr("File exists"),
134                                                                 tr("File %1 already exists in directory, overwrite?").arg(filename),
135                                                                 QMessageBox::Yes | QMessageBox::No);
136
137     return (result == QMessageBox::Yes);
138 }
139