Sticky timers are loaded with zero timer at startup
[kitchenalert] / src / kitchenalertmainwindow.cpp
index b1bd95c..f74da41 100644 (file)
@@ -35,8 +35,6 @@
 #include "createtimersequencedialog.h"
 #include "selectsounddialog.h"
 
-
-
 #include <QDebug>
 
 #include <QAction>
 #include <QSettings>
 #include <QFileDialog>
 
+#include <QStringListModel>
+#include <QListView>
+#include "stickydialog.h"
 
 
 KitchenAlertMainWindow::KitchenAlertMainWindow(QWidget *parent) :
     QMainWindow(parent),
     ui(new Ui::KitchenAlertMainWindow)
     {
-    ui->setupUi(this);
-
-    setWindowIcon(QIcon(":/kitchenalert.png"));
-
-
-
+      
+  defaultSaveDirectory_ = "/home/user/KitchenAlert";    
 
-  connect(ui->CreateNewScheduleButton, SIGNAL (pressed()), this, SLOT (newTimerSequence()));
+  ui->setupUi(this);
 
+  setWindowIcon(QIcon(":/kitchenalert.png"));
 
   //alerts' tableview setup
 
-
   ui->ComingAlertsTableView->setModel(&model_);
   ui->ComingAlertsTableView->setSelectionMode(QAbstractItemView::SingleSelection);
   ui->ComingAlertsTableView->setSelectionBehavior(QAbstractItemView::SelectRows);
@@ -79,19 +76,20 @@ KitchenAlertMainWindow::KitchenAlertMainWindow(QWidget *parent) :
   ui->ComingAlertsTableView->verticalHeader()->setResizeMode(QHeaderView::ResizeToContents);
 
 
-  //Buttons used to reacting an alarm are hidden by default
+  //Buttons used to reacting to an alarm are hidden by default
 
   disableSelectionDependentButtons();
 
 
   connect(ui->ComingAlertsTableView->selectionModel(),SIGNAL(selectionChanged(QItemSelection,QItemSelection)),this,SLOT(timerSelected(QItemSelection,QItemSelection)));
-
+  connect(ui->CreateNewScheduleButton, SIGNAL (clicked()), this, SLOT (newTimerSequence()));
   connect(ui->DoneButton,SIGNAL(clicked()),this,SLOT(stop()));
   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);
@@ -101,6 +99,13 @@ KitchenAlertMainWindow::KitchenAlertMainWindow(QWidget *parent) :
   QAction * p_AboutAction = new QAction(tr("About"),this);
   connect(p_AboutAction,SIGNAL(triggered()),this,SLOT(openAbout()));
   menuBar()->addAction(p_AboutAction);
+
+  QAction * p_StickyAction = new QAction(tr("Edit sticky timers"),this);
+  connect(p_StickyAction, SIGNAL(triggered()),this,SLOT(openStickyDialog()));
+  menuBar()->addAction(p_StickyAction);
+
+  QTimer::singleShot(0,this,SLOT(loadStickies())); //Load sticky timers after construction to speed the application showing up
+
     }
 
 KitchenAlertMainWindow::~KitchenAlertMainWindow()
@@ -206,6 +211,7 @@ void KitchenAlertMainWindow::timerSelected(QItemSelection selected,QItemSelectio
     ui->DoneButton->setEnabled(true);
     ui->RestartButton->setEnabled(true);
     ui->RemoveButton->setEnabled(true);
+    ui->SaveButton->setEnabled(true);
 
 
     //snooze button enabled only when alerting
@@ -349,6 +355,8 @@ void KitchenAlertMainWindow::disableSelectionDependentButtons()
     ui->SnoozeButton->setDisabled(true);
     ui->RestartButton->setDisabled(true);
     ui->RemoveButton->setDisabled(true);
+    ui->SaveButton->setDisabled(true);
+
 
 }
 
