PageRenderTime 26ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/indra/newview/lllocationhistory.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 179 lines | 109 code | 32 blank | 38 comment | 19 complexity | 0e92ed187fda5d1286150e6c7884c399 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file lllocationhistory.cpp
  3. * @brief Typed locations 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 "lllocationhistory.h"
  28. #include <iomanip> // for std::setw()
  29. #include "llui.h"
  30. #include "llsd.h"
  31. #include "llsdserialize.h"
  32. LLLocationHistory::LLLocationHistory() :
  33. mFilename("typed_locations.txt")
  34. {
  35. }
  36. void LLLocationHistory::addItem(const LLLocationHistoryItem& item) {
  37. static LLUICachedControl<S32> max_items("LocationHistoryMaxSize", 100);
  38. // check if this item doesn't duplicate any existing one
  39. location_list_t::iterator item_iter = std::find(mItems.begin(), mItems.end(),item);
  40. if(item_iter != mItems.end()) // if it already exists, erase the old one
  41. {
  42. mItems.erase(item_iter);
  43. }
  44. mItems.push_back(item);
  45. // If the vector size exceeds the maximum, purge the oldest items (at the start of the mItems vector).
  46. if ((S32)mItems.size() > max_items)
  47. {
  48. mItems.erase(mItems.begin(), mItems.end()-max_items);
  49. }
  50. llassert((S32)mItems.size() <= max_items);
  51. mChangedSignal(ADD);
  52. }
  53. /*
  54. * @brief Try to find item in history.
  55. * If item has been founded, it will be places into end of history.
  56. * @return true - item has founded
  57. */
  58. bool LLLocationHistory::touchItem(const LLLocationHistoryItem& item) {
  59. bool result = false;
  60. location_list_t::iterator item_iter = std::find(mItems.begin(), mItems.end(), item);
  61. // the last used item should be the first in the history
  62. if (item_iter != mItems.end()) {
  63. mItems.erase(item_iter);
  64. mItems.push_back(item);
  65. result = true;
  66. }
  67. return result;
  68. }
  69. void LLLocationHistory::removeItems()
  70. {
  71. mItems.clear();
  72. mChangedSignal(CLEAR);
  73. }
  74. bool LLLocationHistory::getMatchingItems(const std::string& substring, location_list_t& result) const
  75. {
  76. // *TODO: an STL algorithm would look nicer
  77. result.clear();
  78. std::string needle = substring;
  79. LLStringUtil::toLower(needle);
  80. for (location_list_t::const_iterator it = mItems.begin(); it != mItems.end(); ++it)
  81. {
  82. std::string haystack = it->getLocation();
  83. LLStringUtil::toLower(haystack);
  84. if (haystack.find(needle) != std::string::npos)
  85. result.push_back(*it);
  86. }
  87. return result.size();
  88. }
  89. void LLLocationHistory::dump() const
  90. {
  91. llinfos << "Location history dump:" << llendl;
  92. int i = 0;
  93. for (location_list_t::const_iterator it = mItems.begin(); it != mItems.end(); ++it, ++i)
  94. {
  95. llinfos << "#" << std::setw(2) << std::setfill('0') << i << ": " << it->getLocation() << llendl;
  96. }
  97. }
  98. void LLLocationHistory::save() const
  99. {
  100. // build filename for each user
  101. std::string resolved_filename = gDirUtilp->getExpandedFilename(LL_PATH_PER_SL_ACCOUNT, mFilename);
  102. if (resolved_filename.empty())
  103. {
  104. llinfos << "can't get path to location history filename - probably not logged in yet." << llendl;
  105. return;
  106. }
  107. // open a file for writing
  108. llofstream file (resolved_filename);
  109. if (!file.is_open())
  110. {
  111. llwarns << "can't open location history file \"" << mFilename << "\" for writing" << llendl;
  112. return;
  113. }
  114. for (location_list_t::const_iterator it = mItems.begin(); it != mItems.end(); ++it)
  115. {
  116. file << LLSDOStreamer<LLSDNotationFormatter>((*it).toLLSD()) << std::endl;
  117. }
  118. file.close();
  119. }
  120. void LLLocationHistory::load()
  121. {
  122. llinfos << "Loading location history." << llendl;
  123. // build filename for each user
  124. std::string resolved_filename = gDirUtilp->getExpandedFilename(LL_PATH_PER_SL_ACCOUNT, mFilename);
  125. llifstream file(resolved_filename);
  126. if (!file.is_open())
  127. {
  128. llwarns << "can't load location history from file \"" << mFilename << "\"" << llendl;
  129. return;
  130. }
  131. mItems.clear();// need to use a direct call of clear() method to avoid signal invocation
  132. // add each line in the file to the list
  133. std::string line;
  134. LLPointer<LLSDParser> parser = new LLSDNotationParser();
  135. while (std::getline(file, line)) {
  136. LLSD s_item;
  137. std::istringstream iss(line);
  138. if (parser->parse(iss, s_item, line.length()) == LLSDParser::PARSE_FAILURE)
  139. {
  140. llinfos<< "Parsing saved teleport history failed" << llendl;
  141. break;
  142. }
  143. mItems.push_back(s_item);
  144. }
  145. file.close();
  146. mChangedSignal(LOAD);
  147. }