Annotation of chimera/BookmarksService.mm, revision 1.32

1.16      hyatt       1: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
                      2: /* ***** BEGIN LICENSE BLOCK *****
                      3:  * Version: NPL 1.1/GPL 2.0/LGPL 2.1
1.1       hyatt       4:  *
1.16      hyatt       5:  * The contents of this file are subject to the Netscape Public License
                      6:  * Version 1.1 (the "License"); you may not use this file except in
                      7:  * compliance with the License. You may obtain a copy of the License at
                      8:  * http://www.mozilla.org/NPL/
1.1       hyatt       9:  *
1.16      hyatt      10:  * Software distributed under the License is distributed on an "AS IS" basis,
                     11:  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
                     12:  * for the specific language governing rights and limitations under the
                     13:  * License.
                     14:  *
                     15:  * The Original Code is mozilla.org code.
                     16:  *
                     17:  * The Initial Developer of the Original Code is 
                     18:  * Netscape Communications Corporation.
                     19:  * Portions created by the Initial Developer are Copyright (C) 2002
                     20:  * the Initial Developer. All Rights Reserved.
                     21:  *
                     22:  * Contributor(s):
                     23:  *
                     24:  * Alternatively, the contents of this file may be used under the terms of
                     25:  * either the GNU General Public License Version 2 or later (the "GPL"), or 
                     26:  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
                     27:  * in which case the provisions of the GPL or the LGPL are applicable instead
                     28:  * of those above. If you wish to allow use of your version of this file only
                     29:  * under the terms of either the GPL or the LGPL, and not to allow others to
                     30:  * use your version of this file under the terms of the NPL, indicate your
                     31:  * decision by deleting the provisions above and replace them with the notice
                     32:  * and other provisions required by the GPL or the LGPL. If you do not delete
                     33:  * the provisions above, a recipient may use your version of this file under
                     34:  * the terms of any one of the NPL, the GPL or the LGPL.
                     35:  *
                     36:  * ***** END LICENSE BLOCK ***** */
1.1       hyatt      37: 
                     38: #import "NSBrowserView.h"
                     39: #include "BookmarksService.h"
                     40: #include "nsIDocument.h"
                     41: #include "nsIContent.h"
                     42: #include "nsIAtom.h"
                     43: #include "nsITextContent.h"
1.5       hyatt      44: #include "nsIDOMWindow.h"
                     45: #include "nsIDOMHTMLDocument.h"
1.1       hyatt      46: #include "nsIDOMElement.h"
                     47: #include "nsString.h"
                     48: #include "nsIFile.h"
                     49: #include "nsAppDirectoryServiceDefs.h"
                     50: #include "nsIXMLHttpRequest.h"
1.6       hyatt      51: #include "nsIDOMSerializer.h"
1.1       hyatt      52: #include "nsNetUtil.h"
                     53: #include "nsINamespaceManager.h"
                     54: #include "nsIXBLService.h"
1.5       hyatt      55: #include "nsIWebBrowser.h"
1.1       hyatt      56: 
                     57: @implementation BookmarksDataSource
                     58: 
                     59: -(id) init
                     60: {
1.30      macserv    61:     if ( (self = [super init]) ) {
                     62:         mBookmarks = nsnull;
                     63:         mCachedHref = nil;
                     64:     }
1.1       hyatt      65:     return self;
                     66: }
                     67: 
                     68: -(void) dealloc
                     69: {
1.21      hyatt      70:   [super dealloc];
1.5       hyatt      71: }
                     72: 
                     73: -(void) windowClosing
                     74: {
1.21      hyatt      75:   if (mBookmarks) {
                     76:     mBookmarks->RemoveObserver();
                     77:     delete mBookmarks;
                     78:   }
1.1       hyatt      79: }
                     80: 
                     81: -(void) ensureBookmarks
                     82: {
                     83:     if (mBookmarks)
                     84:         return;
                     85:     
1.5       hyatt      86:     mBookmarks = new BookmarksService(self);
1.4       hyatt      87:     mBookmarks->AddObserver();
1.1       hyatt      88:     
                     89:     [mOutlineView setTarget: self];
                     90:     [mOutlineView setDoubleAction: @selector(openBookmark:)];
1.27      hyatt      91:     [mOutlineView setDeleteAction: @selector(deleteBookmarks:)];
1.1       hyatt      92:     [mOutlineView reloadData];
                     93: }
                     94: 
1.2       hyatt      95: -(IBAction)addBookmark:(id)aSender
                     96: {
1.24      hyatt      97:   [self addBookmark: aSender useSelection: YES isFolder: NO];
1.19      hyatt      98: }
                     99: 
1.24      hyatt     100: -(IBAction)addFolder:(id)aSender
                    101: {
                    102:   [self addBookmark: aSender useSelection: YES isFolder: YES];
                    103: }
                    104: 
                    105: -(void)addBookmark:(id)aSender useSelection:(BOOL)aUseSel isFolder:(BOOL)aIsFolder
