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

/indra/newview/llurlhistory.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 139 lines | 87 code | 18 blank | 34 comment | 9 complexity | d77aee4daadd82fb55860dadfcea7289 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file llurlhistory.cpp
  3. * @brief Manages a list of recent URLs
  4. *
  5. * $LicenseInfo:firstyear=2007&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 "llurlhistory.h"
  28. #include "lldir.h"
  29. #include "llsdserialize.h"
  30. LLSD LLURLHistory::sHistorySD;
  31. const int MAX_URL_COUNT = 10;
  32. /////////////////////////////////////////////////////////////////////////////
  33. // static
  34. bool LLURLHistory::loadFile(const std::string& filename)
  35. {
  36. LLSD data;
  37. {
  38. std::string temp_str = gDirUtilp->getLindenUserDir() + gDirUtilp->getDirDelimiter();
  39. llifstream file((temp_str + filename));
  40. if (file.is_open())
  41. {
  42. llinfos << "Loading history.xml file at " << filename << llendl;
  43. LLSDSerialize::fromXML(data, file);
  44. }
  45. if (data.isUndefined())
  46. {
  47. llinfos << "file missing, ill-formed, "
  48. "or simply undefined; not changing the"
  49. " file" << llendl;
  50. sHistorySD = LLSD();
  51. return false;
  52. }
  53. }
  54. sHistorySD = data;
  55. return true;
  56. }
  57. // static
  58. bool LLURLHistory::saveFile(const std::string& filename)
  59. {
  60. std::string temp_str = gDirUtilp->getLindenUserDir();
  61. if( temp_str.empty() )
  62. {
  63. llinfos << "Can't save URL history - no user directory set yet." << llendl;
  64. return false;
  65. }
  66. temp_str += gDirUtilp->getDirDelimiter() + filename;
  67. llofstream out(temp_str);
  68. if (!out.good())
  69. {
  70. llwarns << "Unable to open " << filename << " for output." << llendl;
  71. return false;
  72. }
  73. LLSDSerialize::toXML(sHistorySD, out);
  74. out.close();
  75. return true;
  76. }
  77. // static
  78. // This function returns a portion of the history llsd that contains the collected
  79. // url history
  80. LLSD LLURLHistory::getURLHistory(const std::string& collection)
  81. {
  82. if(sHistorySD.has(collection))
  83. {
  84. return sHistorySD[collection];
  85. }
  86. return LLSD();
  87. }
  88. // static
  89. void LLURLHistory::addURL(const std::string& collection, const std::string& url)
  90. {
  91. if(! url.empty())
  92. {
  93. sHistorySD[collection].insert(0, url);
  94. LLURLHistory::limitSize(collection);
  95. }
  96. }
  97. // static
  98. void LLURLHistory::removeURL(const std::string& collection, const std::string& url)
  99. {
  100. LLSD::array_iterator iter = sHistorySD[collection].beginArray();
  101. LLSD::array_iterator end = sHistorySD[collection].endArray();
  102. for(int index = 0; index < sHistorySD[collection].size(); index++)
  103. {
  104. if(sHistorySD[collection].get(index).asString() == url)
  105. {
  106. sHistorySD[collection].erase(index);
  107. }
  108. }
  109. }
  110. // static
  111. void LLURLHistory::clear(const std::string& collection)
  112. {
  113. sHistorySD[ collection ] = LLSD();
  114. }
  115. void LLURLHistory::limitSize(const std::string& collection)
  116. {
  117. while(sHistorySD[collection].size() > MAX_URL_COUNT)
  118. {
  119. sHistorySD[collection].erase(MAX_URL_COUNT);
  120. }
  121. }