File:  [mozdev] / chimera / BookmarksService.mm
Revision 1.4: download - view: text, annotated - select for diffs - revision graph
Sat Feb 9 01:52:27 2002 UTC (17 years, 3 months ago) by hyatt
Branches: MAIN
CVS tags: HEAD
Preparing for bookmark notifications.

    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 "nsIDOMDocument.h"
   17: #include "nsIDOMElement.h"
   18: #include "nsString.h"
   19: #include "nsIFile.h"
   20: #include "nsAppDirectoryServiceDefs.h"
   21: #include "nsIXMLHttpRequest.h"
   22: #include "nsNetUtil.h"
   23: #include "nsINamespaceManager.h"
   24: #include "nsIXBLService.h"
   25: 
   26: @implementation BookmarksDataSource
   27: 
   28: -(id) init
   29: {
   30:     [super init];
   31:     mBookmarks = nsnull;
   32:     return self;
   33: }
   34: 
   35: -(void) dealloc
   36: {
   37:     [super dealloc];
   38:     if (mBookmarks)
   39:         mBookmarks->RemoveObserver();
   40: }
   41: 
   42: -(void) ensureBookmarks
   43: {
   44:     if (mBookmarks)
   45:         return;
   46:     
   47:     mBookmarks = new BookmarksService();
   48:     mBookmarks->AddObserver();
   49:     
   50:     [mOutlineView setTarget: self];
   51:     [mOutlineView setDoubleAction: @selector(openBookmark:)];
   52:     [mOutlineView reloadData];
   53: }
   54: 
   55: -(IBAction)addBookmark:(id)aSender
   56: {
   57:     if (!mBookmarks)
   58:         return;
   59:         
   60:     nsCOMPtr<nsIContent> content;
   61:     int index = [mOutlineView selectedRow];
   62:     
   63:     if (index > 0) {
   64:         BookmarkItem* item = [mOutlineView itemAtRow: index];
   65:         if (![mOutlineView isExpandable: item])
   66:             content = [item contentNode];
   67:     }
   68:     
   69:     if (!content)
   70:         mBookmarks->GetRootContent(getter_AddRefs(content));
   71:         
   72:     nsCOMPtr<nsIDocument> doc;
   73:     content->GetDocument(*getter_AddRefs(doc));
   74:     nsCOMPtr<nsIDOMDocument> domDoc(do_QueryInterface(doc));
   75:     nsCOMPtr<nsIDOMElement> elt;
   76:     domDoc->CreateElementNS(NS_LITERAL_STRING("http://chimera.mozdev.org/bookmarks"), 
   77:                             NS_LITERAL_STRING("bookmark"), 
   78:                             getter_AddRefs(elt));
   79:     
   80:     elt->SetAttribute(NS_LITERAL_STRING("title"), NS_LITERAL_STRING("MozillaZine"));
   81:     elt->SetAttribute(NS_LITERAL_STRING("href"), NS_LITERAL_STRING("http://www.mozillazine.org/"));
   82:     
   83:     nsCOMPtr<nsIDOMElement> parent(do_QueryInterface(content));
   84:     nsCOMPtr<nsIDOMNode> dummy;
   85:     parent->AppendChild(elt, getter_AddRefs(dummy));
   86: }
   87: 
   88: -(IBAction)deleteBookmark: (id)aSender
   89: {
   90: }
   91: 
   92: -(IBAction)openBookmark: (id)aSender
   93: {
   94:     int index = [mOutlineView selectedRow];
   95:     if (index == -1)
   96:         return;
   97:     
   98:     id item = [mOutlineView itemAtRow: index];
   99:     if (!item)
  100:         return;
  101:         
  102:     if ([mOutlineView isExpandable: item]) {
  103:         if ([mOutlineView isItemExpanded: item])
  104:             [mOutlineView collapseItem: item];
  105:         else
  106:             [mOutlineView expandItem: item];
  107:     }
  108:     else {
  109:         nsIContent* content = [item contentNode];
  110:         nsAutoString href;
  111:         content->GetAttr(kNameSpaceID_None, BookmarksService::gHrefAtom, href);
  112:         if (!href.IsEmpty()) {
  113:             nsCAutoString cstr; cstr.AssignWithConversion(href);
  114:             NSString* url = [NSString stringWithCString: cstr.get()];
  115:             [[mBrowserView getBrowserView] loadURI:[NSURL URLWithString: url] flags:NSLoadFlagsNone];
  116:         }
  117:     } 
  118: }
  119: 
  120: - (id)outlineView:(NSOutlineView *)outlineView child:(int)index ofItem:(id)item
  121: {
  122:     if (!mBookmarks)
  123:         return nil;
  124:        
  125:     nsCOMPtr<nsIContent> content;
  126:     if (!item)
  127:         mBookmarks->GetRootContent(getter_AddRefs(content));
  128:     else
  129:         content = [item contentNode];
  130:     
  131:     nsCOMPtr<nsIContent> child;
  132:     content->ChildAt(index, *getter_AddRefs(child));
  133:     return mBookmarks->GetWrapperFor(child);
  134: }
  135: 
  136: - (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item
  137: {
  138:     if (!mBookmarks)
  139:         return NO;
  140:     
  141:     if (!item)
  142:         return YES; // The root node is always open.
  143:     
  144:     nsCOMPtr<nsIAtom> tagName;
  145:     nsIContent* content = [item contentNode];
  146:     content->GetTag(*getter_AddRefs(tagName));
  147:     
  148:     return (tagName == BookmarksService::gFolderAtom);
  149: }
  150: 
  151: - (int)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item
  152: {
  153:     if (!mBookmarks)
  154:         return 0;
  155:   
  156:     nsCOMPtr<nsIContent> content;
  157:     if (!item)
  158:         mBookmarks->GetRootContent(getter_AddRefs(content));
  159:     else 
  160:         content = [item contentNode];
  161:     
  162:     PRInt32 childCount;
  163:     content->ChildCount(childCount);
  164:     
  165:     return childCount;
  166: }
  167: 
  168: - (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item
  169: {
  170:     if (!item)
  171:         return nil;
  172:     
  173:     NSString* columnName = [tableColumn identifier];
  174:       
  175:     if ([columnName isEqualToString: @"name"]) {
  176:         nsIContent* content = [item contentNode];
  177:         nsAutoString nameAttr;
  178:         content->GetAttr(kNameSpaceID_None, BookmarksService::gNameAtom, nameAttr);
  179:         nsCAutoString cStr; cStr.AssignWithConversion(nameAttr);
  180:         return [NSString stringWithCString: cStr.get()];
  181:     }
  182:     
  183:     return nil;
  184: }
  185: 
  186: - (void)outlineView:(NSOutlineView *)outlineView setObjectValue:(id)object forTableColumn:(NSTableColumn *)tableColumn byItem:(id)item
  187: {
  188: }
  189: 
  190: @end
  191: 
  192: @implementation BookmarkItem
  193: -(nsIContent*)contentNode
  194: {
  195:     return mContentNode;
  196: }
  197: 
  198: -(void)setContentNode: (nsIContent*)aContentNode
  199: {
  200:     mContentNode = aContentNode;
  201: }
  202: @end
  203: 
  204: // Helper for stripping whitespace
  205: static void
  206: StripWhitespaceNodes(nsIContent* aElement)
  207: {
  208:     PRInt32 childCount;
  209:     aElement->ChildCount(childCount);
  210:     for (PRInt32 i = 0; i < childCount; i++) {
  211:         nsCOMPtr<nsIContent> child;
  212:         aElement->ChildAt(i, *getter_AddRefs(child));
  213:         nsCOMPtr<nsITextContent> text = do_QueryInterface(child);
  214:         if (text) {
  215:             PRBool isEmpty;
  216:             text->IsOnlyWhitespace(&isEmpty);
  217:             if (isEmpty) {
  218:                 // This node contained nothing but whitespace.
  219:                 // Remove it from the content model.
  220:                 aElement->RemoveChildAt(i, PR_TRUE);
  221:                 i--; // Decrement our count, since we just removed this child.
  222:                 childCount--; // Also decrement our total count.
  223:             }
  224:         }
  225:         else StripWhitespaceNodes(child);
  226:     }
  227: }
  228: 
  229: PRUint32 BookmarksService::gRefCnt = 0;
  230: nsIDocument* BookmarksService::gBookmarks = nsnull;
  231: NSMutableDictionary* BookmarksService::gDictionary = nil;
  232: nsIAtom* BookmarksService::gFolderAtom = nsnull;
  233: nsIAtom* BookmarksService::gBookmarkAtom = nsnull;
  234: nsIAtom* BookmarksService::gHrefAtom = nsnull;
  235: nsIAtom* BookmarksService::gNameAtom = nsnull;
  236: 
  237: BookmarksService::BookmarksService()
  238: {
  239:     NS_INIT_ISUPPORTS();
  240: }
  241: 
  242: BookmarksService::~BookmarksService()
  243: {
  244: }
  245: 
  246: void
  247: BookmarksService::GetRootContent(nsIContent** aResult)
  248: {
  249:     *aResult = nsnull;
  250:     if (gBookmarks) {
  251:         nsCOMPtr<nsIDOMElement> elt;
  252:         nsCOMPtr<nsIDOMDocument> domDoc(do_QueryInterface(gBookmarks));
  253:         domDoc->GetDocumentElement(getter_AddRefs(elt));
  254:         elt->QueryInterface(NS_GET_IID(nsIContent), (void**)aResult); // Addref happens here.
  255:     }
  256: }
  257: 
  258: BookmarkItem*
  259: BookmarksService::GetWrapperFor(nsIContent* aContent)
  260: {
  261:     if (!gDictionary)
  262:         gDictionary = [[NSMutableDictionary alloc] initWithCapacity: 30];
  263:     
  264:     PRUint32 contentID;
  265:     aContent->GetContentID(&contentID);
  266:     
  267:     BookmarkItem* item = [gDictionary objectForKey: [NSNumber numberWithInt: contentID]];
  268:     if (item)
  269:         return item;
  270:     else {
  271:         // Create an item.
  272:         item = [[[BookmarkItem alloc] init] autorelease]; // The dictionary retains us.
  273:         [item setContentNode: aContent];
  274:         [gDictionary setObject: item forKey: [NSNumber numberWithInt: contentID]];
  275:     }
  276:     return item;
  277: }
  278: 
  279: NS_IMETHODIMP
  280: BookmarksService::AttributeChanged(nsIDocument *aDocument,
  281:                               nsIContent*  aContent,
  282:                               PRInt32      aNameSpaceID,
  283:                               nsIAtom*     aAttribute,
  284:                               PRInt32      aModType,    
  285:                               PRInt32      aHint)
  286: {
  287:     return NS_OK;
  288: }
  289: 
  290: NS_IMETHODIMP
  291: BookmarksService::ContentAppended(nsIDocument *aDocument,
  292:                              nsIContent* aContainer,
  293:                              PRInt32     aNewIndexInContainer)
  294: {
  295:     return NS_OK;
  296: }
  297: 
  298: NS_IMETHODIMP
  299: BookmarksService::ContentInserted(nsIDocument *aDocument,
  300:                              nsIContent* aContainer,
  301:                              nsIContent* aChild,
  302:                              PRInt32 aIndexInContainer)
  303: {
  304:     return NS_OK;
  305: }
  306:                              
  307: NS_IMETHODIMP
  308: BookmarksService::ContentRemoved(nsIDocument *aDocument,
  309:                             nsIContent* aContainer,
  310:                             nsIContent* aChild,
  311:                             PRInt32 aIndexInContainer)
  312: {
  313:     return NS_OK;
  314: }
  315: 
  316: NS_IMPL_ISUPPORTS1(BookmarksService, nsIDocumentObserver)
  317: 
  318: void
  319: BookmarksService::AddObserver()
  320: {
  321:     gRefCnt++;
  322:     if (gRefCnt == 1) {
  323:         gBookmarkAtom = NS_NewAtom("bookmark");
  324:         gFolderAtom = NS_NewAtom("folder");
  325:         gNameAtom = NS_NewAtom("name");
  326:         gHrefAtom = NS_NewAtom("href");
  327:         
  328:         nsCOMPtr<nsIFile> profileDir;
  329:         NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR, getter_AddRefs(profileDir));
  330:         profileDir->Append("bookmarks.xml");
  331:     
  332:         nsXPIDLCString bookmarksFileURL;
  333:         NS_GetURLSpecFromFile(profileDir, getter_Copies(bookmarksFileURL));
  334:         
  335:         nsCOMPtr<nsIURI> uri;
  336:         NS_NewURI(getter_AddRefs(uri), bookmarksFileURL.get());
  337:     
  338:         nsCOMPtr<nsIXBLService> xblService(do_GetService("@mozilla.org/xbl;1"));    
  339:         xblService->FetchSyncXMLDocument(uri, &gBookmarks); // The addref is here.
  340:         
  341:         nsCOMPtr<nsIContent> rootNode;
  342:         GetRootContent(getter_AddRefs(rootNode));
  343:         StripWhitespaceNodes(rootNode);
  344:     }
  345:     
  346:     gBookmarks->AddObserver(this);
  347: }
  348: 
  349: void
  350: BookmarksService::RemoveObserver()
  351: {
  352:     if (gRefCnt == 0)
  353:         return;
  354:  
  355:     gBookmarks->RemoveObserver(this);
  356:     
  357:     gRefCnt--;
  358:     if (gRefCnt == 0) {
  359:         NS_IF_RELEASE(gBookmarks);
  360:         NS_RELEASE(gBookmarkAtom);
  361:         NS_RELEASE(gFolderAtom);
  362:         NS_RELEASE(gNameAtom);
  363:         NS_RELEASE(gHrefAtom);
  364:         [gDictionary release];
  365:     }
  366: }
  367: 

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