Annotation of chimera/BookmarksService.mm, revision 1.14

1.1       hyatt       1: /*
                      2:  *  BookmarksService.cpp
                      3:  *  Chimera
                      4:  *
                      5:  *  Created by David Hyatt on Thu Feb 07 2002.
                      6:  *  Copyright (c) 2001 __MyCompanyName__. All rights reserved.
                      7:  *
                      8:  */
                      9: 
                     10: #import "NSBrowserView.h"
                     11: #include "BookmarksService.h"
                     12: #include "nsIDocument.h"
                     13: #include "nsIContent.h"
                     14: #include "nsIAtom.h"
                     15: #include "nsITextContent.h"
1.5       hyatt      16: #include "nsIDOMWindow.h"
                     17: #include "nsIDOMHTMLDocument.h"
1.1       hyatt      18: #include "nsIDOMElement.h"
                     19: #include "nsString.h"
                     20: #include "nsIFile.h"
                     21: #include "nsAppDirectoryServiceDefs.h"
                     22: #include "nsIXMLHttpRequest.h"
1.6       hyatt      23: #include "nsIDOMSerializer.h"
1.1       hyatt      24: #include "nsNetUtil.h"
                     25: #include "nsINamespaceManager.h"
                     26: #include "nsIXBLService.h"
1.5       hyatt      27: #include "nsIWebBrowser.h"
1.1       hyatt      28: 
                     29: @implementation BookmarksDataSource
                     30: 
                     31: -(id) init
                     32: {
                     33:     [super init];
                     34:     mBookmarks = nsnull;
                     35:     return self;
                     36: }
                     37: 
                     38: -(void) dealloc
                     39: {
                     40:     [super dealloc];
1.5       hyatt      41: }
                     42: 
                     43: -(void) windowClosing
                     44: {
                     45:     if (mBookmarks) {
1.4       hyatt      46:         mBookmarks->RemoveObserver();
1.5       hyatt      47:         delete mBookmarks;
                     48:     }
1.1       hyatt      49: }
                     50: 
                     51: -(void) ensureBookmarks
                     52: {
                     53:     if (mBookmarks)
                     54:         return;
                     55:     
1.5       hyatt      56:     mBookmarks = new BookmarksService(self);
1.4       hyatt      57:     mBookmarks->AddObserver();
1.1       hyatt      58:     
                     59:     [mOutlineView setTarget: self];
                     60:     [mOutlineView setDoubleAction: @selector(openBookmark:)];
                     61:     [mOutlineView reloadData];
                     62: }
                     63: 
