Diff for /chimera/BookmarksService.mm between versions 1.3 and 1.4

version 1.3, 2002/02/09 01:25:26 version 1.4, 2002/02/09 01:52:27
Line 36 Line 36
 {  {
     [super dealloc];      [super dealloc];
     if (mBookmarks)      if (mBookmarks)
        BookmarksService::RemoveObserver(nsnull);        mBookmarks->RemoveObserver();
 }  }
   
 -(void) ensureBookmarks  -(void) ensureBookmarks
Line 44 Line 44
     if (mBookmarks)      if (mBookmarks)
         return;          return;
           
    mBookmarks = BookmarksService::AddObserver(nsnull);    mBookmarks = new BookmarksService();
     mBookmarks->AddObserver();
           
     [mOutlineView setTarget: self];      [mOutlineView setTarget: self];
     [mOutlineView setDoubleAction: @selector(openBookmark:)];      [mOutlineView setDoubleAction: @selector(openBookmark:)];
Line 226  StripWhitespaceNodes(nsIContent* aElemen Line 227  StripWhitespaceNodes(nsIContent* aElemen
 }  }
   
 PRUint32 BookmarksService::gRefCnt = 0;  PRUint32 BookmarksService::gRefCnt = 0;
BookmarksService* BookmarksService::gSingleton = nsnull;nsIDocument* BookmarksService::gBookmarks = nsnull;
 NSMutableDictionary* BookmarksService::gDictionary = nil;
 nsIAtom* BookmarksService::gFolderAtom = nsnull;  nsIAtom* BookmarksService::gFolderAtom = nsnull;
 nsIAtom* BookmarksService::gBookmarkAtom = nsnull;  nsIAtom* BookmarksService::gBookmarkAtom = nsnull;
 nsIAtom* BookmarksService::gHrefAtom = nsnull;  nsIAtom* BookmarksService::gHrefAtom = nsnull;
