Diff for /chimera/BookmarksService.mm between versions 1.1 and 1.9

version 1.1, 2002/02/09 00:00:47 version 1.9, 2002/02/10 02:45:56
Line 13 Line 13
 #include "nsIContent.h"  #include "nsIContent.h"
 #include "nsIAtom.h"  #include "nsIAtom.h"
 #include "nsITextContent.h"  #include "nsITextContent.h"
#include "nsIDOMDocument.h"#include "nsIDOMWindow.h"
 #include "nsIDOMHTMLDocument.h"
 #include "nsIDOMElement.h"  #include "nsIDOMElement.h"
 #include "nsString.h"  #include "nsString.h"
 #include "nsIFile.h"  #include "nsIFile.h"
 #include "nsAppDirectoryServiceDefs.h"  #include "nsAppDirectoryServiceDefs.h"
 #include "nsIXMLHttpRequest.h"  #include "nsIXMLHttpRequest.h"
   #include "nsIDOMSerializer.h"
 #include "nsNetUtil.h"  #include "nsNetUtil.h"
 #include "nsINamespaceManager.h"  #include "nsINamespaceManager.h"
 #include "nsIXBLService.h"  #include "nsIXBLService.h"
   #include "nsIWebBrowser.h"
   
 @implementation BookmarksDataSource  @implementation BookmarksDataSource
   
Line 35 Line 38
 -(void) dealloc  -(void) dealloc
 {  {
     [super dealloc];      [super dealloc];
    if (mBookmarks)}
        BookmarksService::RemoveObserver(nsnull);
 -(void) windowClosing
 {
     if (mBookmarks) {
         mBookmarks->RemoveObserver();
         delete mBookmarks;
     }
 }  }
   
 -(void) ensureBookmarks  -(void) ensureBookmarks
Line 44 Line 53
     if (mBookmarks)      if (mBookmarks)
         return;          return;
           
    mBookmarks = BookmarksService::AddObserver(nsnull);    mBookmarks = new BookmarksService(self);
     mBookmarks->AddObserver();
           
     [mOutlineView setTarget: self];      [mOutlineView setTarget: self];
     [mOutlineView setDoubleAction: @selector(openBookmark:)];      [mOutlineView setDoubleAction: @selector(openBookmark:)];
     [mOutlineView reloadData];      [mOutlineView reloadData];
 }  }
   
