Diff for /chimera/BookmarksService.mm between versions 1.18 and 1.19

version 1.18, 2002/04/12 08:45:17 version 1.19, 2002/04/14 08:43:03
Line 91 Line 91
   
 -(IBAction)addBookmark:(id)aSender  -(IBAction)addBookmark:(id)aSender
 {  {
    if (!mBookmarks)  [self addBookmark: aSender useSelection: YES];
        return;}
    
    nsCOMPtr<nsIContent> content;-(void)addBookmark:(id)aSender useSelection:(BOOL)aUseSel
 {
   if (!mBookmarks)
     return;
 
   nsCOMPtr<nsIContent> content;
   if (aUseSel) {
     int index = [mOutlineView selectedRow];      int index = [mOutlineView selectedRow];
   
     if (index >= 0) {      if (index >= 0) {
        BookmarkItem* item = [mOutlineView itemAtRow: index];      BookmarkItem* item = [mOutlineView itemAtRow: index];
        if ([mOutlineView isExpandable: item])       if ([mOutlineView isExpandable: item])
            content = [item contentNode];        content = [item contentNode];
     }      }
      }
    if (!content)
        mBookmarks->GetRootContent(getter_AddRefs(content));  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([[[mBrowserWindowController getMyBrowserView] 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)); 
        nsCAutoString spec; 
        url->GetSpec(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);  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([[[mBrowserWindowController getMyBrowserView] 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));
     nsCAutoString spec;
     url->GetSpec(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));
 
   nsCOMPtr<nsIContent> childContent(do_QueryInterface(elt));
   mBookmarks->BookmarkAdded(content, childContent);
 }  }
   
 -(IBAction)deleteBookmark: (id)aSender  -(IBAction)deleteBookmark: (id)aSender
Line 160 Line 168
     nsCOMPtr<nsIDOMElement> child(do_QueryInterface(content));      nsCOMPtr<nsIDOMElement> child(do_QueryInterface(content));
     nsCOMPtr<nsIDOMNode> parent;      nsCOMPtr<nsIDOMNode> parent;
     child->GetParentNode(getter_AddRefs(parent));      child->GetParentNode(getter_AddRefs(parent));
       nsCOMPtr<nsIContent> parentContent(do_QueryInterface(parent));
     nsCOMPtr<nsIDOMNode> dummy;      nsCOMPtr<nsIDOMNode> dummy;
     parent->RemoveChild(child, getter_AddRefs(dummy));      parent->RemoveChild(child, getter_AddRefs(dummy));
    nsCOMPtr<nsIContent> parentContent(do_QueryInterface(parent));    mBookmarks->BookmarkRemoved(parentContent, content);
    mBookmarks->NotifyObservers(parentContent, PR_TRUE); 
           
     int total = [mOutlineView numberOfRows];      int total = [mOutlineView numberOfRows];
     if (index == total)      if (index == total)
Line 364  PRUint32 BookmarksService::gRefCnt = 0; Line 372  PRUint32 BookmarksService::gRefCnt = 0;
 nsIDocument* BookmarksService::gBookmarks = nsnull;  nsIDocument* BookmarksService::gBookmarks = nsnull;
 NSMutableDictionary* BookmarksService::gDictionary = nil;  NSMutableDictionary* BookmarksService::gDictionary = nil;
 MainController* BookmarksService::gMainController = nil;  MainController* BookmarksService::gMainController = nil;
   NSMenu* BookmarksService::gBookmarksMenu = 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 412  BookmarksService::GetWrapperFor(nsIConte Line 421  BookmarksService::GetWrapperFor(nsIConte
     return item;      return item;
 }  }
   
   NSMenu*
   BookmarksService::LocateMenu(nsIContent* aContent)
   {
     nsCOMPtr<nsIContent> parent;
     aContent->GetParent(*getter_AddRefs(parent));
     if (!parent)
       return BookmarksService::gBookmarksMenu;
   
     NSMenu* parentMenu = LocateMenu(parent);
     
     PRUint32 contentID;
     aContent->GetContentID(&contentID);
   
     NSMenuItem* childMenu = [parentMenu itemWithTag: contentID];
     return [childMenu menu];
   }
   
 void  void
BookmarksService::NotifyObservers(nsIContent* aContainer, PRBool aReloadChildren)BookmarksService::BookmarkAdded(nsIContent* aContainer, nsIContent* aChild)
 {  {
    if (!gInstances)  if (!gInstances || !gDictionary)
        return;    return;
    
    PRInt32 count = gInstances->Count();  PRInt32 count = gInstances->Count();
    for (PRInt32 i = 0; i < count; i++) {  for (PRInt32 i = 0; i < count; i++) {
        BookmarksService* instance = (BookmarksService*)gInstances->ElementAt(i);    BookmarksService* instance = (BookmarksService*)gInstances->ElementAt(i);
        instance->NotifyObserver(aContainer, aReloadChildren);
     if (instance->mDataSource) {
       // We're a tree view.
       nsCOMPtr<nsIContent> parent;
       aContainer->GetParent(*getter_AddRefs(parent));
 
       BookmarkItem* item = nil;
       if (parent)
         // We're not the root.
         item = GetWrapperFor(aContainer);
 
       [(instance->mDataSource) reloadDataForItem: item reloadChildren: YES];
     }      }
        else {
    FlushBookmarks();      // We're the menu.
       PRInt32 index = -1;
       aChild->IndexOf(aContainer, index);
       NSMenu* menu = LocateMenu(aContainer);
       AddMenuBookmark(menu, aContainer, aChild, index);
     }
   }
   
   FlushBookmarks();  
 }  }
   
   
 void  void
BookmarksService::NotifyObserver(nsIContent* aContainer, PRBool aReloadChildren)BookmarksService::BookmarkChanged(nsIContent* aItem)
 {  {
    if (!gDictionary)  if (!gInstances || !gDictionary)
        return;    return;
 
   PRInt32 count = gInstances->Count();
   for (PRInt32 i = 0; i < count; i++) {
     BookmarksService* instance = (BookmarksService*)gInstances->ElementAt(i);
         
    nsCOMPtr<nsIContent> parent;    if (instance->mDataSource) {
    aContainer->GetParent(*getter_AddRefs(parent));      BookmarkItem* item = GetWrapperFor(aItem);
          [(instance->mDataSource) reloadDataForItem: item reloadChildren: NO];
    BookmarkItem* item = nil;    }
    if (parent)  }
 
   FlushBookmarks();  
 }
 
 void
 BookmarksService::BookmarkRemoved(nsIContent* aContainer, nsIContent* aChild)
 {
   if (!gInstances)
     return;
 
   PRInt32 count = gInstances->Count();
   for (PRInt32 i = 0; i < count; i++) {
     BookmarksService* instance = (BookmarksService*)gInstances->ElementAt(i);
 
     if (instance->mDataSource) {
       // We're a tree view.
       nsCOMPtr<nsIContent> parent;
       aContainer->GetParent(*getter_AddRefs(parent));
 
       BookmarkItem* item = nil;
       if (parent)
         // We're not the root.          // We're not the root.
         item = GetWrapperFor(aContainer);          item = GetWrapperFor(aContainer);
    
    [mDataSource reloadDataForItem: item reloadChildren: aReloadChildren];      [(instance->mDataSource) reloadDataForItem: item reloadChildren: YES];
     }
     else {
       // We're the menu.
       NSMenu* menu = LocateMenu(aContainer);
       PRUint32 contentID;
       aChild->GetContentID(&contentID);
       NSMenuItem* childItem = [menu itemWithTag: contentID];
       [menu removeItem: childItem];
     }
   }
 
   FlushBookmarks(); 
 }  }
   
 void  void
Line 519  BookmarksService::ConstructBookmarksMenu Line 600  BookmarksService::ConstructBookmarksMenu
     if (!content) {      if (!content) {
         GetRootContent(getter_AddRefs(content));          GetRootContent(getter_AddRefs(content));
         GetWrapperFor(content);          GetWrapperFor(content);
           gBookmarksMenu = aMenu;
     }      }
           
     // Now walk our children, and for folders also recur into them.      // Now walk our children, and for folders also recur into them.
Line 526  BookmarksService::ConstructBookmarksMenu Line 608  BookmarksService::ConstructBookmarksMenu
     content->ChildCount(childCount);      content->ChildCount(childCount);
           
     for (PRInt32 i = 0; i < childCount; i++) {      for (PRInt32 i = 0; i < childCount; i++) {
        nsCOMPtr<nsIContent> child;      nsCOMPtr<nsIContent> child;
        content->ChildAt(i, *getter_AddRefs(child));      content->ChildAt(i, *getter_AddRefs(child));
              AddMenuBookmark(aMenu, content, child, -1);
        // 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::AddMenuBookmark(NSMenu* aMenu, nsIContent* aParent, nsIContent* aChild, PRInt32 aIndex)
   {
     nsAutoString name;
     aChild->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(aChild);
   
     if (aIndex == -1)
       [aMenu addItem: menuItem];
     else
       [aMenu insertItem: menuItem atIndex: aIndex];
     
     nsCOMPtr<nsIAtom> tagName;
     aChild->GetTag(*getter_AddRefs(tagName));
   
     if (tagName == gFolderAtom) {
       NSMenu* menu = [[[NSMenu alloc] initWithTitle: title] autorelease];
       [aMenu setSubmenu: menu forItem: menuItem];
       [menu setAutoenablesItems: NO];
       ConstructBookmarksMenu(menu, aChild);
     }
     else {
       [menuItem setTarget: gMainController];
       [menuItem setAction: @selector(openMenuBookmark:)];
     }
   
     PRUint32 contentID;
     aChild->GetContentID(&contentID);
     [menuItem setTag: contentID];
   }
   
 void   void 
 BookmarksService::OpenMenuBookmark(BrowserWindowController* aController, id aMenuItem)  BookmarksService::OpenMenuBookmark(BrowserWindowController* aController, id aMenuItem)
 {  {

Removed from v.1.18  
changed lines
  Added in v.1.19


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