File:  [mozdev] / chimera / BookmarksService.mm
Revision 1.2: download - view: text, annotated - select for diffs - revision graph
Sat Feb 9 01:24:11 2002 UTC (17 years, 4 months ago) by hyatt
Branches: MAIN
CVS tags: HEAD
Stubs for adding bookmarks.

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

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