Annotation of chimera/BookmarksService.mm, revision 1.6

1.1       hyatt       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"
1.5       hyatt      16: #include "nsIDOMWindow.h"
                     17: #include "nsIDOMHTMLDocument.h"
1.1       hyatt      18: #include "nsIDOMElement.h"
                     19: #include "nsString.h"
                     20: #include "nsIFile.h"
                     21: #include "nsAppDirectoryServiceDefs.h"
                     22: #include "nsIXMLHttpRequest.h"
1.6     ! hyatt      23: #include "nsIDOMSerializer.h"
1.1       hyatt      24: #include "nsNetUtil.h"
                     25: #include "nsINamespaceManager.h"
                     26: #include "nsIXBLService.h"
1.5       hyatt      27: #include "nsIWebBrowser.h"
1.1       hyatt      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];
1.5       hyatt      41: }
                     42: 
                     43: -(void) windowClosing
                     44: {
                     45:     if (mBookmarks) {
1.4       hyatt      46:         mBookmarks->RemoveObserver();
1.5       hyatt      47:         delete mBookmarks;
                     48:     }
1.1       hyatt      49: }
                     50: 
                     51: -(void) ensureBookmarks
                     52: {
                     53:     if (mBookmarks)
                     54:         return;
                     55:     
1.5       hyatt      56:     mBookmarks = new BookmarksService(self);
1.4       hyatt      57:     mBookmarks->AddObserver();
1.1       hyatt      58:     
                     59:     [mOutlineView setTarget: self];
                     60:     [mOutlineView setDoubleAction: @selector(openBookmark:)];
                     61:     [mOutlineView reloadData];
                     62: }
                     63: 
