Added about dialog
[bootcreen] / src / 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     menuBar()->addAction(tr("About"), this, SLOT(showAbout()));
35
36     setWindowTitle("BootScreen Manager");
37
38     createList();
39 }
40
41 void BootScreen::checkSettings()
42 {
43     QDir dir(settingsDir);
44     settingsFile.setFileName(dir.filePath("bootscreen.conf"));
45
46     if (!settingsFile.exists())
47         restoreSettings();
48     else{
49          QSettings tempSettings(settingsFile.fileName(),QSettings::IniFormat);
50          random = tempSettings.value("random").toBool();
51     }
52 }
53
54 void BootScreen::createList()
55 {
56     QWidget *central = new QWidget();
57
58     QVBoxLayout *lay = new QVBoxLayout(central);
59     lay->setSpacing(8);
60     lay->setContentsMargins(0, 0, 0, 15);
61
62     list = new QListWidget();
63
64
65     list->setSelectionMode(QAbstractItemView::MultiSelection);
66
67     QDir dirHildon(hildonWelcome);
68     dirHildon.setFilter(QDir::Files | QDir::NoSymLinks);
69     dirHildon.setSorting(QDir::Name);
70
71     QFileInfoList ls = dirHildon.entryInfoList();
72     for (int i = 0; i < ls.size(); ++i) {
73         QFileInfo fileInfo = ls.at(i);
74         QSettings tempSettings(fileInfo.absoluteFilePath(),QSettings::IniFormat);
75         tempSettings.beginGroup("hildon-welcome");
76
77         QFileInfo prettyName(tempSettings.value("filename").toString());
78         list->addItem(QString("%1").arg(prettyName.fileName()));
79         mediaFiles[prettyName.fileName()] = tempSettings.value("filename").toString();
80
81
82         qDebug() << fileInfo.absoluteFilePath() << endl;
83         qDebug() << tempSettings.value("filename") << endl;
84     }
85
86
87     QListWidgetItem *temp;
88     int listSize = list->count();
89     //activate items
90     for(int i = 0; i < listSize; i++){
91         temp = list->item(i);
92         if(temp)
93             temp->setSelected(true);
94     }
95
96     if(random)
97         list->setSelectionMode(QAbstractItemView::NoSelection);
98
99     //Add hands video
100
101     if(!mediaFiles.contains("Hands-v32-h264.avi")){
102         QFileInfo handsTmp("/usr/share/hildon-welcome/media/Hands-v32-h264.avi");
103         if(handsTmp.exists()){
104             mediaFiles[handsTmp.fileName()] = handsTmp.absoluteFilePath();
105             list->addItem(QString("%1").arg(handsTmp.fileName()));
106         }
107     }
108
109     QDir dir(videosDir);
110     dir.setFilter(QDir::Files | QDir::NoSymLinks);
111     dir.setSorting(QDir::Name);
112     //if empty show warning to add a file via menu
113
114     ls = dir.entryInfoList();
115     for (int i = 0; i < ls.size(); ++i) {
116         QFileInfo fileInfo = ls.at(i);
117         if(!mediaFiles.contains(fileInfo.fileName())){
118             mediaFiles[fileInfo.fileName()] = fileInfo.absoluteFilePath();
119             list->addItem(QString("%1").arg(fileInfo.fileName()));
120         }
121         qDebug() << fileInfo.absoluteFilePath() << endl;
122
123     }
124
125    // for(int i =0; i < 8; i++){
126      //  list->addItem("bbr");
127     //}
128
129     lay->addWidget(list);
130
131     QHBoxLayout *layButtons = new QHBoxLayout();
132     up = new QPushButton("Up");
133     down = new QPushButton("Down");
134     save = new QPushButton("Save");
135     save->setEnabled(false);
136
137     if(random){
138         up->setEnabled(false);
139         down->setEnabled(false);
140     }
141
142     layButtons->addWidget(up);
143     layButtons->addWidget(down);
144     layButtons->addSpacing(50);
145     layButtons->addWidget(save);
146     lay->addLayout(layButtons);
147
148     connect(up, SIGNAL(clicked()), this, SLOT(moveUp()));
149     connect(down, SIGNAL(clicked()), this, SLOT(moveDown()));
150     connect(save, SIGNAL(clicked()), this, SLOT(saveConfs()));
151     connect(list, SIGNAL(itemActivated(QListWidgetItem*)), this, SLOT(setPendingChanges(QListWidgetItem*)));
152
153     setCentralWidget(central);
154 }
155
156 void BootScreen::addVideo()
157 {
158     QString filePath = QFileDialog::getOpenFileName(this,
159         tr("Open file"), "/home/user/MyDocs", tr("Video Files (*.avi)"));
160     qDebug() << filePath << endl;
161
162     if(filePath != ""){
163         QFile fileCopy(filePath);
164         QFileInfo fileInfo(filePath);
165         QString fileName = fileInfo.fileName();
166         //boot player can't read files with spaces in the name
167         fileName.replace(QString(" "), QString("_"));
168         QString newFilePath = videosDir + fileName;
169         qDebug() << newFilePath << endl;
170         qDebug() << fileInfo.size() << endl;
171         if(fileInfo.size() > 11100000)
172             QMaemo5InformationBox::information(this, "<font color=black><b>Error:</b> The selected file is too big</font>",
173                                                         QMaemo5InformationBox::DefaultTimeout);
174
175         else{
176             QFile tempTest(newFilePath);
177             if(tempTest.exists())
178                 QMaemo5InformationBox::information(this, "<font color=black><b>Error:</b> The file already exists</font>",
179                                                             QMaemo5InformationBox::DefaultTimeout);
180             else{
181                 bool result = fileCopy.copy(newFilePath);
182                 if(result){
183                     list->addItem(QString("%1").arg(fileName));
184                     save->setEnabled(true);
185                     //autoselect item, if not in random mode
186                     if(!random){
187                         QListWidgetItem *currentItem = list->item(list->count()-1);
188                         currentItem->setSelected(true);
189                     }
190                 }
191                 else
192                     QMaemo5InformationBox::information(this, "<font color=black>Error copying file</font>",
193                                                                 QMaemo5InformationBox::DefaultTimeout);
194             }
195         }
196     }
197
198 }
199
200 void BootScreen::removeVideos()
201 {
202     QListWidgetItem *temp;
203     QList <QListWidgetItem *> selectedItems = list->selectedItems();
204     if(selectedItems.size()>0){
205         QDir vidDir(videosDir);
206         int ret = QMessageBox::information(this, tr("Remove files"), tr("Remove selected file(s) ?"), QMessageBox::Yes | QMessageBox::No);
207
208         if(ret == QMessageBox::Yes){
209             for(int i=0; i< selectedItems.size();i++){
210                 temp = selectedItems.at(i);
211                 bool result = vidDir.remove(temp->text());
212                 qDebug() << result << " :" << temp->text() << endl;
213
214                 if(result){
215                     int rw = list->row(temp);
216                     temp = list->takeItem(rw);
217                     delete temp;
218                 }
219             }
220             save->setEnabled(true);
221         }
222     }
223
224 }
225
226 void BootScreen::showAbout()
227 {
228     QString aboutText = "Written by Valerio Valerio <vdv100@gmail.com\n";
229     aboutText += "Icon by Claes Norin <claes.norin@gmail.com>\n\n";
230     QMessageBox::information(this, "BootScreen Manager", aboutText);
231 }
232
233 void BootScreen::moveUp()
234 {
235     int currentRow = list->currentRow();
236     if (currentRow == 0) 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::moveDown()
244 {
245     int currentRow = list->currentRow();
246     if (currentRow >= list->count()-1) return;
247     QListWidgetItem * currentItem = list->takeItem(currentRow);
248     list->insertItem(currentRow + 1, currentItem);
249     list->setCurrentRow(currentRow + 1);
250     save->setEnabled(true);
251 }
252
253 void BootScreen::disableSelection(bool state)
254 {
255     if(state){
256         up->setEnabled(false);
257         down->setEnabled(false);
258         QListWidgetItem *temp;
259         int listSize = list->count();
260         //disable items
261         QTime fixTime(0, 0, 0);
262         qsrand(fixTime.secsTo(QTime::currentTime()));
263         int randomNumb = qrand() % listSize;
264         qDebug() << randomNumb << endl;
265         for(int i = 0; i < listSize; i++){
266             temp = list->item(i);
267             if(temp){
268                 if(i == randomNumb)
269                     temp->setSelected(true);
270                 else
271                     temp->setSelected(false);
272             }
273         }
274         list->setSelectionMode(QAbstractItemView::NoSelection);
275         QMaemo5InformationBox::information(this, "<font color=black><b>Random mode:</b> Selection disabled</font>",
276                                                     QMaemo5InformationBox::DefaultTimeout);
277     }
278
279     random = true;
280     save->setEnabled(true);
281 }
282
283 void BootScreen::selectMultiple(bool state)
284 {
285     if(state){
286         up->setEnabled(true);
287         down->setEnabled(true);
288         list->setSelectionMode(QAbstractItemView::MultiSelection);
289         random = false;
290     }
291 }
292
293 void BootScreen::writeSettings()
294 {
295     QSettings settings(settingsFile.fileName(),QSettings::IniFormat);
296     settings.setValue("random", random);
297 }
298
299 void BootScreen::restoreSettings()
300 {
301      qDebug() << settingsFile.fileName() << endl;
302
303      if (!settingsFile.open(QIODevice::ReadWrite | QIODevice::Text)){
304         qWarning("Cannot create the settings file"); //abord
305         QMaemo5InformationBox::information(this, "<font color=black><b>Error:</b> Cannot create the settings file</font>",
306                                                     QMaemo5InformationBox::DefaultTimeout);
307      }
308
309      QTextStream out(&settingsFile);
310      out << "random=false" << endl;
311
312      random = false;
313 }
314
315 void BootScreen::saveConfs(){
316
317     //remove old files
318     QDir dir(hildonWelcome);
319     dir.setFilter(QDir::Files);
320
321     QFileInfoList ls = dir.entryInfoList();
322     for (int i = 0; i < ls.size(); ++i) {
323         QFileInfo fileInfo = ls.at(i);
324         bool result = dir.remove(fileInfo.fileName());
325         qDebug() << result << " :" << fileInfo.fileName() << endl;
326     }
327
328     QListWidgetItem *temp;
329     for(int i=0; i< list->count();i++){
330         temp = list->item(i);
331         if(temp->isSelected())
332             if(random)
333                  createFile(temp->text(), 0);
334             else
335                 createFile(temp->text(), i);
336     }
337     save->setEnabled(false);
338 }
339
340 void BootScreen::createFile(QString filename, int index)
341 {
342     QString ind = QString::number(index);
343     if(mediaFiles.contains(filename)){
344         QFile confFile(hildonWelcome + ind + ".conf");
345         qDebug() << confFile.fileName() << endl;
346         if (!confFile.open(QIODevice::ReadWrite | QIODevice::Text)){
347            qWarning("Cannot create the settings file"); //abord
348            QMaemo5InformationBox::information(this, "<font color=black><b>Error:</b> Cannot create the configurations file</font>",
349                                                        QMaemo5InformationBox::DefaultTimeout);
350        }
351         else{
352             QString filePath = mediaFiles.value(filename);
353             QTextStream out(&confFile);
354             out << "[hildon-welcome]" << endl;
355             out << "filename=" << filePath << endl;
356         }
357     }
358     else{
359         qDebug() << filename << endl;
360         qDebug() << "File already exists" << endl;
361     }
362 }
363
364 void BootScreen::setPendingChanges(QListWidgetItem *item)
365 {
366     if(!random)
367         save->setEnabled(true);
368 }
369
370 void BootScreen::closeEvent(QCloseEvent *event)
371 {
372     if(save->isEnabled()){
373         int ret = QMessageBox::information(this, tr("There are pending changes"), tr("Do you want to save your changes?"),
374                                            QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
375
376         if(ret == QMessageBox::Save){
377             saveConfs();
378             writeSettings();
379             event->accept();
380
381         }
382         else if(ret == QMessageBox::Discard){
383             writeSettings();
384             event->accept();
385         }
386         else{
387             event->ignore();
388         }
389     }
390     else{
391         writeSettings();
392         event->accept();
393     }
394 }
395
396 BootScreen::~BootScreen()
397 {
398 }
399
400