Merge branch 'master' of /opt/src/sb1/qt/scorecard
[scorecard] / src / main-window.cpp
index 58d3f9f..0b53fd4 100644 (file)
@@ -24,6 +24,7 @@ QString topDir("/opt");
 QString mmcDir("/media/mmc1");
 QString dataDirName("data");
 QString dataDir;
+QString userDataDir;
 QString imgDir(topDir + "/pixmaps");
 QString scoreFileName("score.xml");
 QString scoreFile;
@@ -47,7 +48,12 @@ bool dateMoreThan(const Score *s1, const Score *s2)
 
 bool scoreMoreThan(const Score *s1, const Score *s2)
 {
-    return s1->getTotal(Total) > s2->getTotal(Total);
+    return s1->getTotal(Total).toInt() > s2->getTotal(Total).toInt();
+}
+
+bool scoreLessThan(const Score *s1, const Score *s2)
+{
+    return s1->getTotal(Total).toInt() < s2->getTotal(Total).toInt();
 }
 
 // Find score based on club and course name
@@ -167,7 +173,7 @@ void MainWindow::sortScoreList()
     if (conf.sortOrder == "Date")
         qSort(scoreList.begin(), scoreList.end(), dateMoreThan); 
     else if (conf.sortOrder == "Score")
-        qSort(scoreList.begin(), scoreList.end(), scoreMoreThan); 
+        qSort(scoreList.begin(), scoreList.end(), scoreLessThan); 
 }
 
 MainWindow::MainWindow(QMainWindow *parent): QMainWindow(parent)
@@ -226,17 +232,43 @@ void MainWindow::loadSettings(void)
         dataDir = topDir + "/" + appName + "/" + dataDirName;
     }
 #endif
-    scoreFile = dataDir + "/" + scoreFileName;
-    clubFile = dataDir + "/" + clubFileName;
-    masterFile = dataDir + "/" + masterFileName;
 
-    QDir dir(dataDir);
+    userDataDir = QDir::homePath() + "/." + appName;
+    QDir dir(userDataDir);
     if (!dir.exists())
-        if (!dir.mkpath(dataDir)) {
-            qWarning() << "Unable to create: " + dataDir;
+        if (!dir.mkpath(userDataDir)) {
+            qWarning() << "Unable to create: " + userDataDir;
             return;
         }
-    qDebug() << "Data is at:" + dataDir;
+
+    masterFile = dataDir + "/" + masterFileName;
+
+    // Store user data files under $HOME so they are not lost in
+    // re-flash
+    scoreFile = userDataDir + "/" + scoreFileName;
+    clubFile = userDataDir + "/" + clubFileName;
+
+    // Start of 0.19 migration
+    // Copy existing user data to new location
+    // 0.18 and earlier: score.xml and club.xml are in /opt/scorecard/data
+    // 0.19 and later: score.xml and club.xml are in /home/user/.scorecard
+    QString scoreFileOld = dataDir + "/" + scoreFileName;
+    QString clubFileOld = dataDir + "/" + clubFileName;
+
+    QFile file1(scoreFileOld);
+    QFile file2(clubFileOld);
+    QDir move;
+    if (file1.exists()) {
+        move.rename(scoreFileOld, scoreFile);
+        qDebug() << "Moved: " << scoreFileOld << "->" << scoreFile;
+    }
+    if (file2.exists()) {
+        move.rename(clubFileOld, clubFile);
+        qDebug() << "Moved: " << clubFileOld << "->" << clubFile;
+    }
+    // End of 0.19 migration
+
+    qDebug() << "User data is at:" + userDataDir;
 
     settings.beginGroup(settingsGroup);
     conf.hcp = settings.value(settingsHcp);
@@ -251,7 +283,7 @@ void MainWindow::loadSettings(void)
 
     // Use date sort order if no settings for that
     if (!conf.sortOrder.isValid())
-        conf.sortOrder = "Yes";
+        conf.sortOrder = "Date";
 
     qDebug() << "Settings: " << conf.hcp << conf.homeClub << conf.sortOrder << conf.defaultCourses;
 }