HGSuitcaseInventoryService.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections.Generic;
  29. using System.Linq;
  30. using OpenMetaverse;
  31. using log4net;
  32. using Nini.Config;
  33. using System.Reflection;
  34. using OpenSim.Services.Base;
  35. using OpenSim.Services.Interfaces;
  36. using OpenSim.Services.InventoryService;
  37. using OpenSim.Data;
  38. using OpenSim.Framework;
  39. using OpenSim.Server.Base;
  40. namespace OpenSim.Services.HypergridService
  41. {
  42. /// <summary>
  43. /// Hypergrid inventory service. It serves the IInventoryService interface,
  44. /// but implements it in ways that are appropriate for inter-grid
  45. /// inventory exchanges. Specifically, it does not performs deletions
  46. /// and it responds to GetRootFolder requests with the ID of the
  47. /// Suitcase folder, not the actual "My Inventory" folder.
  48. /// </summary>
  49. public class HGSuitcaseInventoryService : XInventoryService, IInventoryService
  50. {
  51. private static readonly ILog m_log =
  52. LogManager.GetLogger(
  53. MethodBase.GetCurrentMethod().DeclaringType);
  54. // private string m_HomeURL;
  55. private IUserAccountService m_UserAccountService;
  56. private IAvatarService m_AvatarService;
  57. // private UserAccountCache m_Cache;
  58. private ExpiringCache<UUID, List<XInventoryFolder>> m_SuitcaseTrees = new ExpiringCache<UUID, List<XInventoryFolder>>();
  59. private ExpiringCache<UUID, AvatarAppearance> m_Appearances = new ExpiringCache<UUID, AvatarAppearance>();
  60. public HGSuitcaseInventoryService(IConfigSource config, string configName)
  61. : base(config, configName)
  62. {
  63. m_log.DebugFormat("[HG SUITCASE INVENTORY SERVICE]: Starting with config name {0}", configName);
  64. if (configName != string.Empty)
  65. m_ConfigName = configName;
  66. if (m_Database == null)
  67. m_log.ErrorFormat("[HG SUITCASE INVENTORY SERVICE]: m_Database is null!");
  68. //
  69. // Try reading the [InventoryService] section, if it exists
  70. //
  71. IConfig invConfig = config.Configs[m_ConfigName];
  72. if (invConfig != null)
  73. {
  74. string userAccountsDll = invConfig.GetString("UserAccountsService", string.Empty);
  75. if (userAccountsDll == string.Empty)
  76. throw new Exception("Please specify UserAccountsService in HGInventoryService configuration");
  77. Object[] args = new Object[] { config };
  78. m_UserAccountService = ServerUtils.LoadPlugin<IUserAccountService>(userAccountsDll, args);
  79. if (m_UserAccountService == null)
  80. throw new Exception(String.Format("Unable to create UserAccountService from {0}", userAccountsDll));
  81. string avatarDll = invConfig.GetString("AvatarService", string.Empty);
  82. if (avatarDll == string.Empty)
  83. throw new Exception("Please specify AvatarService in HGInventoryService configuration");
  84. m_AvatarService = ServerUtils.LoadPlugin<IAvatarService>(avatarDll, args);
  85. if (m_AvatarService == null)
  86. throw new Exception(String.Format("Unable to create m_AvatarService from {0}", avatarDll));
  87. // m_HomeURL = Util.GetConfigVarFromSections<string>(config, "HomeURI",
  88. // new string[] { "Startup", "Hypergrid", m_ConfigName }, String.Empty);
  89. // m_Cache = UserAccountCache.CreateUserAccountCache(m_UserAccountService);
  90. }
  91. m_log.Debug("[HG SUITCASE INVENTORY SERVICE]: Starting...");
  92. }
  93. public override bool CreateUserInventory(UUID principalID)
  94. {
  95. // NOGO
  96. return false;
  97. }
  98. public override List<InventoryFolderBase> GetInventorySkeleton(UUID principalID)
  99. {
  100. XInventoryFolder suitcase = GetSuitcaseXFolder(principalID);
  101. if (suitcase == null)
  102. {
  103. m_log.WarnFormat("[HG SUITCASE INVENTORY SERVICE]: Found no suitcase folder for user {0} when looking for inventory skeleton", principalID);
  104. return null;
  105. }
  106. List<XInventoryFolder> tree = GetFolderTree(principalID, suitcase.folderID);
  107. if (tree == null || (tree != null && tree.Count == 0))
  108. return null;
  109. List<InventoryFolderBase> folders = new List<InventoryFolderBase>();
  110. foreach (XInventoryFolder x in tree)
  111. {
  112. folders.Add(ConvertToOpenSim(x));
  113. }
  114. SetAsNormalFolder(suitcase);
  115. folders.Add(ConvertToOpenSim(suitcase));
  116. return folders;
  117. }
  118. public override InventoryCollection GetUserInventory(UUID userID)
  119. {
  120. m_log.DebugFormat("[HG SUITCASE INVENTORY SERVICE]: Get Suitcase inventory for user {0}", userID);
  121. InventoryCollection userInventory = new InventoryCollection();
  122. userInventory.UserID = userID;
  123. userInventory.Folders = new List<InventoryFolderBase>();
  124. userInventory.Items = new List<InventoryItemBase>();
  125. XInventoryFolder suitcase = GetSuitcaseXFolder(userID);
  126. if (suitcase == null)
  127. {
  128. m_log.WarnFormat("[HG SUITCASE INVENTORY SERVICE]: Found no suitcase folder for user {0} when looking for user inventory", userID);
  129. return null;
  130. }
  131. List<XInventoryFolder> tree = GetFolderTree(userID, suitcase.folderID);
  132. if (tree == null || (tree != null && tree.Count == 0))
  133. {
  134. SetAsNormalFolder(suitcase);
  135. userInventory.Folders.Add(ConvertToOpenSim(suitcase));
  136. return userInventory;
  137. }
  138. List<InventoryItemBase> items;
  139. foreach (XInventoryFolder f in tree)
  140. {
  141. // Add the items of this subfolder
  142. items = GetFolderItems(userID, f.folderID);
  143. if (items != null && items.Count > 0)
  144. {
  145. userInventory.Items.AddRange(items);
  146. }
  147. // Add the folder itself
  148. userInventory.Folders.Add(ConvertToOpenSim(f));
  149. }
  150. items = GetFolderItems(userID, suitcase.folderID);
  151. if (items != null && items.Count > 0)
  152. {
  153. userInventory.Items.AddRange(items);
  154. }
  155. SetAsNormalFolder(suitcase);
  156. userInventory.Folders.Add(ConvertToOpenSim(suitcase));
  157. m_log.DebugFormat("[HG SUITCASE INVENTORY SERVICE]: GetUserInventory for user {0} returning {1} folders and {2} items",
  158. userID, userInventory.Folders.Count, userInventory.Items.Count);
  159. return userInventory;
  160. }
  161. public override InventoryFolderBase GetRootFolder(UUID principalID)
  162. {
  163. m_log.DebugFormat("[HG SUITCASE INVENTORY SERVICE]: GetRootFolder for {0}", principalID);
  164. // Let's find out the local root folder
  165. XInventoryFolder root = GetRootXFolder(principalID);
  166. if (root == null)
  167. {
  168. m_log.WarnFormat("[HG SUITCASE INVENTORY SERVICE]: Unable to retrieve local root folder for user {0}", principalID);
  169. return null;
  170. }
  171. // Warp! Root folder for travelers is the suitcase folder
  172. XInventoryFolder suitcase = GetSuitcaseXFolder(principalID);
  173. if (suitcase == null)
  174. {
  175. m_log.DebugFormat("[HG SUITCASE INVENTORY SERVICE]: User {0} does not have a Suitcase folder. Creating it...", principalID);
  176. // make one, and let's add it to the user's inventory as a direct child of the root folder
  177. // In the DB we tag it as type 100, but we use -1 (Unknown) outside
  178. suitcase = CreateFolder(principalID, root.folderID, 100, "My Suitcase");
  179. if (suitcase == null)
  180. {
  181. m_log.ErrorFormat("[HG SUITCASE INVENTORY SERVICE]: Unable to create suitcase folder");
  182. return null;
  183. }
  184. m_Database.StoreFolder(suitcase);
  185. CreateSystemFolders(principalID, suitcase.folderID);
  186. }
  187. SetAsNormalFolder(suitcase);
  188. return ConvertToOpenSim(suitcase);
  189. }
  190. protected void CreateSystemFolders(UUID principalID, UUID rootID)
  191. {
  192. m_log.Debug("[HG SUITCASE INVENTORY SERVICE]: Creating System folders under Suitcase...");
  193. XInventoryFolder[] sysFolders = GetSystemFolders(principalID, rootID);
  194. if (!Array.Exists(sysFolders, delegate(XInventoryFolder f) { if (f.type == (int)AssetType.Animation) return true; return false; }))
  195. CreateFolder(principalID, rootID, (int)AssetType.Animation, "Animations");
  196. if (!Array.Exists(sysFolders, delegate(XInventoryFolder f) { if (f.type == (int)AssetType.Bodypart) return true; return false; }))
  197. CreateFolder(principalID, rootID, (int)AssetType.Bodypart, "Body Parts");
  198. if (!Array.Exists(sysFolders, delegate(XInventoryFolder f) { if (f.type == (int)AssetType.CallingCard) return true; return false; }))
  199. CreateFolder(principalID, rootID, (int)AssetType.CallingCard, "Calling Cards");
  200. if (!Array.Exists(sysFolders, delegate(XInventoryFolder f) { if (f.type == (int)AssetType.Clothing) return true; return false; }))
  201. CreateFolder(principalID, rootID, (int)AssetType.Clothing, "Clothing");
  202. if (!Array.Exists(sysFolders, delegate(XInventoryFolder f) { if (f.type == (int)AssetType.Gesture) return true; return false; }))
  203. CreateFolder(principalID, rootID, (int)AssetType.Gesture, "Gestures");
  204. if (!Array.Exists(sysFolders, delegate(XInventoryFolder f) { if (f.type == (int)AssetType.Landmark) return true; return false; }))
  205. CreateFolder(principalID, rootID, (int)AssetType.Landmark, "Landmarks");
  206. if (!Array.Exists(sysFolders, delegate(XInventoryFolder f) { if (f.type == (int)AssetType.LostAndFoundFolder) return true; return false; }))
  207. CreateFolder(principalID, rootID, (int)AssetType.LostAndFoundFolder, "Lost And Found");
  208. if (!Array.Exists(sysFolders, delegate(XInventoryFolder f) { if (f.type == (int)AssetType.Notecard) return true; return false; }))
  209. CreateFolder(principalID, rootID, (int)AssetType.Notecard, "Notecards");
  210. if (!Array.Exists(sysFolders, delegate(XInventoryFolder f) { if (f.type == (int)AssetType.Object) return true; return false; }))
  211. CreateFolder(principalID, rootID, (int)AssetType.Object, "Objects");
  212. if (!Array.Exists(sysFolders, delegate(XInventoryFolder f) { if (f.type == (int)AssetType.SnapshotFolder) return true; return false; }))
  213. CreateFolder(principalID, rootID, (int)AssetType.SnapshotFolder, "Photo Album");
  214. if (!Array.Exists(sysFolders, delegate(XInventoryFolder f) { if (f.type == (int)AssetType.LSLText) return true; return false; }))
  215. CreateFolder(principalID, rootID, (int)AssetType.LSLText, "Scripts");
  216. if (!Array.Exists(sysFolders, delegate(XInventoryFolder f) { if (f.type == (int)AssetType.Sound) return true; return false; }))
  217. CreateFolder(principalID, rootID, (int)AssetType.Sound, "Sounds");
  218. if (!Array.Exists(sysFolders, delegate(XInventoryFolder f) { if (f.type == (int)AssetType.Texture) return true; return false; }))
  219. CreateFolder(principalID, rootID, (int)AssetType.Texture, "Textures");
  220. if (!Array.Exists(sysFolders, delegate(XInventoryFolder f) { if (f.type == (int)AssetType.TrashFolder) return true; return false; }))
  221. CreateFolder(principalID, rootID, (int)AssetType.TrashFolder, "Trash");
  222. if (!Array.Exists(sysFolders, delegate(XInventoryFolder f) { if (f.type == (int)AssetType.FavoriteFolder) return true; return false; }))
  223. CreateFolder(principalID, rootID, (int)AssetType.FavoriteFolder, "Favorites");
  224. if (!Array.Exists(sysFolders, delegate(XInventoryFolder f) { if (f.type == (int)AssetType.CurrentOutfitFolder) return true; return false; }))
  225. CreateFolder(principalID, rootID, (int)AssetType.CurrentOutfitFolder, "Current Outfit");
  226. }
  227. public override InventoryFolderBase GetFolderForType(UUID principalID, AssetType type)
  228. {
  229. //m_log.DebugFormat("[HG INVENTORY SERVICE]: GetFolderForType for {0} {0}", principalID, type);
  230. XInventoryFolder suitcase = GetSuitcaseXFolder(principalID);
  231. if (suitcase == null)
  232. {
  233. m_log.WarnFormat("[HG SUITCASE INVENTORY SERVICE]: Found no suitcase folder for user {0} when looking for child type folder {1}", principalID, type);
  234. return null;
  235. }
  236. XInventoryFolder[] folders = m_Database.GetFolders(
  237. new string[] { "agentID", "type", "parentFolderID" },
  238. new string[] { principalID.ToString(), ((int)type).ToString(), suitcase.folderID.ToString() });
  239. if (folders.Length == 0)
  240. {
  241. m_log.WarnFormat("[HG SUITCASE INVENTORY SERVICE]: Found no folder for type {0} for user {1}", type, principalID);
  242. return null;
  243. }
  244. m_log.DebugFormat(
  245. "[HG SUITCASE INVENTORY SERVICE]: Found folder {0} {1} for type {2} for user {3}",
  246. folders[0].folderName, folders[0].folderID, type, principalID);
  247. return ConvertToOpenSim(folders[0]);
  248. }
  249. public override InventoryCollection GetFolderContent(UUID principalID, UUID folderID)
  250. {
  251. InventoryCollection coll = null;
  252. if (!IsWithinSuitcaseTree(principalID, folderID))
  253. return new InventoryCollection();
  254. coll = base.GetFolderContent(principalID, folderID);
  255. if (coll == null)
  256. {
  257. m_log.WarnFormat("[HG SUITCASE INVENTORY SERVICE]: Something wrong with user {0}'s suitcase folder", principalID);
  258. coll = new InventoryCollection();
  259. }
  260. return coll;
  261. }
  262. public override List<InventoryItemBase> GetFolderItems(UUID principalID, UUID folderID)
  263. {
  264. // Let's do a bit of sanity checking, more than the base service does
  265. // make sure the given folder exists under the suitcase tree of this user
  266. if (!IsWithinSuitcaseTree(principalID, folderID))
  267. return new List<InventoryItemBase>();
  268. return base.GetFolderItems(principalID, folderID);
  269. }
  270. public override bool AddFolder(InventoryFolderBase folder)
  271. {
  272. //m_log.WarnFormat("[HG SUITCASE INVENTORY SERVICE]: AddFolder {0} {1}", folder.Name, folder.ParentID);
  273. // Let's do a bit of sanity checking, more than the base service does
  274. // make sure the given folder's parent folder exists under the suitcase tree of this user
  275. if (!IsWithinSuitcaseTree(folder.Owner, folder.ParentID))
  276. return false;
  277. // OK, it's legit
  278. if (base.AddFolder(folder))
  279. {
  280. List<XInventoryFolder> tree;
  281. if (m_SuitcaseTrees.TryGetValue(folder.Owner, out tree))
  282. tree.Add(ConvertFromOpenSim(folder));
  283. return true;
  284. }
  285. return false;
  286. }
  287. public override bool UpdateFolder(InventoryFolderBase folder)
  288. {
  289. //m_log.DebugFormat("[HG SUITCASE INVENTORY SERVICE]: Update folder {0}, version {1}", folder.ID, folder.Version);
  290. if (!IsWithinSuitcaseTree(folder.Owner, folder.ID))
  291. {
  292. m_log.DebugFormat("[HG SUITCASE INVENTORY SERVICE]: folder {0} not within Suitcase tree", folder.Name);
  293. return false;
  294. }
  295. // For all others
  296. return base.UpdateFolder(folder);
  297. }
  298. public override bool MoveFolder(InventoryFolderBase folder)
  299. {
  300. if (!IsWithinSuitcaseTree(folder.Owner, folder.ID) ||
  301. !IsWithinSuitcaseTree(folder.Owner, folder.ParentID))
  302. return false;
  303. return base.MoveFolder(folder);
  304. }
  305. public override bool DeleteFolders(UUID principalID, List<UUID> folderIDs)
  306. {
  307. // NOGO
  308. return false;
  309. }
  310. public override bool PurgeFolder(InventoryFolderBase folder)
  311. {
  312. // NOGO
  313. return false;
  314. }
  315. public override bool AddItem(InventoryItemBase item)
  316. {
  317. // Let's do a bit of sanity checking, more than the base service does
  318. // make sure the given folder's parent folder exists under the suitcase tree of this user
  319. if (!IsWithinSuitcaseTree(item.Owner, item.Folder))
  320. return false;
  321. // OK, it's legit
  322. return base.AddItem(item);
  323. }
  324. public override bool UpdateItem(InventoryItemBase item)
  325. {
  326. if (!IsWithinSuitcaseTree(item.Owner, item.Folder))
  327. return false;
  328. return base.UpdateItem(item);
  329. }
  330. public override bool MoveItems(UUID principalID, List<InventoryItemBase> items)
  331. {
  332. // Principal is b0rked. *sigh*
  333. if (!IsWithinSuitcaseTree(items[0].Owner, items[0].Folder))
  334. return false;
  335. return base.MoveItems(principalID, items);
  336. }
  337. public override bool DeleteItems(UUID principalID, List<UUID> itemIDs)
  338. {
  339. return false;
  340. }
  341. public new InventoryItemBase GetItem(InventoryItemBase item)
  342. {
  343. InventoryItemBase it = base.GetItem(item);
  344. if (it == null)
  345. {
  346. m_log.DebugFormat("[HG SUITCASE INVENTORY SERVICE]: Unable to retrieve item {0} ({1}) in folder {2}",
  347. item.Name, item.ID, item.Folder);
  348. return null;
  349. }
  350. if (!IsWithinSuitcaseTree(it.Owner, it.Folder) && !IsPartOfAppearance(it.Owner, it.ID))
  351. {
  352. m_log.DebugFormat("[HG SUITCASE INVENTORY SERVICE]: Item {0} (folder {1}) is not within Suitcase",
  353. it.Name, it.Folder);
  354. return null;
  355. }
  356. // UserAccount user = m_Cache.GetUser(it.CreatorId);
  357. // // Adjust the creator data
  358. // if (user != null && it != null && (it.CreatorData == null || it.CreatorData == string.Empty))
  359. // it.CreatorData = m_HomeURL + ";" + user.FirstName + " " + user.LastName;
  360. //}
  361. return it;
  362. }
  363. public new InventoryFolderBase GetFolder(InventoryFolderBase folder)
  364. {
  365. InventoryFolderBase f = base.GetFolder(folder);
  366. if (f != null)
  367. {
  368. if (!IsWithinSuitcaseTree(f.Owner, f.ID))
  369. return null;
  370. }
  371. return f;
  372. }
  373. //public List<InventoryItemBase> GetActiveGestures(UUID principalID)
  374. //{
  375. //}
  376. //public int GetAssetPermissions(UUID principalID, UUID assetID)
  377. //{
  378. //}
  379. #region Auxiliary functions
  380. private XInventoryFolder GetXFolder(UUID userID, UUID folderID)
  381. {
  382. XInventoryFolder[] folders = m_Database.GetFolders(
  383. new string[] { "agentID", "folderID" },
  384. new string[] { userID.ToString(), folderID.ToString() });
  385. if (folders.Length == 0)
  386. return null;
  387. return folders[0];
  388. }
  389. private XInventoryFolder GetRootXFolder(UUID principalID)
  390. {
  391. XInventoryFolder[] folders = m_Database.GetFolders(
  392. new string[] { "agentID", "folderName", "type" },
  393. new string[] { principalID.ToString(), "My Inventory", ((int)AssetType.RootFolder).ToString() });
  394. if (folders != null && folders.Length > 0)
  395. return folders[0];
  396. // OK, so the RootFolder type didn't work. Let's look for any type with parent UUID.Zero.
  397. folders = m_Database.GetFolders(
  398. new string[] { "agentID", "folderName", "parentFolderID" },
  399. new string[] { principalID.ToString(), "My Inventory", UUID.Zero.ToString() });
  400. if (folders != null && folders.Length > 0)
  401. return folders[0];
  402. return null;
  403. }
  404. private XInventoryFolder GetCurrentOutfitXFolder(UUID userID)
  405. {
  406. XInventoryFolder root = GetRootXFolder(userID);
  407. if (root == null)
  408. return null;
  409. XInventoryFolder[] folders = m_Database.GetFolders(
  410. new string[] { "agentID", "type", "parentFolderID" },
  411. new string[] { userID.ToString(), ((int)AssetType.CurrentOutfitFolder).ToString(), root.folderID.ToString() });
  412. if (folders.Length == 0)
  413. return null;
  414. return folders[0];
  415. }
  416. private XInventoryFolder GetSuitcaseXFolder(UUID principalID)
  417. {
  418. // Warp! Root folder for travelers
  419. XInventoryFolder[] folders = m_Database.GetFolders(
  420. new string[] { "agentID", "type" },
  421. new string[] { principalID.ToString(), "100" }); // This is a special folder type...
  422. if (folders != null && folders.Length > 0)
  423. return folders[0];
  424. // check to see if we have the old Suitcase folder
  425. folders = m_Database.GetFolders(
  426. new string[] { "agentID", "folderName", "parentFolderID" },
  427. new string[] { principalID.ToString(), "My Suitcase", UUID.Zero.ToString() });
  428. if (folders != null && folders.Length > 0)
  429. {
  430. // Move it to under the root folder
  431. XInventoryFolder root = GetRootXFolder(principalID);
  432. folders[0].parentFolderID = root.folderID;
  433. folders[0].type = 100;
  434. m_Database.StoreFolder(folders[0]);
  435. return folders[0];
  436. }
  437. return null;
  438. }
  439. private void SetAsNormalFolder(XInventoryFolder suitcase)
  440. {
  441. suitcase.type = (short)AssetType.Folder;
  442. }
  443. private List<XInventoryFolder> GetFolderTree(UUID principalID, UUID folder)
  444. {
  445. List<XInventoryFolder> t = null;
  446. if (m_SuitcaseTrees.TryGetValue(principalID, out t))
  447. return t;
  448. // Get the tree of the suitcase folder
  449. t = GetFolderTreeRecursive(folder);
  450. m_SuitcaseTrees.AddOrUpdate(principalID, t, 5*60); // 5minutes
  451. return t;
  452. }
  453. private List<XInventoryFolder> GetFolderTreeRecursive(UUID root)
  454. {
  455. List<XInventoryFolder> tree = new List<XInventoryFolder>();
  456. XInventoryFolder[] folders = m_Database.GetFolders(
  457. new string[] { "parentFolderID" },
  458. new string[] { root.ToString() });
  459. if (folders == null || (folders != null && folders.Length == 0))
  460. return tree; // empty tree
  461. else
  462. {
  463. foreach (XInventoryFolder f in folders)
  464. {
  465. tree.Add(f);
  466. tree.AddRange(GetFolderTreeRecursive(f.folderID));
  467. }
  468. return tree;
  469. }
  470. }
  471. /// <summary>
  472. /// Return true if the folderID is a subfolder of the Suitcase or the suitcase folder itself
  473. /// </summary>
  474. /// <param name="folderID"></param>
  475. /// <param name="root"></param>
  476. /// <param name="suitcase"></param>
  477. /// <returns></returns>
  478. private bool IsWithinSuitcaseTree(UUID principalID, UUID folderID)
  479. {
  480. XInventoryFolder suitcase = GetSuitcaseXFolder(principalID);
  481. if (suitcase == null)
  482. {
  483. m_log.WarnFormat("[HG SUITCASE INVENTORY SERVICE]: User {0} does not have a Suitcase folder", principalID);
  484. return false;
  485. }
  486. List<XInventoryFolder> tree = new List<XInventoryFolder>();
  487. tree.Add(suitcase); // Warp! the tree is the real root folder plus the children of the suitcase folder
  488. tree.AddRange(GetFolderTree(principalID, suitcase.folderID));
  489. // Also add the Current Outfit folder to the list of available folders
  490. tree.Add(GetCurrentOutfitXFolder(principalID));
  491. XInventoryFolder f = tree.Find(delegate(XInventoryFolder fl)
  492. {
  493. if (fl.folderID == folderID) return true;
  494. else return false;
  495. });
  496. if (f == null) return false;
  497. else return true;
  498. }
  499. #endregion
  500. #region Avatar Appearance
  501. private AvatarAppearance GetAppearance(UUID principalID)
  502. {
  503. AvatarAppearance a = null;
  504. if (m_Appearances.TryGetValue(principalID, out a))
  505. return a;
  506. a = m_AvatarService.GetAppearance(principalID);
  507. m_Appearances.AddOrUpdate(principalID, a, 5 * 60); // 5minutes
  508. return a;
  509. }
  510. private bool IsPartOfAppearance(UUID principalID, UUID itemID)
  511. {
  512. AvatarAppearance a = GetAppearance(principalID);
  513. if (a == null)
  514. return false;
  515. // Check wearables (body parts and clothes)
  516. for (int i = 0; i < a.Wearables.Length; i++)
  517. {
  518. for (int j = 0; j < a.Wearables[i].Count; j++)
  519. {
  520. if (a.Wearables[i][j].ItemID == itemID)
  521. {
  522. //m_log.DebugFormat("[HG SUITCASE INVENTORY SERVICE]: item {0} is a wearable", itemID);
  523. return true;
  524. }
  525. }
  526. }
  527. // Check attachments
  528. if (a.GetAttachmentForItem(itemID) != null)
  529. {
  530. //m_log.DebugFormat("[HG SUITCASE INVENTORY SERVICE]: item {0} is an attachment", itemID);
  531. return true;
  532. }
  533. return false;
  534. }
  535. #endregion
  536. }
  537. }