slfloatermediafilter.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. /**
  2. * @file slfloatermediafilter.cpp
  3. * @brief The SLFloaterMediaFilter class definitions
  4. *
  5. * $LicenseInfo:firstyear=2011&license=viewergpl$
  6. *
  7. * Copyright (c) 2011, Sione Lomu
  8. * with debugging and improvements by Henri Beauchamp
  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 "llviewerprecompiledheaders.h"
  34. #include "slfloatermediafilter.h"
  35. #include "lllineeditor.h"
  36. #include "llscrolllistctrl.h"
  37. #include "lluictrlfactory.h"
  38. #include "llviewercontrol.h"
  39. #include "llviewermedia.h"
  40. bool SLFloaterMediaFilter::sIsWhitelist = false;
  41. bool SLFloaterMediaFilter::sShowIPs = false;
  42. SLFloaterMediaFilter::SLFloaterMediaFilter(const LLSD&)
  43. : mIsDirty(true)
  44. {
  45. LLUICtrlFactory::getInstance()->buildFloater(this,
  46. "floater_media_filter.xml");
  47. }
  48. //virtual
  49. bool SLFloaterMediaFilter::postBuild()
  50. {
  51. mWhitelistSLC = getChild<LLScrollListCtrl>("whitelist_list");
  52. mBlacklistSLC = getChild<LLScrollListCtrl>("blacklist_list");
  53. childSetAction("clear_lists", onClearLists, this);
  54. childSetAction("show_ips", onShowIPs, this);
  55. childSetAction("add_whitelist", onWhitelistAdd, this);
  56. childSetAction("remove_whitelist", onWhitelistRemove, this);
  57. childSetAction("add_blacklist", onBlacklistAdd, this);
  58. childSetAction("remove_blacklist", onBlacklistRemove, this);
  59. childSetAction("commit_domain", onCommitDomain, this);
  60. childSetUserData("whitelist_list", this);
  61. childSetUserData("blacklist_list", this);
  62. return true;
  63. }
  64. //virtual
  65. void SLFloaterMediaFilter::draw()
  66. {
  67. LLTimer update_timer;
  68. if (mIsDirty)
  69. {
  70. S32 whitescrollpos = mWhitelistSLC->getScrollPos();
  71. S32 blackscrollpos = mBlacklistSLC->getScrollPos();
  72. mWhitelistSLC->deleteAllItems();
  73. mBlacklistSLC->deleteAllItems();
  74. std::set<std::string> listed;
  75. std::string ip;
  76. std::string domain;
  77. std::string action;
  78. LLSD element;
  79. element["columns"][0]["font"] = "SANSSERIF";
  80. element["columns"][0]["font-style"] = "BOLD";
  81. for (S32 i = 0; i < (S32)LLViewerMedia::sMediaFilterList.size(); ++i)
  82. {
  83. domain = LLViewerMedia::sMediaFilterList[i]["domain"].asString();
  84. if (sShowIPs && update_timer.getElapsedTimeF32() < 1.0f)
  85. {
  86. ip = LLViewerMedia::getDomainIP(domain, true);
  87. if (ip != domain && domain.find('/') == std::string::npos)
  88. {
  89. domain += " (" + ip + ")";
  90. }
  91. }
  92. action = LLViewerMedia::sMediaFilterList[i]["action"].asString();
  93. if (!domain.empty() && action == "allow")
  94. {
  95. element["columns"][0]["column"] = "whitelist_col";
  96. element["columns"][0]["value"] = domain;
  97. //element["columns"][0]["color"] = LLColor4::green3.getValue();
  98. mWhitelistSLC->addElement(element, ADD_BOTTOM);
  99. listed.emplace(domain);
  100. }
  101. else if (!domain.empty() && action == "deny")
  102. {
  103. element["columns"][0]["column"] = "blacklist_col";
  104. element["columns"][0]["value"] = domain;
  105. //element["columns"][0]["color"] = LLColor4::red2.getValue();
  106. mBlacklistSLC->addElement(element, ADD_BOTTOM);
  107. listed.emplace(domain);
  108. }
  109. else
  110. {
  111. llwarns << "Bad media filter list: removing corrupted entry for \""
  112. << domain << "\"" << llendl;
  113. LLViewerMedia::sMediaFilterList.erase(i--);
  114. }
  115. }
  116. element["columns"][0]["font"] = "SANSSERIF";
  117. element["columns"][0]["font-style"] = "ITALIC";
  118. #if 0
  119. element["columns"][0]["color"] = LLColor4::green3.getValue();
  120. #endif
  121. element["columns"][0]["column"] = "whitelist_col";
  122. for (std::set<std::string>::iterator
  123. it = LLViewerMedia::sAllowedMedia.begin(),
  124. end = LLViewerMedia::sAllowedMedia.end();
  125. it != end; ++it)
  126. {
  127. domain = *it;
  128. if (sShowIPs && update_timer.getElapsedTimeF32() < 1.0f)
  129. {
  130. ip = LLViewerMedia::getDomainIP(domain, true);
  131. if (ip != domain && domain.find('/') == std::string::npos)
  132. {
  133. domain += " (" + ip + ")";
  134. }
  135. }
  136. if (listed.count(domain) == 0)
  137. {
  138. element["columns"][0]["value"] = domain;
  139. mWhitelistSLC->addElement(element, ADD_BOTTOM);
  140. }
  141. }
  142. element["columns"][0]["column"] = "blacklist_col";
  143. for (std::set<std::string>::iterator
  144. it = LLViewerMedia::sDeniedMedia.begin(),
  145. end = LLViewerMedia::sDeniedMedia.end();
  146. it != end; ++it)
  147. {
  148. domain = *it;
  149. if (sShowIPs && update_timer.getElapsedTimeF32() < 1.0f)
  150. {
  151. ip = LLViewerMedia::getDomainIP(domain, true);
  152. if (ip != domain && domain.find('/') == std::string::npos)
  153. {
  154. domain += " (" + ip + ")";
  155. }
  156. }
  157. if (listed.count(domain) == 0)
  158. {
  159. element["columns"][0]["value"] = domain;
  160. mBlacklistSLC->addElement(element, ADD_BOTTOM);
  161. }
  162. }
  163. mWhitelistSLC->setScrollPos(whitescrollpos);
  164. mBlacklistSLC->setScrollPos(blackscrollpos);
  165. if (!gSavedSettings.getBool("MediaEnableFilter"))
  166. {
  167. childDisable("clear_lists");
  168. childDisable("show_ips");
  169. childDisable("blacklist_list");
  170. childDisable("whitelist_list");
  171. childDisable("remove_whitelist");
  172. childDisable("add_whitelist");
  173. childDisable("remove_blacklist");
  174. childDisable("add_blacklist");
  175. childDisable("match_ip");
  176. childDisable("input_domain");
  177. childDisable("commit_domain");
  178. childSetText("add_text", getString("disabled"));
  179. }
  180. if (sShowIPs)
  181. {
  182. if (update_timer.getElapsedTimeF32() < 1.0f)
  183. {
  184. mIsDirty = sShowIPs = false;
  185. }
  186. }
  187. else
  188. {
  189. mIsDirty = false;
  190. }
  191. }
  192. LLFloater::draw();
  193. }
  194. void SLFloaterMediaFilter::setDirty()
  195. {
  196. SLFloaterMediaFilter* self = findInstance();
  197. if (self)
  198. {
  199. self->mIsDirty = true;
  200. }
  201. }
  202. void SLFloaterMediaFilter::onClearLists(void*)
  203. {
  204. LLViewerMedia::clearDomainFilterList();
  205. }
  206. void SLFloaterMediaFilter::onShowIPs(void*)
  207. {
  208. sShowIPs = true;
  209. setDirty();
  210. }
  211. void SLFloaterMediaFilter::onWhitelistAdd(void* data)
  212. {
  213. SLFloaterMediaFilter* self = (SLFloaterMediaFilter*)data;
  214. if (self)
  215. {
  216. self->childDisable("clear_lists");
  217. self->childDisable("show_ips");
  218. self->childDisable("blacklist_list");
  219. self->childDisable("whitelist_list");
  220. self->childDisable("remove_whitelist");
  221. self->childDisable("add_whitelist");
  222. self->childDisable("remove_blacklist");
  223. self->childDisable("add_blacklist");
  224. self->childEnable("input_domain");
  225. self->childEnable("commit_domain");
  226. self->childSetText("add_text", self->getString("white_prompt"));
  227. sIsWhitelist = true;
  228. }
  229. }
  230. void SLFloaterMediaFilter::onWhitelistRemove(void* data)
  231. {
  232. SLFloaterMediaFilter* self = (SLFloaterMediaFilter*)data;
  233. if (!self)
  234. {
  235. return;
  236. }
  237. LLScrollListItem* selected = self->mWhitelistSLC->getFirstSelected();
  238. if (selected)
  239. {
  240. std::string domain = self->mWhitelistSLC->getSelectedItemLabel();
  241. size_t pos = domain.find(' ');
  242. if (pos != std::string::npos)
  243. {
  244. domain = domain.substr(0, pos);
  245. }
  246. LLViewerMedia::sAllowedMedia.erase(domain);
  247. for (S32 i = 0, count = LLViewerMedia::sMediaFilterList.size();
  248. i < count; ++i)
  249. {
  250. if (LLViewerMedia::sMediaFilterList[i]["domain"].asString() == domain)
  251. {
  252. LLViewerMedia::sMediaFilterList.erase(i);
  253. break;
  254. }
  255. }
  256. if (self->childGetValue("match_ip") &&
  257. domain.find('/') == std::string::npos)
  258. {
  259. std::string ip = LLViewerMedia::getDomainIP(domain, true);
  260. if (ip != domain)
  261. {
  262. LLViewerMedia::sAllowedMedia.erase(ip);
  263. for (S32 i = 0, count = LLViewerMedia::sMediaFilterList.size();
  264. i < count; ++i)
  265. {
  266. if (LLViewerMedia::sMediaFilterList[i]["domain"].asString() == ip)
  267. {
  268. LLViewerMedia::sMediaFilterList.erase(i);
  269. break;
  270. }
  271. }
  272. }
  273. }
  274. LLViewerMedia::saveDomainFilterList();
  275. setDirty();
  276. }
  277. }
  278. void SLFloaterMediaFilter::onBlacklistAdd(void* data)
  279. {
  280. SLFloaterMediaFilter* self = (SLFloaterMediaFilter*)data;
  281. if (self)
  282. {
  283. self->childDisable("clear_lists");
  284. self->childDisable("show_ips");
  285. self->childDisable("blacklist_list");
  286. self->childDisable("whitelist_list");
  287. self->childDisable("remove_whitelist");
  288. self->childDisable("add_whitelist");
  289. self->childDisable("remove_blacklist");
  290. self->childDisable("add_blacklist");
  291. self->childEnable("input_domain");
  292. self->childEnable("commit_domain");
  293. self->childSetText("add_text", self->getString("black_prompt"));
  294. sIsWhitelist = false;
  295. }
  296. }
  297. void SLFloaterMediaFilter::onBlacklistRemove(void* data)
  298. {
  299. SLFloaterMediaFilter* self = (SLFloaterMediaFilter*)data;
  300. if (!self)
  301. {
  302. return;
  303. }
  304. LLScrollListItem* selected = self->mBlacklistSLC->getFirstSelected();
  305. if (selected)
  306. {
  307. std::string domain = self->mBlacklistSLC->getSelectedItemLabel();
  308. size_t pos = domain.find(' ');
  309. if (pos != std::string::npos)
  310. {
  311. domain = domain.substr(0, pos);
  312. }
  313. LLViewerMedia::sDeniedMedia.erase(domain);
  314. for (S32 i = 0, count = LLViewerMedia::sMediaFilterList.size();
  315. i < count; ++i)
  316. {
  317. if (LLViewerMedia::sMediaFilterList[i]["domain"].asString() == domain)
  318. {
  319. LLViewerMedia::sMediaFilterList.erase(i);
  320. break;
  321. }
  322. }
  323. if (self->childGetValue("match_ip") &&
  324. domain.find('/') == std::string::npos)
  325. {
  326. std::string ip = LLViewerMedia::getDomainIP(domain, true);
  327. if (ip != domain)
  328. {
  329. LLViewerMedia::sDeniedMedia.erase(ip);
  330. for (S32 i = 0, count = LLViewerMedia::sMediaFilterList.size();
  331. i < count; ++i)
  332. {
  333. if (LLViewerMedia::sMediaFilterList[i]["domain"].asString() == ip)
  334. {
  335. LLViewerMedia::sMediaFilterList.erase(i);
  336. break;
  337. }
  338. }
  339. }
  340. }
  341. LLViewerMedia::saveDomainFilterList();
  342. setDirty();
  343. }
  344. }
  345. void SLFloaterMediaFilter::onCommitDomain(void* data)
  346. {
  347. SLFloaterMediaFilter* self = (SLFloaterMediaFilter*)data;
  348. if (!self)
  349. {
  350. return;
  351. }
  352. std::string domain = self->childGetText("input_domain");
  353. domain = LLViewerMedia::extractDomain(domain);
  354. std::string ip = domain;
  355. bool match_ip = (self->childGetValue("match_ip") &&
  356. domain.find('/') == std::string::npos);
  357. if (match_ip)
  358. {
  359. ip = LLViewerMedia::getDomainIP(domain, true);
  360. match_ip = (ip != domain);
  361. }
  362. if (!domain.empty())
  363. {
  364. LLViewerMedia::sDeniedMedia.erase(domain);
  365. LLViewerMedia::sAllowedMedia.erase(domain);
  366. for (S32 i = 0; i < (S32)LLViewerMedia::sMediaFilterList.size(); )
  367. {
  368. if (LLViewerMedia::sMediaFilterList[i]["domain"].asString() == domain)
  369. {
  370. LLViewerMedia::sMediaFilterList.erase(i);
  371. }
  372. else
  373. {
  374. ++i;
  375. }
  376. }
  377. if (match_ip)
  378. {
  379. LLViewerMedia::sDeniedMedia.erase(ip);
  380. LLViewerMedia::sAllowedMedia.erase(ip);
  381. for (S32 i = 0; i < (S32)LLViewerMedia::sMediaFilterList.size(); )
  382. {
  383. if (LLViewerMedia::sMediaFilterList[i]["domain"].asString() == ip)
  384. {
  385. LLViewerMedia::sMediaFilterList.erase(i);
  386. }
  387. else
  388. {
  389. ++i;
  390. }
  391. }
  392. }
  393. LLSD newmedia;
  394. newmedia["domain"] = domain;
  395. if (sIsWhitelist)
  396. {
  397. newmedia["action"] = "allow";
  398. }
  399. else
  400. {
  401. newmedia["action"] = "deny";
  402. }
  403. LLViewerMedia::sMediaFilterList.append(newmedia);
  404. if (match_ip)
  405. {
  406. newmedia["domain"] = ip;
  407. LLViewerMedia::sMediaFilterList.append(newmedia);
  408. }
  409. LLViewerMedia::saveDomainFilterList();
  410. }
  411. self->childEnable("clear_lists");
  412. self->childEnable("show_ips");
  413. self->childEnable("blacklist_list");
  414. self->childEnable("whitelist_list");
  415. self->childEnable("remove_whitelist");
  416. self->childEnable("add_whitelist");
  417. self->childEnable("remove_blacklist");
  418. self->childEnable("add_blacklist");
  419. self->childDisable("input_domain");
  420. self->childDisable("commit_domain");
  421. self->childSetText("add_text", self->getString("domain_prompt"));
  422. self->childSetText("input_domain", "");
  423. setDirty();
  424. }