PageRenderTime 65ms CodeModel.GetById 0ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/newview/lldaycyclemanager.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 230 lines | 157 code | 37 blank | 36 comment | 18 complexity | 8e9a4b4e13a0e30e72b4bb71e56ce37f MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file lldaycyclemanager.cpp
  3. * @brief Implementation for the LLDayCycleManager class.
  4. *
  5. * $LicenseInfo:firstyear=2011&license=viewerlgpl$
  6. * Second Life Viewer Source Code
  7. * Copyright (C) 2011, 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 "lldaycyclemanager.h"
  28. #include "lldiriterator.h"
  29. void LLDayCycleManager::getPresetNames(preset_name_list_t& names) const
  30. {
  31. names.clear();
  32. for (dc_map_t::const_iterator it = mDayCycleMap.begin(); it != mDayCycleMap.end(); ++it)
  33. {
  34. names.push_back(it->first);
  35. }
  36. }
  37. void LLDayCycleManager::getPresetNames(preset_name_list_t& user, preset_name_list_t& sys) const
  38. {
  39. user.clear();
  40. sys.clear();
  41. for (dc_map_t::const_iterator it = mDayCycleMap.begin(); it != mDayCycleMap.end(); ++it)
  42. {
  43. const std::string& name = it->first;
  44. if (isSystemPreset(name))
  45. {
  46. sys.push_back(name);
  47. }
  48. else
  49. {
  50. user.push_back(name);
  51. }
  52. }
  53. }
  54. void LLDayCycleManager::getUserPresetNames(preset_name_list_t& user) const
  55. {
  56. preset_name_list_t sys; // unused
  57. getPresetNames(user, sys);
  58. }
  59. bool LLDayCycleManager::getPreset(const std::string name, LLWLDayCycle& day_cycle) const
  60. {
  61. dc_map_t::const_iterator it = mDayCycleMap.find(name);
  62. if (it == mDayCycleMap.end())
  63. {
  64. return false;
  65. }
  66. day_cycle = it->second;
  67. return true;
  68. }
  69. bool LLDayCycleManager::getPreset(const std::string name, LLSD& day_cycle) const
  70. {
  71. LLWLDayCycle dc;
  72. if (!getPreset(name, dc))
  73. {
  74. return false;
  75. }
  76. day_cycle = dc.asLLSD();
  77. return true;
  78. }
  79. bool LLDayCycleManager::presetExists(const std::string name) const
  80. {
  81. LLWLDayCycle dummy;
  82. return getPreset(name, dummy);
  83. }
  84. bool LLDayCycleManager::isSystemPreset(const std::string& name) const
  85. {
  86. return gDirUtilp->fileExists(getSysDir() + LLURI::escape(name) + ".xml");
  87. }
  88. bool LLDayCycleManager::savePreset(const std::string& name, const LLSD& data)
  89. {
  90. // Save given preset.
  91. LLWLDayCycle day;
  92. day.loadDayCycle(data, LLEnvKey::SCOPE_LOCAL);
  93. day.save(getUserDir() + LLURI::escape(name) + ".xml");
  94. // Add it to our map.
  95. addPreset(name, data);
  96. mModifySignal();
  97. return true;
  98. }
  99. bool LLDayCycleManager::deletePreset(const std::string& name)
  100. {
  101. // Remove it from the map.
  102. dc_map_t::iterator it = mDayCycleMap.find(name);
  103. if (it == mDayCycleMap.end())
  104. {
  105. LL_WARNS("Windlight") << "No day cycle named " << name << LL_ENDL;
  106. return false;
  107. }
  108. mDayCycleMap.erase(it);
  109. // Remove from the filesystem.
  110. std::string filename = LLURI::escape(name) + ".xml";
  111. if (gDirUtilp->fileExists(getUserDir() + filename))
  112. {
  113. gDirUtilp->deleteFilesInDir(getUserDir(), filename);
  114. }
  115. // Signal interested parties.
  116. mModifySignal();
  117. return true;
  118. }
  119. bool LLDayCycleManager::isSkyPresetReferenced(const std::string& preset_name) const
  120. {
  121. // We're traversing local day cycles, they can only reference local skies.
  122. LLWLParamKey key(preset_name, LLEnvKey::SCOPE_LOCAL);
  123. for (dc_map_t::const_iterator it = mDayCycleMap.begin(); it != mDayCycleMap.end(); ++it)
  124. {
  125. if (it->second.hasReferencesTo(key))
  126. {
  127. return true;
  128. }
  129. }
  130. return false;
  131. }
  132. boost::signals2::connection LLDayCycleManager::setModifyCallback(const modify_signal_t::slot_type& cb)
  133. {
  134. return mModifySignal.connect(cb);
  135. }
  136. // virtual
  137. void LLDayCycleManager::initSingleton()
  138. {
  139. LL_DEBUGS("Windlight") << "Loading all day cycles" << LL_ENDL;
  140. loadAllPresets();
  141. }
  142. void LLDayCycleManager::loadAllPresets()
  143. {
  144. mDayCycleMap.clear();
  145. // First, load system (coming out of the box) day cycles.
  146. loadPresets(getSysDir());
  147. // Then load user presets. Note that user day cycles will modify any system ones already loaded.
  148. loadPresets(getUserDir());
  149. }
  150. void LLDayCycleManager::loadPresets(const std::string& dir)
  151. {
  152. LLDirIterator dir_iter(dir, "*.xml");
  153. while (1)
  154. {
  155. std::string file;
  156. if (!dir_iter.next(file)) break; // no more files
  157. loadPreset(dir + file);
  158. }
  159. }
  160. bool LLDayCycleManager::loadPreset(const std::string& path)
  161. {
  162. LLSD data = LLWLDayCycle::loadDayCycleFromPath(path);
  163. if (data.isUndefined())
  164. {
  165. llwarns << "Error loading day cycle from " << path << llendl;
  166. return false;
  167. }
  168. std::string name(gDirUtilp->getBaseFileName(LLURI::unescape(path), /*strip_exten = */ true));
  169. addPreset(name, data);
  170. return true;
  171. }
  172. bool LLDayCycleManager::addPreset(const std::string& name, const LLSD& data)
  173. {
  174. if (name.empty())
  175. {
  176. llassert(name.empty());
  177. return false;
  178. }
  179. LLWLDayCycle day;
  180. day.loadDayCycle(data, LLEnvKey::SCOPE_LOCAL);
  181. mDayCycleMap[name] = day;
  182. return true;
  183. }
  184. // static
  185. std::string LLDayCycleManager::getSysDir()
  186. {
  187. return gDirUtilp->getExpandedFilename(LL_PATH_APP_SETTINGS, "windlight/days", "");
  188. }
  189. // static
  190. std::string LLDayCycleManager::getUserDir()
  191. {
  192. return gDirUtilp->getExpandedFilename(LL_PATH_USER_SETTINGS , "windlight/days", "");
  193. }