1.2       hyatt      64: -(IBAction)addBookmark:(id)aSender
                     65: {
                     66:     if (!mBookmarks)
                     67:         return;
1.5       hyatt      68:     
1.2       hyatt      69:     nsCOMPtr<nsIContent> content;
                     70:     int index = [mOutlineView selectedRow];
1.5       hyatt      71:    
1.7       hyatt      72:     if (index >= 0) {
1.2       hyatt      73:         BookmarkItem* item = [mOutlineView itemAtRow: index];
1.5       hyatt      74:         if ([mOutlineView isExpandable: item]) 
1.2       hyatt      75:             content = [item contentNode];
                     76:     }
                     77:     
                     78:     if (!content)
                     79:         mBookmarks->GetRootContent(getter_AddRefs(content));
                     80:         
1.5       hyatt      81:     nsCOMPtr<nsIDOMDocument> domDoc(do_QueryInterface(mBookmarks->gBookmarks));
1.2       hyatt      82:     nsCOMPtr<nsIDOMElement> elt;
                     83:     domDoc->CreateElementNS(NS_LITERAL_STRING("http://chimera.mozdev.org/bookmarks"), 
                     84:                             NS_LITERAL_STRING("bookmark"), 
                     85:                             getter_AddRefs(elt));
                     86:     
1.5       hyatt      87:     // Fetch the title of the current page and the URL.
1.14    ! hyatt      88:     nsCOMPtr<nsIWebBrowser> webBrowser = getter_AddRefs([[[mBrowserWindowController getMyBrowserView] getBrowserView] getWebBrowser]);
1.5       hyatt      89:     nsCOMPtr<nsIDOMWindow> window;
                     90:     webBrowser->GetContentDOMWindow(getter_AddRefs(window));
                     91:     nsCOMPtr<nsIDOMDocument> htmlDoc;
                     92:     window->GetDocument(getter_AddRefs(htmlDoc));
                     93:     nsCOMPtr<nsIDocument> pageDoc(do_QueryInterface(htmlDoc));
                     94:     
                     95:     nsAutoString href; 
                     96:     if (pageDoc) {
                     97:         nsCOMPtr<nsIURI> url;
                     98:         pageDoc->GetDocumentURL(getter_AddRefs(url));
                     99:         nsXPIDLCString spec;
                    100:         url->GetSpec(getter_Copies(spec));
                    101:         href.AssignWithConversion(spec.get());
                    102:     }
                    103:     
                    104:     nsAutoString title;
                    105:     nsCOMPtr<nsIDOMHTMLDocument> htmlDocument(do_QueryInterface(htmlDoc));
                    106:     if (htmlDocument)
                    107:         htmlDocument->GetTitle(title);
                    108:     if (title.IsEmpty())
                    109:         title = href;
                    110:         
                    111:     elt->SetAttribute(NS_LITERAL_STRING("name"), title);
                    112:     elt->SetAttribute(NS_LITERAL_STRING("href"), href);    
1.2       hyatt     113:     
                    114:     nsCOMPtr<nsIDOMElement> parent(do_QueryInterface(content));
                    115:     nsCOMPtr<nsIDOMNode> dummy;
                    116:     parent->AppendChild(elt, getter_AddRefs(dummy));
1.5       hyatt     117: 
                    118:     mBookmarks->NotifyObservers(content, PR_TRUE);
1.2       hyatt     119: }
                    120: 
                    121: -(IBAction)deleteBookmark: (id)aSender
                    122: {
1.6       hyatt     123:     if (!mBookmarks)
                    124:         return;
                    125:     
                    126:     int index = [mOutlineView selectedRow];
                    127:     if (index == -1)
                    128:         return;
                    129:         
                    130:     BookmarkItem* item = [mOutlineView itemAtRow: index];
                    131:     nsCOMPtr<nsIContent> content = [item contentNode];
                    132:     nsCOMPtr<nsIDOMElement> child(do_QueryInterface(content));
                    133:     nsCOMPtr<nsIDOMNode> parent;
                    134:     child->GetParentNode(getter_AddRefs(parent));
                    135:     nsCOMPtr<nsIDOMNode> dummy;
                    136:     parent->RemoveChild(child, getter_AddRefs(dummy));
                    137:     nsCOMPtr<nsIContent> parentContent(do_QueryInterface(parent));
                    138:     mBookmarks->NotifyObservers(parentContent, PR_TRUE);
                    139:     
                    140:     int total = [mOutlineView numberOfRows];
                    141:     if (index == total)
                    142:         index--;
                    143:         
                    144:     [mOutlineView selectRow: index byExtendingSelection: NO];
1.2       hyatt     145: }
                    146: 