1.19      hyatt     106: {
                    107:   if (!mBookmarks)
                    108:     return;
                    109: 
1.28      hyatt     110:   // We use the selected item to determine the parent only if aUseSel is YES.
1.26      hyatt     111:   BookmarkItem* item = nil;
1.28      hyatt     112:   if (aUseSel && ([mOutlineView numberOfSelectedRows] == 1)) {
                    113:     // There is only one selected row.  If it is a folder, use it as our parent.
                    114:     // Otherwise, use our parent,
1.2       hyatt     115:     int index = [mOutlineView selectedRow];
1.28      hyatt     116:     item = [mOutlineView itemAtRow: index];
                    117:     if (![mOutlineView isExpandable: item]) {
                    118:       // We can't be used as the parent.  Try our parent.
                    119:       nsIContent* content = [item contentNode];
                    120:       nsCOMPtr<nsIContent> parentContent;
                    121:       content->GetParent(*getter_AddRefs(parentContent));
                    122:       nsCOMPtr<nsIContent> root;
                    123:       mBookmarks->GetRootContent(getter_AddRefs(root));
                    124:       
                    125:       // The root has no item, so we don't need to do a lookup unless we
                    126:       // aren't the root.
                    127:       if (parentContent != root) {
                    128:         PRUint32 contentID;
                    129:         parentContent->GetContentID(&contentID);
                    130:         item = [(BookmarksService::gDictionary) objectForKey: [NSNumber numberWithInt: contentID]];
                    131:       }
1.2       hyatt     132:     }
1.19      hyatt     133:   }
                    134: 
                    135:   nsCOMPtr<nsIDOMDocument> domDoc(do_QueryInterface(mBookmarks->gBookmarks));
1.20      hyatt     136:   
1.19      hyatt     137:   // Fetch the title of the current page and the URL.
1.28      hyatt     138:   nsAutoString title, href;
1.24      hyatt     139:   if (!aIsFolder) {
1.28      hyatt     140:     BookmarksService::GetTitleAndHrefForBrowserView([[mBrowserWindowController getMyBrowserView] getBrowserView],
                    141:                                                     title, href);
1.19      hyatt     142: 
1.24      hyatt     143:     mCachedHref = [NSString stringWithCharacters: href.get() length: nsCRT::strlen(href.get())];
                    144:     [mCachedHref retain];
                    145:   }
                    146:   else {
                    147:     mCachedHref = nil;
                    148:     title = NS_LITERAL_STRING("New Folder");
                    149:   }
1.20      hyatt     150:   
                    151:   NSTextField* textField = [mBrowserWindowController getAddBookmarkTitle];
                    152:   [textField setStringValue: [NSString stringWithCharacters: title.get() length: nsCRT::strlen(title.get())]];
                    153: 
                    154:   [mBrowserWindowController cacheBookmarkDS: self];
1.26      hyatt     155: 
1.28      hyatt     156:   // Show/hide the bookmark all tabs checkbox as appropriate.
                    157:   NSTabView* tabView = [mBrowserWindowController getTabBrowser];
                    158:   id checkbox = [mBrowserWindowController getAddBookmarkCheckbox];
                    159:   BOOL hasSuperview = [checkbox superview] != nil;
                    160:   if (aIsFolder && hasSuperview) {
                    161:     // Just don't show it at all.
                    162:     [checkbox removeFromSuperview];
                    163:     [checkbox retain];
                    164:   }
                    165:   else if (!aIsFolder && !hasSuperview) {
                    166:     // Put it back in.
                    167:     [[[mBrowserWindowController getAddBookmarkSheetWindow] contentView] addSubview: checkbox];
                    168:     [checkbox autorelease];
                    169:   }
                    170: 
                    171:   // Enable the bookmark all tabs checkbox if appropriate.
                    172:   if (!aIsFolder)
                    173:     [[mBrowserWindowController getAddBookmarkCheckbox] setEnabled: ([tabView numberOfTabViewItems] > 1)];
                    174:   
1.26      hyatt     175:   // Build up the folder list.
                    176:   NSPopUpButton* popup = [mBrowserWindowController getAddBookmarkFolder];
                    177:   BookmarksService::ConstructAddBookmarkFolderList(popup, item);
1.20      hyatt     178:   
                    179:   [NSApp beginSheet: [mBrowserWindowController getAddBookmarkSheetWindow]
                    180:      modalForWindow: [mBrowserWindowController window]
                    181:       modalDelegate: nil //self
                    182:      didEndSelector: nil //@selector(sheetDidEnd:)
                    183:         contextInfo: nil];
                    184: }
                    185: 
                    186: -(void)endAddBookmark: (int)aCode
                    187: {
                    188:   if (aCode == 0)
                    189:     return;
1.28      hyatt     190: 
                    191:   BOOL isGroup = NO;
                    192:   id checkbox = [mBrowserWindowController getAddBookmarkCheckbox];
                    193:   if (([checkbox superview] != nil) && [checkbox isEnabled] && ([checkbox state] == NSOnState)) {
                    194:     mCachedHref = nil;
                    195:     isGroup = YES;
                    196:   }
1.20      hyatt     197:   
                    198:   const char* titleC = [[[mBrowserWindowController getAddBookmarkTitle] stringValue] cString];
                    199:   nsAutoString title; title.AssignWithConversion(titleC);
                    200: 
1.24      hyatt     201:   nsAutoString tagName;
                    202:   if (mCachedHref)
                    203:     tagName = NS_LITERAL_STRING("bookmark");
                    204:   else
                    205:     tagName = NS_LITERAL_STRING("folder");
                    206:   
1.20      hyatt     207:   nsCOMPtr<nsIDOMDocument> domDoc(do_QueryInterface(mBookmarks->gBookmarks));
                    208:   nsCOMPtr<nsIDOMElement> elt;
1.24      hyatt     209:   domDoc->CreateElementNS(NS_LITERAL_STRING("http://chimera.mozdev.org/bookmarks/"),
                    210:                           tagName,
1.20      hyatt     211:                           getter_AddRefs(elt));
1.19      hyatt     212: 
                    213:   elt->SetAttribute(NS_LITERAL_STRING("name"), title);
1.24      hyatt     214: 
                    215:   if (mCachedHref) {
                    216:     nsAutoString href; href.AssignWithConversion([mCachedHref cString]);
                    217:     [mCachedHref release];
                    218:     elt->SetAttribute(NS_LITERAL_STRING("href"), href);
                    219:   }
1.26      hyatt     220: 
1.28      hyatt     221:   if (isGroup) {
                    222:     // We have to iterate over each tab and create content nodes using the
                    223:     // title/href of all the pages.  They are inserted underneath the parent.
                    224:     elt->SetAttribute(NS_LITERAL_STRING("group"), NS_LITERAL_STRING("true"));
                    225:     id tabBrowser = [mBrowserWindowController getTabBrowser];
                    226:     int count = [tabBrowser numberOfTabViewItems];
                    227:     for (int i = 0; i < count; i++) {
                    228:       id browserView = [[[tabBrowser tabViewItemAtIndex: i] view] getBrowserView];
                    229:       nsAutoString title, href;
                    230:       BookmarksService::GetTitleAndHrefForBrowserView(browserView, title, href);
                    231:       nsCOMPtr<nsIDOMElement> childElt;
                    232:       domDoc->CreateElementNS(NS_LITERAL_STRING("http://chimera.mozdev.org/bookmarks/"),
                    233:                               NS_LITERAL_STRING("bookmark"),
                    234:                               getter_AddRefs(childElt));
                    235:       childElt->SetAttribute(NS_LITERAL_STRING("name"), title);
                    236:       childElt->SetAttribute(NS_LITERAL_STRING("href"), href);
                    237:       nsCOMPtr<nsIDOMNode> dummy;
                    238:       elt->AppendChild(childElt, getter_AddRefs(dummy));
                    239:     }
                    240:   }
                    241:   
1.26      hyatt     242:   // Figure out the parent element.
                    243:   nsCOMPtr<nsIDOMElement> parentElt;
                    244:   nsCOMPtr<nsIContent> parentContent;
                    245:   NSPopUpButton* popup = [mBrowserWindowController getAddBookmarkFolder];
                    246:   NSMenuItem* selectedItem = [popup selectedItem];
                    247:   int tag = [selectedItem tag];
                    248:   if (tag == -1) {
                    249:     mBookmarks->GetRootContent(getter_AddRefs(parentContent));
                    250:     parentElt = do_QueryInterface(parentContent);
                    251:   }
                    252:   else {
                    253:     BookmarkItem* item = [(BookmarksService::gDictionary) objectForKey: [NSNumber numberWithInt: tag]];
                    254:     // Get the content node.
                    255:     parentContent = [item contentNode];
                    256:     parentElt = do_QueryInterface(parentContent);
                    257:   }
1.20      hyatt     258:   
1.19      hyatt     259:   nsCOMPtr<nsIDOMNode> dummy;
1.26      hyatt     260:   parentElt->AppendChild(elt, getter_AddRefs(dummy));
1.5       hyatt     261: 
1.19      hyatt     262:   nsCOMPtr<nsIContent> childContent(do_QueryInterface(elt));
1.26      hyatt     263:   mBookmarks->BookmarkAdded(parentContent, childContent);
1.2       hyatt     264: }
                    265: 
