PageRenderTime 36ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/indra/newview/llsearchhistory.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 148 lines | 93 code | 26 blank | 29 comment | 12 complexity | a89e71a48fff9fd5aecbc929239571e5 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file llsearchhistory.cpp
  3. * @brief Search history container implementation
  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 "llsearchhistory.h"
  28. #include "llfile.h"
  29. #include "llsdserialize.h"
  30. #include "llxmlnode.h"
  31. std::string LLSearchHistory::SEARCH_QUERY = "search_query";
  32. std::string LLSearchHistory::SEARCH_HISTORY_FILE_NAME = "search_history.txt";
  33. LLSearchHistory::LLSearchHistory()
  34. {
  35. }
  36. bool LLSearchHistory::load()
  37. {
  38. // build filename for each user
  39. std::string resolved_filename = getHistoryFilePath();
  40. llifstream file(resolved_filename);
  41. if (!file.is_open())
  42. {
  43. return false;
  44. }
  45. clearHistory();
  46. // add each line in the file to the list
  47. std::string line;
  48. LLPointer<LLSDParser> parser = new LLSDNotationParser();
  49. while (std::getline(file, line))
  50. {
  51. LLSD s_item;
  52. std::istringstream iss(line);
  53. if (parser->parse(iss, s_item, line.length()) == LLSDParser::PARSE_FAILURE)
  54. {
  55. break;
  56. }
  57. mSearchHistory.push_back(s_item);
  58. }
  59. file.close();
  60. return true;
  61. }
  62. bool LLSearchHistory::save()
  63. {
  64. // build filename for each user
  65. std::string resolved_filename = getHistoryFilePath();
  66. // open a file for writing
  67. llofstream file (resolved_filename);
  68. if (!file.is_open())
  69. {
  70. return false;
  71. }
  72. search_history_list_t::const_iterator it = mSearchHistory.begin();
  73. for (; mSearchHistory.end() != it; ++it)
  74. {
  75. file << LLSDOStreamer<LLSDNotationFormatter>((*it).toLLSD()) << std::endl;
  76. }
  77. file.close();
  78. return true;
  79. }
  80. std::string LLSearchHistory::getHistoryFilePath()
  81. {
  82. return gDirUtilp->getExpandedFilename(LL_PATH_PER_SL_ACCOUNT, SEARCH_HISTORY_FILE_NAME);
  83. }
  84. void LLSearchHistory::addEntry(const std::string& search_query)
  85. {
  86. if(search_query.empty())
  87. {
  88. return;
  89. }
  90. search_history_list_t::iterator it =
  91. find(mSearchHistory.begin(), mSearchHistory.end(), search_query);
  92. if(mSearchHistory.end() != it)
  93. {
  94. mSearchHistory.erase(it);
  95. }
  96. LLSearchHistoryItem item(search_query);
  97. mSearchHistory.push_front(item);
  98. }
  99. bool LLSearchHistory::LLSearchHistoryItem::operator < (const LLSearchHistory::LLSearchHistoryItem& right)
  100. {
  101. S32 result = LLStringUtil::compareInsensitive(search_query, right.search_query);
  102. return result < 0;
  103. }
  104. bool LLSearchHistory::LLSearchHistoryItem::operator > (const LLSearchHistory::LLSearchHistoryItem& right)
  105. {
  106. S32 result = LLStringUtil::compareInsensitive(search_query, right.search_query);
  107. return result > 0;
  108. }
  109. bool LLSearchHistory::LLSearchHistoryItem::operator==(const LLSearchHistory::LLSearchHistoryItem& right)
  110. {
  111. return 0 == LLStringUtil::compareInsensitive(search_query, right.search_query);
  112. }
  113. bool LLSearchHistory::LLSearchHistoryItem::operator==(const std::string& right)
  114. {
  115. return 0 == LLStringUtil::compareInsensitive(search_query, right);
  116. }
  117. LLSD LLSearchHistory::LLSearchHistoryItem::toLLSD() const
  118. {
  119. LLSD ret;
  120. ret[SEARCH_QUERY] = search_query;
  121. return ret;
  122. }