Line 234  nsIAtom* BookmarksService::gNameAtom = n Line 236  nsIAtom* BookmarksService::gNameAtom = n
   
 BookmarksService::BookmarksService()  BookmarksService::BookmarksService()
 {  {
    mDictionary = nil;    NS_INIT_ISUPPORTS();
    nsCOMPtr<nsIFile> profileDir; 
    NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR, getter_AddRefs(profileDir)); 
    profileDir->Append("bookmarks.xml"); 
 
    nsXPIDLCString bookmarksFileURL; 
    NS_GetURLSpecFromFile(profileDir, getter_Copies(bookmarksFileURL)); 
     
    nsCOMPtr<nsIURI> uri; 
    NS_NewURI(getter_AddRefs(uri), bookmarksFileURL.get()); 
 
    nsCOMPtr<nsIXBLService> xblService(do_GetService("@mozilla.org/xbl;1"));     
    xblService->FetchSyncXMLDocument(uri, getter_AddRefs(mBookmarks)); 
     
    nsCOMPtr<nsIContent> rootNode; 
    GetRootContent(getter_AddRefs(rootNode)); 
    StripWhitespaceNodes(rootNode); 
 }  }
   
 BookmarksService::~BookmarksService()  BookmarksService::~BookmarksService()
 {  {
     [mDictionary release];  
 }  }
   
 void  void
 BookmarksService::GetRootContent(nsIContent** aResult)  BookmarksService::GetRootContent(nsIContent** aResult)
 {  {
     *aResult = nsnull;      *aResult = nsnull;
    if (mBookmarks) {    if (gBookmarks) {
         nsCOMPtr<nsIDOMElement> elt;          nsCOMPtr<nsIDOMElement> elt;
        nsCOMPtr<nsIDOMDocument> domDoc(do_QueryInterface(mBookmarks));        nsCOMPtr<nsIDOMDocument> domDoc(do_QueryInterface(gBookmarks));
         domDoc->GetDocumentElement(getter_AddRefs(elt));          domDoc->GetDocumentElement(getter_AddRefs(elt));
         elt->QueryInterface(NS_GET_IID(nsIContent), (void**)aResult); // Addref happens here.          elt->QueryInterface(NS_GET_IID(nsIContent), (void**)aResult); // Addref happens here.
     }      }
Line 273  BookmarksService::GetRootContent(nsICont Line 258  BookmarksService::GetRootContent(nsICont
 BookmarkItem*  BookmarkItem*
 BookmarksService::GetWrapperFor(nsIContent* aContent)  BookmarksService::GetWrapperFor(nsIContent* aContent)
 {  {
    if (!mDictionary)    if (!gDictionary)
        mDictionary = [[NSMutableDictionary alloc] initWithCapacity: 30];        gDictionary = [[NSMutableDictionary alloc] initWithCapacity: 30];
           
     PRUint32 contentID;      PRUint32 contentID;
     aContent->GetContentID(&contentID);      aContent->GetContentID(&contentID);
           
    BookmarkItem* item = [mDictionary objectForKey: [NSNumber numberWithInt: contentID]];    BookmarkItem* item = [gDictionary objectForKey: [NSNumber numberWithInt: contentID]];
     if (item)      if (item)
         return item;          return item;
     else {      else {
         // Create an item.          // Create an item.
         item = [[[BookmarkItem alloc] init] autorelease]; // The dictionary retains us.          item = [[[BookmarkItem alloc] init] autorelease]; // The dictionary retains us.
         [item setContentNode: aContent];          [item setContentNode: aContent];
        [mDictionary setObject: item forKey: [NSNumber numberWithInt: contentID]];        [gDictionary setObject: item forKey: [NSNumber numberWithInt: contentID]];
     }      }
     return item;      return item;
 }  }
   
BookmarksService*NS_IMETHODIMP
BookmarksService::AddObserver(nsIDocumentObserver* aObserver)BookmarksService::AttributeChanged(nsIDocument *aDocument,
                               nsIContent*  aContent,
                               PRInt32      aNameSpaceID,
                               nsIAtom*     aAttribute,
                               PRInt32      aModType,    
                               PRInt32      aHint)
 {
     return NS_OK;
 }
 
 NS_IMETHODIMP
 BookmarksService::ContentAppended(nsIDocument *aDocument,
                              nsIContent* aContainer,
                              PRInt32     aNewIndexInContainer)
 {
     return NS_OK;
 }
 
 NS_IMETHODIMP
 BookmarksService::ContentInserted(nsIDocument *aDocument,
                              nsIContent* aContainer,
                              nsIContent* aChild,
                              PRInt32 aIndexInContainer)
 {
     return NS_OK;
 }
                              
 NS_IMETHODIMP
 BookmarksService::ContentRemoved(nsIDocument *aDocument,
                             nsIContent* aContainer,
                             nsIContent* aChild,
                             PRInt32 aIndexInContainer)
 {
     return NS_OK;
 }
 
 NS_IMPL_ISUPPORTS1(BookmarksService, nsIDocumentObserver)
 
 void
 BookmarksService::AddObserver()
 {  {
     gRefCnt++;      gRefCnt++;
     if (gRefCnt == 1) {      if (gRefCnt == 1) {
         gSingleton = new BookmarksService();  
         gBookmarkAtom = NS_NewAtom("bookmark");          gBookmarkAtom = NS_NewAtom("bookmark");
         gFolderAtom = NS_NewAtom("folder");          gFolderAtom = NS_NewAtom("folder");
         gNameAtom = NS_NewAtom("name");          gNameAtom = NS_NewAtom("name");
         gHrefAtom = NS_NewAtom("href");          gHrefAtom = NS_NewAtom("href");
           
           nsCOMPtr<nsIFile> profileDir;
           NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR, getter_AddRefs(profileDir));
           profileDir->Append("bookmarks.xml");
       
           nsXPIDLCString bookmarksFileURL;
           NS_GetURLSpecFromFile(profileDir, getter_Copies(bookmarksFileURL));
           
           nsCOMPtr<nsIURI> uri;
           NS_NewURI(getter_AddRefs(uri), bookmarksFileURL.get());
       
           nsCOMPtr<nsIXBLService> xblService(do_GetService("@mozilla.org/xbl;1"));    
           xblService->FetchSyncXMLDocument(uri, &gBookmarks); // The addref is here.
           
           nsCOMPtr<nsIContent> rootNode;
           GetRootContent(getter_AddRefs(rootNode));
           StripWhitespaceNodes(rootNode);
     }      }
           
    return gSingleton;    gBookmarks->AddObserver(this);
 }  }
   
 void  void
BookmarksService::RemoveObserver(nsIDocumentObserver* aObserver)BookmarksService::RemoveObserver()
 {  {
     if (gRefCnt == 0)      if (gRefCnt == 0)
         return;          return;
         
     gBookmarks->RemoveObserver(this);
     
     gRefCnt--;      gRefCnt--;
     if (gRefCnt == 0) {      if (gRefCnt == 0) {
        delete gSingleton;        NS_IF_RELEASE(gBookmarks);
         NS_RELEASE(gBookmarkAtom);          NS_RELEASE(gBookmarkAtom);
         NS_RELEASE(gFolderAtom);          NS_RELEASE(gFolderAtom);
         NS_RELEASE(gNameAtom);          NS_RELEASE(gNameAtom);
         NS_RELEASE(gHrefAtom);          NS_RELEASE(gHrefAtom);
           [gDictionary release];
     }      }
 }  }
   

Removed from v.1.3  
changed lines
  Added in v.1.4


FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>