Added saving and loading of timers
[kitchenalert] / src / kitchenalertmainwindow.cpp
index f65a002..b1bd95c 100644 (file)
@@ -2,7 +2,7 @@
 
         KitchenAlert
 
-        Copyright (C) 2010  Heli Hyvättinen
+        Copyright (C) 2010-2011  Heli Hyvättinen
 
         This file is part of KitchenAlert.
 
@@ -43,6 +43,7 @@
 #include <QMenuBar>
 #include <QMessageBox>
 #include <QSettings>
+#include <QFileDialog>
 
 
 
@@ -89,6 +90,8 @@ KitchenAlertMainWindow::KitchenAlertMainWindow(QWidget *parent) :
   connect(ui->RestartButton,SIGNAL(clicked()),this,SLOT(restart()));
   connect(ui->SnoozeButton,SIGNAL(clicked()),this, SLOT(snooze()));
   connect(ui->RemoveButton,SIGNAL(clicked()),this,SLOT(remove()));
+  connect(ui->SaveButton,SIGNAL(clicked()),this,SLOT(saveTimer()));
+  connect(ui->OpenButton,SIGNAL(clicked()),this,SLOT(loadTimer()));
   // menu setup
 
   QAction * p_SelectSoundAction = new QAction(tr("Select alert sound"),this);
@@ -137,7 +140,7 @@ void KitchenAlertMainWindow::newTimerSequence()
 
        connect(timer1,SIGNAL(alert(QModelIndex)),this,SLOT(alert(QModelIndex)));
 
-       //TODO: FIND A WAY TO INFORM THE TIMERS' ALERTSOUND'S OF A CHANGE OF THE SOUND FILE THEY SHOULD USE!!!!
+
 
        connect(this,SIGNAL(defaultSoundEnabled()),timer1,SLOT(enableDefaultSound()));
        connect(this,SIGNAL(soundChanged(QString)),timer1,SLOT(changeAlertSound(QString)));
@@ -175,7 +178,10 @@ void KitchenAlertMainWindow::alert(QModelIndex indexOfAlerter)
 
 
     activateWindow();
-    raise();
+
+// removing everything below does not solve the bug #6752!
+
+    raise();  //this may be unnecessary
 
     // The alerting timer is selected
     ui->ComingAlertsTableView->selectionModel()->select(QItemSelection(indexOfAlerter,indexOfAlerter),QItemSelectionModel::SelectCurrent | QItemSelectionModel::Rows );
@@ -190,17 +196,7 @@ void KitchenAlertMainWindow::alert(QModelIndex indexOfAlerter)
 
 
     ui->SnoozeButton->setEnabled(true);
-
-    //Debug message
-
-
-
-    ui->debugLabel->setText(tr("Alert received from row %1").arg(indexOfAlerter.row()));
-    qDebug() << "Wrote the debug message";
-
-    //The alert sound is played
-   //TESTING TO MOVE THIS OPERATION TO THE TIMER ITSELF
-//    alertSound_.play();
+//qDebug ("Snooze on when alerting");
 
 }
 
@@ -222,8 +218,13 @@ void KitchenAlertMainWindow::timerSelected(QItemSelection selected,QItemSelectio
         if (model_.isThisTimerAlerting(index) == true)
         {
              ui->SnoozeButton->setEnabled(true);
+//qDebug() << "Snooze on";
+        }
+        else
+        {
+            ui->SnoozeButton->setDisabled(true);
+//qDebug() << "Snooze off";
         }
-        else ui->SnoozeButton->setDisabled(true);
     }
 
 }
@@ -236,7 +237,7 @@ void KitchenAlertMainWindow::snooze()
         model_.snoozeTimer(row);
     }
     ui->SnoozeButton->setDisabled(true);
-  //  alertSound_.stop();
+
 
 }
 
@@ -248,8 +249,14 @@ void KitchenAlertMainWindow::restart()
 
         model_.startTimer(row);
     }
-    ui->SnoozeButton->setDisabled(true);
- //   alertSound_.stop();
+
+
+   if (model_.isThisTimerAlerting(row) == false) //This has to be checked, because 00:00:00 alerts alert *before* the program execution reaches here
+    {
+        ui->SnoozeButton->setDisabled(true);
+    }
+ //   qDebug () << "disabled snooze because of restart";
+
 
 }
 
@@ -261,7 +268,7 @@ void KitchenAlertMainWindow::stop()
         model_.stopTimer(row);
     }
     ui->SnoozeButton->setDisabled(true);
-//    alertSound_.stop();
+
 }
 
 QModelIndex KitchenAlertMainWindow::selectedRow()
@@ -282,10 +289,22 @@ void KitchenAlertMainWindow::openSelectSoundDialog()
     SelectSoundDialog dialog;
    if ( dialog.exec() == QDialog::Accepted) //if user pressed OK
     {
+       QSettings settings ("KitchenAlert","KitchenAlert");
+      
        if (dialog.isDefaultSoundChecked() == true)
+       { 
+        
+          settings.setValue("UseDefaultSound",true); 
            emit defaultSoundEnabled();
-       else
-           emit soundChanged(dialog.getSoundFileName());
+       }   
+      else
+       {
+           QString filename = dialog.getSoundFileName();
+           settings.setValue("UseDefaultSound",false);
+           settings.setValue("soundfile",filename);
+           emit soundChanged(filename);
+       }
+
     }
 
 }
