Added save button
[bootcreen] / bootscreen.cpp
index d80a8ae..0f9fd97 100644 (file)
@@ -38,7 +38,7 @@ BootScreen::BootScreen(QWidget *parent)
 void BootScreen::checkSettings()
 {
     QDir dir(settingsDir);
-    settingsFile.setFileName(dir.filePath("bootscrenn.conf"));
+    settingsFile.setFileName(dir.filePath("bootscreen.conf"));
 
     if (!settingsFile.exists())
         restoreSettings();
@@ -128,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);
@@ -136,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);
 }
@@ -172,6 +178,7 @@ void BootScreen::addVideo()
                 bool result = fileCopy.copy(newFilePath);
                 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);
@@ -193,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){
@@ -208,6 +214,7 @@ void BootScreen::removeVideos()
                     delete temp;
                 }
             }
+            save->setEnabled(true);
         }
     }
 
@@ -220,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()
@@ -229,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)
@@ -258,6 +267,7 @@ void BootScreen::disableSelection(bool state)
     }
 
     random = true;
+    save->setEnabled(true);
 }
 
 void BootScreen::selectMultiple(bool state)
@@ -292,7 +302,7 @@ void BootScreen::restoreSettings()
      random = false;
 }
 
-void BootScreen::writeFiles(){
+void BootScreen::saveConfs(){
 
     //remove old files
     QDir dir(hildonWelcome);
@@ -314,6 +324,7 @@ void BootScreen::writeFiles(){
             else
                 createFile(temp->text(), i);
     }
+    save->setEnabled(false);
 }
 
 void BootScreen::createFile(QString filename, int index)
@@ -322,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);
@@ -334,15 +347,40 @@ void BootScreen::createFile(QString filename, int index)
     }
     else{
         qDebug() << filename << endl;
-        qDebug() << "print a error" << 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()