Annotation of chimera/BookmarksService.mm, revision 1.24

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: 
                    209: -(IBAction)deleteBookmark: (id)aSender
                    210: {
1.6       hyatt     211:     if (!mBookmarks)
                    212:         return;
                    213:     
                    214:     int index = [mOutlineView selectedRow];
                    215:     if (index == -1)
                    216:         return;
                    217:         
                    218:     BookmarkItem* item = [mOutlineView itemAtRow: index];
                    219:     nsCOMPtr<nsIContent> content = [item contentNode];
                    220:     nsCOMPtr<nsIDOMElement> child(do_QueryInterface(content));
                    221:     nsCOMPtr<nsIDOMNode> parent;
                    222:     child->GetParentNode(getter_AddRefs(parent));
1.19      hyatt     223:     nsCOMPtr<nsIContent> parentContent(do_QueryInterface(parent));
1.6       hyatt     224:     nsCOMPtr<nsIDOMNode> dummy;
                    225:     parent->RemoveChild(child, getter_AddRefs(dummy));
1.19      hyatt     226:     mBookmarks->BookmarkRemoved(parentContent, content);
1.6       hyatt     227:     
                    228:     int total = [mOutlineView numberOfRows];
                    229:     if (index == total)
                    230:         index--;
                    231:         
                    232:     [mOutlineView selectRow: index byExtendingSelection: NO];
1.2       hyatt     233: }
                    234: 
1.1       hyatt     235: -(IBAction)openBookmark: (id)aSender
                    236: {
                    237:     int index = [mOutlineView selectedRow];
                    238:     if (index == -1)
                    239:         return;
                    240:     
                    241:     id item = [mOutlineView itemAtRow: index];
                    242:     if (!item)
                    243:         return;
                    244:         
                    245:     if ([mOutlineView isExpandable: item]) {
                    246:         if ([mOutlineView isItemExpanded: item])
                    247:             [mOutlineView collapseItem: item];
                    248:         else
                    249:             [mOutlineView expandItem: item];
                    250:     }
                    251:     else {
                    252:         nsIContent* content = [item contentNode];
                    253:         nsAutoString href;
                    254:         content->GetAttr(kNameSpaceID_None, BookmarksService::gHrefAtom, href);
                    255:         if (!href.IsEmpty()) {
                    256:             nsCAutoString cstr; cstr.AssignWithConversion(href);
                    257:             NSString* url = [NSString stringWithCString: cstr.get()];
1.14      hyatt     258:             [[[mBrowserWindowController getMyBrowserView] getBrowserView] loadURI:[NSURL URLWithString: url] flags:NSLoadFlagsNone];
1.18      hyatt     259:             // Focus and activate our content area.
                    260:             [[[mBrowserWindowController getMyBrowserView] getBrowserView] setActive: YES];
1.1       hyatt     261:         }
                    262:     } 
                    263: }
                    264: 
1.9       hyatt     265: - (BOOL)outlineView:(NSOutlineView *)outlineView shouldEditTableColumn:(NSTableColumn *)tableColumn item:(id)item
                    266: {
1.10      hyatt     267:     return NO;
1.9       hyatt     268: }
                    269: 
