Added save button
[bootcreen] / bootscreen.cpp
index fca329f..0f9fd97 100644 (file)
@@ -7,6 +7,7 @@ BootScreen::BootScreen(QWidget *parent)
 {
     hildonWelcome = "/etc/hildon-welcome.d/";
     videosDir = "/opt/bootscreen/media/";
+    settingsDir = "/opt/bootscreen/data/";
     checkSettings();
 
     filterGroup = new QActionGroup(this);
@@ -36,23 +37,15 @@ BootScreen::BootScreen(QWidget *parent)
 
 void BootScreen::checkSettings()
 {
-    QDir dir("/home/user");
-    if (!dir.cd(".bootscreen")) {
-         qWarning("Cannot find the \".bootscreen/\" directory");
-         dir.mkdir(".bootscreen");
-         if (!dir.cd(".bootscreen")) {
-              qWarning("Cannot create the \".bootscreen/\" directory");
-
-              //terminar applicacao
-          }
-     }
-     settingsFile.setFileName(dir.filePath("settings.ini"));
-     if (!settingsFile.exists())
+    QDir dir(settingsDir);
+    settingsFile.setFileName(dir.filePath("bootscreen.conf"));
+
+    if (!settingsFile.exists())
         restoreSettings();
-     else{
+    else{
          QSettings tempSettings(settingsFile.fileName(),QSettings::IniFormat);
          random = tempSettings.value("random").toBool();
-     }
+    }
 }
 
 void BootScreen::createList()
@@ -135,6 +128,8 @@ void BootScreen::createList()
     QHBoxLayout *layButtons = new QHBoxLayout();
     up = new QPushButton("Up");
     down = new QPushButton("Down");
+    save = new QPushButton("Save");
+    save->setEnabled(false);
 
     if(random){
         up->setEnabled(false);
@@ -143,10 +138,14 @@ void BootScreen::createList()
 
     layButtons->addWidget(up);
     layButtons->addWidget(down);
+    layButtons->addSpacing(50);
+    layButtons->addWidget(save);
     lay->addLayout(layButtons);
 
     connect(up, SIGNAL(clicked()), this, SLOT(moveUp()));
     connect(down, SIGNAL(clicked()), this, SLOT(moveDown()));
+    connect(save, SIGNAL(clicked()), this, SLOT(saveConfs()));
+    connect(list, SIGNAL(itemActivated(QListWidgetItem*)), this, SLOT(setPendingChanges(QListWidgetItem*)));
 
     setCentralWidget(central);
 }
@@ -160,7 +159,10 @@ void BootScreen::addVideo()
     if(filePath != ""){
         QFile fileCopy(filePath);
         QFileInfo fileInfo(filePath);
-        QString newFilePath = videosDir + fileInfo.fileName();
+        QString fileName = fileInfo.fileName();
+        //boot player can't read files with spaces in the name
+        fileName.replace(QString(" "), QString("_"));
+        QString newFilePath = videosDir + fileName;
         qDebug() << newFilePath << endl;
         qDebug() << fileInfo.size() << endl;
         if(fileInfo.size() > 11100000)
@@ -174,16 +176,20 @@ void BootScreen::addVideo()
                                                             QMaemo5InformationBox::DefaultTimeout);
             else{
                 bool result = fileCopy.copy(newFilePath);
-            // tamanho
-                if(result)
-                    list->addItem(QString("%1").arg(fileInfo.fileName()));
+                if(result){
+                    list->addItem(QString("%1").arg(fileName));
+                    save->setEnabled(true);
+                    //autoselect item, if not in random mode
+                    if(!random){
+                        QListWidgetItem *currentItem = list->item(list->count()-1);
+                        currentItem->setSelected(true);
+                    }
+                }
                 else
                     QMaemo5InformationBox::information(this, "<font color=black>Error copying file</font>",
                                                                 QMaemo5InformationBox::DefaultTimeout);
             }
         }
-
-        //add somewhere
     }
 
 }
@@ -194,7 +200,6 @@ void BootScreen::removeVideos()
     QList <QListWidgetItem *> selectedItems = list->selectedItems();
     if(selectedItems.size()>0){
         QDir vidDir(videosDir);
-        QMessageBox msgBox;
         int ret = QMessageBox::information(this, tr("Remove files"), tr("Remove selected file(s) ?"), QMessageBox::Yes | QMessageBox::No);
 
         if(ret == QMessageBox::Yes){
@@ -209,6 +214,7 @@ void BootScreen::removeVideos()
                     delete temp;
                 }
             }
