diff --git a/src/client/Client.cpp b/src/client/Client.cpp index d851e55f7..5ecb8ac00 100644 --- a/src/client/Client.cpp +++ b/src/client/Client.cpp @@ -685,6 +685,67 @@ failure: return RequestFailure; } +RequestStatus Client::AddComment(int saveID, std::string comment) +{ + lastError = ""; + std::vector * tags = NULL; + std::stringstream urlStream; + char * data = NULL; + int dataStatus, dataLength; + urlStream << "http://" << SERVER << "/Browse/Comments.json?ID=" << saveID << "&Key=" << authUser.SessionKey; + if(authUser.ID) + { + std::stringstream userIDStream; + userIDStream << authUser.ID; + + char * postNames[] = { "Comment", NULL }; + char * postDatas[] = { (char*)(comment.c_str()) }; + int postLengths[] = { comment.length() }; + data = http_multipart_post((char *)urlStream.str().c_str(), postNames, postDatas, postLengths, (char *)(userIDStream.str().c_str()), NULL, (char *)(authUser.SessionID.c_str()), &dataStatus, &dataLength); + } + else + { + lastError = "Not authenticated"; + return RequestFailure; + } + if(dataStatus == 200 && data) + { + try + { + std::istringstream dataStream(data); + json::Object objDocument; + json::Reader::Read(objDocument, dataStream); + + int status = ((json::Number)objDocument["Status"]).Value(); + + if(status!=1) + { + lastError = ((json::Number)objDocument["Error"]).Value(); + } + + if(status!=1) + goto failure; + } + catch (json::Exception &e) + { + lastError = "Could not read response"; + goto failure; + } + } + else + { + lastError = http_ret_text(dataStatus); + goto failure; + } + if(data) + free(data); + return RequestOkay; +failure: + if(data) + free(data); + return RequestFailure; +} + RequestStatus Client::FavouriteSave(int saveID, bool favourite) { lastError = ""; diff --git a/src/client/Client.h b/src/client/Client.h index adaa4494f..a2d8720a4 100644 --- a/src/client/Client.h +++ b/src/client/Client.h @@ -92,6 +92,8 @@ public: int GetStampsCount(); SaveFile * GetFirstStamp(); + RequestStatus AddComment(int saveID, std::string comment); + unsigned char * GetSaveData(int saveID, int saveDate, int & dataLength); LoginStatus Login(string username, string password, User & user); void ClearThumbnailRequests(); diff --git a/src/interface/Textbox.cpp b/src/interface/Textbox.cpp index 3180cb752..7d939fd64 100644 --- a/src/interface/Textbox.cpp +++ b/src/interface/Textbox.cpp @@ -27,6 +27,11 @@ Textbox::~Textbox() delete actionCallback; } +void Textbox::SetPlaceholder(std::string text) +{ + placeHolder = text; +} + void Textbox::SetText(std::string newText) { backingText = newText; diff --git a/src/interface/Textbox.h b/src/interface/Textbox.h index 700974ac9..e8e9d221c 100644 --- a/src/interface/Textbox.h +++ b/src/interface/Textbox.h @@ -34,6 +34,8 @@ public: virtual void SetText(std::string text); virtual std::string GetText(); + virtual void SetPlaceholder(std::string text); + void SetBorder(bool border) { this->border = border; }; void SetHidden(bool hidden) { masked = hidden; } bool GetHidden() { return masked; } diff --git a/src/preview/PreviewController.cpp b/src/preview/PreviewController.cpp index d5e1a938f..a922bc631 100644 --- a/src/preview/PreviewController.cpp +++ b/src/preview/PreviewController.cpp @@ -60,6 +60,26 @@ void PreviewController::Update() } } +void PreviewController::SubmitComment(std::string comment) +{ + if(comment.length() < 4) + { + new ErrorMessage("Error", "Comment is too short"); + } + else + { + RequestStatus status = Client::Ref().AddComment(saveId, comment); + if(status != RequestOkay) + { + new ErrorMessage("Error Submitting comment", Client::Ref().GetLastError()); + } + else + { + previewModel->UpdateComments(1); + } + } +} + void PreviewController::ShowLogin() { loginWindow = new LoginController(); diff --git a/src/preview/PreviewController.h b/src/preview/PreviewController.h index 81c457b30..e6b8caa84 100644 --- a/src/preview/PreviewController.h +++ b/src/preview/PreviewController.h @@ -38,6 +38,7 @@ public: PreviewView * GetView() { return previewView; } void Update(); void FavouriteSave(); + void SubmitComment(std::string comment); void NextCommentPage(); void PrevCommentPage(); diff --git a/src/preview/PreviewView.cpp b/src/preview/PreviewView.cpp index d7c44b33b..79d2cddf3 100644 --- a/src/preview/PreviewView.cpp +++ b/src/preview/PreviewView.cpp @@ -28,6 +28,17 @@ public: } }; +class PreviewView::SubmitCommentAction: public ui::ButtonAction +{ + PreviewView * v; +public: + SubmitCommentAction(PreviewView * v_){ v = v_; } + virtual void ActionCallback(ui::Button * sender) + { + v->submitComment(); + } +}; + class PreviewView::AutoCommentSizeAction: public ui::TextboxAction { PreviewView * v; @@ -361,6 +372,23 @@ void PreviewView::NotifySaveChanged(PreviewModel * sender) } } +void PreviewView::submitComment() +{ + if(addCommentBox) + { + std::string comment = std::string(addCommentBox->GetText()); + submitCommentButton->Enabled = false; + addCommentBox->SetText(""); + addCommentBox->SetPlaceholder("Submitting comment"); + FocusComponent(NULL); + + c->SubmitComment(comment); + + addCommentBox->SetPlaceholder("Add comment"); + submitCommentButton->Enabled = true; + } +} + void PreviewView::displayComments(int yOffset) { for(int i = 0; i < commentComponents.size(); i++) @@ -441,6 +469,7 @@ void PreviewView::NotifyCommentBoxEnabledChanged(PreviewModel * sender) addCommentBox->Appearance.HorizontalAlign = ui::Appearance::AlignLeft; AddComponent(addCommentBox); submitCommentButton = new ui::Button(ui::Point(Size.X-40, Size.Y-19), ui::Point(40, 19), "Submit"); + submitCommentButton->SetActionCallback(new SubmitCommentAction(this)); //submitCommentButton->Enabled = false; AddComponent(submitCommentButton); } diff --git a/src/preview/PreviewView.h b/src/preview/PreviewView.h index 7b85ea530..2e94b85fc 100644 --- a/src/preview/PreviewView.h +++ b/src/preview/PreviewView.h @@ -21,6 +21,7 @@ class PreviewModel; class PreviewController; class PreviewView: public ui::Window { + class SubmitCommentAction; class LoginAction; class AutoCommentSizeAction; PreviewController * c; @@ -56,6 +57,7 @@ class PreviewView: public ui::Window { void displayComments(int yOffset); void commentBoxAutoHeight(); + void submitComment(); public: void AttachController(PreviewController * controller) { c = controller;} PreviewView();