1 #include "bootscreen.h"
5 BootScreen::BootScreen(QWidget *parent)
8 hildonWelcome = "/etc/hildon-welcome.d/";
9 videosDir = "/opt/bootscreen/media/";
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);
21 mRandom->setChecked(true);
23 mMultiple->setChecked(true);
26 connect(mRandom, SIGNAL(toggled(bool)), this, SLOT(disableSelection(bool)));
27 connect(mMultiple, SIGNAL(toggled(bool)), this, SLOT(selectMultiple(bool)));
29 menuBar()->addActions(filterGroup->actions());
31 menuBar()->addAction(tr("Add"), this, SLOT(addVideo()));
32 menuBar()->addAction(tr("Remove"), this, SLOT(removeVideos()));
37 void BootScreen::checkSettings()
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");
49 settingsFile.setFileName(dir.filePath("settings.ini"));
50 if (!settingsFile.exists())
53 QSettings tempSettings(settingsFile.fileName(),QSettings::IniFormat);
54 random = tempSettings.value("random").toBool();
58 void BootScreen::createList()
60 QWidget *central = new QWidget();
62 QVBoxLayout *lay = new QVBoxLayout(central);
64 lay->setContentsMargins(0, 0, 0, 15);
66 list = new QListWidget();
69 list->setSelectionMode(QAbstractItemView::MultiSelection);
71 QDir dirHildon(hildonWelcome);
72 dirHildon.setFilter(QDir::Files | QDir::NoSymLinks);
73 dirHildon.setSorting(QDir::Name);
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");
81 QFileInfo prettyName(tempSettings.value("filename").toString());
82 list->addItem(QString("%1").arg(prettyName.fileName()));
83 mediaFiles[prettyName.fileName()] = tempSettings.value("filename").toString();
86 qDebug() << fileInfo.absoluteFilePath() << endl;
87 qDebug() << tempSettings.value("filename") << endl;
91 QListWidgetItem *temp;
92 int listSize = list->count();
94 for(int i = 0; i < listSize; i++){
97 temp->setSelected(true);
101 list->setSelectionMode(QAbstractItemView::NoSelection);
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()));
114 dir.setFilter(QDir::Files | QDir::NoSymLinks);
115 dir.setSorting(QDir::Name);
116 //if empty show warning to add a file via menu
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()));
125 qDebug() << fileInfo.absoluteFilePath() << endl;
129 // for(int i =0; i < 8; i++){
130 // list->addItem("bbr");
133 lay->addWidget(list);
135 QHBoxLayout *layButtons = new QHBoxLayout();
136 up = new QPushButton("Up");
137 down = new QPushButton("Down");
140 up->setEnabled(false);
141 down->setEnabled(false);
144 layButtons->addWidget(up);
145 layButtons->addWidget(down);
146 lay->addLayout(layButtons);
148 connect(up, SIGNAL(clicked()), this, SLOT(moveUp()));
149 connect(down, SIGNAL(clicked()), this, SLOT(moveDown()));
151 setCentralWidget(central);
154 void BootScreen::addVideo()
156 QString filePath = QFileDialog::getOpenFileName(this,
157 tr("Open file"), "/home/user/MyDocs", tr("Video Files (*.avi)"));
158 qDebug() << filePath << endl;
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);
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);
176 bool result = fileCopy.copy(newFilePath);
179 list->addItem(QString("%1").arg(fileInfo.fileName()));
181 QMaemo5InformationBox::information(this, "<font color=black>Error copying file</font>",
182 QMaemo5InformationBox::DefaultTimeout);
191 void BootScreen::removeVideos()
193 QListWidgetItem *temp;
194 QList <QListWidgetItem *> selectedItems = list->selectedItems();
195 if(selectedItems.size()>0){
196 QDir vidDir(videosDir);
198 int ret = QMessageBox::information(this, tr("Remove files"), tr("Remove selected file(s) ?"), QMessageBox::Yes | QMessageBox::No);
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;
207 int rw = list->row(temp);
208 temp = list->takeItem(rw);
217 void BootScreen::moveUp()
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);
226 void BootScreen::moveDown()
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);
235 void BootScreen::disableSelection(bool state)
238 up->setEnabled(false);
239 down->setEnabled(false);
240 QListWidgetItem *temp;
241 int listSize = list->count();
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);
251 temp->setSelected(true);
253 temp->setSelected(false);
256 list->setSelectionMode(QAbstractItemView::NoSelection);
257 QMaemo5InformationBox::information(this, "<font color=black><b>Random mode:</b> Selection disabled</font>",
258 QMaemo5InformationBox::DefaultTimeout);
264 void BootScreen::selectMultiple(bool state)
267 up->setEnabled(true);
268 down->setEnabled(true);
269 list->setSelectionMode(QAbstractItemView::MultiSelection);
274 void BootScreen::writeSettings()
276 QSettings settings(settingsFile.fileName(),QSettings::IniFormat);
277 settings.setValue("random", random);
280 void BootScreen::restoreSettings()
282 qDebug() << settingsFile.fileName() << endl;
284 if (!settingsFile.open(QIODevice::ReadWrite | QIODevice::Text))
285 qWarning("Cannot create the settings file"); //abord
287 QTextStream out(&settingsFile);
288 out << "random=false" << endl;
293 void BootScreen::writeFiles(){
296 QDir dir(hildonWelcome);
297 dir.setFilter(QDir::Files);
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;
306 QListWidgetItem *temp;
307 for(int i=0; i< list->count();i++){
308 temp = list->item(i);
309 if(temp->isSelected())
311 createFile(temp->text(), 0);
313 createFile(temp->text(), i);
317 void BootScreen::createFile(QString filename, int index)
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
327 QString filePath = mediaFiles.value(filename);
328 QTextStream out(&confFile);
329 out << "[hildon-welcome]" << endl;
330 out << "filename=" << filePath << endl;
334 qDebug() << "print a error" << endl;
337 void BootScreen::closeEvent(QCloseEvent *event)
344 BootScreen::~BootScreen()