File:  [mozdev] / chimera / BookmarksService.mm
Revision 1.19: download - view: text, annotated - select for diffs - revision graph
Sun Apr 14 08:43:03 2002 UTC (17 years, 1 month ago) by hyatt
Branches: MAIN
CVS tags: HEAD
So many changes I can't even go into it. :)

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

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