XInventoryService.cs 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  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 OpenMetaverse;
  30. using log4net;
  31. using Nini.Config;
  32. using System.Reflection;
  33. using OpenSim.Services.Base;
  34. using OpenSim.Services.Interfaces;
  35. using OpenSim.Data;
  36. using OpenSim.Framework;
  37. namespace OpenSim.Services.InventoryService
  38. {
  39. public class XInventoryService : ServiceBase, IInventoryService
  40. {
  41. private static readonly ILog m_log =
  42. LogManager.GetLogger(
  43. MethodBase.GetCurrentMethod().DeclaringType);
  44. protected IXInventoryData m_Database;
  45. protected bool m_AllowDelete = true;
  46. protected string m_ConfigName = "InventoryService";
  47. public XInventoryService(IConfigSource config)
  48. : this(config, "InventoryService")
  49. {
  50. }
  51. public XInventoryService(IConfigSource config, string configName) : base(config)
  52. {
  53. if (configName != string.Empty)
  54. m_ConfigName = configName;
  55. string dllName = String.Empty;
  56. string connString = String.Empty;
  57. //string realm = "Inventory"; // OSG version doesn't use this
  58. //
  59. // Try reading the [InventoryService] section first, if it exists
  60. //
  61. IConfig authConfig = config.Configs[m_ConfigName];
  62. if (authConfig != null)
  63. {
  64. dllName = authConfig.GetString("StorageProvider", dllName);
  65. connString = authConfig.GetString("ConnectionString", connString);
  66. m_AllowDelete = authConfig.GetBoolean("AllowDelete", true);
  67. // realm = authConfig.GetString("Realm", realm);
  68. }
  69. //
  70. // Try reading the [DatabaseService] section, if it exists
  71. //
  72. IConfig dbConfig = config.Configs["DatabaseService"];
  73. if (dbConfig != null)
  74. {
  75. if (dllName == String.Empty)
  76. dllName = dbConfig.GetString("StorageProvider", String.Empty);
  77. if (connString == String.Empty)
  78. connString = dbConfig.GetString("ConnectionString", String.Empty);
  79. }
  80. //
  81. // We tried, but this doesn't exist. We can't proceed.
  82. //
  83. if (dllName == String.Empty)
  84. throw new Exception("No StorageProvider configured");
  85. m_Database = LoadPlugin<IXInventoryData>(dllName,
  86. new Object[] {connString, String.Empty});
  87. if (m_Database == null)
  88. throw new Exception("Could not find a storage interface in the given module");
  89. }
  90. public virtual bool CreateUserInventory(UUID principalID)
  91. {
  92. // This is braindeaad. We can't ever communicate that we fixed
  93. // an existing inventory. Well, just return root folder status,
  94. // but check sanity anyway.
  95. //
  96. bool result = false;
  97. InventoryFolderBase rootFolder = GetRootFolder(principalID);
  98. if (rootFolder == null)
  99. {
  100. rootFolder = ConvertToOpenSim(CreateFolder(principalID, UUID.Zero, (int)AssetType.RootFolder, "My Inventory"));
  101. result = true;
  102. }
  103. XInventoryFolder[] sysFolders = GetSystemFolders(principalID, rootFolder.ID);
  104. if (!Array.Exists(sysFolders, delegate (XInventoryFolder f) { if (f.type == (int)AssetType.Animation) return true; return false; }))
  105. CreateFolder(principalID, rootFolder.ID, (int)AssetType.Animation, "Animations");
  106. if (!Array.Exists(sysFolders, delegate (XInventoryFolder f) { if (f.type == (int)AssetType.Bodypart) return true; return false; }))
  107. CreateFolder(principalID, rootFolder.ID, (int)AssetType.Bodypart, "Body Parts");
  108. if (!Array.Exists(sysFolders, delegate (XInventoryFolder f) { if (f.type == (int)AssetType.CallingCard) return true; return false; }))
  109. CreateFolder(principalID, rootFolder.ID, (int)AssetType.CallingCard, "Calling Cards");
  110. if (!Array.Exists(sysFolders, delegate (XInventoryFolder f) { if (f.type == (int)AssetType.Clothing) return true; return false; }))
  111. CreateFolder(principalID, rootFolder.ID, (int)AssetType.Clothing, "Clothing");
  112. if (!Array.Exists(sysFolders, delegate (XInventoryFolder f) { if (f.type == (int)AssetType.Gesture) return true; return false; }))
  113. CreateFolder(principalID, rootFolder.ID, (int)AssetType.Gesture, "Gestures");
  114. if (!Array.Exists(sysFolders, delegate (XInventoryFolder f) { if (f.type == (int)AssetType.Landmark) return true; return false; }))
  115. CreateFolder(principalID, rootFolder.ID, (int)AssetType.Landmark, "Landmarks");
  116. if (!Array.Exists(sysFolders, delegate (XInventoryFolder f) { if (f.type == (int)AssetType.LostAndFoundFolder) return true; return false; }))
  117. CreateFolder(principalID, rootFolder.ID, (int)AssetType.LostAndFoundFolder, "Lost And Found");
  118. if (!Array.Exists(sysFolders, delegate (XInventoryFolder f) { if (f.type == (int)AssetType.Notecard) return true; return false; }))
  119. CreateFolder(principalID, rootFolder.ID, (int)AssetType.Notecard, "Notecards");
  120. if (!Array.Exists(sysFolders, delegate (XInventoryFolder f) { if (f.type == (int)AssetType.Object) return true; return false; }))
  121. CreateFolder(principalID, rootFolder.ID, (int)AssetType.Object, "Objects");
  122. if (!Array.Exists(sysFolders, delegate (XInventoryFolder f) { if (f.type == (int)AssetType.SnapshotFolder) return true; return false; }))
  123. CreateFolder(principalID, rootFolder.ID, (int)AssetType.SnapshotFolder, "Photo Album");
  124. if (!Array.Exists(sysFolders, delegate (XInventoryFolder f) { if (f.type == (int)AssetType.LSLText) return true; return false; }))
  125. CreateFolder(principalID, rootFolder.ID, (int)AssetType.LSLText, "Scripts");
  126. if (!Array.Exists(sysFolders, delegate (XInventoryFolder f) { if (f.type == (int)AssetType.Sound) return true; return false; }))
  127. CreateFolder(principalID, rootFolder.ID, (int)AssetType.Sound, "Sounds");
  128. if (!Array.Exists(sysFolders, delegate (XInventoryFolder f) { if (f.type == (int)AssetType.Texture) return true; return false; }))
  129. CreateFolder(principalID, rootFolder.ID, (int)AssetType.Texture, "Textures");
  130. if (!Array.Exists(sysFolders, delegate (XInventoryFolder f) { if (f.type == (int)AssetType.TrashFolder) return true; return false; }))
  131. CreateFolder(principalID, rootFolder.ID, (int)AssetType.TrashFolder, "Trash");
  132. return result;
  133. }
  134. protected XInventoryFolder CreateFolder(UUID principalID, UUID parentID, int type, string name)
  135. {
  136. XInventoryFolder newFolder = new XInventoryFolder();
  137. newFolder.folderName = name;
  138. newFolder.type = type;
  139. newFolder.version = 1;
  140. newFolder.folderID = UUID.Random();
  141. newFolder.agentID = principalID;
  142. newFolder.parentFolderID = parentID;
  143. m_Database.StoreFolder(newFolder);
  144. return newFolder;
  145. }
  146. protected virtual XInventoryFolder[] GetSystemFolders(UUID principalID, UUID rootID)
  147. {
  148. // m_log.DebugFormat("[XINVENTORY SERVICE]: Getting system folders for {0}", principalID);
  149. XInventoryFolder[] allFolders = m_Database.GetFolders(
  150. new string[] { "agentID", "parentFolderID" },
  151. new string[] { principalID.ToString(), rootID.ToString() });
  152. XInventoryFolder[] sysFolders = Array.FindAll(
  153. allFolders,
  154. delegate (XInventoryFolder f)
  155. {
  156. if (f.type > 0)
  157. return true;
  158. return false;
  159. });
  160. // m_log.DebugFormat(
  161. // "[XINVENTORY SERVICE]: Found {0} system folders for {1}", sysFolders.Length, principalID);
  162. return sysFolders;
  163. }
  164. public virtual List<InventoryFolderBase> GetInventorySkeleton(UUID principalID)
  165. {
  166. XInventoryFolder[] allFolders = m_Database.GetFolders(
  167. new string[] { "agentID" },
  168. new string[] { principalID.ToString() });
  169. if (allFolders.Length == 0)
  170. return null;
  171. List<InventoryFolderBase> folders = new List<InventoryFolderBase>();
  172. foreach (XInventoryFolder x in allFolders)
  173. {
  174. //m_log.DebugFormat("[XINVENTORY SERVICE]: Adding folder {0} to skeleton", x.folderName);
  175. folders.Add(ConvertToOpenSim(x));
  176. }
  177. return folders;
  178. }
  179. public virtual InventoryFolderBase GetRootFolder(UUID principalID)
  180. {
  181. XInventoryFolder[] folders = m_Database.GetFolders(
  182. new string[] { "agentID", "parentFolderID"},
  183. new string[] { principalID.ToString(), UUID.Zero.ToString() });
  184. if (folders.Length == 0)
  185. return null;
  186. XInventoryFolder root = null;
  187. foreach (XInventoryFolder folder in folders)
  188. if (folder.folderName == "My Inventory")
  189. root = folder;
  190. if (folders == null) // oops
  191. root = folders[0];
  192. return ConvertToOpenSim(root);
  193. }
  194. public virtual InventoryFolderBase GetFolderForType(UUID principalID, AssetType type)
  195. {
  196. // m_log.DebugFormat("[XINVENTORY SERVICE]: Getting folder type {0} for user {1}", type, principalID);
  197. InventoryFolderBase rootFolder = GetRootFolder(principalID);
  198. if (rootFolder == null)
  199. {
  200. m_log.WarnFormat(
  201. "[XINVENTORY]: Found no root folder for {0} in GetFolderForType() when looking for {1}",
  202. principalID, type);
  203. return null;
  204. }
  205. return GetSystemFolderForType(rootFolder, type);
  206. }
  207. private InventoryFolderBase GetSystemFolderForType(InventoryFolderBase rootFolder, AssetType type)
  208. {
  209. // m_log.DebugFormat("[XINVENTORY SERVICE]: Getting folder type {0} for user {1}", type, principalID);
  210. XInventoryFolder[] folders = m_Database.GetFolders(
  211. new string[] { "agentID", "parentFolderID", "type"},
  212. new string[] { rootFolder.Owner.ToString(), rootFolder.ID.ToString(), ((int)type).ToString() });
  213. if (folders.Length == 0)
  214. {
  215. // m_log.WarnFormat("[XINVENTORY SERVICE]: Found no folder for type {0} for user {1}", type, principalID);
  216. return null;
  217. }
  218. // m_log.DebugFormat(
  219. // "[XINVENTORY SERVICE]: Found folder {0} {1} for type {2} for user {3}",
  220. // folders[0].folderName, folders[0].folderID, type, principalID);
  221. return ConvertToOpenSim(folders[0]);
  222. }
  223. public virtual InventoryCollection GetFolderContent(UUID principalID, UUID folderID)
  224. {
  225. // This method doesn't receive a valud principal id from the
  226. // connector. So we disregard the principal and look
  227. // by ID.
  228. //
  229. //m_log.DebugFormat("[XINVENTORY SERVICE]: Fetch contents for folder {0}", folderID.ToString());
  230. InventoryCollection inventory = new InventoryCollection();
  231. inventory.UserID = principalID;
  232. inventory.Folders = new List<InventoryFolderBase>();
  233. inventory.Items = new List<InventoryItemBase>();
  234. XInventoryFolder[] folders = m_Database.GetFolders(
  235. new string[] { "parentFolderID"},
  236. new string[] { folderID.ToString() });
  237. foreach (XInventoryFolder x in folders)
  238. {
  239. //m_log.DebugFormat("[XINVENTORY]: Adding folder {0} to response", x.folderName);
  240. inventory.Folders.Add(ConvertToOpenSim(x));
  241. }
  242. XInventoryItem[] items = m_Database.GetItems(
  243. new string[] { "parentFolderID"},
  244. new string[] { folderID.ToString() });
  245. foreach (XInventoryItem i in items)
  246. {
  247. //m_log.DebugFormat("[XINVENTORY]: Adding item {0} to response", i.inventoryName);
  248. inventory.Items.Add(ConvertToOpenSim(i));
  249. }
  250. return inventory;
  251. }
  252. public virtual List<InventoryItemBase> GetFolderItems(UUID principalID, UUID folderID)
  253. {
  254. // m_log.DebugFormat("[XINVENTORY]: Fetch items for folder {0}", folderID);
  255. // Since we probably don't get a valid principal here, either ...
  256. //
  257. List<InventoryItemBase> invItems = new List<InventoryItemBase>();
  258. XInventoryItem[] items = m_Database.GetItems(
  259. new string[] { "parentFolderID" },
  260. new string[] { folderID.ToString() });
  261. foreach (XInventoryItem i in items)
  262. invItems.Add(ConvertToOpenSim(i));
  263. return invItems;
  264. }
  265. public virtual bool AddFolder(InventoryFolderBase folder)
  266. {
  267. // m_log.DebugFormat("[XINVENTORY]: Add folder {0} type {1} in parent {2}", folder.Name, folder.Type, folder.ParentID);
  268. InventoryFolderBase check = GetFolder(folder);
  269. if (check != null)
  270. return false;
  271. if (folder.Type != (short)AssetType.Folder && folder.Type != (short)AssetType.Unknown)
  272. {
  273. InventoryFolderBase rootFolder = GetRootFolder(folder.Owner);
  274. if (rootFolder == null)
  275. {
  276. m_log.WarnFormat(
  277. "[XINVENTORY]: Found no root folder for {0} in AddFolder() when looking for {1}",
  278. folder.Owner, folder.Type);
  279. return false;
  280. }
  281. // Check we're not trying to add this as a system folder.
  282. if (folder.ParentID == rootFolder.ID)
  283. {
  284. InventoryFolderBase existingSystemFolder
  285. = GetSystemFolderForType(rootFolder, (AssetType)folder.Type);
  286. if (existingSystemFolder != null)
  287. {
  288. m_log.WarnFormat(
  289. "[XINVENTORY]: System folder of type {0} already exists when tried to add {1} to {2} for {3}",
  290. folder.Type, folder.Name, folder.ParentID, folder.Owner);
  291. return false;
  292. }
  293. }
  294. }
  295. XInventoryFolder xFolder = ConvertFromOpenSim(folder);
  296. return m_Database.StoreFolder(xFolder);
  297. }
  298. public virtual bool UpdateFolder(InventoryFolderBase folder)
  299. {
  300. // m_log.DebugFormat("[XINVENTORY]: Update folder {0} {1} ({2})", folder.Name, folder.Type, folder.ID);
  301. XInventoryFolder xFolder = ConvertFromOpenSim(folder);
  302. InventoryFolderBase check = GetFolder(folder);
  303. if (check == null)
  304. return AddFolder(folder);
  305. if ((check.Type != (short)AssetType.Unknown || xFolder.type != (short)AssetType.Unknown)
  306. && (check.Type != (short)AssetType.OutfitFolder || xFolder.type != (short)AssetType.OutfitFolder))
  307. {
  308. if (xFolder.version < check.Version)
  309. {
  310. // m_log.DebugFormat("[XINVENTORY]: {0} < {1} can't do", xFolder.version, check.Version);
  311. return false;
  312. }
  313. check.Version = (ushort)xFolder.version;
  314. xFolder = ConvertFromOpenSim(check);
  315. // m_log.DebugFormat(
  316. // "[XINVENTORY]: Storing version only update to system folder {0} {1} {2}",
  317. // xFolder.folderName, xFolder.version, xFolder.type);
  318. return m_Database.StoreFolder(xFolder);
  319. }
  320. if (xFolder.version < check.Version)
  321. xFolder.version = check.Version;
  322. xFolder.folderID = check.ID;
  323. return m_Database.StoreFolder(xFolder);
  324. }
  325. public virtual bool MoveFolder(InventoryFolderBase folder)
  326. {
  327. return m_Database.MoveFolder(folder.ID.ToString(), folder.ParentID.ToString());
  328. }
  329. // We don't check the principal's ID here
  330. //
  331. public virtual bool DeleteFolders(UUID principalID, List<UUID> folderIDs)
  332. {
  333. return DeleteFolders(principalID, folderIDs, true);
  334. }
  335. public virtual bool DeleteFolders(UUID principalID, List<UUID> folderIDs, bool onlyIfTrash)
  336. {
  337. if (!m_AllowDelete)
  338. return false;
  339. // Ignore principal ID, it's bogus at connector level
  340. //
  341. foreach (UUID id in folderIDs)
  342. {
  343. if (onlyIfTrash && !ParentIsTrash(id))
  344. continue;
  345. //m_log.InfoFormat("[XINVENTORY SERVICE]: Delete folder {0}", id);
  346. InventoryFolderBase f = new InventoryFolderBase();
  347. f.ID = id;
  348. PurgeFolder(f, onlyIfTrash);
  349. m_Database.DeleteFolders("folderID", id.ToString());
  350. }
  351. return true;
  352. }
  353. public virtual bool PurgeFolder(InventoryFolderBase folder)
  354. {
  355. return PurgeFolder(folder, true);
  356. }
  357. public virtual bool PurgeFolder(InventoryFolderBase folder, bool onlyIfTrash)
  358. {
  359. if (!m_AllowDelete)
  360. return false;
  361. if (onlyIfTrash && !ParentIsTrash(folder.ID))
  362. return false;
  363. XInventoryFolder[] subFolders = m_Database.GetFolders(
  364. new string[] { "parentFolderID" },
  365. new string[] { folder.ID.ToString() });
  366. foreach (XInventoryFolder x in subFolders)
  367. {
  368. PurgeFolder(ConvertToOpenSim(x), onlyIfTrash);
  369. m_Database.DeleteFolders("folderID", x.folderID.ToString());
  370. }
  371. m_Database.DeleteItems("parentFolderID", folder.ID.ToString());
  372. return true;
  373. }
  374. public virtual bool AddItem(InventoryItemBase item)
  375. {
  376. // m_log.DebugFormat(
  377. // "[XINVENTORY SERVICE]: Adding item {0} {1} to folder {2} for {3}", item.Name, item.ID, item.Folder, item.Owner);
  378. return m_Database.StoreItem(ConvertFromOpenSim(item));
  379. }
  380. public virtual bool UpdateItem(InventoryItemBase item)
  381. {
  382. if (!m_AllowDelete)
  383. if (item.AssetType == (sbyte)AssetType.Link || item.AssetType == (sbyte)AssetType.LinkFolder)
  384. return false;
  385. // m_log.InfoFormat(
  386. // "[XINVENTORY SERVICE]: Updating item {0} {1} in folder {2}", item.Name, item.ID, item.Folder);
  387. InventoryItemBase retrievedItem = GetItem(item);
  388. if (retrievedItem == null)
  389. {
  390. m_log.WarnFormat(
  391. "[XINVENTORY SERVICE]: Tried to update item {0} {1}, owner {2} but no existing item found.",
  392. item.Name, item.ID, item.Owner);
  393. return false;
  394. }
  395. // Do not allow invariants to change. Changes to folder ID occur in MoveItems()
  396. if (retrievedItem.InvType != item.InvType
  397. || retrievedItem.AssetType != item.AssetType
  398. || retrievedItem.Folder != item.Folder
  399. || retrievedItem.CreatorIdentification != item.CreatorIdentification
  400. || retrievedItem.Owner != item.Owner)
  401. {
  402. m_log.WarnFormat(
  403. "[XINVENTORY SERVICE]: Caller to UpdateItem() for {0} {1} tried to alter property(s) that should be invariant, (InvType, AssetType, Folder, CreatorIdentification, Owner), existing ({2}, {3}, {4}, {5}, {6}), update ({7}, {8}, {9}, {10}, {11})",
  404. retrievedItem.Name,
  405. retrievedItem.ID,
  406. retrievedItem.InvType,
  407. retrievedItem.AssetType,
  408. retrievedItem.Folder,
  409. retrievedItem.CreatorIdentification,
  410. retrievedItem.Owner,
  411. item.InvType,
  412. item.AssetType,
  413. item.Folder,
  414. item.CreatorIdentification,
  415. item.Owner);
  416. item.InvType = retrievedItem.InvType;
  417. item.AssetType = retrievedItem.AssetType;
  418. item.Folder = retrievedItem.Folder;
  419. item.CreatorIdentification = retrievedItem.CreatorIdentification;
  420. item.Owner = retrievedItem.Owner;
  421. }
  422. return m_Database.StoreItem(ConvertFromOpenSim(item));
  423. }
  424. public virtual bool MoveItems(UUID principalID, List<InventoryItemBase> items)
  425. {
  426. // Principal is b0rked. *sigh*
  427. //
  428. foreach (InventoryItemBase i in items)
  429. {
  430. m_Database.MoveItem(i.ID.ToString(), i.Folder.ToString());
  431. }
  432. return true;
  433. }
  434. public virtual bool DeleteItems(UUID principalID, List<UUID> itemIDs)
  435. {
  436. if (!m_AllowDelete)
  437. {
  438. // We must still allow links and links to folders to be deleted, otherwise they will build up
  439. // in the player's inventory until they can no longer log in. Deletions of links due to code bugs or
  440. // similar is inconvenient but on a par with accidental movement of items. The original item is never
  441. // touched.
  442. foreach (UUID id in itemIDs)
  443. {
  444. if (!m_Database.DeleteItems(
  445. new string[] { "inventoryID", "assetType" },
  446. new string[] { id.ToString(), ((sbyte)AssetType.Link).ToString() }))
  447. {
  448. m_Database.DeleteItems(
  449. new string[] { "inventoryID", "assetType" },
  450. new string[] { id.ToString(), ((sbyte)AssetType.LinkFolder).ToString() });
  451. }
  452. }
  453. }
  454. else
  455. {
  456. // Just use the ID... *facepalms*
  457. //
  458. foreach (UUID id in itemIDs)
  459. m_Database.DeleteItems("inventoryID", id.ToString());
  460. }
  461. return true;
  462. }
  463. public virtual InventoryItemBase GetItem(InventoryItemBase item)
  464. {
  465. XInventoryItem[] items = m_Database.GetItems(
  466. new string[] { "inventoryID" },
  467. new string[] { item.ID.ToString() });
  468. if (items.Length == 0)
  469. return null;
  470. return ConvertToOpenSim(items[0]);
  471. }
  472. public virtual InventoryFolderBase GetFolder(InventoryFolderBase folder)
  473. {
  474. XInventoryFolder[] folders = m_Database.GetFolders(
  475. new string[] { "folderID"},
  476. new string[] { folder.ID.ToString() });
  477. if (folders.Length == 0)
  478. return null;
  479. return ConvertToOpenSim(folders[0]);
  480. }
  481. public virtual List<InventoryItemBase> GetActiveGestures(UUID principalID)
  482. {
  483. XInventoryItem[] items = m_Database.GetActiveGestures(principalID);
  484. if (items.Length == 0)
  485. return new List<InventoryItemBase>();
  486. List<InventoryItemBase> ret = new List<InventoryItemBase>();
  487. foreach (XInventoryItem x in items)
  488. ret.Add(ConvertToOpenSim(x));
  489. return ret;
  490. }
  491. public virtual int GetAssetPermissions(UUID principalID, UUID assetID)
  492. {
  493. return m_Database.GetAssetPermissions(principalID, assetID);
  494. }
  495. public virtual InventoryCollection GetUserInventory(UUID userID)
  496. {
  497. InventoryCollection userInventory = new InventoryCollection();
  498. userInventory.UserID = userID;
  499. userInventory.Folders = new List<InventoryFolderBase>();
  500. userInventory.Items = new List<InventoryItemBase>();
  501. List<InventoryFolderBase> skel = GetInventorySkeleton(userID);
  502. if (skel != null)
  503. {
  504. foreach (InventoryFolderBase f in skel)
  505. {
  506. InventoryCollection c = GetFolderContent(userID, f.ID);
  507. if (c != null && c.Items != null && c.Items.Count > 0)
  508. userInventory.Items.AddRange(c.Items);
  509. if (c != null && c.Folders != null && c.Folders.Count > 0)
  510. userInventory.Folders.AddRange(c.Folders);
  511. }
  512. }
  513. m_log.DebugFormat("[XINVENTORY SERVICE]: GetUserInventory for user {0} returning {1} folders and {2} items",
  514. userID, userInventory.Folders.Count, userInventory.Items.Count);
  515. return userInventory;
  516. }
  517. public void GetUserInventory(UUID userID, InventoryReceiptCallback callback)
  518. {
  519. }
  520. // Unused.
  521. //
  522. public bool HasInventoryForUser(UUID userID)
  523. {
  524. return false;
  525. }
  526. // CM Helpers
  527. //
  528. protected InventoryFolderBase ConvertToOpenSim(XInventoryFolder folder)
  529. {
  530. InventoryFolderBase newFolder = new InventoryFolderBase();
  531. newFolder.ParentID = folder.parentFolderID;
  532. newFolder.Type = (short)folder.type;
  533. // Viewer can't understand anything that's not in it's LLFolderType enum
  534. if (newFolder.Type == 100)
  535. newFolder.Type = -1;
  536. newFolder.Version = (ushort)folder.version;
  537. newFolder.Name = folder.folderName;
  538. newFolder.Owner = folder.agentID;
  539. newFolder.ID = folder.folderID;
  540. return newFolder;
  541. }
  542. protected XInventoryFolder ConvertFromOpenSim(InventoryFolderBase folder)
  543. {
  544. XInventoryFolder newFolder = new XInventoryFolder();
  545. newFolder.parentFolderID = folder.ParentID;
  546. newFolder.type = (int)folder.Type;
  547. newFolder.version = (int)folder.Version;
  548. newFolder.folderName = folder.Name;
  549. newFolder.agentID = folder.Owner;
  550. newFolder.folderID = folder.ID;
  551. return newFolder;
  552. }
  553. protected InventoryItemBase ConvertToOpenSim(XInventoryItem item)
  554. {
  555. InventoryItemBase newItem = new InventoryItemBase();
  556. newItem.AssetID = item.assetID;
  557. newItem.AssetType = item.assetType;
  558. newItem.Name = item.inventoryName;
  559. newItem.Owner = item.avatarID;
  560. newItem.ID = item.inventoryID;
  561. newItem.InvType = item.invType;
  562. newItem.Folder = item.parentFolderID;
  563. newItem.CreatorIdentification = item.creatorID;
  564. newItem.Description = item.inventoryDescription;
  565. newItem.NextPermissions = (uint)item.inventoryNextPermissions;
  566. newItem.CurrentPermissions = (uint)item.inventoryCurrentPermissions;
  567. newItem.BasePermissions = (uint)item.inventoryBasePermissions;
  568. newItem.EveryOnePermissions = (uint)item.inventoryEveryOnePermissions;
  569. newItem.GroupPermissions = (uint)item.inventoryGroupPermissions;
  570. newItem.GroupID = item.groupID;
  571. if (item.groupOwned == 0)
  572. newItem.GroupOwned = false;
  573. else
  574. newItem.GroupOwned = true;
  575. newItem.SalePrice = item.salePrice;
  576. newItem.SaleType = (byte)item.saleType;
  577. newItem.Flags = (uint)item.flags;
  578. newItem.CreationDate = item.creationDate;
  579. return newItem;
  580. }
  581. protected XInventoryItem ConvertFromOpenSim(InventoryItemBase item)
  582. {
  583. XInventoryItem newItem = new XInventoryItem();
  584. newItem.assetID = item.AssetID;
  585. newItem.assetType = item.AssetType;
  586. newItem.inventoryName = item.Name;
  587. newItem.avatarID = item.Owner;
  588. newItem.inventoryID = item.ID;
  589. newItem.invType = item.InvType;
  590. newItem.parentFolderID = item.Folder;
  591. newItem.creatorID = item.CreatorIdentification;
  592. newItem.inventoryDescription = item.Description;
  593. newItem.inventoryNextPermissions = (int)item.NextPermissions;
  594. newItem.inventoryCurrentPermissions = (int)item.CurrentPermissions;
  595. newItem.inventoryBasePermissions = (int)item.BasePermissions;
  596. newItem.inventoryEveryOnePermissions = (int)item.EveryOnePermissions;
  597. newItem.inventoryGroupPermissions = (int)item.GroupPermissions;
  598. newItem.groupID = item.GroupID;
  599. if (item.GroupOwned)
  600. newItem.groupOwned = 1;
  601. else
  602. newItem.groupOwned = 0;
  603. newItem.salePrice = item.SalePrice;
  604. newItem.saleType = (int)item.SaleType;
  605. newItem.flags = (int)item.Flags;
  606. newItem.creationDate = item.CreationDate;
  607. return newItem;
  608. }
  609. private bool ParentIsTrash(UUID folderID)
  610. {
  611. XInventoryFolder[] folder = m_Database.GetFolders(new string[] {"folderID"}, new string[] {folderID.ToString()});
  612. if (folder.Length < 1)
  613. return false;
  614. if (folder[0].type == (int)AssetType.TrashFolder)
  615. return true;
  616. UUID parentFolder = folder[0].parentFolderID;
  617. while (parentFolder != UUID.Zero)
  618. {
  619. XInventoryFolder[] parent = m_Database.GetFolders(new string[] {"folderID"}, new string[] {parentFolder.ToString()});
  620. if (parent.Length < 1)
  621. return false;
  622. if (parent[0].type == (int)AssetType.TrashFolder)
  623. return true;
  624. if (parent[0].type == (int)AssetType.RootFolder)
  625. return false;
  626. parentFolder = parent[0].parentFolderID;
  627. }
  628. return false;
  629. }
  630. }
  631. }