Annotation of chimera/BookmarksService.mm, revision 1.25

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

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