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 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);
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);
169 bool result = fileCopy.copy(newFilePath);
171 list->addItem(QString("%1").arg(fileInfo.fileName()));
172 //autoselect item, if not in random mode
174 QListWidgetItem *currentItem = list->item(list->count()-1);
175 currentItem->setSelected(true);
179 QMaemo5InformationBox::information(this, "<font color=black>Error copying file</font>",
180 QMaemo5InformationBox::DefaultTimeout);
187 void BootScreen::removeVideos()
189 QListWidgetItem *temp;
190 QList <QListWidgetItem *> selectedItems = list->selectedItems();
191 if(selectedItems.size()>0){
192 QDir vidDir(videosDir);
194 int ret = QMessageBox::information(this, tr("Remove files"), tr("Remove selected file(s) ?"), QMessageBox::Yes | QMessageBox::No);
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;
203 int rw = list->row(temp);
204 temp = list->takeItem(rw);
213 void BootScreen::moveUp()
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);
222 void BootScreen::moveDown()
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);
231 void BootScreen::disableSelection(bool state)
234 up->setEnabled(false);
235 down->setEnabled(false);
236 QListWidgetItem *temp;
237 int listSize = list->count();
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);
247 temp->setSelected(true);
249 temp->setSelected(false);
252 list->setSelectionMode(QAbstractItemView::NoSelection);
253 QMaemo5InformationBox::information(this, "<font color=black><b>Random mode:</b> Selection disabled</font>",
254 QMaemo5InformationBox::DefaultTimeout);
260 void BootScreen::selectMultiple(bool state)
263 up->setEnabled(true);
264 down->setEnabled(true);
265 list->setSelectionMode(QAbstractItemView::MultiSelection);
270 void BootScreen::writeSettings()
272 QSettings settings(settingsFile.fileName(),QSettings::IniFormat);
273 settings.setValue("random", random);
276 void BootScreen::restoreSettings()
278 qDebug() << settingsFile.fileName() << endl;
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);
286 QTextStream out(&settingsFile);
287 out << "random=false" << endl;
292 void BootScreen::writeFiles(){
295 QDir dir(hildonWelcome);
296 dir.setFilter(QDir::Files);
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;
305 QListWidgetItem *temp;
306 for(int i=0; i< list->count();i++){
307 temp = list->item(i);
308 if(temp->isSelected())
310 createFile(temp->text(), 0);
312 createFile(temp->text(), i);
316 void BootScreen::createFile(QString filename, int index)
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
326 QString filePath = mediaFiles.value(filename);
327 QTextStream out(&confFile);
328 out << "[hildon-welcome]" << endl;
329 out << "filename=" << filePath << endl;
333 qDebug() << "print a error" << endl;
336 void BootScreen::closeEvent(QCloseEvent *event)
343 BootScreen::~BootScreen()