1 #include "bootscreen.h"
5 BootScreen::BootScreen(QWidget *parent)
8 hildonWelcome = "/etc/hildon-welcome.d/";
9 videosDir = "/opt/bootscreen/media/";
10 settingsDir = "/opt/bootscreen/data/";
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);
22 mRandom->setChecked(true);
24 mMultiple->setChecked(true);
27 connect(mRandom, SIGNAL(toggled(bool)), this, SLOT(disableSelection(bool)));
28 connect(mMultiple, SIGNAL(toggled(bool)), this, SLOT(selectMultiple(bool)));
30 menuBar()->addActions(filterGroup->actions());
32 menuBar()->addAction(tr("Add"), this, SLOT(addVideo()));
33 menuBar()->addAction(tr("Remove"), this, SLOT(removeVideos()));
38 void BootScreen::checkSettings()
40 QDir dir(settingsDir);
41 settingsFile.setFileName(dir.filePath("bootscrenn.conf"));
43 if (!settingsFile.exists())
46 QSettings tempSettings(settingsFile.fileName(),QSettings::IniFormat);
47 random = tempSettings.value("random").toBool();
51 void BootScreen::createList()
53 QWidget *central = new QWidget();
55 QVBoxLayout *lay = new QVBoxLayout(central);
57 lay->setContentsMargins(0, 0, 0, 15);
59 list = new QListWidget();
62 list->setSelectionMode(QAbstractItemView::MultiSelection);
64 QDir dirHildon(hildonWelcome);
65 dirHildon.setFilter(QDir::Files | QDir::NoSymLinks);
66 dirHildon.setSorting(QDir::Name);
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");
74 QFileInfo prettyName(tempSettings.value("filename").toString());
75 list->addItem(QString("%1").arg(prettyName.fileName()));
76 mediaFiles[prettyName.fileName()] = tempSettings.value("filename").toString();
79 qDebug() << fileInfo.absoluteFilePath() << endl;
80 qDebug() << tempSettings.value("filename") << endl;
84 QListWidgetItem *temp;
85 int listSize = list->count();
87 for(int i = 0; i < listSize; i++){
90 temp->setSelected(true);
94 list->setSelectionMode(QAbstractItemView::NoSelection);
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()));
107 dir.setFilter(QDir::Files | QDir::NoSymLinks);
108 dir.setSorting(QDir::Name);
109 //if empty show warning to add a file via menu
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()));
118 qDebug() << fileInfo.absoluteFilePath() << endl;
122 // for(int i =0; i < 8; i++){
123 // list->addItem("bbr");
126 lay->addWidget(list);
128 QHBoxLayout *layButtons = new QHBoxLayout();
129 up = new QPushButton("Up");
130 down = new QPushButton("Down");
133 up->setEnabled(false);
134 down->setEnabled(false);
137 layButtons->addWidget(up);
138 layButtons->addWidget(down);
139 lay->addLayout(layButtons);
141 connect(up, SIGNAL(clicked()), this, SLOT(moveUp()));
142 connect(down, SIGNAL(clicked()), this, SLOT(moveDown()));
144 setCentralWidget(central);
147 void BootScreen::addVideo()
149 QString filePath = QFileDialog::getOpenFileName(this,
150 tr("Open file"), "/home/user/MyDocs", tr("Video Files (*.avi)"));
151 qDebug() << filePath << endl;
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);
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);
172 bool result = fileCopy.copy(newFilePath);
174 list->addItem(QString("%1").arg(fileName));
175 //autoselect item, if not in random mode
177 QListWidgetItem *currentItem = list->item(list->count()-1);
178 currentItem->setSelected(true);
182 QMaemo5InformationBox::information(this, "<font color=black>Error copying file</font>",
183 QMaemo5InformationBox::DefaultTimeout);
190 void BootScreen::removeVideos()
192 QListWidgetItem *temp;
193 QList <QListWidgetItem *> selectedItems = list->selectedItems();
194 if(selectedItems.size()>0){
195 QDir vidDir(videosDir);
197 int ret = QMessageBox::information(this, tr("Remove files"), tr("Remove selected file(s) ?"), QMessageBox::Yes | QMessageBox::No);
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;
206 int rw = list->row(temp);
207 temp = list->takeItem(rw);
216 void BootScreen::moveUp()
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);
225 void BootScreen::moveDown()
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);
234 void BootScreen::disableSelection(bool state)
237 up->setEnabled(false);
238 down->setEnabled(false);
239 QListWidgetItem *temp;
240 int listSize = list->count();
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);
250 temp->setSelected(true);
252 temp->setSelected(false);
255 list->setSelectionMode(QAbstractItemView::NoSelection);
256 QMaemo5InformationBox::information(this, "<font color=black><b>Random mode:</b> Selection disabled</font>",
257 QMaemo5InformationBox::DefaultTimeout);
263 void BootScreen::selectMultiple(bool state)
266 up->setEnabled(true);
267 down->setEnabled(true);
268 list->setSelectionMode(QAbstractItemView::MultiSelection);
273 void BootScreen::writeSettings()
275 QSettings settings(settingsFile.fileName(),QSettings::IniFormat);
276 settings.setValue("random", random);
279 void BootScreen::restoreSettings()
281 qDebug() << settingsFile.fileName() << endl;
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);
289 QTextStream out(&settingsFile);
290 out << "random=false" << endl;
295 void BootScreen::writeFiles(){
298 QDir dir(hildonWelcome);
299 dir.setFilter(QDir::Files);
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;
308 QListWidgetItem *temp;
309 for(int i=0; i< list->count();i++){
310 temp = list->item(i);
311 if(temp->isSelected())
313 createFile(temp->text(), 0);
315 createFile(temp->text(), i);
319 void BootScreen::createFile(QString filename, int index)
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
329 QString filePath = mediaFiles.value(filename);
330 QTextStream out(&confFile);
331 out << "[hildon-welcome]" << endl;
332 out << "filename=" << filePath << endl;
336 qDebug() << filename << endl;
337 qDebug() << "print a error" << endl;
341 void BootScreen::closeEvent(QCloseEvent *event)
348 BootScreen::~BootScreen()