PageRenderTime 49ms CodeModel.GetById 29ms app.highlight 17ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/newview/llmimetypes.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 302 lines | 235 code | 23 blank | 44 comment | 51 complexity | b04d34d3c272c76e9e468d1621ce6ed5 MD5 | raw file
  1/**
  2 * @file llmimetypes.cpp
  3 * @brief Translates a MIME type like "video/quicktime" into a
  4 * localizable user-friendly string like "QuickTime Movie"
  5 *
  6 * $LicenseInfo:firstyear=2007&license=viewerlgpl$
  7 * Second Life Viewer Source Code
  8 * Copyright (C) 2010, Linden Research, Inc.
  9 * 
 10 * This library is free software; you can redistribute it and/or
 11 * modify it under the terms of the GNU Lesser General Public
 12 * License as published by the Free Software Foundation;
 13 * version 2.1 of the License only.
 14 * 
 15 * This library is distributed in the hope that it will be useful,
 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 18 * Lesser General Public License for more details.
 19 * 
 20 * You should have received a copy of the GNU Lesser General Public
 21 * License along with this library; if not, write to the Free Software
 22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 23 * 
 24 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 25 * $/LicenseInfo$
 26 */
 27
 28#include "llviewerprecompiledheaders.h"
 29
 30#include "llmimetypes.h"
 31#include "lltrans.h"
 32#include "llxmlnode.h"
 33
 34#include "lluictrlfactory.h"
 35
 36LLMIMETypes::mime_info_map_t LLMIMETypes::sMap;
 37LLMIMETypes::mime_widget_set_map_t LLMIMETypes::sWidgetMap;
 38
 39std::string sDefaultLabel;
 40	// Returned when we don't know what to do with the mime type
 41std::string sDefaultWidgetType;
 42	// Returned when we don't know what widget set to use
 43std::string sDefaultImpl;
 44	// Returned when we don't know what impl to use
 45std::string sXMLFilename; 
 46    // Squirrel away XML filename so we know how to reset
 47std::string DEFAULT_MIME_TYPE = "none/none";
 48
 49/////////////////////////////////////////////////////////////////////////////
 50
 51// static
 52bool LLMIMETypes::parseMIMETypes(const std::string& xml_filename)
 53{
 54	LLXMLNodePtr root;
 55	bool success = LLUICtrlFactory::getLayeredXMLNode(xml_filename, root);
 56	if ( ! success || root.isNull() || ! root->hasName( "mimetypes" ) )
 57	{
 58		llwarns << "Unable to read MIME type file: "
 59			<< xml_filename << llendl;
 60		return false;
 61	}
 62
 63	for (LLXMLNode* node = root->getFirstChild();
 64		 node != NULL;
 65		 node = node->getNextSibling())
 66	{
 67		if (node->hasName("defaultlabel"))
 68		{
 69			sDefaultLabel = node->getTextContents();
 70		}
 71		else if (node->hasName("defaultwidget"))
 72		{
 73			sDefaultWidgetType = node->getTextContents();
 74		}
 75		else if (node->hasName("defaultimpl"))
 76		{
 77			sDefaultImpl = node->getTextContents();
 78		}
 79		else if (node->hasName("mimetype") || node->hasName("scheme"))
 80		{
 81			std::string mime_type;
 82			node->getAttributeString("name", mime_type);
 83			LLMIMEInfo info;
 84			for (LLXMLNode* child = node->getFirstChild();
 85				 child != NULL;
 86				 child = child->getNextSibling())
 87			{
 88				if (child->hasName("label"))
 89				{
 90					info.mLabel = child->getTextContents();
 91				}
 92				else if (child->hasName("widgettype"))
 93				{
 94					info.mWidgetType = child->getTextContents();
 95				}
 96				else if (child->hasName("impl"))
 97				{
 98					info.mImpl = child->getTextContents();
 99				}
100			}
101			sMap[mime_type] = info;
102		}
103		else if (node->hasName("widgetset"))
104		{
105			std::string set_name;
106			node->getAttributeString("name", set_name);
107			LLMIMEWidgetSet info;
108			for (LLXMLNode* child = node->getFirstChild();
109				child != NULL;
110				child = child->getNextSibling())
111			{
112				if (child->hasName("label"))
113				{
114					info.mLabel = child->getTextContents();
115				}
116				if (child->hasName("icon"))
117				{
118					info.mIcon = child->getTextContents();
119				}
120				if (child->hasName("default_type"))
121				{
122					info.mDefaultMimeType = child->getTextContents();
123				}
124				if (child->hasName("tooltip"))
125				{
126					info.mToolTip = child->getTextContents();
127				}
128				if (child->hasName("playtip"))
129				{
130					info.mPlayTip = child->getTextContents();
131				}
132				if (child->hasName("allow_resize"))
133				{
134					BOOL allow_resize = FALSE;
135					child->getBoolValue( 1, &allow_resize );
136					info.mAllowResize = (bool)allow_resize;
137				}
138				if (child->hasName("allow_looping"))
139				{
140					BOOL allow_looping = FALSE;
141					child->getBoolValue( 1, &allow_looping );
142					info.mAllowLooping = (bool)allow_looping;
143				}
144			}
145			sWidgetMap[set_name] = info;
146		}
147	}
148
149	sXMLFilename = xml_filename;
150	return true;
151}
152
153// static
154std::string LLMIMETypes::translate(const std::string& mime_type)
155{
156	mime_info_map_t::const_iterator it = sMap.find(mime_type);
157	if (it != sMap.end())
158	{
159		return it->second.mLabel;
160	}
161	else
162	{
163		return sDefaultLabel;
164	}
165}
166
167// static
168std::string LLMIMETypes::widgetType(const std::string& mime_type)
169{
170	mime_info_map_t::const_iterator it = sMap.find(mime_type);
171	if (it != sMap.end())
172	{
173		return it->second.mWidgetType;
174	}
175	else
176	{
177		return sDefaultWidgetType;
178	}
179}
180
181// static
182std::string LLMIMETypes::implType(const std::string& mime_type)
183{
184	mime_info_map_t::const_iterator it = sMap.find(mime_type);
185	if (it != sMap.end())
186	{
187		return it->second.mImpl;
188	}
189	else
190	{
191		return sDefaultImpl;
192	}
193}
194
195// static
196std::string LLMIMETypes::findIcon(const std::string& mime_type)
197{
198	std::string icon = "";
199	std::string widget_type = LLMIMETypes::widgetType(mime_type);
200	mime_widget_set_map_t::iterator it = sWidgetMap.find(widget_type);
201	if(it != sWidgetMap.end())
202	{
203		icon = it->second.mIcon;
204	}
205	return icon;
206}
207
208// static
209std::string LLMIMETypes::findDefaultMimeType(const std::string& widget_type)
210{
211	std::string mime_type = getDefaultMimeType();
212	mime_widget_set_map_t::iterator it = sWidgetMap.find(widget_type);
213	if(it != sWidgetMap.end())
214	{
215		mime_type = it->second.mDefaultMimeType;
216	}
217	return mime_type;
218}
219
220// static
221const std::string& LLMIMETypes::getDefaultMimeType()
222{
223	return DEFAULT_MIME_TYPE;
224}
225
226const std::string& LLMIMETypes::getDefaultMimeTypeTranslation()
227{
228	static std::string mime_type = LLTrans::getString("DefaultMimeType");
229	return mime_type;
230}
231
232// static
233std::string LLMIMETypes::findToolTip(const std::string& mime_type)
234{
235	std::string tool_tip = "";
236	std::string widget_type = LLMIMETypes::widgetType(mime_type);
237	mime_widget_set_map_t::iterator it = sWidgetMap.find(widget_type);
238	if(it != sWidgetMap.end())
239	{
240		tool_tip = it->second.mToolTip;
241	}
242	return tool_tip;
243}
244
245// static
246std::string LLMIMETypes::findPlayTip(const std::string& mime_type)
247{
248	std::string play_tip = "";
249	std::string widget_type = LLMIMETypes::widgetType(mime_type);
250	mime_widget_set_map_t::iterator it = sWidgetMap.find(widget_type);
251	if(it != sWidgetMap.end())
252	{
253		play_tip = it->second.mPlayTip;
254	}
255	return play_tip;
256}
257
258// static
259bool LLMIMETypes::findAllowResize(const std::string& mime_type)
260{
261	bool allow_resize = false;
262	std::string widget_type = LLMIMETypes::widgetType(mime_type);
263	mime_widget_set_map_t::iterator it = sWidgetMap.find(widget_type);
264	if(it != sWidgetMap.end())
265	{
266		allow_resize = it->second.mAllowResize;
267	}
268	return allow_resize;
269}
270
271// static
272bool LLMIMETypes::findAllowLooping(const std::string& mime_type)
273{
274	bool allow_looping = false;
275	std::string widget_type = LLMIMETypes::widgetType(mime_type);
276	mime_widget_set_map_t::iterator it = sWidgetMap.find(widget_type);
277	if(it != sWidgetMap.end())
278	{
279		allow_looping = it->second.mAllowLooping;
280	}
281	return allow_looping;
282}
283
284// static
285bool LLMIMETypes::isTypeHandled(const std::string& mime_type)
286{
287	mime_info_map_t::const_iterator it = sMap.find(mime_type);
288	if (it != sMap.end())
289	{
290		return true;
291	}
292	return false;
293}
294
295// static
296void LLMIMETypes::reload(void*)
297{
298	sMap.clear();
299	sWidgetMap.clear();
300	(void)LLMIMETypes::parseMIMETypes(sXMLFilename);
301}
302