00001 00032 #include "llviewerprecompiledheaders.h" 00033 00034 #include "llurlhistory.h" 00035 00036 #include "lldir.h" 00037 #include "llsdserialize.h" 00038 00039 LLSD LLURLHistory::sHistorySD; 00040 00041 const int MAX_URL_COUNT = 10; 00042 00044 00045 // static 00046 bool LLURLHistory::loadFile(const LLString& filename) 00047 { 00048 LLSD data; 00049 { 00050 LLString temp_str = gDirUtilp->getLindenUserDir() + gDirUtilp->getDirDelimiter(); 00051 00052 llifstream file((temp_str + filename).c_str()); 00053 00054 if (file.is_open()) 00055 { 00056 llinfos << "Loading history.xml file at " << filename << llendl; 00057 LLSDSerialize::fromXML(data, file); 00058 } 00059 00060 if (data.isUndefined()) 00061 { 00062 llinfos << "file missing, ill-formed, " 00063 "or simply undefined; not changing the" 00064 " file" << llendl; 00065 sHistorySD = LLSD(); 00066 return false; 00067 } 00068 } 00069 sHistorySD = data; 00070 return true; 00071 } 00072 00073 // static 00074 bool LLURLHistory::saveFile(const LLString& filename) 00075 { 00076 LLString temp_str = gDirUtilp->getLindenUserDir() + gDirUtilp->getDirDelimiter(); 00077 llofstream out((temp_str + filename).c_str()); 00078 if (!out.good()) 00079 { 00080 llwarns << "Unable to open " << filename << " for output." << llendl; 00081 return false; 00082 } 00083 00084 LLSDSerialize::toXML(sHistorySD, out); 00085 00086 out.close(); 00087 return true; 00088 } 00089 // static 00090 // This function returns a portion of the history llsd that contains the collected 00091 // url history 00092 LLSD LLURLHistory::getURLHistory(const std::string& collection) 00093 { 00094 if(sHistorySD.has(collection)) 00095 { 00096 return sHistorySD[collection]; 00097 } 00098 return LLSD(); 00099 } 00100 00101 // static 00102 void LLURLHistory::addURL(const std::string& collection, const std::string& url) 00103 { 00104 if(! url.empty()) 00105 { 00106 sHistorySD[collection].insert(0, url); 00107 LLURLHistory::limitSize(collection); 00108 } 00109 } 00110 // static 00111 void LLURLHistory::removeURL(const std::string& collection, const std::string& url) 00112 { 00113 LLSD::array_iterator iter = sHistorySD[collection].beginArray(); 00114 LLSD::array_iterator end = sHistorySD[collection].endArray(); 00115 for(int index = 0; index < sHistorySD[collection].size(); index++) 00116 { 00117 if(sHistorySD[collection].get(index).asString() == url) 00118 { 00119 sHistorySD[collection].erase(index); 00120 } 00121 } 00122 } 00123 00124 // static 00125 void LLURLHistory::clear(const std::string& collection) 00126 { 00127 sHistorySD[ collection ] = LLSD(); 00128 } 00129 00130 void LLURLHistory::limitSize(const std::string& collection) 00131 { 00132 while(sHistorySD[collection].size() > MAX_URL_COUNT) 00133 { 00134 sHistorySD[collection].erase(MAX_URL_COUNT); 00135 } 00136 } 00137