1.1       hyatt     147: -(IBAction)openBookmark: (id)aSender
                    148: {
                    149:     int index = [mOutlineView selectedRow];
                    150:     if (index == -1)
                    151:         return;
                    152:     
                    153:     id item = [mOutlineView itemAtRow: index];
                    154:     if (!item)
                    155:         return;
                    156:         
                    157:     if ([mOutlineView isExpandable: item]) {
                    158:         if ([mOutlineView isItemExpanded: item])
                    159:             [mOutlineView collapseItem: item];
                    160:         else
                    161:             [mOutlineView expandItem: item];
                    162:     }
                    163:     else {
                    164:         nsIContent* content = [item contentNode];
                    165:         nsAutoString href;
                    166:         content->GetAttr(kNameSpaceID_None, BookmarksService::gHrefAtom, href);
                    167:         if (!href.IsEmpty()) {
                    168:             nsCAutoString cstr; cstr.AssignWithConversion(href);
                    169:             NSString* url = [NSString stringWithCString: cstr.get()];
1.14    ! hyatt     170:             [[[mBrowserWindowController getMyBrowserView] getBrowserView] loadURI:[NSURL URLWithString: url] flags:NSLoadFlagsNone];
1.1       hyatt     171:         }
                    172:     } 
                    173: }
                    174: 
1.9       hyatt     175: - (BOOL)outlineView:(NSOutlineView *)outlineView shouldEditTableColumn:(NSTableColumn *)tableColumn item:(id)item
                    176: {
1.10      hyatt     177:     return NO;
1.9       hyatt     178: }
                    179: 
