HGSuitcaseInventoryService.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  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. }
  183. else
  184. {
  185. m_Database.StoreFolder(suitcase);
  186. // Create System folders
  187. CreateSystemFolders(principalID, suitcase.folderID);
  188. SetAsNormalFolder(suitcase);
  189. return ConvertToOpenSim(suitcase);
  190. }
  191. }
  192. return null;
  193. }
  194. protected void CreateSystemFolders(UUID principalID, UUID rootID)
  195. {
  196. m_log.Debug("[HG SUITCASE INVENTORY SERVICE]: Creating System folders under Suitcase...");
  197. XInventoryFolder[] sysFolders = GetSystemFolders(principalID, rootID);
  198. if (!Array.Exists(sysFolders, delegate(XInventoryFolder f) { if (f.type == (int)AssetType.Animation) return true; return false; }))
  199. CreateFolder(principalID, rootID, (int)AssetType.Animation, "Animations");
  200. if (!Array.Exists(sysFolders, delegate(XInventoryFolder f) { if (f.type == (int)AssetType.Bodypart) return true; return false; }))
  201. CreateFolder(principalID, rootID, (int)AssetType.Bodypart, "Body Parts");
  202. if (!Array.Exists(sysFolders, delegate(XInventoryFolder f) { if (f.type == (int)AssetType.CallingCard) return true; return false; }))
  203. CreateFolder(principalID, rootID, (int)AssetType.CallingCard, "Calling Cards");
  204. if (!Array.Exists(sysFolders, delegate(XInventoryFolder f) { if (f.type == (int)AssetType.Clothing) return true; return false; }))
  205. CreateFolder(principalID, rootID, (int)AssetType.Clothing, "Clothing");
  206. if (!Array.Exists(sysFolders, delegate(XInventoryFolder f) { if (f.type == (int)AssetType.Gesture) return true; return false; }))
  207. CreateFolder(principalID, rootID, (int)AssetType.Gesture, "Gestures");
  208. if (!Array.Exists(sysFolders, delegate(XInventoryFolder f) { if (f.type == (int)AssetType.Landmark) return true; return false; }))
  209. CreateFolder(principalID, rootID, (int)AssetType.Landmark, "Landmarks");
  210. if (!Array.Exists(sysFolders, delegate(XInventoryFolder f) { if (f.type == (int)AssetType.LostAndFoundFolder) return true; return false; }))
  211. CreateFolder(principalID, rootID, (int)AssetType.LostAndFoundFolder, "Lost And Found");
  212. if (!Array.Exists(sysFolders, delegate(XInventoryFolder f) { if (f.type == (int)AssetType.Notecard) return true; return false; }))
  213. CreateFolder(principalID, rootID, (int)AssetType.Notecard, "Notecards");
  214. if (!Array.Exists(sysFolders, delegate(XInventoryFolder f) { if (f.type == (int)AssetType.Object) return true; return false; }))
  215. CreateFolder(principalID, rootID, (int)AssetType.Object, "Objects");
  216. if (!Array.Exists(sysFolders, delegate(XInventoryFolder f) { if (f.type == (int)AssetType.SnapshotFolder) return true; return false; }))
  217. CreateFolder(principalID, rootID, (int)AssetType.SnapshotFolder, "Photo Album");
  218. if (!Array.Exists(sysFolders, delegate(XInventoryFolder f) { if (f.type == (int)AssetType.LSLText) return true; return false; }))
  219. CreateFolder(principalID, rootID, (int)AssetType.LSLText, "Scripts");
  220. if (!Array.Exists(sysFolders, delegate(XInventoryFolder f) { if (f.type == (int)AssetType.Sound) return true; return false; }))
  221. CreateFolder(principalID, rootID, (int)AssetType.Sound, "Sounds");
  222. if (!Array.Exists(sysFolders, delegate(XInventoryFolder f) { if (f.type == (int)AssetType.Texture) return true; return false; }))
  223. CreateFolder(principalID, rootID, (int)AssetType.Texture, "Textures");
  224. if (!Array.Exists(sysFolders, delegate(XInventoryFolder f) { if (f.type == (int)AssetType.TrashFolder) return true; return false; }))
  225. CreateFolder(principalID, rootID, (int)AssetType.TrashFolder, "Trash");
  226. if (!Array.Exists(sysFolders, delegate(XInventoryFolder f) { if (f.type == (int)AssetType.FavoriteFolder) return true; return false; }))
  227. CreateFolder(principalID, rootID, (int)AssetType.FavoriteFolder, "Favorites");
  228. if (!Array.Exists(sysFolders, delegate(XInventoryFolder f) { if (f.type == (int)AssetType.CurrentOutfitFolder) return true; return false; }))
  229. CreateFolder(principalID, rootID, (int)AssetType.CurrentOutfitFolder, "Current Outfit");
  230. }
  231. public override InventoryFolderBase GetFolderForType(UUID principalID, AssetType type)
  232. {
  233. //m_log.DebugFormat("[HG INVENTORY SERVICE]: GetFolderForType for {0} {0}", principalID, type);
  234. XInventoryFolder suitcase = GetSuitcaseXFolder(principalID);
  235. if (suitcase == null)
  236. {
  237. m_log.WarnFormat("[HG SUITCASE INVENTORY SERVICE]: Found no suitcase folder for user {0} when looking for child type folder {1}", principalID, type);
  238. return null;
  239. }
  240. XInventoryFolder[] folders = m_Database.GetFolders(
  241. new string[] { "agentID", "type", "parentFolderID" },
  242. new string[] { principalID.ToString(), ((int)type).ToString(), suitcase.folderID.ToString() });
  243. if (folders.Length == 0)
  244. {
  245. m_log.WarnFormat("[HG SUITCASE INVENTORY SERVICE]: Found no folder for type {0} for user {1}", type, principalID);
  246. return null;
  247. }
  248. m_log.DebugFormat(
  249. "[HG SUITCASE INVENTORY SERVICE]: Found folder {0} {1} for type {2} for user {3}",
  250. folders[0].folderName, folders[0].folderID, type, principalID);
  251. return ConvertToOpenSim(folders[0]);
  252. }
  253. public override InventoryCollection GetFolderContent(UUID principalID, UUID folderID)
  254. {
  255. InventoryCollection coll = null;
  256. if (!IsWithinSuitcaseTree(principalID, folderID))
  257. return new InventoryCollection();
  258. coll = base.GetFolderContent(principalID, folderID);
  259. if (coll == null)
  260. {
  261. m_log.WarnFormat("[HG SUITCASE INVENTORY SERVICE]: Something wrong with user {0}'s suitcase folder", principalID);
  262. coll = new InventoryCollection();
  263. }
  264. return coll;
  265. }
  266. public override List<InventoryItemBase> GetFolderItems(UUID principalID, UUID folderID)
  267. {
  268. // Let's do a bit of sanity checking, more than the base service does
  269. // make sure the given folder exists under the suitcase tree of this user
  270. if (!IsWithinSuitcaseTree(principalID, folderID))
  271. return new List<InventoryItemBase>();
  272. return base.GetFolderItems(principalID, folderID);
  273. }
  274. public override bool AddFolder(InventoryFolderBase folder)
  275. {
  276. //m_log.WarnFormat("[HG SUITCASE INVENTORY SERVICE]: AddFolder {0} {1}", folder.Name, folder.ParentID);
  277. // Let's do a bit of sanity checking, more than the base service does
  278. // make sure the given folder's parent folder exists under the suitcase tree of this user
  279. if (!IsWithinSuitcaseTree(folder.Owner, folder.ParentID))
  280. return false;
  281. // OK, it's legit
  282. if (base.AddFolder(folder))
  283. {
  284. List<XInventoryFolder> tree;
  285. if (m_SuitcaseTrees.TryGetValue(folder.Owner, out tree))
  286. tree.Add(ConvertFromOpenSim(folder));
  287. return true;
  288. }
  289. return false;
  290. }
  291. public override bool UpdateFolder(InventoryFolderBase folder)
  292. {
  293. //m_log.DebugFormat("[HG SUITCASE INVENTORY SERVICE]: Update folder {0}, version {1}", folder.ID, folder.Version);
  294. if (!IsWithinSuitcaseTree(folder.Owner, folder.ID))
  295. {
  296. m_log.DebugFormat("[HG SUITCASE INVENTORY SERVICE]: folder {0} not within Suitcase tree", folder.Name);
  297. return false;
  298. }
  299. // For all others
  300. return base.UpdateFolder(folder);
  301. }
  302. public override bool MoveFolder(InventoryFolderBase folder)
  303. {
  304. if (!IsWithinSuitcaseTree(folder.Owner, folder.ID) ||
  305. !IsWithinSuitcaseTree(folder.Owner, folder.ParentID))
  306. return false;
  307. return base.MoveFolder(folder);
  308. }
  309. public override bool DeleteFolders(UUID principalID, List<UUID> folderIDs)
  310. {
  311. // NOGO
  312. return false;
  313. }
  314. public override bool PurgeFolder(InventoryFolderBase folder)
  315. {
  316. // NOGO
  317. return false;
  318. }
  319. public override bool AddItem(InventoryItemBase item)
  320. {
  321. // Let's do a bit of sanity checking, more than the base service does
  322. // make sure the given folder's parent folder exists under the suitcase tree of this user
  323. if (!IsWithinSuitcaseTree(item.Owner, item.Folder))
  324. return false;
  325. // OK, it's legit
  326. return base.AddItem(item);
  327. }
  328. public override bool UpdateItem(InventoryItemBase item)
  329. {
  330. if (!IsWithinSuitcaseTree(item.Owner, item.Folder))
  331. return false;
  332. return base.UpdateItem(item);
  333. }
  334. public override bool MoveItems(UUID principalID, List<InventoryItemBase> items)
  335. {
  336. // Principal is b0rked. *sigh*
  337. if (!IsWithinSuitcaseTree(items[0].Owner, items[0].Folder))
  338. return false;
  339. return base.MoveItems(principalID, items);
  340. }
  341. public override bool DeleteItems(UUID principalID, List<UUID> itemIDs)
  342. {
  343. return false;
  344. }
  345. public new InventoryItemBase GetItem(InventoryItemBase item)
  346. {
  347. InventoryItemBase it = base.GetItem(item);
  348. if (it == null)
  349. {
  350. m_log.DebugFormat("[HG SUITCASE INVENTORY SERVICE]: Unable to retrieve item {0} ({1}) in folder {2}",
  351. item.Name, item.ID, item.Folder);
  352. return null;
  353. }
  354. if (!IsWithinSuitcaseTree(it.Owner, it.Folder) && !IsPartOfAppearance(it.Owner, it.ID))
  355. {
  356. m_log.DebugFormat("[HG SUITCASE INVENTORY SERVICE]: Item {0} (folder {1}) is not within Suitcase",
  357. it.Name, it.Folder);
  358. return null;
  359. }
  360. // UserAccount user = m_Cache.GetUser(it.CreatorId);
  361. // // Adjust the creator data
  362. // if (user != null && it != null && (it.CreatorData == null || it.CreatorData == string.Empty))
  363. // it.CreatorData = m_HomeURL + ";" + user.FirstName + " " + user.LastName;
  364. //}
  365. return it;
  366. }
  367. public new InventoryFolderBase GetFolder(InventoryFolderBase folder)
  368. {
  369. InventoryFolderBase f = base.GetFolder(folder);
  370. if (f != null)
  371. {
  372. if (!IsWithinSuitcaseTree(f.Owner, f.ID))
  373. return null;
  374. }
  375. return f;
  376. }
  377. //public List<InventoryItemBase> GetActiveGestures(UUID principalID)
  378. //{
  379. //}
  380. //public int GetAssetPermissions(UUID principalID, UUID assetID)
  381. //{
  382. //}
  383. #region Auxiliary functions
  384. private XInventoryFolder GetXFolder(UUID userID, UUID folderID)
  385. {
  386. XInventoryFolder[] folders = m_Database.GetFolders(
  387. new string[] { "agentID", "folderID" },
  388. new string[] { userID.ToString(), folderID.ToString() });
  389. if (folders.Length == 0)
  390. return null;
  391. return folders[0];
  392. }
  393. private XInventoryFolder GetRootXFolder(UUID principalID)
  394. {
  395. XInventoryFolder[] folders = m_Database.GetFolders(
  396. new string[] { "agentID", "folderName", "type" },
  397. new string[] { principalID.ToString(), "My Inventory", ((int)AssetType.RootFolder).ToString() });
  398. if (folders != null && folders.Length > 0)
  399. return folders[0];
  400. // OK, so the RootFolder type didn't work. Let's look for any type with parent UUID.Zero.
  401. folders = m_Database.GetFolders(
  402. new string[] { "agentID", "folderName", "parentFolderID" },
  403. new string[] { principalID.ToString(), "My Inventory", UUID.Zero.ToString() });
  404. if (folders != null && folders.Length > 0)
  405. return folders[0];
  406. return null;
  407. }
  408. private XInventoryFolder GetCurrentOutfitXFolder(UUID userID)
  409. {
  410. XInventoryFolder root = GetRootXFolder(userID);
  411. if (root == null)
  412. return null;
  413. XInventoryFolder[] folders = m_Database.GetFolders(
  414. new string[] { "agentID", "type", "parentFolderID" },
  415. new string[] { userID.ToString(), ((int)AssetType.CurrentOutfitFolder).ToString(), root.folderID.ToString() });
  416. if (folders.Length == 0)
  417. return null;
  418. return folders[0];
  419. }
  420. private XInventoryFolder GetSuitcaseXFolder(UUID principalID)
  421. {
  422. // Warp! Root folder for travelers
  423. XInventoryFolder[] folders = m_Database.GetFolders(
  424. new string[] { "agentID", "type" },
  425. new string[] { principalID.ToString(), "100" }); // This is a special folder type...
  426. if (folders != null && folders.Length > 0)
  427. return folders[0];
  428. // check to see if we have the old Suitcase folder
  429. folders = m_Database.GetFolders(
  430. new string[] { "agentID", "folderName", "parentFolderID" },
  431. new string[] { principalID.ToString(), "My Suitcase", UUID.Zero.ToString() });
  432. if (folders != null && folders.Length > 0)
  433. {
  434. // Move it to under the root folder
  435. XInventoryFolder root = GetRootXFolder(principalID);
  436. folders[0].parentFolderID = root.folderID;
  437. folders[0].type = 100;
  438. m_Database.StoreFolder(folders[0]);
  439. return folders[0];
  440. }
  441. return null;
  442. }
  443. private void SetAsNormalFolder(XInventoryFolder suitcase)
  444. {
  445. suitcase.type = (short)AssetType.Folder;
  446. }
  447. private List<XInventoryFolder> GetFolderTree(UUID principalID, UUID folder)
  448. {
  449. List<XInventoryFolder> t = null;
  450. if (m_SuitcaseTrees.TryGetValue(principalID, out t))
  451. return t;
  452. // Get the tree of the suitcase folder
  453. t = GetFolderTreeRecursive(folder);
  454. m_SuitcaseTrees.AddOrUpdate(principalID, t, 5*60); // 5minutes
  455. return t;
  456. }
  457. private List<XInventoryFolder> GetFolderTreeRecursive(UUID root)
  458. {
  459. List<XInventoryFolder> tree = new List<XInventoryFolder>();
  460. XInventoryFolder[] folders = m_Database.GetFolders(
  461. new string[] { "parentFolderID" },
  462. new string[] { root.ToString() });
  463. if (folders == null || (folders != null && folders.Length == 0))
  464. return tree; // empty tree
  465. else
  466. {
  467. foreach (XInventoryFolder f in folders)
  468. {
  469. tree.Add(f);
  470. tree.AddRange(GetFolderTreeRecursive(f.folderID));
  471. }
  472. return tree;
  473. }
  474. }
  475. /// <summary>
  476. /// Return true if the folderID is a subfolder of the Suitcase or the suitcase folder itself
  477. /// </summary>
  478. /// <param name="folderID"></param>
  479. /// <param name="root"></param>
  480. /// <param name="suitcase"></param>
  481. /// <returns></returns>
  482. private bool IsWithinSuitcaseTree(UUID principalID, UUID folderID)
  483. {
  484. XInventoryFolder suitcase = GetSuitcaseXFolder(principalID);
  485. if (suitcase == null)
  486. {
  487. m_log.WarnFormat("[HG SUITCASE INVENTORY SERVICE]: User {0} does not have a Suitcase folder", principalID);
  488. return false;
  489. }
  490. List<XInventoryFolder> tree = new List<XInventoryFolder>();
  491. tree.Add(suitcase); // Warp! the tree is the real root folder plus the children of the suitcase folder
  492. tree.AddRange(GetFolderTree(principalID, suitcase.folderID));
  493. // Also add the Current Outfit folder to the list of available folders
  494. tree.Add(GetCurrentOutfitXFolder(principalID));
  495. XInventoryFolder f = tree.Find(delegate(XInventoryFolder fl)
  496. {
  497. if (fl.folderID == folderID) return true;
  498. else return false;
  499. });
  500. if (f == null) return false;
  501. else return true;
  502. }
  503. #endregion
  504. #region Avatar Appearance
  505. private AvatarAppearance GetAppearance(UUID principalID)
  506. {
  507. AvatarAppearance a = null;
  508. if (m_Appearances.TryGetValue(principalID, out a))
  509. return a;
  510. a = m_AvatarService.GetAppearance(principalID);
  511. m_Appearances.AddOrUpdate(principalID, a, 5 * 60); // 5minutes
  512. return a;
  513. }
  514. private bool IsPartOfAppearance(UUID principalID, UUID itemID)
  515. {
  516. AvatarAppearance a = GetAppearance(principalID);
  517. if (a == null)
  518. return false;
  519. // Check wearables (body parts and clothes)
  520. for (int i = 0; i < a.Wearables.Length; i++)
  521. {
  522. for (int j = 0; j < a.Wearables[i].Count; j++)
  523. {
  524. if (a.Wearables[i][j].ItemID == itemID)
  525. {
  526. //m_log.DebugFormat("[HG SUITCASE INVENTORY SERVICE]: item {0} is a wearable", itemID);
  527. return true;
  528. }
  529. }
  530. }
  531. // Check attachments
  532. if (a.GetAttachmentForItem(itemID) != null)
  533. {
  534. //m_log.DebugFormat("[HG SUITCASE INVENTORY SERVICE]: item {0} is an attachment", itemID);
  535. return true;
  536. }
  537. return false;
  538. }
  539. #endregion
  540. }
  541. }