llmimetypes.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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=viewergpl$
  7. *
  8. * Copyright (c) 2007-2009, Linden Research, Inc.
  9. *
  10. * Second Life Viewer Source Code
  11. * The source code in this file ("Source Code") is provided by Linden Lab
  12. * to you under the terms of the GNU General Public License, version 2.0
  13. * ("GPL"), unless you have obtained a separate licensing agreement
  14. * ("Other License"), formally executed by you and Linden Lab. Terms of
  15. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  16. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  17. *
  18. * There are special exceptions to the terms and conditions of the GPL as
  19. * it is applied to this Source Code. View the full text of the exception
  20. * in the file doc/FLOSS-exception.txt in this software distribution, or
  21. * online at
  22. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  23. *
  24. * By copying, modifying or distributing this software, you acknowledge
  25. * that you have read and understood your obligations described above,
  26. * and agree to abide by those obligations.
  27. *
  28. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  29. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  30. * COMPLETENESS OR PERFORMANCE.
  31. * $/LicenseInfo$
  32. */
  33. #include "linden_common.h"
  34. #include "llmimetypes.h"
  35. #include "lldir.h"
  36. #include "lluictrlfactory.h"
  37. LLMIMETypes::mime_info_map_t LLMIMETypes::sMap;
  38. LLMIMETypes::mime_widget_set_map_t LLMIMETypes::sWidgetMap;
  39. std::string sDefaultLabel;
  40. // Returned when we don't know what to do with the mime type
  41. std::string sDefaultWidgetType;
  42. // Returned when we don't know what widget set to use
  43. std::string sDefaultImpl;
  44. // Returned when we don't know what impl to use
  45. std::string sXMLFilename;
  46. // Squirrel away XML filename so we know how to reset
  47. std::string DEFAULT_MIME_TYPE = "none/none";
  48. /////////////////////////////////////////////////////////////////////////////
  49. //static
  50. bool LLMIMETypes::parseMIMETypes(const std::string& xml_filename)
  51. {
  52. LLXMLNodePtr root;
  53. std::string settings_fname = gDirUtil.getFullPath(LL_PATH_APP_SETTINGS,
  54. xml_filename);
  55. bool success = LLUICtrlFactory::getLayeredXMLNode(settings_fname, root);
  56. if (!success || root.isNull() || !root->hasName("mimetypes"))
  57. {
  58. llwarns << "Unable to read MIME type file: " << xml_filename << llendl;
  59. return false;
  60. }
  61. for (LLXMLNode* node = root->getFirstChild(); node != NULL;
  62. node = node->getNextSibling())
  63. {
  64. if (node->hasName("defaultlabel"))
  65. {
  66. sDefaultLabel = node->getTextContents();
  67. }
  68. else if (node->hasName("defaultwidget"))
  69. {
  70. sDefaultWidgetType = node->getTextContents();
  71. }
  72. else if (node->hasName("defaultimpl"))
  73. {
  74. sDefaultImpl = node->getTextContents();
  75. }
  76. else if (node->hasName("mimetype") || node->hasName("scheme"))
  77. {
  78. std::string mime_type;
  79. node->getAttributeString("name", mime_type);
  80. LLMIMEInfo info;
  81. for (LLXMLNode* child = node->getFirstChild(); child != NULL;
  82. child = child->getNextSibling())
  83. {
  84. if (child->hasName("label"))
  85. {
  86. info.mLabel = child->getTextContents();
  87. }
  88. else if (child->hasName("widgettype"))
  89. {
  90. info.mWidgetType = child->getTextContents();
  91. }
  92. else if (child->hasName("impl"))
  93. {
  94. info.mImpl = child->getTextContents();
  95. }
  96. }
  97. sMap[mime_type] = info;
  98. }
  99. else if (node->hasName("widgetset"))
  100. {
  101. std::string set_name;
  102. node->getAttributeString("name", set_name);
  103. LLMIMEWidgetSet info;
  104. for (LLXMLNode* child = node->getFirstChild(); child != NULL;
  105. child = child->getNextSibling())
  106. {
  107. if (child->hasName("label"))
  108. {
  109. info.mLabel = child->getTextContents();
  110. }
  111. if (child->hasName("icon"))
  112. {
  113. info.mIcon = child->getTextContents();
  114. }
  115. if (child->hasName("default_type"))
  116. {
  117. info.mDefaultMimeType = child->getTextContents();
  118. }
  119. if (child->hasName("tooltip"))
  120. {
  121. info.mToolTip = child->getTextContents();
  122. }
  123. if (child->hasName("playtip"))
  124. {
  125. info.mPlayTip = child->getTextContents();
  126. }
  127. if (child->hasName("allow_resize"))
  128. {
  129. bool allow_resize = false;
  130. child->getBoolValue(1, &allow_resize);
  131. info.mAllowResize = allow_resize;
  132. }
  133. if (child->hasName("allow_looping"))
  134. {
  135. bool allow_looping = false;
  136. child->getBoolValue(1, &allow_looping);
  137. info.mAllowLooping = allow_looping;
  138. }
  139. }
  140. sWidgetMap[set_name] = info;
  141. }
  142. }
  143. sXMLFilename = xml_filename;
  144. return true;
  145. }
  146. //static
  147. std::string LLMIMETypes::translate(const std::string& mime_type)
  148. {
  149. mime_info_map_t::const_iterator it = sMap.find(mime_type);
  150. if (it != sMap.end())
  151. {
  152. return it->second.mLabel;
  153. }
  154. else
  155. {
  156. return sDefaultLabel;
  157. }
  158. }
  159. //static
  160. std::string LLMIMETypes::widgetType(const std::string& mime_type)
  161. {
  162. mime_info_map_t::const_iterator it = sMap.find(mime_type);
  163. if (it != sMap.end())
  164. {
  165. return it->second.mWidgetType;
  166. }
  167. else
  168. {
  169. return sDefaultWidgetType;
  170. }
  171. }
  172. //static
  173. std::string LLMIMETypes::implType(const std::string& mime_type)
  174. {
  175. mime_info_map_t::const_iterator it = sMap.find(mime_type);
  176. if (it != sMap.end())
  177. {
  178. return it->second.mImpl;
  179. }
  180. else
  181. {
  182. return sDefaultImpl;
  183. }
  184. }
  185. //static
  186. std::string LLMIMETypes::findIcon(const std::string& mime_type)
  187. {
  188. std::string icon;
  189. std::string widget_type = LLMIMETypes::widgetType(mime_type);
  190. mime_widget_set_map_t::iterator it = sWidgetMap.find(widget_type);
  191. if (it != sWidgetMap.end())
  192. {
  193. icon = it->second.mIcon;
  194. }
  195. return icon;
  196. }
  197. //static
  198. std::string LLMIMETypes::findDefaultMimeType(const std::string& widget_type)
  199. {
  200. std::string mime_type = getDefaultMimeType();
  201. mime_widget_set_map_t::iterator it = sWidgetMap.find(widget_type);
  202. if (it != sWidgetMap.end())
  203. {
  204. mime_type = it->second.mDefaultMimeType;
  205. }
  206. return mime_type;
  207. }
  208. //static
  209. const std::string& LLMIMETypes::getDefaultMimeType()
  210. {
  211. return DEFAULT_MIME_TYPE;
  212. }
  213. //static
  214. std::string LLMIMETypes::findToolTip(const std::string& mime_type)
  215. {
  216. std::string tool_tip;
  217. std::string widget_type = LLMIMETypes::widgetType(mime_type);
  218. mime_widget_set_map_t::iterator it = sWidgetMap.find(widget_type);
  219. if (it != sWidgetMap.end())
  220. {
  221. tool_tip = it->second.mToolTip;
  222. }
  223. return tool_tip;
  224. }
  225. //static
  226. std::string LLMIMETypes::findPlayTip(const std::string& mime_type)
  227. {
  228. std::string play_tip;
  229. std::string widget_type = LLMIMETypes::widgetType(mime_type);
  230. mime_widget_set_map_t::iterator it = sWidgetMap.find(widget_type);
  231. if (it != sWidgetMap.end())
  232. {
  233. play_tip = it->second.mPlayTip;
  234. }
  235. return play_tip;
  236. }
  237. //static
  238. bool LLMIMETypes::findAllowResize(const std::string& mime_type)
  239. {
  240. bool allow_resize = false;
  241. std::string widget_type = LLMIMETypes::widgetType(mime_type);
  242. mime_widget_set_map_t::iterator it = sWidgetMap.find(widget_type);
  243. if (it != sWidgetMap.end())
  244. {
  245. allow_resize = it->second.mAllowResize;
  246. }
  247. return allow_resize;
  248. }
  249. //static
  250. bool LLMIMETypes::findAllowLooping(const std::string& mime_type)
  251. {
  252. bool allow_looping = false;
  253. std::string widget_type = LLMIMETypes::widgetType(mime_type);
  254. mime_widget_set_map_t::iterator it = sWidgetMap.find(widget_type);
  255. if (it != sWidgetMap.end())
  256. {
  257. allow_looping = it->second.mAllowLooping;
  258. }
  259. return allow_looping;
  260. }
  261. //static
  262. bool LLMIMETypes::isTypeHandled(const std::string& mime_type)
  263. {
  264. return sMap.count(mime_type) != 0;
  265. }
  266. //static
  267. void LLMIMETypes::reload(void*)
  268. {
  269. sMap.clear();
  270. sWidgetMap.clear();
  271. LLMIMETypes::parseMIMETypes(sXMLFilename);
  272. }