File:  [mozdev] / chimera / BookmarksService.mm
Revision 1.27: download - view: text, annotated - select for diffs - revision graph
Fri Apr 19 08:45:57 2002 UTC (16 years, 11 months ago) by hyatt
Branches: MAIN
CVS tags: HEAD
add support for additional keys to the outline view used by bookmarks.  adding delete support, return support, and left/right arrow support.

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

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