Annotation of chimera/BookmarksService.mm, revision 1.17

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.1       hyatt     199:         }
                    200:     } 
                    201: }
                    202: 
1.9       hyatt     203: - (BOOL)outlineView:(NSOutlineView *)outlineView shouldEditTableColumn:(NSTableColumn *)tableColumn item:(id)item
                    204: {
1.10      hyatt     205:     return NO;
1.9       hyatt     206: }
                    207: 
1.1       hyatt     208: - (id)outlineView:(NSOutlineView *)outlineView child:(int)index ofItem:(id)item
                    209: {
                    210:     if (!mBookmarks)
                    211:         return nil;
                    212:        
                    213:     nsCOMPtr<nsIContent> content;
                    214:     if (!item)
                    215:         mBookmarks->GetRootContent(getter_AddRefs(content));
                    216:     else
                    217:         content = [item contentNode];
                    218:     
                    219:     nsCOMPtr<nsIContent> child;
                    220:     content->ChildAt(index, *getter_AddRefs(child));
                    221:     return mBookmarks->GetWrapperFor(child);
                    222: }
                    223: 
                    224: - (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item
                    225: {
                    226:     if (!mBookmarks)
                    227:         return NO;
                    228:     
                    229:     if (!item)
                    230:         return YES; // The root node is always open.
                    231:     
                    232:     nsCOMPtr<nsIAtom> tagName;
                    233:     nsIContent* content = [item contentNode];
                    234:     content->GetTag(*getter_AddRefs(tagName));
                    235:     
                    236:     return (tagName == BookmarksService::gFolderAtom);
                    237: }
                    238: 
                    239: - (int)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item
                    240: {
                    241:     if (!mBookmarks)
                    242:         return 0;
1.2       hyatt     243:   
1.1       hyatt     244:     nsCOMPtr<nsIContent> content;
                    245:     if (!item)
                    246:         mBookmarks->GetRootContent(getter_AddRefs(content));
                    247:     else 
                    248:         content = [item contentNode];
                    249:     
                    250:     PRInt32 childCount;
                    251:     content->ChildCount(childCount);
                    252:     
                    253:     return childCount;
                    254: }
                    255: 
                    256: - (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item
                    257: {
1.13      macserv   258:     NSString           *columnName = [tableColumn identifier];
1.11      macserv   259:     NSMutableAttributedString  *cellValue = [[NSMutableAttributedString alloc] init];
1.13      macserv   260:     NSFileWrapper        *fileWrapper = [[NSFileWrapper alloc] initRegularFileWithContents:nil];
                    261:     NSTextAttachment     *textAttachment = [[NSTextAttachment alloc] initWithFileWrapper:fileWrapper];
1.11      macserv   262:     NSMutableAttributedString   *attachmentAttrString = nil;
1.13      macserv   263:     NSCell             *attachmentAttrStringCell;
1.11      macserv   264: 
1.13      macserv   265:     if ([columnName isEqualToString: @"name"]) {
                    266:         nsIContent* content = [item contentNode];
                    267:         nsAutoString nameAttr;
                    268:         content->GetAttr(kNameSpaceID_None, BookmarksService::gNameAtom, nameAttr);
                    269:         nsCAutoString cStr; cStr.AssignWithConversion(nameAttr);
                    270:         
                    271:         //Set cell's textual contents
                    272:         [cellValue replaceCharactersInRange:NSMakeRange(0, [cellValue length])
                    273:                                  withString:[NSString stringWithCString: cStr.get()]];
                    274:         
                    275:         //Create an attributed string to hold the empty attachment, then release the components.
                    276:         attachmentAttrString = [[NSMutableAttributedString attributedStringWithAttachment:textAttachment] retain];
                    277:         [textAttachment release];
                    278:         [fileWrapper release];
1.11      macserv   279: 
1.13      macserv   280:         //Get the cell of the text attachment.
                    281:         attachmentAttrStringCell = (NSCell *)[(NSTextAttachment *)[attachmentAttrString attribute:NSAttachmentAttributeName
                    282:                                                                                           atIndex:0
                    283:                                                                                    effectiveRange:nil] attachmentCell];
                    284:         //Figure out which image to add, and set the cell's image.
                    285:         if ( [self outlineView:outlineView isItemExpandable:item] ) {
                    286:             [attachmentAttrStringCell setImage:[NSImage imageNamed:@"folder"]];
                    287:         } else {
1.17    ! macserv   288:             [attachmentAttrStringCell setImage:[NSImage imageNamed:@"smallbookmark"]];
1.11      macserv   289:         }
1.13      macserv   290:         //Insert the image
                    291:         [cellValue replaceCharactersInRange:NSMakeRange(0, 0) withAttributedString:attachmentAttrString];
                    292:         
                    293:         //Tweak the baseline to vertically center the text.
                    294:         [cellValue addAttribute:NSBaselineOffsetAttributeName
                    295:                           value:[NSNumber numberWithFloat:-3.0]
                    296:                           range:NSMakeRange(0, 1)];
1.1       hyatt     297:     }
1.13      macserv   298:     return cellValue;
1.1       hyatt     299: }
                    300: 
                    301: - (void)outlineView:(NSOutlineView *)outlineView setObjectValue:(id)object forTableColumn:(NSTableColumn *)tableColumn byItem:(id)item
                    302: {
                    303: }
                    304: 
1.5       hyatt     305: - (void)reloadDataForItem:(id)item reloadChildren: (BOOL)aReloadChildren
                    306: {
                    307:     printf("Reloading?\n");
                    308:     if (!item)
                    309:         [mOutlineView reloadData];
                    310:     else if ([mOutlineView isItemExpanded: item])
                    311:         [mOutlineView reloadItem: item reloadChildren: aReloadChildren];
                    312: }
                    313: 
1.1       hyatt     314: @end
                    315: 
                    316: @implementation BookmarkItem
                    317: -(nsIContent*)contentNode
                    318: {
                    319:     return mContentNode;
                    320: }
                    321: 
                    322: -(void)setContentNode: (nsIContent*)aContentNode
                    323: {
                    324:     mContentNode = aContentNode;
                    325: }
1.7       hyatt     326: 
                    327: - (id)copyWithZone:(NSZone *)aZone
                    328: {
                    329:     BookmarkItem* copy = [[[self class] allocWithZone: aZone] init];
                    330:     [copy setContentNode: mContentNode];
                    331:     return copy;
                    332: }
                    333: 
1.1       hyatt     334: @end
                    335: 
                    336: // Helper for stripping whitespace
                    337: static void
                    338: StripWhitespaceNodes(nsIContent* aElement)
                    339: {
                    340:     PRInt32 childCount;
                    341:     aElement->ChildCount(childCount);
                    342:     for (PRInt32 i = 0; i < childCount; i++) {
                    343:         nsCOMPtr<nsIContent> child;
                    344:         aElement->ChildAt(i, *getter_AddRefs(child));
                    345:         nsCOMPtr<nsITextContent> text = do_QueryInterface(child);
                    346:         if (text) {
                    347:             PRBool isEmpty;
                    348:             text->IsOnlyWhitespace(&isEmpty);
                    349:             if (isEmpty) {
                    350:                 // This node contained nothing but whitespace.
                    351:                 // Remove it from the content model.
                    352:                 aElement->RemoveChildAt(i, PR_TRUE);
                    353:                 i--; // Decrement our count, since we just removed this child.
                    354:                 childCount--; // Also decrement our total count.
                    355:             }
                    356:         }
                    357:         else StripWhitespaceNodes(child);
                    358:     }
                    359: }
                    360: 
                    361: PRUint32 BookmarksService::gRefCnt = 0;
1.4       hyatt     362: nsIDocument* BookmarksService::gBookmarks = nsnull;
                    363: NSMutableDictionary* BookmarksService::gDictionary = nil;
1.8       hyatt     364: MainController* BookmarksService::gMainController = nil;
1.1       hyatt     365: nsIAtom* BookmarksService::gFolderAtom = nsnull;
                    366: nsIAtom* BookmarksService::gBookmarkAtom = nsnull;
                    367: nsIAtom* BookmarksService::gHrefAtom = nsnull;
                    368: nsIAtom* BookmarksService::gNameAtom = nsnull;
1.5       hyatt     369: nsVoidArray* BookmarksService::gInstances = nsnull;
1.1       hyatt     370: 
1.5       hyatt     371: BookmarksService::BookmarksService(BookmarksDataSource* aDataSource)
1.1       hyatt     372: {
1.5       hyatt     373:     mDataSource = aDataSource;
1.1       hyatt     374: }
                    375: 
                    376: BookmarksService::~BookmarksService()
                    377: {
                    378: }
                    379: 
                    380: void
                    381: BookmarksService::GetRootContent(nsIContent** aResult)
                    382: {
                    383:     *aResult = nsnull;
1.4       hyatt     384:     if (gBookmarks) {
1.1       hyatt     385:         nsCOMPtr<nsIDOMElement> elt;
1.4       hyatt     386:         nsCOMPtr<nsIDOMDocument> domDoc(do_QueryInterface(gBookmarks));
1.1       hyatt     387:         domDoc->GetDocumentElement(getter_AddRefs(elt));
                    388:         elt->QueryInterface(NS_GET_IID(nsIContent), (void**)aResult); // Addref happens here.
                    389:     }
                    390: }
                    391: 
                    392: BookmarkItem*
                    393: BookmarksService::GetWrapperFor(nsIContent* aContent)
                    394: {
1.4       hyatt     395:     if (!gDictionary)
                    396:         gDictionary = [[NSMutableDictionary alloc] initWithCapacity: 30];
1.1       hyatt     397:     
                    398:     PRUint32 contentID;
                    399:     aContent->GetContentID(&contentID);
                    400:     
1.4       hyatt     401:     BookmarkItem* item = [gDictionary objectForKey: [NSNumber numberWithInt: contentID]];
1.1       hyatt     402:     if (item)
                    403:         return item;
                    404:     else {
                    405:         // Create an item.
                    406:         item = [[[BookmarkItem alloc] init] autorelease]; // The dictionary retains us.
                    407:         [item setContentNode: aContent];
1.4       hyatt     408:         [gDictionary setObject: item forKey: [NSNumber numberWithInt: contentID]];
1.1       hyatt     409:     }
                    410:     return item;
                    411: }
                    412: 
1.5       hyatt     413: void
                    414: BookmarksService::NotifyObservers(nsIContent* aContainer, PRBool aReloadChildren)
1.4       hyatt     415: {
1.5       hyatt     416:     if (!gInstances)
                    417:         return;
                    418:     
                    419:     PRInt32 count = gInstances->Count();
                    420:     for (PRInt32 i = 0; i < count; i++) {
                    421:         BookmarksService* instance = (BookmarksService*)gInstances->ElementAt(i);
                    422:         instance->NotifyObserver(aContainer, aReloadChildren);
                    423:     }
1.6       hyatt     424:     
                    425:     FlushBookmarks();
1.4       hyatt     426: }
                    427: 
                    428: 
1.5       hyatt     429: void
                    430: BookmarksService::NotifyObserver(nsIContent* aContainer, PRBool aReloadChildren)
1.4       hyatt     431: {
1.5       hyatt     432:     if (!gDictionary)
                    433:         return;
1.6       hyatt     434:    
1.5       hyatt     435:     nsCOMPtr<nsIContent> parent;
                    436:     aContainer->GetParent(*getter_AddRefs(parent));
                    437:     
                    438:     BookmarkItem* item = nil;
                    439:     if (parent)
                    440:         // We're not the root.
                    441:         item = GetWrapperFor(aContainer);
                    442:     
                    443:     [mDataSource reloadDataForItem: item reloadChildren: aReloadChildren];
1.4       hyatt     444: }
                    445: 
                    446: void
                    447: BookmarksService::AddObserver()
1.1       hyatt     448: {
                    449:     gRefCnt++;
                    450:     if (gRefCnt == 1) {
                    451:         gBookmarkAtom = NS_NewAtom("bookmark");
                    452:         gFolderAtom = NS_NewAtom("folder");
                    453:         gNameAtom = NS_NewAtom("name");
                    454:         gHrefAtom = NS_NewAtom("href");
1.5       hyatt     455:         gInstances = new nsVoidArray();
1.8       hyatt     456:                 
1.4       hyatt     457:         nsCOMPtr<nsIFile> profileDir;
                    458:         NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR, getter_AddRefs(profileDir));
                    459:         profileDir->Append("bookmarks.xml");
                    460:     
1.15      hyatt     461:         nsCAutoString bookmarksFileURL;
                    462:         NS_GetURLSpecFromFile(profileDir, bookmarksFileURL);
1.4       hyatt     463:         
                    464:         nsCOMPtr<nsIURI> uri;
                    465:         NS_NewURI(getter_AddRefs(uri), bookmarksFileURL.get());
                    466:     
                    467:         nsCOMPtr<nsIXBLService> xblService(do_GetService("@mozilla.org/xbl;1"));    
                    468:         xblService->FetchSyncXMLDocument(uri, &gBookmarks); // The addref is here.
                    469:         
                    470:         nsCOMPtr<nsIContent> rootNode;
                    471:         GetRootContent(getter_AddRefs(rootNode));
                    472:         StripWhitespaceNodes(rootNode);
1.1       hyatt     473:     }
                    474:     
1.5       hyatt     475:     gInstances->AppendElement(this);
1.1       hyatt     476: }
                    477: 
                    478: void
1.4       hyatt     479: BookmarksService::RemoveObserver()
1.1       hyatt     480: {
                    481:     if (gRefCnt == 0)
                    482:         return;
1.4       hyatt     483:  
1.5       hyatt     484:     gInstances->RemoveElement(this);
                    485:      
1.1       hyatt     486:     gRefCnt--;
                    487:     if (gRefCnt == 0) {
1.4       hyatt     488:         NS_IF_RELEASE(gBookmarks);
1.1       hyatt     489:         NS_RELEASE(gBookmarkAtom);
                    490:         NS_RELEASE(gFolderAtom);
                    491:         NS_RELEASE(gNameAtom);
                    492:         NS_RELEASE(gHrefAtom);
1.4       hyatt     493:         [gDictionary release];
1.1       hyatt     494:     }
                    495: }
                    496: 
1.6       hyatt     497: void
                    498: BookmarksService::FlushBookmarks()
                    499: {
                    500:     nsCOMPtr<nsIFile> bookmarksFile;
                    501:     NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR, getter_AddRefs(bookmarksFile));
                    502:     bookmarksFile->Append("bookmarks.xml");
                    503: 
                    504:     nsCOMPtr<nsIOutputStream> outputStream;
                    505:     NS_NewLocalFileOutputStream(getter_AddRefs(outputStream), bookmarksFile);
                    506: 
                    507:     nsCOMPtr<nsIDOMDocument> domDoc(do_QueryInterface(gBookmarks));
                    508:     
                    509:     nsCOMPtr<nsIDOMSerializer> domSerializer(do_CreateInstance(NS_XMLSERIALIZER_CONTRACTID));
                    510:     domSerializer->SerializeToStream(domDoc, outputStream, nsnull);
1.7       hyatt     511: }
                    512: 
                    513: void
                    514: BookmarksService::ConstructBookmarksMenu(NSMenu* aMenu, nsIContent* aContent)
                    515: {
                    516:     nsCOMPtr<nsIContent> content = aContent;
                    517:     if (!content) {
                    518:         GetRootContent(getter_AddRefs(content));
1.8       hyatt     519:         GetWrapperFor(content);
1.7       hyatt     520:     }
                    521:     
                    522:     // Now walk our children, and for folders also recur into them.
                    523:     PRInt32 childCount;
                    524:     content->ChildCount(childCount);
                    525:     
                    526:     for (PRInt32 i = 0; i < childCount; i++) {
                    527:         nsCOMPtr<nsIContent> child;
                    528:         content->ChildAt(i, *getter_AddRefs(child));
                    529:         
                    530:         // Obtain our name attribute.
                    531:         nsAutoString name;
                    532:         child->GetAttr(kNameSpaceID_None, gNameAtom, name);
                    533:         nsCAutoString nameCStr; nameCStr.AssignWithConversion(name);
                    534:         NSString* title = [NSString stringWithCString: nameCStr.get()];
                    535:                 
                    536:         // Create a menu or menu item for the child.
1.8       hyatt     537:         NSMenuItem* menuItem = [[[NSMenuItem alloc] initWithTitle: title action: NULL keyEquivalent: @""] autorelease];
                    538:         GetWrapperFor(child);
                    539:         [aMenu addItem: menuItem];
                    540:         
1.7       hyatt     541:         nsCOMPtr<nsIAtom> tag;
1.8       hyatt     542:         child->GetTag(*getter_AddRefs(tag));
                    543:         
1.7       hyatt     544:         if (tag == gFolderAtom) {
                    545:             NSMenu* menu = [[[NSMenu alloc] initWithTitle: title] autorelease];
1.8       hyatt     546:             [aMenu setSubmenu: menu forItem: menuItem];
                    547:             [menu setAutoenablesItems: NO];
1.7       hyatt     548:             ConstructBookmarksMenu(menu, child);
                    549:         }
                    550:         else {
1.8       hyatt     551:             [menuItem setTarget: gMainController];
                    552:             [menuItem setAction: @selector(openMenuBookmark:)];
1.7       hyatt     553:         }
1.8       hyatt     554:         
                    555:         PRUint32 contentID;
                    556:         child->GetContentID(&contentID);
                    557:         [menuItem setTag: contentID];
1.7       hyatt     558:     }
1.6       hyatt     559: }
1.8       hyatt     560: 
                    561: void 
                    562: BookmarksService::OpenMenuBookmark(BrowserWindowController* aController, id aMenuItem)
                    563: {
                    564:     // Get the corresponding bookmark item.
                    565:     BookmarkItem* item = [gDictionary objectForKey: [NSNumber numberWithInt: [aMenuItem tag]]];
                    566:         
                    567:     // Get the content node.
                    568:     nsIContent* content = [item contentNode];
                    569:         
                    570:     // Get the href attribute.  This is the URL we want to load.
                    571:     nsAutoString href;
                    572:     content->GetAttr(kNameSpaceID_None, gHrefAtom, href);
                    573:     nsCAutoString cref; cref.AssignWithConversion(href);
                    574:     if (cref.IsEmpty())
                    575:         return;
                    576:         
                    577:     NSString* url = [NSString stringWithCString: cref.get()];
                    578:     
                    579:     // Now load the URL in the window.
                    580:     [aController loadURL:[NSURL URLWithString: url]];
                    581: }

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