1.1       hyatt     270: - (id)outlineView:(NSOutlineView *)outlineView child:(int)index ofItem:(id)item
                    271: {
                    272:     if (!mBookmarks)
                    273:         return nil;
                    274:        
                    275:     nsCOMPtr<nsIContent> content;
                    276:     if (!item)
                    277:         mBookmarks->GetRootContent(getter_AddRefs(content));
                    278:     else
                    279:         content = [item contentNode];
                    280:     
                    281:     nsCOMPtr<nsIContent> child;
                    282:     content->ChildAt(index, *getter_AddRefs(child));
                    283:     return mBookmarks->GetWrapperFor(child);
                    284: }
                    285: 
                    286: - (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item
                    287: {
                    288:     if (!mBookmarks)
                    289:         return NO;
                    290:     
                    291:     if (!item)
                    292:         return YES; // The root node is always open.
                    293:     
                    294:     nsCOMPtr<nsIAtom> tagName;
                    295:     nsIContent* content = [item contentNode];
                    296:     content->GetTag(*getter_AddRefs(tagName));
                    297:     
                    298:     return (tagName == BookmarksService::gFolderAtom);
                    299: }
                    300: 
                    301: - (int)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item
                    302: {
                    303:     if (!mBookmarks)
                    304:         return 0;
1.2       hyatt     305:   
1.1       hyatt     306:     nsCOMPtr<nsIContent> content;
                    307:     if (!item)
                    308:         mBookmarks->GetRootContent(getter_AddRefs(content));
                    309:     else 
                    310:         content = [item contentNode];
                    311:     
                    312:     PRInt32 childCount;
                    313:     content->ChildCount(childCount);
                    314:     
                    315:     return childCount;
                    316: }
                    317: 
                    318: - (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item
                    319: {
1.13      macserv   320:     NSString           *columnName = [tableColumn identifier];
1.11      macserv   321:     NSMutableAttributedString  *cellValue = [[NSMutableAttributedString alloc] init];
1.13      macserv   322:     NSFileWrapper        *fileWrapper = [[NSFileWrapper alloc] initRegularFileWithContents:nil];
                    323:     NSTextAttachment     *textAttachment = [[NSTextAttachment alloc] initWithFileWrapper:fileWrapper];
1.11      macserv   324:     NSMutableAttributedString   *attachmentAttrString = nil;
1.13      macserv   325:     NSCell             *attachmentAttrStringCell;
1.11      macserv   326: 
1.13      macserv   327:     if ([columnName isEqualToString: @"name"]) {
                    328:         nsIContent* content = [item contentNode];
                    329:         nsAutoString nameAttr;
                    330:         content->GetAttr(kNameSpaceID_None, BookmarksService::gNameAtom, nameAttr);
                    331:         nsCAutoString cStr; cStr.AssignWithConversion(nameAttr);
                    332:         
                    333:         //Set cell's textual contents
                    334:         [cellValue replaceCharactersInRange:NSMakeRange(0, [cellValue length])
                    335:                                  withString:[NSString stringWithCString: cStr.get()]];
                    336:         
                    337:         //Create an attributed string to hold the empty attachment, then release the components.
                    338:         attachmentAttrString = [[NSMutableAttributedString attributedStringWithAttachment:textAttachment] retain];
                    339:         [textAttachment release];
                    340:         [fileWrapper release];
1.11      macserv   341: 
1.13      macserv   342:         //Get the cell of the text attachment.
                    343:         attachmentAttrStringCell = (NSCell *)[(NSTextAttachment *)[attachmentAttrString attribute:NSAttachmentAttributeName
                    344:                                                                                           atIndex:0
                    345:                                                                                    effectiveRange:nil] attachmentCell];
                    346:         //Figure out which image to add, and set the cell's image.
                    347:         if ( [self outlineView:outlineView isItemExpandable:item] ) {
                    348:             [attachmentAttrStringCell setImage:[NSImage imageNamed:@"folder"]];
                    349:         } else {
1.17      macserv   350:             [attachmentAttrStringCell setImage:[NSImage imageNamed:@"smallbookmark"]];
1.11      macserv   351:         }
1.13      macserv   352:         //Insert the image
                    353:         [cellValue replaceCharactersInRange:NSMakeRange(0, 0) withAttributedString:attachmentAttrString];
                    354:         
                    355:         //Tweak the baseline to vertically center the text.
                    356:         [cellValue addAttribute:NSBaselineOffsetAttributeName
                    357:                           value:[NSNumber numberWithFloat:-3.0]
                    358:                           range:NSMakeRange(0, 1)];
1.1       hyatt     359:     }
1.13      macserv   360:     return cellValue;
1.1       hyatt     361: }
                    362: 
                    363: - (void)outlineView:(NSOutlineView *)outlineView setObjectValue:(id)object forTableColumn:(NSTableColumn *)tableColumn byItem:(id)item
                    364: {
                    365: }
                    366: 
1.5       hyatt     367: - (void)reloadDataForItem:(id)item reloadChildren: (BOOL)aReloadChildren
                    368: {
                    369:     printf("Reloading?\n");
                    370:     if (!item)
                    371:         [mOutlineView reloadData];
                    372:     else if ([mOutlineView isItemExpanded: item])
                    373:         [mOutlineView reloadItem: item reloadChildren: aReloadChildren];
                    374: }
                    375: 
1.1       hyatt     376: @end
                    377: 
                    378: @implementation BookmarkItem
                    379: -(nsIContent*)contentNode
                    380: {
                    381:     return mContentNode;
                    382: }
                    383: 
                    384: -(void)setContentNode: (nsIContent*)aContentNode
                    385: {
                    386:     mContentNode = aContentNode;
                    387: }
1.7       hyatt     388: 
                    389: - (id)copyWithZone:(NSZone *)aZone
                    390: {
                    391:     BookmarkItem* copy = [[[self class] allocWithZone: aZone] init];
                    392:     [copy setContentNode: mContentNode];
                    393:     return copy;
                    394: }
                    395: 
1.1       hyatt     396: @end
                    397: 
                    398: // Helper for stripping whitespace
                    399: static void
                    400: StripWhitespaceNodes(nsIContent* aElement)
                    401: {
                    402:     PRInt32 childCount;
                    403:     aElement->ChildCount(childCount);
                    404:     for (PRInt32 i = 0; i < childCount; i++) {
                    405:         nsCOMPtr<nsIContent> child;
                    406:         aElement->ChildAt(i, *getter_AddRefs(child));
                    407:         nsCOMPtr<nsITextContent> text = do_QueryInterface(child);
                    408:         if (text) {
                    409:             PRBool isEmpty;
                    410:             text->IsOnlyWhitespace(&isEmpty);
                    411:             if (isEmpty) {
                    412:                 // This node contained nothing but whitespace.
                    413:                 // Remove it from the content model.
                    414:                 aElement->RemoveChildAt(i, PR_TRUE);
                    415:                 i--; // Decrement our count, since we just removed this child.
                    416:                 childCount--; // Also decrement our total count.
                    417:             }
                    418:         }
                    419:         else StripWhitespaceNodes(child);
                    420:     }
                    421: }
                    422: 
                    423: PRUint32 BookmarksService::gRefCnt = 0;
1.4       hyatt     424: nsIDocument* BookmarksService::gBookmarks = nsnull;
                    425: NSMutableDictionary* BookmarksService::gDictionary = nil;
1.8       hyatt     426: MainController* BookmarksService::gMainController = nil;
1.19      hyatt     427: NSMenu* BookmarksService::gBookmarksMenu = nil;
1.22      hyatt     428: nsIDOMElement* BookmarksService::gToolbarRoot = nsnull;
1.1       hyatt     429: nsIAtom* BookmarksService::gFolderAtom = nsnull;
                    430: nsIAtom* BookmarksService::gBookmarkAtom = nsnull;
                    431: nsIAtom* BookmarksService::gHrefAtom = nsnull;
                    432: nsIAtom* BookmarksService::gNameAtom = nsnull;
1.5       hyatt     433: nsVoidArray* BookmarksService::gInstances = nsnull;
1.1       hyatt     434: 
1.5       hyatt     435: BookmarksService::BookmarksService(BookmarksDataSource* aDataSource)
1.1       hyatt     436: {
1.21      hyatt     437:   mDataSource = aDataSource;
                    438:   mToolbar = nil;
                    439: }
                    440: 
                    441: BookmarksService::BookmarksService(BookmarksToolbar* aToolbar)
                    442: {
                    443:   mDataSource = nil;
                    444:   mToolbar = aToolbar;
1.1       hyatt     445: }
                    446: 
                    447: BookmarksService::~BookmarksService()
                    448: {
                    449: }
                    450: 
                    451: void
                    452: BookmarksService::GetRootContent(nsIContent** aResult)
                    453: {
                    454:     *aResult = nsnull;
1.4       hyatt     455:     if (gBookmarks) {
1.1       hyatt     456:         nsCOMPtr<nsIDOMElement> elt;
1.4       hyatt     457:         nsCOMPtr<nsIDOMDocument> domDoc(do_QueryInterface(gBookmarks));
1.1       hyatt     458:         domDoc->GetDocumentElement(getter_AddRefs(elt));
                    459:         elt->QueryInterface(NS_GET_IID(nsIContent), (void**)aResult); // Addref happens here.
                    460:     }
                    461: }
                    462: 
                    463: BookmarkItem*
                    464: BookmarksService::GetWrapperFor(nsIContent* aContent)
                    465: {
1.4       hyatt     466:     if (!gDictionary)
                    467:         gDictionary = [[NSMutableDictionary alloc] initWithCapacity: 30];
1.1       hyatt     468:     
                    469:     PRUint32 contentID;
                    470:     aContent->GetContentID(&contentID);
                    471:     
1.4       hyatt     472:     BookmarkItem* item = [gDictionary objectForKey: [NSNumber numberWithInt: contentID]];
1.1       hyatt     473:     if (item)
                    474:         return item;
                    475:     else {
                    476:         // Create an item.
                    477:         item = [[[BookmarkItem alloc] init] autorelease]; // The dictionary retains us.
                    478:         [item setContentNode: aContent];
1.4       hyatt     479:         [gDictionary setObject: item forKey: [NSNumber numberWithInt: contentID]];
1.1       hyatt     480:     }
                    481:     return item;
                    482: }
                    483: 
1.19      hyatt     484: NSMenu*
                    485: BookmarksService::LocateMenu(nsIContent* aContent)
                    486: {
                    487:   nsCOMPtr<nsIContent> parent;
                    488:   aContent->GetParent(*getter_AddRefs(parent));
1.23      hyatt     489:   if (!parent) {
1.19      hyatt     490:     return BookmarksService::gBookmarksMenu;
1.23      hyatt     491:   }
                    492:   
1.19      hyatt     493:   NSMenu* parentMenu = LocateMenu(parent);
                    494:   
                    495:   PRUint32 contentID;
                    496:   aContent->GetContentID(&contentID);
                    497: 
                    498:   NSMenuItem* childMenu = [parentMenu itemWithTag: contentID];
1.23      hyatt     499:   return [childMenu submenu];
1.19      hyatt     500: }
                    501: 
1.5       hyatt     502: void
1.19      hyatt     503: BookmarksService::BookmarkAdded(nsIContent* aContainer, nsIContent* aChild)
1.4       hyatt     504: {
1.19      hyatt     505:   if (!gInstances || !gDictionary)
                    506:     return;
                    507: 
                    508:   PRInt32 count = gInstances->Count();
                    509:   for (PRInt32 i = 0; i < count; i++) {
                    510:     BookmarksService* instance = (BookmarksService*)gInstances->ElementAt(i);
                    511: 
                    512:     if (instance->mDataSource) {
                    513:       // We're a tree view.
                    514:       nsCOMPtr<nsIContent> parent;
                    515:       aContainer->GetParent(*getter_AddRefs(parent));
                    516: 
                    517:       BookmarkItem* item = nil;
                    518:       if (parent)
                    519:         // We're not the root.
                    520:         item = GetWrapperFor(aContainer);
                    521: 
                    522:       [(instance->mDataSource) reloadDataForItem: item reloadChildren: YES];
                    523:     }
1.23      hyatt     524:     else if (instance->mToolbar) {
                    525:       // We're a personal toolbar.
                    526:       nsCOMPtr<nsIDOMElement> parentElt(do_QueryInterface(aContainer));
                    527:       if (parentElt == gToolbarRoot) {
                    528:         // We only care about changes that occur to the personal toolbar's immediate
                    529:         // children.
                    530:         PRInt32 index = -1;
                    531:         aContainer->IndexOf(aChild, index);
                    532:         nsCOMPtr<nsIDOMElement> elt(do_QueryInterface(aChild));
                    533:         [(instance->mToolbar) addButton: elt atIndex: index];
                    534:       }
                    535:     }
1.19      hyatt     536:     else {
                    537:       // We're the menu.
                    538:       PRInt32 index = -1;
1.23      hyatt     539:       aContainer->IndexOf(aChild, index);
1.19      hyatt     540:       NSMenu* menu = LocateMenu(aContainer);
                    541:       AddMenuBookmark(menu, aContainer, aChild, index);
1.5       hyatt     542:     }
1.19      hyatt     543:   }
                    544:   
                    545:   FlushBookmarks();  
1.4       hyatt     546: }
                    547: 
1.19      hyatt     548: void
                    549: BookmarksService::BookmarkChanged(nsIContent* aItem)
                    550: {
                    551:   if (!gInstances || !gDictionary)
                    552:     return;
                    553: 
                    554:   PRInt32 count = gInstances->Count();
                    555:   for (PRInt32 i = 0; i < count; i++) {
                    556:     BookmarksService* instance = (BookmarksService*)gInstances->ElementAt(i);
                    557:    
                    558:     if (instance->mDataSource) {
                    559:       BookmarkItem* item = GetWrapperFor(aItem);
                    560:       [(instance->mDataSource) reloadDataForItem: item reloadChildren: NO];
                    561:     }
                    562:   }
                    563: 
                    564:   FlushBookmarks();  
                    565: }
1.4       hyatt     566: 
1.5       hyatt     567: void
1.19      hyatt     568: BookmarksService::BookmarkRemoved(nsIContent* aContainer, nsIContent* aChild)
1.4       hyatt     569: {
1.19      hyatt     570:   if (!gInstances)
                    571:     return;
                    572: 
                    573:   PRInt32 count = gInstances->Count();
                    574:   for (PRInt32 i = 0; i < count; i++) {
                    575:     BookmarksService* instance = (BookmarksService*)gInstances->ElementAt(i);
                    576: 
                    577:     if (instance->mDataSource) {
                    578:       // We're a tree view.
                    579:       nsCOMPtr<nsIContent> parent;
                    580:       aContainer->GetParent(*getter_AddRefs(parent));
                    581: 
                    582:       BookmarkItem* item = nil;
                    583:       if (parent)
1.5       hyatt     584:         // We're not the root.
                    585:         item = GetWrapperFor(aContainer);
1.19      hyatt     586: 
                    587:       [(instance->mDataSource) reloadDataForItem: item reloadChildren: YES];
                    588:     }
1.23      hyatt     589:     else if (instance->mToolbar) {
                    590:       // We're a personal toolbar.
                    591:       nsCOMPtr<nsIDOMElement> parentElt(do_QueryInterface(aContainer));
                    592:       if (parentElt == gToolbarRoot) {
                    593:         // We only care about changes that occur to the personal toolbar's immediate
                    594:         // children.
                    595:         nsCOMPtr<nsIDOMElement> childElt(do_QueryInterface(aChild));
                    596:         [(instance->mToolbar) removeButton: childElt];
                    597:       }
                    598:     }    
1.19      hyatt     599:     else {
                    600:       // We're the menu.
                    601:       NSMenu* menu = LocateMenu(aContainer);
                    602:       PRUint32 contentID;
                    603:       aChild->GetContentID(&contentID);
                    604:       NSMenuItem* childItem = [menu itemWithTag: contentID];
                    605:       [menu removeItem: childItem];
                    606:     }
                    607:   }
                    608: 
                    609:   FlushBookmarks(); 
1.4       hyatt     610: }
                    611: 
                    612: void
                    613: BookmarksService::AddObserver()
1.1       hyatt     614: {
                    615:     gRefCnt++;
                    616:     if (gRefCnt == 1) {
                    617:         gBookmarkAtom = NS_NewAtom("bookmark");
                    618:         gFolderAtom = NS_NewAtom("folder");
                    619:         gNameAtom = NS_NewAtom("name");
                    620:         gHrefAtom = NS_NewAtom("href");
1.5       hyatt     621:         gInstances = new nsVoidArray();
1.8       hyatt     622:                 
1.4       hyatt     623:         nsCOMPtr<nsIFile> profileDir;
                    624:         NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR, getter_AddRefs(profileDir));
                    625:         profileDir->Append("bookmarks.xml");
                    626:     
1.15      hyatt     627:         nsCAutoString bookmarksFileURL;
                    628:         NS_GetURLSpecFromFile(profileDir, bookmarksFileURL);
1.4       hyatt     629:         
                    630:         nsCOMPtr<nsIURI> uri;
                    631:         NS_NewURI(getter_AddRefs(uri), bookmarksFileURL.get());
                    632:     
                    633:         nsCOMPtr<nsIXBLService> xblService(do_GetService("@mozilla.org/xbl;1"));    
                    634:         xblService->FetchSyncXMLDocument(uri, &gBookmarks); // The addref is here.
                    635:         
                    636:         nsCOMPtr<nsIContent> rootNode;
                    637:         GetRootContent(getter_AddRefs(rootNode));
                    638:         StripWhitespaceNodes(rootNode);
1.1       hyatt     639:     }
                    640:     
1.5       hyatt     641:     gInstances->AppendElement(this);
1.1       hyatt     642: }
                    643: 
                    644: void
1.4       hyatt     645: BookmarksService::RemoveObserver()
1.1       hyatt     646: {
                    647:     if (gRefCnt == 0)
                    648:         return;
1.4       hyatt     649:  
1.5       hyatt     650:     gInstances->RemoveElement(this);
                    651:      
1.1       hyatt     652:     gRefCnt--;
                    653:     if (gRefCnt == 0) {
1.4       hyatt     654:         NS_IF_RELEASE(gBookmarks);
1.1       hyatt     655:         NS_RELEASE(gBookmarkAtom);
                    656:         NS_RELEASE(gFolderAtom);
                    657:         NS_RELEASE(gNameAtom);
                    658:         NS_RELEASE(gHrefAtom);
1.4       hyatt     659:         [gDictionary release];
1.1       hyatt     660:     }
                    661: }
                    662: 
1.6       hyatt     663: void
                    664: BookmarksService::FlushBookmarks()
                    665: {
                    666:     nsCOMPtr<nsIFile> bookmarksFile;
                    667:     NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR, getter_AddRefs(bookmarksFile));
                    668:     bookmarksFile->Append("bookmarks.xml");
                    669: 
                    670:     nsCOMPtr<nsIOutputStream> outputStream;
                    671:     NS_NewLocalFileOutputStream(getter_AddRefs(outputStream), bookmarksFile);
                    672: 
                    673:     nsCOMPtr<nsIDOMDocument> domDoc(do_QueryInterface(gBookmarks));
                    674:     
                    675:     nsCOMPtr<nsIDOMSerializer> domSerializer(do_CreateInstance(NS_XMLSERIALIZER_CONTRACTID));
                    676:     domSerializer->SerializeToStream(domDoc, outputStream, nsnull);
1.21      hyatt     677: }
                    678: 
