Eliminate calls to DOM APIs in bookmark-related code outside of the bookmarks service, so that we can eventually move away from the assumption that bookmarks are DOM-based.

This commit is contained in:
sfraser%netscape.com 2003-03-05 23:18:09 +00:00
Родитель 8a59af932a
Коммит 50a0433f9e
11 изменённых файлов: 276 добавлений и 241 удалений

Просмотреть файл

@ -18,9 +18,11 @@
* Rights Reserved.
*
* Contributor(s):
* Ben Goodger <ben@netscape.com> (Original Author)
* David Haas <haasd@cae.wisc.edu>
* Ben Goodger <ben@netscape.com> (Original Author)
* Simon Fraser <sfraser@netscape.com>
* David Haas <haasd@cae.wisc.edu>
*/
#import <AppKit/AppKit.h>
#import "BookmarksService.h"

Просмотреть файл

@ -18,8 +18,9 @@
* Rights Reserved.
*
* Contributor(s):
* Ben Goodger <ben@netscape.com> (Original Author)
* David Haas <haasd@cae.wisc.edu>
* Ben Goodger <ben@netscape.com> (Original Author)
* Simon Fraser <sfraser@netscape.com>
* David Haas <haasd@cae.wisc.edu>
*/
#import "NSString+Utils.h"
@ -27,8 +28,6 @@
#import "BookmarkInfoController.h"
#include "nsIContent.h"
#include "nsINamespaceManager.h"
@interface BookmarkInfoController(Private)
@ -167,15 +166,12 @@ static BookmarkInfoController *sharedBookmarkInfoController = nil;
// return YES if changed
- (BOOL)commitField:(id)textField toProperty:(nsIAtom*)propertyAtom
{
nsAutoString attributeString;
[[textField stringValue] assignTo_nsAString:attributeString];
NSString* newValue = [textField stringValue];
NSString* oldValue = [mBookmarkItem getAttributeValue:propertyAtom];
nsAutoString oldAttribValue;
[mBookmarkItem contentNode]->GetAttr(kNameSpaceID_None, propertyAtom, oldAttribValue);
if (!attributeString.Equals(oldAttribValue))
if (![newValue isEqualToString:oldValue])
{
[mBookmarkItem contentNode]->SetAttr(kNameSpaceID_None, propertyAtom, attributeString, PR_TRUE);
[mBookmarkItem setAttribute:propertyAtom toValue:newValue];
return YES;
}
@ -192,10 +188,7 @@ static BookmarkInfoController *sharedBookmarkInfoController = nil;
- (IBAction)tabGroupCheckboxClicked:(id)sender
{
if ([sender state] == NSOnState)
[mBookmarkItem contentNode]->SetAttr(kNameSpaceID_None, BookmarksService::gGroupAtom, NS_LITERAL_STRING("true"), PR_TRUE);
else
[mBookmarkItem contentNode]->UnsetAttr(kNameSpaceID_None, BookmarksService::gGroupAtom, PR_TRUE);
[mBookmarkItem setIsGroup:[sender state] == NSOnState];
[mBookmarkItem itemChanged:YES];
}

Просмотреть файл

@ -24,22 +24,20 @@
#import <Foundation/Foundation.h>
#import <Appkit/Appkit.h>
class nsIDOMElement;
class BookmarksService;
@class BookmarkItem;
@interface BookmarksButton : NSButton
{
nsIDOMElement* mElement;
BookmarkItem* mBookmarkItem;
BOOL mIsFolder;
}
-(id)initWithFrame:(NSRect)frame element:(nsIDOMElement*)element;
-(id)initWithFrame:(NSRect)frame item:(BookmarkItem*)item;
-(void)setElement: (nsIDOMElement*)aElt;
-(nsIDOMElement*)element;
- (void)setItem:(BookmarkItem*)inItem;
- (BookmarkItem*)getItem;
-(IBAction)openBookmark:(id)aSender;
-(IBAction)openBookmarkInNewTab:(id)aSender;

Просмотреть файл

@ -28,7 +28,6 @@
#include "nsCOMPtr.h"
#include "nsIContent.h"
#include "nsIDOMElement.h"
#include "nsString.h"
#include "nsCRT.h"
@ -62,11 +61,11 @@
return self;
}
-(id)initWithFrame:(NSRect)frame element:(nsIDOMElement*)element
-(id)initWithFrame:(NSRect)frame item:(BookmarkItem*)item
{
if ( (self = [self initWithFrame:frame]) )
{
[self setElement:element];
[self setItem:item];
}
return self;
}
@ -97,12 +96,7 @@
-(IBAction)openBookmarkInNewTab:(id)aSender
{
if (!mElement) return;
// Get the href attribute. This is the URL we want to load.
nsAutoString hrefAttr;
mElement->GetAttribute(NS_LITERAL_STRING("href"), hrefAttr);
NSString* hrefStr = [NSString stringWith_nsAString:hrefAttr];
NSString* hrefStr = [mBookmarkItem url];
BOOL loadInBackground = [[PreferenceManager sharedInstance] getBooleanPref:"browser.tabs.loadInBackground" withSuccess:NULL];
@ -112,23 +106,13 @@
-(IBAction)openBookmarkInNewWindow:(id)aSender
{
if (!mElement) return;
// Get the href attribute. This is the URL we want to load.
nsAutoString hrefAttr;
mElement->GetAttribute(NS_LITERAL_STRING("href"), hrefAttr);
NSString* hrefStr = [NSString stringWith_nsAString:hrefAttr];
BOOL loadInBackground = [[PreferenceManager sharedInstance] getBooleanPref:"browser.tabs.loadInBackground" withSuccess:NULL];
nsAutoString group;
mElement->GetAttribute(NS_LITERAL_STRING("group"), group);
BrowserWindowController* brController = [[self window] windowController];
if (group.IsEmpty())
[brController openNewWindowWithURL: hrefStr referrer: nil loadInBackground: loadInBackground];
else
if ([mBookmarkItem isGroup])
[brController openNewWindowWithGroup:[mBookmarkItem contentNode] loadInBackground: loadInBackground];
else
[brController openNewWindowWithURL:[mBookmarkItem url] referrer: nil loadInBackground: loadInBackground];
}
-(IBAction)showBookmarkInfo:(id)aSender
@ -148,7 +132,6 @@
[mBookmarkItem remove];
mBookmarkItem = nil;
mElement = nil;
}
-(IBAction)addFolder:(id)aSender
@ -179,14 +162,11 @@
-(BOOL)validateMenuItem:(NSMenuItem*)aMenuItem
{
if (!mBookmarkItem || !mElement)
if (!mBookmarkItem)
return NO;
BOOL isBookmark = [mBookmarkItem isFolder] == NO;
nsAutoString group;
mElement->GetAttribute(NS_LITERAL_STRING("group"), group);
BOOL isGroup = !group.IsEmpty();
BOOL isGroup = [mBookmarkItem isGroup];
if (([aMenuItem action] == @selector(openBookmarkInNewWindow:))) {
// Bookmarks and Bookmark Groups can be opened in a new window
@ -204,12 +184,11 @@
{
// pop up a "context menu" on folders showing their contents. we check
// for single click to fix issues with dblclicks (bug 162367)
if (mElement && mIsFolder && [aEvent clickCount] == 1)
if (mBookmarkItem && mIsFolder && [aEvent clickCount] == 1)
{
nsCOMPtr<nsIContent> content = do_QueryInterface(mElement);
NSMenu* popupMenu = [[NSMenu alloc] init];
// make a temporary BookmarksMenu to build the menu
BookmarksMenu* bmMenu = [[BookmarksMenu alloc] initWithMenu:popupMenu firstItem:0 rootContent:content watchedFolder:eBookmarksFolderNormal];
BookmarksMenu* bmMenu = [[BookmarksMenu alloc] initWithMenu:popupMenu firstItem:0 rootContent:[mBookmarkItem contentNode] watchedFolder:eBookmarksFolderNormal];
[NSMenu popUpContextMenu: popupMenu withEvent: aEvent forView: self];
[bmMenu release];
@ -219,52 +198,40 @@
[super mouseDown:aEvent];
}
-(void)setElement: (nsIDOMElement*)aElt
- (void)setItem:(BookmarkItem*)inItem
{
mElement = aElt; // not addreffed
if (!inItem) return;
if (!mElement) return;
mBookmarkItem = inItem; // we don't hold a ref to this.
nsAutoString tag;
mElement->GetLocalName(tag);
NSImage* bookmarkImage = BookmarksService::CreateIconForBookmark(aElt, PR_TRUE);
nsAutoString group;
mElement->GetAttribute(NS_LITERAL_STRING("group"), group);
if (!group.IsEmpty()) {
NSImage* bookmarkImage = [[BookmarksManager sharedBookmarksManager] createIconForBookmarkItem:mBookmarkItem useSiteIcon:YES];
if ([mBookmarkItem isGroup])
{
mIsFolder = NO;
[self setImage: bookmarkImage];
[self setAction: @selector(openBookmark:)];
[self setTarget: self];
}
else if (tag.Equals(NS_LITERAL_STRING("folder"))) {
else if ([mBookmarkItem isFolder])
{
[self setImage: bookmarkImage];
mIsFolder = YES;
}
else {
else
{
mIsFolder = NO;
[self setImage: bookmarkImage];
[self setAction: @selector(openBookmark:)];
[self setTarget: self];
nsAutoString href;
mElement->GetAttribute(NS_LITERAL_STRING("href"), href);
NSString* helpText = [NSString stringWith_nsAString:href];
[self setToolTip: helpText];
[self setToolTip: [mBookmarkItem url]];
}
nsAutoString name;
mElement->GetAttribute(NS_LITERAL_STRING("name"), name);
[self setTitle: [NSString stringWith_nsAString: name]];
nsCOMPtr<nsIContent> content(do_QueryInterface(mElement));
mBookmarkItem = BookmarksService::GetWrapperFor(content);
[self setTitle: [mBookmarkItem name]];
}
-(nsIDOMElement*)element
- (BookmarkItem*)getItem
{
return mElement;
return mBookmarkItem;
}
- (unsigned int)draggingSourceOperationMaskForLocal:(BOOL)localFlag
@ -276,27 +243,19 @@
}
- (void) mouseDragged: (NSEvent*) aEvent
{
if (!mElement) return;
// Get the href attribute. This is the URL we want to load.
nsAutoString hrefStr;
mElement->GetAttribute(NS_LITERAL_STRING("href"), hrefStr);
if (hrefStr.IsEmpty())
{
if ([[mBookmarkItem url] length] == 0)
return;
nsAutoString titleStr;
mElement->GetAttribute(NS_LITERAL_STRING("name"), titleStr);
NSPasteboard *pboard = [NSPasteboard pasteboardWithName:NSDragPboard];
[pboard declareURLPasteboardWithAdditionalTypes:[NSArray arrayWithObjects:@"MozBookmarkType", nil] owner:self];
NSString *url = [NSString stringWith_nsAString: hrefStr];
NSString *title = [NSString stringWith_nsAString: titleStr];
NSString *url = [mBookmarkItem url];
NSString *title = [mBookmarkItem name];
NSString *cleanedTitle = [title stringByReplacingCharactersInSet:[NSCharacterSet controlCharacterSet] withString:@" "];
// MozBookmarkType
nsCOMPtr<nsIContent> content = do_QueryInterface(mElement);
nsCOMPtr<nsIContent> content = [mBookmarkItem contentNode];
if (content)
{
PRUint32 contentID;

Просмотреть файл

@ -45,11 +45,6 @@
#include "nsCOMPtr.h"
#include "nsIContent.h"
#include "nsIDocument.h"
#include "nsIDocumentObserver.h"
#include "nsIDOMDocument.h"
#include "nsIDOMElement.h"
#include "nsINamespaceManager.h"
#include "nsVoidArray.h"
@ -472,11 +467,7 @@ const int kBookmarksRootItemTag = -2;
if (item)
{
nsCOMPtr<nsIContent> content;
content = [item contentNode];
PRBool isOpen = content && content->HasAttr(kNameSpaceID_None, BookmarksService::gOpenAtom);
if (isOpen)
if ([item isExpanded])
[mOutlineView expandItem: item];
else
[mOutlineView collapseItem: item];
@ -595,13 +586,9 @@ const int kBookmarksRootItemTag = -2;
NSFileWrapper *fileWrapper = [[NSFileWrapper alloc] initRegularFileWithContents:nil];
NSTextAttachment *textAttachment = [[NSTextAttachment alloc] initWithFileWrapper:fileWrapper];
nsIContent* content = [item contentNode];
nsAutoString nameAttr;
content->GetAttr(kNameSpaceID_None, BookmarksService::gNameAtom, nameAttr);
//Set cell's textual contents
//[cellValue replaceCharactersInRange:NSMakeRange(0, [cellValue length]) withString:[NSString stringWith_nsAString: nameAttr]];
cellValue = [[[NSMutableAttributedString alloc] initWithString:[NSString stringWith_nsAString: nameAttr]] autorelease];
cellValue = [[[NSMutableAttributedString alloc] initWithString:[item name]] autorelease];
//Create an attributed string to hold the empty attachment, then release the components.
NSMutableAttributedString* attachmentAttrString = [NSMutableAttributedString attributedStringWithAttachment:textAttachment];
@ -611,8 +598,7 @@ const int kBookmarksRootItemTag = -2;
//Get the cell of the text attachment.
NSCell* attachmentAttrStringCell = (NSCell *)[(NSTextAttachment *)[attachmentAttrString attribute:NSAttachmentAttributeName atIndex:0 effectiveRange:nil] attachmentCell];
nsCOMPtr<nsIDOMElement> elt(do_QueryInterface(content));
NSImage* bookmarkImage = BookmarksService::CreateIconForBookmark(elt);
NSImage* bookmarkImage = [[BookmarksManager sharedBookmarksManager] createIconForBookmarkItem:item useSiteIcon:NO];
[attachmentAttrStringCell setImage:bookmarkImage];
//Insert the image
@ -637,15 +623,8 @@ const int kBookmarksRootItemTag = -2;
NSMutableString* mutableString = [NSMutableString stringWithString:object];
[mutableString replaceOccurrencesOfString:[NSString stringWithCharacters:&kAttachmentCharacter length:1] withString:@"" options:0 range:NSMakeRange(0, [mutableString length])];
nsAutoString nameAttr;
[mutableString assignTo_nsAString:nameAttr];
// stash it into the DOM
BookmarkItem* bmItem = (BookmarkItem*)item;
nsIContent* content = [bmItem contentNode];
if (content)
content->SetAttr(kNameSpaceID_None, BookmarksService::gNameAtom, nameAttr, PR_TRUE);
[bmItem setName:mutableString];
[bmItem itemChanged:YES];
}
}
@ -762,25 +741,22 @@ const int kBookmarksRootItemTag = -2;
NSString* descStr = nil;
NSString* hrefStr = nil;
nsIContent* content = [item contentNode];
nsAutoString value;
content->GetAttr(kNameSpaceID_None, BookmarksService::gDescriptionAtom, value);
if (value.Length())
descStr = [NSString stringWith_nsAString:value];
if ([[item descriptionString] length] > 0)
descStr = [item descriptionString];
// Only description for folders
if ([item isFolder])
return descStr;
// Extract the URL from the item
content->GetAttr(kNameSpaceID_None, BookmarksService::gHrefAtom, value);
if (value.Length())
hrefStr = [NSString stringWith_nsAString:value];
if ([[item url] length] > 0)
hrefStr = [item url];
if (!hrefStr)
return descStr;
else if (!descStr)
if (!descStr)
return hrefStr;
// Display both URL and description
@ -870,13 +846,13 @@ const int kBookmarksRootItemTag = -2;
- (void)outlineViewItemWillExpand:(NSNotification *)notification
{
BookmarkItem* item = [[notification userInfo] objectForKey:[[[notification userInfo] allKeys] objectAtIndex: 0]];
[item contentNode]->SetAttr(kNameSpaceID_None, BookmarksService::gOpenAtom, NS_LITERAL_STRING("true"), PR_FALSE);
[item setIsExpanded:YES];
}
- (void)outlineViewItemWillCollapse:(NSNotification *)notification
{
BookmarkItem* item = [[notification userInfo] objectForKey:[[[notification userInfo] allKeys] objectAtIndex: 0]];
[item contentNode]->UnsetAttr(kNameSpaceID_None, BookmarksService::gOpenAtom, PR_FALSE);
[item setIsExpanded:NO];
}
@end

Просмотреть файл

@ -41,9 +41,6 @@
#include "nsCOMPtr.h"
#include "nsIContent.h"
#include "nsIDOMElement.h"
#include "nsINameSpaceManager.h"
#include "nsIAtom.h"
@interface BookmarksMenu(Private)
@ -104,28 +101,17 @@
{
if (!child) return;
nsAutoString name;
child->GetAttr(kNameSpaceID_None, BookmarksService::gNameAtom, name);
NSString* title = [[NSString stringWith_nsAString: name] stringByTruncatingTo:80 at:kTruncateAtMiddle];
BookmarksManager* bmManager = [BookmarksManager sharedBookmarksManager];
nsCOMPtr<nsIDOMElement> elt = do_QueryInterface(child);
NSImage* menuItemImage = BookmarksService::CreateIconForBookmark(elt);
// ensure a wrapper exists
[[BookmarksManager sharedBookmarksManager] getWrapperForContent:child];
BookmarkItem* bmItem = [bmManager getWrapperForContent:child];
NSImage* menuItemImage = [bmManager createIconForBookmarkItem:bmItem useSiteIcon:NO];
NSString* title = [[bmItem name] stringByTruncatingTo:80 at:kTruncateAtMiddle];
nsCOMPtr<nsIAtom> tagName;
child->GetTag(*getter_AddRefs(tagName));
nsAutoString group;
if (tagName == BookmarksService::gFolderAtom)
child->GetAttr(kNameSpaceID_None, BookmarksService::gGroupAtom, group);
BOOL isFolder = (tagName == BookmarksService::gFolderAtom);
BOOL isGroup = isFolder && !group.IsEmpty();
BOOL isFolder = [bmItem isFolder];
BOOL isGroup = [bmItem isGroup];
// Create a menu or menu item for the child.
NSMenuItem* menuItem = [[[NSMenuItem alloc] initWithTitle: title action: NULL keyEquivalent: @""] autorelease];
NSMenuItem* menuItem = [[[NSMenuItem alloc] initWithTitle:title action: NULL keyEquivalent: @""] autorelease];
if (index == -1)
[menu addItem: menuItem];
else
@ -137,9 +123,7 @@
[menu insertItem:menuItem atIndex:insertIndex];
}
PRUint32 contentID;
child->GetContentID(&contentID);
[menuItem setTag: contentID];
[menuItem setTag: [bmItem intContentID]];
if (isFolder && !isGroup) // folder
{
@ -223,25 +207,24 @@
{
if (!bookmark) return;
BookmarksManager* bmManager = [BookmarksManager sharedBookmarksManager];
BookmarkItem* bmItem = [bmManager getWrapperForContent:bookmark];
// XXX fix to not use nsIContent
nsCOMPtr<nsIContent> parent;
bookmark->GetParent(*getter_AddRefs(parent));
PRUint32 contentID = 0;
bookmark->GetContentID(&contentID);
NSMenu* menu = [self locateMenuForContent:parent];
if (!menu) return;
NSMenuItem* menuItem = [menu itemWithTag: contentID];
NSMenuItem* menuItem = [menu itemWithTag: [bmItem intContentID]];
nsAutoString name;
bookmark->GetAttr(kNameSpaceID_None, BookmarksService::gNameAtom, name);
NSString* bookmarkTitle = [[NSString stringWith_nsAString: name] stringByTruncatingTo:80 at:kTruncateAtMiddle];
NSString* bookmarkTitle = [[bmItem name] stringByTruncatingTo:80 at:kTruncateAtMiddle];
[menuItem setTitle: bookmarkTitle];
// and reset the image
nsCOMPtr<nsIDOMElement> elt = do_QueryInterface(bookmark);
NSImage* menuItemImage = BookmarksService::CreateIconForBookmark(elt);
NSImage* menuItemImage = [bmManager createIconForBookmarkItem:bmItem useSiteIcon:NO];
[menuItem setImage:menuItemImage];
}

Просмотреть файл

@ -26,9 +26,6 @@
#import "BookmarksDataSource.h"
#include "nsCOMPtr.h"
#include "nsIDOMElement.h"
#include "nsIContent.h"
#include "nsIDOMNode.h"
@implementation BookmarksOutlineView

Просмотреть файл

@ -189,16 +189,32 @@ protected:
- (NSString*)keyword;
- (NSString*)descriptionString;
- (void)setName:(NSString*)inName;
- (void)setUrl:(NSString*)inName;
- (void)setKeyword:(NSString*)inName;
- (void)setDescriptionString:(NSString*)inName;
- (NSString*)getAttributeValue:(nsIAtom*)atom;
- (void)setAttribute:(nsIAtom*)atom toValue:(NSString*)value;
- (NSImage*)siteIcon;
- (NSNumber*)contentID;
- (int)intContentID;
- (BOOL)isFolder;
- (BOOL)isGroup;
- (void)setIsGroup:(BOOL)isGroup;
// only applicable for folders
- (BOOL)isExpanded;
- (void)setIsExpanded:(BOOL)isExpanded;
- (BOOL)isToobarRoot;
- (BOOL)isDockMenuRoot;
- (BookmarkItem*)parentItem;
- (NSArray*)getChildren;
- (int)getNumberOfChildren;
@end
@ -224,6 +240,10 @@ protected:
- (nsIContent*)getToolbarRoot; // addrefs return value
- (nsIContent*)getDockMenuRoot; // addrefs return value
- (BookmarkItem*)getRootItem;
- (BookmarkItem*)getToolbarRootItem;
- (BookmarkItem*)getDockMenuRootItem;
- (nsIDOMDocument*)getBookmarksDocument; // addrefs
- (NSArray*)getBookmarkGroupURIs:(BookmarkItem*)item;
@ -234,6 +254,8 @@ protected:
- (void)addNewBookmark:(NSString*)url title:(NSString*)title withParent:(nsIContent*)parent;
- (void)addNewBookmarkFolder:(NSString*)title withParent:(nsIContent*)parent;
- (NSImage*)createIconForBookmarkItem:(BookmarkItem*)inItem useSiteIcon:(BOOL)useSiteIcon;
// itemsArray is an array of NSDictionaries, with "href" and "title" entries
- (void)addNewBookmarkGroup:(NSString*)titleString items:(NSArray*)itemsArray withParent:(nsIContent*)parent;

Просмотреть файл

@ -20,6 +20,7 @@
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Simon Fraser <sfraser@netscape.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
@ -369,8 +370,14 @@ BookmarksService::BookmarkRemoved(nsIContent* aContainer, nsIContent* aChild, bo
{
NotifyDescendents(aContainer, aChild, PR_TRUE, NotifyBookmarkRemoved);
// remove the bookmark item after everyone has been notified
PRUint32 contentID = 0;
aChild->GetContentID(&contentID);
// the BookmarkItem is released here
[gBookmarkItemDictionary removeObjectForKey:[NSNumber numberWithInt:contentID]];
if (shouldFlush)
FlushBookmarks();
FlushBookmarks();
}
void
@ -567,13 +574,13 @@ BookmarksService::ReadBookmarks()
// behaviour.
nsCOMPtr<nsIDOMDocument> bookmarksDOMDoc;
nsCOMPtr<nsISyncLoadDOMService> loader = do_GetService("@mozilla.org/content/syncload-dom-service;1");
if (loader) {
if (loader)
{
nsCOMPtr<nsIChannel> channel;
NS_NewChannel(getter_AddRefs(channel), uri, nsnull, nsnull);
if (channel) {
loader->LoadLocalDocument(channel, nsnull,
getter_AddRefs(bookmarksDOMDoc));
if (channel)
{
loader->LoadLocalDocument(channel, nsnull, getter_AddRefs(bookmarksDOMDoc));
if (bookmarksDOMDoc)
CallQueryInterface(bookmarksDOMDoc, &gBookmarksDocument); // addrefs
}
@ -583,8 +590,9 @@ BookmarksService::ReadBookmarks()
// that has a <parsererror> node as the root.
BOOL validBookmarksFile = CheckXMLDocumentParseSuccessful(bookmarksDOMDoc);
if (!validBookmarksFile) {
// uh oh, parser error. Throw some UI
if (!validBookmarksFile)
{
// uh oh, parser error, or we just failed to load it. Throw some UI
NSString *alert = NSLocalizedString(@"CorruptedBookmarksAlert",@"");
NSString *message = NSLocalizedString(@"CorruptedBookmarksMsg",@"");
NSString *okButton = NSLocalizedString(@"OKButtonText",@"");
@ -1534,8 +1542,6 @@ BookmarksService::PerformURLDrop(BookmarkItem* parentItem, BookmarkItem* beforeI
@interface BookmarkItem(Private)
- (NSString*)getAttributeValue:(nsIAtom*)atom;
@end
@ -1577,6 +1583,16 @@ BookmarksService::PerformURLDrop(BookmarkItem* parentItem, BookmarkItem* beforeI
return @"";
}
- (void)setAttribute:(nsIAtom*)atom toValue:(NSString*)value
{
if (mContentNode)
{
nsAutoString attrString;
[value assignTo_nsAString:attrString];
mContentNode->SetAttr(kNameSpaceID_None, atom, attrString, PR_TRUE);
}
}
- (NSString *)description
{
NSString* info = [self getAttributeValue:BookmarksService::gNameAtom];
@ -1603,6 +1619,26 @@ BookmarksService::PerformURLDrop(BookmarkItem* parentItem, BookmarkItem* beforeI
return [self getAttributeValue:BookmarksService::gDescriptionAtom];
}
- (void)setName:(NSString*)inName
{
[self setAttribute:BookmarksService::gNameAtom toValue:inName];
}
- (void)setUrl:(NSString*)inName
{
[self setAttribute:BookmarksService::gHrefAtom toValue:inName];
}
- (void)setKeyword:(NSString*)inName
{
[self setAttribute:BookmarksService::gKeywordAtom toValue:inName];
}
- (void)setDescriptionString:(NSString*)inName
{
[self setAttribute:BookmarksService::gDescriptionAtom toValue:inName];
}
- (void)setSiteIcon:(NSImage*)image
{
[mSiteIcon autorelease];
@ -1657,6 +1693,27 @@ BookmarksService::PerformURLDrop(BookmarkItem* parentItem, BookmarkItem* beforeI
return !group.IsEmpty();
}
- (void)setIsGroup:(BOOL)isGroup
{
if (isGroup)
mContentNode->SetAttr(kNameSpaceID_None, BookmarksService::gGroupAtom, NS_LITERAL_STRING("true"), PR_TRUE);
else
mContentNode->UnsetAttr(kNameSpaceID_None, BookmarksService::gGroupAtom, PR_TRUE);
}
- (BOOL)isExpanded
{
return (BOOL)mContentNode->HasAttr(kNameSpaceID_None, BookmarksService::gOpenAtom);
}
- (void)setIsExpanded:(BOOL)isExpanded
{
if (isExpanded)
mContentNode->SetAttr(kNameSpaceID_None, BookmarksService::gOpenAtom, NS_LITERAL_STRING("true"), PR_FALSE);
else
mContentNode->UnsetAttr(kNameSpaceID_None, BookmarksService::gOpenAtom, PR_FALSE);
}
- (BOOL)isToobarRoot
{
nsCOMPtr<nsIContent> toolbarRoot = do_QueryInterface(BookmarksService::gToolbarRoot);
@ -1688,6 +1745,31 @@ BookmarksService::PerformURLDrop(BookmarkItem* parentItem, BookmarkItem* beforeI
return nil;
}
- (NSArray*)getChildren
{
PRInt32 numChildren;
mContentNode->ChildCount(numChildren);
if (numChildren == 0)
return nil;
NSMutableArray* array = [[[NSMutableArray alloc] initWithCapacity:numChildren] autorelease];
for (PRInt32 i = 0; i < numChildren; i ++)
{
nsCOMPtr<nsIContent> childContent;
mContentNode->ChildAt(i, *getter_AddRefs(childContent));
[array addObject:BookmarksService::GetWrapperFor(childContent)];
}
return array;
}
- (int)getNumberOfChildren
{
PRInt32 numChildren;
mContentNode->ChildCount(numChildren);
return numChildren;
}
@end
@ -1936,6 +2018,25 @@ static BOOL gMadeBMManager;
return rootAsContent;
}
- (BookmarkItem*)getRootItem
{
nsCOMPtr<nsIContent> rootContent;
BookmarksService::GetRootContent(getter_AddRefs(rootContent));
return BookmarksService::GetWrapperFor(rootContent);
}
- (BookmarkItem*)getToolbarRootItem
{
nsCOMPtr<nsIContent> toolbarRoot = do_QueryInterface(BookmarksService::gToolbarRoot);
return BookmarksService::GetWrapperFor(toolbarRoot);
}
- (BookmarkItem*)getDockMenuRootItem
{
nsCOMPtr<nsIContent> dockRoot = do_QueryInterface(BookmarksService::gDockMenuRoot);
return BookmarksService::GetWrapperFor(dockRoot);
}
- (nsIDOMDocument*)getBookmarksDocument
{
nsIDOMDocument* domDoc = NULL;
@ -2053,6 +2154,28 @@ static BOOL gMadeBMManager;
return [NSArray arrayWithObject:locationString];
}
- (NSImage*)createIconForBookmarkItem:(BookmarkItem*)inItem useSiteIcon:(BOOL)useSiteIcon
{
if ([inItem isGroup])
return [NSImage imageNamed:@"groupbookmark"];
if ([inItem isFolder])
return [NSImage imageNamed:@"folder"];
// fire off a site icon load
if (useSiteIcon && [self useSiteIcons])
{
if ([inItem siteIcon])
return [inItem siteIcon];
NSString* url = [inItem url];
if ([url length] > 0)
[self loadProxyImageFor:inItem withURI:url];
}
return [NSImage imageNamed:@"smallbookmark"];
}
// return value is addreffed
// XXX factor this code with BookmarksService code
- (nsIDOMElement*)createNewBookmarkElement:(NSString*)urlString title:(NSString*)titleString itemType:(EBookmarkItemType)itemType
@ -2273,9 +2396,6 @@ static BOOL gMadeBMManager;
if ([siteBookmarks count] == 0)
[gBookmarkFaviconURLDictionary removeObjectForKey:faviconURL]; // release the dict too
}
[[bmItem retain] autorelease]; // keep the bm item alive until autorelease time
[gBookmarkItemDictionary removeObjectForKey:[NSNumber numberWithInt:contentID]];
}
- (void)bookmarkChanged:(nsIContent*)bookmark

Просмотреть файл

@ -42,9 +42,9 @@ class nsIDOMElement;
// Called to construct & edit the initial set of personal toolbar buttons.
-(void)buildButtonList;
-(void)addButton: (nsIDOMElement*)aElt atIndex: (int)aIndex;
-(void)editButton: (nsIDOMElement*)aElt;
-(void)removeButton: (nsIDOMElement*)aElt;
-(void)addButton:(BookmarkItem*)aItem atIndex:(int)aIndex;
-(void)editButton:(BookmarkItem*)aItem;
-(void)removeButton:(BookmarkItem*)aItem;
// Called to lay out the buttons on the toolbar.
-(void)reflowButtons;

Просмотреть файл

@ -30,7 +30,6 @@
#import "BrowserWindowController.h"
#import "BookmarksDataSource.h"
#include "nsIDOMElement.h"
#include "nsIContent.h"
#include "nsIAtom.h"
@ -40,7 +39,7 @@
- (void)registerForShutdownNotification;
- (void)setButtonInsertionPoint:(id <NSDraggingInfo>)sender;
- (NSRect)insertionRectForButton:(NSView*)aButton position:(int)aPosition;
- (BookmarksButton*)makeNewButtonWithElement:(nsIDOMElement*)element;
- (BookmarksButton*)makeNewButtonWithItem:(BookmarkItem*)aItem;
@end
@ -122,25 +121,15 @@
-(void)buildButtonList
{
// gToolbarRoot may be nil
if (BookmarksService::gToolbarRoot)
NSArray* toolbarChildren = [[[BookmarksManager sharedBookmarksManager] getToolbarRootItem] getChildren];
for (unsigned int i = 0; i < [toolbarChildren count]; i ++)
{
// Build the buttons, and then lay them all out.
nsCOMPtr<nsIDOMNode> child;
BookmarksService::gToolbarRoot->GetFirstChild(getter_AddRefs(child));
while (child) {
nsCOMPtr<nsIDOMElement> childElt(do_QueryInterface(child));
if (childElt) {
BookmarksButton* button = [self makeNewButtonWithElement:childElt];
[self addSubview: button];
[mButtons addObject: button];
}
nsCOMPtr<nsIDOMNode> temp = child;
temp->GetNextSibling(getter_AddRefs(child));
}
BookmarksButton* button = [self makeNewButtonWithItem:[toolbarChildren objectAtIndex:i]];
[self addSubview: button];
[mButtons addObject: button];
}
if ([self isShown])
[self reflowButtons];
}
@ -158,22 +147,24 @@
[self setNeedsDisplay:YES];
}
-(void)addButton: (nsIDOMElement*)aElt atIndex: (int)aIndex
-(void)addButton:(BookmarkItem*)aItem atIndex:(int)aIndex
{
BookmarksButton* button = [self makeNewButtonWithElement:aElt];
BookmarksButton* button = [self makeNewButtonWithItem:aItem];
[self addSubview: button];
[mButtons insertObject: button atIndex: aIndex];
if ([self isShown])
[self reflowButtonsStartingAtIndex: aIndex];
}
-(void)editButton: (nsIDOMElement*)aElt
-(void)editButton:(BookmarkItem*)aItem
{
int count = [mButtons count];
for (int i = 0; i < count; i++) {
for (int i = 0; i < count; i++)
{
BookmarksButton* button = [mButtons objectAtIndex: i];
if ([button element] == aElt) {
[button setElement: aElt];
if ([button getItem] == aItem)
{
[button setItem: aItem];
if (count > i && [self isShown])
[self reflowButtonsStartingAtIndex: i];
break;
@ -183,12 +174,15 @@
[self setNeedsDisplay:YES];
}
-(void)removeButton: (nsIDOMElement*)aElt
-(void)removeButton:(BookmarkItem*)aItem
{
int count = [mButtons count];
for (int i = 0; i < count; i++) {
for (int i = 0; i < count; i++)
{
BookmarksButton* button = [mButtons objectAtIndex: i];
if ([button element] == aElt) {
NSLog(@"Looking at button with item %x, %@, for item %x", [button getItem], [button getItem], aItem);
if ([button getItem] == aItem)
{
[mButtons removeObjectAtIndex: i];
[button removeFromSuperview];
if (count > i && [self isShown])
@ -388,12 +382,9 @@
if (foundView && [foundView isMemberOfClass:[BookmarksButton class]])
{
BookmarksButton* targetButton = foundView;
nsCOMPtr<nsIAtom> tagName;
nsCOMPtr<nsIContent> contentNode = do_QueryInterface([targetButton element]);
contentNode->GetTag(*getter_AddRefs(tagName));
if (tagName == BookmarksService::gFolderAtom)
BookmarkItem* targetItem = [targetButton getItem];
if ([targetItem isFolder])
{
mDragInsertionButton = targetButton;
mDragInsertionPosition = BookmarksService::CHInsertInto;
@ -431,29 +422,26 @@
{
NSArray *draggedIDs = [draggingPasteboard propertyListForType: @"MozBookmarkType"];
nsCOMPtr<nsIContent> destinationContent;
BookmarkItem* destItem = nil;
int index = 0;
if (mDragInsertionPosition == BookmarksService::CHInsertInto) // drop onto folder
{
nsCOMPtr<nsIDOMElement> parentElt = [mDragInsertionButton element];
destinationContent = do_QueryInterface(parentElt);
destItem = [mDragInsertionButton getItem];
index = 0;
}
else if (mDragInsertionPosition == BookmarksService::CHInsertBefore ||
mDragInsertionPosition == BookmarksService::CHInsertAfter) // drop onto toolbar
{
nsCOMPtr<nsIDOMElement> toolbarRoot = BookmarksService::gToolbarRoot;
destinationContent = do_QueryInterface(toolbarRoot);
index = [mButtons indexOfObject: mDragInsertionButton];
destItem = [[BookmarksManager sharedBookmarksManager] getToolbarRootItem];
index = [mButtons indexOfObject:mDragInsertionButton];
if (mDragInsertionPosition == BookmarksService::CHInsertAfter)
++index;
}
// we rely on IsBookmarkDropValid to filter out drops where the source
// and destination are the same.
BookmarkItem* toolbarFolderItem = BookmarksService::GetWrapperFor(destinationContent);
if (!BookmarksService::IsBookmarkDropValid(toolbarFolderItem, index, draggedIDs, isCopy)) {
if (!BookmarksService::IsBookmarkDropValid(destItem, index, draggedIDs, isCopy)) {
return NO;
}
}
@ -513,7 +501,7 @@
- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
{
BookmarkItem* parent = nsnull;
BookmarkItem* parent = nil;
int index = 0;
if (!BookmarksService::gToolbarRoot)
@ -521,20 +509,16 @@
if (mDragInsertionPosition == BookmarksService::CHInsertInto) // drop onto folder
{
nsCOMPtr<nsIDOMElement> parentElt = [mDragInsertionButton element];
nsCOMPtr<nsIContent> parentContent(do_QueryInterface(parentElt));
parent = BookmarksService::GetWrapperFor(parentContent);
parent = [mDragInsertionButton getItem];
index = 0;
}
else if (mDragInsertionPosition == BookmarksService::CHInsertBefore ||
mDragInsertionPosition == BookmarksService::CHInsertAfter) // drop onto toolbar
{
nsCOMPtr<nsIDOMElement> rootElt = BookmarksService::gToolbarRoot;
nsCOMPtr<nsIContent> rootContent(do_QueryInterface(rootElt));
parent = BookmarksService::GetWrapperFor(rootContent);
parent = [[BookmarksManager sharedBookmarksManager] getToolbarRootItem];
index = [mButtons indexOfObject: mDragInsertionButton];
if (index == NSNotFound)
rootContent->ChildCount(index);
index = [parent getNumberOfChildren];
else if (mDragInsertionPosition == BookmarksService::CHInsertAfter)
index++;
}
@ -595,44 +579,45 @@
}
}
- (BookmarksButton*)makeNewButtonWithElement:(nsIDOMElement*)element
- (BookmarksButton*)makeNewButtonWithItem:(BookmarkItem*)aItem
{
return [[[BookmarksButton alloc] initWithFrame: NSMakeRect(2, 1, 100, 17) element:element] autorelease];
return [[[BookmarksButton alloc] initWithFrame: NSMakeRect(2, 1, 100, 17) item:aItem] autorelease];
}
#pragma mark -
- (void)bookmarkAdded:(nsIContent*)bookmark inContainer:(nsIContent*)container isChangedRoot:(BOOL)isRoot
{
//NSLog(@"Toolbar notified that %x added in %x, is root %d", bookmark, container, isRoot);
NSLog(@"Toolbar notified that %x added in %x, is root %d", bookmark, container, isRoot);
nsCOMPtr<nsIContent> toolbarRootContent = getter_AddRefs([[BookmarksManager sharedBookmarksManager] getToolbarRoot]);
if (container == toolbarRootContent.get())
{
// We only care about changes that occur to the personal toolbar's immediate
// children.
// We only care about changes that occur to the personal toolbar's immediate children.
PRInt32 index = -1;
container->IndexOf(bookmark, index);
nsCOMPtr<nsIDOMElement> elt = do_QueryInterface(bookmark);
[self addButton: elt atIndex:index];
BookmarkItem* item = BookmarksService::GetWrapperFor(bookmark);
[self addButton:item atIndex:index];
}
}
- (void)bookmarkRemoved:(nsIContent*)bookmark inContainer:(nsIContent*)container isChangedRoot:(BOOL)isRoot
{
NSLog(@"Toolbar notified that %x removed in %x, is root %d", bookmark, container, isRoot);
nsCOMPtr<nsIContent> toolbarRootContent = getter_AddRefs([[BookmarksManager sharedBookmarksManager] getToolbarRoot]);
if (container == toolbarRootContent.get())
{
// We only care about changes that occur to the personal toolbar's immediate
// children.
nsCOMPtr<nsIDOMElement> childElt = do_QueryInterface(bookmark);
[self removeButton: childElt];
BookmarkItem* item = BookmarksService::GetWrapperFor(bookmark);
[self removeButton:item];
}
}
- (void)bookmarkChanged:(nsIContent*)bookmark
{
nsCOMPtr<nsIDOMElement> elt = do_QueryInterface(bookmark);
[self editButton:elt];
BookmarkItem* item = BookmarksService::GetWrapperFor(bookmark);
[self editButton:item];
}
- (void)specialFolder:(EBookmarksFolderType)folderType changedTo:(nsIContent*)newFolderContent