Auto-select new added files
[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 newFilePath = videosDir + fileInfo.fileName();
157         qDebug() << newFilePath << endl;
158         qDebug() << fileInfo.size() << endl;
159         if(fileInfo.size() > 11100000)
160             QMaemo5InformationBox::information(this, "<font color=black><b>Error:</b> The selected file is too big</font>",
161                                                         QMaemo5InformationBox::DefaultTimeout);
162
163         else{
164             QFile tempTest(newFilePath);
165             if(tempTest.exists())
166                 QMaemo5InformationBox::information(this, "<font color=black><b>Error:</b> The file already exists</font>",
167                                                             QMaemo5InformationBox::DefaultTimeout);
168             else{
169                 bool result = fileCopy.copy(newFilePath);
170                 if(result){
171                     list->addItem(QString("%1").arg(fileInfo.fileName()));
172                     //autoselect item, if not in random mode
173                     if(!random){
174                         QListWidgetItem *currentItem = list->item(list->count()-1);
175                         currentItem->setSelected(true);
176                     }
177                 }
178                 else
179                     QMaemo5InformationBox::information(this, "<font color=black>Error copying file</font>",
180                                                                 QMaemo5InformationBox::DefaultTimeout);
181             }
182         }
183     }
184
185 }
186
187 void BootScreen::removeVideos()
188 {
189     QListWidgetItem *temp;
190     QList <QListWidgetItem *> selectedItems = list->selectedItems();
191     if(selectedItems.size()>0){
192         QDir vidDir(videosDir);
193         QMessageBox msgBox;
194         int ret = QMessageBox::information(this, tr("Remove files"), tr("Remove selected file(s) ?"), QMessageBox::Yes | QMessageBox::No);
195
196         if(ret == QMessageBox::Yes){
197             for(int i=0; i< selectedItems.size();i++){
198                 temp = selectedItems.at(i);
199                 bool result = vidDir.remove(temp->text());
200                 qDebug() << result << " :" << temp->text() << endl;
201
202                 if(result){
203                     int rw = list->row(temp);
204                     temp = list->takeItem(rw);
205                     delete temp;
206                 }
207             }
208         }
209     }
210
211 }
212
213 void BootScreen::moveUp()
214 {
215     int currentRow = list->currentRow();
216     if (currentRow == 0) return;
217     QListWidgetItem * currentItem = list->takeItem(currentRow);
218     list->insertItem(currentRow - 1, currentItem);
219     list->setCurrentRow(currentRow - 1);
220 }
221
222 void BootScreen::moveDown()
223 {
224     int currentRow = list->currentRow();
225     if (currentRow >= list->count()-1) return;
226     QListWidgetItem * currentItem = list->takeItem(currentRow);
227     list->insertItem(currentRow + 1, currentItem);
228     list->setCurrentRow(currentRow + 1);
229 }
230
231 void BootScreen::disableSelection(bool state)
232 {
233     if(state){
234         up->setEnabled(false);
235         down->setEnabled(false);
236         QListWidgetItem *temp;
237         int listSize = list->count();
238         //disable items
239         QTime fixTime(0, 0, 0);
240         qsrand(fixTime.secsTo(QTime::currentTime()));
241         int randomNumb = qrand() % listSize;
242         qDebug() << randomNumb << endl;
243         for(int i = 0; i < listSize; i++){
244             temp = list->item(i);
245             if(temp){
246                 if(i == randomNumb)
247                     temp->setSelected(true);
248                 else
249                     temp->setSelected(false);
250             }
251         }
252         list->setSelectionMode(QAbstractItemView::NoSelection);
253         QMaemo5InformationBox::information(this, "<font color=black><b>Random mode:</b> Selection disabled</font>",
254                                                     QMaemo5InformationBox::DefaultTimeout);
255     }
256
257     random = true;
258 }
259
260 void BootScreen::selectMultiple(bool state)
261 {
262     if(state){
263         up->setEnabled(true);
264         down->setEnabled(true);
265         list->setSelectionMode(QAbstractItemView::MultiSelection);
266         random = false;
267     }
268 }
269
270 void BootScreen::writeSettings()
271 {
272     QSettings settings(settingsFile.fileName(),QSettings::IniFormat);
273     settings.setValue("random", random);
274 }
275
276 void BootScreen::restoreSettings()
277 {
278      qDebug() << settingsFile.fileName() << endl;
279
280      if (!settingsFile.open(QIODevice::ReadWrite | QIODevice::Text)){
281         qWarning("Cannot create the settings file"); //abord
282         QMaemo5InformationBox::information(this, "<font color=black><b>Error:</b> Cannot create the settings file</font>",
283                                                     QMaemo5InformationBox::DefaultTimeout);
284      }
285
286      QTextStream out(&settingsFile);
287      out << "random=false" << endl;
288
289      random = false;
290 }
291
292 void BootScreen::writeFiles(){
293
294     //remove old files
295     QDir dir(hildonWelcome);
296     dir.setFilter(QDir::Files);
297
298     QFileInfoList ls = dir.entryInfoList();
299     for (int i = 0; i < ls.size(); ++i) {
300         QFileInfo fileInfo = ls.at(i);
301         bool result = dir.remove(fileInfo.fileName());
302         qDebug() << result << " :" << fileInfo.fileName() << endl;
303     }
304
305     QListWidgetItem *temp;
306     for(int i=0; i< list->count();i++){
307         temp = list->item(i);
308         if(temp->isSelected())
309             if(random)
310                  createFile(temp->text(), 0);
311             else
312                 createFile(temp->text(), i);
313     }
314 }
315
316 void BootScreen::createFile(QString filename, int index)
317 {
318     QString ind = QString::number(index);
319     if(mediaFiles.contains(filename)){
320         QFile confFile(hildonWelcome + ind + ".conf");
321         qDebug() << confFile.fileName() << endl;
322         if (!confFile.open(QIODevice::ReadWrite | QIODevice::Text))
323            qWarning("Cannot create the settings file"); //abord
324
325         else{
326             QString filePath = mediaFiles.value(filename);
327             QTextStream out(&confFile);
328             out << "[hildon-welcome]" << endl;
329             out << "filename=" << filePath << endl;
330         }
331     }
332     else
333         qDebug() << "print a error" << endl;
334 }
335
336 void BootScreen::closeEvent(QCloseEvent *event)
337 {
338     writeSettings();
339     writeFiles();
340     event->accept();
341 }
342
343 BootScreen::~BootScreen()
344 {
345 }
346
347