From 15d8b6bef173b565164c930692fe419cc8beab20 Mon Sep 17 00:00:00 2001
From: Chris Tallon <chris@vomp.tv>
Date: Fri, 29 Dec 2006 23:49:59 +0000
Subject: [PATCH] Recordings sort order

---
 directory.cc      |  9 ++++++---
 directory.h       | 12 ++++++++++--
 recman.cc         | 23 +++++++++++++++++++----
 recman.h          |  5 +++++
 voptionsmenu.cc   |  4 +++-
 vrecordinglist.cc | 31 +++++++++++++++++++++++++++++--
 vrecordinglist.h  |  1 +
 7 files changed, 73 insertions(+), 12 deletions(-)

diff --git a/directory.cc b/directory.cc
index 13ce2d3..cc7f275 100644
--- a/directory.cc
+++ b/directory.cc
@@ -69,14 +69,17 @@ ULONG Directory::getNumRecordings()
   return total;
 }
 
-void Directory::sort()
+void Directory::sort(bool chronoSortOrder)
 {
   // Sort the directory order
   ::sort(dirList.begin(), dirList.end(), DirectorySorter());
 
   // Sort the recordings order
-  ::sort(recList.begin(), recList.end(), RecordingSorter());
+  if (chronoSortOrder)
+    ::sort(recList.begin(), recList.end(), RecordingSorterChrono());
+  else
+    ::sort(recList.begin(), recList.end(), RecordingSorterAlpha());
 
   // Now get the dirs to sort themselves! oh I love recursion.
-  for(UINT i = 0; i < dirList.size(); i++) dirList[i]->sort();
+  for(UINT i = 0; i < dirList.size(); i++) dirList[i]->sort(chronoSortOrder);
 }
diff --git a/directory.h b/directory.h
index f70aad1..330e79a 100644
--- a/directory.h
+++ b/directory.h
@@ -41,7 +41,7 @@ class Directory
 
     Directory* getDirByName(char* dirName);
     ULONG getNumRecordings();
-    void sort();
+    void sort(bool chronoSortOrder);
 
     char* name;
 
@@ -63,7 +63,7 @@ struct DirectorySorter
   }
 };
 
-struct RecordingSorter
+struct RecordingSorterAlpha
 {
   bool operator() (const Recording* a, const Recording* b)
   {
@@ -75,4 +75,12 @@ struct RecordingSorter
   }
 };
 
+struct RecordingSorterChrono
+{
+  bool operator() (const Recording* a, const Recording* b)
+  {
+    return a->getStartTime() < b->getStartTime();
+  }
+};
+
 #endif
diff --git a/recman.cc b/recman.cc
index 11afd4d..e46da95 100644
--- a/recman.cc
+++ b/recman.cc
@@ -29,6 +29,8 @@ RecMan::RecMan()
 
   rootDir = new Directory("/");
   currentDir = rootDir;
+
+  chronoSortOrder = false;
 }
 
 RecMan::~RecMan()
@@ -107,9 +109,6 @@ void RecMan::addEntry(ULONG startTime, char* name, char* fileName)
   }
 
   targetDirectory->recList.push_back(rec);
-
-  // Sort it all.
-  rootDir->sort();
 }
 
 ULONG RecMan::getTotalSpace()
@@ -179,7 +178,7 @@ int RecMan::moveRecording(Recording* toMove, Directory* toDir)
 
         currentDir->recList.erase(i);
         toDir->recList.push_back(toMove);
-        toDir->sort();
+        toDir->sort(chronoSortOrder);
       }
       break;
     }
@@ -253,3 +252,19 @@ Directory* RecMan::getRootDir()
 {
   return rootDir;
 }
+
+void RecMan::setSortOrderChron()
+{
+  chronoSortOrder = true;
+}
+
+void RecMan::toggleSortOrder()
+{
+  chronoSortOrder = !chronoSortOrder;
+}
+
+void RecMan::sort()
+{
+  Log::getInstance()->log("RecMan", Log::DEBUG, "Sort");
+  rootDir->sort(chronoSortOrder);
+}
diff --git a/recman.h b/recman.h
index 68b8691..f34c55b 100644
--- a/recman.h
+++ b/recman.h
@@ -38,8 +38,11 @@ class RecMan
     RecMan();
     ~RecMan();
 
