PageRenderTime 18ms CodeModel.GetById 2ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/llui/llcommandmanager.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 172 lines | 103 code | 32 blank | 37 comment | 6 complexity | a985540305a1322d284930f4daaf329e MD5 | raw file
  1/** 
  2 * @file llcommandmanager.cpp
  3 * @brief LLCommandManager class
  4 *
  5 * $LicenseInfo:firstyear=2001&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// A control that displays the name of the chosen item, which when
 28// clicked shows a scrolling box of options.
 29
 30#include "linden_common.h"
 31
 32#include "llcommandmanager.h"
 33#include "lldir.h"
 34#include "llerror.h"
 35#include "llxuiparser.h"
 36
 37#include <boost/foreach.hpp>
 38
 39
 40//
 41// LLCommandId class
 42//
 43
 44const LLCommandId LLCommandId::null = LLCommandId("null command");
 45
 46//
 47// LLCommand class
 48//
 49
 50LLCommand::Params::Params()
 51	: available_in_toybox("available_in_toybox", false)
 52	, icon("icon")
 53	, label_ref("label_ref")
 54	, name("name")
 55	, tooltip_ref("tooltip_ref")
 56	, execute_function("execute_function")
 57	, execute_parameters("execute_parameters")
 58	, execute_stop_function("execute_stop_function")
 59	, execute_stop_parameters("execute_stop_parameters")
 60	, is_enabled_function("is_enabled_function")
 61	, is_enabled_parameters("is_enabled_parameters")
 62	, is_running_function("is_running_function")
 63	, is_running_parameters("is_running_parameters")
 64	, is_starting_function("is_starting_function")
 65	, is_starting_parameters("is_starting_parameters")
 66{
 67}
 68
 69LLCommand::LLCommand(const LLCommand::Params& p)
 70	: mIdentifier(p.name)
 71	, mAvailableInToybox(p.available_in_toybox)
 72	, mIcon(p.icon)
 73	, mLabelRef(p.label_ref)
 74	, mName(p.name)
 75	, mTooltipRef(p.tooltip_ref)
 76	, mExecuteFunction(p.execute_function)
 77	, mExecuteParameters(p.execute_parameters)
 78	, mExecuteStopFunction(p.execute_stop_function)
 79	, mExecuteStopParameters(p.execute_stop_parameters)
 80	, mIsEnabledFunction(p.is_enabled_function)
 81	, mIsEnabledParameters(p.is_enabled_parameters)
 82	, mIsRunningFunction(p.is_running_function)
 83	, mIsRunningParameters(p.is_running_parameters)
 84	, mIsStartingFunction(p.is_starting_function)
 85	, mIsStartingParameters(p.is_starting_parameters)
 86{
 87}
 88
 89
 90//
 91// LLCommandManager class
 92//
 93
 94LLCommandManager::LLCommandManager()
 95{
 96}
 97
 98LLCommandManager::~LLCommandManager()
 99{
100	for (CommandVector::iterator cmdIt = mCommands.begin(); cmdIt != mCommands.end(); ++cmdIt)
101	{
102		LLCommand * command = *cmdIt;
103
104		delete command;
105	}
106}
107
108U32 LLCommandManager::commandCount() const
109{
110	return mCommands.size();
111}
112
113LLCommand * LLCommandManager::getCommand(U32 commandIndex)
114{
115	return mCommands[commandIndex];
116}
117
118LLCommand * LLCommandManager::getCommand(const LLCommandId& commandId)
119{
120	LLCommand * command_match = NULL;
121
122	CommandIndexMap::const_iterator found = mCommandIndices.find(commandId.uuid());
123	
124	if (found != mCommandIndices.end())
125	{
126		command_match = mCommands[found->second];
127	}
128
129	return command_match;
130}
131
132void LLCommandManager::addCommand(LLCommand * command)
133{
134	LLCommandId command_id = command->id();
135	mCommandIndices[command_id.uuid()] = mCommands.size();
136	mCommands.push_back(command);
137
138	lldebugs << "Successfully added command: " << command->name() << llendl;
139}
140
141//static
142bool LLCommandManager::load()
143{
144	LLCommandManager& mgr = LLCommandManager::instance();
145
146	std::string commands_file = gDirUtilp->getExpandedFilename(LL_PATH_APP_SETTINGS, "commands.xml");
147
148	LLCommandManager::Params commandsParams;
149
150	LLSimpleXUIParser parser;
151	
152	if (!parser.readXUI(commands_file, commandsParams))
153	{
154		llerrs << "Unable to load xml file: " << commands_file << llendl;
155		return false;
156	}
157
158	if (!commandsParams.validateBlock())
159	{
160		llerrs << "Invalid commands file: " << commands_file << llendl;
161		return false;
162	}
163
164	BOOST_FOREACH(LLCommand::Params& commandParams, commandsParams.commands)
165	{
166		LLCommand * command = new LLCommand(commandParams);
167
168		mgr.addCommand(command);
169	}
170
171	return true;
172}