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