Sort scores based on dates not the entry date.
[scorecard] / src / main-window.cpp
index a9f7cf3..6ca8d3a 100644 (file)
 #include "xml-parser.h"
 #include "xml-dom-parser.h"
 
-QString scoreFile("data/score.xml");
-QString scoreFileWr("data/score-save.xml");
-QString clubFile("data/club.xml");
-QString clubFileWr("data/club-save.xml");
+QString appName("scorecard");
+QString topDir("/opt/scorecard");
+QString mmcDir("/media/mmc1/scorecard");
+QString dataDirName("/data/");
+QString dataDir;
+QString imgDir(topDir + "/pixmaps");
+QString scoreFileName("score.xml");
+QString scoreFile;
+QString clubFileName("club.xml");
+QString clubFile;
+QString logFile("/tmp/scorecard.log");
+
+bool dateLessThan(const Score *s1, const Score *s2)
+{
+  return (*s1) < (*s2);
+}
 
 MainWindow::MainWindow(QMainWindow *parent) : QMainWindow(parent)
 {
   resize(800, 480);
 
+  loadSettings();
+
   QWidget *centralWidget = new QWidget(this);
 
   // TODO: move to proper function
@@ -31,10 +45,8 @@ MainWindow::MainWindow(QMainWindow *parent) : QMainWindow(parent)
   loadScoreFile(scoreFile, scoreList);
   loadClubFile(clubFile, clubList);
 
-#ifdef TEST
-  saveScoreFile(scoreFileWr, scoreList);
-  saveClubFile(clubFileWr, clubList);
-#endif
+  // Sort the scores based on dates
+  qSort(scoreList.begin(), scoreList.end(), dateLessThan); 
 
   createTableView(scoreList, clubList);
   //createTreeView(scoreList, parent);
@@ -46,6 +58,37 @@ MainWindow::MainWindow(QMainWindow *parent) : QMainWindow(parent)
   createMenus();
 }
 
+void MainWindow::loadSettings(void)
+{
+  bool external = false;
+
+#ifndef Q_WS_HILDON
+  topDir = ".";
+#endif
+
+  QDir mmc(mmcDir);
+  if (mmc.exists())
+    external = true;
+
+  if (external) {
+    dataDir = mmcDir + dataDirName;
+  }
+  else {
+    dataDir = topDir + dataDirName;
+  }
+  scoreFile = dataDir + scoreFileName;
+  clubFile = dataDir + clubFileName;
+
+  QDir dir(dataDir);
+  if (!dir.exists())
+    if (!dir.mkdir(dataDir)) {
+      // TODO: mkdir does not work...
+      qDebug() << "Unable to create: " + dataDir;
+      return;
+    }
+  qDebug() << "Data is at: " + dataDir;
+}
+
 void MainWindow::createLayout(QWidget *parent)
 {
   buttonLayout = new QVBoxLayout;
@@ -229,8 +272,6 @@ void MainWindow::newCourse()
     QString date;
 
     selectDialog->results(clubName, courseName);
-    qDebug() << "club=" << clubName;
-    qDebug() << "course=" << courseName;
 
     CourseDialog *courseDialog = new CourseDialog(this);
 
@@ -265,6 +306,10 @@ void MainWindow::newCourse()
        clubList << club;
       }
       saveClubFile(clubFile, clubList);
+
+      // TODO: does this really work? No mem leaks?
+      scoreTableModel->setClub(clubList);
+
     }
   }
 }
@@ -282,11 +327,10 @@ void MainWindow::newScore()
     QString date;
 
     selectDialog->results(clubName, courseName, date);
-    qDebug() << "club=" << clubName;
-    qDebug() << "course=" << courseName;
-    qDebug() << "date=" << date;
 
     ScoreDialog *scoreDialog = new ScoreDialog(this);
+    QString title = "New Score : " + courseName + ", " + date;
+    scoreDialog->setWindowTitle(title);
 
     Club *club = findClub(clubName);
     if (!club) {
@@ -310,11 +354,14 @@ void MainWindow::newScore()
       Score *score = new Score(scores, clubName, courseName, date);
       scoreList << score;
 
+      // Sort the scores based on dates
+      qSort(scoreList.begin(), scoreList.end(), dateLessThan); 
+      // Save it
       saveScoreFile(scoreFile, scoreList);
 
       // TODO: does this really work? No mem leaks?
-      scoreTableModel->setScore(scoreList);
-      lastButtonClicked();
+      scoreTableModel->setScore(scoreList, score);
+      updateStatusBar();
     }
   }
 }
@@ -324,8 +371,7 @@ void MainWindow::loadScoreFile(QString &fileName, QList<Score *> &list)
   ScoreXmlHandler handler(list);
 
   if (handler.parse(fileName))
-    qDebug() << "Score loaded : " << fileName << 
-      " entries : " << list.size();
+    qDebug() << "File loaded: " << fileName << " entries : " << list.size();
 }
 
 void MainWindow::saveScoreFile(QString &fileName, QList<Score *> &list)
@@ -333,8 +379,10 @@ void MainWindow::saveScoreFile(QString &fileName, QList<Score *> &list)
   ScoreXmlHandler handler(list);
 
   if (handler.save(fileName))
-    qDebug() << "Score saved : " << fileName << 
-      " entries : " << list.size();
+    // TODO: banner
+    qDebug() << "File saved : " << fileName << " entries : " << list.size();
+  else
+    qDebug() << "Unable to save : " << fileName;
 }
 
 void MainWindow::loadClubFile(QString &fileName, QList<Club *> &list)
@@ -342,8 +390,7 @@ void MainWindow::loadClubFile(QString &fileName, QList<Club *> &list)
   ClubXmlHandler handler(list);
 
   if (handler.parse(fileName))
-    qDebug() << "Club loaded: " << fileName << 
-      " entries : " << list.size();
+    qDebug() << "File loaded: " << fileName << " entries : " << list.size();
 }
 
 void MainWindow::saveClubFile(QString &fileName, QList<Club *> &list)
@@ -351,6 +398,9 @@ void MainWindow::saveClubFile(QString &fileName, QList<Club *> &list)
   ClubXmlHandler handler(list);
 
   if (handler.save(fileName))
-    qDebug() << "Club saved : " << fileName << 
-      " entries : " << list.size();
+    // TODO: banner
+    qDebug() << "File saved : " << fileName << " entries : " << list.size();
+  else
+    qDebug() << "Unable to save : " << fileName;
+
 }