0f9fd9768efd2cc2192bf2a37f1db4007000aab6
[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("bootscreen.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     save = new QPushButton("Save");
132     save->setEnabled(false);
133
134     if(random){
135         up->setEnabled(false);
136         down->setEnabled(false);
137     }
138
139     layButtons->addWidget(up);
140     layButtons->addWidget(down);
141     layButtons->addSpacing(50);
142     layButtons->addWidget(save);
143     lay->addLayout(layButtons);
144
145     connect(up, SIGNAL(clicked()), this, SLOT(moveUp()));
146     connect(down, SIGNAL(clicked()), this, SLOT(moveDown()));
147     connect(save, SIGNAL(clicked()), this, SLOT(saveConfs()));
148     connect(list, SIGNAL(itemActivated(QListWidgetItem*)), this, SLOT(setPendingChanges(QListWidgetItem*)));
149
150     setCentralWidget(central);
151 }
152
153 void BootScreen::addVideo()
154 {
155     QString filePath = QFileDialog::getOpenFileName(this,
156         tr("Open file"), "/home/user/MyDocs", tr("Video Files (*.avi)"));
157     qDebug() << filePath << endl;
158
159     if(filePath != ""){
160         QFile fileCopy(filePath);
161         QFileInfo fileInfo(filePath);
162         QString fileName = fileInfo.fileName();
163         //boot player can't read files with spaces in the name
164         fileName.replace(QString(" "), QString("_"));
165         QString newFilePath = videosDir + fileName;
166         qDebug() << newFilePath << endl;
167         qDebug() << fileInfo.size() << endl;
168         if(fileInfo.size() > 11100000)
169             QMaemo5InformationBox::information(this, "<font color=black><b>Error:</b> The selected file is too big</font>",
170                                                         QMaemo5InformationBox::DefaultTimeout);
171
172         else{
173             QFile tempTest(newFilePath);
174             if(tempTest.exists())
175                 QMaemo5InformationBox::information(this, "<font color=black><b>Error:</b> The file already exists</font>",
176                                                             QMaemo5InformationBox::DefaultTimeout);
177             else{
178                 bool result = fileCopy.copy(newFilePath);
179                 if(result){
180                     list->addItem(QString("%1").arg(fileName));
181                     save->setEnabled(true);
182                     //autoselect item, if not in random mode
183                     if(!random){
184                         QListWidgetItem *currentItem = list->item(list->count()-1);
185                         currentItem->setSelected(true);
186                     }
187                 }
188                 else
189                     QMaemo5InformationBox::information(this, "<font color=black>Error copying file</font>",
190                                                                 QMaemo5InformationBox::DefaultTimeout);
191             }
192         }
193     }
194
195 }
196
197 void BootScreen::removeVideos()
198 {
199     QListWidgetItem *temp;
200     QList <QListWidgetItem *> selectedItems = list->selectedItems();
201     if(selectedItems.size()>0){
202         QDir vidDir(videosDir);
203         int ret = QMessageBox::information(this, tr("Remove files"), tr("Remove selected file(s) ?"), QMessageBox::Yes | QMessageBox::No);
204
205         if(ret == QMessageBox::Yes){
206             for(int i=0; i< selectedItems.size();i++){
207                 temp = selectedItems.at(i);
208                 bool result = vidDir.remove(temp->text());
209                 qDebug() << result << " :" << temp->text() << endl;
210
211                 if(result){
212                     int rw = list->row(temp);
213                     temp = list->takeItem(rw);
214                     delete temp;
215                 }
216             }
217             save->setEnabled(true);
218         }
219     }
220
221 }
222
223 void BootScreen::moveUp()
224 {
225     int currentRow = list->currentRow();
226     if (currentRow == 0) return;
227     QListWidgetItem * currentItem = list->takeItem(currentRow);
228     list->insertItem(currentRow - 1, currentItem);
229     list->setCurrentRow(currentRow - 1);
230     save->setEnabled(true);
231 }
232
233 void BootScreen::moveDown()
234 {
235     int currentRow = list->currentRow();
236     if (currentRow >= list->count()-1) return;
237     QListWidgetItem * currentItem = list->takeItem(currentRow);
238     list->insertItem(currentRow + 1, currentItem);
239     list->setCurrentRow(currentRow + 1);
240     save->setEnabled(true);
241 }
242
243 void BootScreen::disableSelection(bool state)
244 {
245     if(state){
246         up->setEnabled(false);
247         down->setEnabled(false);
248         QListWidgetItem *temp;
249         int listSize = list->count();
250         //disable items
251         QTime fixTime(0, 0, 0);
252         qsrand(fixTime.secsTo(QTime::currentTime()));
253         int randomNumb = qrand() % listSize;
254         qDebug() << randomNumb << endl;
255         for(int i = 0; i < listSize; i++){
256             temp = list->item(i);
257             if(temp){
258                 if(i == randomNumb)
259                     temp->setSelected(true);
260                 else
261                     temp->setSelected(false);
262             }
263         }
264         list->setSelectionMode(QAbstractItemView::NoSelection);
265         QMaemo5InformationBox::information(this, "<font color=black><b>Random mode:</b> Selection disabled</font>",
266                                                     QMaemo5InformationBox::DefaultTimeout);
267     }
268
269     random = true;
270     save->setEnabled(true);
271 }
272
273 void BootScreen::selectMultiple(bool state)
274 {
275     if(state){
276         up->setEnabled(true);
277         down->setEnabled(true);
278         list->setSelectionMode(QAbstractItemView::MultiSelection);
279         random = false;
280     }
281 }
282
283 void BootScreen::writeSettings()
284 {
285     QSettings settings(settingsFile.fileName(),QSettings::IniFormat);
286     settings.setValue("random", random);
287 }
288
289 void BootScreen::restoreSettings()
290 {
291      qDebug() << settingsFile.fileName() << endl;
292
293      if (!settingsFile.open(QIODevice::ReadWrite | QIODevice::Text)){
294         qWarning("Cannot create the settings file"); //abord
295         QMaemo5InformationBox::information(this, "<font color=black><b>Error:</b> Cannot create the settings file</font>",
296                                                     QMaemo5InformationBox::DefaultTimeout);
297      }
298
299      QTextStream out(&settingsFile);
300      out << "random=false" << endl;
301
302      random = false;
303 }
304
305 void BootScreen::saveConfs(){
306
307     //remove old files
308     QDir dir(hildonWelcome);
309     dir.setFilter(QDir::Files);
310
311     QFileInfoList ls = dir.entryInfoList();
312     for (int i = 0; i < ls.size(); ++i) {
313         QFileInfo fileInfo = ls.at(i);
314         bool result = dir.remove(fileInfo.fileName());
315         qDebug() << result << " :" << fileInfo.fileName() << endl;
316     }
317
318     QListWidgetItem *temp;
319     for(int i=0; i< list->count();i++){
320         temp = list->item(i);
321         if(temp->isSelected())
322             if(random)
323                  createFile(temp->text(), 0);
324             else
325                 createFile(temp->text(), i);
326     }
327     save->setEnabled(false);
328 }
329
330 void BootScreen::createFile(QString filename, int index)
331 {
332     QString ind = QString::number(index);
333     if(mediaFiles.contains(filename)){
334         QFile confFile(hildonWelcome + ind + ".conf");
335         qDebug() << confFile.fileName() << endl;
336         if (!confFile.open(QIODevice::ReadWrite | QIODevice::Text)){
337            qWarning("Cannot create the settings file"); //abord
338            QMaemo5InformationBox::information(this, "<font color=black><b>Error:</b> Cannot create the configurations file</font>",
339                                                        QMaemo5InformationBox::DefaultTimeout);
340        }
341         else{
342             QString filePath = mediaFiles.value(filename);
343             QTextStream out(&confFile);
344             out << "[hildon-welcome]" << endl;
345             out << "filename=" << filePath << endl;
346         }
347     }
348     else{
349         qDebug() << filename << endl;
350         qDebug() << "File already exists" << endl;
351     }
352 }
353
354 void BootScreen::setPendingChanges(QListWidgetItem *item)
355 {
356     if(!random)
357         save->setEnabled(true);
358 }
359
360 void BootScreen::closeEvent(QCloseEvent *event)
361 {
362     if(save->isEnabled()){
363         int ret = QMessageBox::information(this, tr("There are pending changes"), tr("Do you want to save your changes?"),
364                                            QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
365
366         if(ret == QMessageBox::Save){
367             saveConfs();
368             writeSettings();
369             event->accept();
370
371         }
372         else if(ret == QMessageBox::Discard){
373             writeSettings();
374             event->accept();
375         }
376         else{
377             event->ignore();
378         }
379     }
380     else{
381         writeSettings();
382         event->accept();
383     }
384 }
385
386 BootScreen::~BootScreen()
387 {
388 }
389
390