Diff for /chimera/BookmarksService.mm between versions 1.27 and 1.31

version 1.27, 2002/04/19 08:45:57 version 1.31, 2002/04/20 01:18:54
Line 58 Line 58
   
 -(id) init  -(id) init
 {  {
    [super init];    if ( (self = [super init]) ) {
    mBookmarks = nsnull;        mBookmarks = nsnull;
    mCachedHref = nil;        mCachedHref = nil;
     }
     return self;      return self;
 }  }
   
Line 106 Line 107
   if (!mBookmarks)    if (!mBookmarks)
     return;      return;
   
     // We use the selected item to determine the parent only if aUseSel is YES.
   BookmarkItem* item = nil;    BookmarkItem* item = nil;
  if (aUseSel) {  if (aUseSel && ([mOutlineView numberOfSelectedRows] == 1)) {
     // There is only one selected row.  If it is a folder, use it as our parent.
     // Otherwise, use our parent,
     int index = [mOutlineView selectedRow];      int index = [mOutlineView selectedRow];
    item = [mOutlineView itemAtRow: index];
    if (index >= 0) {    if (![mOutlineView isExpandable: item]) {
      item = [mOutlineView itemAtRow: index];      // We can't be used as the parent.  Try our parent.
      if ([mOutlineView numberOfSelectedRows] != 1 || ![mOutlineView isExpandable: item])      nsIContent* content = [item contentNode];
        item = nil;      nsCOMPtr<nsIContent> parentContent;
       content->GetParent(*getter_AddRefs(parentContent));
       nsCOMPtr<nsIContent> root;
       mBookmarks->GetRootContent(getter_AddRefs(root));
       
       // The root has no item, so we don't need to do a lookup unless we
       // aren't the root.
       if (parentContent != root) {
         PRUint32 contentID;
         parentContent->GetContentID(&contentID);
         item = [(BookmarksService::gDictionary) objectForKey: [NSNumber numberWithInt: contentID]];
       }
     }      }
   }    }
   
   nsCOMPtr<nsIDOMDocument> domDoc(do_QueryInterface(mBookmarks->gBookmarks));    nsCOMPtr<nsIDOMDocument> domDoc(do_QueryInterface(mBookmarks->gBookmarks));
       
   // Fetch the title of the current page and the URL.    // Fetch the title of the current page and the URL.
  nsAutoString title;  nsAutoString title, href;
   if (!aIsFolder) {    if (!aIsFolder) {
    nsCOMPtr<nsIWebBrowser> webBrowser = getter_AddRefs([[[mBrowserWindowController getMyBrowserView]    BookmarksService::GetTitleAndHrefForBrowserView([[mBrowserWindowController getMyBrowserView] getBrowserView],
      getBrowserView] getWebBrowser]);                                                    title, href);
    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()); 
    } 
   
     mCachedHref = [NSString stringWithCharacters: href.get() length: nsCRT::strlen(href.get())];      mCachedHref = [NSString stringWithCharacters: href.get() length: nsCRT::strlen(href.get())];
     [mCachedHref retain];      [mCachedHref retain];
     
     nsCOMPtr<nsIDOMHTMLDocument> htmlDocument(do_QueryInterface(htmlDoc));  
     if (htmlDocument)  
       htmlDocument->GetTitle(title);  
     if (title.IsEmpty())  
       title = href;  
   }    }
   else {    else {
     mCachedHref = nil;      mCachedHref = nil;
Line 158 Line 153
   
   [mBrowserWindowController cacheBookmarkDS: self];    [mBrowserWindowController cacheBookmarkDS: self];
   
     // Show/hide the bookmark all tabs checkbox as appropriate.
     NSTabView* tabView = [mBrowserWindowController getTabBrowser];
     id checkbox = [mBrowserWindowController getAddBookmarkCheckbox];
     BOOL hasSuperview = [checkbox superview] != nil;
     if (aIsFolder && hasSuperview) {
       // Just don't show it at all.
       [checkbox removeFromSuperview];
       [checkbox retain];
     }
     else if (!aIsFolder && !hasSuperview) {
       // Put it back in.
       [[[mBrowserWindowController getAddBookmarkSheetWindow] contentView] addSubview: checkbox];
       [checkbox autorelease];
     }
   
     // Enable the bookmark all tabs checkbox if appropriate.
     if (!aIsFolder)
       [[mBrowserWindowController getAddBookmarkCheckbox] setEnabled: ([tabView numberOfTabViewItems] > 1)];
     
   // Build up the folder list.    // Build up the folder list.
   NSPopUpButton* popup = [mBrowserWindowController getAddBookmarkFolder];    NSPopUpButton* popup = [mBrowserWindowController getAddBookmarkFolder];
   BookmarksService::ConstructAddBookmarkFolderList(popup, item);    BookmarksService::ConstructAddBookmarkFolderList(popup, item);
Line 173 Line 187
 {  {
   if (aCode == 0)    if (aCode == 0)
     return;      return;
   
     BOOL isGroup = NO;
     id checkbox = [mBrowserWindowController getAddBookmarkCheckbox];
     if (([checkbox superview] != nil) && [checkbox isEnabled] && ([checkbox state] == NSOnState)) {
       mCachedHref = nil;
       isGroup = YES;
     }
       
   const char* titleC = [[[mBrowserWindowController getAddBookmarkTitle] stringValue] cString];    const char* titleC = [[[mBrowserWindowController getAddBookmarkTitle] stringValue] cString];
   nsAutoString title; title.AssignWithConversion(titleC);    nsAutoString title; title.AssignWithConversion(titleC);
Line 197 Line 218
     elt->SetAttribute(NS_LITERAL_STRING("href"), href);      elt->SetAttribute(NS_LITERAL_STRING("href"), href);
   }    }
   
     if (isGroup) {
       // We have to iterate over each tab and create content nodes using the
       // title/href of all the pages.  They are inserted underneath the parent.
       elt->SetAttribute(NS_LITERAL_STRING("group"), NS_LITERAL_STRING("true"));
       id tabBrowser = [mBrowserWindowController getTabBrowser];
       int count = [tabBrowser numberOfTabViewItems];
       for (int i = 0; i < count; i++) {
         id browserView = [[[tabBrowser tabViewItemAtIndex: i] view] getBrowserView];
         nsAutoString title, href;
         BookmarksService::GetTitleAndHrefForBrowserView(browserView, title, href);
         nsCOMPtr<nsIDOMElement> childElt;
         domDoc->CreateElementNS(NS_LITERAL_STRING("http://chimera.mozdev.org/bookmarks/"),
                                 NS_LITERAL_STRING("bookmark"),
                                 getter_AddRefs(childElt));
         childElt->SetAttribute(NS_LITERAL_STRING("name"), title);
         childElt->SetAttribute(NS_LITERAL_STRING("href"), href);
         nsCOMPtr<nsIDOMNode> dummy;
         elt->AppendChild(childElt, getter_AddRefs(dummy));
       }
     }
     
   // Figure out the parent element.    // Figure out the parent element.
   nsCOMPtr<nsIDOMElement> parentElt;    nsCOMPtr<nsIDOMElement> parentElt;
   nsCOMPtr<nsIContent> parentContent;    nsCOMPtr<nsIContent> parentContent;
Line 273 Line 315
   
 -(IBAction)openBookmark: (id)aSender  -(IBAction)openBookmark: (id)aSender
 {  {
    int index = [mOutlineView selectedRow];  int index = [mOutlineView selectedRow];
    if (index == -1)  if (index == -1)
        return;    return;
    
    id item = [mOutlineView itemAtRow: index];  id item = [mOutlineView itemAtRow: index];
    if (!item)  if (!item)
        return;    return;
        
    if ([mOutlineView isExpandable: item]) {  nsIContent* content = [item contentNode];
        if ([mOutlineView isItemExpanded: item])  nsCOMPtr<nsIDOMElement> elt(do_QueryInterface(content));
            [mOutlineView collapseItem: item];  nsAutoString group;
        else  elt->GetAttribute(NS_LITERAL_STRING("group"), group);
            [mOutlineView expandItem: item];  if (!group.IsEmpty())
     mBookmarks->OpenBookmarkGroup([mBrowserWindowController getTabBrowser], elt);
   else if ([mOutlineView isExpandable: item]) {
     if ([mOutlineView isItemExpanded: item])
       [mOutlineView collapseItem: item];
     else
       [mOutlineView expandItem: item];
   }
   else {
     nsAutoString href;
     content->GetAttr(kNameSpaceID_None, BookmarksService::gHrefAtom, href);
     if (!href.IsEmpty()) {
       nsCAutoString cstr; cstr.AssignWithConversion(href);
       NSString* url = [NSString stringWithCString: cstr.get()];
       [[[mBrowserWindowController getMyBrowserView] getBrowserView] loadURI:[NSURL URLWithString: url] flags:                   NSLoadFlagsNone];
       // Focus and activate our content area.
       [[[mBrowserWindowController getMyBrowserView] getBrowserView] setActive: YES];
     }      }
    else {  }
        nsIContent* content = [item contentNode]; 
        nsAutoString href; 
        content->GetAttr(kNameSpaceID_None, BookmarksService::gHrefAtom, href); 
        if (!href.IsEmpty()) { 
            nsCAutoString cstr; cstr.AssignWithConversion(href); 
            NSString* url = [NSString stringWithCString: cstr.get()]; 
            [[[mBrowserWindowController getMyBrowserView] getBrowserView] loadURI:[NSURL URLWithString: url] flags:NSLoadFlagsNone]; 
            // Focus and activate our content area. 
            [[[mBrowserWindowController getMyBrowserView] getBrowserView] setActive: YES]; 
        } 
    }  
 }  }
   
 - (BOOL)outlineView:(NSOutlineView *)outlineView shouldEditTableColumn:(NSTableColumn *)tableColumn item:(id)item  - (BOOL)outlineView:(NSOutlineView *)outlineView shouldEditTableColumn:(NSTableColumn *)tableColumn item:(id)item
Line 800  BookmarksService::ConstructAddBookmarkFo Line 847  BookmarksService::ConstructAddBookmarkFo
 }  }
   
 void  void
   BookmarksService::GetTitleAndHrefForBrowserView(id aBrowserView, nsString& aTitle, nsString& aHref)
   {
     nsCOMPtr<nsIWebBrowser> webBrowser = getter_AddRefs([aBrowserView getWebBrowser]);
     nsCOMPtr<nsIDOMWindow> window;
     webBrowser->GetContentDOMWindow(getter_AddRefs(window));
     nsCOMPtr<nsIDOMDocument> htmlDoc;
     window->GetDocument(getter_AddRefs(htmlDoc));
     nsCOMPtr<nsIDocument> pageDoc(do_QueryInterface(htmlDoc));
   
     if (pageDoc) {
       nsCOMPtr<nsIURI> url;
       pageDoc->GetDocumentURL(getter_AddRefs(url));
       nsCAutoString spec;
       url->GetSpec(spec);
       aHref.AssignWithConversion(spec.get());
     }
   
     nsCOMPtr<nsIDOMHTMLDocument> htmlDocument(do_QueryInterface(htmlDoc));
     if (htmlDocument)
       htmlDocument->GetTitle(aTitle);
     if (aTitle.IsEmpty())
       aTitle = aHref;  
   }
   
   void
 BookmarksService::ConstructBookmarksMenu(NSMenu* aMenu, nsIContent* aContent)  BookmarksService::ConstructBookmarksMenu(NSMenu* aMenu, nsIContent* aContent)
 {  {
     nsCOMPtr<nsIContent> content = aContent;      nsCOMPtr<nsIContent> content = aContent;
Line 840  BookmarksService::AddMenuBookmark(NSMenu Line 912  BookmarksService::AddMenuBookmark(NSMenu
   nsCOMPtr<nsIAtom> tagName;    nsCOMPtr<nsIAtom> tagName;
   aChild->GetTag(*getter_AddRefs(tagName));    aChild->GetTag(*getter_AddRefs(tagName));
   
  if (tagName == gFolderAtom) {  nsAutoString group;
   nsCOMPtr<nsIDOMElement> elt(do_QueryInterface(aChild));
   elt->GetAttribute(NS_LITERAL_STRING("group"), group);
   
   if (group.IsEmpty() && tagName == gFolderAtom) {
     NSMenu* menu = [[[NSMenu alloc] initWithTitle: title] autorelease];      NSMenu* menu = [[[NSMenu alloc] initWithTitle: title] autorelease];
     [aMenu setSubmenu: menu forItem: menuItem];      [aMenu setSubmenu: menu forItem: menuItem];
     [menu setAutoenablesItems: NO];      [menu setAutoenablesItems: NO];
Line 859  BookmarksService::AddMenuBookmark(NSMenu Line 935  BookmarksService::AddMenuBookmark(NSMenu
 void   void 
 BookmarksService::OpenMenuBookmark(BrowserWindowController* aController, id aMenuItem)  BookmarksService::OpenMenuBookmark(BrowserWindowController* aController, id aMenuItem)
 {  {
    // Get the corresponding bookmark item.  // Get the corresponding bookmark item.
    BookmarkItem* item = [gDictionary objectForKey: [NSNumber numberWithInt: [aMenuItem tag]]];  BookmarkItem* item = [gDictionary objectForKey: [NSNumber numberWithInt: [aMenuItem tag]]];
        
    // Get the content node.  // Get the content node.
    nsIContent* content = [item contentNode];  nsIContent* content = [item contentNode];
          nsCOMPtr<nsIDOMElement> elt(do_QueryInterface(content));
    // Get the href attribute.  This is the URL we want to load.  nsAutoString group;
    nsAutoString href;  elt->GetAttribute(NS_LITERAL_STRING("group"), group);
    content->GetAttr(kNameSpaceID_None, gHrefAtom, href);  if (!group.IsEmpty())
    nsCAutoString cref; cref.AssignWithConversion(href);    return OpenBookmarkGroup([aController getTabBrowser], elt);
    if (cref.IsEmpty())  
        return;  // Get the href attribute.  This is the URL we want to load.
          nsAutoString href;
    NSString* url = [NSString stringWithCString: cref.get()];  content->GetAttr(kNameSpaceID_None, gHrefAtom, href);
      nsCAutoString cref; cref.AssignWithConversion(href);
    // Now load the URL in the window.  if (cref.IsEmpty())
    [aController loadURL:[NSURL URLWithString: url]];    return;
 
   NSString* url = [NSString stringWithCString: cref.get()];
 
   // Now load the URL in the window.
   [aController loadURL:[NSURL URLWithString: url]];
 
   // Focus and activate our content area.
   [[[aController getMyBrowserView] getBrowserView] setActive: YES];
 }
 
 void
 BookmarksService::OpenBookmarkGroup(id aTabView, nsIDOMElement* aFolder)
 {
   // We might conceivably have to make new tabs in order to load all
   // the items in the group.
   int currentIndex = 0;
   int total = [aTabView numberOfTabViewItems];
   nsCOMPtr<nsIDOMNode> child;
   aFolder->GetFirstChild(getter_AddRefs(child));
   while (child) {
     nsCOMPtr<nsIDOMElement> elt(do_QueryInterface(child));
     if (elt) {
       nsAutoString href;
       elt->GetAttribute(NS_LITERAL_STRING("href"), href);
       if (!href.IsEmpty()) {
         nsCAutoString cref; cref.AssignWithConversion(href);
         NSString* url = [NSString stringWithCString: cref.get()];
         NSTabViewItem* tabViewItem = nil;
         if (currentIndex >= total) {
           // We need to make a new tab.
           tabViewItem = [[[NSTabViewItem alloc] initWithIdentifier: nil] autorelease];
           MyBrowserView* newView = [[[MyBrowserView alloc] initWithTab: tabViewItem andWindow: [aTabView window]] autorelease];
           [tabViewItem setLabel: @"Untitled"];
           [tabViewItem setView: newView];
           [aTabView addTabViewItem: tabViewItem];
         }
         else
           tabViewItem = [aTabView tabViewItemAtIndex: currentIndex];
 
         [[[tabViewItem view] getBrowserView] loadURI:[NSURL URLWithString: url]
                                                flags: NSLoadFlagsNone];
       }
     }
           
     // Focus and activate our content area.  
     [[[aController getMyBrowserView] getBrowserView] setActive: YES];  
 }  
   
       nsCOMPtr<nsIDOMNode> temp = child;
       temp->GetNextSibling(getter_AddRefs(child));
       currentIndex++;
     }
   
     // Select and activate the first tab.
     [aTabView selectTabViewItemAtIndex: 0];
     [[[[aTabView tabViewItemAtIndex: 0] view] getBrowserView] setActive: YES];
   }

Removed from v.1.27  
changed lines
  Added in v.1.31


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