File:  [mozdev] / chimera / BookmarksService.mm
Revision 1.29: download - view: text, annotated - select for diffs - revision graph
Sat Apr 20 00:02:55 2002 UTC (16 years, 7 months ago) by hyatt
Branches: MAIN
CVS tags: HEAD
Make hidden tabs actually able to work with Gecko (for background tab loading and bookmark groups).  Requires an update of the Mozilla tree.

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

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