@@ -388,9 +396,12 @@ void KitchenAlertMainWindow::remove()
         if (QMessageBox::question(this,tr("Confirm timer removal"),text,QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes)
         {
             model_.removeTimer(row);
+            ui->ComingAlertsTableView->clearSelection();
+            disableSelectionDependentButtons();
+
         }
     }
-    ui->SnoozeButton->setDisabled(true);
+
 }
 
 void KitchenAlertMainWindow::saveTimer()
@@ -403,11 +414,11 @@ void KitchenAlertMainWindow::saveTimer()
 
 
     //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);
+    QString filename = QFileDialog::getSaveFileName(this, "", defaultSaveDirectory_, "*.kitchenalert",NULL,QFileDialog::DontConfirmOverwrite);
 
-    disableSelectionDependentButtons();
 
-    qDebug() << filename;
+
+//    qDebug() << filename;
 
     if (filename.isEmpty()) //user cancelled the dialog (or gave an empty name)
     {
@@ -420,7 +431,7 @@ void KitchenAlertMainWindow::saveTimer()
 
     }
 
-    qDebug() << "filename appended to " << filename;
+  //  qDebug() << "filename appended to " << filename;
 
 
     //MANUAL CONFIRMATION OF OWERWRITE
@@ -454,7 +465,26 @@ void KitchenAlertMainWindow::saveTimer()
 
 void KitchenAlertMainWindow::loadTimer()
 {
-    QString filename = QFileDialog::getOpenFileName(this,"","",tr("KitchenAlert timer files (*.kitchenalert)"));
+
+// If the default save directory does not exist use /home/user instead (as that's what the save dialog will use)
+// This avoids a situation where save directs to a folder that cannot be accessed with open...
+
+    QString startDirectory;
+
+    if (QFile(defaultSaveDirectory_).exists())
+    {
+        startDirectory = defaultSaveDirectory_;
+    }
+    else
+    {
+        startDirectory = "/home/user/";
+        qDebug () << "default save directory not found";
+    }
+
+
+ //Get the filename to open with a dialog
+
+    QString filename = QFileDialog::getOpenFileName(this,"",startDirectory,tr("KitchenAlert timer files (*.kitchenalert)"));
     if (!filename.isEmpty())
     {
 
@@ -463,22 +493,12 @@ void KitchenAlertMainWindow::loadTimer()
 //            filename.append(".kitchenalert");
 //        }
 
-        QString errorTitle(tr("Failed to load file "));
-        errorTitle.append(filename);
+        loadTimer(filename,true); //timer gets started
 
-        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)
+void KitchenAlertMainWindow::initializeTimer(Timer *p_timer, bool startImmediately)
 {
 
 //connect alert
@@ -503,9 +523,53 @@ disableSelectionDependentButtons();
 QList<Timer *> timerList;
 
 timerList.append(p_timer);
-model_.addTimers(timerList,true); //timer gets started in the model
+model_.addTimers(timerList,startImmediately); //timer gets started in the model if startImmediately is true (default)
+
+}
 
+void KitchenAlertMainWindow::openStickyDialog()
+{
+    StickyDialog stickyDialog(defaultSaveDirectory_);
+
+
+    if (stickyDialog.exec()== QDialog::Accepted)
+    {
+        QSettings settings;
+        settings.setValue("sticky",stickyDialog.getStickyList());
+    }
 }
 
+bool KitchenAlertMainWindow::loadStickies()
+{
+    bool allSuccess = true;
 
+    QSettings settings;
 
+    QStringList stickies = settings.value("sticky").toStringList();
+
+
+    foreach (QString stickyFileName, stickies)
+    {
+       if (!loadTimer(stickyFileName,false)) //initializes the timer without starting it
+            allSuccess = false;
+    }
+
+    return allSuccess;
+}
+
+bool KitchenAlertMainWindow::loadTimer(QString filename, bool startImmediately)
+{
+    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 false;
+    }
+
+    initializeTimer(p_timer,startImmediately);
+    return true;
+}