Working comment submission

This commit is contained in:
Simon Robertshaw 2012-07-18 13:07:33 +01:00
parent 78c4aba468
commit a8e4221f38
8 changed files with 122 additions and 0 deletions

View File

@ -685,6 +685,67 @@ failure:
return RequestFailure;
}
RequestStatus Client::AddComment(int saveID, std::string comment)
{
lastError = "";
std::vector<string> * 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 = "";

View File

@ -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();

View File

@ -27,6 +27,11 @@ Textbox::~Textbox()
delete actionCallback;
}
void Textbox::SetPlaceholder(std::string text)
{
placeHolder = text;
}
void Textbox::SetText(std::string newText)
{
backingText = newText;

View File

@ -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; }

View File

@ -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();

View File

@ -38,6 +38,7 @@ public:
PreviewView * GetView() { return previewView; }
void Update();
void FavouriteSave();
void SubmitComment(std::string comment);
void NextCommentPage();
void PrevCommentPage();

View File

@ -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);
}

View File

@ -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();