@@ -293,7 +312,7 @@ void KitchenAlertMainWindow::openSelectSoundDialog()
 void KitchenAlertMainWindow::openAbout()
 {
     QMessageBox::about(this,tr("About KitchenAlert"),tr("<p>Version %1"
-                                                        "<p>Copyright &copy; Heli Hyv&auml;ttinen 2010"
+                                                        "<p>Copyright &copy; Heli Hyv&auml;ttinen 2010-2011"
                                                          "<p>License: General Public License v3"
                                                          "<p>Web page: http://kitchenalert.garage.maemo.org/"
                                                          "<p>Bugtracker: https://garage.maemo.org/projects/kitchenalert/").arg(QApplication::applicationVersion()));
@@ -301,8 +320,6 @@ void KitchenAlertMainWindow::openAbout()
 
 bool KitchenAlertMainWindow::event(QEvent *event)
 {
-    QMainWindow::event(event);
-
 
 
     switch (event->type())
@@ -310,21 +327,20 @@ bool KitchenAlertMainWindow::event(QEvent *event)
         case QEvent::WindowActivate:
 
             model_.setUpdateViewOnChanges(true);
-            ui->debugLabel->setText("Returned to the application!");
-
-
-              break;
+//            ui->debugLabel->setText("Returned to the application!");
+            break;
 
        case QEvent::WindowDeactivate:
             model_.setUpdateViewOnChanges(false);
-            ui->debugLabel->setText("");
+//            ui->debugLabel->setText("");
             break;
 
        default:
             break;
 
-
     }
+
+    return QMainWindow::event(event); // Send the event to the base class implementation (also when handling the event in this function): necessary for the program to work!
 }
 
 void KitchenAlertMainWindow::disableSelectionDependentButtons()
@@ -376,3 +392,120 @@ void KitchenAlertMainWindow::remove()
     }
     ui->SnoozeButton->setDisabled(true);
 }
+
+void KitchenAlertMainWindow::saveTimer()
+{
+
+    QModelIndex row = selectedRow();
+
+    if (row.isValid() == false) //If there was no row selected invalid row was returned
+        return;
+
+
+    //file name is asked. As the filename will be appended, there's no point in confirming owerwrite here
+    QString filename = QFileDialog::getSaveFileName(this, "", "", "*.kitchenalert",NULL,QFileDialog::DontConfirmOverwrite);
+
+    disableSelectionDependentButtons();
+
+    qDebug() << filename;
+
+    if (filename.isEmpty()) //user cancelled the dialog (or gave an empty name)
+    {
+        return;
+    }
+
+    if (!filename.endsWith(".kitchenalert"))
+    {
+        filename.append(".kitchenalert");
+
+    }
+
+    qDebug() << "filename appended to " << filename;
+
+
+    //MANUAL CONFIRMATION OF OWERWRITE
+
+    if ( QFile::exists(filename))
+    {
+         //ASK FOR CONFIRMATION
+
+        QString overwriteQuestion ("File ");
+        overwriteQuestion.append(filename);
+        overwriteQuestion.append(" already exists. Do you want to overwrite it?");
+        if (QMessageBox::question(this,"Confirm overwrite?", overwriteQuestion,QMessageBox::Yes | QMessageBox::No,QMessageBox::No) != QMessageBox::Yes)
+        {
+            return;
+        }
+    }
+
+
+
+
+    QString errorMessage(tr("Cannot write to file "));
+    errorMessage.append(filename);
+
+    if (!model_.saveTimer(row,filename)) //Save the file, if not successful give an error message
+    {
+        QMessageBox::critical(this,tr("Save timer failed!"), errorMessage);
+    }
+
+
+}
+
+void KitchenAlertMainWindow::loadTimer()
+{
+    QString filename = QFileDialog::getOpenFileName(this,"","",tr("KitchenAlert timer files (*.kitchenalert)"));
+    if (!filename.isEmpty())
+    {
+
+//        if (!filename.endsWith(".kitchenalert"))      //not needed, the dialog won't let the user to select files not ending with ".kitchenalert"
+//        {
+//            filename.append(".kitchenalert");
+//        }
+
+        QString errorTitle(tr("Failed to load file "));
+        errorTitle.append(filename);
+
+        Timer * p_timer = new Timer();
+        if (!p_timer->load(filename))
+        {
+            QMessageBox::critical(this,errorTitle,tr("Unable to open file or not a valid KitchenAlert timer file."));
+            delete p_timer;
+            return;
+        }
+
+        initializeTimer(p_timer);
+    }
+}
+
+void KitchenAlertMainWindow::initializeTimer(Timer *p_timer)
+{
+
+//connect alert
+
+
+connect(p_timer,SIGNAL(alert(QModelIndex)),this,SLOT(alert(QModelIndex)));
+
+
+//connect change sound functions
+
+connect(this,SIGNAL(defaultSoundEnabled()),p_timer,SLOT(enableDefaultSound()));
+connect(this,SIGNAL(soundChanged(QString)),p_timer,SLOT(changeAlertSound(QString)));
+
+
+//Disable buttons, as selection is cleared when view is refreshed to show the new timer
+
+disableSelectionDependentButtons();
+
+
+// give timers to the model (model wants list of timers now..)
+
+QList<Timer *> timerList;
+
+timerList.append(p_timer);
+model_.addTimers(timerList,true); //timer gets started in the model
+
+}
+
+
+