PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms 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
Possible License(s): LGPL-2.1
  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. #include "llviewerprecompiledheaders.h"
  28. #include "llmimetypes.h"
  29. #include "lltrans.h"
  30. #include "llxmlnode.h"
  31. #include "lluictrlfactory.h"
  32. LLMIMETypes::mime_info_map_t LLMIMETypes::sMap;
  33. LLMIMETypes::mime_widget_set_map_t LLMIMETypes::sWidgetMap;
  34. std::string sDefaultLabel;
  35. // Returned when we don't know what to do with the mime type
  36. std::string sDefaultWidgetType;
  37. // Returned when we don't know what widget set to use
  38. std::string sDefaultImpl;
  39. // Returned when we don't know what impl to use
  40. std::string sXMLFilename;
  41. // Squirrel away XML filename so we know how to reset
  42. std::string DEFAULT_MIME_TYPE = "none/none";
  43. /////////////////////////////////////////////////////////////////////////////
  44. // static
  45. bool LLMIMETypes::parseMIMETypes(const std::string& xml_filename)
  46. {
  47. LLXMLNodePtr root;
  48. bool success = LLUICtrlFactory::getLayeredXMLNode(xml_filename, root);
  49. if ( ! success || root.isNull() || ! root->hasName( "mimetypes" ) )
  50. {
  51. llwarns << "Unable to read MIME type file: "
  52. << xml_filename << llendl;
  53. return false;
  54. }
  55. for (LLXMLNode* node = root->getFirstChild();
  56. node != NULL;
  57. node = node->getNextSibling())
  58. {
  59. if (node->hasName("defaultlabel"))
  60. {
  61. sDefaultLabel = node->getTextContents();
  62. }
  63. else if (node->hasName("defaultwidget"))
  64. {
  65. sDefaultWidgetType = node->getTextContents();
  66. }
  67. else if (node->hasName("defaultimpl"))
  68. {
  69. sDefaultImpl = node->getTextContents();
  70. }
  71. else if (node->hasName("mimetype") || node->hasName("scheme"))
  72. {
  73. std::string mime_type;
  74. node->getAttributeString("name", mime_type);
  75. LLMIMEInfo info;
  76. for (LLXMLNode* child = node->getFirstChild();
  77. child != NULL;
  78. child = child->getNextSibling())
  79. {
  80. if (child->hasName("label"))
  81. {
  82. info.mLabel = child->getTextContents();
  83. }
  84. else if (child->hasName("widgettype"))
  85. {
  86. info.mWidgetType = child->getTextContents();
  87. }
  88. else if (child->hasName("impl"))
  89. {
  90. info.mImpl = child->getTextContents();
  91. }
  92. }
  93. sMap[mime_type] = info;
  94. }
  95. else if (node->hasName("widgetset"))
  96. {
  97. std::string set_name;
  98. node->getAttributeString("name", set_name);
  99. LLMIMEWidgetSet info;
  100. for (LLXMLNode* child = node->getFirstChild();
  101. child != NULL;
  102. child = child->getNextSibling())
  103. {
  104. if (child->hasName("label"))
  105. {
  106. info.mLabel = child->getTextContents();
  107. }
  108. if (child->hasName("icon"))
  109. {
  110. info.mIcon = child->getTextContents();
  111. }
  112. if (child->hasName("default_type"))
  113. {
  114. info.mDefaultMimeType = child->getTextContents();
  115. }
  116. if (child->hasName("tooltip"))
  117. {
  118. info.mToolTip = child->getTextContents();
  119. }
  120. if (child->hasName("playtip"))
  121. {
  122. info.mPlayTip = child->getTextContents();
  123. }
  124. if (child->hasName("allow_resize"))
  125. {
  126. BOOL allow_resize = FALSE;
  127. child->getBoolValue( 1, &allow_resize );
  128. info.mAllowResize = (bool)allow_resize;
  129. }
  130. if (child->hasName("allow_looping"))
  131. {
  132. BOOL allow_looping = FALSE;
  133. child->getBoolValue( 1, &allow_looping );
  134. info.mAllowLooping = (bool)allow_looping;
  135. }
  136. }
  137. sWidgetMap[set_name] = info;
  138. }
  139. }
  140. sXMLFilename = xml_filename;
  141. return true;
  142. }
  143. // static
  144. std::string LLMIMETypes::translate(const std::string& mime_type)
  145. {
  146. mime_info_map_t::const_iterator it = sMap.find(mime_type);
  147. if (it != sMap.end())
  148. {
  149. return it->second.mLabel;
  150. }
  151. else
  152. {
  153. return sDefaultLabel;
  154. }
  155. }
  156. // static
  157. std::string LLMIMETypes::widgetType(const std::string& mime_type)
  158. {
  159. mime_info_map_t::const_iterator it = sMap.find(mime_type);
  160. if (it != sMap.end())
  161. {
  162. return it->second.mWidgetType;
  163. }
  164. else
  165. {
  166. return sDefaultWidgetType;
  167. }
  168. }
  169. // static
  170. std::string LLMIMETypes::implType(const std::string& mime_type)
  171. {
  172. mime_info_map_t::const_iterator it = sMap.find(mime_type);
  173. if (it != sMap.end())
  174. {
  175. return it->second.mImpl;
  176. }
  177. else
  178. {
  179. return sDefaultImpl;
  180. }
  181. }
  182. // static
  183. std::string LLMIMETypes::findIcon(const std::string& mime_type)
  184. {
  185. std::string icon = "";
  186. std::string widget_type = LLMIMETypes::widgetType(mime_type);
  187. mime_widget_set_map_t::iterator it = sWidgetMap.find(widget_type);
  188. if(it != sWidgetMap.end())
  189. {
  190. icon = it->second.mIcon;
  191. }
  192. return icon;
  193. }
  194. // static
  195. std::string LLMIMETypes::findDefaultMimeType(const std::string& widget_type)
  196. {
  197. std::string mime_type = getDefaultMimeType();
  198. mime_widget_set_map_t::iterator it = sWidgetMap.find(widget_type);
  199. if(it != sWidgetMap.end())
  200. {
  201. mime_type = it->second.mDefaultMimeType;
  202. }
  203. return mime_type;
  204. }
  205. // static
  206. const std::string& LLMIMETypes::getDefaultMimeType()
  207. {
  208. return DEFAULT_MIME_TYPE;
  209. }
  210. const std::string& LLMIMETypes::getDefaultMimeTypeTranslation()
  211. {
  212. static std::string mime_type = LLTrans::getString("DefaultMimeType");
  213. return mime_type;
  214. }
  215. // static
  216. std::string LLMIMETypes::findToolTip(const std::string& mime_type)
  217. {
  218. std::string tool_tip = "";
  219. std::string widget_type = LLMIMETypes::widgetType(mime_type);
  220. mime_widget_set_map_t::iterator it = sWidgetMap.find(widget_type);
  221. if(it != sWidgetMap.end())
  222. {
  223. tool_tip = it->second.mToolTip;
  224. }
  225. return tool_tip;
  226. }
  227. // static
  228. std::string LLMIMETypes::findPlayTip(const std::string& mime_type)
  229. {
  230. std::string play_tip = "";
  231. std::string widget_type = LLMIMETypes::widgetType(mime_type);
  232. mime_widget_set_map_t::iterator it = sWidgetMap.find(widget_type);
  233. if(it != sWidgetMap.end())
  234. {
  235. play_tip = it->second.mPlayTip;
  236. }
  237. return play_tip;
  238. }
  239. // static
  240. bool LLMIMETypes::findAllowResize(const std::string& mime_type)
  241. {
  242. bool allow_resize = false;
  243. std::string widget_type = LLMIMETypes::widgetType(mime_type);
  244. mime_widget_set_map_t::iterator it = sWidgetMap.find(widget_type);
  245. if(it != sWidgetMap.end())
  246. {
  247. allow_resize = it->second.mAllowResize;
  248. }
  249. return allow_resize;
  250. }
  251. // static
  252. bool LLMIMETypes::findAllowLooping(const std::string& mime_type)
  253. {
  254. bool allow_looping = false;
  255. std::string widget_type = LLMIMETypes::widgetType(mime_type);
  256. mime_widget_set_map_t::iterator it = sWidgetMap.find(widget_type);
  257. if(it != sWidgetMap.end())
  258. {
  259. allow_looping = it->second.mAllowLooping;
  260. }
  261. return allow_looping;
  262. }
  263. // static
  264. bool LLMIMETypes::isTypeHandled(const std::string& mime_type)
  265. {
  266. mime_info_map_t::const_iterator it = sMap.find(mime_type);
  267. if (it != sMap.end())
  268. {
  269. return true;
  270. }
  271. return false;
  272. }
  273. // static
  274. void LLMIMETypes::reload(void*)
  275. {
  276. sMap.clear();
  277. sWidgetMap.clear();
  278. (void)LLMIMETypes::parseMIMETypes(sXMLFilename);
  279. }