diff --git a/src/client/Client.cpp b/src/client/Client.cpp index 8ee062f55..131ff1b46 100644 --- a/src/client/Client.cpp +++ b/src/client/Client.cpp @@ -1676,6 +1676,36 @@ failure: return RequestFailure; } +RequestStatus Client::PublishSave(int saveID) +{ + lastError = ""; + std::stringstream urlStream; + int dataStatus; + urlStream << "http://" << SERVER << "/Browse/View.html?ID=" << saveID << "&Key=" << authUser.SessionKey; + if (authUser.ID) + { + std::stringstream userIDStream; + userIDStream << authUser.ID; + const char *const postNames[] = { "ActionPublish", NULL }; + const char *const postDatas[] = { "" }; + int postLengths[] = { 1 }; + char *data = http_multipart_post(urlStream.str().c_str(), postNames, postDatas, postLengths, userIDStream.str().c_str(), NULL, authUser.SessionID.c_str(), &dataStatus, NULL); + if (data) + free(data); + } + else + { + lastError = "Not authenticated"; + return RequestFailure; + } + if (dataStatus != 302) + { + lastError = http_ret_text(dataStatus); + return RequestFailure; + } + return RequestOkay; +} + SaveInfo * Client::GetSave(int saveID, int saveDate) { lastError = ""; diff --git a/src/client/Client.h b/src/client/Client.h index 35cb548e9..d373dc76c 100644 --- a/src/client/Client.h +++ b/src/client/Client.h @@ -157,6 +157,7 @@ public: RequestStatus DeleteSave(int saveID); RequestStatus ReportSave(int saveID, std::string message); RequestStatus UnpublishSave(int saveID); + RequestStatus PublishSave(int saveID); RequestStatus FavouriteSave(int saveID, bool favourite); void SetAuthUser(User user); User GetAuthUser(); diff --git a/src/gui/search/SearchController.cpp b/src/gui/search/SearchController.cpp index b2c03dd28..602a8f084 100644 --- a/src/gui/search/SearchController.cpp +++ b/src/gui/search/SearchController.cpp @@ -269,6 +269,8 @@ void SearchController::removeSelectedC() std::stringstream saveIDF; saveIDF << "\boFailed to delete [" << saves[i] << "] ..."; notifyStatus(saveIDF.str()); + c->Refresh(); + return false; } notifyProgress((float(i+1)/float(saves.size())*100)); } @@ -283,47 +285,73 @@ void SearchController::removeSelectedC() searchModel->UpdateSaveList(searchModel->GetPageNum(), searchModel->GetLastQuery()); } -void SearchController::UnpublishSelected() +void SearchController::UnpublishSelected(bool publish) { class UnpublishSelectedConfirmation: public ConfirmDialogueCallback { public: SearchController * c; - UnpublishSelectedConfirmation(SearchController * c_) { c = c_; } + bool publish; + UnpublishSelectedConfirmation(SearchController * c_, bool publish_) { c = c_; publish = publish_; } virtual void ConfirmCallback(ConfirmPrompt::DialogueResult result) { if (result == ConfirmPrompt::ResultOkay) - c->unpublishSelectedC(); + c->unpublishSelectedC(publish); } virtual ~UnpublishSelectedConfirmation() { } }; std::stringstream desc; - desc << "Are you sure you want to hide " << searchModel->GetSelected().size() << " save"; + desc << "Are you sure you want to " << (publish ? "publish " : "unpublish ") << searchModel->GetSelected().size() << " save"; if(searchModel->GetSelected().size()>1) desc << "s"; desc << "?"; - new ConfirmPrompt("Unpublish saves", desc.str(), new UnpublishSelectedConfirmation(this)); + new ConfirmPrompt((publish ? "Publish Saves" : "Unpublish Saves"), desc.str(), new UnpublishSelectedConfirmation(this, publish)); } -void SearchController::unpublishSelectedC() +void SearchController::unpublishSelectedC(bool publish) { class UnpublishSavesTask : public Task { std::vector saves; SearchController *c; + bool publish; public: - UnpublishSavesTask(std::vector saves_, SearchController *c_) { saves = saves_; c = c_; } + UnpublishSavesTask(std::vector saves_, SearchController *c_, bool publish_) { saves = saves_; c = c_; publish = publish_; } + + bool PublishSave(int saveID) + { + std::stringstream message; + message << "Publishing save [" << saveID << "]"; + notifyStatus(message.str()); + if (Client::Ref().PublishSave(saveID) != RequestOkay) + return false; + return true; + } + + bool UnpublishSave(int saveID) + { + std::stringstream message; + message << "Unpublishing save [" << saveID << "]"; + notifyStatus(message.str()); + if (Client::Ref().UnpublishSave(saveID) != RequestOkay) + return false; + return true; + } + virtual bool doWork() { + bool ret; for(int i = 0; i < saves.size(); i++) { - std::stringstream saveID; - saveID << "Hiding save [" << saves[i] << "]"; - notifyStatus(saveID.str()); - if(Client::Ref().UnpublishSave(saves[i])!=RequestOkay) + if (publish) + ret = PublishSave(saves[i]); + else + ret = UnpublishSave(saves[i]); + if (!ret) { - std::stringstream saveIDF; - saveIDF << "\boFailed to hide [" << saves[i] << "], is this save yours?"; - notifyError(saveIDF.str()); + std::stringstream error; + error << "\boFailed to " << (publish ? "Publish" : "Unpublish") << " [" << saves[i] << "], is this save yours?"; + notifyError(error.str()); + c->Refresh(); return false; } notifyProgress((float(i+1)/float(saves.size())*100)); @@ -334,7 +362,7 @@ void SearchController::unpublishSelectedC() }; std::vector selected = searchModel->GetSelected(); - new TaskWindow("Unpublishing saves", new UnpublishSavesTask(selected, this)); + new TaskWindow((publish ? "Publishing Saves" : "Unpublishing Saves"), new UnpublishSavesTask(selected, this, publish)); } void SearchController::FavouriteSelected() diff --git a/src/gui/search/SearchController.h b/src/gui/search/SearchController.h index 403fbe7e2..24e982394 100644 --- a/src/gui/search/SearchController.h +++ b/src/gui/search/SearchController.h @@ -24,7 +24,7 @@ private: bool instantOpen; bool doRefresh; void removeSelectedC(); - void unpublishSelectedC(); + void unpublishSelectedC(bool publish); public: class OpenCallback; bool HasExited; @@ -47,7 +47,7 @@ public: void Update(); void ClearSelection(); void RemoveSelected(); - void UnpublishSelected(); + void UnpublishSelected(bool publish); void FavouriteSelected(); void ReleaseLoadedSave(); SaveInfo * GetLoadedSave(); diff --git a/src/gui/search/SearchView.cpp b/src/gui/search/SearchView.cpp index c79b2a1ec..a5edf3563 100644 --- a/src/gui/search/SearchView.cpp +++ b/src/gui/search/SearchView.cpp @@ -19,7 +19,8 @@ SearchView::SearchView(): c(NULL), changed(true), lastChanged(0), - pageCount(0) + pageCount(0), + publishButtonShown(false) { Client::Ref().AddListener(this); @@ -202,7 +203,7 @@ SearchView::SearchView(): UnpublishSelectedAction(SearchView * _v) { v = _v; } void ActionCallback(ui::Button * sender) { - v->c->UnpublishSelected(); + v->c->UnpublishSelected(v->publishButtonShown); } }; @@ -730,13 +731,18 @@ void SearchView::NotifySaveListChanged(SearchModel * sender) void SearchView::NotifySelectedChanged(SearchModel * sender) { vector selected = sender->GetSelected(); + int published = 0; for(int j = 0; j < saveButtons.size(); j++) { saveButtons[j]->SetSelected(false); for(int i = 0; i < selected.size(); i++) { - if(saveButtons[j]->GetSave()->GetID()==selected[i]) + if(saveButtons[j]->GetSave()->GetID() == selected[i]) + { saveButtons[j]->SetSelected(true); + if (saveButtons[j]->GetSave()->GetPublished()) + published++; + } } } @@ -746,13 +752,29 @@ void SearchView::NotifySelectedChanged(SearchModel * sender) unpublishSelected->Visible = true; favouriteSelected->Visible = true; clearSelection->Visible = true; + pageTextbox->Visible = false; + pageLabel->Visible = false; + pageCountLabel->Visible = false; + if (published <= selected.size()/2) + { + unpublishSelected->SetText("Publish"); + publishButtonShown = true; + } + else + { + unpublishSelected->SetText("Unpublish"); + publishButtonShown = false; + } } - else + else if (removeSelected->Visible) { removeSelected->Visible = false; unpublishSelected->Visible = false; favouriteSelected->Visible = false; clearSelection->Visible = false; + pageTextbox->Visible = true; + pageLabel->Visible = true; + pageCountLabel->Visible = true; } } diff --git a/src/gui/search/SearchView.h b/src/gui/search/SearchView.h index dfc055930..3f27d63a8 100644 --- a/src/gui/search/SearchView.h +++ b/src/gui/search/SearchView.h @@ -50,6 +50,7 @@ private: bool changed; int lastChanged; int pageCount; + bool publishButtonShown; public: void NotifyTagListChanged(SearchModel * sender); void NotifySaveListChanged(SearchModel * sender);