1.25      hyatt     266: -(IBAction)deleteBookmarks: (id)aSender
1.2       hyatt     267: {
1.25      hyatt     268:   if (!mBookmarks)
                    269:     return;
                    270: 
                    271:   int index = [mOutlineView selectedRow];
                    272:   if (index == -1)
                    273:     return;
                    274:   if ([mOutlineView numberOfSelectedRows] == 1) {
1.6       hyatt     275:     BookmarkItem* item = [mOutlineView itemAtRow: index];
1.25      hyatt     276:     [self deleteBookmark: item];
1.6       hyatt     277:     int total = [mOutlineView numberOfRows];
                    278:     if (index == total)
1.25      hyatt     279:       index--;
1.6       hyatt     280:     [mOutlineView selectRow: index byExtendingSelection: NO];
1.25      hyatt     281:   }
                    282:   else {
                    283:     NSMutableArray* itemsToDelete = [[[NSMutableArray alloc] init] autorelease];
                    284:     NSEnumerator* selRows = [mOutlineView selectedRowEnumerator];
                    285:     for (NSNumber* currIndex = [selRows nextObject];
                    286:          currIndex != nil;
                    287:          currIndex = [selRows nextObject]) {
                    288:       index = [currIndex intValue];
                    289:       BookmarkItem* item = [mOutlineView itemAtRow: index];
                    290:       [itemsToDelete addObject: item];
                    291:     }
                    292: 
                    293:     int count = [itemsToDelete count];
                    294:     for (int i = 0; i < count; i++) {
                    295:       BookmarkItem* item = [itemsToDelete objectAtIndex: i];
                    296:       [self deleteBookmark: item]; 
                    297:     }
                    298:   }
                    299: }  
                    300: 
                    301: -(void)deleteBookmark:(id)aItem
                    302: {
                    303:   nsCOMPtr<nsIContent> content = [aItem contentNode];
                    304:   nsCOMPtr<nsIDOMElement> child(do_QueryInterface(content));
                    305:   if (child == BookmarksService::gToolbarRoot)
                    306:     return; // Don't allow the personal toolbar to be deleted.
                    307:   
                    308:   nsCOMPtr<nsIDOMNode> parent;
                    309:   child->GetParentNode(getter_AddRefs(parent));
                    310:   nsCOMPtr<nsIContent> parentContent(do_QueryInterface(parent));
                    311:   nsCOMPtr<nsIDOMNode> dummy;
                    312:   parent->RemoveChild(child, getter_AddRefs(dummy));
                    313:   mBookmarks->BookmarkRemoved(parentContent, content);
1.2       hyatt     314: }
                    315: 
1.1       hyatt     316: -(IBAction)openBookmark: (id)aSender
                    317: {
1.29      hyatt     318:   int index = [mOutlineView selectedRow];
                    319:   if (index == -1)
                    320:     return;
                    321: 
                    322:   id item = [mOutlineView itemAtRow: index];
                    323:   if (!item)
                    324:     return;
                    325: 
                    326:   nsIContent* content = [item contentNode];
                    327:   nsCOMPtr<nsIDOMElement> elt(do_QueryInterface(content));
                    328:   nsAutoString group;
                    329:   elt->GetAttribute(NS_LITERAL_STRING("group"), group);
                    330:   if (!group.IsEmpty())
                    331:     mBookmarks->OpenBookmarkGroup([mBrowserWindowController getTabBrowser], elt);
                    332:   else if ([mOutlineView isExpandable: item]) {
                    333:     if ([mOutlineView isItemExpanded: item])
                    334:       [mOutlineView collapseItem: item];
                    335:     else
                    336:       [mOutlineView expandItem: item];
                    337:   }
                    338:   else {
                    339:     nsAutoString href;
                    340:     content->GetAttr(kNameSpaceID_None, BookmarksService::gHrefAtom, href);
                    341:     if (!href.IsEmpty()) {
                    342:       nsCAutoString cstr; cstr.AssignWithConversion(href);
                    343:       NSString* url = [NSString stringWithCString: cstr.get()];
                    344:       [[[mBrowserWindowController getMyBrowserView] getBrowserView] loadURI:[NSURL URLWithString: url] flags:      NSLoadFlagsNone];
                    345:       // Focus and activate our content area.
                    346:       [[[mBrowserWindowController getMyBrowserView] getBrowserView] setActive: YES];
1.1       hyatt     347:     }
1.29      hyatt     348:   }
1.1       hyatt     349: }
                    350: 
1.9       hyatt     351: - (BOOL)outlineView:(NSOutlineView *)outlineView shouldEditTableColumn:(NSTableColumn *)tableColumn item:(id)item
                    352: {
1.10      hyatt     353:     return NO;
1.9       hyatt     354: }
                    355: 
