PageRenderTime 44ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/indra/newview/llteleporthistorystorage.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 255 lines | 168 code | 49 blank | 38 comment | 21 complexity | 7b798068ba8737438da1bfe0bf8f4903 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file llteleporthistorystorage.cpp
  3. * @brief Teleport history
  4. *
  5. * $LicenseInfo:firstyear=2009&license=viewerlgpl$
  6. * Second Life Viewer Source Code
  7. * Copyright (C) 2010, Linden Research, Inc.
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation;
  12. * version 2.1 of the License only.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. *
  23. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  24. * $/LicenseInfo$
  25. */
  26. #include "llviewerprecompiledheaders.h"
  27. #include "llteleporthistorystorage.h"
  28. #include "llsd.h"
  29. #include "llsdserialize.h"
  30. #include "lldir.h"
  31. #include "llteleporthistory.h"
  32. #include "llagent.h"
  33. // Max offset for two global positions to consider them as equal
  34. const F64 MAX_GLOBAL_POS_OFFSET = 5.0f;
  35. LLTeleportHistoryPersistentItem::LLTeleportHistoryPersistentItem(const LLSD& val)
  36. {
  37. mTitle = val["title"].asString();
  38. mGlobalPos.setValue(val["global_pos"]);
  39. mDate = val["date"];
  40. }
  41. LLSD LLTeleportHistoryPersistentItem::toLLSD() const
  42. {
  43. LLSD val;
  44. val["title"] = mTitle;
  45. val["global_pos"] = mGlobalPos.getValue();
  46. val["date"] = mDate;
  47. return val;
  48. }
  49. struct LLSortItemsByDate
  50. {
  51. bool operator()(const LLTeleportHistoryPersistentItem& a, const LLTeleportHistoryPersistentItem& b)
  52. {
  53. return a.mDate < b.mDate;
  54. }
  55. };
  56. LLTeleportHistoryStorage::LLTeleportHistoryStorage() :
  57. mFilename("teleport_history.txt")
  58. {
  59. mItems.clear();
  60. LLTeleportHistory *th = LLTeleportHistory::getInstance();
  61. if (th)
  62. th->setHistoryChangedCallback(boost::bind(&LLTeleportHistoryStorage::onTeleportHistoryChange, this));
  63. load();
  64. }
  65. LLTeleportHistoryStorage::~LLTeleportHistoryStorage()
  66. {
  67. }
  68. void LLTeleportHistoryStorage::onTeleportHistoryChange()
  69. {
  70. LLTeleportHistory *th = LLTeleportHistory::getInstance();
  71. if (!th)
  72. return;
  73. // Hacky sanity check. (EXT-6798)
  74. if (th->getItems().size() == 0)
  75. {
  76. llassert(!"Inconsistent teleport history state");
  77. return;
  78. }
  79. const LLTeleportHistoryItem &item = th->getItems()[th->getCurrentItemIndex()];
  80. addItem(item.mTitle, item.mGlobalPos);
  81. save();
  82. }
  83. void LLTeleportHistoryStorage::purgeItems()
  84. {
  85. mItems.clear();
  86. mHistoryChangedSignal(-1);
  87. }
  88. void LLTeleportHistoryStorage::addItem(const std::string title, const LLVector3d& global_pos)
  89. {
  90. addItem(title, global_pos, LLDate::now());
  91. }
  92. bool LLTeleportHistoryStorage::compareByTitleAndGlobalPos(const LLTeleportHistoryPersistentItem& a, const LLTeleportHistoryPersistentItem& b)
  93. {
  94. return a.mTitle == b.mTitle && (a.mGlobalPos - b.mGlobalPos).length() < MAX_GLOBAL_POS_OFFSET;
  95. }
  96. void LLTeleportHistoryStorage::addItem(const std::string title, const LLVector3d& global_pos, const LLDate& date)
  97. {
  98. LLTeleportHistoryPersistentItem item(title, global_pos, date);
  99. slurl_list_t::iterator item_iter = std::find_if(mItems.begin(), mItems.end(),
  100. boost::bind(&LLTeleportHistoryStorage::compareByTitleAndGlobalPos, this, _1, item));
  101. // If there is such item already, remove it, since new item is more recent
  102. S32 removed_index = -1;
  103. if (item_iter != mItems.end())
  104. {
  105. removed_index = item_iter - mItems.begin();
  106. mItems.erase(item_iter);
  107. }
  108. mItems.push_back(item);
  109. // Check whether sorting is needed
  110. if (mItems.size() > 1)
  111. {
  112. item_iter = mItems.end();
  113. item_iter--;
  114. item_iter--;
  115. // If second to last item is more recent than last, then resort items
  116. if (item_iter->mDate > item.mDate)
  117. {
  118. removed_index = -1;
  119. std::sort(mItems.begin(), mItems.end(), LLSortItemsByDate());
  120. }
  121. }
  122. mHistoryChangedSignal(removed_index);
  123. }
  124. void LLTeleportHistoryStorage::removeItem(S32 idx)
  125. {
  126. if (idx < 0 || idx >= (S32)mItems.size())
  127. return;
  128. mItems.erase (mItems.begin() + idx);
  129. }
  130. void LLTeleportHistoryStorage::save()
  131. {
  132. // build filename for each user
  133. std::string resolvedFilename = gDirUtilp->getExpandedFilename(LL_PATH_PER_SL_ACCOUNT, mFilename);
  134. // open the history file for writing
  135. llofstream file (resolvedFilename);
  136. if (!file.is_open())
  137. {
  138. llwarns << "can't open teleport history file \"" << mFilename << "\" for writing" << llendl;
  139. return;
  140. }
  141. for (size_t i=0; i<mItems.size(); i++)
  142. {
  143. LLSD s_item = mItems[i].toLLSD();
  144. file << LLSDOStreamer<LLSDNotationFormatter>(s_item) << std::endl;
  145. }
  146. file.close();
  147. }
  148. void LLTeleportHistoryStorage::load()
  149. {
  150. // build filename for each user
  151. std::string resolved_filename = gDirUtilp->getExpandedFilename(LL_PATH_PER_SL_ACCOUNT, mFilename);
  152. // open the history file for reading
  153. llifstream file(resolved_filename);
  154. if (!file.is_open())
  155. {
  156. llwarns << "can't load teleport history from file \"" << mFilename << "\"" << llendl;
  157. return;
  158. }
  159. // remove current entries before we load over them
  160. mItems.clear();
  161. // the parser's destructor is protected so we cannot create in the stack.
  162. LLPointer<LLSDParser> parser = new LLSDNotationParser();
  163. std::string line;
  164. while (std::getline(file, line))
  165. {
  166. LLSD s_item;
  167. std::istringstream iss(line);
  168. if (parser->parse(iss, s_item, line.length()) == LLSDParser::PARSE_FAILURE)
  169. {
  170. llinfos << "Parsing saved teleport history failed" << llendl;
  171. break;
  172. }
  173. mItems.push_back(s_item);
  174. }
  175. file.close();
  176. std::sort(mItems.begin(), mItems.end(), LLSortItemsByDate());
  177. mHistoryChangedSignal(-1);
  178. }
  179. void LLTeleportHistoryStorage::dump() const
  180. {
  181. llinfos << "Teleport history storage dump (" << mItems.size() << " items):" << llendl;
  182. for (size_t i=0; i<mItems.size(); i++)
  183. {
  184. std::stringstream line;
  185. line << i << ": " << mItems[i].mTitle;
  186. line << " global pos: " << mItems[i].mGlobalPos;
  187. line << " date: " << mItems[i].mDate;
  188. llinfos << line.str() << llendl;
  189. }
  190. }
  191. boost::signals2::connection LLTeleportHistoryStorage::setHistoryChangedCallback(history_callback_t cb)
  192. {
  193. return mHistoryChangedSignal.connect(cb);
  194. }
  195. void LLTeleportHistoryStorage::goToItem(S32 idx)
  196. {
  197. // Validate specified index.
  198. if (idx < 0 || idx >= (S32)mItems.size())
  199. {
  200. llwarns << "Invalid teleport history index (" << idx << ") specified" << llendl;
  201. dump();
  202. return;
  203. }
  204. // Attempt to teleport to the requested item.
  205. gAgent.teleportViaLocation(mItems[idx].mGlobalPos);
  206. }