1.1       hyatt     180: - (id)outlineView:(NSOutlineView *)outlineView child:(int)index ofItem:(id)item
                    181: {
                    182:     if (!mBookmarks)
                    183:         return nil;
                    184:        
                    185:     nsCOMPtr<nsIContent> content;
                    186:     if (!item)
                    187:         mBookmarks->GetRootContent(getter_AddRefs(content));
                    188:     else
                    189:         content = [item contentNode];
                    190:     
                    191:     nsCOMPtr<nsIContent> child;
                    192:     content->ChildAt(index, *getter_AddRefs(child));
                    193:     return mBookmarks->GetWrapperFor(child);
                    194: }
                    195: 
                    196: - (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item
                    197: {
                    198:     if (!mBookmarks)
                    199:         return NO;
                    200:     
                    201:     if (!item)
                    202:         return YES; // The root node is always open.
                    203:     
                    204:     nsCOMPtr<nsIAtom> tagName;
                    205:     nsIContent* content = [item contentNode];
                    206:     content->GetTag(*getter_AddRefs(tagName));
                    207:     
                    208:     return (tagName == BookmarksService::gFolderAtom);
                    209: }
                    210: 
                    211: - (int)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item
                    212: {
                    213:     if (!mBookmarks)
                    214:         return 0;
1.2       hyatt     215:   
1.1       hyatt     216:     nsCOMPtr<nsIContent> content;
                    217:     if (!item)
                    218:         mBookmarks->GetRootContent(getter_AddRefs(content));
                    219:     else 
                    220:         content = [item contentNode];
                    221:     
                    222:     PRInt32 childCount;
                    223:     content->ChildCount(childCount);
                    224:     
                    225:     return childCount;
                    226: }
                    227: 
                    228: - (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item
                    229: {
1.13      macserv   230:     NSString                                   *columnName = [tableColumn identifier];
1.11      macserv   231:     NSMutableAttributedString  *cellValue = [[NSMutableAttributedString alloc] init];
1.13      macserv   232:     NSFileWrapper                              *fileWrapper = [[NSFileWrapper alloc] initRegularFileWithContents:nil];
                    233:     NSTextAttachment                   *textAttachment = [[NSTextAttachment alloc] initWithFileWrapper:fileWrapper];
1.11      macserv   234:     NSMutableAttributedString   *attachmentAttrString = nil;
1.13      macserv   235:     NSCell                                             *attachmentAttrStringCell;
1.11      macserv   236: 
1.13      macserv   237:     if ([columnName isEqualToString: @"name"]) {
                    238:         nsIContent* content = [item contentNode];
                    239:         nsAutoString nameAttr;
                    240:         content->GetAttr(kNameSpaceID_None, BookmarksService::gNameAtom, nameAttr);
                    241:         nsCAutoString cStr; cStr.AssignWithConversion(nameAttr);
                    242:         
                    243:         //Set cell's textual contents
                    244:         [cellValue replaceCharactersInRange:NSMakeRange(0, [cellValue length])
                    245:                                  withString:[NSString stringWithCString: cStr.get()]];
                    246:         
                    247:         //Create an attributed string to hold the empty attachment, then release the components.
                    248:         attachmentAttrString = [[NSMutableAttributedString attributedStringWithAttachment:textAttachment] retain];
                    249:         [textAttachment release];
                    250:         [fileWrapper release];
1.11      macserv   251: 
1.13      macserv   252:         //Get the cell of the text attachment.
                    253:         attachmentAttrStringCell = (NSCell *)[(NSTextAttachment *)[attachmentAttrString attribute:NSAttachmentAttributeName
                    254:                                                                                           atIndex:0
                    255:                                                                                    effectiveRange:nil] attachmentCell];
                    256:         //Figure out which image to add, and set the cell's image.
                    257:         if ( [self outlineView:outlineView isItemExpandable:item] ) {
                    258:             [attachmentAttrStringCell setImage:[NSImage imageNamed:@"folder"]];
                    259:         } else {
                    260:             [attachmentAttrStringCell setImage:[NSImage imageNamed:@"smallDocument"]];
1.11      macserv   261:         }
1.13      macserv   262:         //Insert the image
                    263:         [cellValue replaceCharactersInRange:NSMakeRange(0, 0) withAttributedString:attachmentAttrString];
                    264:         
                    265:         //Tweak the baseline to vertically center the text.
                    266:         [cellValue addAttribute:NSBaselineOffsetAttributeName
                    267:                           value:[NSNumber numberWithFloat:-3.0]
                    268:                           range:NSMakeRange(0, 1)];
1.1       hyatt     269:     }
1.13      macserv   270:     return cellValue;
1.1       hyatt     271: }
                    272: 
                    273: - (void)outlineView:(NSOutlineView *)outlineView setObjectValue:(id)object forTableColumn:(NSTableColumn *)tableColumn byItem:(id)item
                    274: {
                    275: }
                    276: 
1.5       hyatt     277: - (void)reloadDataForItem:(id)item reloadChildren: (BOOL)aReloadChildren
                    278: {
                    279:     printf("Reloading?\n");
                    280:     if (!item)
                    281:         [mOutlineView reloadData];
                    282:     else if ([mOutlineView isItemExpanded: item])
                    283:         [mOutlineView reloadItem: item reloadChildren: aReloadChildren];
                    284: }
                    285: 
1.1       hyatt     286: @end
                    287: 
                    288: @implementation BookmarkItem
                    289: -(nsIContent*)contentNode
                    290: {
                    291:     return mContentNode;
                    292: }
                    293: 
                    294: -(void)setContentNode: (nsIContent*)aContentNode
                    295: {
                    296:     mContentNode = aContentNode;
                    297: }
1.7       hyatt     298: 
                    299: - (id)copyWithZone:(NSZone *)aZone
                    300: {
                    301:     BookmarkItem* copy = [[[self class] allocWithZone: aZone] init];
                    302:     [copy setContentNode: mContentNode];
                    303:     return copy;
                    304: }
                    305: 
1.1       hyatt     306: @end
                    307: 
                    308: // Helper for stripping whitespace
                    309: static void
                    310: StripWhitespaceNodes(nsIContent* aElement)
                    311: {
                    312:     PRInt32 childCount;
                    313:     aElement->ChildCount(childCount);
                    314:     for (PRInt32 i = 0; i < childCount; i++) {
                    315:         nsCOMPtr<nsIContent> child;
                    316:         aElement->ChildAt(i, *getter_AddRefs(child));
                    317:         nsCOMPtr<nsITextContent> text = do_QueryInterface(child);
                    318:         if (text) {
                    319:             PRBool isEmpty;
                    320:             text->IsOnlyWhitespace(&isEmpty);
                    321:             if (isEmpty) {
                    322:                 // This node contained nothing but whitespace.
                    323:                 // Remove it from the content model.
                    324:                 aElement->RemoveChildAt(i, PR_TRUE);
                    325:                 i--; // Decrement our count, since we just removed this child.
                    326:                 childCount--; // Also decrement our total count.
                    327:             }
                    328:         }
                    329:         else StripWhitespaceNodes(child);
                    330:     }
                    331: }
                    332: 
                    333: PRUint32 BookmarksService::gRefCnt = 0;
1.4       hyatt     334: nsIDocument* BookmarksService::gBookmarks = nsnull;
                    335: NSMutableDictionary* BookmarksService::gDictionary = nil;
1.8       hyatt     336: MainController* BookmarksService::gMainController = nil;
1.1       hyatt     337: nsIAtom* BookmarksService::gFolderAtom = nsnull;
                    338: nsIAtom* BookmarksService::gBookmarkAtom = nsnull;
                    339: nsIAtom* BookmarksService::gHrefAtom = nsnull;
                    340: nsIAtom* BookmarksService::gNameAtom = nsnull;
1.5       hyatt     341: nsVoidArray* BookmarksService::gInstances = nsnull;
1.1       hyatt     342: 
1.5       hyatt     343: BookmarksService::BookmarksService(BookmarksDataSource* aDataSource)
1.1       hyatt     344: {
1.5       hyatt     345:     mDataSource = aDataSource;
1.1       hyatt     346: }
                    347: 
                    348: BookmarksService::~BookmarksService()
                    349: {
                    350: }
                    351: 
                    352: void
                    353: BookmarksService::GetRootContent(nsIContent** aResult)
                    354: {
                    355:     *aResult = nsnull;
1.4       hyatt     356:     if (gBookmarks) {
1.1       hyatt     357:         nsCOMPtr<nsIDOMElement> elt;
1.4       hyatt     358:         nsCOMPtr<nsIDOMDocument> domDoc(do_QueryInterface(gBookmarks));
1.1       hyatt     359:         domDoc->GetDocumentElement(getter_AddRefs(elt));
                    360:         elt->QueryInterface(NS_GET_IID(nsIContent), (void**)aResult); // Addref happens here.
                    361:     }
                    362: }
                    363: 
                    364: BookmarkItem*
                    365: BookmarksService::GetWrapperFor(nsIContent* aContent)
                    366: {
1.4       hyatt     367:     if (!gDictionary)
                    368:         gDictionary = [[NSMutableDictionary alloc] initWithCapacity: 30];
1.1       hyatt     369:     
                    370:     PRUint32 contentID;
                    371:     aContent->GetContentID(&contentID);
                    372:     
1.4       hyatt     373:     BookmarkItem* item = [gDictionary objectForKey: [NSNumber numberWithInt: contentID]];
1.1       hyatt     374:     if (item)
                    375:         return item;
                    376:     else {
                    377:         // Create an item.
                    378:         item = [[[BookmarkItem alloc] init] autorelease]; // The dictionary retains us.
                    379:         [item setContentNode: aContent];
1.4       hyatt     380:         [gDictionary setObject: item forKey: [NSNumber numberWithInt: contentID]];
1.1       hyatt     381:     }
                    382:     return item;
                    383: }
                    384: 
1.5       hyatt     385: void
                    386: BookmarksService::NotifyObservers(nsIContent* aContainer, PRBool aReloadChildren)
1.4       hyatt     387: {
1.5       hyatt     388:     if (!gInstances)
                    389:         return;
                    390:     
                    391:     PRInt32 count = gInstances->Count();
                    392:     for (PRInt32 i = 0; i < count; i++) {
                    393:         BookmarksService* instance = (BookmarksService*)gInstances->ElementAt(i);
                    394:         instance->NotifyObserver(aContainer, aReloadChildren);
                    395:     }
1.6       hyatt     396:     
                    397:     FlushBookmarks();
1.4       hyatt     398: }
                    399: 
                    400: 
1.5       hyatt     401: void
                    402: BookmarksService::NotifyObserver(nsIContent* aContainer, PRBool aReloadChildren)
1.4       hyatt     403: {
1.5       hyatt     404:     if (!gDictionary)
                    405:         return;
1.6       hyatt     406:    
1.5       hyatt     407:     nsCOMPtr<nsIContent> parent;
                    408:     aContainer->GetParent(*getter_AddRefs(parent));
                    409:     
                    410:     BookmarkItem* item = nil;
                    411:     if (parent)
                    412:         // We're not the root.
                    413:         item = GetWrapperFor(aContainer);
                    414:     
                    415:     [mDataSource reloadDataForItem: item reloadChildren: aReloadChildren];
1.4       hyatt     416: }
                    417: 
                    418: void
                    419: BookmarksService::AddObserver()
1.1       hyatt     420: {
                    421:     gRefCnt++;
                    422:     if (gRefCnt == 1) {
                    423:         gBookmarkAtom = NS_NewAtom("bookmark");
                    424:         gFolderAtom = NS_NewAtom("folder");
                    425:         gNameAtom = NS_NewAtom("name");
                    426:         gHrefAtom = NS_NewAtom("href");
1.5       hyatt     427:         gInstances = new nsVoidArray();
1.8       hyatt     428:                 
1.4       hyatt     429:         nsCOMPtr<nsIFile> profileDir;
                    430:         NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR, getter_AddRefs(profileDir));
                    431:         profileDir->Append("bookmarks.xml");
                    432:     
                    433:         nsXPIDLCString bookmarksFileURL;
                    434:         NS_GetURLSpecFromFile(profileDir, getter_Copies(bookmarksFileURL));
                    435:         
                    436:         nsCOMPtr<nsIURI> uri;
                    437:         NS_NewURI(getter_AddRefs(uri), bookmarksFileURL.get());
                    438:     
                    439:         nsCOMPtr<nsIXBLService> xblService(do_GetService("@mozilla.org/xbl;1"));    
                    440:         xblService->FetchSyncXMLDocument(uri, &gBookmarks); // The addref is here.
                    441:         
                    442:         nsCOMPtr<nsIContent> rootNode;
                    443:         GetRootContent(getter_AddRefs(rootNode));
                    444:         StripWhitespaceNodes(rootNode);
1.1       hyatt     445:     }
                    446:     
1.5       hyatt     447:     gInstances->AppendElement(this);
1.1       hyatt     448: }
                    449: 
                    450: void
1.4       hyatt     451: BookmarksService::RemoveObserver()
1.1       hyatt     452: {
                    453:     if (gRefCnt == 0)
                    454:         return;
1.4       hyatt     455:  
1.5       hyatt     456:     gInstances->RemoveElement(this);
                    457:      
1.1       hyatt     458:     gRefCnt--;
                    459:     if (gRefCnt == 0) {
1.4       hyatt     460:         NS_IF_RELEASE(gBookmarks);
1.1       hyatt     461:         NS_RELEASE(gBookmarkAtom);
                    462:         NS_RELEASE(gFolderAtom);
                    463:         NS_RELEASE(gNameAtom);
                    464:         NS_RELEASE(gHrefAtom);
1.4       hyatt     465:         [gDictionary release];
1.1       hyatt     466:     }
                    467: }
                    468: 
1.6       hyatt     469: void
                    470: BookmarksService::FlushBookmarks()
                    471: {
                    472:     nsCOMPtr<nsIFile> bookmarksFile;
                    473:     NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR, getter_AddRefs(bookmarksFile));
                    474:     bookmarksFile->Append("bookmarks.xml");
                    475: 
                    476:     nsCOMPtr<nsIOutputStream> outputStream;
                    477:     NS_NewLocalFileOutputStream(getter_AddRefs(outputStream), bookmarksFile);
                    478: 
                    479:     nsCOMPtr<nsIDOMDocument> domDoc(do_QueryInterface(gBookmarks));
                    480:     
                    481:     nsCOMPtr<nsIDOMSerializer> domSerializer(do_CreateInstance(NS_XMLSERIALIZER_CONTRACTID));
                    482:     domSerializer->SerializeToStream(domDoc, outputStream, nsnull);
1.7       hyatt     483: }
                    484: 
                    485: void
                    486: BookmarksService::ConstructBookmarksMenu(NSMenu* aMenu, nsIContent* aContent)
                    487: {
                    488:     nsCOMPtr<nsIContent> content = aContent;
                    489:     if (!content) {
                    490:         GetRootContent(getter_AddRefs(content));
1.8       hyatt     491:         GetWrapperFor(content);
1.7       hyatt     492:     }
                    493:     
                    494:     // Now walk our children, and for folders also recur into them.
                    495:     PRInt32 childCount;
                    496:     content->ChildCount(childCount);
                    497:     
                    498:     for (PRInt32 i = 0; i < childCount; i++) {
                    499:         nsCOMPtr<nsIContent> child;
                    500:         content->ChildAt(i, *getter_AddRefs(child));
                    501:         
                    502:         // Obtain our name attribute.
                    503:         nsAutoString name;
                    504:         child->GetAttr(kNameSpaceID_None, gNameAtom, name);
                    505:         nsCAutoString nameCStr; nameCStr.AssignWithConversion(name);
                    506:         NSString* title = [NSString stringWithCString: nameCStr.get()];
                    507:                 
                    508:         // Create a menu or menu item for the child.
1.8       hyatt     509:         NSMenuItem* menuItem = [[[NSMenuItem alloc] initWithTitle: title action: NULL keyEquivalent: @""] autorelease];
                    510:         GetWrapperFor(child);
                    511:         [aMenu addItem: menuItem];
                    512:         
1.7       hyatt     513:         nsCOMPtr<nsIAtom> tag;
1.8       hyatt     514:         child->GetTag(*getter_AddRefs(tag));
                    515:         
1.7       hyatt     516:         if (tag == gFolderAtom) {
                    517:             NSMenu* menu = [[[NSMenu alloc] initWithTitle: title] autorelease];
1.8       hyatt     518:             [aMenu setSubmenu: menu forItem: menuItem];
                    519:             [menu setAutoenablesItems: NO];
1.7       hyatt     520:             ConstructBookmarksMenu(menu, child);
                    521:         }
                    522:         else {
1.8       hyatt     523:             [menuItem setTarget: gMainController];
                    524:             [menuItem setAction: @selector(openMenuBookmark:)];
1.7       hyatt     525:         }
1.8       hyatt     526:         
                    527:         PRUint32 contentID;
                    528:         child->GetContentID(&contentID);
                    529:         [menuItem setTag: contentID];
1.7       hyatt     530:     }
1.6       hyatt     531: }
1.8       hyatt     532: 
                    533: void 
                    534: BookmarksService::OpenMenuBookmark(BrowserWindowController* aController, id aMenuItem)
                    535: {
                    536:     // Get the corresponding bookmark item.
                    537:     BookmarkItem* item = [gDictionary objectForKey: [NSNumber numberWithInt: [aMenuItem tag]]];
                    538:         
                    539:     // Get the content node.
                    540:     nsIContent* content = [item contentNode];
                    541:         
                    542:     // Get the href attribute.  This is the URL we want to load.
                    543:     nsAutoString href;
                    544:     content->GetAttr(kNameSpaceID_None, gHrefAtom, href);
                    545:     nsCAutoString cref; cref.AssignWithConversion(href);
                    546:     if (cref.IsEmpty())
                    547:         return;
                    548:         
                    549:     NSString* url = [NSString stringWithCString: cref.get()];
                    550:     
                    551:     // Now load the URL in the window.
                    552:     [aController loadURL:[NSURL URLWithString: url]];
                    553: }

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