+    void setSortOrderChron();
+    void toggleSortOrder();
     void setStats(ULONG totalSpace, ULONG freeSpace, ULONG usedPercent);
     void addEntry(ULONG startTime, char* name, char* filename); // modifies name
+    void sort();
     int deleteRecording(Recording* rec);
     int moveRecording(Recording* toMove, Directory* toDir);
 
@@ -65,6 +68,8 @@ class RecMan
     Directory* rootDir;
     Directory* currentDir;
 
+    bool chronoSortOrder;
+
     void constructPath(char* target, Directory* dir) ;
 };
 
diff --git a/voptionsmenu.cc b/voptionsmenu.cc
index d5d90df..7bc1c72 100644
--- a/voptionsmenu.cc
+++ b/voptionsmenu.cc
@@ -290,7 +290,7 @@ void VOptionsMenu::doApplyChanges(map<int,int>* changedOptions)
 
 void VOptionsMenu::doGeneral()
 {
-  static const int numOptions = 7;
+  static const int numOptions = 8;
 
   static const char* options1[] = {"Old", "New"};
   static const char* options3[] = {"RGB+composite", "S-Video"};
@@ -298,6 +298,7 @@ void VOptionsMenu::doGeneral()
   static const char* options5[] = {"Chop sides", "Letterbox"};
   static const char* options6[] = {"On", "Off", "Last state"};
   static const char* options7[] = {"All", "FTA only"};
+  static const char* options15[] = {"Alphabetical", "Chronological"};
 
   const static OPTIONDATA optionData[numOptions] =
   {
@@ -308,6 +309,7 @@ void VOptionsMenu::doGeneral()
     {5, "16:9 on 4:3 display mode", "TV",      "Widemode",         OPTIONTYPE_TEXT, 2, 0, 0, options5 },
     {6, "Power state after bootup", "General", "Power After Boot", OPTIONTYPE_TEXT, 3, 0, 0, options6 },
     {7, "Display channels",         "General", "Channels",         OPTIONTYPE_TEXT, 2, 0, 0, options7 },
+    {15, "Recordings sort order",   "General", "Recordings Sort Order", OPTIONTYPE_TEXT, 2, 0, 0, options15 },
   };
 
   // As all the above data is const static, it can be sent to the new View, this stack frame can
diff --git a/vrecordinglist.cc b/vrecordinglist.cc
index 8c821fd..ce3b7d9 100644
--- a/vrecordinglist.cc
+++ b/vrecordinglist.cc
@@ -465,7 +465,13 @@ int VRecordingList::handleCommand(int command)
       if (doPlay(true)) return 2;
       return 1;
     }
-
+    case Remote::LEFT:
+    case Remote::RIGHT:
+    case Remote::ZERO:
+    {
+      reSort();
+      return 2;
+    }
   }
   // stop command getting to any more views
   return 1;
@@ -473,14 +479,35 @@ int VRecordingList::handleCommand(int command)
 
 bool VRecordingList::load()
 {
+  VDR* vdr = VDR::getInstance();
+
   recman = new RecMan();
-  bool success = VDR::getInstance()->getRecordingsList(recman);
+  bool success = vdr->getRecordingsList(recman);
   if (success)
   {
     loading = false;
+
+    char* defaultSortOrder = vdr->configLoad("General", "Recordings Sort Order");
+    if (defaultSortOrder)
+    {
+      if (!STRCASECMP(defaultSortOrder, "Chronological")) recman->setSortOrderChron();
+      delete[] defaultSortOrder;
+    }
+
+    recman->sort();
+
     draw();
     viewman->updateView(this);
   }
 
   return success;
 }
+
+void VRecordingList::reSort()
+{
+  recman->toggleSortOrder();
+  recman->sort();
+  sl.clear();
+  draw();
+  viewman->updateView(this);
+}
diff --git a/vrecordinglist.h b/vrecordinglist.h
index b5355cc..eaf6bd8 100644
--- a/vrecordinglist.h
+++ b/vrecordinglist.h
@@ -70,6 +70,7 @@ class VRecordingList : public View
     int doPlay(bool resume);
     void doMoveRecording(Directory* toDir);
     Recording* getCurrentOptionRecording();
+    void reSort();
 
     stack<int> slIndexStack;
 };
-- 
2.39.5