2012-01-19 07:44:59 -06:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <iostream>
|
|
|
|
#include <sstream>
|
|
|
|
#include <string>
|
2012-01-19 14:10:05 -06:00
|
|
|
#include <vector>
|
2012-04-02 11:01:28 -05:00
|
|
|
#include <iomanip>
|
2012-01-19 11:59:00 -06:00
|
|
|
#include <time.h>
|
2012-04-19 09:22:18 -05:00
|
|
|
#include <stdio.h>
|
2012-01-19 14:10:05 -06:00
|
|
|
|
2012-04-02 11:01:28 -05:00
|
|
|
#ifdef WIN32
|
|
|
|
#include <direct.h>
|
|
|
|
#else
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2012-01-19 07:44:59 -06:00
|
|
|
#include "Config.h"
|
|
|
|
#include "Client.h"
|
2012-01-24 18:57:39 -06:00
|
|
|
#include "MD5.h"
|
2012-01-19 07:44:59 -06:00
|
|
|
#include "Graphics.h"
|
2012-02-01 15:20:27 -06:00
|
|
|
#include "Misc.h"
|
2012-01-19 07:44:59 -06:00
|
|
|
|
2012-01-19 14:10:05 -06:00
|
|
|
#include "interface/Point.h"
|
|
|
|
|
|
|
|
#include "search/Save.h"
|
|
|
|
|
2012-01-28 13:56:13 -06:00
|
|
|
Client::Client():
|
|
|
|
authUser(0, "")
|
2012-01-19 07:44:59 -06:00
|
|
|
{
|
|
|
|
int i = 0;
|
2012-01-31 12:49:14 -06:00
|
|
|
std::string proxyString("");
|
2012-01-19 11:59:00 -06:00
|
|
|
for(i = 0; i < THUMB_CACHE_SIZE; i++)
|
2012-01-19 07:44:59 -06:00
|
|
|
{
|
|
|
|
thumbnailCache[i] = NULL;
|
|
|
|
}
|
2012-01-19 11:59:00 -06:00
|
|
|
for(i = 0; i < IMGCONNS; i++)
|
2012-01-19 07:44:59 -06:00
|
|
|
{
|
|
|
|
activeThumbRequests[i] = NULL;
|
|
|
|
activeThumbRequestTimes[i] = 0;
|
2012-01-19 11:59:00 -06:00
|
|
|
activeThumbRequestCompleteTimes[i] = 0;
|
2012-01-19 07:44:59 -06:00
|
|
|
}
|
2012-01-31 12:49:14 -06:00
|
|
|
|
|
|
|
//Read config
|
|
|
|
std::ifstream configFile;
|
|
|
|
configFile.open("powder.pref", ios::binary);
|
|
|
|
if(configFile)
|
|
|
|
{
|
|
|
|
int fsize = configFile.tellg();
|
|
|
|
configFile.seekg(0, std::ios::end);
|
2012-04-19 19:45:43 -05:00
|
|
|
fsize = configFile.tellg() - (std::streampos)fsize;
|
2012-01-31 12:49:14 -06:00
|
|
|
configFile.seekg(0, ios::beg);
|
|
|
|
if(fsize)
|
|
|
|
{
|
|
|
|
json::Reader::Read(configDocument, configFile);
|
|
|
|
try
|
|
|
|
{
|
|
|
|
authUser.ID = ((json::Number)(configDocument["User"]["ID"])).Value();
|
|
|
|
authUser.SessionID = ((json::String)(configDocument["User"]["SessionID"])).Value();
|
|
|
|
authUser.SessionKey = ((json::String)(configDocument["User"]["SessionKey"])).Value();
|
|
|
|
authUser.Username = ((json::String)(configDocument["User"]["Username"])).Value();
|
2012-04-14 13:00:24 -05:00
|
|
|
|
|
|
|
std::string userElevation = ((json::String)(configDocument["User"]["Elevation"])).Value();
|
|
|
|
if(userElevation == "Admin")
|
|
|
|
authUser.UserElevation = ElevationAdmin;
|
|
|
|
else if(userElevation == "Mod")
|
|
|
|
authUser.UserElevation = ElevationModerator;
|
|
|
|
else
|
|
|
|
authUser.UserElevation= ElevationNone;
|
2012-01-31 12:49:14 -06:00
|
|
|
}
|
|
|
|
catch (json::Exception &e)
|
|
|
|
{
|
|
|
|
authUser = User(0, "");
|
|
|
|
std::cerr << "Error: Client [Read User data from pref] " << e.what() << std::endl;
|
|
|
|
}
|
|
|
|
try
|
|
|
|
{
|
|
|
|
proxyString = ((json::String)(configDocument["Proxy"])).Value();
|
|
|
|
}
|
|
|
|
catch (json::Exception &e)
|
|
|
|
{
|
|
|
|
proxyString = "";
|
|
|
|
std::cerr << "Error: Client [Read Proxy from pref] " << e.what() << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
configFile.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
if(proxyString.length())
|
|
|
|
{
|
|
|
|
http_init((char *)proxyString.c_str());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
http_init(NULL);
|
|
|
|
}
|
2012-04-02 11:01:28 -05:00
|
|
|
|
|
|
|
//Read stamps library
|
|
|
|
std::ifstream stampsLib;
|
|
|
|
stampsLib.open(STAMPS_DIR PATH_SEP "stamps.def", ios::binary);
|
|
|
|
while(true)
|
|
|
|
{
|
|
|
|
char data[11];
|
|
|
|
memset(data, 0, 11);
|
|
|
|
if(stampsLib.readsome(data, 10)!=10)
|
|
|
|
break;
|
|
|
|
if(!data[0])
|
|
|
|
break;
|
|
|
|
stampIDs.push_back(data);
|
|
|
|
}
|
|
|
|
stampsLib.close();
|
2012-01-19 07:44:59 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
Client::~Client()
|
|
|
|
{
|
2012-01-21 07:19:10 -06:00
|
|
|
ClearThumbnailRequests();
|
2012-01-19 07:44:59 -06:00
|
|
|
http_done();
|
2012-01-31 12:49:14 -06:00
|
|
|
|
|
|
|
//Save config
|
|
|
|
std::ofstream configFile;
|
|
|
|
configFile.open("powder.pref", ios::trunc);
|
|
|
|
if(configFile)
|
|
|
|
{
|
|
|
|
if(authUser.ID)
|
|
|
|
{
|
|
|
|
configDocument["User"]["ID"] = json::Number(authUser.ID);
|
|
|
|
configDocument["User"]["SessionID"] = json::String(authUser.SessionID);
|
|
|
|
configDocument["User"]["SessionKey"] = json::String(authUser.SessionKey);
|
|
|
|
configDocument["User"]["Username"] = json::String(authUser.Username);
|
2012-04-14 13:00:24 -05:00
|
|
|
if(authUser.UserElevation == ElevationAdmin)
|
|
|
|
configDocument["User"]["Elevation"] = json::String("Admin");
|
|
|
|
else if(authUser.UserElevation == ElevationModerator)
|
|
|
|
configDocument["User"]["Elevation"] = json::String("Mod");
|
|
|
|
else
|
|
|
|
configDocument["User"]["Elevation"] = json::String("None");
|
2012-01-31 12:49:14 -06:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
configDocument["User"] = json::Null();
|
|
|
|
}
|
|
|
|
json::Writer::Write(configDocument, configFile);
|
|
|
|
configFile.close();
|
|
|
|
}
|
2012-01-19 07:44:59 -06:00
|
|
|
}
|
|
|
|
|
2012-01-31 12:49:14 -06:00
|
|
|
|
2012-01-28 13:56:13 -06:00
|
|
|
void Client::SetAuthUser(User user)
|
|
|
|
{
|
|
|
|
authUser = user;
|
|
|
|
}
|
|
|
|
|
|
|
|
User Client::GetAuthUser()
|
|
|
|
{
|
|
|
|
return authUser;
|
|
|
|
}
|
|
|
|
|
2012-01-29 18:40:28 -06:00
|
|
|
RequestStatus Client::UploadSave(Save * save)
|
|
|
|
{
|
|
|
|
lastError = "";
|
|
|
|
int dataStatus;
|
|
|
|
char * data;
|
|
|
|
int dataLength = 0;
|
|
|
|
std::stringstream userIDStream;
|
|
|
|
userIDStream << authUser.ID;
|
|
|
|
if(authUser.ID)
|
|
|
|
{
|
|
|
|
char * postNames[] = { "Name", "Description", "Data:save.bin", "Publish", NULL };
|
|
|
|
char * postDatas[] = { (char *)(save->name.c_str()), (char *)(save->Description.c_str()), (char *)(save->GetData()), (char *)(save->Published?"Public":"Private") };
|
|
|
|
int postLengths[] = { save->name.length(), save->Description.length(), save->GetDataLength(), save->Published?6:7 };
|
|
|
|
//std::cout << postNames[0] << " " << postDatas[0] << " " << postLengths[0] << std::endl;
|
|
|
|
data = http_multipart_post("http://" SERVER "/Save.api", postNames, postDatas, postLengths, (char *)(userIDStream.str().c_str()), NULL, (char *)(authUser.SessionID.c_str()), &dataStatus, &dataLength);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
lastError = "Not authenticated";
|
|
|
|
return RequestFailure;
|
|
|
|
}
|
|
|
|
if(data && dataStatus == 200)
|
|
|
|
{
|
|
|
|
if(strncmp((const char *)data, "OK", 2)!=0)
|
|
|
|
{
|
|
|
|
free(data);
|
|
|
|
lastError = std::string((const char *)data);
|
|
|
|
return RequestFailure;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int tempID;
|
|
|
|
std::stringstream saveIDStream((char *)(data+3));
|
|
|
|
saveIDStream >> tempID;
|
|
|
|
if(!tempID)
|
|
|
|
{
|
|
|
|
lastError = "Server did not return Save ID";
|
|
|
|
return RequestFailure;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
save->id = tempID;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(data);
|
|
|
|
return RequestOkay;
|
|
|
|
}
|
|
|
|
else if(data)
|
|
|
|
{
|
|
|
|
free(data);
|
|
|
|
}
|
|
|
|
return RequestFailure;
|
|
|
|
}
|
|
|
|
|
2012-04-02 11:01:28 -05:00
|
|
|
Save * Client::GetStamp(string stampID)
|
|
|
|
{
|
|
|
|
std::ifstream stampFile;
|
|
|
|
stampFile.open(string(STAMPS_DIR PATH_SEP + stampID + ".stm").c_str(), ios::binary);
|
|
|
|
if(stampFile.is_open())
|
|
|
|
{
|
|
|
|
stampFile.seekg(0, ios::end);
|
|
|
|
size_t fileSize = stampFile.tellg();
|
|
|
|
stampFile.seekg(0);
|
|
|
|
|
|
|
|
unsigned char * tempData = (unsigned char *)malloc(fileSize);
|
|
|
|
stampFile.read((char *)tempData, fileSize);
|
|
|
|
stampFile.close();
|
|
|
|
|
|
|
|
|
2012-04-19 09:22:18 -05:00
|
|
|
Save * tempSave = new Save(0, 0, 0, 0, "", stampID);
|
2012-04-02 11:01:28 -05:00
|
|
|
tempSave->SetData(tempData, fileSize);
|
|
|
|
return tempSave;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Client::DeleteStamp(string stampID)
|
|
|
|
{
|
2012-04-19 09:22:18 -05:00
|
|
|
for(int i = 0; i < stampIDs.size(); i++)
|
|
|
|
{
|
|
|
|
if(stampIDs[i] == stampID)
|
|
|
|
{
|
|
|
|
remove(string(STAMPS_DIR PATH_SEP + stampID + ".stm").c_str());
|
|
|
|
stampIDs.erase(stampIDs.begin()+i);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2012-04-02 11:01:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
string Client::AddStamp(Save * saveData)
|
|
|
|
{
|
|
|
|
unsigned t=(unsigned)time(NULL);
|
|
|
|
if (lastStampTime!=t)
|
|
|
|
{
|
|
|
|
lastStampTime=t;
|
|
|
|
lastStampName=0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
lastStampName++;
|
|
|
|
std::stringstream saveID;
|
|
|
|
//sprintf(saveID, "%08x%02x", lastStampTime, lastStampName);
|
|
|
|
saveID
|
|
|
|
<< std::setw(8) << std::setfill('0') << std::hex << lastStampTime
|
|
|
|
<< std::setw(2) << std::setfill('0') << std::hex << lastStampName;
|
|
|
|
|
|
|
|
#ifdef WIN32
|
|
|
|
_mkdir(STAMPS_DIR);
|
|
|
|
#else
|
|
|
|
mkdir(STAMPS_DIR, 0755);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
std::ofstream stampStream;
|
|
|
|
stampStream.open(string(STAMPS_DIR PATH_SEP + saveID.str()+".stm").c_str(), ios::binary);
|
|
|
|
stampStream.write((const char *)saveData->data, saveData->dataLength);
|
|
|
|
stampStream.close();
|
|
|
|
|
|
|
|
stampIDs.push_back(saveID.str());
|
|
|
|
|
|
|
|
updateStamps();
|
|
|
|
|
|
|
|
return saveID.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Client::updateStamps()
|
|
|
|
{
|
|
|
|
|
|
|
|
#ifdef WIN32
|
|
|
|
_mkdir(STAMPS_DIR);
|
|
|
|
#else
|
|
|
|
mkdir(STAMPS_DIR, 0755);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
std::ofstream stampsStream;
|
|
|
|
stampsStream.open(string(STAMPS_DIR PATH_SEP "stamps.def").c_str(), ios::binary);
|
|
|
|
for(int i = 0; i < stampIDs.size(); i++)
|
|
|
|
{
|
|
|
|
stampsStream.write(stampIDs[i].c_str(), 10);
|
|
|
|
}
|
|
|
|
stampsStream.write("\0", 1);
|
|
|
|
stampsStream.close();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
vector<string> Client::GetStamps()
|
|
|
|
{
|
|
|
|
return stampIDs;
|
|
|
|
}
|
|
|
|
|
2012-01-28 13:56:13 -06:00
|
|
|
RequestStatus Client::ExecVote(int saveID, int direction)
|
|
|
|
{
|
|
|
|
lastError = "";
|
|
|
|
int dataStatus;
|
|
|
|
char * data;
|
|
|
|
int dataLength = 0;
|
|
|
|
std::stringstream idStream;
|
|
|
|
idStream << saveID;
|
|
|
|
std::string directionS;
|
|
|
|
if(direction==1)
|
|
|
|
{
|
|
|
|
directionS = "Up";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
directionS = "Down";
|
|
|
|
}
|
|
|
|
std::stringstream userIDStream;
|
|
|
|
userIDStream << authUser.ID;
|
|
|
|
if(authUser.ID)
|
|
|
|
{
|
|
|
|
char * postNames[] = { "ID", "Action", NULL };
|
|
|
|
char * postDatas[] = { (char*)(idStream.str().c_str()), (char*)(directionS.c_str()) };
|
|
|
|
int postLengths[] = { idStream.str().length(), directionS.length() };
|
|
|
|
//std::cout << postNames[0] << " " << postDatas[0] << " " << postLengths[0] << std::endl;
|
|
|
|
data = http_multipart_post("http://" SERVER "/Vote.api", postNames, postDatas, postLengths, (char *)(userIDStream.str().c_str()), NULL, (char *)(authUser.SessionID.c_str()), &dataStatus, &dataLength);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
lastError = "Not authenticated";
|
|
|
|
return RequestFailure;
|
|
|
|
}
|
|
|
|
if(data && dataStatus == 200)
|
|
|
|
{
|
|
|
|
if(strncmp((const char *)data, "OK", 2)!=0)
|
|
|
|
{
|
|
|
|
free(data);
|
|
|
|
lastError = std::string((const char *)data);
|
|
|
|
return RequestFailure;
|
|
|
|
}
|
|
|
|
free(data);
|
|
|
|
return RequestOkay;
|
|
|
|
}
|
|
|
|
else if(data)
|
|
|
|
{
|
|
|
|
free(data);
|
|
|
|
}
|
|
|
|
lastError = http_ret_text(dataStatus);
|
|
|
|
return RequestFailure;
|
|
|
|
}
|
|
|
|
|
2012-01-25 19:13:33 -06:00
|
|
|
unsigned char * Client::GetSaveData(int saveID, int saveDate, int & dataLength)
|
|
|
|
{
|
2012-01-26 10:18:43 -06:00
|
|
|
lastError = "";
|
|
|
|
int dataStatus;
|
|
|
|
unsigned char * data;
|
2012-01-25 19:13:33 -06:00
|
|
|
dataLength = 0;
|
2012-01-26 10:18:43 -06:00
|
|
|
std::stringstream urlStream;
|
|
|
|
if(saveDate)
|
|
|
|
{
|
|
|
|
urlStream << "http://" << STATICSERVER << "/" << saveID << "_" << saveDate << ".cps";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
urlStream << "http://" << STATICSERVER << "/" << saveID << ".cps";
|
|
|
|
}
|
2012-05-15 12:13:17 -05:00
|
|
|
|
2012-01-26 10:18:43 -06:00
|
|
|
data = (unsigned char *)http_simple_get((char *)urlStream.str().c_str(), &dataStatus, &dataLength);
|
|
|
|
if(data && dataStatus == 200)
|
|
|
|
{
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
else if(data)
|
|
|
|
{
|
|
|
|
free(data);
|
|
|
|
}
|
2012-01-25 19:13:33 -06:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-01-25 11:21:55 -06:00
|
|
|
LoginStatus Client::Login(string username, string password, User & user)
|
2012-01-24 14:19:19 -06:00
|
|
|
{
|
2012-01-24 18:57:39 -06:00
|
|
|
lastError = "";
|
2012-01-24 14:19:19 -06:00
|
|
|
std::stringstream urlStream;
|
2012-01-24 18:57:39 -06:00
|
|
|
std::stringstream hashStream;
|
2012-01-25 11:21:55 -06:00
|
|
|
char passwordHash[33];
|
|
|
|
char totalHash[33];
|
2012-01-24 18:57:39 -06:00
|
|
|
|
2012-01-25 11:21:55 -06:00
|
|
|
user.ID = 0;
|
|
|
|
user.Username = "";
|
|
|
|
user.SessionID = "";
|
|
|
|
user.SessionKey = "";
|
|
|
|
|
|
|
|
//Doop
|
|
|
|
md5_ascii(passwordHash, (const unsigned char *)password.c_str(), password.length());
|
|
|
|
passwordHash[32] = 0;
|
|
|
|
hashStream << username << "-" << passwordHash;
|
|
|
|
md5_ascii(totalHash, (const unsigned char *)(hashStream.str().c_str()), hashStream.str().length());
|
|
|
|
totalHash[32] = 0;
|
2012-01-24 18:57:39 -06:00
|
|
|
|
2012-01-24 14:19:19 -06:00
|
|
|
char * data;
|
|
|
|
int dataStatus, dataLength;
|
2012-01-24 18:57:39 -06:00
|
|
|
char * postNames[] = { "Username", "Hash", NULL };
|
2012-01-25 11:21:55 -06:00
|
|
|
char * postDatas[] = { (char*)username.c_str(), totalHash };
|
|
|
|
int postLengths[] = { username.length(), 32 };
|
2012-01-24 18:57:39 -06:00
|
|
|
data = http_multipart_post("http://" SERVER "/Login.json", postNames, postDatas, postLengths, NULL, NULL, NULL, &dataStatus, &dataLength);
|
|
|
|
//data = http_auth_get("http://" SERVER "/Login.json", (char*)username.c_str(), (char*)password.c_str(), NULL, &dataStatus, &dataLength);
|
2012-01-24 14:19:19 -06:00
|
|
|
if(dataStatus == 200 && data)
|
|
|
|
{
|
2012-01-24 18:57:39 -06:00
|
|
|
try
|
2012-01-24 14:19:19 -06:00
|
|
|
{
|
2012-01-24 18:57:39 -06:00
|
|
|
std::istringstream dataStream(data);
|
|
|
|
json::Object objDocument;
|
|
|
|
json::Reader::Read(objDocument, dataStream);
|
|
|
|
json::Number tempStatus = objDocument["Status"];
|
|
|
|
|
|
|
|
free(data);
|
|
|
|
if(tempStatus.Value() == 1)
|
|
|
|
{
|
2012-01-25 11:21:55 -06:00
|
|
|
json::Number userIDTemp = objDocument["UserID"];
|
|
|
|
json::String sessionIDTemp = objDocument["SessionID"];
|
|
|
|
json::String sessionKeyTemp = objDocument["SessionKey"];
|
2012-04-14 13:00:24 -05:00
|
|
|
json::String userElevationTemp = objDocument["Elevation"];
|
2012-01-25 11:21:55 -06:00
|
|
|
user.Username = username;
|
|
|
|
user.ID = userIDTemp.Value();
|
|
|
|
user.SessionID = sessionIDTemp.Value();
|
|
|
|
user.SessionKey = sessionKeyTemp.Value();
|
2012-04-14 13:00:24 -05:00
|
|
|
std::string userElevation = userElevationTemp.Value();
|
|
|
|
if(userElevation == "Admin")
|
|
|
|
user.UserElevation = ElevationAdmin;
|
|
|
|
else if(userElevation == "Mod")
|
|
|
|
user.UserElevation = ElevationModerator;
|
|
|
|
else
|
|
|
|
user.UserElevation= ElevationNone;
|
2012-01-24 18:57:39 -06:00
|
|
|
return LoginOkay;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
json::String tempError = objDocument["Error"];
|
|
|
|
lastError = tempError.Value();
|
|
|
|
return LoginError;
|
|
|
|
}
|
2012-01-24 14:19:19 -06:00
|
|
|
}
|
2012-01-24 18:57:39 -06:00
|
|
|
catch (json::Exception &e)
|
2012-01-24 14:19:19 -06:00
|
|
|
{
|
2012-01-24 18:57:39 -06:00
|
|
|
lastError = "Server responded with crap";
|
2012-01-24 14:19:19 -06:00
|
|
|
return LoginError;
|
|
|
|
}
|
|
|
|
}
|
2012-01-25 11:21:55 -06:00
|
|
|
else
|
|
|
|
{
|
|
|
|
lastError = http_ret_text(dataStatus);
|
|
|
|
}
|
2012-01-24 14:19:19 -06:00
|
|
|
if(data)
|
|
|
|
{
|
|
|
|
free(data);
|
|
|
|
}
|
|
|
|
return LoginError;
|
|
|
|
}
|
|
|
|
|
2012-04-06 18:45:24 -05:00
|
|
|
RequestStatus Client::DeleteSave(int saveID)
|
|
|
|
{
|
|
|
|
lastError = "";
|
|
|
|
std::vector<string> * tags = NULL;
|
|
|
|
std::stringstream urlStream;
|
|
|
|
char * data = NULL;
|
|
|
|
int dataStatus, dataLength;
|
2012-04-14 13:00:24 -05:00
|
|
|
urlStream << "http://" << SERVER << "/Browse/Delete.json?ID=" << saveID << "&Mode=Delete&Key=" << authUser.SessionKey;
|
|
|
|
if(authUser.ID)
|
|
|
|
{
|
|
|
|
std::stringstream userIDStream;
|
|
|
|
userIDStream << authUser.ID;
|
|
|
|
data = http_auth_get((char *)urlStream.str().c_str(), (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)
|
|
|
|
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 = "";
|
|
|
|
std::vector<string> * tags = NULL;
|
|
|
|
std::stringstream urlStream;
|
|
|
|
char * data = NULL;
|
|
|
|
int dataStatus, dataLength;
|
|
|
|
urlStream << "http://" << SERVER << "/Browse/Favourite.json?ID=" << saveID << "&Key=" << authUser.SessionKey;
|
|
|
|
if(!favourite)
|
|
|
|
urlStream << "&Mode=Remove";
|
|
|
|
if(authUser.ID)
|
|
|
|
{
|
|
|
|
std::stringstream userIDStream;
|
|
|
|
userIDStream << authUser.ID;
|
|
|
|
data = http_auth_get((char *)urlStream.str().c_str(), (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::String)objDocument["Error"]).Value();
|
|
|
|
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::ReportSave(int saveID, std::string message)
|
|
|
|
{
|
|
|
|
lastError = "";
|
|
|
|
std::vector<string> * tags = NULL;
|
|
|
|
std::stringstream urlStream;
|
|
|
|
char * data = NULL;
|
|
|
|
int dataStatus, dataLength;
|
|
|
|
urlStream << "http://" << SERVER << "/Browse/Report.json?ID=" << saveID << "&Key=" << authUser.SessionKey;
|
|
|
|
if(authUser.ID)
|
|
|
|
{
|
|
|
|
std::stringstream userIDStream;
|
|
|
|
userIDStream << authUser.ID;
|
|
|
|
|
|
|
|
char * postNames[] = { "Reason", NULL };
|
|
|
|
char * postDatas[] = { (char*)(message.c_str()) };
|
|
|
|
int postLengths[] = { message.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::String)objDocument["Error"]).Value();
|
|
|
|
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::UnpublishSave(int saveID)
|
|
|
|
{
|
|
|
|
lastError = "";
|
|
|
|
std::vector<string> * tags = NULL;
|
|
|
|
std::stringstream urlStream;
|
|
|
|
char * data = NULL;
|
|
|
|
int dataStatus, dataLength;
|
|
|
|
urlStream << "http://" << SERVER << "/Browse/Delete.json?ID=" << saveID << "&Mode=Unpublish&Key=" << authUser.SessionKey;
|
2012-04-06 18:45:24 -05:00
|
|
|
if(authUser.ID)
|
|
|
|
{
|
|
|
|
std::stringstream userIDStream;
|
|
|
|
userIDStream << authUser.ID;
|
|
|
|
data = http_auth_get((char *)urlStream.str().c_str(), (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)
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-01-23 16:53:57 -06:00
|
|
|
Save * Client::GetSave(int saveID, int saveDate)
|
|
|
|
{
|
|
|
|
lastError = "";
|
|
|
|
std::stringstream urlStream;
|
|
|
|
urlStream << "http://" << SERVER << "/Browse/View.json?ID=" << saveID;
|
|
|
|
if(saveDate)
|
|
|
|
{
|
|
|
|
urlStream << "&Date=" << saveDate;
|
|
|
|
}
|
|
|
|
char * data;
|
|
|
|
int dataStatus, dataLength;
|
|
|
|
//Save(int _id, int _votesUp, int _votesDown, string _userName, string _name, string description_, string date_, bool published_):
|
2012-01-28 13:56:13 -06:00
|
|
|
if(authUser.ID)
|
|
|
|
{
|
|
|
|
std::stringstream userIDStream;
|
|
|
|
userIDStream << authUser.ID;
|
|
|
|
data = http_auth_get((char *)urlStream.str().c_str(), (char *)(userIDStream.str().c_str()), NULL, (char *)(authUser.SessionID.c_str()), &dataStatus, &dataLength);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
data = http_simple_get((char *)urlStream.str().c_str(), &dataStatus, &dataLength);
|
|
|
|
}
|
2012-01-23 16:53:57 -06:00
|
|
|
if(dataStatus == 200 && data)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
std::istringstream dataStream(data);
|
|
|
|
json::Object objDocument;
|
|
|
|
json::Reader::Read(objDocument, dataStream);
|
|
|
|
|
|
|
|
json::Number tempID = objDocument["ID"];
|
|
|
|
json::Number tempScoreUp = objDocument["ScoreUp"];
|
|
|
|
json::Number tempScoreDown = objDocument["ScoreDown"];
|
2012-01-28 13:56:13 -06:00
|
|
|
json::Number tempMyScore = objDocument["ScoreMine"];
|
2012-01-23 16:53:57 -06:00
|
|
|
json::String tempUsername = objDocument["Username"];
|
|
|
|
json::String tempName = objDocument["Name"];
|
|
|
|
json::String tempDescription = objDocument["Description"];
|
2012-01-25 19:13:33 -06:00
|
|
|
json::Number tempDate = objDocument["Date"];
|
2012-01-23 16:53:57 -06:00
|
|
|
json::Boolean tempPublished = objDocument["Published"];
|
2012-04-14 13:00:24 -05:00
|
|
|
json::Boolean tempFavourite = objDocument["Favourite"];
|
2012-03-22 08:50:43 -05:00
|
|
|
|
|
|
|
json::Array tagsArray = objDocument["Tags"];
|
|
|
|
vector<string> tempTags;
|
|
|
|
|
|
|
|
for(int j = 0; j < tagsArray.Size(); j++)
|
|
|
|
{
|
|
|
|
json::String tempTag = tagsArray[j];
|
|
|
|
tempTags.push_back(tempTag.Value());
|
|
|
|
}
|
|
|
|
|
2012-04-14 13:00:24 -05:00
|
|
|
Save * tempSave = new Save(
|
2012-01-23 16:53:57 -06:00
|
|
|
tempID.Value(),
|
2012-01-25 19:13:33 -06:00
|
|
|
tempDate.Value(),
|
2012-01-23 16:53:57 -06:00
|
|
|
tempScoreUp.Value(),
|
|
|
|
tempScoreDown.Value(),
|
2012-01-28 13:56:13 -06:00
|
|
|
tempMyScore.Value(),
|
2012-01-23 16:53:57 -06:00
|
|
|
tempUsername.Value(),
|
|
|
|
tempName.Value(),
|
|
|
|
tempDescription.Value(),
|
2012-03-22 08:50:43 -05:00
|
|
|
tempPublished.Value(),
|
|
|
|
tempTags
|
2012-01-23 16:53:57 -06:00
|
|
|
);
|
2012-04-14 13:00:24 -05:00
|
|
|
tempSave->Favourite = tempFavourite.Value();
|
|
|
|
return tempSave;
|
2012-01-23 16:53:57 -06:00
|
|
|
}
|
|
|
|
catch (json::Exception &e)
|
|
|
|
{
|
|
|
|
lastError = "Could not read response";
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
lastError = http_ret_text(dataStatus);
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
Thumbnail * Client::GetPreview(int saveID, int saveDate)
|
|
|
|
{
|
|
|
|
std::stringstream urlStream;
|
2012-01-26 10:18:43 -06:00
|
|
|
urlStream << "http://" << STATICSERVER << "/" << saveID;
|
2012-01-23 16:53:57 -06:00
|
|
|
if(saveDate)
|
|
|
|
{
|
2012-01-26 10:18:43 -06:00
|
|
|
urlStream << "_" << saveDate;
|
2012-01-23 16:53:57 -06:00
|
|
|
}
|
2012-01-26 10:18:43 -06:00
|
|
|
urlStream << "_large.pti";
|
2012-01-23 16:53:57 -06:00
|
|
|
pixel * thumbData;
|
|
|
|
char * data;
|
|
|
|
int status, data_size, imgw, imgh;
|
|
|
|
data = http_simple_get((char *)urlStream.str().c_str(), &status, &data_size);
|
|
|
|
if (status == 200 && data)
|
|
|
|
{
|
|
|
|
thumbData = Graphics::ptif_unpack(data, data_size, &imgw, &imgh);
|
|
|
|
if(data)
|
|
|
|
{
|
|
|
|
free(data);
|
|
|
|
}
|
|
|
|
if(thumbData)
|
|
|
|
{
|
|
|
|
return new Thumbnail(saveID, saveDate, thumbData, ui::Point(imgw, imgh));
|
2012-04-03 11:08:56 -05:00
|
|
|
free(thumbData);
|
2012-01-23 16:53:57 -06:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-04-03 11:08:56 -05:00
|
|
|
thumbData = (pixel *)malloc((128*128) * PIXELSIZE);
|
|
|
|
return new Thumbnail(saveID, saveDate, thumbData, ui::Point(128, 128));
|
|
|
|
free(thumbData);
|
2012-01-23 16:53:57 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(data)
|
|
|
|
{
|
|
|
|
free(data);
|
|
|
|
}
|
|
|
|
}
|
2012-04-14 13:00:24 -05:00
|
|
|
return new Thumbnail(saveID, saveDate, (pixel *)malloc((128*128) * PIXELSIZE), ui::Point(128, 128));
|
2012-01-23 16:53:57 -06:00
|
|
|
}
|
|
|
|
|
2012-05-31 04:24:44 -05:00
|
|
|
std::vector<SaveComment*> * Client::GetComments(int saveID, int start, int count)
|
2012-02-11 10:08:59 -06:00
|
|
|
{
|
|
|
|
lastError = "";
|
2012-05-31 04:24:44 -05:00
|
|
|
std::vector<SaveComment*> * commentArray = new std::vector<SaveComment*>();
|
2012-02-11 10:08:59 -06:00
|
|
|
|
|
|
|
std::stringstream urlStream;
|
|
|
|
char * data;
|
|
|
|
int dataStatus, dataLength;
|
|
|
|
urlStream << "http://" << SERVER << "/Browse/View.json?ID=" << saveID << "&Mode=Comments&Start=" << start << "&Count=" << count;
|
|
|
|
data = http_simple_get((char *)urlStream.str().c_str(), &dataStatus, &dataLength);
|
|
|
|
if(dataStatus == 200 && data)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
std::istringstream dataStream(data);
|
|
|
|
json::Array commentsArray;
|
|
|
|
json::Reader::Read(commentsArray, dataStream);
|
|
|
|
|
|
|
|
for(int j = 0; j < commentsArray.Size(); j++)
|
|
|
|
{
|
|
|
|
json::Number tempUserID = commentsArray[j]["UserID"];
|
|
|
|
json::String tempUsername = commentsArray[j]["Username"];
|
|
|
|
json::String tempComment = commentsArray[j]["Text"];
|
|
|
|
commentArray->push_back(
|
2012-05-31 04:24:44 -05:00
|
|
|
new SaveComment(
|
2012-02-11 10:08:59 -06:00
|
|
|
tempUserID.Value(),
|
|
|
|
tempUsername.Value(),
|
|
|
|
tempComment.Value()
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (json::Exception &e)
|
|
|
|
{
|
|
|
|
lastError = "Could not read response";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
lastError = http_ret_text(dataStatus);
|
|
|
|
}
|
|
|
|
if(data)
|
|
|
|
free(data);
|
|
|
|
return commentArray;
|
|
|
|
}
|
|
|
|
|
2012-04-14 15:11:54 -05:00
|
|
|
std::vector<Save*> * Client::SearchSaves(int start, int count, string query, string sort, std::string category, int & resultCount)
|
2012-01-19 14:10:05 -06:00
|
|
|
{
|
|
|
|
lastError = "";
|
2012-01-21 12:51:28 -06:00
|
|
|
resultCount = 0;
|
2012-01-20 18:17:42 -06:00
|
|
|
std::vector<Save*> * saveArray = new std::vector<Save*>();
|
2012-01-19 14:10:05 -06:00
|
|
|
std::stringstream urlStream;
|
|
|
|
char * data;
|
|
|
|
int dataStatus, dataLength;
|
2012-01-28 13:56:13 -06:00
|
|
|
urlStream << "http://" << SERVER << "/Browse.json?Start=" << start << "&Count=" << count;
|
2012-01-19 14:10:05 -06:00
|
|
|
if(query.length() || sort.length())
|
|
|
|
{
|
|
|
|
urlStream << "&Search_Query=";
|
|
|
|
if(query.length())
|
2012-02-01 15:20:27 -06:00
|
|
|
urlStream << URLEscape(query);
|
2012-01-21 12:51:28 -06:00
|
|
|
if(sort == "date")
|
2012-01-19 14:10:05 -06:00
|
|
|
{
|
|
|
|
if(query.length())
|
2012-02-01 15:20:27 -06:00
|
|
|
urlStream << URLEscape(" ");
|
|
|
|
urlStream << URLEscape("sort:") << URLEscape(sort);
|
2012-01-19 14:10:05 -06:00
|
|
|
}
|
|
|
|
}
|
2012-04-14 15:11:54 -05:00
|
|
|
if(category.length())
|
|
|
|
{
|
|
|
|
urlStream << "&Category=" << URLEscape(category);
|
|
|
|
}
|
|
|
|
if(authUser.ID)
|
|
|
|
{
|
|
|
|
std::stringstream userIDStream;
|
|
|
|
userIDStream << authUser.ID;
|
|
|
|
data = http_auth_get((char *)urlStream.str().c_str(), (char *)(userIDStream.str().c_str()), NULL, (char *)(authUser.SessionID.c_str()), &dataStatus, &dataLength);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
data = http_simple_get((char *)urlStream.str().c_str(), &dataStatus, &dataLength);
|
|
|
|
}
|
2012-01-19 14:10:05 -06:00
|
|
|
if(dataStatus == 200 && data)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2012-01-23 16:53:57 -06:00
|
|
|
std::istringstream dataStream(data);
|
2012-01-19 14:10:05 -06:00
|
|
|
json::Object objDocument;
|
|
|
|
json::Reader::Read(objDocument, dataStream);
|
|
|
|
|
2012-01-21 12:51:28 -06:00
|
|
|
json::Number tempCount = objDocument["Count"];
|
|
|
|
resultCount = tempCount.Value();
|
2012-01-19 14:10:05 -06:00
|
|
|
json::Array savesArray = objDocument["Saves"];
|
|
|
|
for(int j = 0; j < savesArray.Size(); j++)
|
|
|
|
{
|
|
|
|
json::Number tempID = savesArray[j]["ID"];
|
2012-01-25 19:13:33 -06:00
|
|
|
json::Number tempDate = savesArray[j]["Date"];
|
2012-01-19 14:10:05 -06:00
|
|
|
json::Number tempScoreUp = savesArray[j]["ScoreUp"];
|
|
|
|
json::Number tempScoreDown = savesArray[j]["ScoreDown"];
|
|
|
|
json::String tempUsername = savesArray[j]["Username"];
|
|
|
|
json::String tempName = savesArray[j]["Name"];
|
2012-01-20 18:17:42 -06:00
|
|
|
saveArray->push_back(
|
|
|
|
new Save(
|
2012-01-19 14:10:05 -06:00
|
|
|
tempID.Value(),
|
2012-01-25 19:13:33 -06:00
|
|
|
tempDate.Value(),
|
2012-01-19 14:10:05 -06:00
|
|
|
tempScoreUp.Value(),
|
|
|
|
tempScoreDown.Value(),
|
|
|
|
tempUsername.Value(),
|
|
|
|
tempName.Value()
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (json::Exception &e)
|
|
|
|
{
|
|
|
|
lastError = "Could not read response";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
lastError = http_ret_text(dataStatus);
|
|
|
|
}
|
|
|
|
if(data)
|
|
|
|
free(data);
|
|
|
|
return saveArray;
|
|
|
|
}
|
|
|
|
|
2012-01-19 07:44:59 -06:00
|
|
|
void Client::ClearThumbnailRequests()
|
|
|
|
{
|
2012-01-19 11:59:00 -06:00
|
|
|
for(int i = 0; i < IMGCONNS; i++)
|
2012-01-19 07:44:59 -06:00
|
|
|
{
|
|
|
|
if(activeThumbRequests[i])
|
|
|
|
{
|
|
|
|
http_async_req_close(activeThumbRequests[i]);
|
|
|
|
activeThumbRequests[i] = NULL;
|
2012-01-19 11:59:00 -06:00
|
|
|
activeThumbRequestTimes[i] = 0;
|
|
|
|
activeThumbRequestCompleteTimes[i] = 0;
|
2012-01-19 07:44:59 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Thumbnail * Client::GetThumbnail(int saveID, int saveDate)
|
|
|
|
{
|
|
|
|
std::stringstream urlStream;
|
|
|
|
std::stringstream idStream;
|
2012-01-19 11:59:00 -06:00
|
|
|
int i = 0, currentTime = time(NULL);
|
|
|
|
//Check active requests for any "forgotten" requests
|
|
|
|
for(i = 0; i < IMGCONNS; i++)
|
|
|
|
{
|
|
|
|
//If the request is active, and we've recieved a response
|
|
|
|
if(activeThumbRequests[i] && http_async_req_status(activeThumbRequests[i]))
|
|
|
|
{
|
|
|
|
//If we haven't already, mark the request as completed
|
|
|
|
if(!activeThumbRequestCompleteTimes[i])
|
|
|
|
{
|
|
|
|
activeThumbRequestCompleteTimes[i] = time(NULL);
|
|
|
|
}
|
2012-01-21 12:51:28 -06:00
|
|
|
else if(activeThumbRequestCompleteTimes[i] < (currentTime-2)) //Otherwise, if it completed more than 2 seconds ago, destroy it.
|
2012-01-19 11:59:00 -06:00
|
|
|
{
|
|
|
|
http_async_req_close(activeThumbRequests[i]);
|
|
|
|
activeThumbRequests[i] = NULL;
|
|
|
|
activeThumbRequestTimes[i] = 0;
|
|
|
|
activeThumbRequestCompleteTimes[i] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for(i = 0; i < THUMB_CACHE_SIZE; i++)
|
2012-01-19 07:44:59 -06:00
|
|
|
{
|
|
|
|
if(thumbnailCache[i] && thumbnailCache[i]->ID == saveID && thumbnailCache[i]->Datestamp == saveDate)
|
|
|
|
return thumbnailCache[i];
|
|
|
|
}
|
2012-01-26 10:18:43 -06:00
|
|
|
urlStream << "http://" << STATICSERVER << "/" << saveID;
|
2012-01-19 07:44:59 -06:00
|
|
|
if(saveDate)
|
|
|
|
{
|
2012-01-26 10:18:43 -06:00
|
|
|
urlStream << "_" << saveDate;
|
2012-01-19 07:44:59 -06:00
|
|
|
}
|
2012-01-26 10:18:43 -06:00
|
|
|
urlStream << "_small.pti";
|
2012-01-19 07:44:59 -06:00
|
|
|
idStream << saveID << ":" << saveDate;
|
|
|
|
std::string idString = idStream.str();
|
|
|
|
bool found = false;
|
2012-01-19 11:59:00 -06:00
|
|
|
for(i = 0; i < IMGCONNS; i++)
|
2012-01-19 07:44:59 -06:00
|
|
|
{
|
|
|
|
if(activeThumbRequests[i] && activeThumbRequestIDs[i] == idString)
|
|
|
|
{
|
|
|
|
found = true;
|
|
|
|
if(http_async_req_status(activeThumbRequests[i]))
|
|
|
|
{
|
|
|
|
pixel * thumbData;
|
|
|
|
char * data;
|
|
|
|
int status, data_size, imgw, imgh;
|
|
|
|
data = http_async_req_stop(activeThumbRequests[i], &status, &data_size);
|
2012-01-21 12:51:28 -06:00
|
|
|
free(activeThumbRequests[i]);
|
2012-01-19 07:44:59 -06:00
|
|
|
activeThumbRequests[i] = NULL;
|
|
|
|
if (status == 200 && data)
|
|
|
|
{
|
|
|
|
thumbData = Graphics::ptif_unpack(data, data_size, &imgw, &imgh);
|
|
|
|
if(data)
|
|
|
|
{
|
|
|
|
free(data);
|
|
|
|
}
|
2012-01-19 11:59:00 -06:00
|
|
|
thumbnailCacheNextID %= THUMB_CACHE_SIZE;
|
2012-01-19 07:44:59 -06:00
|
|
|
if(thumbnailCache[thumbnailCacheNextID])
|
|
|
|
{
|
|
|
|
delete thumbnailCache[thumbnailCacheNextID];
|
|
|
|
}
|
|
|
|
if(thumbData)
|
|
|
|
{
|
|
|
|
thumbnailCache[thumbnailCacheNextID] = new Thumbnail(saveID, saveDate, thumbData, ui::Point(imgw, imgh));
|
2012-04-03 11:08:56 -05:00
|
|
|
free(thumbData);
|
2012-01-19 07:44:59 -06:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-04-03 11:08:56 -05:00
|
|
|
thumbData = (pixel *)malloc((128*128) * PIXELSIZE);
|
|
|
|
thumbnailCache[thumbnailCacheNextID] = new Thumbnail(saveID, saveDate, thumbData, ui::Point(128, 128));
|
|
|
|
free(thumbData);
|
2012-01-19 07:44:59 -06:00
|
|
|
}
|
|
|
|
return thumbnailCache[thumbnailCacheNextID++];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(data)
|
|
|
|
{
|
|
|
|
free(data);
|
|
|
|
}
|
2012-01-19 11:59:00 -06:00
|
|
|
thumbnailCacheNextID %= THUMB_CACHE_SIZE;
|
2012-01-19 07:44:59 -06:00
|
|
|
if(thumbnailCache[thumbnailCacheNextID])
|
|
|
|
{
|
|
|
|
delete thumbnailCache[thumbnailCacheNextID];
|
|
|
|
}
|
2012-04-03 11:08:56 -05:00
|
|
|
thumbData = (pixel *)malloc((128*128) * PIXELSIZE);
|
|
|
|
thumbnailCache[thumbnailCacheNextID] = new Thumbnail(saveID, saveDate, thumbData, ui::Point(128, 128));
|
|
|
|
free(thumbData);
|
2012-01-19 07:44:59 -06:00
|
|
|
return thumbnailCache[thumbnailCacheNextID++];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(!found)
|
|
|
|
{
|
2012-01-19 11:59:00 -06:00
|
|
|
for(i = 0; i < IMGCONNS; i++)
|
2012-01-19 07:44:59 -06:00
|
|
|
{
|
|
|
|
if(!activeThumbRequests[i])
|
|
|
|
{
|
|
|
|
activeThumbRequests[i] = http_async_req_start(NULL, (char *)urlStream.str().c_str(), NULL, 0, 1);
|
2012-01-19 11:59:00 -06:00
|
|
|
activeThumbRequestTimes[i] = currentTime;
|
|
|
|
activeThumbRequestCompleteTimes[i] = 0;
|
2012-01-19 07:44:59 -06:00
|
|
|
activeThumbRequestIDs[i] = idString;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//http_async_req_start(http, urlStream.str().c_str(), NULL, 0, 1);
|
|
|
|
return NULL;
|
|
|
|
}
|
2012-03-22 17:12:16 -05:00
|
|
|
|
|
|
|
std::vector<string> * Client::RemoveTag(int saveID, string tag)
|
|
|
|
{
|
|
|
|
lastError = "";
|
|
|
|
std::vector<string> * tags = NULL;
|
|
|
|
std::stringstream urlStream;
|
|
|
|
char * data = NULL;
|
|
|
|
int dataStatus, dataLength;
|
2012-04-14 13:00:24 -05:00
|
|
|
urlStream << "http://" << SERVER << "/Browse/EditTag.json?Op=delete&ID=" << saveID << "&Tag=" << tag << "&Key=" << authUser.SessionKey;;
|
2012-03-22 17:12:16 -05:00
|
|
|
if(authUser.ID)
|
|
|
|
{
|
|
|
|
std::stringstream userIDStream;
|
|
|
|
userIDStream << authUser.ID;
|
|
|
|
data = http_auth_get((char *)urlStream.str().c_str(), (char *)(userIDStream.str().c_str()), NULL, (char *)(authUser.SessionID.c_str()), &dataStatus, &dataLength);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
lastError = "Not authenticated";
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if(dataStatus == 200 && data)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
std::istringstream dataStream(data);
|
|
|
|
json::Array tagsArray;
|
|
|
|
json::Reader::Read(tagsArray, dataStream);
|
|
|
|
|
|
|
|
tags = new std::vector<string>();
|
|
|
|
|
|
|
|
for(int j = 0; j < tagsArray.Size(); j++)
|
|
|
|
{
|
|
|
|
json::String tempTag = tagsArray[j];
|
|
|
|
tags->push_back(tempTag.Value());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (json::Exception &e)
|
|
|
|
{
|
|
|
|
lastError = "Could not read response";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
lastError = http_ret_text(dataStatus);
|
|
|
|
}
|
|
|
|
if(data)
|
|
|
|
free(data);
|
|
|
|
return tags;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<string> * Client::AddTag(int saveID, string tag)
|
|
|
|
{
|
|
|
|
lastError = "";
|
|
|
|
std::vector<string> * tags = NULL;
|
|
|
|
std::stringstream urlStream;
|
|
|
|
char * data = NULL;
|
|
|
|
int dataStatus, dataLength;
|
2012-04-14 13:00:24 -05:00
|
|
|
urlStream << "http://" << SERVER << "/Browse/EditTag.json?Op=add&ID=" << saveID << "&Tag=" << tag << "&Key=" << authUser.SessionKey;
|
2012-03-22 17:12:16 -05:00
|
|
|
if(authUser.ID)
|
|
|
|
{
|
|
|
|
std::stringstream userIDStream;
|
|
|
|
userIDStream << authUser.ID;
|
|
|
|
data = http_auth_get((char *)urlStream.str().c_str(), (char *)(userIDStream.str().c_str()), NULL, (char *)(authUser.SessionID.c_str()), &dataStatus, &dataLength);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
lastError = "Not authenticated";
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if(dataStatus == 200 && data)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
std::istringstream dataStream(data);
|
|
|
|
json::Array tagsArray;
|
|
|
|
json::Reader::Read(tagsArray, dataStream);
|
|
|
|
|
|
|
|
tags = new std::vector<string>();
|
|
|
|
|
|
|
|
for(int j = 0; j < tagsArray.Size(); j++)
|
|
|
|
{
|
|
|
|
json::String tempTag = tagsArray[j];
|
|
|
|
tags->push_back(tempTag.Value());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (json::Exception &e)
|
|
|
|
{
|
|
|
|
lastError = "Could not read response";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
lastError = http_ret_text(dataStatus);
|
|
|
|
}
|
|
|
|
if(data)
|
|
|
|
free(data);
|
|
|
|
return tags;
|
|
|
|
}
|