1
0

HGSuitcaseInventoryService.cs 28 KB

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