File:  [mozdev] / chimera / BookmarksService.mm
Revision 1.28: download - view: text, annotated - select for diffs - revision graph
Fri Apr 19 19:28:43 2002 UTC (17 years, 1 month ago) by hyatt
Branches: MAIN
CVS tags: HEAD
You can now create bookmark groups.

    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:     if ([mOutlineView isExpandable: item]) {
  326:         if ([mOutlineView isItemExpanded: item])
  327:             [mOutlineView collapseItem: item];
  328:         else
  329:             [mOutlineView expandItem: item];
  330:     }
  331:     else {
  332:         nsIContent* content = [item contentNode];
  333:         nsAutoString href;
  334:         content->GetAttr(kNameSpaceID_None, BookmarksService::gHrefAtom, href);
  335:         if (!href.IsEmpty()) {
  336:             nsCAutoString cstr; cstr.AssignWithConversion(href);
  337:             NSString* url = [NSString stringWithCString: cstr.get()];
  338:             [[[mBrowserWindowController getMyBrowserView] getBrowserView] loadURI:[NSURL URLWithString: url] flags:NSLoadFlagsNone];
  339:             // Focus and activate our content area.
  340:             [[[mBrowserWindowController getMyBrowserView] getBrowserView] setActive: YES];
  341:         }
  342:     } 
  343: }
  344: 
  345: - (BOOL)outlineView:(NSOutlineView *)outlineView shouldEditTableColumn:(NSTableColumn *)tableColumn item:(id)item
  346: {
  347:     return NO;
  348: }
  349: 
  350: - (id)outlineView:(NSOutlineView *)outlineView child:(int)index ofItem:(id)item
  351: {
  352:     if (!mBookmarks)
  353:         return nil;
  354:        
  355:     nsCOMPtr<nsIContent> content;
  356:     if (!item)
  357:         mBookmarks->GetRootContent(getter_AddRefs(content));
  358:     else
  359:         content = [item contentNode];
  360:     
  361:     nsCOMPtr<nsIContent> child;
  362:     content->ChildAt(index, *getter_AddRefs(child));
  363:     return mBookmarks->GetWrapperFor(child);
  364: }
  365: 
  366: - (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item
  367: {
  368:     if (!mBookmarks)
  369:         return NO;
  370:     
  371:     if (!item)
  372:         return YES; // The root node is always open.
  373:     
  374:     nsCOMPtr<nsIAtom> tagName;
  375:     nsIContent* content = [item contentNode];
  376:     content->GetTag(*getter_AddRefs(tagName));
  377:     
  378:     return (tagName == BookmarksService::gFolderAtom);
  379: }
  380: 
  381: - (int)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item
  382: {
  383:     if (!mBookmarks)
  384:         return 0;
  385:   
  386:     nsCOMPtr<nsIContent> content;
  387:     if (!item)
  388:         mBookmarks->GetRootContent(getter_AddRefs(content));
  389:     else 
  390:         content = [item contentNode];
  391:     
  392:     PRInt32 childCount;
  393:     content->ChildCount(childCount);
  394:     
  395:     return childCount;
  396: }
  397: 
  398: - (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item
  399: {
  400:     NSString 					*columnName = [tableColumn identifier];
  401:     NSMutableAttributedString 	*cellValue = [[NSMutableAttributedString alloc] init];
  402:     NSFileWrapper				*fileWrapper = [[NSFileWrapper alloc] initRegularFileWithContents:nil];
  403:     NSTextAttachment			*textAttachment = [[NSTextAttachment alloc] initWithFileWrapper:fileWrapper];
  404:     NSMutableAttributedString   *attachmentAttrString = nil;
  405:     NSCell 						*attachmentAttrStringCell;
  406: 
  407:     if ([columnName isEqualToString: @"name"]) {
  408:         nsIContent* content = [item contentNode];
  409:         nsAutoString nameAttr;
  410:         content->GetAttr(kNameSpaceID_None, BookmarksService::gNameAtom, nameAttr);
  411:         nsCAutoString cStr; cStr.AssignWithConversion(nameAttr);
  412:         
  413:         //Set cell's textual contents
  414:         [cellValue replaceCharactersInRange:NSMakeRange(0, [cellValue length])
  415:                                  withString:[NSString stringWithCString: cStr.get()]];
  416:         
  417:         //Create an attributed string to hold the empty attachment, then release the components.
  418:         attachmentAttrString = [[NSMutableAttributedString attributedStringWithAttachment:textAttachment] retain];
  419:         [textAttachment release];
  420:         [fileWrapper release];
  421: 
  422:         //Get the cell of the text attachment.
  423:         attachmentAttrStringCell = (NSCell *)[(NSTextAttachment *)[attachmentAttrString attribute:NSAttachmentAttributeName
  424:                                                                                           atIndex:0
  425:                                                                                    effectiveRange:nil] attachmentCell];
  426:         //Figure out which image to add, and set the cell's image.
  427:         if ( [self outlineView:outlineView isItemExpandable:item] ) {
  428:             [attachmentAttrStringCell setImage:[NSImage imageNamed:@"folder"]];
  429:         } else {
  430:             [attachmentAttrStringCell setImage:[NSImage imageNamed:@"smallbookmark"]];
  431:         }
  432:         //Insert the image
  433:         [cellValue replaceCharactersInRange:NSMakeRange(0, 0) withAttributedString:attachmentAttrString];
  434:         
  435:         //Tweak the baseline to vertically center the text.
  436:         [cellValue addAttribute:NSBaselineOffsetAttributeName
  437:                           value:[NSNumber numberWithFloat:-3.0]
  438:                           range:NSMakeRange(0, 1)];
  439:     }
  440:     return cellValue;
  441: }
  442: 
  443: - (void)outlineView:(NSOutlineView *)outlineView setObjectValue:(id)object forTableColumn:(NSTableColumn *)tableColumn byItem:(id)item
  444: {
  445: }
  446: 
  447: - (void)reloadDataForItem:(id)item reloadChildren: (BOOL)aReloadChildren
  448: {
  449:     printf("Reloading?\n");
  450:     if (!item)
  451:         [mOutlineView reloadData];
  452:     else if ([mOutlineView isItemExpanded: item])
  453:         [mOutlineView reloadItem: item reloadChildren: aReloadChildren];
  454: }
  455: 
  456: @end
  457: 
  458: @implementation BookmarkItem
  459: -(nsIContent*)contentNode
  460: {
  461:     return mContentNode;
  462: }
  463: 
  464: -(void)setContentNode: (nsIContent*)aContentNode
  465: {
  466:     mContentNode = aContentNode;
  467: }
  468: 
  469: - (id)copyWithZone:(NSZone *)aZone
  470: {
  471:     BookmarkItem* copy = [[[self class] allocWithZone: aZone] init];
  472:     [copy setContentNode: mContentNode];
  473:     return copy;
  474: }
  475: 
  476: @end
  477: 
  478: // Helper for stripping whitespace
  479: static void
  480: StripWhitespaceNodes(nsIContent* aElement)
  481: {
  482:     PRInt32 childCount;
  483:     aElement->ChildCount(childCount);
  484:     for (PRInt32 i = 0; i < childCount; i++) {
  485:         nsCOMPtr<nsIContent> child;
  486:         aElement->ChildAt(i, *getter_AddRefs(child));
  487:         nsCOMPtr<nsITextContent> text = do_QueryInterface(child);
  488:         if (text) {
  489:             PRBool isEmpty;
  490:             text->IsOnlyWhitespace(&isEmpty);
  491:             if (isEmpty) {
  492:                 // This node contained nothing but whitespace.
  493:                 // Remove it from the content model.
  494:                 aElement->RemoveChildAt(i, PR_TRUE);
  495:                 i--; // Decrement our count, since we just removed this child.
  496:                 childCount--; // Also decrement our total count.
  497:             }
  498:         }
  499:         else StripWhitespaceNodes(child);
  500:     }
  501: }
  502: 
  503: PRUint32 BookmarksService::gRefCnt = 0;
  504: nsIDocument* BookmarksService::gBookmarks = nsnull;
  505: NSMutableDictionary* BookmarksService::gDictionary = nil;
  506: MainController* BookmarksService::gMainController = nil;
  507: NSMenu* BookmarksService::gBookmarksMenu = nil;
  508: nsIDOMElement* BookmarksService::gToolbarRoot = nsnull;
  509: nsIAtom* BookmarksService::gFolderAtom = nsnull;
  510: nsIAtom* BookmarksService::gBookmarkAtom = nsnull;
  511: nsIAtom* BookmarksService::gHrefAtom = nsnull;
  512: nsIAtom* BookmarksService::gNameAtom = nsnull;
  513: nsVoidArray* BookmarksService::gInstances = nsnull;
  514: 
  515: BookmarksService::BookmarksService(BookmarksDataSource* aDataSource)
  516: {
  517:   mDataSource = aDataSource;
  518:   mToolbar = nil;
  519: }
  520: 
  521: BookmarksService::BookmarksService(BookmarksToolbar* aToolbar)
  522: {
  523:   mDataSource = nil;
  524:   mToolbar = aToolbar;
  525: }
  526: 
  527: BookmarksService::~BookmarksService()
  528: {
  529: }
  530: 
  531: void
  532: BookmarksService::GetRootContent(nsIContent** aResult)
  533: {
  534:     *aResult = nsnull;
  535:     if (gBookmarks) {
  536:         nsCOMPtr<nsIDOMElement> elt;
  537:         nsCOMPtr<nsIDOMDocument> domDoc(do_QueryInterface(gBookmarks));
  538:         domDoc->GetDocumentElement(getter_AddRefs(elt));
  539:         elt->QueryInterface(NS_GET_IID(nsIContent), (void**)aResult); // Addref happens here.
  540:     }
  541: }
  542: 
  543: BookmarkItem*
  544: BookmarksService::GetWrapperFor(nsIContent* aContent)
  545: {
  546:     if (!gDictionary)
  547:         gDictionary = [[NSMutableDictionary alloc] initWithCapacity: 30];
  548:     
  549:     PRUint32 contentID;
  550:     aContent->GetContentID(&contentID);
  551:     
  552:     BookmarkItem* item = [gDictionary objectForKey: [NSNumber numberWithInt: contentID]];
  553:     if (item)
  554:         return item;
  555:     else {
  556:         // Create an item.
  557:         item = [[[BookmarkItem alloc] init] autorelease]; // The dictionary retains us.
  558:         [item setContentNode: aContent];
  559:         [gDictionary setObject: item forKey: [NSNumber numberWithInt: contentID]];
  560:     }
  561:     return item;
  562: }
  563: 
  564: NSMenu*
  565: BookmarksService::LocateMenu(nsIContent* aContent)
  566: {
  567:   nsCOMPtr<nsIContent> parent;
  568:   aContent->GetParent(*getter_AddRefs(parent));
  569:   if (!parent) {
  570:     return BookmarksService::gBookmarksMenu;
  571:   }
  572:   
  573:   NSMenu* parentMenu = LocateMenu(parent);
  574:   
  575:   PRUint32 contentID;
  576:   aContent->GetContentID(&contentID);
  577: 
  578:   NSMenuItem* childMenu = [parentMenu itemWithTag: contentID];
  579:   return [childMenu submenu];
  580: }
  581: 
  582: void
  583: BookmarksService::BookmarkAdded(nsIContent* aContainer, nsIContent* aChild)
  584: {
  585:   if (!gInstances || !gDictionary)
  586:     return;
  587: 
  588:   PRInt32 count = gInstances->Count();
  589:   for (PRInt32 i = 0; i < count; i++) {
  590:     BookmarksService* instance = (BookmarksService*)gInstances->ElementAt(i);
  591: 
  592:     if (instance->mDataSource) {
  593:       // We're a tree view.
  594:       nsCOMPtr<nsIContent> parent;
  595:       aContainer->GetParent(*getter_AddRefs(parent));
  596: 
  597:       BookmarkItem* item = nil;
  598:       if (parent)
  599:         // We're not the root.
  600:         item = GetWrapperFor(aContainer);
  601: 
  602:       [(instance->mDataSource) reloadDataForItem: item reloadChildren: YES];
  603:     }
  604:     else if (instance->mToolbar) {
  605:       // We're a personal toolbar.
  606:       nsCOMPtr<nsIDOMElement> parentElt(do_QueryInterface(aContainer));
  607:       if (parentElt == gToolbarRoot) {
  608:         // We only care about changes that occur to the personal toolbar's immediate
  609:         // children.
  610:         PRInt32 index = -1;
  611:         aContainer->IndexOf(aChild, index);
  612:         nsCOMPtr<nsIDOMElement> elt(do_QueryInterface(aChild));
  613:         [(instance->mToolbar) addButton: elt atIndex: index];
  614:       }
  615:     }
  616:     else {
  617:       // We're the menu.
  618:       PRInt32 index = -1;
  619:       aContainer->IndexOf(aChild, index);
  620:       NSMenu* menu = LocateMenu(aContainer);
  621:       AddMenuBookmark(menu, aContainer, aChild, index);
  622:     }
  623:   }
  624:   
  625:   FlushBookmarks();  
  626: }
  627: 
  628: void
  629: BookmarksService::BookmarkChanged(nsIContent* aItem)
  630: {
  631:   if (!gInstances || !gDictionary)
  632:     return;
  633: 
  634:   PRInt32 count = gInstances->Count();
  635:   for (PRInt32 i = 0; i < count; i++) {
  636:     BookmarksService* instance = (BookmarksService*)gInstances->ElementAt(i);
  637:    
  638:     if (instance->mDataSource) {
  639:       BookmarkItem* item = GetWrapperFor(aItem);
  640:       [(instance->mDataSource) reloadDataForItem: item reloadChildren: NO];
  641:     }
  642:   }
  643: 
  644:   FlushBookmarks();  
  645: }
  646: 
  647: void
  648: BookmarksService::BookmarkRemoved(nsIContent* aContainer, nsIContent* aChild)
  649: {
  650:   if (!gInstances)
  651:     return;
  652: 
  653:   PRInt32 count = gInstances->Count();
  654:   for (PRInt32 i = 0; i < count; i++) {
  655:     BookmarksService* instance = (BookmarksService*)gInstances->ElementAt(i);
  656: 
  657:     if (instance->mDataSource) {
  658:       // We're a tree view.
  659:       nsCOMPtr<nsIContent> parent;
  660:       aContainer->GetParent(*getter_AddRefs(parent));
  661: 
  662:       BookmarkItem* item = nil;
  663:       if (parent)
  664:         // We're not the root.
  665:         item = GetWrapperFor(aContainer);
  666: 
  667:       [(instance->mDataSource) reloadDataForItem: item reloadChildren: YES];
  668:     }
  669:     else if (instance->mToolbar) {
  670:       // We're a personal toolbar.
  671:       nsCOMPtr<nsIDOMElement> parentElt(do_QueryInterface(aContainer));
  672:       if (parentElt == gToolbarRoot) {
  673:         // We only care about changes that occur to the personal toolbar's immediate
  674:         // children.
  675:         nsCOMPtr<nsIDOMElement> childElt(do_QueryInterface(aChild));
  676:         [(instance->mToolbar) removeButton: childElt];
  677:       }
  678:     }    
  679:     else {
  680:       // We're the menu.
  681:       NSMenu* menu = LocateMenu(aContainer);
  682:       PRUint32 contentID;
  683:       aChild->GetContentID(&contentID);
  684:       NSMenuItem* childItem = [menu itemWithTag: contentID];
  685:       [menu removeItem: childItem];
  686:     }
  687:   }
  688: 
  689:   FlushBookmarks(); 
  690: }
  691: 
  692: void
  693: BookmarksService::AddObserver()
  694: {
  695:     gRefCnt++;
  696:     if (gRefCnt == 1) {
  697:         gBookmarkAtom = NS_NewAtom("bookmark");
  698:         gFolderAtom = NS_NewAtom("folder");
  699:         gNameAtom = NS_NewAtom("name");
  700:         gHrefAtom = NS_NewAtom("href");
  701:         gInstances = new nsVoidArray();
  702:                 
  703:         nsCOMPtr<nsIFile> profileDir;
  704:         NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR, getter_AddRefs(profileDir));
  705:         profileDir->Append("bookmarks.xml");
  706:     
  707:         nsCAutoString bookmarksFileURL;
  708:         NS_GetURLSpecFromFile(profileDir, bookmarksFileURL);
  709:         
  710:         nsCOMPtr<nsIURI> uri;
  711:         NS_NewURI(getter_AddRefs(uri), bookmarksFileURL.get());
  712:     
  713:         nsCOMPtr<nsIXBLService> xblService(do_GetService("@mozilla.org/xbl;1"));    
  714:         xblService->FetchSyncXMLDocument(uri, &gBookmarks); // The addref is here.
  715:         
  716:         nsCOMPtr<nsIContent> rootNode;
  717:         GetRootContent(getter_AddRefs(rootNode));
  718:         StripWhitespaceNodes(rootNode);
  719:     }
  720:     
  721:     gInstances->AppendElement(this);
  722: }
  723: 
  724: void
  725: BookmarksService::RemoveObserver()
  726: {
  727:     if (gRefCnt == 0)
  728:         return;
  729:  
  730:     gInstances->RemoveElement(this);
  731:      
  732:     gRefCnt--;
  733:     if (gRefCnt == 0) {
  734:         NS_IF_RELEASE(gBookmarks);
  735:         NS_RELEASE(gBookmarkAtom);
  736:         NS_RELEASE(gFolderAtom);
  737:         NS_RELEASE(gNameAtom);
  738:         NS_RELEASE(gHrefAtom);
  739:         [gDictionary release];
  740:     }
  741: }
  742: 
  743: void
  744: BookmarksService::FlushBookmarks()
  745: {
  746:     nsCOMPtr<nsIFile> bookmarksFile;
  747:     NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR, getter_AddRefs(bookmarksFile));
  748:     bookmarksFile->Append("bookmarks.xml");
  749: 
  750:     nsCOMPtr<nsIOutputStream> outputStream;
  751:     NS_NewLocalFileOutputStream(getter_AddRefs(outputStream), bookmarksFile);
  752: 
  753:     nsCOMPtr<nsIDOMDocument> domDoc(do_QueryInterface(gBookmarks));
  754:     
  755:     nsCOMPtr<nsIDOMSerializer> domSerializer(do_CreateInstance(NS_XMLSERIALIZER_CONTRACTID));
  756:     domSerializer->SerializeToStream(domDoc, outputStream, nsnull);
  757: }
  758: 
  759: void BookmarksService::EnsureToolbarRoot()
  760: {
  761:   if (gToolbarRoot)
  762:     return;
  763: 
  764:   nsCOMPtr<nsIDOMDocument> domDoc(do_QueryInterface(gBookmarks));
  765:   nsCOMPtr<nsIDOMElement> rootElt;
  766:   domDoc->GetDocumentElement(getter_AddRefs(rootElt));
  767:   
  768:   nsCOMPtr<nsIDOMNode> child;
  769:   rootElt->GetFirstChild(getter_AddRefs(child));
  770:   nsAutoString typeValue;
  771:   while (child) {
  772:     nsCOMPtr<nsIDOMElement> childElt(do_QueryInterface(child));
  773:     if (childElt) {
  774:       childElt->GetAttribute(NS_LITERAL_STRING("type"), typeValue);
  775:       if (typeValue.Equals(NS_LITERAL_STRING("toolbar")))
  776:         gToolbarRoot = childElt;
  777:     }
  778:     
  779:     nsCOMPtr<nsIDOMNode> temp;
  780:     child->GetNextSibling(getter_AddRefs(temp));
  781:     child = temp;
  782:   }
  783: 
  784:   if (!gToolbarRoot) {
  785:     printf("Repairing personal toolbar.\n");
  786:     nsCOMPtr<nsIDOMElement> elt;
  787:     domDoc->CreateElementNS(NS_LITERAL_STRING("http://chimera.mozdev.org/bookmarks/"),
  788:                             NS_LITERAL_STRING("folder"),
  789:                             getter_AddRefs(elt));
  790: 
  791:     elt->SetAttribute(NS_LITERAL_STRING("name"), NS_LITERAL_STRING("Toolbar Bookmarks"));
  792:     elt->SetAttribute(NS_LITERAL_STRING("type"), NS_LITERAL_STRING("toolbar"));
  793: 
  794:     nsCOMPtr<nsIDOMNode> dummy;
  795:     rootElt->AppendChild(elt, getter_AddRefs(dummy));
  796:     gToolbarRoot = elt;
  797:   }
  798: }
  799: 
  800: static
  801: void RecursiveAddBookmarkConstruct(NSPopUpButton* aPopup, NSMenu* aMenu, int aTagToMatch)
  802: {
  803:   // Get the menu item children.
  804:   NSArray* children = [aMenu itemArray];
  805:   int startPosition = 0;
  806:   if (aMenu == BookmarksService::gBookmarksMenu)
  807:     startPosition = 3;
  808: 
  809:   int count = [children count];
  810:   for (int i = startPosition; i < count; i++) {
  811:     NSMenuItem* menuItem = [children objectAtIndex: i];
  812:     NSMenu* submenu = [menuItem submenu];
  813:     if (submenu) {
  814:       // This is a folder.  Add it to our list and then recur.
  815:       [aPopup addItemWithTitle: [menuItem title]];
  816:       NSMenuItem* lastItem = [aPopup lastItem];
  817:       if ([menuItem tag] == aTagToMatch)
  818:         [aPopup selectItem: lastItem];
  819:       
  820:       [lastItem setTag: [menuItem tag]];
  821:       RecursiveAddBookmarkConstruct(aPopup, submenu, aTagToMatch);
  822:     }
  823:   }
  824: }
  825: 
  826: void
  827: BookmarksService::ConstructAddBookmarkFolderList(NSPopUpButton* aPopup, BookmarkItem* aItem)
  828: {
  829:   [aPopup removeAllItems];
  830:   [aPopup addItemWithTitle: [gBookmarksMenu title]];
  831:   NSMenuItem* lastItem = [aPopup lastItem];
  832:   [lastItem setTag: -1];
  833:   int tag = -1;
  834:   if (aItem) {
  835:     nsIContent* content = [aItem contentNode];
  836:     PRUint32 utag;
  837:     content->GetContentID(&utag);
  838:     tag = (int)utag;
  839:   }
  840:   RecursiveAddBookmarkConstruct(aPopup, gBookmarksMenu, tag);
  841: }
  842: 
  843: void
  844: BookmarksService::GetTitleAndHrefForBrowserView(id aBrowserView, nsString& aTitle, nsString& aHref)
  845: {
  846:   nsCOMPtr<nsIWebBrowser> webBrowser = getter_AddRefs([aBrowserView getWebBrowser]);
  847:   nsCOMPtr<nsIDOMWindow> window;
  848:   webBrowser->GetContentDOMWindow(getter_AddRefs(window));
  849:   nsCOMPtr<nsIDOMDocument> htmlDoc;
  850:   window->GetDocument(getter_AddRefs(htmlDoc));
  851:   nsCOMPtr<nsIDocument> pageDoc(do_QueryInterface(htmlDoc));
  852: 
  853:   if (pageDoc) {
  854:     nsCOMPtr<nsIURI> url;
  855:     pageDoc->GetDocumentURL(getter_AddRefs(url));
  856:     nsCAutoString spec;
  857:     url->GetSpec(spec);
  858:     aHref.AssignWithConversion(spec.get());
  859:   }
  860: 
  861:   nsCOMPtr<nsIDOMHTMLDocument> htmlDocument(do_QueryInterface(htmlDoc));
  862:   if (htmlDocument)
  863:     htmlDocument->GetTitle(aTitle);
  864:   if (aTitle.IsEmpty())
  865:     aTitle = aHref;  
  866: }
  867: 
  868: void
  869: BookmarksService::ConstructBookmarksMenu(NSMenu* aMenu, nsIContent* aContent)
  870: {
  871:     nsCOMPtr<nsIContent> content = aContent;
  872:     if (!content) {
  873:         GetRootContent(getter_AddRefs(content));
  874:         GetWrapperFor(content);
  875:         gBookmarksMenu = aMenu;
  876:     }
  877:     
  878:     // Now walk our children, and for folders also recur into them.
  879:     PRInt32 childCount;
  880:     content->ChildCount(childCount);
  881:     
  882:     for (PRInt32 i = 0; i < childCount; i++) {
  883:       nsCOMPtr<nsIContent> child;
  884:       content->ChildAt(i, *getter_AddRefs(child));
  885:       AddMenuBookmark(aMenu, content, child, -1);
  886:     }
  887: }
  888: 
  889: void
  890: BookmarksService::AddMenuBookmark(NSMenu* aMenu, nsIContent* aParent, nsIContent* aChild, PRInt32 aIndex)
  891: {
  892:   nsAutoString name;
  893:   aChild->GetAttr(kNameSpaceID_None, gNameAtom, name);
  894:   nsCAutoString nameCStr; nameCStr.AssignWithConversion(name);
  895:   NSString* title = [NSString stringWithCString: nameCStr.get()];
  896: 
  897:   // Create a menu or menu item for the child.
  898:   NSMenuItem* menuItem = [[[NSMenuItem alloc] initWithTitle: title action: NULL keyEquivalent: @""] autorelease];
  899:   GetWrapperFor(aChild);
  900: 
  901:   if (aIndex == -1)
  902:     [aMenu addItem: menuItem];
  903:   else
  904:     [aMenu insertItem: menuItem atIndex: aIndex];
  905:   
  906:   nsCOMPtr<nsIAtom> tagName;
  907:   aChild->GetTag(*getter_AddRefs(tagName));
  908: 
  909:   if (tagName == gFolderAtom) {
  910:     NSMenu* menu = [[[NSMenu alloc] initWithTitle: title] autorelease];
  911:     [aMenu setSubmenu: menu forItem: menuItem];
  912:     [menu setAutoenablesItems: NO];
  913:     ConstructBookmarksMenu(menu, aChild);
  914:   }
  915:   else {
  916:     [menuItem setTarget: gMainController];
  917:     [menuItem setAction: @selector(openMenuBookmark:)];
  918:   }
  919: 
  920:   PRUint32 contentID;
  921:   aChild->GetContentID(&contentID);
  922:   [menuItem setTag: contentID];
  923: }
  924: 
  925: void 
  926: BookmarksService::OpenMenuBookmark(BrowserWindowController* aController, id aMenuItem)
  927: {
  928:     // Get the corresponding bookmark item.
  929:     BookmarkItem* item = [gDictionary objectForKey: [NSNumber numberWithInt: [aMenuItem tag]]];
  930:         
  931:     // Get the content node.
  932:     nsIContent* content = [item contentNode];
  933:         
  934:     // Get the href attribute.  This is the URL we want to load.
  935:     nsAutoString href;
  936:     content->GetAttr(kNameSpaceID_None, gHrefAtom, href);
  937:     nsCAutoString cref; cref.AssignWithConversion(href);
  938:     if (cref.IsEmpty())
  939:         return;
  940:         
  941:     NSString* url = [NSString stringWithCString: cref.get()];
  942:     
  943:     // Now load the URL in the window.
  944:     [aController loadURL:[NSURL URLWithString: url]];
  945:     
  946:     // Focus and activate our content area.
  947:     [[[aController getMyBrowserView] getBrowserView] setActive: YES];
  948: }

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