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()));
34 menuBar()->addAction(tr("About"), this, SLOT(showAbout()));
36 setWindowTitle("BootScreen Manager");
41 void BootScreen::checkSettings()
43 QDir dir(settingsDir);
44 settingsFile.setFileName(dir.filePath("bootscreen.conf"));
46 if (!settingsFile.exists())
49 QSettings tempSettings(settingsFile.fileName(),QSettings::IniFormat);
50 random = tempSettings.value("random").toBool();
54 void BootScreen::createList()
56 QWidget *central = new QWidget();
58 QVBoxLayout *lay = new QVBoxLayout(central);
60 lay->setContentsMargins(0, 0, 0, 15);
62 list = new QListWidget();
65 list->setSelectionMode(QAbstractItemView::MultiSelection);
67 QDir dirHildon(hildonWelcome);
68 dirHildon.setFilter(QDir::Files | QDir::NoSymLinks);
69 dirHildon.setSorting(QDir::Name);
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");
77 QFileInfo prettyName(tempSettings.value("filename").toString());
78 list->addItem(QString("%1").arg(prettyName.fileName()));
79 mediaFiles[prettyName.fileName()] = tempSettings.value("filename").toString();
82 qDebug() << fileInfo.absoluteFilePath() << endl;
83 qDebug() << tempSettings.value("filename") << endl;
87 QListWidgetItem *temp;
88 int listSize = list->count();
90 for(int i = 0; i < listSize; i++){
93 temp->setSelected(true);
97 list->setSelectionMode(QAbstractItemView::NoSelection);
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()));
110 dir.setFilter(QDir::Files | QDir::NoSymLinks);
111 dir.setSorting(QDir::Name);
112 //if empty show warning to add a file via menu
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()));
121 qDebug() << fileInfo.absoluteFilePath() << endl;
125 // for(int i =0; i < 8; i++){
126 // list->addItem("bbr");
129 lay->addWidget(list);
131 QHBoxLayout *layButtons = new QHBoxLayout();
132 up = new QPushButton("Up");
133 down = new QPushButton("Down");
134 save = new QPushButton("Save");
135 save->setEnabled(false);
138 up->setEnabled(false);
139 down->setEnabled(false);
142 layButtons->addWidget(up);
143 layButtons->addWidget(down);
144 layButtons->addSpacing(50);
145 layButtons->addWidget(save);
146 lay->addLayout(layButtons);
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*)));
153 setCentralWidget(central);
156 void BootScreen::addVideo()
158 QString filePath = QFileDialog::getOpenFileName(this,
159 tr("Open file"), "/home/user/MyDocs", tr("Video Files (*.avi)"));
160 qDebug() << filePath << endl;
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);
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);
181 bool result = fileCopy.copy(newFilePath);
183 list->addItem(QString("%1").arg(fileName));
184 save->setEnabled(true);
185 //autoselect item, if not in random mode
187 QListWidgetItem *currentItem = list->item(list->count()-1);
188 currentItem->setSelected(true);
192 QMaemo5InformationBox::information(this, "<font color=black>Error copying file</font>",
193 QMaemo5InformationBox::DefaultTimeout);
200 void BootScreen::removeVideos()
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);
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;
215 int rw = list->row(temp);
216 temp = list->takeItem(rw);
220 save->setEnabled(true);
226 void BootScreen::showAbout()
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);
233 void BootScreen::moveUp()
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);
243 void BootScreen::moveDown()
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);
253 void BootScreen::disableSelection(bool state)
256 up->setEnabled(false);
257 down->setEnabled(false);
258 QListWidgetItem *temp;
259 int listSize = list->count();
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);
269 temp->setSelected(true);
271 temp->setSelected(false);
274 list->setSelectionMode(QAbstractItemView::NoSelection);
275 QMaemo5InformationBox::information(this, "<font color=black><b>Random mode:</b> Selection disabled</font>",
276 QMaemo5InformationBox::DefaultTimeout);
280 save->setEnabled(true);
283 void BootScreen::selectMultiple(bool state)
286 up->setEnabled(true);
287 down->setEnabled(true);
288 list->setSelectionMode(QAbstractItemView::MultiSelection);
293 void BootScreen::writeSettings()
295 QSettings settings(settingsFile.fileName(),QSettings::IniFormat);
296 settings.setValue("random", random);
299 void BootScreen::restoreSettings()
301 qDebug() << settingsFile.fileName() << endl;
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);
309 QTextStream out(&settingsFile);
310 out << "random=false" << endl;
315 void BootScreen::saveConfs(){
318 QDir dir(hildonWelcome);
319 dir.setFilter(QDir::Files);
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;
328 QListWidgetItem *temp;
329 for(int i=0; i< list->count();i++){
330 temp = list->item(i);
331 if(temp->isSelected())
333 createFile(temp->text(), 0);
335 createFile(temp->text(), i);
337 save->setEnabled(false);
340 void BootScreen::createFile(QString filename, int index)
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);
352 QString filePath = mediaFiles.value(filename);
353 QTextStream out(&confFile);
354 out << "[hildon-welcome]" << endl;
355 out << "filename=" << filePath << endl;
359 qDebug() << filename << endl;
360 qDebug() << "File already exists" << endl;
364 void BootScreen::setPendingChanges(QListWidgetItem *item)
367 save->setEnabled(true);
370 void BootScreen::closeEvent(QCloseEvent *event)
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);
376 if(ret == QMessageBox::Save){
382 else if(ret == QMessageBox::Discard){
396 BootScreen::~BootScreen()