-(IBAction)openBookmark: (id)aSender-(IBAction)addBookmark:(id)aSender
 {
     if (!mBookmarks)
         return;
     
     nsCOMPtr<nsIContent> content;
     int index = [mOutlineView selectedRow];
    
     if (index >= 0) {
         BookmarkItem* item = [mOutlineView itemAtRow: index];
         if ([mOutlineView isExpandable: item]) 
             content = [item contentNode];
     }
     
     if (!content)
         mBookmarks->GetRootContent(getter_AddRefs(content));
         
     nsCOMPtr<nsIDOMDocument> domDoc(do_QueryInterface(mBookmarks->gBookmarks));
     nsCOMPtr<nsIDOMElement> elt;
     domDoc->CreateElementNS(NS_LITERAL_STRING("http://chimera.mozdev.org/bookmarks"), 
                             NS_LITERAL_STRING("bookmark"), 
                             getter_AddRefs(elt));
     
     // Fetch the title of the current page and the URL.
     nsCOMPtr<nsIWebBrowser> webBrowser = getter_AddRefs([[mBrowserView getBrowserView] getWebBrowser]);
     nsCOMPtr<nsIDOMWindow> window;
     webBrowser->GetContentDOMWindow(getter_AddRefs(window));
     nsCOMPtr<nsIDOMDocument> htmlDoc;
     window->GetDocument(getter_AddRefs(htmlDoc));
     nsCOMPtr<nsIDocument> pageDoc(do_QueryInterface(htmlDoc));
     
     nsAutoString href; 
     if (pageDoc) {
         nsCOMPtr<nsIURI> url;
         pageDoc->GetDocumentURL(getter_AddRefs(url));
         nsXPIDLCString spec;
         url->GetSpec(getter_Copies(spec));
         href.AssignWithConversion(spec.get());
     }
     
     nsAutoString title;
     nsCOMPtr<nsIDOMHTMLDocument> htmlDocument(do_QueryInterface(htmlDoc));
     if (htmlDocument)
         htmlDocument->GetTitle(title);
     if (title.IsEmpty())
         title = href;
         
     elt->SetAttribute(NS_LITERAL_STRING("name"), title);
     elt->SetAttribute(NS_LITERAL_STRING("href"), href);    
     
     nsCOMPtr<nsIDOMElement> parent(do_QueryInterface(content));
     nsCOMPtr<nsIDOMNode> dummy;
     parent->AppendChild(elt, getter_AddRefs(dummy));
 
     mBookmarks->NotifyObservers(content, PR_TRUE);
 }
 
 -(IBAction)deleteBookmark: (id)aSender
 {  {
    printf("Opening bookmark!\n");    if (!mBookmarks)
         return;
           
     int index = [mOutlineView selectedRow];      int index = [mOutlineView selectedRow];
     if (index == -1)      if (index == -1)
         return;          return;
           
       BookmarkItem* item = [mOutlineView itemAtRow: index];
       nsCOMPtr<nsIContent> content = [item contentNode];
       nsCOMPtr<nsIDOMElement> child(do_QueryInterface(content));
       nsCOMPtr<nsIDOMNode> parent;
       child->GetParentNode(getter_AddRefs(parent));
       nsCOMPtr<nsIDOMNode> dummy;
       parent->RemoveChild(child, getter_AddRefs(dummy));
       nsCOMPtr<nsIContent> parentContent(do_QueryInterface(parent));
       mBookmarks->NotifyObservers(parentContent, PR_TRUE);
       
       int total = [mOutlineView numberOfRows];
       if (index == total)
           index--;
           
       [mOutlineView selectRow: index byExtendingSelection: NO];
   }
   
   -(IBAction)openBookmark: (id)aSender
   {
       int index = [mOutlineView selectedRow];
       if (index == -1)
           return;
           
     id item = [mOutlineView itemAtRow: index];      id item = [mOutlineView itemAtRow: index];
     if (!item)      if (!item)
Line 81 Line 172
     }       } 
 }  }
   
   - (BOOL)outlineView:(NSOutlineView *)outlineView shouldEditTableColumn:(NSTableColumn *)tableColumn item:(id)item
   {
       if (!mBookmarks)
           return NO;
           
       return YES;
   }
   
 - (id)outlineView:(NSOutlineView *)outlineView child:(int)index ofItem:(id)item  - (id)outlineView:(NSOutlineView *)outlineView child:(int)index ofItem:(id)item
 {  {
     if (!mBookmarks)      if (!mBookmarks)
         return nil;          return nil;
                 
     printf("Begin item obtain\n");  
     nsCOMPtr<nsIContent> content;      nsCOMPtr<nsIContent> content;
     if (!item)      if (!item)
         mBookmarks->GetRootContent(getter_AddRefs(content));          mBookmarks->GetRootContent(getter_AddRefs(content));
Line 105 Line 203
           
     if (!item)      if (!item)
         return YES; // The root node is always open.          return YES; // The root node is always open.
           
     printf("Begin expandable check.\n");  
           
     nsCOMPtr<nsIAtom> tagName;      nsCOMPtr<nsIAtom> tagName;
     nsIContent* content = [item contentNode];      nsIContent* content = [item contentNode];
Line 119 Line 215
 {  {
     if (!mBookmarks)      if (!mBookmarks)
         return 0;          return 0;
   
    printf("Begin child count.\n"); 
     
     nsCOMPtr<nsIContent> content;      nsCOMPtr<nsIContent> content;
     if (!item)      if (!item)
         mBookmarks->GetRootContent(getter_AddRefs(content));          mBookmarks->GetRootContent(getter_AddRefs(content));
     else       else 
         content = [item contentNode];          content = [item contentNode];
           
     if (!content)  
         printf("OH NO!!!!!\n");  
           
     PRInt32 childCount;      PRInt32 childCount;
     content->ChildCount(childCount);      content->ChildCount(childCount);
           
     printf("Child count is %d\n", childCount);  
       
     return childCount;      return childCount;
 }  }
   
Line 144 Line 233
     if (!item)      if (!item)
         return nil;          return nil;
           
     printf("Obtaining string.\n");  
       
     NSString* columnName = [tableColumn identifier];      NSString* columnName = [tableColumn identifier];
     if (columnName)  
       printf("It has a name!\n");  
               
     if ([columnName isEqualToString: @"name"]) {      if ([columnName isEqualToString: @"name"]) {
         nsIContent* content = [item contentNode];          nsIContent* content = [item contentNode];
         nsAutoString nameAttr;          nsAutoString nameAttr;
         content->GetAttr(kNameSpaceID_None, BookmarksService::gNameAtom, nameAttr);          content->GetAttr(kNameSpaceID_None, BookmarksService::gNameAtom, nameAttr);
         nsCAutoString cStr; cStr.AssignWithConversion(nameAttr);          nsCAutoString cStr; cStr.AssignWithConversion(nameAttr);
         printf("Well? %s\n", cStr.get());  
         return [NSString stringWithCString: cStr.get()];          return [NSString stringWithCString: cStr.get()];
     }      }
           
Line 166 Line 250
 {  {
 }  }
   
   - (void)reloadDataForItem:(id)item reloadChildren: (BOOL)aReloadChildren
   {
       printf("Reloading?\n");
       if (!item)
           [mOutlineView reloadData];
       else if ([mOutlineView isItemExpanded: item])
           [mOutlineView reloadItem: item reloadChildren: aReloadChildren];
   }
   
 @end  @end
   
 @implementation BookmarkItem  @implementation BookmarkItem
Line 178 Line 271
 {  {
     mContentNode = aContentNode;      mContentNode = aContentNode;
 }  }
   
   - (id)copyWithZone:(NSZone *)aZone
   {
       BookmarkItem* copy = [[[self class] allocWithZone: aZone] init];
       [copy setContentNode: mContentNode];
       return copy;
   }
   
 @end  @end
   
 // Helper for stripping whitespace  // Helper for stripping whitespace
Line 206  StripWhitespaceNodes(nsIContent* aElemen Line 307  StripWhitespaceNodes(nsIContent* aElemen
 }  }
   
 PRUint32 BookmarksService::gRefCnt = 0;  PRUint32 BookmarksService::gRefCnt = 0;
BookmarksService* BookmarksService::gSingleton = nsnull;nsIDocument* BookmarksService::gBookmarks = nsnull;
 NSMutableDictionary* BookmarksService::gDictionary = nil;
 MainController* BookmarksService::gMainController = 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;
 nsIAtom* BookmarksService::gNameAtom = nsnull;  nsIAtom* BookmarksService::gNameAtom = nsnull;
   nsVoidArray* BookmarksService::gInstances = nsnull;
   
BookmarksService::BookmarksService()BookmarksService::BookmarksService(BookmarksDataSource* aDataSource)
 {  {
    mDictionary = nil;    mDataSource = aDataSource;
    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)); 
     
    if (!rootNode) 
        printf("OH SHIT\n"); 
         
    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 258  BookmarksService::GetRootContent(nsICont Line 340  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*void
BookmarksService::AddObserver(nsIDocumentObserver* aObserver)BookmarksService::NotifyObservers(nsIContent* aContainer, PRBool aReloadChildren)
 {
     if (!gInstances)
         return;
     
     PRInt32 count = gInstances->Count();
     for (PRInt32 i = 0; i < count; i++) {
         BookmarksService* instance = (BookmarksService*)gInstances->ElementAt(i);
         instance->NotifyObserver(aContainer, aReloadChildren);
     }
     
     FlushBookmarks();
 }
 
 
 void
 BookmarksService::NotifyObserver(nsIContent* aContainer, PRBool aReloadChildren)
 {
     if (!gDictionary)
         return;
    
     nsCOMPtr<nsIContent> parent;
     aContainer->GetParent(*getter_AddRefs(parent));
     
     BookmarkItem* item = nil;
     if (parent)
         // We're not the root.
         item = GetWrapperFor(aContainer);
     
     [mDataSource reloadDataForItem: item reloadChildren: aReloadChildren];
 }
 
 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");
           gInstances = new nsVoidArray();
                   
           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;    gInstances->AppendElement(this);
 }  }
   
 void  void
BookmarksService::RemoveObserver(nsIDocumentObserver* aObserver)BookmarksService::RemoveObserver()
 {  {
     if (gRefCnt == 0)      if (gRefCnt == 0)
         return;          return;
         
     gInstances->RemoveElement(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];
     }      }
 }  }
   
   void
   BookmarksService::FlushBookmarks()
   {
       nsCOMPtr<nsIFile> bookmarksFile;
       NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR, getter_AddRefs(bookmarksFile));
       bookmarksFile->Append("bookmarks.xml");
   
       nsCOMPtr<nsIOutputStream> outputStream;
       NS_NewLocalFileOutputStream(getter_AddRefs(outputStream), bookmarksFile);
   
       nsCOMPtr<nsIDOMDocument> domDoc(do_QueryInterface(gBookmarks));
       
       nsCOMPtr<nsIDOMSerializer> domSerializer(do_CreateInstance(NS_XMLSERIALIZER_CONTRACTID));
       domSerializer->SerializeToStream(domDoc, outputStream, nsnull);
   }
   
   void
   BookmarksService::ConstructBookmarksMenu(NSMenu* aMenu, nsIContent* aContent)
   {
       nsCOMPtr<nsIContent> content = aContent;
       if (!content) {
           GetRootContent(getter_AddRefs(content));
           GetWrapperFor(content);
       }
       
       // Now walk our children, and for folders also recur into them.
       PRInt32 childCount;
       content->ChildCount(childCount);
       
       for (PRInt32 i = 0; i < childCount; i++) {
           nsCOMPtr<nsIContent> child;
           content->ChildAt(i, *getter_AddRefs(child));
           
           // Obtain our name attribute.
           nsAutoString name;
           child->GetAttr(kNameSpaceID_None, gNameAtom, name);
           nsCAutoString nameCStr; nameCStr.AssignWithConversion(name);
           NSString* title = [NSString stringWithCString: nameCStr.get()];
                   
           // Create a menu or menu item for the child.
           NSMenuItem* menuItem = [[[NSMenuItem alloc] initWithTitle: title action: NULL keyEquivalent: @""] autorelease];
           GetWrapperFor(child);
           [aMenu addItem: menuItem];
           
           nsCOMPtr<nsIAtom> tag;
           child->GetTag(*getter_AddRefs(tag));
           
           if (tag == gFolderAtom) {
               NSMenu* menu = [[[NSMenu alloc] initWithTitle: title] autorelease];
               [aMenu setSubmenu: menu forItem: menuItem];
               [menu setAutoenablesItems: NO];
               ConstructBookmarksMenu(menu, child);
           }
           else {
               [menuItem setTarget: gMainController];
               [menuItem setAction: @selector(openMenuBookmark:)];
           }
           
           PRUint32 contentID;
           child->GetContentID(&contentID);
           [menuItem setTag: contentID];
       }
   }
   
   void 
   BookmarksService::OpenMenuBookmark(BrowserWindowController* aController, id aMenuItem)
   {
       // Get the corresponding bookmark item.
       BookmarkItem* item = [gDictionary objectForKey: [NSNumber numberWithInt: [aMenuItem tag]]];
           
       // Get the content node.
       nsIContent* content = [item contentNode];
           
       // Get the href attribute.  This is the URL we want to load.
       nsAutoString href;
       content->GetAttr(kNameSpaceID_None, gHrefAtom, href);
       nsCAutoString cref; cref.AssignWithConversion(href);
       if (cref.IsEmpty())
           return;
           
       NSString* url = [NSString stringWithCString: cref.get()];
       
       // Now load the URL in the window.
       [aController loadURL:[NSURL URLWithString: url]];
   }
   

Removed from v.1.1  
changed lines
  Added in v.1.9


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