00001
00033 #include "llviewerprecompiledheaders.h"
00034
00035 #include "llurlsimstring.h"
00036
00037 #include "llpanellogin.h"
00038 #include "llviewercontrol.h"
00039
00040 #include "curl/curl.h"
00041
00042
00043 LLURLSimString LLURLSimString::sInstance;
00044 LLString LLURLSimString::sLocationStringHome("My Home");
00045 LLString LLURLSimString::sLocationStringLast("My Last Location");
00046
00047
00048
00049
00050 void LLURLSimString::setString(const LLString& sim_string)
00051 {
00052 sInstance.mSimString.clear();
00053 sInstance.mSimName.clear();
00054 sInstance.mParseState = NOT_PARSED;
00055 if (sim_string == sLocationStringHome)
00056 {
00057 gSavedSettings.setBOOL("LoginLastLocation", FALSE);
00058 }
00059 else if (sim_string == sLocationStringLast)
00060 {
00061 gSavedSettings.setBOOL("LoginLastLocation", TRUE);
00062 }
00063 else
00064 {
00065 char* curlstr = curl_unescape(sim_string.c_str(), sim_string.size());
00066 LLString tstring = LLString(curlstr);
00067 curl_free(curlstr);
00068 std::string::size_type idx = tstring.find("//");
00069 idx = (idx == LLString::npos) ? 0 : idx+2;
00070 sInstance.mSimString = tstring.substr(idx);
00071 }
00072 LLPanelLogin::refreshLocation( false );
00073 }
00074
00075
00076
00077 S32 LLURLSimString::parseGridIdx(const LLString& in_string, S32 idx0, S32* res, S32 max)
00078 {
00079 if (idx0 == INT_MAX || in_string[idx0] != '/')
00080 {
00081 return INT_MAX;
00082 }
00083 idx0++;
00084 LLString::size_type idx1 = in_string.find_first_of('/', idx0);
00085 LLString::size_type len = (idx1 == LLString::npos) ? LLString::npos : idx1-idx0;
00086 LLString tstring = in_string.substr(idx0,len);
00087 if (!tstring.empty())
00088 {
00089 S32 val = atoi(tstring.c_str());
00090 *res = llclamp(val,0,max);
00091 }
00092 return idx1;
00093 }
00094
00095
00096
00097 bool LLURLSimString::parse()
00098 {
00099 if (sInstance.mParseState == NOT_SET)
00100 {
00101 return false;
00102 }
00103 if (sInstance.mParseState == NOT_PARSED)
00104 {
00105 if (parse(sInstance.mSimString,
00106 &sInstance.mSimName,
00107 &sInstance.mX,
00108 &sInstance.mY,
00109 &sInstance.mZ))
00110 {
00111 sInstance.mParseState = PARSE_OK;
00112 }
00113 else
00114 {
00115 sInstance.mParseState = PARSE_FAIL;
00116 }
00117 }
00118 return (sInstance.mParseState == PARSE_OK);
00119 }
00120
00121
00122 bool LLURLSimString::parse(const LLString& sim_string, std::string *region_name, S32 *x, S32 *y, S32 *z)
00123 {
00124
00125 LLString::size_type idx0 = sim_string.find_first_not_of('/');
00126 if (idx0 == std::string::npos) idx0 = 0;
00127
00128 LLString::size_type idx1 = sim_string.find_first_of('/', idx0);
00129 LLString::size_type len = (idx1 == std::string::npos) ? std::string::npos : idx1-idx0;
00130 LLString tstring = sim_string.substr(idx0,len);
00131 *region_name = unescapeRegionName(tstring);
00132 if (!region_name->empty())
00133 {
00134 if (idx1 != std::string::npos)
00135 {
00136 idx1 = parseGridIdx(sim_string, idx1, x, 255);
00137 idx1 = parseGridIdx(sim_string, idx1, y, 255);
00138 idx1 = parseGridIdx(sim_string, idx1, z, 1000);
00139 }
00140 return true;
00141 }
00142 else
00143 {
00144 return false;
00145 }
00146 }
00147
00148
00149 std::string LLURLSimString::getURL()
00150 {
00151 std::string url;
00152 if (sInstance.mParseState == PARSE_OK)
00153 {
00154 url = llformat("secondlife://%s/%d/%d/%d/",
00155 sInstance.mSimName.c_str(),
00156 sInstance.mX,
00157 sInstance.mY,
00158 sInstance.mZ);
00159 }
00160 return url;
00161 }
00162
00163
00164 std::string LLURLSimString::unescapeRegionName(std::string region_name)
00165 {
00166 std::string result;
00167 char* curlstr = curl_unescape(region_name.c_str(), region_name.size());
00168 result = std::string(curlstr);
00169 curl_free(curlstr);
00170 return result;
00171 }