OpenSim
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros
UserInventoryHelpers.cs
Go to the documentation of this file.
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 
28 using System;
29 using System.Collections.Generic;
30 using OpenMetaverse;
31 using OpenSim.Framework;
32 using OpenSim.Region.CoreModules.Avatar.Inventory.Archiver;
33 using OpenSim.Region.Framework.Scenes;
34 using OpenSim.Services.Interfaces;
35 
36 namespace OpenSim.Tests.Common
37 {
41  public static class UserInventoryHelpers
42  {
43  public static readonly string PATH_DELIMITER = "/";
44 
56  public static InventoryItemBase AddInventoryItem(
57  Scene scene, SceneObjectGroup so, int inventoryIdTail, int assetIdTail)
58  {
59  return AddInventoryItem(
60  scene,
61  so.Name,
62  TestHelpers.ParseTail(inventoryIdTail),
63  InventoryType.Object,
64  AssetHelpers.CreateAsset(TestHelpers.ParseTail(assetIdTail), so),
65  so.OwnerID);
66  }
67 
76  public static InventoryItemBase AddInventoryItem(
77  Scene scene, SceneObjectGroup so, int inventoryIdTail, int assetIdTail, string path)
78  {
79  return AddInventoryItem(
80  scene,
81  so.Name,
82  TestHelpers.ParseTail(inventoryIdTail),
83  InventoryType.Object,
84  AssetHelpers.CreateAsset(TestHelpers.ParseTail(assetIdTail), so),
85  so.OwnerID,
86  path);
87  }
88 
100  private static InventoryItemBase AddInventoryItem(
101  Scene scene, string itemName, UUID itemId, InventoryType itemType, AssetBase asset, UUID userId)
102  {
103  return AddInventoryItem(
104  scene, itemName, itemId, itemType, asset, userId,
105  scene.InventoryService.GetFolderForType(userId, (FolderType)asset.Type).Name);
106  }
107 
119  private static InventoryItemBase AddInventoryItem(
120  Scene scene, string itemName, UUID itemId, InventoryType itemType, AssetBase asset, UUID userId, string path)
121  {
122  scene.AssetService.Store(asset);
123 
125  item.Name = itemName;
126  item.AssetID = asset.FullID;
127  item.ID = itemId;
128  item.Owner = userId;
129  item.AssetType = asset.Type;
130  item.InvType = (int)itemType;
131 
132  InventoryFolderBase folder = InventoryArchiveUtils.FindFoldersByPath(scene.InventoryService, userId, path)[0];
133 
134  item.Folder = folder.ID;
135  scene.AddInventoryItem(item);
136 
137  return item;
138  }
139 
148  public static InventoryItemBase CreateInventoryItem(Scene scene, string itemName, UUID userId)
149  {
150  return CreateInventoryItem(scene, itemName, UUID.Random(), UUID.Random(), userId, InventoryType.Notecard);
151  }
152 
162  public static InventoryItemBase CreateInventoryItem(
163  Scene scene, string itemName, UUID userId, InventoryType type)
164  {
165  return CreateInventoryItem(scene, itemName, UUID.Random(), UUID.Random(), userId, type);
166  }
167 
178  public static InventoryItemBase CreateInventoryItem(
179  Scene scene, string itemName, UUID itemId, UUID assetId, UUID userId, InventoryType itemType)
180  {
181  AssetBase asset = null;
182 
183  if (itemType == InventoryType.Notecard)
184  {
185  asset = AssetHelpers.CreateNotecardAsset();
186  asset.CreatorID = userId.ToString();
187  }
188  else if (itemType == InventoryType.Object)
189  {
190  asset = AssetHelpers.CreateAsset(assetId, SceneHelpers.CreateSceneObject(1, userId));
191  }
192  else
193  {
194  throw new Exception(string.Format("Inventory type {0} not supported", itemType));
195  }
196 
197  return AddInventoryItem(scene, itemName, itemId, itemType, asset, userId);
198  }
199 
219  IInventoryService inventoryService, UUID userId, string path, bool useExistingFolders)
220  {
221  return CreateInventoryFolder(inventoryService, userId, UUID.Random(), path, useExistingFolders);
222  }
223 
244  IInventoryService inventoryService, UUID userId, UUID folderId, string path, bool useExistingFolders)
245  {
246  InventoryFolderBase rootFolder = inventoryService.GetRootFolder(userId);
247 
248  if (null == rootFolder)
249  return null;
250 
251  return CreateInventoryFolder(inventoryService, folderId, rootFolder, path, useExistingFolders);
252  }
253 
278  IInventoryService inventoryService, UUID folderId, InventoryFolderBase parentFolder, string path, bool useExistingFolders)
279  {
280  string[] components = path.Split(new string[] { PATH_DELIMITER }, 2, StringSplitOptions.None);
281 
282  InventoryFolderBase folder = null;
283 
284  if (useExistingFolders)
285  folder = InventoryArchiveUtils.FindFolderByPath(inventoryService, parentFolder, components[0]);
286 
287  if (folder == null)
288  {
289 // Console.WriteLine("Creating folder {0} at {1}", components[0], parentFolder.Name);
290 
291  UUID folderIdForCreate;
292 
293  if (components.Length > 1)
294  folderIdForCreate = UUID.Random();
295  else
296  folderIdForCreate = folderId;
297 
298  folder
299  = new InventoryFolderBase(
300  folderIdForCreate, components[0], parentFolder.Owner, (short)AssetType.Unknown, parentFolder.ID, 0);
301 
302  inventoryService.AddFolder(folder);
303  }
304 // else
305 // {
306 // Console.WriteLine("Found existing folder {0}", folder.Name);
307 // }
308 
309  if (components.Length > 1)
310  return CreateInventoryFolder(inventoryService, folderId, folder, components[1], useExistingFolders);
311  else
312  return folder;
313  }
314 
323  public static InventoryFolderBase GetInventoryFolder(IInventoryService inventoryService, UUID userId, string path)
324  {
325  List<InventoryFolderBase> folders = GetInventoryFolders(inventoryService, userId, path);
326 
327  if (folders.Count != 0)
328  return folders[0];
329  else
330  return null;
331  }
332 
340  public static List<InventoryFolderBase> GetInventoryFolders(IInventoryService inventoryService, UUID userId, string path)
341  {
342  return InventoryArchiveUtils.FindFoldersByPath(inventoryService, userId, path);
343  }
344 
353  public static InventoryItemBase GetInventoryItem(IInventoryService inventoryService, UUID userId, string path)
354  {
355  return InventoryArchiveUtils.FindItemByPath(inventoryService, userId, path);
356  }
357 
365  public static List<InventoryItemBase> GetInventoryItems(IInventoryService inventoryService, UUID userId, string path)
366  {
367  return InventoryArchiveUtils.FindItemsByPath(inventoryService, userId, path);
368  }
369  }
370 }
A scene object group is conceptually an object in the scene. The object is constituted of SceneObject...
sbyte Type
(sbyte) AssetType enum
Definition: AssetBase.cs:198
Asset class. All Assets are reference by this class or a class derived from this class ...
Definition: AssetBase.cs:49
Inventory Item - contains all the properties associated with an individual inventory piece...
delegate void CreateInventoryFolder(IClientAPI remoteClient, UUID folderID, ushort folderType, string folderName, UUID parentID)
IInventoryService InventoryService
Definition: Scene.cs:654
override string Name
The name of an object grouping is always the same as its root part
InventoryFolderBase GetFolderForType(UUID userID, FolderType type)
Gets the user folder for the given folder-type