clockwerk-opensim – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 vero 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 System.Reflection;
31 using log4net;
32 using OpenMetaverse;
33  
34 namespace OpenSim.Framework
35 {
36 public class InventoryFolderImpl : InventoryFolderBase
37 {
38 // private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
39  
40 public static readonly string PATH_DELIMITER = "/";
41  
42 /// <summary>
43 /// Items that are contained in this folder
44 /// </summary>
45 public Dictionary<UUID, InventoryItemBase> Items = new Dictionary<UUID, InventoryItemBase>();
46  
47 /// <summary>
48 /// Child folders that are contained in this folder
49 /// </summary>
50 protected Dictionary<UUID, InventoryFolderImpl> m_childFolders = new Dictionary<UUID, InventoryFolderImpl>();
51  
52 // Constructors
53 public InventoryFolderImpl(InventoryFolderBase folderbase)
54 {
55 Owner = folderbase.Owner;
56 ID = folderbase.ID;
57 Name = folderbase.Name;
58 ParentID = folderbase.ParentID;
59 Type = folderbase.Type;
60 Version = folderbase.Version;
61 }
62  
63 public InventoryFolderImpl()
64 {
65 }
66  
67 /// <summary>
68 /// Create a new subfolder.
69 /// </summary>
70 /// <param name="folderID"></param>
71 /// <param name="folderName"></param>
72 /// <param name="type"></param>
73 /// <returns>The newly created subfolder. Returns null if the folder already exists</returns>
74 public InventoryFolderImpl CreateChildFolder(UUID folderID, string folderName, ushort type)
75 {
76 lock (m_childFolders)
77 {
78 if (!m_childFolders.ContainsKey(folderID))
79 {
80 InventoryFolderImpl subFold = new InventoryFolderImpl();
81 subFold.Name = folderName;
82 subFold.ID = folderID;
83 subFold.Type = (short)type;
84 subFold.ParentID = this.ID;
85 subFold.Owner = Owner;
86 m_childFolders.Add(subFold.ID, subFold);
87  
88 return subFold;
89 }
90 }
91  
92 return null;
93 }
94  
95 /// <summary>
96 /// Add a folder that already exists.
97 /// </summary>
98 /// <param name="folder"></param>
99 public void AddChildFolder(InventoryFolderImpl folder)
100 {
101 lock (m_childFolders)
102 {
103 folder.ParentID = ID;
104 m_childFolders[folder.ID] = folder;
105 }
106 }
107  
108 /// <summary>
109 /// Does this folder contain the given child folder?
110 /// </summary>
111 /// <param name="folderID"></param>
112 /// <returns></returns>
113 public bool ContainsChildFolder(UUID folderID)
114 {
115 return m_childFolders.ContainsKey(folderID);
116 }
117  
118 /// <summary>
119 /// Get a child folder
120 /// </summary>
121 /// <param name="folderID"></param>
122 /// <returns>The folder if it exists, null if it doesn't</returns>
123 public InventoryFolderImpl GetChildFolder(UUID folderID)
124 {
125 InventoryFolderImpl folder = null;
126  
127 lock (m_childFolders)
128 {
129 m_childFolders.TryGetValue(folderID, out folder);
130 }
131  
132 return folder;
133 }
134  
135 /// <summary>
136 /// Removes the given child subfolder.
137 /// </summary>
138 /// <param name="folderID"></param>
139 /// <returns>
140 /// The folder removed, or null if the folder was not present.
141 /// </returns>
142 public InventoryFolderImpl RemoveChildFolder(UUID folderID)
143 {
144 InventoryFolderImpl removedFolder = null;
145  
146 lock (m_childFolders)
147 {
148 if (m_childFolders.ContainsKey(folderID))
149 {
150 removedFolder = m_childFolders[folderID];
151 m_childFolders.Remove(folderID);
152 }
153 }
154  
155 return removedFolder;
156 }
157  
158 /// <summary>
159 /// Delete all the folders and items in this folder.
160 /// </summary>
161 public void Purge()
162 {
163 foreach (InventoryFolderImpl folder in m_childFolders.Values)
164 {
165 folder.Purge();
166 }
167  
168 m_childFolders.Clear();
169 Items.Clear();
170 }
171  
172 /// <summary>
173 /// Returns the item if it exists in this folder or in any of this folder's descendant folders
174 /// </summary>
175 /// <param name="itemID"></param>
176 /// <returns>null if the item is not found</returns>
177 public InventoryItemBase FindItem(UUID itemID)
178 {
179 lock (Items)
180 {
181 if (Items.ContainsKey(itemID))
182 {
183 return Items[itemID];
184 }
185 }
186  
187 lock (m_childFolders)
188 {
189 foreach (InventoryFolderImpl folder in m_childFolders.Values)
190 {
191 InventoryItemBase item = folder.FindItem(itemID);
192  
193 if (item != null)
194 {
195 return item;
196 }
197 }
198 }
199  
200 return null;
201 }
202  
203 public InventoryItemBase FindAsset(UUID assetID)
204 {
205 lock (Items)
206 {
207 foreach (InventoryItemBase item in Items.Values)
208 {
209 if (item.AssetID == assetID)
210 return item;
211 }
212 }
213  
214 lock (m_childFolders)
215 {
216 foreach (InventoryFolderImpl folder in m_childFolders.Values)
217 {
218 InventoryItemBase item = folder.FindAsset(assetID);
219  
220 if (item != null)
221 {
222 return item;
223 }
224 }
225 }
226  
227 return null;
228 }
229  
230 /// <summary>
231 /// Deletes an item if it exists in this folder or any children
232 /// </summary>
233 /// <param name="folderID"></param>
234 /// <returns></returns>
235 public bool DeleteItem(UUID itemID)
236 {
237 bool found = false;
238  
239 lock (Items)
240 {
241 if (Items.ContainsKey(itemID))
242 {
243 Items.Remove(itemID);
244 return true;
245 }
246 }
247  
248 lock (m_childFolders)
249 {
250 foreach (InventoryFolderImpl folder in m_childFolders.Values)
251 {
252 found = folder.DeleteItem(itemID);
253  
254 if (found == true)
255 {
256 break;
257 }
258 }
259 }
260  
261 return found;
262 }
263  
264 /// <summary>
265 /// Returns the folder requested if it is this folder or is a descendent of this folder. The search is depth
266 /// first.
267 /// </summary>
268 /// <returns>The requested folder if it exists, null if it does not.</returns>
269 public InventoryFolderImpl FindFolder(UUID folderID)
270 {
271 if (folderID == ID)
272 return this;
273  
274 lock (m_childFolders)
275 {
276 foreach (InventoryFolderImpl folder in m_childFolders.Values)
277 {
278 InventoryFolderImpl returnFolder = folder.FindFolder(folderID);
279  
280 if (returnFolder != null)
281 return returnFolder;
282 }
283 }
284  
285 return null;
286 }
287  
288 /// <summary>
289 /// Look through all child subfolders for a folder marked as one for a particular asset type, and return it.
290 /// </summary>
291 /// <param name="type"></param>
292 /// <returns>Returns null if no such folder is found</returns>
293 public InventoryFolderImpl FindFolderForType(int type)
294 {
295 lock (m_childFolders)
296 {
297 foreach (InventoryFolderImpl f in m_childFolders.Values)
298 {
299 if (f.Type == type)
300 return f;
301 }
302 }
303  
304 return null;
305 }
306  
307 /// <summary>
308 /// Find a folder given a PATH_DELIMITER delimited path starting from this folder
309 /// </summary>
310 ///
311 /// This method does not handle paths that contain multiple delimitors
312 ///
313 /// FIXME: We do not yet handle situations where folders have the same name. We could handle this by some
314 /// XPath like expression
315 ///
316 /// FIXME: Delimitors which occur in names themselves are not currently escapable.
317 ///
318 /// <param name="path">
319 /// The path to the required folder.
320 /// It this is empty or consists only of the PATH_DELIMTER then this folder itself is returned.
321 /// </param>
322 /// <returns>null if the folder is not found</returns>
323 public InventoryFolderImpl FindFolderByPath(string path)
324 {
325 if (path == string.Empty)
326 return this;
327  
328 path = path.Trim();
329  
330 if (path == PATH_DELIMITER)
331 return this;
332  
333 string[] components = path.Split(new string[] { PATH_DELIMITER }, 2, StringSplitOptions.None);
334  
335 lock (m_childFolders)
336 {
337 foreach (InventoryFolderImpl folder in m_childFolders.Values)
338 {
339 if (folder.Name == components[0])
340 if (components.Length > 1)
341 return folder.FindFolderByPath(components[1]);
342 else
343 return folder;
344 }
345 }
346  
347 // We didn't find a folder with the given name
348 return null;
349 }
350  
351 /// <summary>
352 /// Find an item given a PATH_DELIMITOR delimited path starting from this folder.
353 ///
354 /// This method does not handle paths that contain multiple delimitors
355 ///
356 /// FIXME: We do not yet handle situations where folders or items have the same name. We could handle this by some
357 /// XPath like expression
358 ///
359 /// FIXME: Delimitors which occur in names themselves are not currently escapable.
360 /// </summary>
361 /// <param name="path">
362 /// The path to the required item.
363 /// </param>
364 /// <returns>null if the item is not found</returns>
365 public InventoryItemBase FindItemByPath(string path)
366 {
367 string[] components = path.Split(new string[] { PATH_DELIMITER }, 2, StringSplitOptions.None);
368  
369 if (components.Length == 1)
370 {
371 lock (Items)
372 {
373 foreach (InventoryItemBase item in Items.Values)
374 {
375 if (item.Name == components[0])
376 return item;
377 }
378 }
379 }
380 else
381 {
382 lock (m_childFolders)
383 {
384 foreach (InventoryFolderImpl folder in m_childFolders.Values)
385 {
386 if (folder.Name == components[0])
387 return folder.FindItemByPath(components[1]);
388 }
389 }
390 }
391  
392 // We didn't find an item or intermediate folder with the given name
393 return null;
394 }
395  
396 /// <summary>
397 /// Return a copy of the list of child items in this folder. The items themselves are the originals.
398 /// </summary>
399 public List<InventoryItemBase> RequestListOfItems()
400 {
401 List<InventoryItemBase> itemList = new List<InventoryItemBase>();
402  
403 lock (Items)
404 {
405 foreach (InventoryItemBase item in Items.Values)
406 {
407 // m_log.DebugFormat(
408 // "[INVENTORY FOLDER IMPL]: Returning item {0} {1}, OwnerPermissions {2:X}",
409 // item.Name, item.ID, item.CurrentPermissions);
410  
411 itemList.Add(item);
412 }
413 }
414  
415 //m_log.DebugFormat("[INVENTORY FOLDER IMPL]: Found {0} items", itemList.Count);
416  
417 return itemList;
418 }
419  
420 /// <summary>
421 /// Return a copy of the list of child folders in this folder. The folders themselves are the originals.
422 /// </summary>
423 public List<InventoryFolderBase> RequestListOfFolders()
424 {
425 List<InventoryFolderBase> folderList = new List<InventoryFolderBase>();
426  
427 lock (m_childFolders)
428 {
429 foreach (InventoryFolderBase folder in m_childFolders.Values)
430 {
431 folderList.Add(folder);
432 }
433 }
434  
435 return folderList;
436 }
437  
438 public List<InventoryFolderImpl> RequestListOfFolderImpls()
439 {
440 List<InventoryFolderImpl> folderList = new List<InventoryFolderImpl>();
441  
442 lock (m_childFolders)
443 {
444 foreach (InventoryFolderImpl folder in m_childFolders.Values)
445 {
446 folderList.Add(folder);
447 }
448 }
449  
450 return folderList;
451 }
452  
453 /// <value>
454 /// The total number of items in this folder and in the immediate child folders (though not from other
455 /// descendants).
456 /// </value>
457 public int TotalCount
458 {
459 get
460 {
461 int total = Items.Count;
462  
463 foreach (InventoryFolderImpl folder in m_childFolders.Values)
464 {
465 total = total + folder.TotalCount;
466 }
467  
468 return total;
469 }
470 }
471 }
472 }