File:  [mozdev] / chimera / BookmarksService.mm
Revision 1.15: download - view: text, annotated - select for diffs - revision graph
Thu Mar 7 03:18:55 2002 UTC (17 years, 3 months ago) by hyatt
Branches: MAIN
CVS tags: HEAD
Recover from darin's URL landing.

    1: /*
    2:  *  BookmarksService.cpp
    3:  *  Chimera
    4:  *
    5:  *  Created by David Hyatt on Thu Feb 07 2002.
    6:  *  Copyright (c) 2001 __MyCompanyName__. All rights reserved.
    7:  *
    8:  */
    9: 
   10: #import "NSBrowserView.h"
   11: #include "BookmarksService.h"
   12: #include "nsIDocument.h"
   13: #include "nsIContent.h"
   14: #include "nsIAtom.h"
   15: #include "nsITextContent.h"
   16: #include "nsIDOMWindow.h"
   17: #include "nsIDOMHTMLDocument.h"
   18: #include "nsIDOMElement.h"
   19: #include "nsString.h"
   20: #include "nsIFile.h"
   21: #include "nsAppDirectoryServiceDefs.h"
   22: #include "nsIXMLHttpRequest.h"
   23: #include "nsIDOMSerializer.h"
   24: #include "nsNetUtil.h"
   25: #include "nsINamespaceManager.h"
   26: #include "nsIXBLService.h"
   27: #include "nsIWebBrowser.h"
   28: 
   29: @implementation BookmarksDataSource
   30: 
   31: -(id) init
   32: {
   33:     [super init];
   34:     mBookmarks = nsnull;
   35:     return self;
   36: }
   37: 
   38: -(void) dealloc
   39: {
   40:     [super dealloc];
   41: }
   42: 
   43: -(void) windowClosing
   44: {
   45:     if (mBookmarks) {
   46:         mBookmarks->RemoveObserver();
   47:         delete mBookmarks;
   48:     }
   49: }
   50: 
   51: -(void) ensureBookmarks
   52: {
   53:     if (mBookmarks)
   54:         return;
   55:     
   56:     mBookmarks = new BookmarksService(self);
   57:     mBookmarks->AddObserver();
   58:     
   59:     [mOutlineView setTarget: self];
   60:     [mOutlineView setDoubleAction: @selector(openBookmark:)];
   61:     [mOutlineView reloadData];
   62: }
   63: 
   64: -(IBAction)addBookmark:(id)aSender
   65: {
   66:     if (!mBookmarks)
   67:         return;
   68:     
   69:     nsCOMPtr<nsIContent> content;
   70:     int index = [mOutlineView selectedRow];
   71:    
   72:     if (index >= 0) {
   73:         BookmarkItem* item = [mOutlineView itemAtRow: index];
   74:         if ([mOutlineView isExpandable: item]) 
   75:             content = [item contentNode];
   76:     }
   77:     
   78:     if (!content)
   79:         mBookmarks->GetRootContent(getter_AddRefs(content));
   80:         
   81:     nsCOMPtr<nsIDOMDocument> domDoc(do_QueryInterface(mBookmarks->gBookmarks));
   82:     nsCOMPtr<nsIDOMElement> elt;
   83:     domDoc->CreateElementNS(NS_LITERAL_STRING("http://chimera.mozdev.org/bookmarks"), 
   84:                             NS_LITERAL_STRING("bookmark"), 
   85:                             getter_AddRefs(elt));
   86:     
   87:     // Fetch the title of the current page and the URL.
   88:     nsCOMPtr<nsIWebBrowser> webBrowser = getter_AddRefs([[[mBrowserWindowController getMyBrowserView] getBrowserView] getWebBrowser]);
   89:     nsCOMPtr<nsIDOMWindow> window;
   90:     webBrowser->GetContentDOMWindow(getter_AddRefs(window));
   91:     nsCOMPtr<nsIDOMDocument> htmlDoc;
   92:     window->GetDocument(getter_AddRefs(htmlDoc));
   93:     nsCOMPtr<nsIDocument> pageDoc(do_QueryInterface(htmlDoc));
   94:     
   95:     nsAutoString href; 
   96:     if (pageDoc) {
   97:         nsCOMPtr<nsIURI> url;
   98:         pageDoc->GetDocumentURL(getter_AddRefs(url));
   99:         nsCAutoString spec;
  100:         url->GetSpec(spec);
  101:         href.AssignWithConversion(spec.get());
  102:     }
  103:     
  104:     nsAutoString title;
  105:     nsCOMPtr<nsIDOMHTMLDocument> htmlDocument(do_QueryInterface(htmlDoc));
  106:     if (htmlDocument)
  107:         htmlDocument->GetTitle(title);
  108:     if (title.IsEmpty())
  109:         title = href;
  110:         
  111:     elt->SetAttribute(NS_LITERAL_STRING("name"), title);
  112:     elt->SetAttribute(NS_LITERAL_STRING("href"), href);    
  113:     
  114:     nsCOMPtr<nsIDOMElement> parent(do_QueryInterface(content));
  115:     nsCOMPtr<nsIDOMNode> dummy;
  116:     parent->AppendChild(elt, getter_AddRefs(dummy));
  117: 
  118:     mBookmarks->NotifyObservers(content, PR_TRUE);
  119: }
  120: 
  121: -(IBAction)deleteBookmark: (id)aSender
  122: {
  123:     if (!mBookmarks)
  124:         return;
  125:     
  126:     int index = [mOutlineView selectedRow];
  127:     if (index == -1)
  128:         return;
  129:         
  130:     BookmarkItem* item = [mOutlineView itemAtRow: index];
  131:     nsCOMPtr<nsIContent> content = [item contentNode];
  132:     nsCOMPtr<nsIDOMElement> child(do_QueryInterface(content));
  133:     nsCOMPtr<nsIDOMNode> parent;
  134:     child->GetParentNode(getter_AddRefs(parent));
  135:     nsCOMPtr<nsIDOMNode> dummy;
  136:     parent->RemoveChild(child, getter_AddRefs(dummy));
  137:     nsCOMPtr<nsIContent> parentContent(do_QueryInterface(parent));
  138:     mBookmarks->NotifyObservers(parentContent, PR_TRUE);
  139:     
  140:     int total = [mOutlineView numberOfRows];
  141:     if (index == total)
  142:         index--;
  143:         
  144:     [mOutlineView selectRow: index byExtendingSelection: NO];
  145: }
  146: 
  147: -(IBAction)openBookmark: (id)aSender
  148: {
  149:     int index = [mOutlineView selectedRow];
  150:     if (index == -1)
  151:         return;
  152:     
  153:     id item = [mOutlineView itemAtRow: index];
  154:     if (!item)
  155:         return;
  156:         
  157:     if ([mOutlineView isExpandable: item]) {
  158:         if ([mOutlineView isItemExpanded: item])
  159:             [mOutlineView collapseItem: item];
  160:         else
  161:             [mOutlineView expandItem: item];
  162:     }
  163:     else {
  164:         nsIContent* content = [item contentNode];
  165:         nsAutoString href;
  166:         content->GetAttr(kNameSpaceID_None, BookmarksService::gHrefAtom, href);
  167:         if (!href.IsEmpty()) {
  168:             nsCAutoString cstr; cstr.AssignWithConversion(href);
  169:             NSString* url = [NSString stringWithCString: cstr.get()];
  170:             [[[mBrowserWindowController getMyBrowserView] getBrowserView] loadURI:[NSURL URLWithString: url] flags:NSLoadFlagsNone];
  171:         }
  172:     } 
  173: }
  174: 
  175: - (BOOL)outlineView:(NSOutlineView *)outlineView shouldEditTableColumn:(NSTableColumn *)tableColumn item:(id)item
  176: {
  177:     return NO;
  178: }
  179: 
  180: - (id)outlineView:(NSOutlineView *)outlineView child:(int)index ofItem:(id)item
  181: {
  182:     if (!mBookmarks)
  183:         return nil;
  184:        
  185:     nsCOMPtr<nsIContent> content;
  186:     if (!item)
  187:         mBookmarks->GetRootContent(getter_AddRefs(content));
  188:     else
  189:         content = [item contentNode];
  190:     
  191:     nsCOMPtr<nsIContent> child;
  192:     content->ChildAt(index, *getter_AddRefs(child));
  193:     return mBookmarks->GetWrapperFor(child);
  194: }
  195: 
  196: - (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item
  197: {
  198:     if (!mBookmarks)
  199:         return NO;
  200:     
  201:     if (!item)
  202:         return YES; // The root node is always open.
  203:     
  204:     nsCOMPtr<nsIAtom> tagName;
  205:     nsIContent* content = [item contentNode];
  206:     content->GetTag(*getter_AddRefs(tagName));
  207:     
  208:     return (tagName == BookmarksService::gFolderAtom);
  209: }
  210: 
  211: - (int)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item
  212: {
  213:     if (!mBookmarks)
  214:         return 0;
  215:   
  216:     nsCOMPtr<nsIContent> content;
  217:     if (!item)
  218:         mBookmarks->GetRootContent(getter_AddRefs(content));
  219:     else 
  220:         content = [item contentNode];
  221:     
  222:     PRInt32 childCount;
  223:     content->ChildCount(childCount);
  224:     
  225:     return childCount;
  226: }
  227: 
  228: - (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item
  229: {
  230:     NSString 					*columnName = [tableColumn identifier];
  231:     NSMutableAttributedString 	*cellValue = [[NSMutableAttributedString alloc] init];
  232:     NSFileWrapper				*fileWrapper = [[NSFileWrapper alloc] initRegularFileWithContents:nil];
  233:     NSTextAttachment			*textAttachment = [[NSTextAttachment alloc] initWithFileWrapper:fileWrapper];
  234:     NSMutableAttributedString   *attachmentAttrString = nil;
  235:     NSCell 						*attachmentAttrStringCell;
  236: 
  237:     if ([columnName isEqualToString: @"name"]) {
  238:         nsIContent* content = [item contentNode];
  239:         nsAutoString nameAttr;
  240:         content->GetAttr(kNameSpaceID_None, BookmarksService::gNameAtom, nameAttr);
  241:         nsCAutoString cStr; cStr.AssignWithConversion(nameAttr);
  242:         
  243:         //Set cell's textual contents
  244:         [cellValue replaceCharactersInRange:NSMakeRange(0, [cellValue length])
  245:                                  withString:[NSString stringWithCString: cStr.get()]];
  246:         
  247:         //Create an attributed string to hold the empty attachment, then release the components.
  248:         attachmentAttrString = [[NSMutableAttributedString attributedStringWithAttachment:textAttachment] retain];
  249:         [textAttachment release];
  250:         [fileWrapper release];
  251: 
  252:         //Get the cell of the text attachment.
  253:         attachmentAttrStringCell = (NSCell *)[(NSTextAttachment *)[attachmentAttrString attribute:NSAttachmentAttributeName
  254:                                                                                           atIndex:0
  255:                                                                                    effectiveRange:nil] attachmentCell];
  256:         //Figure out which image to add, and set the cell's image.
  257:         if ( [self outlineView:outlineView isItemExpandable:item] ) {
  258:             [attachmentAttrStringCell setImage:[NSImage imageNamed:@"folder"]];
  259:         } else {
  260:             [attachmentAttrStringCell setImage:[NSImage imageNamed:@"smallDocument"]];
  261:         }
  262:         //Insert the image
  263:         [cellValue replaceCharactersInRange:NSMakeRange(0, 0) withAttributedString:attachmentAttrString];
  264:         
  265:         //Tweak the baseline to vertically center the text.
  266:         [cellValue addAttribute:NSBaselineOffsetAttributeName
  267:                           value:[NSNumber numberWithFloat:-3.0]
  268:                           range:NSMakeRange(0, 1)];
  269:     }
  270:     return cellValue;
  271: }
  272: 
  273: - (void)outlineView:(NSOutlineView *)outlineView setObjectValue:(id)object forTableColumn:(NSTableColumn *)tableColumn byItem:(id)item
  274: {
  275: }
  276: 
  277: - (void)reloadDataForItem:(id)item reloadChildren: (BOOL)aReloadChildren
  278: {
  279:     printf("Reloading?\n");
  280:     if (!item)
  281:         [mOutlineView reloadData];
  282:     else if ([mOutlineView isItemExpanded: item])
  283:         [mOutlineView reloadItem: item reloadChildren: aReloadChildren];
  284: }
  285: 
  286: @end
  287: 
  288: @implementation BookmarkItem
  289: -(nsIContent*)contentNode
  290: {
  291:     return mContentNode;
  292: }
  293: 
  294: -(void)setContentNode: (nsIContent*)aContentNode
  295: {
  296:     mContentNode = aContentNode;
  297: }
  298: 
  299: - (id)copyWithZone:(NSZone *)aZone
  300: {
  301:     BookmarkItem* copy = [[[self class] allocWithZone: aZone] init];
  302:     [copy setContentNode: mContentNode];
  303:     return copy;
  304: }
  305: 
  306: @end
  307: 
  308: // Helper for stripping whitespace
  309: static void
  310: StripWhitespaceNodes(nsIContent* aElement)
  311: {
  312:     PRInt32 childCount;
  313:     aElement->ChildCount(childCount);
  314:     for (PRInt32 i = 0; i < childCount; i++) {
  315:         nsCOMPtr<nsIContent> child;
  316:         aElement->ChildAt(i, *getter_AddRefs(child));
  317:         nsCOMPtr<nsITextContent> text = do_QueryInterface(child);
  318:         if (text) {
  319:             PRBool isEmpty;
  320:             text->IsOnlyWhitespace(&isEmpty);
  321:             if (isEmpty) {
  322:                 // This node contained nothing but whitespace.
  323:                 // Remove it from the content model.
  324:                 aElement->RemoveChildAt(i, PR_TRUE);
  325:                 i--; // Decrement our count, since we just removed this child.
  326:                 childCount--; // Also decrement our total count.
  327:             }
  328:         }
  329:         else StripWhitespaceNodes(child);
  330:     }
  331: }
  332: 
  333: PRUint32 BookmarksService::gRefCnt = 0;
  334: nsIDocument* BookmarksService::gBookmarks = nsnull;
  335: NSMutableDictionary* BookmarksService::gDictionary = nil;
  336: MainController* BookmarksService::gMainController = nil;
  337: nsIAtom* BookmarksService::gFolderAtom = nsnull;
  338: nsIAtom* BookmarksService::gBookmarkAtom = nsnull;
  339: nsIAtom* BookmarksService::gHrefAtom = nsnull;
  340: nsIAtom* BookmarksService::gNameAtom = nsnull;
  341: nsVoidArray* BookmarksService::gInstances = nsnull;
  342: 
  343: BookmarksService::BookmarksService(BookmarksDataSource* aDataSource)
  344: {
  345:     mDataSource = aDataSource;
  346: }
  347: 
  348: BookmarksService::~BookmarksService()
  349: {
  350: }
  351: 
  352: void
  353: BookmarksService::GetRootContent(nsIContent** aResult)
  354: {
  355:     *aResult = nsnull;
  356:     if (gBookmarks) {
  357:         nsCOMPtr<nsIDOMElement> elt;
  358:         nsCOMPtr<nsIDOMDocument> domDoc(do_QueryInterface(gBookmarks));
  359:         domDoc->GetDocumentElement(getter_AddRefs(elt));
  360:         elt->QueryInterface(NS_GET_IID(nsIContent), (void**)aResult); // Addref happens here.
  361:     }
  362: }
  363: 
  364: BookmarkItem*
  365: BookmarksService::GetWrapperFor(nsIContent* aContent)
  366: {
  367:     if (!gDictionary)
  368:         gDictionary = [[NSMutableDictionary alloc] initWithCapacity: 30];
  369:     
  370:     PRUint32 contentID;
  371:     aContent->GetContentID(&contentID);
  372:     
  373:     BookmarkItem* item = [gDictionary objectForKey: [NSNumber numberWithInt: contentID]];
  374:     if (item)
  375:         return item;
  376:     else {
  377:         // Create an item.
  378:         item = [[[BookmarkItem alloc] init] autorelease]; // The dictionary retains us.
  379:         [item setContentNode: aContent];
  380:         [gDictionary setObject: item forKey: [NSNumber numberWithInt: contentID]];
  381:     }
  382:     return item;
  383: }
  384: 
  385: void
  386: BookmarksService::NotifyObservers(nsIContent* aContainer, PRBool aReloadChildren)
  387: {
  388:     if (!gInstances)
  389:         return;
  390:     
  391:     PRInt32 count = gInstances->Count();
  392:     for (PRInt32 i = 0; i < count; i++) {
  393:         BookmarksService* instance = (BookmarksService*)gInstances->ElementAt(i);
  394:         instance->NotifyObserver(aContainer, aReloadChildren);
  395:     }
  396:     
  397:     FlushBookmarks();
  398: }
  399: 
  400: 
  401: void
  402: BookmarksService::NotifyObserver(nsIContent* aContainer, PRBool aReloadChildren)
  403: {
  404:     if (!gDictionary)
  405:         return;
  406:    
  407:     nsCOMPtr<nsIContent> parent;
  408:     aContainer->GetParent(*getter_AddRefs(parent));
  409:     
  410:     BookmarkItem* item = nil;
  411:     if (parent)
  412:         // We're not the root.
  413:         item = GetWrapperFor(aContainer);
  414:     
  415:     [mDataSource reloadDataForItem: item reloadChildren: aReloadChildren];
  416: }
  417: 
  418: void
  419: BookmarksService::AddObserver()
  420: {
  421:     gRefCnt++;
  422:     if (gRefCnt == 1) {
  423:         gBookmarkAtom = NS_NewAtom("bookmark");
  424:         gFolderAtom = NS_NewAtom("folder");
  425:         gNameAtom = NS_NewAtom("name");
  426:         gHrefAtom = NS_NewAtom("href");
  427:         gInstances = new nsVoidArray();
  428:                 
  429:         nsCOMPtr<nsIFile> profileDir;
  430:         NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR, getter_AddRefs(profileDir));
  431:         profileDir->Append("bookmarks.xml");
  432:     
  433:         nsCAutoString bookmarksFileURL;
  434:         NS_GetURLSpecFromFile(profileDir, bookmarksFileURL);
  435:         
  436:         nsCOMPtr<nsIURI> uri;
  437:         NS_NewURI(getter_AddRefs(uri), bookmarksFileURL.get());
  438:     
  439:         nsCOMPtr<nsIXBLService> xblService(do_GetService("@mozilla.org/xbl;1"));    
  440:         xblService->FetchSyncXMLDocument(uri, &gBookmarks); // The addref is here.
  441:         
  442:         nsCOMPtr<nsIContent> rootNode;
  443:         GetRootContent(getter_AddRefs(rootNode));
  444:         StripWhitespaceNodes(rootNode);
  445:     }
  446:     
  447:     gInstances->AppendElement(this);
  448: }
  449: 
  450: void
  451: BookmarksService::RemoveObserver()
  452: {
  453:     if (gRefCnt == 0)
  454:         return;
  455:  
  456:     gInstances->RemoveElement(this);
  457:      
  458:     gRefCnt--;
  459:     if (gRefCnt == 0) {
  460:         NS_IF_RELEASE(gBookmarks);
  461:         NS_RELEASE(gBookmarkAtom);
  462:         NS_RELEASE(gFolderAtom);
  463:         NS_RELEASE(gNameAtom);
  464:         NS_RELEASE(gHrefAtom);
  465:         [gDictionary release];
  466:     }
  467: }
  468: 
  469: void
  470: BookmarksService::FlushBookmarks()
  471: {
  472:     nsCOMPtr<nsIFile> bookmarksFile;
  473:     NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR, getter_AddRefs(bookmarksFile));
  474:     bookmarksFile->Append("bookmarks.xml");
  475: 
  476:     nsCOMPtr<nsIOutputStream> outputStream;
  477:     NS_NewLocalFileOutputStream(getter_AddRefs(outputStream), bookmarksFile);
  478: 
  479:     nsCOMPtr<nsIDOMDocument> domDoc(do_QueryInterface(gBookmarks));
  480:     
  481:     nsCOMPtr<nsIDOMSerializer> domSerializer(do_CreateInstance(NS_XMLSERIALIZER_CONTRACTID));
  482:     domSerializer->SerializeToStream(domDoc, outputStream, nsnull);
  483: }
  484: 
  485: void
  486: BookmarksService::ConstructBookmarksMenu(NSMenu* aMenu, nsIContent* aContent)
  487: {
  488:     nsCOMPtr<nsIContent> content = aContent;
  489:     if (!content) {
  490:         GetRootContent(getter_AddRefs(content));
  491:         GetWrapperFor(content);
  492:     }
  493:     
  494:     // Now walk our children, and for folders also recur into them.
  495:     PRInt32 childCount;
  496:     content->ChildCount(childCount);
  497:     
  498:     for (PRInt32 i = 0; i < childCount; i++) {
  499:         nsCOMPtr<nsIContent> child;
  500:         content->ChildAt(i, *getter_AddRefs(child));
  501:         
  502:         // Obtain our name attribute.
  503:         nsAutoString name;
  504:         child->GetAttr(kNameSpaceID_None, gNameAtom, name);
  505:         nsCAutoString nameCStr; nameCStr.AssignWithConversion(name);
  506:         NSString* title = [NSString stringWithCString: nameCStr.get()];
  507:                 
  508:         // Create a menu or menu item for the child.
  509:         NSMenuItem* menuItem = [[[NSMenuItem alloc] initWithTitle: title action: NULL keyEquivalent: @""] autorelease];
  510:         GetWrapperFor(child);
  511:         [aMenu addItem: menuItem];
  512:         
  513:         nsCOMPtr<nsIAtom> tag;
  514:         child->GetTag(*getter_AddRefs(tag));
  515:         
  516:         if (tag == gFolderAtom) {
  517:             NSMenu* menu = [[[NSMenu alloc] initWithTitle: title] autorelease];
  518:             [aMenu setSubmenu: menu forItem: menuItem];
  519:             [menu setAutoenablesItems: NO];
  520:             ConstructBookmarksMenu(menu, child);
  521:         }
  522:         else {
  523:             [menuItem setTarget: gMainController];
  524:             [menuItem setAction: @selector(openMenuBookmark:)];
  525:         }
  526:         
  527:         PRUint32 contentID;
  528:         child->GetContentID(&contentID);
  529:         [menuItem setTag: contentID];
  530:     }
  531: }
  532: 
  533: void 
  534: BookmarksService::OpenMenuBookmark(BrowserWindowController* aController, id aMenuItem)
  535: {
  536:     // Get the corresponding bookmark item.
  537:     BookmarkItem* item = [gDictionary objectForKey: [NSNumber numberWithInt: [aMenuItem tag]]];
  538:         
  539:     // Get the content node.
  540:     nsIContent* content = [item contentNode];
  541:         
  542:     // Get the href attribute.  This is the URL we want to load.
  543:     nsAutoString href;
  544:     content->GetAttr(kNameSpaceID_None, gHrefAtom, href);
  545:     nsCAutoString cref; cref.AssignWithConversion(href);
  546:     if (cref.IsEmpty())
  547:         return;
  548:         
  549:     NSString* url = [NSString stringWithCString: cref.get()];
  550:     
  551:     // Now load the URL in the window.
  552:     [aController loadURL:[NSURL URLWithString: url]];
  553: }

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