summaryrefslogtreecommitdiff
path: root/src/search
diff options
context:
space:
mode:
authorSimon Robertshaw <simon@hardwired.org.uk>2012-01-29 14:44:36 (GMT)
committer Simon Robertshaw <simon@hardwired.org.uk>2012-01-29 14:44:36 (GMT)
commit680a36549adaed0c3ce7e8906fadbdf190b0b3b0 (patch)
tree21ca16411c1687bb212369a59c5f3de2a0cf023b /src/search
parent7c53ca7799832920066c23cfad2f1d7fa82233c7 (diff)
downloadpowder-680a36549adaed0c3ce7e8906fadbdf190b0b3b0.zip
powder-680a36549adaed0c3ce7e8906fadbdf190b0b3b0.tar.gz
Background retrieval of save info and save list.
Diffstat (limited to 'src/search')
-rw-r--r--src/search/SearchController.cpp1
-rw-r--r--src/search/SearchModel.cpp60
-rw-r--r--src/search/SearchModel.h11
-rw-r--r--src/search/SearchView.cpp23
4 files changed, 81 insertions, 14 deletions
diff --git a/src/search/SearchController.cpp b/src/search/SearchController.cpp
index 2102a7d..6aff962 100644
--- a/src/search/SearchController.cpp
+++ b/src/search/SearchController.cpp
@@ -43,6 +43,7 @@ Save * SearchController::GetLoadedSave()
void SearchController::Update()
{
+ searchModel->Update();
if(activePreview && activePreview->HasExited)
{
delete activePreview;
diff --git a/src/search/SearchModel.cpp b/src/search/SearchModel.cpp
index d740620..e40c4b0 100644
--- a/src/search/SearchModel.cpp
+++ b/src/search/SearchModel.cpp
@@ -6,29 +6,43 @@
SearchModel::SearchModel():
currentSort("votes"),
showOwn(false),
- loadedSave(NULL)
+ loadedSave(NULL),
+ updateSaveListWorking(false),
+ updateSaveListFinished(false),
+ saveListLoaded(false)
{
}
+void * SearchModel::updateSaveListTHelper(void * obj)
+{
+ return ((SearchModel *)obj)->updateSaveListT();
+}
+
+void * SearchModel::updateSaveListT()
+{
+ vector<Save*> * tempSaveList = Client::Ref().SearchSaves((currentPage-1)*20, 20, lastQuery, currentSort, resultCount);
+ updateSaveListFinished = true;
+ return tempSaveList;
+}
+
void SearchModel::UpdateSaveList(int pageNumber, std::string query)
{
lastQuery = query;
lastError = "";
+ saveListLoaded = false;
saveList.clear();
- currentPage = 1;
- resultCount = 0;
+ //resultCount = 0;
+ currentPage = pageNumber;
notifySaveListChanged();
notifyPageChanged();
- vector<Save*> * tempSaveList = Client::Ref().SearchSaves((pageNumber-1)*20, 20, query, currentSort, resultCount);
- saveList = *tempSaveList;
- delete tempSaveList;
- if(!saveList.size())
+
+ //Threading
+ if(!updateSaveListWorking)
{
- lastError = Client::Ref().GetLastError();
+ updateSaveListFinished = false;
+ updateSaveListWorking = true;
+ pthread_create(&updateSaveListThread, 0, &SearchModel::updateSaveListTHelper, this);
}
- currentPage = pageNumber;
- notifyPageChanged();
- notifySaveListChanged();
}
void SearchModel::SetLoadedSave(Save * save)
@@ -45,6 +59,30 @@ vector<Save*> SearchModel::GetSaveList()
return saveList;
}
+void SearchModel::Update()
+{
+ if(updateSaveListWorking)
+ {
+ if(updateSaveListFinished)
+ {
+ updateSaveListWorking = false;
+ lastError = "";
+ saveListLoaded = true;
+ vector<Save*> * tempSaveList;
+ pthread_join(updateSaveListThread, (void**)(&tempSaveList));
+ saveList = *tempSaveList;
+ delete tempSaveList;
+ if(!saveList.size())
+ {
+ lastError = Client::Ref().GetLastError();
+ }
+ //currentPage = pageNumber;
+ notifyPageChanged();
+ notifySaveListChanged();
+ }
+ }
+}
+
void SearchModel::AddObserver(SearchView * observer)
{
observers.push_back(observer);
diff --git a/src/search/SearchModel.h b/src/search/SearchModel.h
index e99e4ca..d52529d 100644
--- a/src/search/SearchModel.h
+++ b/src/search/SearchModel.h
@@ -3,6 +3,7 @@
#include <vector>
#include <string>
+#include <pthread.h>
#include <math.h>
#include "Save.h"
#include "SearchView.h"
@@ -26,6 +27,14 @@ private:
void notifyPageChanged();
void notifySortChanged();
void notifyShowOwnChanged();
+
+ //Variables and methods for backgroun save request
+ bool saveListLoaded;
+ bool updateSaveListWorking;
+ volatile bool updateSaveListFinished;
+ pthread_t updateSaveListThread;
+ static void * updateSaveListTHelper(void * obj);
+ void * updateSaveListT();
public:
SearchModel();
virtual ~SearchModel();
@@ -42,6 +51,8 @@ public:
bool GetShowOwn() { return showOwn; }
void SetLoadedSave(Save * save);
Save * GetLoadedSave();
+ bool GetSavesLoaded() { return saveListLoaded; }
+ void Update();
};
#endif // SEARCHMODEL_H
diff --git a/src/search/SearchView.cpp b/src/search/SearchView.cpp
index b1d9765..b9c8bcb 100644
--- a/src/search/SearchView.cpp
+++ b/src/search/SearchView.cpp
@@ -152,6 +152,16 @@ void SearchView::NotifySaveListChanged(SearchModel * sender)
delete saveButtons[i];
}
saveButtons.clear();
+ if(!sender->GetSavesLoaded())
+ {
+ nextButton->Enabled = false;
+ previousButton->Enabled = false;
+ }
+ else
+ {
+ nextButton->Enabled = true;
+ previousButton->Enabled = true;
+ }
if(!saves.size())
{
if(!errorLabel)
@@ -159,10 +169,17 @@ void SearchView::NotifySaveListChanged(SearchModel * sender)
errorLabel = new ui::Label(ui::Point(((XRES+BARSIZE)/2)-100, ((YRES+MENUSIZE)/2)-6), ui::Point(200, 12), "Error");
AddComponent(errorLabel);
}
- if(sender->GetLastError().length())
- errorLabel->SetText("\bo" + sender->GetLastError());
+ if(!sender->GetSavesLoaded())
+ {
+ errorLabel->SetText("Loading...");
+ }
else
- errorLabel->SetText("\boNo saves found");
+ {
+ if(sender->GetLastError().length())
+ errorLabel->SetText("\bo" + sender->GetLastError());
+ else
+ errorLabel->SetText("\boNo saves found");
+ }
}
else
{