File:  [mozdev] / chimera / BookmarksService.mm
Revision 1.24: download - view: text, annotated - select for diffs - revision graph
Wed Apr 17 23:55:25 2002 UTC (17 years, 1 month ago) by hyatt
Branches: MAIN
CVS tags: HEAD
Hooking up new folder.

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

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