30 #include <nsAutoPtr.h>
31 #include <nsComponentManagerUtils.h>
32 #include <nsServiceManagerUtils.h>
34 #include <nsIAsyncOutputStream.h>
35 #include <nsIAsyncInputStream.h>
36 #include <nsILocalFile.h>
37 #include <nsIFileStreams.h>
38 #include <nsIProperties.h>
39 #include <nsAppDirectoryServiceDefs.h>
43 #define TREE_FOLDER_NAME "fstrees"
44 #define SESSION_FILENAME_EXTENSION ".tree"
45 #define TREE_SCHEMA_VERSION 1
88 const nsID & aSessionID)
90 NS_ENSURE_ARG_POINTER(aTree);
94 nsCOMPtr<nsIFile> savedSessionFile;
97 getter_AddRefs(savedSessionFile));
98 NS_ENSURE_SUCCESS(rv, rv);
100 nsRefPtr<sbFileObjectOutputStream> fileObjectStream =
102 NS_ENSURE_TRUE(fileObjectStream, NS_ERROR_OUT_OF_MEMORY);
104 rv = fileObjectStream->InitWithFile(savedSessionFile);
105 NS_ENSURE_SUCCESS(rv, rv);
110 NS_ENSURE_SUCCESS(rv, rv);
113 rv = fileObjectStream->WriteString(aTree->mRootPath);
114 NS_ENSURE_SUCCESS(rv, rv);
117 rv = fileObjectStream->WritePRBool(aTree->mIsRecursiveBuild);
118 NS_ENSURE_SUCCESS(rv, rv);
121 PRUint32 nodeCount = 0;
123 NS_ENSURE_SUCCESS(rv, rv);
125 rv = fileObjectStream->WriteUint32(nodeCount);
126 NS_ENSURE_SUCCESS(rv, rv);
129 std::stack<nsRefPtr<sbFileSystemNode> > nodeStack;
130 nodeStack.push(aTree->mRootNode);
134 PRUint32 curNodeID = 0;
136 while (!nodeStack.empty()) {
137 nsRefPtr<sbFileSystemNode> curNode = nodeStack.top();
141 NS_WARNING(
"Coult not get the node from the node stack!");
146 rv = curNode->SetNodeID(curNodeID);
148 NS_WARNING(
"Could not set the node ID!");
152 rv =
WriteNode(fileObjectStream, curNode);
154 NS_WARNING(
"Could not write curNode to disk!");
158 sbNodeMap *curNodeChildren = curNode->GetChildren();
159 if (curNodeChildren && curNodeChildren->size() > 0) {
165 for (next = begin; next != end; ++
next) {
166 nsRefPtr<sbFileSystemNode> curChildNode(next->second);
168 NS_WARNING(
"Could not get get curChildNode!");
172 rv = curChildNode->SetParentID(curNodeID);
174 NS_WARNING(
"Could not set the parent GUID!");
178 nodeStack.push(curChildNode);
186 rv = fileObjectStream->Close();
187 NS_ENSURE_SUCCESS(rv, rv);
194 nsString & aSessionAbsolutePath,
195 PRBool *aIsRecursiveWatch,
198 NS_ENSURE_ARG_POINTER(aOutRootNode);
202 nsCOMPtr<nsIFile> savedSessionFile;
205 getter_AddRefs(savedSessionFile));
206 NS_ENSURE_SUCCESS(rv, rv);
209 PRBool exists = PR_FALSE;
210 if (NS_FAILED(savedSessionFile->Exists(&exists)) || !exists) {
211 NS_WARNING(
"The saved session file no longer exists!");
212 return NS_ERROR_UNEXPECTED;
215 nsRefPtr<sbFileObjectInputStream> fileObjectStream =
217 NS_ENSURE_TRUE(fileObjectStream, NS_ERROR_OUT_OF_MEMORY);
219 rv = fileObjectStream->InitWithFile(savedSessionFile);
220 NS_ENSURE_SUCCESS(rv, rv);
224 PRUint32 schemaVersion = 0;
225 rv = fileObjectStream->ReadUint32(&schemaVersion);
226 NS_ENSURE_SUCCESS(rv, rv);
231 return NS_ERROR_FAILURE;
235 rv = fileObjectStream->ReadString(aSessionAbsolutePath);
236 NS_ENSURE_SUCCESS(rv, rv);
239 rv = fileObjectStream->ReadPRBool(aIsRecursiveWatch);
240 NS_ENSURE_SUCCESS(rv, rv);
243 PRUint32 nodeCount = 0;
244 rv = fileObjectStream->ReadUint32(&nodeCount);
245 NS_ENSURE_SUCCESS(rv, rv);
249 nsRefPtr<sbFileSystemNode> savedRootNode;
251 for (PRUint32
i = 0;
i < nodeCount;
i++) {
252 nsRefPtr<sbFileSystemNode> curNode;
253 rv =
ReadNode(fileObjectStream, getter_AddRefs(curNode));
255 NS_ENSURE_SUCCESS(rv, rv);
256 NS_ENSURE_TRUE(curNode, NS_ERROR_UNEXPECTED);
260 rv = curNode->GetNodeID(&curNodeID);
262 NS_ENSURE_SUCCESS(rv, rv);
269 savedRootNode = curNode;
276 NS_ENSURE_SUCCESS(rv, rv);
279 rv = fileObjectStream->Close();
280 NS_WARN_IF_FALSE(NS_SUCCEEDED(rv),
"Could not close the file object stream!");
282 savedRootNode.forget(aOutRootNode);
290 nsCOMPtr<nsIFile> sessionFile;
292 NS_ENSURE_SUCCESS(rv, rv);
294 PRBool fileExists = PR_FALSE;
295 if (NS_SUCCEEDED(sessionFile->Exists(&fileExists)) && fileExists) {
296 rv = sessionFile->Remove(PR_FALSE);
297 NS_ENSURE_SUCCESS(rv, rv);
307 NS_ENSURE_ARG_POINTER(aOutputStream);
308 NS_ENSURE_ARG_POINTER(aOutNode);
311 nsCOMPtr<nsISupports> writeSupports =
312 do_QueryInterface(NS_ISUPPORTS_CAST(
nsISerializable *, aOutNode), &rv);
313 NS_ENSURE_SUCCESS(rv, rv);
315 return aOutputStream->
WriteObject(writeSupports, PR_TRUE);
322 NS_ENSURE_ARG_POINTER(aInputStream);
323 NS_ENSURE_ARG_POINTER(aOutNode);
326 nsCOMPtr<nsISupports> readSupports;
327 rv = aInputStream->
ReadObject(PR_TRUE, getter_AddRefs(readSupports));
328 NS_ENSURE_SUCCESS(rv, rv);
332 nsCOMPtr<nsISerializable> serializable =
333 do_QueryInterface(readSupports, &rv);
334 NS_ENSURE_SUCCESS(rv, rv);
337 NS_IF_ADDREF(*aOutNode);
346 NS_ENSURE_ARG_POINTER(aChildNode);
351 NS_ENSURE_SUCCESS(rv, rv);
354 if (found == aParentGuidMap.end()) {
355 return NS_ERROR_UNEXPECTED;
358 nsRefPtr<sbFileSystemNode> parentNode(found->second);
359 NS_ENSURE_TRUE(parentNode, NS_ERROR_UNEXPECTED);
361 rv = parentNode->AddChild(aChildNode);
362 NS_ENSURE_SUCCESS(rv, rv);
369 PRBool aShouldCreate,
372 char idChars[NSID_LENGTH];
373 aSessionID.ToProvidedString(idChars);
374 nsString sessionFilename;
375 sessionFilename.Append(NS_ConvertASCIItoUTF16(idChars));
379 nsCOMPtr<nsIProperties> dirService =
380 do_GetService(
"@mozilla.org/file/directory_service;1", &rv);
381 NS_ENSURE_SUCCESS(rv, rv);
384 rv = dirService->Get(NS_APP_PREFS_50_DIR,
386 getter_AddRefs(profileDir));
387 NS_ENSURE_SUCCESS(rv, rv);
389 nsCOMPtr<nsIFile> treeFolder;
390 rv = profileDir->Clone(getter_AddRefs(treeFolder));
391 NS_ENSURE_SUCCESS(rv, rv);
394 NS_ENSURE_SUCCESS(rv, rv);
396 PRBool folderExists = PR_FALSE;
397 if (NS_SUCCEEDED(treeFolder->Exists(&folderExists)) && !folderExists) {
398 rv = treeFolder->Create(nsIFile::DIRECTORY_TYPE, 0755);
399 NS_ENSURE_SUCCESS(rv, rv);
402 nsCOMPtr<nsIFile> newFile;
403 rv = treeFolder->Clone(getter_AddRefs(newFile));
404 NS_ENSURE_SUCCESS(rv, rv);
406 rv = newFile->Append(sessionFilename);
407 NS_ENSURE_SUCCESS(rv, rv);
410 PRBool exists = PR_FALSE;
411 if (NS_SUCCEEDED(newFile->Exists(&exists)) && exists) {
412 rv = newFile->Remove(PR_FALSE);
413 NS_ENSURE_SUCCESS(rv, rv);
416 rv = newFile->Create(nsIFile::NORMAL_FILE_TYPE, 0600);
417 NS_ENSURE_SUCCESS(rv, rv);
420 newFile.swap(*aOutFile);
426 PRUint32 *aNodeCount)
428 NS_ENSURE_ARG_POINTER(aRootNode);
429 NS_ENSURE_ARG_POINTER(aNodeCount);
431 PRUint32 nodeCount = 0;
433 std::stack<nsRefPtr<sbFileSystemNode> > nodeStack;
434 nodeStack.push(aRootNode);
436 while (!nodeStack.empty()) {
437 nsRefPtr<sbFileSystemNode> curNode = nodeStack.top();
443 sbNodeMap *childMap = curNode->GetChildren();
444 if (!childMap || childMap->size() == 0) {
452 for (next = begin; next != end; ++
next) {
453 nodeStack.push(next->second);
457 *aNodeCount = nodeCount;
nsresult LoadTreeState(nsID &aSessionID, nsString &aSessionAbsolutePath, PRBool *aIsRecursiveWatch, sbFileSystemNode **aOutRootNode)
#define TREE_SCHEMA_VERSION
#define SESSION_FILENAME_EXTENSION
nsresult GetTreeNodeCount(sbFileSystemNode *aRootNode, PRUint32 *aNodeCount)
static nsresult DeleteSavedTreeState(const nsID &aSessionID)
nsresult WriteObject(nsISupports *aSupports, PRBool aIsStrongRef)
nsresult AssignRelationships(sbFileSystemNode *aChildNode, sbNodeIDMap &aParentGuidMap)
NS_DECL_ISUPPORTS nsresult SaveTreeState(sbFileSystemTree *aFileSystemTree, const nsID &aSessionID)
std::map< nsString, nsRefPtr< sbFileSystemNode > > sbNodeMap
nsresult GetParentID(PRUint32 *aOutID)
virtual ~sbFileSystemTreeState()
sbNodeMap::const_iterator sbNodeMapIter
static nsresult GetTreeSessionFile(const nsID &aSessionID, PRBool aShouldCreate, nsIFile **aOutFile)
sbNodeIDMap::const_iterator sbNodeIDMapIter
std::map< PRUint32, nsRefPtr< sbFileSystemNode > > sbNodeIDMap
nsresult WriteNode(sbFileObjectOutputStream *aOutputStream, sbFileSystemNode *aOutNode)
sbNodeIDMap::value_type sbNodeIDMapPair
_getSelectedPageStyle s i
nsresult ReadNode(sbFileObjectInputStream *aInputStream, sbFileSystemNode **aOutNode)