HGSuitcaseInventoryService.cs 28 KB

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