sbFileSystemNode.cpp
Go to the documentation of this file.
1 /*
2 //
3 // BEGIN SONGBIRD GPL
4 //
5 // This file is part of the Songbird web player.
6 //
7 // Copyright(c) 2005-2009 POTI, Inc.
8 // http://songbirdnest.com
9 //
10 // This file may be licensed under the terms of of the
11 // GNU General Public License Version 2 (the "GPL").
12 //
13 // Software distributed under the License is distributed
14 // on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
15 // express or implied. See the GPL for the specific language
16 // governing rights and limitations.
17 //
18 // You should have received a copy of the GPL along with this
19 // program. If not, go to http://www.gnu.org/licenses/gpl.html
20 // or write to the Free Software Foundation, Inc.,
21 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 //
23 // END SONGBIRD GPL
24 //
25 */
26 
27 #include "sbFileSystemNode.h"
28 #include <sbFileSystemCID.h>
29 #include <nsIClassInfoImpl.h>
30 #include <nsIProgrammingLanguage.h>
31 #include <nsIObjectInputStream.h>
32 #include <nsIObjectOutputStream.h>
33 #include <nsMemory.h>
34 #include <stack>
35 
36 
38 {
39  NodeContext(const nsAString & aFullPath, sbFileSystemNode *aNode)
40  : fullPath(aFullPath), node(aNode)
41  {
42  }
43 
44  nsString fullPath;
45  nsRefPtr<sbFileSystemNode> node;
46 };
47 
48 
49 static NS_DEFINE_CID(kFileSystemTreeCID,
51 
55 
59 
60 sbFileSystemNode::sbFileSystemNode()
61  : mID(0), mParentID(0)
62 {
63 }
64 
66 {
67 }
68 
69 nsresult
70 sbFileSystemNode::Init(const nsAString & aLeafName,
71  PRBool aIsDir,
72  PRUint64 aLastModify)
73 {
74  NS_ASSERTION(!aLeafName.IsEmpty(), "Error: Leaf name is empty!");
75 
76  mLeafName.Assign(aLeafName);
77  mIsDir = aIsDir;
78  mLastModify = aLastModify;
79  return NS_OK;
80 }
81 
82 nsresult
84 {
85  mChildMap = aNodeMap;
86  return NS_OK;
87 }
88 
89 sbNodeMap*
91 {
92  return &mChildMap;
93 }
94 
95 nsresult
96 sbFileSystemNode::SetLeafName(const nsAString & aLeafName)
97 {
98  mLeafName = aLeafName;
99  return NS_OK;
100 }
101 
102 nsresult
103 sbFileSystemNode::GetLeafName(nsAString & aLeafName)
104 {
105  aLeafName.Assign(mLeafName);
106  return NS_OK;
107 }
108 
109 nsresult
110 sbFileSystemNode::SetIsDir(const PRBool aIsDir)
111 {
112  mIsDir = aIsDir;
113  return NS_OK;
114 }
115 
116 nsresult
118 {
119  NS_ENSURE_ARG_POINTER(aIsDir);
120  *aIsDir = mIsDir;
121  return NS_OK;
122 }
123 
124 nsresult
125 sbFileSystemNode::SetLastModify(const PRInt64 aLastModify)
126 {
127  mLastModify = aLastModify;
128  return NS_OK;
129 }
130 
131 nsresult
132 sbFileSystemNode::GetLastModify(PRInt64 *aLastModify)
133 {
134  NS_ENSURE_ARG_POINTER(aLastModify);
135  *aLastModify = mLastModify;
136  return NS_OK;
137 }
138 
139 nsresult
141 {
142  NS_ENSURE_ARG_POINTER(aNode);
143 
144  nsString leafName;
145  nsresult rv = aNode->GetLeafName(leafName);
146  NS_ENSURE_SUCCESS(rv, rv);
147 
148  mChildMap.insert(sbNodeMapPair(leafName, aNode));
149 
150  return NS_OK;
151 }
152 
153 nsresult
155 {
156  NS_ENSURE_ARG_POINTER(aNode);
157 
158  nsString nodeFileName;
159  nsresult rv = aNode->GetLeafName(nodeFileName);
160  NS_ENSURE_SUCCESS(rv, rv);
161 
162  mChildMap.erase(nodeFileName);
163 
164  return NS_OK;
165 }
166 
167 nsresult
168 sbFileSystemNode::GetChildCount(PRUint32 *aChildCount)
169 {
170  NS_ENSURE_ARG_POINTER(aChildCount);
171  *aChildCount = mChildMap.size();
172  return NS_OK;
173 }
174 
175 nsresult
176 sbFileSystemNode::ReplaceNode(const nsAString & aLeafName,
177  sbFileSystemNode *aReplacementNode)
178 {
179  NS_ENSURE_ARG_POINTER(aReplacementNode);
180  nsString leafName(aLeafName);
181  mChildMap[leafName] = aReplacementNode;
182  return NS_OK;
183 }
184 
185 nsresult
187 {
188  mID = aID;
189  return NS_OK;
190 }
191 
192 nsresult
194 {
195  NS_ENSURE_ARG_POINTER(aID);
196  *aID = mID;
197  return NS_OK;
198 }
199 
200 nsresult
201 sbFileSystemNode::SetParentID(const PRUint32 aID)
202 {
203  mParentID = aID;
204  return NS_OK;
205 }
206 
207 nsresult
209 {
210  NS_ENSURE_ARG_POINTER(aOutID);
211  *aOutID = mParentID;
212  return NS_OK;
213 }
214 
215 //------------------------------------------------------------------------------
216 // nsISerializable
217 
218 NS_IMETHODIMP
219 sbFileSystemNode::Read(nsIObjectInputStream *aInputStream)
220 {
221  NS_ENSURE_ARG_POINTER(aInputStream);
222 
223  nsresult rv;
224  rv = aInputStream->Read32(&mID);
225  NS_ENSURE_SUCCESS(rv, rv);
226 
227  rv = aInputStream->Read32(&mParentID);
228  NS_ENSURE_SUCCESS(rv, rv);
229 
230  // The string is stored as 8-bit string, convert back to UTF16.
231  nsCString storedLeafName;
232  rv = aInputStream->ReadCString(storedLeafName);
233  NS_ENSURE_SUCCESS(rv, rv);
234 
235  mLeafName.Assign(NS_ConvertUTF8toUTF16(storedLeafName));
236 
237  rv = aInputStream->ReadBoolean(&mIsDir);
238  NS_ENSURE_SUCCESS(rv, rv);
239 
240  PRUint64 lastmodify;
241  rv = aInputStream->Read64(&lastmodify);
242  if (NS_SUCCEEDED(rv)) {
243  mLastModify = lastmodify;
244  }
245  else {
246  mLastModify = 0;
247  }
248 
249  return NS_OK;
250 }
251 
252 NS_IMETHODIMP
253 sbFileSystemNode::Write(nsIObjectOutputStream *aOutputStream)
254 {
255  NS_ENSURE_ARG_POINTER(aOutputStream);
256 
257  nsresult rv;
258  rv = aOutputStream->Write32(mID);
259  NS_ENSURE_SUCCESS(rv, rv);
260 
261  rv = aOutputStream->Write32(mParentID);
262  NS_ENSURE_SUCCESS(rv, rv);
263 
264  // Save disk space by storing as UTF8 and decode back in Read().
265  rv = aOutputStream->WriteUtf8Z(mLeafName.BeginReading());
266  NS_ENSURE_SUCCESS(rv, rv);
267 
268  rv = aOutputStream->WriteBoolean(mIsDir);
269  NS_ENSURE_SUCCESS(rv, rv);
270 
271  PRUint64 lastmodify = mLastModify;
272  rv = aOutputStream->Write64(lastmodify);
273  NS_ENSURE_SUCCESS(rv, rv);
274 
275  return NS_OK;
276 }
277 
278 //------------------------------------------------------------------------------
279 // nsIClassInfo
280 
281 NS_IMETHODIMP
282 sbFileSystemNode::GetInterfaces(PRUint32* count, nsIID*** array)
283 {
284  NS_ENSURE_ARG_POINTER(count);
285  NS_ENSURE_ARG_POINTER(array);
286 
287  return NS_CI_INTERFACE_GETTER_NAME(sbFileSystemNode)(count, array);
288 }
289 
290 NS_IMETHODIMP
291 sbFileSystemNode::GetHelperForLanguage(PRUint32 language,
292  nsISupports** _retval)
293 {
294  NS_ENSURE_ARG_POINTER(_retval);
295  *_retval = nsnull;
296  return NS_OK;
297 }
298 
299 NS_IMETHODIMP
300 sbFileSystemNode::GetContractID(char** aContractID)
301 {
302  NS_ENSURE_ARG_POINTER(aContractID);
303  *aContractID = nsnull;
304  return NS_OK;
305 }
306 
307 NS_IMETHODIMP
308 sbFileSystemNode::GetClassDescription(char** aClassDescription)
309 {
310  NS_ENSURE_ARG_POINTER(aClassDescription);
311  *aClassDescription = nsnull;
312  return NS_OK;
313 }
314 
315 NS_IMETHODIMP
316 sbFileSystemNode::GetClassID(nsCID** aClassID)
317 {
318  NS_ENSURE_ARG_POINTER(aClassID);
319  *aClassID = nsnull;
320  return NS_OK;
321 }
322 
323 NS_IMETHODIMP
324 sbFileSystemNode::GetImplementationLanguage(PRUint32* aImplementationLanguage)
325 {
326  NS_ENSURE_ARG_POINTER(aImplementationLanguage);
327  *aImplementationLanguage = nsIProgrammingLanguage::CPLUSPLUS;
328  return NS_OK;
329 }
330 
331 NS_IMETHODIMP
332 sbFileSystemNode::GetFlags(PRUint32* aFlags)
333 {
334  NS_ENSURE_ARG_POINTER(aFlags);
335  *aFlags = 0;
336  return NS_OK;
337 }
338 
339 NS_IMETHODIMP
340 sbFileSystemNode::GetClassIDNoAlloc(nsCID* aClassIDNoAlloc)
341 {
342  NS_ENSURE_ARG_POINTER(aClassIDNoAlloc);
343  *aClassIDNoAlloc = kFileSystemTreeCID;
344  return NS_OK;
345 }
346 
return NS_OK
nsresult SetNodeID(PRUint32 aID)
virtual ~sbFileSystemNode()
inArray array
sbNodeMap * GetChildren()
var language
Definition: Info.js:44
nsresult AddChild(sbFileSystemNode *aNode)
sbNodeMap::value_type sbNodeMapPair
nsresult GetLeafName(nsAString &aLeafName)
nsresult RemoveChild(sbFileSystemNode *aNode)
NodeContext(const nsAString &aFullPath, sbFileSystemNode *aNode)
var count
Definition: test_bug7406.js:32
nsresult SetLeafName(const nsAString &aLeafName)
std::map< nsString, nsRefPtr< sbFileSystemNode > > sbNodeMap
nsRefPtr< sbFileSystemNode > node
nsresult GetParentID(PRUint32 *aOutID)
nsresult GetIsDir(PRBool *aIsDir)
nsISerializable
nsresult SetIsDir(const PRBool aIsDir)
NS_IMPL_THREADSAFE_ISUPPORTS2(sbFileSystemNode, nsISerializable, nsIClassInfo) NS_IMPL_CI_INTERFACE_GETTER2(sbFileSystemNode
nsresult GetChildCount(PRUint32 *aChildCount)
nsresult GetNodeID(PRUint32 *aID)
nsresult ReplaceNode(const nsAString &aLeafName, sbFileSystemNode *aReplacementNode)
nsresult SetLastModify(const PRInt64 aLastModify)
#define SONGBIRD_FILESYSTEMNODE_CID
NS_IMPL_CI_INTERFACE_GETTER2(sbDataRemoteWrapper, sbIDataRemote, nsIClassInfo) sbDataRemoteWrapper
static NS_DEFINE_CID(kFileSystemTreeCID, SONGBIRD_FILESYSTEMNODE_CID)
nsresult SetParentID(const PRUint32 aID)
nsresult GetLastModify(PRInt64 *aLastModify)
nsresult SetChildren(const sbNodeMap &aNodeMap)
NS_DECL_ISUPPORTS NS_DECL_NSISERIALIZABLE NS_DECL_NSICLASSINFO nsresult Init(const nsAString &aLeafName, PRBool aIsDir, PRUint64 aLastModify)