Annotation of chimera/BookmarksService.mm, revision 1.18

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

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