Fixed "file with spaces in the name" bug, boot player can't read these
[bootcreen] / bootscreen.cpp
1 #include "bootscreen.h"
2
3 #include <QtMaemo5>
4
5 BootScreen::BootScreen(QWidget *parent)
6     : QMainWindow(parent)
7 {
8     hildonWelcome = "/etc/hildon-welcome.d/";
9     videosDir = "/opt/bootscreen/media/";
10     settingsDir = "/opt/bootscreen/data/";
11     checkSettings();
12
13     filterGroup = new QActionGroup(this);
14     filterGroup->setExclusive(true);
15     mRandom = new QAction(tr("Random"), filterGroup);
16     mRandom->setCheckable(true);
17     mMultiple = new QAction(tr("Multiple"), filterGroup);
18     mMultiple->setCheckable(true);
19
20     //laod settings
21     if(random)
22         mRandom->setChecked(true);
23     else
24         mMultiple->setChecked(true);
25
26
27     connect(mRandom, SIGNAL(toggled(bool)), this, SLOT(disableSelection(bool)));
28     connect(mMultiple, SIGNAL(toggled(bool)), this, SLOT(selectMultiple(bool)));
29
30     menuBar()->addActions(filterGroup->actions());
31
32     menuBar()->addAction(tr("Add"), this, SLOT(addVideo()));
33     menuBar()->addAction(tr("Remove"), this, SLOT(removeVideos()));
34
35     createList();
36 }
37
38 void BootScreen::checkSettings()
39 {
40     QDir dir(settingsDir);
41     settingsFile.setFileName(dir.filePath("bootscrenn.conf"));
42
43     if (!settingsFile.exists())
44         restoreSettings();
45     else{
46          QSettings tempSettings(settingsFile.fileName(),QSettings::IniFormat);
47          random = tempSettings.value("random").toBool();
48     }
49 }
50
51 void BootScreen::createList()
52 {
53     QWidget *central = new QWidget();
54
55     QVBoxLayout *lay = new QVBoxLayout(central);
56     lay->setSpacing(8);
57     lay->setContentsMargins(0, 0, 0, 15);
58
59     list = new QListWidget();
60
61
62     list->setSelectionMode(QAbstractItemView::MultiSelection);
63
64     QDir dirHildon(hildonWelcome);
65     dirHildon.setFilter(QDir::Files | QDir::NoSymLinks);
66     dirHildon.setSorting(QDir::Name);
67
68     QFileInfoList ls = dirHildon.entryInfoList();
69     for (int i = 0; i < ls.size(); ++i) {
70         QFileInfo fileInfo = ls.at(i);
71         QSettings tempSettings(fileInfo.absoluteFilePath(),QSettings::IniFormat);
72         tempSettings.beginGroup("hildon-welcome");
73
74         QFileInfo prettyName(tempSettings.value("filename").toString());
75         list->addItem(QString("%1").arg(prettyName.fileName()));
76         mediaFiles[prettyName.fileName()] = tempSettings.value("filename").toString();
77
78
79         qDebug() << fileInfo.absoluteFilePath() << endl;
80         qDebug() << tempSettings.value("filename") << endl;
81     }
82
83
84     QListWidgetItem *temp;
85     int listSize = list->count();
86     //activate items
87     for(int i = 0; i < listSize; i++){
88         temp = list->item(i);
89         if(temp)
90             temp->setSelected(true);
91     }
92
93     if(random)
94         list->setSelectionMode(QAbstractItemView::NoSelection);
95
96     //Add hands video
97
98     if(!mediaFiles.contains("Hands-v32-h264.avi")){
99         QFileInfo handsTmp("/usr/share/hildon-welcome/media/Hands-v32-h264.avi");
100         if(handsTmp.exists()){
101             mediaFiles[handsTmp.fileName()] = handsTmp.absoluteFilePath();
102             list->addItem(QString("%1").arg(handsTmp.fileName()));
103         }
104     }
105
106     QDir dir(videosDir);
107     dir.setFilter(QDir::Files | QDir::NoSymLinks);
108     dir.setSorting(QDir::Name);
109     //if empty show warning to add a file via menu
110
111     ls = dir.entryInfoList();
112     for (int i = 0; i < ls.size(); ++i) {
113         QFileInfo fileInfo = ls.at(i);
114         if(!mediaFiles.contains(fileInfo.fileName())){
115             mediaFiles[fileInfo.fileName()] = fileInfo.absoluteFilePath();
116             list->addItem(QString("%1").arg(fileInfo.fileName()));
117         }
118         qDebug() << fileInfo.absoluteFilePath() << endl;
119
120     }
121
122    // for(int i =0; i < 8; i++){
123      //  list->addItem("bbr");
124     //}
125
126     lay->addWidget(list);
127
128     QHBoxLayout *layButtons = new QHBoxLayout();
129     up = new QPushButton("Up");
130     down = new QPushButton("Down");
131
132     if(random){
133         up->setEnabled(false);
134         down->setEnabled(false);
135     }
136
137     layButtons->addWidget(up);
138     layButtons->addWidget(down);
139     lay->addLayout(layButtons);
140
141     connect(up, SIGNAL(clicked()), this, SLOT(moveUp()));
142     connect(down, SIGNAL(clicked()), this, SLOT(moveDown()));
143
144     setCentralWidget(central);
145 }
146
147 void BootScreen::addVideo()
148 {
149     QString filePath = QFileDialog::getOpenFileName(this,
150         tr("Open file"), "/home/user/MyDocs", tr("Video Files (*.avi)"));
151     qDebug() << filePath << endl;
152
153     if(filePath != ""){
154         QFile fileCopy(filePath);
155         QFileInfo fileInfo(filePath);
156         QString fileName = fileInfo.fileName();
157         //boot player can't read files with spaces in the name
158         fileName.replace(QString(" "), QString("_"));
159         QString newFilePath = videosDir + fileName;
160         qDebug() << newFilePath << endl;
161         qDebug() << fileInfo.size() << endl;
162         if(fileInfo.size() > 11100000)
163             QMaemo5InformationBox::information(this, "<font color=black><b>Error:</b> The selected file is too big</font>",
164                                                         QMaemo5InformationBox::DefaultTimeout);
165
166         else{
167             QFile tempTest(newFilePath);
168             if(tempTest.exists())
169                 QMaemo5InformationBox::information(this, "<font color=black><b>Error:</b> The file already exists</font>",
170                                                             QMaemo5InformationBox::DefaultTimeout);
171             else{
172                 bool result = fileCopy.copy(newFilePath);
173                 if(result){
174                     list->addItem(QString("%1").arg(fileName));
175                     //autoselect item, if not in random mode
176                     if(!random){
177                         QListWidgetItem *currentItem = list->item(list->count()-1);
178                         currentItem->setSelected(true);
179                     }
180                 }
181                 else
182                     QMaemo5InformationBox::information(this, "<font color=black>Error copying file</font>",
183                                                                 QMaemo5InformationBox::DefaultTimeout);
184             }
185         }
186     }
187
188 }
189
190 void BootScreen::removeVideos()
191 {
192     QListWidgetItem *temp;
193     QList <QListWidgetItem *> selectedItems = list->selectedItems();
194     if(selectedItems.size()>0){
195         QDir vidDir(videosDir);
196         QMessageBox msgBox;
197         int ret = QMessageBox::information(this, tr("Remove files"), tr("Remove selected file(s) ?"), QMessageBox::Yes | QMessageBox::No);
198
199         if(ret == QMessageBox::Yes){
200             for(int i=0; i< selectedItems.size();i++){
201                 temp = selectedItems.at(i);
202                 bool result = vidDir.remove(temp->text());
203                 qDebug() << result << " :" << temp->text() << endl;
204
205                 if(result){
206                     int rw = list->row(temp);
207                     temp = list->takeItem(rw);
208                     delete temp;
209                 }
210             }
211         }
212     }
213
214 }
215
216 void BootScreen::moveUp()
217 {
218     int currentRow = list->currentRow();
219     if (currentRow == 0) return;
220     QListWidgetItem * currentItem = list->takeItem(currentRow);
221     list->insertItem(currentRow - 1, currentItem);
222     list->setCurrentRow(currentRow - 1);
223 }
224
225 void BootScreen::moveDown()
226 {
227     int currentRow = list->currentRow();
228     if (currentRow >= list->count()-1) return;
229     QListWidgetItem * currentItem = list->takeItem(currentRow);
230     list->insertItem(currentRow + 1, currentItem);
231     list->setCurrentRow(currentRow + 1);
232 }
233
234 void BootScreen::disableSelection(bool state)
235 {
236     if(state){
237         up->setEnabled(false);
238         down->setEnabled(false);
239         QListWidgetItem *temp;
240         int listSize = list->count();
241         //disable items
242         QTime fixTime(0, 0, 0);
243         qsrand(fixTime.secsTo(QTime::currentTime()));
244         int randomNumb = qrand() % listSize;
245         qDebug() << randomNumb << endl;
246         for(int i = 0; i < listSize; i++){
247             temp = list->item(i);
248             if(temp){
249                 if(i == randomNumb)
250                     temp->setSelected(true);
251                 else
252                     temp->setSelected(false);
253             }
254         }
255         list->setSelectionMode(QAbstractItemView::NoSelection);
256         QMaemo5InformationBox::information(this, "<font color=black><b>Random mode:</b> Selection disabled</font>",
257                                                     QMaemo5InformationBox::DefaultTimeout);
258     }
259
260     random = true;
261 }
262
263 void BootScreen::selectMultiple(bool state)
264 {
265     if(state){
266         up->setEnabled(true);
267         down->setEnabled(true);
268         list->setSelectionMode(QAbstractItemView::MultiSelection);
269         random = false;
270     }
271 }
272
273 void BootScreen::writeSettings()
274 {
275     QSettings settings(settingsFile.fileName(),QSettings::IniFormat);
276     settings.setValue("random", random);
277 }
278
279 void BootScreen::restoreSettings()
280 {
281      qDebug() << settingsFile.fileName() << endl;
282
283      if (!settingsFile.open(QIODevice::ReadWrite | QIODevice::Text)){
284         qWarning("Cannot create the settings file"); //abord
285         QMaemo5InformationBox::information(this, "<font color=black><b>Error:</b> Cannot create the settings file</font>",
286                                                     QMaemo5InformationBox::DefaultTimeout);
287      }
288
289      QTextStream out(&settingsFile);
290      out << "random=false" << endl;
291
292      random = false;
293 }
294
295 void BootScreen::writeFiles(){
296
297     //remove old files
298     QDir dir(hildonWelcome);
299     dir.setFilter(QDir::Files);
300
301     QFileInfoList ls = dir.entryInfoList();
302     for (int i = 0; i < ls.size(); ++i) {
303         QFileInfo fileInfo = ls.at(i);
304         bool result = dir.remove(fileInfo.fileName());
305         qDebug() << result << " :" << fileInfo.fileName() << endl;
306     }
307
308     QListWidgetItem *temp;
309     for(int i=0; i< list->count();i++){
310         temp = list->item(i);
311         if(temp->isSelected())
312             if(random)
313                  createFile(temp->text(), 0);
314             else
315                 createFile(temp->text(), i);
316     }
317 }
318
319 void BootScreen::createFile(QString filename, int index)
320 {
321     QString ind = QString::number(index);
322     if(mediaFiles.contains(filename)){
323         QFile confFile(hildonWelcome + ind + ".conf");
324         qDebug() << confFile.fileName() << endl;
325         if (!confFile.open(QIODevice::ReadWrite | QIODevice::Text))
326            qWarning("Cannot create the settings file"); //abord
327
328         else{
329             QString filePath = mediaFiles.value(filename);
330             QTextStream out(&confFile);
331             out << "[hildon-welcome]" << endl;
332             out << "filename=" << filePath << endl;
333         }
334     }
335     else{
336         qDebug() << filename << endl;
337         qDebug() << "print a error" << endl;
338     }
339 }
340
341 void BootScreen::closeEvent(QCloseEvent *event)
342 {
343     writeSettings();
344     writeFiles();
345     event->accept();
346 }
347
348 BootScreen::~BootScreen()
349 {
350 }
351
352