+            save->setEnabled(true);
         }
     }
 
@@ -221,6 +227,7 @@ void BootScreen::moveUp()
     QListWidgetItem * currentItem = list->takeItem(currentRow);
     list->insertItem(currentRow - 1, currentItem);
     list->setCurrentRow(currentRow - 1);
+    save->setEnabled(true);
 }
 
 void BootScreen::moveDown()
@@ -230,6 +237,7 @@ void BootScreen::moveDown()
     QListWidgetItem * currentItem = list->takeItem(currentRow);
     list->insertItem(currentRow + 1, currentItem);
     list->setCurrentRow(currentRow + 1);
+    save->setEnabled(true);
 }
 
 void BootScreen::disableSelection(bool state)
@@ -259,6 +267,7 @@ void BootScreen::disableSelection(bool state)
     }
 
     random = true;
+    save->setEnabled(true);
 }
 
 void BootScreen::selectMultiple(bool state)
@@ -281,8 +290,11 @@ void BootScreen::restoreSettings()
 {
      qDebug() << settingsFile.fileName() << endl;
 
-     if (!settingsFile.open(QIODevice::ReadWrite | QIODevice::Text))
+     if (!settingsFile.open(QIODevice::ReadWrite | QIODevice::Text)){
         qWarning("Cannot create the settings file"); //abord
+        QMaemo5InformationBox::information(this, "<font color=black><b>Error:</b> Cannot create the settings file</font>",
+                                                    QMaemo5InformationBox::DefaultTimeout);
+     }
 
      QTextStream out(&settingsFile);
      out << "random=false" << endl;
@@ -290,7 +302,7 @@ void BootScreen::restoreSettings()
      random = false;
 }
 
-void BootScreen::writeFiles(){
+void BootScreen::saveConfs(){
 
     //remove old files
     QDir dir(hildonWelcome);
@@ -312,6 +324,7 @@ void BootScreen::writeFiles(){
             else
                 createFile(temp->text(), i);
     }
+    save->setEnabled(false);
 }
 
 void BootScreen::createFile(QString filename, int index)
@@ -320,9 +333,11 @@ void BootScreen::createFile(QString filename, int index)
     if(mediaFiles.contains(filename)){
         QFile confFile(hildonWelcome + ind + ".conf");
         qDebug() << confFile.fileName() << endl;
-        if (!confFile.open(QIODevice::ReadWrite | QIODevice::Text))
+        if (!confFile.open(QIODevice::ReadWrite | QIODevice::Text)){
            qWarning("Cannot create the settings file"); //abord
-
+           QMaemo5InformationBox::information(this, "<font color=black><b>Error:</b> Cannot create the configurations file</font>",
+                                                       QMaemo5InformationBox::DefaultTimeout);
+       }
         else{
             QString filePath = mediaFiles.value(filename);
             QTextStream out(&confFile);
@@ -330,15 +345,42 @@ void BootScreen::createFile(QString filename, int index)
             out << "filename=" << filePath << endl;
         }
     }
-    else
-        qDebug() << "print a error" << endl;
+    else{
+        qDebug() << filename << endl;
+        qDebug() << "File already exists" << endl;
+    }
+}
+
+void BootScreen::setPendingChanges(QListWidgetItem *item)
+{
+    if(!random)
+        save->setEnabled(true);
 }
 
 void BootScreen::closeEvent(QCloseEvent *event)
 {
-    writeSettings();
-    writeFiles();
-    event->accept();
+    if(save->isEnabled()){
+        int ret = QMessageBox::information(this, tr("There are pending changes"), tr("Do you want to save your changes?"),
+                                           QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
+
+        if(ret == QMessageBox::Save){
+            saveConfs();
+            writeSettings();
+            event->accept();
+
+        }
+        else if(ret == QMessageBox::Discard){
+            writeSettings();
+            event->accept();
+        }
+        else{
+            event->ignore();
+        }
+    }
+    else{
+        writeSettings();
+        event->accept();
+    }
 }
 
 BootScreen::~BootScreen()