Changed media player to initialize in boot.
[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)
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             addItem(files.at(i));
43         }
44
45         return true;
46     }
47
48     return false;
49 }
50
51 bool FileSelector::loadFiles(QString const& dir, QString const& pattern)
52 {
53     return loadFiles(dir, QStringList() << pattern);
54 }
55
56 bool FileSelector::importFile(QString const& dir,
57                               QString const& name,
58                               QString const& pattern,
59                               bool add,
60                               QString* addedFile)
61 {
62     QString filename = QFileDialog::getOpenFileName(this, tr("Open file"),
63                                                     QDir::home().path(),
64                                                     name + "(" + pattern + ")");
65
66     if(filename.isEmpty())
67     {
68         return true;
69     }
70
71     QString basename;
72
73     int i = filename.lastIndexOf(QDir::separator());
74
75     if(i == -1)
76     {
77         basename = filename;
78     }
79     else
80     {
81         basename = filename.mid(i + 1);
82     }
83
84     QString targetFile = dir + basename;
85
86     QDir targetDir(dir);
87
88     if(!targetDir.exists())
89     {
90         if(!targetDir.mkpath(dir))
91         {
92             return false;
93         }
94     }
95
96     bool ok = true;
97
98     if(QFile::exists(targetFile))
99     {
100         ok = confirmCopy(basename);
101
102         if(ok)
103         {
104             QFile::remove(targetFile);
105         }
106     }
107
108     if(ok)
109     {
110         if(!QFile::copy(filename, targetFile))
111         {
112             return false;
113         }
114
115         if(addedFile)
116         {
117             *addedFile = basename;
118         }
119
120         if(add)
121         {
122             addItem(basename);
123             selectByValue(basename);
124         }
125
126         return true;
127     }
128     else
129     {
130         return false;
131     }
132 }
133
134 bool FileSelector::confirmCopy(QString const& filename)
135 {
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);
139
140     return (result == QMessageBox::Yes);
141 }
142