1.22      hyatt     679: void BookmarksService::EnsureToolbarRoot()
1.21      hyatt     680: {
1.22      hyatt     681:   if (gToolbarRoot)
                    682:     return;
                    683: 
1.21      hyatt     684:   nsCOMPtr<nsIDOMDocument> domDoc(do_QueryInterface(gBookmarks));
1.22      hyatt     685:   nsCOMPtr<nsIDOMElement> rootElt;
                    686:   domDoc->GetDocumentElement(getter_AddRefs(rootElt));
1.21      hyatt     687:   
1.22      hyatt     688:   nsCOMPtr<nsIDOMNode> child;
                    689:   rootElt->GetFirstChild(getter_AddRefs(child));
                    690:   nsAutoString typeValue;
                    691:   while (child) {
                    692:     nsCOMPtr<nsIDOMElement> childElt(do_QueryInterface(child));
                    693:     if (childElt) {
                    694:       childElt->GetAttribute(NS_LITERAL_STRING("type"), typeValue);
                    695:       if (typeValue.Equals(NS_LITERAL_STRING("toolbar")))
                    696:         gToolbarRoot = childElt;
                    697:     }
                    698:     
                    699:     nsCOMPtr<nsIDOMNode> temp;
                    700:     child->GetNextSibling(getter_AddRefs(temp));
                    701:     child = temp;
                    702:   }
                    703: 
                    704:   if (!gToolbarRoot) {
                    705:     printf("Repairing personal toolbar.\n");
                    706:     nsCOMPtr<nsIDOMElement> elt;
                    707:     domDoc->CreateElementNS(NS_LITERAL_STRING("http://chimera.mozdev.org/bookmarks/"),
                    708:                             NS_LITERAL_STRING("folder"),
                    709:                             getter_AddRefs(elt));
                    710: 
                    711:     elt->SetAttribute(NS_LITERAL_STRING("name"), NS_LITERAL_STRING("Toolbar Bookmarks"));
                    712:     elt->SetAttribute(NS_LITERAL_STRING("type"), NS_LITERAL_STRING("toolbar"));
                    713: 
                    714:     nsCOMPtr<nsIDOMNode> dummy;
                    715:     rootElt->AppendChild(elt, getter_AddRefs(dummy));
                    716:     gToolbarRoot = elt;
                    717:   }
1.7       hyatt     718: }
                    719: 
                    720: void
                    721: BookmarksService::ConstructBookmarksMenu(NSMenu* aMenu, nsIContent* aContent)
                    722: {
                    723:     nsCOMPtr<nsIContent> content = aContent;
                    724:     if (!content) {
                    725:         GetRootContent(getter_AddRefs(content));
1.8       hyatt     726:         GetWrapperFor(content);
1.19      hyatt     727:         gBookmarksMenu = aMenu;
1.7       hyatt     728:     }
                    729:     
                    730:     // Now walk our children, and for folders also recur into them.
                    731:     PRInt32 childCount;
                    732:     content->ChildCount(childCount);
                    733:     
                    734:     for (PRInt32 i = 0; i < childCount; i++) {
1.19      hyatt     735:       nsCOMPtr<nsIContent> child;
                    736:       content->ChildAt(i, *getter_AddRefs(child));
                    737:       AddMenuBookmark(aMenu, content, child, -1);
1.7       hyatt     738:     }
1.19      hyatt     739: }
                    740: 
                    741: void
                    742: BookmarksService::AddMenuBookmark(NSMenu* aMenu, nsIContent* aParent, nsIContent* aChild, PRInt32 aIndex)
                    743: {
                    744:   nsAutoString name;
                    745:   aChild->GetAttr(kNameSpaceID_None, gNameAtom, name);
                    746:   nsCAutoString nameCStr; nameCStr.AssignWithConversion(name);
                    747:   NSString* title = [NSString stringWithCString: nameCStr.get()];
                    748: 
                    749:   // Create a menu or menu item for the child.
                    750:   NSMenuItem* menuItem = [[[NSMenuItem alloc] initWithTitle: title action: NULL keyEquivalent: @""] autorelease];
                    751:   GetWrapperFor(aChild);
                    752: 
                    753:   if (aIndex == -1)
                    754:     [aMenu addItem: menuItem];
                    755:   else
                    756:     [aMenu insertItem: menuItem atIndex: aIndex];
                    757:   
                    758:   nsCOMPtr<nsIAtom> tagName;
                    759:   aChild->GetTag(*getter_AddRefs(tagName));
                    760: 
                    761:   if (tagName == gFolderAtom) {
                    762:     NSMenu* menu = [[[NSMenu alloc] initWithTitle: title] autorelease];
                    763:     [aMenu setSubmenu: menu forItem: menuItem];
                    764:     [menu setAutoenablesItems: NO];
                    765:     ConstructBookmarksMenu(menu, aChild);
                    766:   }
                    767:   else {
                    768:     [menuItem setTarget: gMainController];
                    769:     [menuItem setAction: @selector(openMenuBookmark:)];
                    770:   }
                    771: 
                    772:   PRUint32 contentID;
                    773:   aChild->GetContentID(&contentID);
                    774:   [menuItem setTag: contentID];
1.6       hyatt     775: }
1.8       hyatt     776: 
                    777: void 
                    778: BookmarksService::OpenMenuBookmark(BrowserWindowController* aController, id aMenuItem)
                    779: {
                    780:     // Get the corresponding bookmark item.
                    781:     BookmarkItem* item = [gDictionary objectForKey: [NSNumber numberWithInt: [aMenuItem tag]]];
                    782:         
                    783:     // Get the content node.
                    784:     nsIContent* content = [item contentNode];
                    785:         
                    786:     // Get the href attribute.  This is the URL we want to load.
                    787:     nsAutoString href;
                    788:     content->GetAttr(kNameSpaceID_None, gHrefAtom, href);
                    789:     nsCAutoString cref; cref.AssignWithConversion(href);
                    790:     if (cref.IsEmpty())
                    791:         return;
                    792:         
                    793:     NSString* url = [NSString stringWithCString: cref.get()];
                    794:     
                    795:     // Now load the URL in the window.
                    796:     [aController loadURL:[NSURL URLWithString: url]];
1.18      hyatt     797:     
                    798:     // Focus and activate our content area.
                    799:     [[[aController getMyBrowserView] getBrowserView] setActive: YES];
1.8       hyatt     800: }

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