1.2       hyatt      64: -(IBAction)addBookmark:(id)aSender
                     65: {
                     66:     if (!mBookmarks)
                     67:         return;
1.5       hyatt      68:     
1.2       hyatt      69:     nsCOMPtr<nsIContent> content;
                     70:     int index = [mOutlineView selectedRow];
1.5       hyatt      71:    
1.2       hyatt      72:     if (index > 0) {
                     73:         BookmarkItem* item = [mOutlineView itemAtRow: index];
1.5       hyatt      74:         if ([mOutlineView isExpandable: item]) 
1.2       hyatt      75:             content = [item contentNode];
                     76:     }
                     77:     
                     78:     if (!content)
                     79:         mBookmarks->GetRootContent(getter_AddRefs(content));
                     80:         
1.5       hyatt      81:     nsCOMPtr<nsIDOMDocument> domDoc(do_QueryInterface(mBookmarks->gBookmarks));
1.2       hyatt      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:     
1.5       hyatt      87:     // Fetch the title of the current page and the URL.
                     88:     nsCOMPtr<nsIWebBrowser> webBrowser = getter_AddRefs([[mBrowserView 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:         nsXPIDLCString spec;
                    100:         url->GetSpec(getter_Copies(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);    
1.2       hyatt     113:     
                    114:     nsCOMPtr<nsIDOMElement> parent(do_QueryInterface(content));
                    115:     nsCOMPtr<nsIDOMNode> dummy;
                    116:     parent->AppendChild(elt, getter_AddRefs(dummy));
1.5       hyatt     117: 
                    118:     mBookmarks->NotifyObservers(content, PR_TRUE);
1.2       hyatt     119: }
                    120: 
                    121: -(IBAction)deleteBookmark: (id)aSender
                    122: {
1.6     ! hyatt     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];
1.2       hyatt     145: }
                    146: 
1.1       hyatt     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:             [[mBrowserView getBrowserView] loadURI:[NSURL URLWithString: url] flags:NSLoadFlagsNone];
                    171:         }
                    172:     } 
                    173: }
                    174: 
                    175: - (id)outlineView:(NSOutlineView *)outlineView child:(int)index ofItem:(id)item
                    176: {
                    177:     if (!mBookmarks)
                    178:         return nil;
                    179:        
                    180:     nsCOMPtr<nsIContent> content;
                    181:     if (!item)
                    182:         mBookmarks->GetRootContent(getter_AddRefs(content));
                    183:     else
                    184:         content = [item contentNode];
                    185:     
                    186:     nsCOMPtr<nsIContent> child;
                    187:     content->ChildAt(index, *getter_AddRefs(child));
                    188:     return mBookmarks->GetWrapperFor(child);
                    189: }
                    190: 
                    191: - (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item
                    192: {
                    193:     if (!mBookmarks)
                    194:         return NO;
                    195:     
                    196:     if (!item)
                    197:         return YES; // The root node is always open.
                    198:     
                    199:     nsCOMPtr<nsIAtom> tagName;
                    200:     nsIContent* content = [item contentNode];
                    201:     content->GetTag(*getter_AddRefs(tagName));
                    202:     
                    203:     return (tagName == BookmarksService::gFolderAtom);
                    204: }
                    205: 
                    206: - (int)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item
                    207: {
                    208:     if (!mBookmarks)
                    209:         return 0;
1.2       hyatt     210:   
1.1       hyatt     211:     nsCOMPtr<nsIContent> content;
                    212:     if (!item)
                    213:         mBookmarks->GetRootContent(getter_AddRefs(content));
                    214:     else 
                    215:         content = [item contentNode];
                    216:     
                    217:     PRInt32 childCount;
                    218:     content->ChildCount(childCount);
                    219:     
                    220:     return childCount;
                    221: }
                    222: 
                    223: - (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item
                    224: {
                    225:     if (!item)
                    226:         return nil;
                    227:     
                    228:     NSString* columnName = [tableColumn identifier];
                    229:       
                    230:     if ([columnName isEqualToString: @"name"]) {
                    231:         nsIContent* content = [item contentNode];
                    232:         nsAutoString nameAttr;
                    233:         content->GetAttr(kNameSpaceID_None, BookmarksService::gNameAtom, nameAttr);
                    234:         nsCAutoString cStr; cStr.AssignWithConversion(nameAttr);
                    235:         return [NSString stringWithCString: cStr.get()];
                    236:     }
                    237:     
                    238:     return nil;
                    239: }
                    240: 
                    241: - (void)outlineView:(NSOutlineView *)outlineView setObjectValue:(id)object forTableColumn:(NSTableColumn *)tableColumn byItem:(id)item
                    242: {
                    243: }
                    244: 
1.5       hyatt     245: - (void)reloadDataForItem:(id)item reloadChildren: (BOOL)aReloadChildren
                    246: {
                    247:     printf("Reloading?\n");
                    248:     if (!item)
                    249:         [mOutlineView reloadData];
                    250:     else if ([mOutlineView isItemExpanded: item])
                    251:         [mOutlineView reloadItem: item reloadChildren: aReloadChildren];
                    252: }
                    253: 
1.1       hyatt     254: @end
                    255: 
                    256: @implementation BookmarkItem
                    257: -(nsIContent*)contentNode
                    258: {
                    259:     return mContentNode;
                    260: }
                    261: 
                    262: -(void)setContentNode: (nsIContent*)aContentNode
                    263: {
                    264:     mContentNode = aContentNode;
                    265: }
                    266: @end
                    267: 
                    268: // Helper for stripping whitespace
                    269: static void
                    270: StripWhitespaceNodes(nsIContent* aElement)
                    271: {
                    272:     PRInt32 childCount;
                    273:     aElement->ChildCount(childCount);
                    274:     for (PRInt32 i = 0; i < childCount; i++) {
                    275:         nsCOMPtr<nsIContent> child;
                    276:         aElement->ChildAt(i, *getter_AddRefs(child));
                    277:         nsCOMPtr<nsITextContent> text = do_QueryInterface(child);
                    278:         if (text) {
                    279:             PRBool isEmpty;
                    280:             text->IsOnlyWhitespace(&isEmpty);
                    281:             if (isEmpty) {
                    282:                 // This node contained nothing but whitespace.
                    283:                 // Remove it from the content model.
                    284:                 aElement->RemoveChildAt(i, PR_TRUE);
                    285:                 i--; // Decrement our count, since we just removed this child.
                    286:                 childCount--; // Also decrement our total count.
                    287:             }
                    288:         }
                    289:         else StripWhitespaceNodes(child);
                    290:     }
                    291: }
                    292: 
                    293: PRUint32 BookmarksService::gRefCnt = 0;
1.4       hyatt     294: nsIDocument* BookmarksService::gBookmarks = nsnull;
                    295: NSMutableDictionary* BookmarksService::gDictionary = nil;
1.1       hyatt     296: nsIAtom* BookmarksService::gFolderAtom = nsnull;
                    297: nsIAtom* BookmarksService::gBookmarkAtom = nsnull;
                    298: nsIAtom* BookmarksService::gHrefAtom = nsnull;
                    299: nsIAtom* BookmarksService::gNameAtom = nsnull;
1.5       hyatt     300: nsVoidArray* BookmarksService::gInstances = nsnull;
1.1       hyatt     301: 
1.5       hyatt     302: BookmarksService::BookmarksService(BookmarksDataSource* aDataSource)
1.1       hyatt     303: {
1.5       hyatt     304:     mDataSource = aDataSource;
1.1       hyatt     305: }
                    306: 
                    307: BookmarksService::~BookmarksService()
                    308: {
                    309: }
                    310: 
                    311: void
                    312: BookmarksService::GetRootContent(nsIContent** aResult)
                    313: {
                    314:     *aResult = nsnull;
1.4       hyatt     315:     if (gBookmarks) {
1.1       hyatt     316:         nsCOMPtr<nsIDOMElement> elt;
1.4       hyatt     317:         nsCOMPtr<nsIDOMDocument> domDoc(do_QueryInterface(gBookmarks));
1.1       hyatt     318:         domDoc->GetDocumentElement(getter_AddRefs(elt));
                    319:         elt->QueryInterface(NS_GET_IID(nsIContent), (void**)aResult); // Addref happens here.
                    320:     }
                    321: }
                    322: 
                    323: BookmarkItem*
                    324: BookmarksService::GetWrapperFor(nsIContent* aContent)
                    325: {
1.4       hyatt     326:     if (!gDictionary)
                    327:         gDictionary = [[NSMutableDictionary alloc] initWithCapacity: 30];
1.1       hyatt     328:     
                    329:     PRUint32 contentID;
                    330:     aContent->GetContentID(&contentID);
                    331:     
1.4       hyatt     332:     BookmarkItem* item = [gDictionary objectForKey: [NSNumber numberWithInt: contentID]];
1.1       hyatt     333:     if (item)
                    334:         return item;
                    335:     else {
                    336:         // Create an item.
                    337:         item = [[[BookmarkItem alloc] init] autorelease]; // The dictionary retains us.
                    338:         [item setContentNode: aContent];
1.4       hyatt     339:         [gDictionary setObject: item forKey: [NSNumber numberWithInt: contentID]];
1.1       hyatt     340:     }
                    341:     return item;
                    342: }
                    343: 
1.5       hyatt     344: void
                    345: BookmarksService::NotifyObservers(nsIContent* aContainer, PRBool aReloadChildren)
1.4       hyatt     346: {
1.5       hyatt     347:     if (!gInstances)
                    348:         return;
                    349:     
                    350:     PRInt32 count = gInstances->Count();
                    351:     for (PRInt32 i = 0; i < count; i++) {
                    352:         BookmarksService* instance = (BookmarksService*)gInstances->ElementAt(i);
                    353:         instance->NotifyObserver(aContainer, aReloadChildren);
                    354:     }
1.6     ! hyatt     355:     
        !           356:     FlushBookmarks();
1.4       hyatt     357: }
                    358: 
                    359: 
1.5       hyatt     360: void
                    361: BookmarksService::NotifyObserver(nsIContent* aContainer, PRBool aReloadChildren)
1.4       hyatt     362: {
1.5       hyatt     363:     if (!gDictionary)
                    364:         return;
1.6     ! hyatt     365:    
1.5       hyatt     366:     nsCOMPtr<nsIContent> parent;
                    367:     aContainer->GetParent(*getter_AddRefs(parent));
                    368:     
                    369:     BookmarkItem* item = nil;
                    370:     if (parent)
                    371:         // We're not the root.
                    372:         item = GetWrapperFor(aContainer);
                    373:     
                    374:     [mDataSource reloadDataForItem: item reloadChildren: aReloadChildren];
1.4       hyatt     375: }
                    376: 
                    377: void
                    378: BookmarksService::AddObserver()
1.1       hyatt     379: {
                    380:     gRefCnt++;
                    381:     if (gRefCnt == 1) {
                    382:         gBookmarkAtom = NS_NewAtom("bookmark");
                    383:         gFolderAtom = NS_NewAtom("folder");
                    384:         gNameAtom = NS_NewAtom("name");
                    385:         gHrefAtom = NS_NewAtom("href");
1.5       hyatt     386:         gInstances = new nsVoidArray();
1.4       hyatt     387:         
                    388:         nsCOMPtr<nsIFile> profileDir;
                    389:         NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR, getter_AddRefs(profileDir));
                    390:         profileDir->Append("bookmarks.xml");
                    391:     
                    392:         nsXPIDLCString bookmarksFileURL;
                    393:         NS_GetURLSpecFromFile(profileDir, getter_Copies(bookmarksFileURL));
                    394:         
                    395:         nsCOMPtr<nsIURI> uri;
                    396:         NS_NewURI(getter_AddRefs(uri), bookmarksFileURL.get());
                    397:     
                    398:         nsCOMPtr<nsIXBLService> xblService(do_GetService("@mozilla.org/xbl;1"));    
                    399:         xblService->FetchSyncXMLDocument(uri, &gBookmarks); // The addref is here.
                    400:         
                    401:         nsCOMPtr<nsIContent> rootNode;
                    402:         GetRootContent(getter_AddRefs(rootNode));
                    403:         StripWhitespaceNodes(rootNode);
1.1       hyatt     404:     }
                    405:     
1.5       hyatt     406:     gInstances->AppendElement(this);
1.1       hyatt     407: }
                    408: 
                    409: void
1.4       hyatt     410: BookmarksService::RemoveObserver()
1.1       hyatt     411: {
                    412:     if (gRefCnt == 0)
                    413:         return;
1.4       hyatt     414:  
1.5       hyatt     415:     gInstances->RemoveElement(this);
                    416:      
1.1       hyatt     417:     gRefCnt--;
                    418:     if (gRefCnt == 0) {
1.4       hyatt     419:         NS_IF_RELEASE(gBookmarks);
1.1       hyatt     420:         NS_RELEASE(gBookmarkAtom);
                    421:         NS_RELEASE(gFolderAtom);
                    422:         NS_RELEASE(gNameAtom);
                    423:         NS_RELEASE(gHrefAtom);
1.4       hyatt     424:         [gDictionary release];
1.1       hyatt     425:     }
                    426: }
                    427: 
1.6     ! hyatt     428: void
        !           429: BookmarksService::FlushBookmarks()
        !           430: {
        !           431:     nsCOMPtr<nsIFile> bookmarksFile;
        !           432:     NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR, getter_AddRefs(bookmarksFile));
        !           433:     bookmarksFile->Append("bookmarks.xml");
        !           434: 
        !           435:     nsCOMPtr<nsIOutputStream> outputStream;
        !           436:     NS_NewLocalFileOutputStream(getter_AddRefs(outputStream), bookmarksFile);
        !           437: 
        !           438:     nsCOMPtr<nsIDOMDocument> domDoc(do_QueryInterface(gBookmarks));
        !           439:     
        !           440:     nsCOMPtr<nsIDOMSerializer> domSerializer(do_CreateInstance(NS_XMLSERIALIZER_CONTRACTID));
        !           441:     domSerializer->SerializeToStream(domDoc, outputStream, nsnull);
        !           442: }

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