PageRenderTime 34ms CodeModel.GetById 14ms app.highlight 16ms RepoModel.GetById 2ms 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
  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
 27#include "llviewerprecompiledheaders.h"
 28
 29#include "lldaycyclemanager.h"
 30
 31#include "lldiriterator.h"
 32
 33void LLDayCycleManager::getPresetNames(preset_name_list_t& names) const
 34{
 35	names.clear();
 36
 37	for (dc_map_t::const_iterator it = mDayCycleMap.begin(); it != mDayCycleMap.end(); ++it)
 38	{
 39		names.push_back(it->first);
 40	}
 41}
 42
 43void LLDayCycleManager::getPresetNames(preset_name_list_t& user, preset_name_list_t& sys) const
 44{
 45	user.clear();
 46	sys.clear();
 47
 48	for (dc_map_t::const_iterator it = mDayCycleMap.begin(); it != mDayCycleMap.end(); ++it)
 49	{
 50		const std::string& name = it->first;
 51
 52		if (isSystemPreset(name))
 53		{
 54			sys.push_back(name);
 55		}
 56		else
 57		{
 58			user.push_back(name);
 59		}
 60	}
 61}
 62
 63void LLDayCycleManager::getUserPresetNames(preset_name_list_t& user) const
 64{
 65	preset_name_list_t sys; // unused
 66	getPresetNames(user, sys);
 67}
 68
 69bool LLDayCycleManager::getPreset(const std::string name, LLWLDayCycle& day_cycle) const
 70{
 71	dc_map_t::const_iterator it = mDayCycleMap.find(name);
 72	if (it == mDayCycleMap.end())
 73	{
 74		return false;
 75	}
 76
 77	day_cycle = it->second;
 78	return true;
 79}
 80
 81bool LLDayCycleManager::getPreset(const std::string name, LLSD& day_cycle) const
 82{
 83	LLWLDayCycle dc;
 84	if (!getPreset(name, dc))
 85	{
 86		return false;
 87	}
 88
 89	day_cycle = dc.asLLSD();
 90	return true;
 91}
 92
 93bool LLDayCycleManager::presetExists(const std::string name) const
 94{
 95	LLWLDayCycle dummy;
 96	return getPreset(name, dummy);
 97}
 98
 99bool LLDayCycleManager::isSystemPreset(const std::string& name) const
100{
101	return gDirUtilp->fileExists(getSysDir() + LLURI::escape(name) + ".xml");
102}
103
104bool LLDayCycleManager::savePreset(const std::string& name, const LLSD& data)
105{
106	// Save given preset.
107	LLWLDayCycle day;
108	day.loadDayCycle(data, LLEnvKey::SCOPE_LOCAL);
109	day.save(getUserDir() + LLURI::escape(name) + ".xml");
110
111	// Add it to our map.
112	addPreset(name, data);
113	mModifySignal();
114	return true;
115}
116
117bool LLDayCycleManager::deletePreset(const std::string& name)
118{
119	// Remove it from the map.
120	dc_map_t::iterator it = mDayCycleMap.find(name);
121	if (it == mDayCycleMap.end())
122	{
123		LL_WARNS("Windlight") << "No day cycle named " << name << LL_ENDL;
124		return false;
125	}
126	mDayCycleMap.erase(it);
127
128	// Remove from the filesystem.
129	std::string filename = LLURI::escape(name) + ".xml";
130	if (gDirUtilp->fileExists(getUserDir() + filename))
131	{
132		gDirUtilp->deleteFilesInDir(getUserDir(), filename);
133	}
134
135	// Signal interested parties.
136	mModifySignal();
137	return true;
138}
139
140bool LLDayCycleManager::isSkyPresetReferenced(const std::string& preset_name) const
141{
142	// We're traversing local day cycles, they can only reference local skies.
143	LLWLParamKey key(preset_name, LLEnvKey::SCOPE_LOCAL);
144
145	for (dc_map_t::const_iterator it = mDayCycleMap.begin(); it != mDayCycleMap.end(); ++it)
146	{
147		if (it->second.hasReferencesTo(key))
148		{
149			return true;
150		}
151	}
152
153	return false;
154}
155
156boost::signals2::connection LLDayCycleManager::setModifyCallback(const modify_signal_t::slot_type& cb)
157{
158	return mModifySignal.connect(cb);
159}
160
161// virtual
162void LLDayCycleManager::initSingleton()
163{
164	LL_DEBUGS("Windlight") << "Loading all day cycles" << LL_ENDL;
165	loadAllPresets();
166}
167
168void LLDayCycleManager::loadAllPresets()
169{
170	mDayCycleMap.clear();
171
172	// First, load system (coming out of the box) day cycles.
173	loadPresets(getSysDir());
174
175	// Then load user presets. Note that user day cycles will modify any system ones already loaded.
176	loadPresets(getUserDir());
177}
178
179void LLDayCycleManager::loadPresets(const std::string& dir)
180{
181	LLDirIterator dir_iter(dir, "*.xml");
182
183	while (1)
184	{
185		std::string file;
186		if (!dir_iter.next(file)) break; // no more files
187		loadPreset(dir + file);
188	}
189}
190
191bool LLDayCycleManager::loadPreset(const std::string& path)
192{
193	LLSD data = LLWLDayCycle::loadDayCycleFromPath(path);
194	if (data.isUndefined())
195	{
196		llwarns << "Error loading day cycle from " << path << llendl;
197		return false;
198	}
199
200	std::string name(gDirUtilp->getBaseFileName(LLURI::unescape(path), /*strip_exten = */ true));
201	addPreset(name, data);
202
203	return true;
204}
205
206bool LLDayCycleManager::addPreset(const std::string& name, const LLSD& data)
207{
208	if (name.empty())
209	{
210		llassert(name.empty());
211		return false;
212	}
213
214	LLWLDayCycle day;
215	day.loadDayCycle(data, LLEnvKey::SCOPE_LOCAL);
216	mDayCycleMap[name] = day;
217	return true;
218}
219
220// static
221std::string LLDayCycleManager::getSysDir()
222{
223	return gDirUtilp->getExpandedFilename(LL_PATH_APP_SETTINGS, "windlight/days", "");
224}
225
226// static
227std::string LLDayCycleManager::getUserDir()
228{
229	return gDirUtilp->getExpandedFilename(LL_PATH_USER_SETTINGS , "windlight/days", "");
230}