1.1       hyatt     356: - (id)outlineView:(NSOutlineView *)outlineView child:(int)index ofItem:(id)item
                    357: {
                    358:     if (!mBookmarks)
                    359:         return nil;
                    360:        
                    361:     nsCOMPtr<nsIContent> content;
                    362:     if (!item)
                    363:         mBookmarks->GetRootContent(getter_AddRefs(content));
                    364:     else
                    365:         content = [item contentNode];
                    366:     
                    367:     nsCOMPtr<nsIContent> child;
                    368:     content->ChildAt(index, *getter_AddRefs(child));
                    369:     return mBookmarks->GetWrapperFor(child);
                    370: }
                    371: 
                    372: - (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item
                    373: {
                    374:     if (!mBookmarks)
                    375:         return NO;
                    376:     
                    377:     if (!item)
                    378:         return YES; // The root node is always open.
                    379:     
                    380:     nsCOMPtr<nsIAtom> tagName;
                    381:     nsIContent* content = [item contentNode];
                    382:     content->GetTag(*getter_AddRefs(tagName));
                    383:     
                    384:     return (tagName == BookmarksService::gFolderAtom);
                    385: }
                    386: 
                    387: - (int)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item
                    388: {
                    389:     if (!mBookmarks)
                    390:         return 0;
1.2       hyatt     391:   
1.1       hyatt     392:     nsCOMPtr<nsIContent> content;
                    393:     if (!item)
                    394:         mBookmarks->GetRootContent(getter_AddRefs(content));
                    395:     else 
                    396:         content = [item contentNode];
                    397:     
                    398:     PRInt32 childCount;
                    399:     content->ChildCount(childCount);
                    400:     
                    401:     return childCount;
                    402: }
                    403: 
                    404: - (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item
                    405: {
1.13      macserv   406:     NSString           *columnName = [tableColumn identifier];
1.11      macserv   407:     NSMutableAttributedString  *cellValue = [[NSMutableAttributedString alloc] init];
1.13      macserv   408:     NSFileWrapper        *fileWrapper = [[NSFileWrapper alloc] initRegularFileWithContents:nil];
                    409:     NSTextAttachment     *textAttachment = [[NSTextAttachment alloc] initWithFileWrapper:fileWrapper];
1.11      macserv   410:     NSMutableAttributedString   *attachmentAttrString = nil;
1.13      macserv   411:     NSCell             *attachmentAttrStringCell;
1.11      macserv   412: 
1.13      macserv   413:     if ([columnName isEqualToString: @"name"]) {
                    414:         nsIContent* content = [item contentNode];
                    415:         nsAutoString nameAttr;
                    416:         content->GetAttr(kNameSpaceID_None, BookmarksService::gNameAtom, nameAttr);
                    417:         nsCAutoString cStr; cStr.AssignWithConversion(nameAttr);
                    418:         
                    419:         //Set cell's textual contents
                    420:         [cellValue replaceCharactersInRange:NSMakeRange(0, [cellValue length])
                    421:                                  withString:[NSString stringWithCString: cStr.get()]];
                    422:         
                    423:         //Create an attributed string to hold the empty attachment, then release the components.
                    424:         attachmentAttrString = [[NSMutableAttributedString attributedStringWithAttachment:textAttachment] retain];
                    425:         [textAttachment release];
                    426:         [fileWrapper release];
1.11      macserv   427: 
1.13      macserv   428:         //Get the cell of the text attachment.
                    429:         attachmentAttrStringCell = (NSCell *)[(NSTextAttachment *)[attachmentAttrString attribute:NSAttachmentAttributeName
                    430:                                                                                           atIndex:0
                    431:                                                                                    effectiveRange:nil] attachmentCell];
                    432:         //Figure out which image to add, and set the cell's image.
1.32    ! hyatt     433:         // XXXdwh Use the bookmark groups image for groups.
1.13      macserv   434:         if ( [self outlineView:outlineView isItemExpandable:item] ) {
                    435:             [attachmentAttrStringCell setImage:[NSImage imageNamed:@"folder"]];
                    436:         } else {
1.17      macserv   437:             [attachmentAttrStringCell setImage:[NSImage imageNamed:@"smallbookmark"]];
1.11      macserv   438:         }
1.13      macserv   439:         //Insert the image
                    440:         [cellValue replaceCharactersInRange:NSMakeRange(0, 0) withAttributedString:attachmentAttrString];
                    441:         
                    442:         //Tweak the baseline to vertically center the text.
                    443:         [cellValue addAttribute:NSBaselineOffsetAttributeName
                    444:                           value:[NSNumber numberWithFloat:-3.0]
                    445:                           range:NSMakeRange(0, 1)];
1.1       hyatt     446:     }
1.13      macserv   447:     return cellValue;
1.1       hyatt     448: }
                    449: 
                    450: - (void)outlineView:(NSOutlineView *)outlineView setObjectValue:(id)object forTableColumn:(NSTableColumn *)tableColumn byItem:(id)item
                    451: {
                    452: }
                    453: 
1.5       hyatt     454: - (void)reloadDataForItem:(id)item reloadChildren: (BOOL)aReloadChildren
                    455: {
                    456:     printf("Reloading?\n");
                    457:     if (!item)
                    458:         [mOutlineView reloadData];
                    459:     else if ([mOutlineView isItemExpanded: item])
                    460:         [mOutlineView reloadItem: item reloadChildren: aReloadChildren];
                    461: }
                    462: 
1.1       hyatt     463: @end
                    464: 
                    465: @implementation BookmarkItem
                    466: -(nsIContent*)contentNode
                    467: {
                    468:     return mContentNode;
                    469: }
                    470: 
                    471: -(void)setContentNode: (nsIContent*)aContentNode
                    472: {
                    473:     mContentNode = aContentNode;
                    474: }
1.7       hyatt     475: 
                    476: - (id)copyWithZone:(NSZone *)aZone
                    477: {
                    478:     BookmarkItem* copy = [[[self class] allocWithZone: aZone] init];
                    479:     [copy setContentNode: mContentNode];
                    480:     return copy;
                    481: }
                    482: 
1.1       hyatt     483: @end
                    484: 
                    485: // Helper for stripping whitespace
                    486: static void
                    487: StripWhitespaceNodes(nsIContent* aElement)
                    488: {
                    489:     PRInt32 childCount;
                    490:     aElement->ChildCount(childCount);
                    491:     for (PRInt32 i = 0; i < childCount; i++) {
                    492:         nsCOMPtr<nsIContent> child;
                    493:         aElement->ChildAt(i, *getter_AddRefs(child));
                    494:         nsCOMPtr<nsITextContent> text = do_QueryInterface(child);
                    495:         if (text) {
                    496:             PRBool isEmpty;
                    497:             text->IsOnlyWhitespace(&isEmpty);
                    498:             if (isEmpty) {
                    499:                 // This node contained nothing but whitespace.
                    500:                 // Remove it from the content model.
                    501:                 aElement->RemoveChildAt(i, PR_TRUE);
                    502:                 i--; // Decrement our count, since we just removed this child.
                    503:                 childCount--; // Also decrement our total count.
                    504:             }
                    505:         }
                    506:         else StripWhitespaceNodes(child);
                    507:     }
                    508: }
                    509: 
                    510: PRUint32 BookmarksService::gRefCnt = 0;
1.4       hyatt     511: nsIDocument* BookmarksService::gBookmarks = nsnull;
                    512: NSMutableDictionary* BookmarksService::gDictionary = nil;
1.8       hyatt     513: MainController* BookmarksService::gMainController = nil;
1.19      hyatt     514: NSMenu* BookmarksService::gBookmarksMenu = nil;
1.22      hyatt     515: nsIDOMElement* BookmarksService::gToolbarRoot = nsnull;
1.1       hyatt     516: nsIAtom* BookmarksService::gFolderAtom = nsnull;
                    517: nsIAtom* BookmarksService::gBookmarkAtom = nsnull;
                    518: nsIAtom* BookmarksService::gHrefAtom = nsnull;
                    519: nsIAtom* BookmarksService::gNameAtom = nsnull;
1.5       hyatt     520: nsVoidArray* BookmarksService::gInstances = nsnull;
1.1       hyatt     521: 
1.5       hyatt     522: BookmarksService::BookmarksService(BookmarksDataSource* aDataSource)
1.1       hyatt     523: {
1.21      hyatt     524:   mDataSource = aDataSource;
                    525:   mToolbar = nil;
                    526: }
                    527: 
                    528: BookmarksService::BookmarksService(BookmarksToolbar* aToolbar)
                    529: {
                    530:   mDataSource = nil;
                    531:   mToolbar = aToolbar;
1.1       hyatt     532: }
                    533: 
                    534: BookmarksService::~BookmarksService()
                    535: {
                    536: }
                    537: 
                    538: void
                    539: BookmarksService::GetRootContent(nsIContent** aResult)
                    540: {
                    541:     *aResult = nsnull;
1.4       hyatt     542:     if (gBookmarks) {
1.1       hyatt     543:         nsCOMPtr<nsIDOMElement> elt;
1.4       hyatt     544:         nsCOMPtr<nsIDOMDocument> domDoc(do_QueryInterface(gBookmarks));
1.1       hyatt     545:         domDoc->GetDocumentElement(getter_AddRefs(elt));
                    546:         elt->QueryInterface(NS_GET_IID(nsIContent), (void**)aResult); // Addref happens here.
                    547:     }
                    548: }
                    549: 
                    550: BookmarkItem*
                    551: BookmarksService::GetWrapperFor(nsIContent* aContent)
                    552: {
1.4       hyatt     553:     if (!gDictionary)
                    554:         gDictionary = [[NSMutableDictionary alloc] initWithCapacity: 30];
1.1       hyatt     555:     
                    556:     PRUint32 contentID;
                    557:     aContent->GetContentID(&contentID);
                    558:     
1.4       hyatt     559:     BookmarkItem* item = [gDictionary objectForKey: [NSNumber numberWithInt: contentID]];
1.1       hyatt     560:     if (item)
                    561:         return item;
                    562:     else {
                    563:         // Create an item.
                    564:         item = [[[BookmarkItem alloc] init] autorelease]; // The dictionary retains us.
                    565:         [item setContentNode: aContent];
1.4       hyatt     566:         [gDictionary setObject: item forKey: [NSNumber numberWithInt: contentID]];
1.1       hyatt     567:     }
                    568:     return item;
                    569: }
                    570: 
1.19      hyatt     571: NSMenu*
                    572: BookmarksService::LocateMenu(nsIContent* aContent)
                    573: {
                    574:   nsCOMPtr<nsIContent> parent;
                    575:   aContent->GetParent(*getter_AddRefs(parent));
1.23      hyatt     576:   if (!parent) {
1.19      hyatt     577:     return BookmarksService::gBookmarksMenu;
1.23      hyatt     578:   }
                    579:   
1.19      hyatt     580:   NSMenu* parentMenu = LocateMenu(parent);
                    581:   
                    582:   PRUint32 contentID;
                    583:   aContent->GetContentID(&contentID);
                    584: 
                    585:   NSMenuItem* childMenu = [parentMenu itemWithTag: contentID];
1.23      hyatt     586:   return [childMenu submenu];
1.19      hyatt     587: }
                    588: 
1.5       hyatt     589: void
1.19      hyatt     590: BookmarksService::BookmarkAdded(nsIContent* aContainer, nsIContent* aChild)
1.4       hyatt     591: {
1.19      hyatt     592:   if (!gInstances || !gDictionary)
                    593:     return;
                    594: 
                    595:   PRInt32 count = gInstances->Count();
                    596:   for (PRInt32 i = 0; i < count; i++) {
                    597:     BookmarksService* instance = (BookmarksService*)gInstances->ElementAt(i);
                    598: 
                    599:     if (instance->mDataSource) {
                    600:       // We're a tree view.
                    601:       nsCOMPtr<nsIContent> parent;
                    602:       aContainer->GetParent(*getter_AddRefs(parent));
                    603: 
                    604:       BookmarkItem* item = nil;
                    605:       if (parent)
                    606:         // We're not the root.
                    607:         item = GetWrapperFor(aContainer);
                    608: 
                    609:       [(instance->mDataSource) reloadDataForItem: item reloadChildren: YES];
                    610:     }
1.23      hyatt     611:     else if (instance->mToolbar) {
                    612:       // We're a personal toolbar.
                    613:       nsCOMPtr<nsIDOMElement> parentElt(do_QueryInterface(aContainer));
                    614:       if (parentElt == gToolbarRoot) {
                    615:         // We only care about changes that occur to the personal toolbar's immediate
                    616:         // children.
                    617:         PRInt32 index = -1;
                    618:         aContainer->IndexOf(aChild, index);
                    619:         nsCOMPtr<nsIDOMElement> elt(do_QueryInterface(aChild));
                    620:         [(instance->mToolbar) addButton: elt atIndex: index];
                    621:       }
                    622:     }
1.19      hyatt     623:     else {
                    624:       // We're the menu.
                    625:       PRInt32 index = -1;
1.23      hyatt     626:       aContainer->IndexOf(aChild, index);
1.19      hyatt     627:       NSMenu* menu = LocateMenu(aContainer);
                    628:       AddMenuBookmark(menu, aContainer, aChild, index);
1.5       hyatt     629:     }
1.19      hyatt     630:   }
                    631:   
                    632:   FlushBookmarks();  
1.4       hyatt     633: }
                    634: 
1.19      hyatt     635: void
                    636: BookmarksService::BookmarkChanged(nsIContent* aItem)
                    637: {
                    638:   if (!gInstances || !gDictionary)
                    639:     return;
                    640: 
                    641:   PRInt32 count = gInstances->Count();
                    642:   for (PRInt32 i = 0; i < count; i++) {
                    643:     BookmarksService* instance = (BookmarksService*)gInstances->ElementAt(i);
                    644:    
                    645:     if (instance->mDataSource) {
                    646:       BookmarkItem* item = GetWrapperFor(aItem);
                    647:       [(instance->mDataSource) reloadDataForItem: item reloadChildren: NO];
                    648:     }
                    649:   }
                    650: 
                    651:   FlushBookmarks();  
                    652: }
1.4       hyatt     653: 
1.5       hyatt     654: void
1.19      hyatt     655: BookmarksService::BookmarkRemoved(nsIContent* aContainer, nsIContent* aChild)
1.4       hyatt     656: {
1.19      hyatt     657:   if (!gInstances)
                    658:     return;
                    659: 
                    660:   PRInt32 count = gInstances->Count();
                    661:   for (PRInt32 i = 0; i < count; i++) {
                    662:     BookmarksService* instance = (BookmarksService*)gInstances->ElementAt(i);
                    663: 
                    664:     if (instance->mDataSource) {
                    665:       // We're a tree view.
                    666:       nsCOMPtr<nsIContent> parent;
                    667:       aContainer->GetParent(*getter_AddRefs(parent));
                    668: 
                    669:       BookmarkItem* item = nil;
                    670:       if (parent)
1.5       hyatt     671:         // We're not the root.
                    672:         item = GetWrapperFor(aContainer);
1.19      hyatt     673: 
                    674:       [(instance->mDataSource) reloadDataForItem: item reloadChildren: YES];
                    675:     }
1.23      hyatt     676:     else if (instance->mToolbar) {
                    677:       // We're a personal toolbar.
                    678:       nsCOMPtr<nsIDOMElement> parentElt(do_QueryInterface(aContainer));
                    679:       if (parentElt == gToolbarRoot) {
                    680:         // We only care about changes that occur to the personal toolbar's immediate
                    681:         // children.
                    682:         nsCOMPtr<nsIDOMElement> childElt(do_QueryInterface(aChild));
                    683:         [(instance->mToolbar) removeButton: childElt];
                    684:       }
                    685:     }    
1.19      hyatt     686:     else {
                    687:       // We're the menu.
                    688:       NSMenu* menu = LocateMenu(aContainer);
                    689:       PRUint32 contentID;
                    690:       aChild->GetContentID(&contentID);
                    691:       NSMenuItem* childItem = [menu itemWithTag: contentID];
                    692:       [menu removeItem: childItem];
                    693:     }
                    694:   }
                    695: 
                    696:   FlushBookmarks(); 
1.4       hyatt     697: }
                    698: 
                    699: void
                    700: BookmarksService::AddObserver()
1.1       hyatt     701: {
                    702:     gRefCnt++;
                    703:     if (gRefCnt == 1) {
                    704:         gBookmarkAtom = NS_NewAtom("bookmark");
                    705:         gFolderAtom = NS_NewAtom("folder");
                    706:         gNameAtom = NS_NewAtom("name");
                    707:         gHrefAtom = NS_NewAtom("href");
1.5       hyatt     708:         gInstances = new nsVoidArray();
1.8       hyatt     709:                 
1.4       hyatt     710:         nsCOMPtr<nsIFile> profileDir;
                    711:         NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR, getter_AddRefs(profileDir));
                    712:         profileDir->Append("bookmarks.xml");
                    713:     
1.15      hyatt     714:         nsCAutoString bookmarksFileURL;
                    715:         NS_GetURLSpecFromFile(profileDir, bookmarksFileURL);
1.4       hyatt     716:         
                    717:         nsCOMPtr<nsIURI> uri;
                    718:         NS_NewURI(getter_AddRefs(uri), bookmarksFileURL.get());
                    719:     
                    720:         nsCOMPtr<nsIXBLService> xblService(do_GetService("@mozilla.org/xbl;1"));    
                    721:         xblService->FetchSyncXMLDocument(uri, &gBookmarks); // The addref is here.
                    722:         
                    723:         nsCOMPtr<nsIContent> rootNode;
                    724:         GetRootContent(getter_AddRefs(rootNode));
                    725:         StripWhitespaceNodes(rootNode);
1.1       hyatt     726:     }
                    727:     
1.5       hyatt     728:     gInstances->AppendElement(this);
1.1       hyatt     729: }
                    730: 
                    731: void
1.4       hyatt     732: BookmarksService::RemoveObserver()
1.1       hyatt     733: {
                    734:     if (gRefCnt == 0)
                    735:         return;
1.4       hyatt     736:  
1.5       hyatt     737:     gInstances->RemoveElement(this);
                    738:      
1.1       hyatt     739:     gRefCnt--;
                    740:     if (gRefCnt == 0) {
1.4       hyatt     741:         NS_IF_RELEASE(gBookmarks);
1.1       hyatt     742:         NS_RELEASE(gBookmarkAtom);
                    743:         NS_RELEASE(gFolderAtom);
                    744:         NS_RELEASE(gNameAtom);
                    745:         NS_RELEASE(gHrefAtom);
1.4       hyatt     746:         [gDictionary release];
1.1       hyatt     747:     }
                    748: }
                    749: 
1.6       hyatt     750: void
                    751: BookmarksService::FlushBookmarks()
                    752: {
                    753:     nsCOMPtr<nsIFile> bookmarksFile;
                    754:     NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR, getter_AddRefs(bookmarksFile));
                    755:     bookmarksFile->Append("bookmarks.xml");
                    756: 
                    757:     nsCOMPtr<nsIOutputStream> outputStream;
                    758:     NS_NewLocalFileOutputStream(getter_AddRefs(outputStream), bookmarksFile);
                    759: 
                    760:     nsCOMPtr<nsIDOMDocument> domDoc(do_QueryInterface(gBookmarks));
                    761:     
                    762:     nsCOMPtr<nsIDOMSerializer> domSerializer(do_CreateInstance(NS_XMLSERIALIZER_CONTRACTID));
                    763:     domSerializer->SerializeToStream(domDoc, outputStream, nsnull);
1.21      hyatt     764: }
                    765: 
