File:  [mozdev] / chimera / BookmarksService.mm
Revision 1.26: download - view: text, annotated - select for diffs - revision graph
Fri Apr 19 07:58:30 2002 UTC (17 years, 1 month ago) by hyatt
Branches: MAIN
CVS tags: HEAD
Implement create in folder support for adding bookmarks

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

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