Annotation of chimera/BookmarksService.mm, revision 1.27

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

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