llurlhistory.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /**
  2. * @file llurlhistory.cpp
  3. * @brief Manages a list of recent URLs
  4. *
  5. * $LicenseInfo:firstyear=2007&license=viewergpl$
  6. *
  7. * Copyright (c) 2007-2009, Linden Research, Inc.
  8. *
  9. * Second Life Viewer Source Code
  10. * The source code in this file ("Source Code") is provided by Linden Lab
  11. * to you under the terms of the GNU General Public License, version 2.0
  12. * ("GPL"), unless you have obtained a separate licensing agreement
  13. * ("Other License"), formally executed by you and Linden Lab. Terms of
  14. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  15. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  16. *
  17. * There are special exceptions to the terms and conditions of the GPL as
  18. * it is applied to this Source Code. View the full text of the exception
  19. * in the file doc/FLOSS-exception.txt in this software distribution, or
  20. * online at
  21. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  22. *
  23. * By copying, modifying or distributing this software, you acknowledge
  24. * that you have read and understood your obligations described above,
  25. * and agree to abide by those obligations.
  26. *
  27. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  28. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  29. * COMPLETENESS OR PERFORMANCE.
  30. * $/LicenseInfo$
  31. */
  32. #include "linden_common.h"
  33. #include "llurlhistory.h"
  34. #include "lldir.h"
  35. #include "llsdserialize.h"
  36. #include "lluri.h"
  37. LLSD LLURLHistory::sHistorySD;
  38. constexpr S32 MAX_URL_COUNT = 10;
  39. /////////////////////////////////////////////////////////////////////////////
  40. //static
  41. bool LLURLHistory::loadFile(const std::string& filename)
  42. {
  43. std::string temp_str = gDirUtil.getLindenUserDir() + LL_DIR_DELIM_STR +
  44. filename;
  45. llifstream file(temp_str.c_str());
  46. if (file.is_open())
  47. {
  48. llinfos << "Loading URL history: " << temp_str << llendl;
  49. LLSD data;
  50. LLSDSerialize::fromXML(data, file);
  51. if (data.isUndefined())
  52. {
  53. llinfos << temp_str << " ill-formed or empty; loading aborted."
  54. << llendl;
  55. sHistorySD.clear();
  56. }
  57. else
  58. {
  59. sHistorySD = data;
  60. return true;
  61. }
  62. }
  63. return false;
  64. }
  65. //static
  66. bool LLURLHistory::saveFile(const std::string& filename)
  67. {
  68. std::string temp_str = gDirUtil.getLindenUserDir();
  69. if (temp_str.empty())
  70. {
  71. llwarns << "Can't save URL history. No user directory set." << llendl;
  72. return false;
  73. }
  74. temp_str += LL_DIR_DELIM_STR + filename;
  75. llofstream out(temp_str.c_str());
  76. if (!out.is_open())
  77. {
  78. llwarns << "Unable to open '" << temp_str << "' for writing."
  79. << llendl;
  80. return false;
  81. }
  82. LLSDSerialize::toXML(sHistorySD, out);
  83. out.close();
  84. return true;
  85. }
  86. // This function returns a portion of the history llsd that contains the
  87. // collected url history
  88. //static
  89. LLSD LLURLHistory::getURLHistory(const std::string& collection)
  90. {
  91. if (sHistorySD.has(collection))
  92. {
  93. return sHistorySD[collection];
  94. }
  95. return LLSD();
  96. }
  97. //static
  98. void LLURLHistory::addURL(const std::string& collection,
  99. const std::string& url)
  100. {
  101. if (!url.empty())
  102. {
  103. LLURI uri(url);
  104. std::string simplified_url = uri.scheme() + "://" + uri.authority() +
  105. uri.path();
  106. sHistorySD[collection].insert(0, simplified_url);
  107. LLURLHistory::limitSize(collection);
  108. }
  109. }
  110. //static
  111. void LLURLHistory::removeURL(const std::string& collection,
  112. const std::string& url)
  113. {
  114. if (url.empty())
  115. {
  116. return;
  117. }
  118. LLURI uri(url);
  119. std::string simplified_url = uri.scheme() + "://" + uri.authority() +
  120. uri.path();
  121. for (size_t index = 0; index < sHistorySD[collection].size(); ++index)
  122. {
  123. if (sHistorySD[collection].get(index).asString() == simplified_url)
  124. {
  125. sHistorySD[collection].erase(index);
  126. }
  127. }
  128. }
  129. //static
  130. void LLURLHistory::clear(const std::string& collection)
  131. {
  132. sHistorySD[collection] = LLSD();
  133. }
  134. void LLURLHistory::limitSize(const std::string& collection)
  135. {
  136. while ((S32)sHistorySD[collection].size() > MAX_URL_COUNT)
  137. {
  138. sHistorySD[collection].erase(MAX_URL_COUNT);
  139. }
  140. }