1.22      hyatt     766: void BookmarksService::EnsureToolbarRoot()
1.21      hyatt     767: {
1.22      hyatt     768:   if (gToolbarRoot)
                    769:     return;
                    770: 
1.21      hyatt     771:   nsCOMPtr<nsIDOMDocument> domDoc(do_QueryInterface(gBookmarks));
1.22      hyatt     772:   nsCOMPtr<nsIDOMElement> rootElt;
                    773:   domDoc->GetDocumentElement(getter_AddRefs(rootElt));
1.21      hyatt     774:   
1.22      hyatt     775:   nsCOMPtr<nsIDOMNode> child;
                    776:   rootElt->GetFirstChild(getter_AddRefs(child));
                    777:   nsAutoString typeValue;
                    778:   while (child) {
                    779:     nsCOMPtr<nsIDOMElement> childElt(do_QueryInterface(child));
                    780:     if (childElt) {
                    781:       childElt->GetAttribute(NS_LITERAL_STRING("type"), typeValue);
                    782:       if (typeValue.Equals(NS_LITERAL_STRING("toolbar")))
                    783:         gToolbarRoot = childElt;
                    784:     }
                    785:     
                    786:     nsCOMPtr<nsIDOMNode> temp;
                    787:     child->GetNextSibling(getter_AddRefs(temp));
                    788:     child = temp;
                    789:   }
                    790: 
                    791:   if (!gToolbarRoot) {
                    792:     printf("Repairing personal toolbar.\n");
                    793:     nsCOMPtr<nsIDOMElement> elt;
                    794:     domDoc->CreateElementNS(NS_LITERAL_STRING("http://chimera.mozdev.org/bookmarks/"),
                    795:                             NS_LITERAL_STRING("folder"),
                    796:                             getter_AddRefs(elt));
                    797: 
                    798:     elt->SetAttribute(NS_LITERAL_STRING("name"), NS_LITERAL_STRING("Toolbar Bookmarks"));
                    799:     elt->SetAttribute(NS_LITERAL_STRING("type"), NS_LITERAL_STRING("toolbar"));
                    800: 
                    801:     nsCOMPtr<nsIDOMNode> dummy;
                    802:     rootElt->AppendChild(elt, getter_AddRefs(dummy));
                    803:     gToolbarRoot = elt;
                    804:   }
1.26      hyatt     805: }
                    806: 
                    807: static
                    808: void RecursiveAddBookmarkConstruct(NSPopUpButton* aPopup, NSMenu* aMenu, int aTagToMatch)
                    809: {
                    810:   // Get the menu item children.
                    811:   NSArray* children = [aMenu itemArray];
                    812:   int startPosition = 0;
                    813:   if (aMenu == BookmarksService::gBookmarksMenu)
                    814:     startPosition = 3;
                    815: 
                    816:   int count = [children count];
                    817:   for (int i = startPosition; i < count; i++) {
                    818:     NSMenuItem* menuItem = [children objectAtIndex: i];
                    819:     NSMenu* submenu = [menuItem submenu];
                    820:     if (submenu) {
                    821:       // This is a folder.  Add it to our list and then recur.
                    822:       [aPopup addItemWithTitle: [menuItem title]];
                    823:       NSMenuItem* lastItem = [aPopup lastItem];
                    824:       if ([menuItem tag] == aTagToMatch)
                    825:         [aPopup selectItem: lastItem];
                    826:       
                    827:       [lastItem setTag: [menuItem tag]];
                    828:       RecursiveAddBookmarkConstruct(aPopup, submenu, aTagToMatch);
                    829:     }
                    830:   }
                    831: }
                    832: 
                    833: void
                    834: BookmarksService::ConstructAddBookmarkFolderList(NSPopUpButton* aPopup, BookmarkItem* aItem)
                    835: {
                    836:   [aPopup removeAllItems];
                    837:   [aPopup addItemWithTitle: [gBookmarksMenu title]];
                    838:   NSMenuItem* lastItem = [aPopup lastItem];
                    839:   [lastItem setTag: -1];
                    840:   int tag = -1;
                    841:   if (aItem) {
                    842:     nsIContent* content = [aItem contentNode];
                    843:     PRUint32 utag;
                    844:     content->GetContentID(&utag);
                    845:     tag = (int)utag;
                    846:   }
                    847:   RecursiveAddBookmarkConstruct(aPopup, gBookmarksMenu, tag);
1.28      hyatt     848: }
                    849: 
                    850: void
                    851: BookmarksService::GetTitleAndHrefForBrowserView(id aBrowserView, nsString& aTitle, nsString& aHref)
                    852: {
                    853:   nsCOMPtr<nsIWebBrowser> webBrowser = getter_AddRefs([aBrowserView getWebBrowser]);
                    854:   nsCOMPtr<nsIDOMWindow> window;
                    855:   webBrowser->GetContentDOMWindow(getter_AddRefs(window));
                    856:   nsCOMPtr<nsIDOMDocument> htmlDoc;
                    857:   window->GetDocument(getter_AddRefs(htmlDoc));
                    858:   nsCOMPtr<nsIDocument> pageDoc(do_QueryInterface(htmlDoc));
                    859: 
                    860:   if (pageDoc) {
                    861:     nsCOMPtr<nsIURI> url;
                    862:     pageDoc->GetDocumentURL(getter_AddRefs(url));
                    863:     nsCAutoString spec;
                    864:     url->GetSpec(spec);
                    865:     aHref.AssignWithConversion(spec.get());
                    866:   }
                    867: 
                    868:   nsCOMPtr<nsIDOMHTMLDocument> htmlDocument(do_QueryInterface(htmlDoc));
                    869:   if (htmlDocument)
                    870:     htmlDocument->GetTitle(aTitle);
                    871:   if (aTitle.IsEmpty())
                    872:     aTitle = aHref;  
1.7       hyatt     873: }
                    874: 
                    875: void
                    876: BookmarksService::ConstructBookmarksMenu(NSMenu* aMenu, nsIContent* aContent)
                    877: {
                    878:     nsCOMPtr<nsIContent> content = aContent;
                    879:     if (!content) {
                    880:         GetRootContent(getter_AddRefs(content));
1.8       hyatt     881:         GetWrapperFor(content);
1.19      hyatt     882:         gBookmarksMenu = aMenu;
1.7       hyatt     883:     }
                    884:     
                    885:     // Now walk our children, and for folders also recur into them.
                    886:     PRInt32 childCount;
                    887:     content->ChildCount(childCount);
                    888:     
                    889:     for (PRInt32 i = 0; i < childCount; i++) {
1.19      hyatt     890:       nsCOMPtr<nsIContent> child;
                    891:       content->ChildAt(i, *getter_AddRefs(child));
                    892:       AddMenuBookmark(aMenu, content, child, -1);
1.7       hyatt     893:     }
1.19      hyatt     894: }
                    895: 
                    896: void
                    897: BookmarksService::AddMenuBookmark(NSMenu* aMenu, nsIContent* aParent, nsIContent* aChild, PRInt32 aIndex)
                    898: {
                    899:   nsAutoString name;
                    900:   aChild->GetAttr(kNameSpaceID_None, gNameAtom, name);
                    901:   nsCAutoString nameCStr; nameCStr.AssignWithConversion(name);
                    902:   NSString* title = [NSString stringWithCString: nameCStr.get()];
                    903: 
                    904:   // Create a menu or menu item for the child.
                    905:   NSMenuItem* menuItem = [[[NSMenuItem alloc] initWithTitle: title action: NULL keyEquivalent: @""] autorelease];
                    906:   GetWrapperFor(aChild);
                    907: 
                    908:   if (aIndex == -1)
                    909:     [aMenu addItem: menuItem];
                    910:   else
                    911:     [aMenu insertItem: menuItem atIndex: aIndex];
                    912:   
                    913:   nsCOMPtr<nsIAtom> tagName;
                    914:   aChild->GetTag(*getter_AddRefs(tagName));
                    915: 
1.31      hyatt     916:   nsAutoString group;
                    917:   nsCOMPtr<nsIDOMElement> elt(do_QueryInterface(aChild));
                    918:   elt->GetAttribute(NS_LITERAL_STRING("group"), group);
                    919:   
                    920:   if (group.IsEmpty() && tagName == gFolderAtom) {
1.19      hyatt     921:     NSMenu* menu = [[[NSMenu alloc] initWithTitle: title] autorelease];
                    922:     [aMenu setSubmenu: menu forItem: menuItem];
                    923:     [menu setAutoenablesItems: NO];
                    924:     ConstructBookmarksMenu(menu, aChild);
                    925:   }
                    926:   else {
                    927:     [menuItem setTarget: gMainController];
                    928:     [menuItem setAction: @selector(openMenuBookmark:)];
                    929:   }
                    930: 
                    931:   PRUint32 contentID;
                    932:   aChild->GetContentID(&contentID);
                    933:   [menuItem setTag: contentID];
1.6       hyatt     934: }
1.8       hyatt     935: 
                    936: void 
                    937: BookmarksService::OpenMenuBookmark(BrowserWindowController* aController, id aMenuItem)
                    938: {
1.31      hyatt     939:   // Get the corresponding bookmark item.
                    940:   BookmarkItem* item = [gDictionary objectForKey: [NSNumber numberWithInt: [aMenuItem tag]]];
                    941: 
                    942:   // Get the content node.
                    943:   nsIContent* content = [item contentNode];
                    944:   nsCOMPtr<nsIDOMElement> elt(do_QueryInterface(content));
                    945:   nsAutoString group;
                    946:   elt->GetAttribute(NS_LITERAL_STRING("group"), group);
                    947:   if (!group.IsEmpty())
                    948:     return OpenBookmarkGroup([aController getTabBrowser], elt);
                    949:   
                    950:   // Get the href attribute.  This is the URL we want to load.
                    951:   nsAutoString href;
                    952:   content->GetAttr(kNameSpaceID_None, gHrefAtom, href);
                    953:   nsCAutoString cref; cref.AssignWithConversion(href);
                    954:   if (cref.IsEmpty())
                    955:     return;
                    956: 
                    957:   NSString* url = [NSString stringWithCString: cref.get()];
                    958: 
                    959:   // Now load the URL in the window.
                    960:   [aController loadURL:[NSURL URLWithString: url]];
                    961: 
                    962:   // Focus and activate our content area.
                    963:   [[[aController getMyBrowserView] getBrowserView] setActive: YES];
1.29      hyatt     964: }
                    965: 
                    966: void
                    967: BookmarksService::OpenBookmarkGroup(id aTabView, nsIDOMElement* aFolder)
                    968: {
                    969:   // We might conceivably have to make new tabs in order to load all
                    970:   // the items in the group.
                    971:   int currentIndex = 0;
                    972:   int total = [aTabView numberOfTabViewItems];
                    973:   nsCOMPtr<nsIDOMNode> child;
                    974:   aFolder->GetFirstChild(getter_AddRefs(child));
                    975:   while (child) {
                    976:     nsCOMPtr<nsIDOMElement> elt(do_QueryInterface(child));
                    977:     if (elt) {
                    978:       nsAutoString href;
                    979:       elt->GetAttribute(NS_LITERAL_STRING("href"), href);
                    980:       if (!href.IsEmpty()) {
                    981:         nsCAutoString cref; cref.AssignWithConversion(href);
                    982:         NSString* url = [NSString stringWithCString: cref.get()];
                    983:         NSTabViewItem* tabViewItem = nil;
                    984:         if (currentIndex >= total) {
                    985:           // We need to make a new tab.
                    986:           tabViewItem = [[[NSTabViewItem alloc] initWithIdentifier: nil] autorelease];
                    987:           MyBrowserView* newView = [[[MyBrowserView alloc] initWithTab: tabViewItem andWindow: [aTabView window]] autorelease];
                    988:           [tabViewItem setLabel: @"Untitled"];
                    989:           [tabViewItem setView: newView];
                    990:           [aTabView addTabViewItem: tabViewItem];
                    991:         }
                    992:         else
                    993:           tabViewItem = [aTabView tabViewItemAtIndex: currentIndex];
                    994: 
                    995:         [[[tabViewItem view] getBrowserView] loadURI:[NSURL URLWithString: url]
                    996:                                                flags: NSLoadFlagsNone];
                    997:       }
                    998:     }
                    999:     
                   1000:     nsCOMPtr<nsIDOMNode> temp = child;
                   1001:     temp->GetNextSibling(getter_AddRefs(child));
                   1002:     currentIndex++;
                   1003:   }
                   1004: 
1.31      hyatt    1005:   // Select and activate the first tab.
                   1006:   [aTabView selectTabViewItemAtIndex: 0];
                   1007:   [[[[aTabView tabViewItemAtIndex: 0] view] getBrowserView] setActive: YES];
1.29      hyatt    1008: }

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