bookmark keyword back end + fix for missing bookmark file causes crash on startup

This commit is contained in:
ben%netscape.com 2002-06-05 14:24:54 +00:00
parent b19862423a
commit 55f4aee173
14 changed files with 402 additions and 12 deletions

View File

@ -83,6 +83,8 @@ class nsIAtom;
-(void)addBookmark:(id)aSender useSelection:(BOOL)aSel isFolder:(BOOL)aIsFolder;
-(NSString*)resolveKeyword:(NSString*)aKeyword;
-(IBAction)beginRenameBookmark:(id)sender;
-(IBAction)cancelRenameBookmarkSheet:(id)sender;
-(IBAction)doRenameBookmarkSheet:(id)sender;
@ -156,6 +158,8 @@ public:
static void GetTitleAndHrefForBrowserView(id aBrowserView, nsString& aTitle, nsString& aHref);
static void OpenBookmarkGroup(id aTabView, nsIDOMElement* aFolder);
static NSString* ResolveKeyword(NSString* aKeyword);
static NSImage* CreateIconForBookmark(nsIDOMElement* aElement);

View File

@ -38,6 +38,7 @@
#import "CHBrowserView.h"
#import "BookmarksService.h"
#import "BookmarkInfoController.h"
#import "StringUtils.h"
#include "nsIDocument.h"
#include "nsIContent.h"
#include "nsIAtom.h"
@ -352,6 +353,10 @@
}
}
-(NSString*) resolveKeyword: (NSString*) aKeyword
{
return BookmarksService::ResolveKeyword(aKeyword);
}
//
// outlineView:shouldEditTableColumn:item: (delegate method)
@ -638,6 +643,9 @@
[mRenameSheet orderOut:self];
[NSApp endSheet:mRenameSheet returnCode:0];
nsAutoString buff;
NSStringTo_nsString([mRenameTextField stringValue], buff);
#if 0
// extract the string from the text field into a unicode buffer
unsigned int len = [[mRenameTextField stringValue] length];
PRUnichar* buffer = new PRUnichar[len + 1];
@ -645,11 +653,11 @@
return;
[[mRenameTextField stringValue] getCharacters:buffer];
buffer[len] = (PRUnichar)'\0';
nsXPIDLString buff; buff.Adopt(buffer);
#endif
// stuff it into our bookmarks item. |buff| takes ownership of |buffer| so
// it doesn't have to be deleted manually
BookmarkItem* item = [mOutlineView itemAtRow: [mOutlineView selectedRow]];
nsXPIDLString buff; buff.Adopt(buffer);
[item contentNode]->SetAttr(kNameSpaceID_None, BookmarksService::gNameAtom, buff, PR_TRUE);
mBookmarks->BookmarkChanged([item contentNode]);
}
@ -1034,12 +1042,39 @@ BookmarksService::AddObserver()
NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR, getter_AddRefs(profileDir));
profileDir->Append(NS_LITERAL_STRING("bookmarks.xml"));
PRBool fileExists;
profileDir->Exists(&fileExists);
// If the bookmarks file does not exist, copy from the defaults so we don't
// crash or anything dumb like that.
if (!fileExists) {
nsCOMPtr<nsIFile> defaultBookmarksFile;
NS_GetSpecialDirectory(NS_APP_PROFILE_DEFAULTS_50_DIR, getter_AddRefs(defaultBookmarksFile));
defaultBookmarksFile->Append(NS_LITERAL_STRING("bookmarks.xml"));
// XXX for some reason unknown to me, leaving this code in causes the program to crash
// with 'cannot dereference null COMPtr.'
#if I_WANT_TO_CRASH
PRBool defaultFileExists;
defaultBookmarksFile->Exists(&defaultFileExists);
if (defaultFileExists)
return;
#endif
nsCOMPtr<nsIFile> profileDirectory;
NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR, getter_AddRefs(profileDirectory));
defaultBookmarksFile->CopyToNative(profileDirectory, NS_LITERAL_CSTRING("bookmarks.xml"));
}
nsCAutoString bookmarksFileURL;
NS_GetURLSpecFromFile(profileDir, bookmarksFileURL);
nsCOMPtr<nsIURI> uri;
NS_NewURI(getter_AddRefs(uri), bookmarksFileURL.get());
// XXX this is somewhat lame. we have no way of knowing whether or not the parse succeeded
// or failed. sigh.
nsCOMPtr<nsIXBLService> xblService(do_GetService("@mozilla.org/xbl;1"));
xblService->FetchSyncXMLDocument(uri, &gBookmarks); // The addref is here.
@ -1147,10 +1182,13 @@ BookmarksService::DeleteBookmark(nsIDOMElement* aBookmark)
void
BookmarksService::FlushBookmarks()
{
// XXX we need to insert a mechanism here to ensure that we don't write corrupt
// bookmarks files (e.g. full disk, program crash, whatever), because our
// error handling in the parse stage is NON-EXISTENT.
nsCOMPtr<nsIFile> bookmarksFile;
NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR, getter_AddRefs(bookmarksFile));
bookmarksFile->Append(NS_LITERAL_STRING("bookmarks.xml"));
nsCOMPtr<nsIOutputStream> outputStream;
NS_NewLocalFileOutputStream(getter_AddRefs(outputStream), bookmarksFile);
@ -1586,6 +1624,30 @@ BookmarksService::OpenBookmarkGroup(id aTabView, nsIDOMElement* aFolder)
[[[[aTabView tabViewItemAtIndex: 0] view] getBrowserView] setActive: YES];
}
NSString*
BookmarksService::ResolveKeyword(NSString* aKeyword)
{
nsAutoString keyword;
NSStringTo_nsString(aKeyword, keyword);
if (keyword.IsEmpty())
return [NSString stringWithCString:""];
NSLog(@"str = %s", keyword.get());
nsCOMPtr<nsIDOMDocument> domDoc(do_QueryInterface(gBookmarks));
nsCOMPtr<nsIDOMElement> elt;
domDoc->GetElementById(keyword, getter_AddRefs(elt));
nsCOMPtr<nsIContent> content(do_QueryInterface(elt));
nsAutoString url;
if (content) {
content->GetAttr(kNameSpaceID_None, gHrefAtom, url);
return [NSString stringWithCharacters: url.get() length: url.Length()];
}
return [NSString stringWithCString:""];
}
NSImage*
BookmarksService::CreateIconForBookmark(nsIDOMElement* aElement)
{

View File

@ -416,6 +416,7 @@
2EEC3E63028138724B000102,
2E748B74029A448D4B000102,
F5383FCE02AC8F8E01A80164,
F538C86C02AE2E6701A80166,
);
isa = PBXHeadersBuildPhase;
name = Headers;
@ -1053,6 +1054,7 @@
F52F87CC027D75C301A80165,
2E748B72029A448D4B000102,
F5383FCC02AC8F8E01A80164,
F538C86B02AE2E6701A80166,
);
isa = PBXGroup;
name = Headers;
@ -1892,6 +1894,17 @@
settings = {
};
};
F538C86B02AE2E6701A80166 = {
isa = PBXFileReference;
path = StringUtils.h;
refType = 4;
};
F538C86C02AE2E6701A80166 = {
fileRef = F538C86B02AE2E6701A80166;
isa = PBXBuildFile;
settings = {
};
};
F538DFAE023B36A7010001CA = {
children = (
F538DFAF023B3739010001CA,

37
camino/StringUtils.h Normal file
View File

@ -0,0 +1,37 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* The contents of this file are subject to the Mozilla Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is the Mozilla browser.
*
* The Initial Developer of the Original Code is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 2002 Netscape Communications Corporation. All
* Rights Reserved.
*
* Contributor(s):
*/
// Utility to convert |NSString|s to unicode |nsString|s to save having
// to repeat this set of code each time someone wants to do this...
void
NSStringTo_nsString(NSString* aNSString, nsString& ansString)
{
unsigned int len = [aNSString length];
PRUnichar* buffer = new PRUnichar[len + 1];
if (!buffer)
return;
[aNSString getCharacters: buffer];
buffer[len] = (PRUnichar)'\0';
ansString.Adopt(buffer);
}

View File

@ -416,6 +416,7 @@
2EEC3E63028138724B000102,
2E748B74029A448D4B000102,
F5383FCE02AC8F8E01A80164,
F538C86C02AE2E6701A80166,
);
isa = PBXHeadersBuildPhase;
name = Headers;
@ -1053,6 +1054,7 @@
F52F87CC027D75C301A80165,
2E748B72029A448D4B000102,
F5383FCC02AC8F8E01A80164,
F538C86B02AE2E6701A80166,
);
isa = PBXGroup;
name = Headers;
@ -1892,6 +1894,17 @@
settings = {
};
};
F538C86B02AE2E6701A80166 = {
isa = PBXFileReference;
path = StringUtils.h;
refType = 4;
};
F538C86C02AE2E6701A80166 = {
fileRef = F538C86B02AE2E6701A80166;
isa = PBXBuildFile;
settings = {
};
};
F538DFAE023B36A7010001CA = {
children = (
F538DFAF023B3739010001CA,

View File

@ -83,6 +83,8 @@ class nsIAtom;
-(void)addBookmark:(id)aSender useSelection:(BOOL)aSel isFolder:(BOOL)aIsFolder;
-(NSString*)resolveKeyword:(NSString*)aKeyword;
-(IBAction)beginRenameBookmark:(id)sender;
-(IBAction)cancelRenameBookmarkSheet:(id)sender;
-(IBAction)doRenameBookmarkSheet:(id)sender;
@ -156,6 +158,8 @@ public:
static void GetTitleAndHrefForBrowserView(id aBrowserView, nsString& aTitle, nsString& aHref);
static void OpenBookmarkGroup(id aTabView, nsIDOMElement* aFolder);
static NSString* ResolveKeyword(NSString* aKeyword);
static NSImage* CreateIconForBookmark(nsIDOMElement* aElement);

View File

@ -38,6 +38,7 @@
#import "CHBrowserView.h"
#import "BookmarksService.h"
#import "BookmarkInfoController.h"
#import "StringUtils.h"
#include "nsIDocument.h"
#include "nsIContent.h"
#include "nsIAtom.h"
@ -352,6 +353,10 @@
}
}
-(NSString*) resolveKeyword: (NSString*) aKeyword
{
return BookmarksService::ResolveKeyword(aKeyword);
}
//
// outlineView:shouldEditTableColumn:item: (delegate method)
@ -638,6 +643,9 @@
[mRenameSheet orderOut:self];
[NSApp endSheet:mRenameSheet returnCode:0];
nsAutoString buff;
NSStringTo_nsString([mRenameTextField stringValue], buff);
#if 0
// extract the string from the text field into a unicode buffer
unsigned int len = [[mRenameTextField stringValue] length];
PRUnichar* buffer = new PRUnichar[len + 1];
@ -645,11 +653,11 @@
return;
[[mRenameTextField stringValue] getCharacters:buffer];
buffer[len] = (PRUnichar)'\0';
nsXPIDLString buff; buff.Adopt(buffer);
#endif
// stuff it into our bookmarks item. |buff| takes ownership of |buffer| so
// it doesn't have to be deleted manually
BookmarkItem* item = [mOutlineView itemAtRow: [mOutlineView selectedRow]];
nsXPIDLString buff; buff.Adopt(buffer);
[item contentNode]->SetAttr(kNameSpaceID_None, BookmarksService::gNameAtom, buff, PR_TRUE);
mBookmarks->BookmarkChanged([item contentNode]);
}
@ -1034,12 +1042,39 @@ BookmarksService::AddObserver()
NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR, getter_AddRefs(profileDir));
profileDir->Append(NS_LITERAL_STRING("bookmarks.xml"));
PRBool fileExists;
profileDir->Exists(&fileExists);
// If the bookmarks file does not exist, copy from the defaults so we don't
// crash or anything dumb like that.
if (!fileExists) {
nsCOMPtr<nsIFile> defaultBookmarksFile;
NS_GetSpecialDirectory(NS_APP_PROFILE_DEFAULTS_50_DIR, getter_AddRefs(defaultBookmarksFile));
defaultBookmarksFile->Append(NS_LITERAL_STRING("bookmarks.xml"));
// XXX for some reason unknown to me, leaving this code in causes the program to crash
// with 'cannot dereference null COMPtr.'
#if I_WANT_TO_CRASH
PRBool defaultFileExists;
defaultBookmarksFile->Exists(&defaultFileExists);
if (defaultFileExists)
return;
#endif
nsCOMPtr<nsIFile> profileDirectory;
NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR, getter_AddRefs(profileDirectory));
defaultBookmarksFile->CopyToNative(profileDirectory, NS_LITERAL_CSTRING("bookmarks.xml"));
}
nsCAutoString bookmarksFileURL;
NS_GetURLSpecFromFile(profileDir, bookmarksFileURL);
nsCOMPtr<nsIURI> uri;
NS_NewURI(getter_AddRefs(uri), bookmarksFileURL.get());
// XXX this is somewhat lame. we have no way of knowing whether or not the parse succeeded
// or failed. sigh.
nsCOMPtr<nsIXBLService> xblService(do_GetService("@mozilla.org/xbl;1"));
xblService->FetchSyncXMLDocument(uri, &gBookmarks); // The addref is here.
@ -1147,10 +1182,13 @@ BookmarksService::DeleteBookmark(nsIDOMElement* aBookmark)
void
BookmarksService::FlushBookmarks()
{
// XXX we need to insert a mechanism here to ensure that we don't write corrupt
// bookmarks files (e.g. full disk, program crash, whatever), because our
// error handling in the parse stage is NON-EXISTENT.
nsCOMPtr<nsIFile> bookmarksFile;
NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR, getter_AddRefs(bookmarksFile));
bookmarksFile->Append(NS_LITERAL_STRING("bookmarks.xml"));
nsCOMPtr<nsIOutputStream> outputStream;
NS_NewLocalFileOutputStream(getter_AddRefs(outputStream), bookmarksFile);
@ -1586,6 +1624,30 @@ BookmarksService::OpenBookmarkGroup(id aTabView, nsIDOMElement* aFolder)
[[[[aTabView tabViewItemAtIndex: 0] view] getBrowserView] setActive: YES];
}
NSString*
BookmarksService::ResolveKeyword(NSString* aKeyword)
{
nsAutoString keyword;
NSStringTo_nsString(aKeyword, keyword);
if (keyword.IsEmpty())
return [NSString stringWithCString:""];
NSLog(@"str = %s", keyword.get());
nsCOMPtr<nsIDOMDocument> domDoc(do_QueryInterface(gBookmarks));
nsCOMPtr<nsIDOMElement> elt;
domDoc->GetElementById(keyword, getter_AddRefs(elt));
nsCOMPtr<nsIContent> content(do_QueryInterface(elt));
nsAutoString url;
if (content) {
content->GetAttr(kNameSpaceID_None, gHrefAtom, url);
return [NSString stringWithCharacters: url.get() length: url.Length()];
}
return [NSString stringWithCString:""];
}
NSImage*
BookmarksService::CreateIconForBookmark(nsIDOMElement* aElement)
{

View File

@ -83,6 +83,8 @@ class nsIAtom;
-(void)addBookmark:(id)aSender useSelection:(BOOL)aSel isFolder:(BOOL)aIsFolder;
-(NSString*)resolveKeyword:(NSString*)aKeyword;
-(IBAction)beginRenameBookmark:(id)sender;
-(IBAction)cancelRenameBookmarkSheet:(id)sender;
-(IBAction)doRenameBookmarkSheet:(id)sender;
@ -156,6 +158,8 @@ public:
static void GetTitleAndHrefForBrowserView(id aBrowserView, nsString& aTitle, nsString& aHref);
static void OpenBookmarkGroup(id aTabView, nsIDOMElement* aFolder);
static NSString* ResolveKeyword(NSString* aKeyword);
static NSImage* CreateIconForBookmark(nsIDOMElement* aElement);

View File

@ -38,6 +38,7 @@
#import "CHBrowserView.h"
#import "BookmarksService.h"
#import "BookmarkInfoController.h"
#import "StringUtils.h"
#include "nsIDocument.h"
#include "nsIContent.h"
#include "nsIAtom.h"
@ -352,6 +353,10 @@
}
}
-(NSString*) resolveKeyword: (NSString*) aKeyword
{
return BookmarksService::ResolveKeyword(aKeyword);
}
//
// outlineView:shouldEditTableColumn:item: (delegate method)
@ -638,6 +643,9 @@
[mRenameSheet orderOut:self];
[NSApp endSheet:mRenameSheet returnCode:0];
nsAutoString buff;
NSStringTo_nsString([mRenameTextField stringValue], buff);
#if 0
// extract the string from the text field into a unicode buffer
unsigned int len = [[mRenameTextField stringValue] length];
PRUnichar* buffer = new PRUnichar[len + 1];
@ -645,11 +653,11 @@
return;
[[mRenameTextField stringValue] getCharacters:buffer];
buffer[len] = (PRUnichar)'\0';
nsXPIDLString buff; buff.Adopt(buffer);
#endif
// stuff it into our bookmarks item. |buff| takes ownership of |buffer| so
// it doesn't have to be deleted manually
BookmarkItem* item = [mOutlineView itemAtRow: [mOutlineView selectedRow]];
nsXPIDLString buff; buff.Adopt(buffer);
[item contentNode]->SetAttr(kNameSpaceID_None, BookmarksService::gNameAtom, buff, PR_TRUE);
mBookmarks->BookmarkChanged([item contentNode]);
}
@ -1034,12 +1042,39 @@ BookmarksService::AddObserver()
NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR, getter_AddRefs(profileDir));
profileDir->Append(NS_LITERAL_STRING("bookmarks.xml"));
PRBool fileExists;
profileDir->Exists(&fileExists);
// If the bookmarks file does not exist, copy from the defaults so we don't
// crash or anything dumb like that.
if (!fileExists) {
nsCOMPtr<nsIFile> defaultBookmarksFile;
NS_GetSpecialDirectory(NS_APP_PROFILE_DEFAULTS_50_DIR, getter_AddRefs(defaultBookmarksFile));
defaultBookmarksFile->Append(NS_LITERAL_STRING("bookmarks.xml"));
// XXX for some reason unknown to me, leaving this code in causes the program to crash
// with 'cannot dereference null COMPtr.'
#if I_WANT_TO_CRASH
PRBool defaultFileExists;
defaultBookmarksFile->Exists(&defaultFileExists);
if (defaultFileExists)
return;
#endif
nsCOMPtr<nsIFile> profileDirectory;
NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR, getter_AddRefs(profileDirectory));
defaultBookmarksFile->CopyToNative(profileDirectory, NS_LITERAL_CSTRING("bookmarks.xml"));
}
nsCAutoString bookmarksFileURL;
NS_GetURLSpecFromFile(profileDir, bookmarksFileURL);
nsCOMPtr<nsIURI> uri;
NS_NewURI(getter_AddRefs(uri), bookmarksFileURL.get());
// XXX this is somewhat lame. we have no way of knowing whether or not the parse succeeded
// or failed. sigh.
nsCOMPtr<nsIXBLService> xblService(do_GetService("@mozilla.org/xbl;1"));
xblService->FetchSyncXMLDocument(uri, &gBookmarks); // The addref is here.
@ -1147,10 +1182,13 @@ BookmarksService::DeleteBookmark(nsIDOMElement* aBookmark)
void
BookmarksService::FlushBookmarks()
{
// XXX we need to insert a mechanism here to ensure that we don't write corrupt
// bookmarks files (e.g. full disk, program crash, whatever), because our
// error handling in the parse stage is NON-EXISTENT.
nsCOMPtr<nsIFile> bookmarksFile;
NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR, getter_AddRefs(bookmarksFile));
bookmarksFile->Append(NS_LITERAL_STRING("bookmarks.xml"));
nsCOMPtr<nsIOutputStream> outputStream;
NS_NewLocalFileOutputStream(getter_AddRefs(outputStream), bookmarksFile);
@ -1586,6 +1624,30 @@ BookmarksService::OpenBookmarkGroup(id aTabView, nsIDOMElement* aFolder)
[[[[aTabView tabViewItemAtIndex: 0] view] getBrowserView] setActive: YES];
}
NSString*
BookmarksService::ResolveKeyword(NSString* aKeyword)
{
nsAutoString keyword;
NSStringTo_nsString(aKeyword, keyword);
if (keyword.IsEmpty())
return [NSString stringWithCString:""];
NSLog(@"str = %s", keyword.get());
nsCOMPtr<nsIDOMDocument> domDoc(do_QueryInterface(gBookmarks));
nsCOMPtr<nsIDOMElement> elt;
domDoc->GetElementById(keyword, getter_AddRefs(elt));
nsCOMPtr<nsIContent> content(do_QueryInterface(elt));
nsAutoString url;
if (content) {
content->GetAttr(kNameSpaceID_None, gHrefAtom, url);
return [NSString stringWithCharacters: url.get() length: url.Length()];
}
return [NSString stringWithCString:""];
}
NSImage*
BookmarksService::CreateIconForBookmark(nsIDOMElement* aElement)
{

View File

@ -416,6 +416,7 @@
2EEC3E63028138724B000102,
2E748B74029A448D4B000102,
F5383FCE02AC8F8E01A80164,
F538C86C02AE2E6701A80166,
);
isa = PBXHeadersBuildPhase;
name = Headers;
@ -1053,6 +1054,7 @@
F52F87CC027D75C301A80165,
2E748B72029A448D4B000102,
F5383FCC02AC8F8E01A80164,
F538C86B02AE2E6701A80166,
);
isa = PBXGroup;
name = Headers;
@ -1892,6 +1894,17 @@
settings = {
};
};
F538C86B02AE2E6701A80166 = {
isa = PBXFileReference;
path = StringUtils.h;
refType = 4;
};
F538C86C02AE2E6701A80166 = {
fileRef = F538C86B02AE2E6701A80166;
isa = PBXBuildFile;
settings = {
};
};
F538DFAE023B36A7010001CA = {
children = (
F538DFAF023B3739010001CA,

37
chimera/StringUtils.h Normal file
View File

@ -0,0 +1,37 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* The contents of this file are subject to the Mozilla Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is the Mozilla browser.
*
* The Initial Developer of the Original Code is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 2002 Netscape Communications Corporation. All
* Rights Reserved.
*
* Contributor(s):
*/
// Utility to convert |NSString|s to unicode |nsString|s to save having
// to repeat this set of code each time someone wants to do this...
void
NSStringTo_nsString(NSString* aNSString, nsString& ansString)
{
unsigned int len = [aNSString length];
PRUnichar* buffer = new PRUnichar[len + 1];
if (!buffer)
return;
[aNSString getCharacters: buffer];
buffer[len] = (PRUnichar)'\0';
ansString.Adopt(buffer);
}

View File

@ -416,6 +416,7 @@
2EEC3E63028138724B000102,
2E748B74029A448D4B000102,
F5383FCE02AC8F8E01A80164,
F538C86C02AE2E6701A80166,
);
isa = PBXHeadersBuildPhase;
name = Headers;
@ -1053,6 +1054,7 @@
F52F87CC027D75C301A80165,
2E748B72029A448D4B000102,
F5383FCC02AC8F8E01A80164,
F538C86B02AE2E6701A80166,
);
isa = PBXGroup;
name = Headers;
@ -1892,6 +1894,17 @@
settings = {
};
};
F538C86B02AE2E6701A80166 = {
isa = PBXFileReference;
path = StringUtils.h;
refType = 4;
};
F538C86C02AE2E6701A80166 = {
fileRef = F538C86B02AE2E6701A80166;
isa = PBXBuildFile;
settings = {
};
};
F538DFAE023B36A7010001CA = {
children = (
F538DFAF023B3739010001CA,

View File

@ -83,6 +83,8 @@ class nsIAtom;
-(void)addBookmark:(id)aSender useSelection:(BOOL)aSel isFolder:(BOOL)aIsFolder;
-(NSString*)resolveKeyword:(NSString*)aKeyword;
-(IBAction)beginRenameBookmark:(id)sender;
-(IBAction)cancelRenameBookmarkSheet:(id)sender;
-(IBAction)doRenameBookmarkSheet:(id)sender;
@ -156,6 +158,8 @@ public:
static void GetTitleAndHrefForBrowserView(id aBrowserView, nsString& aTitle, nsString& aHref);
static void OpenBookmarkGroup(id aTabView, nsIDOMElement* aFolder);
static NSString* ResolveKeyword(NSString* aKeyword);
static NSImage* CreateIconForBookmark(nsIDOMElement* aElement);

View File

@ -38,6 +38,7 @@
#import "CHBrowserView.h"
#import "BookmarksService.h"
#import "BookmarkInfoController.h"
#import "StringUtils.h"
#include "nsIDocument.h"
#include "nsIContent.h"
#include "nsIAtom.h"
@ -352,6 +353,10 @@
}
}
-(NSString*) resolveKeyword: (NSString*) aKeyword
{
return BookmarksService::ResolveKeyword(aKeyword);
}
//
// outlineView:shouldEditTableColumn:item: (delegate method)
@ -638,6 +643,9 @@
[mRenameSheet orderOut:self];
[NSApp endSheet:mRenameSheet returnCode:0];
nsAutoString buff;
NSStringTo_nsString([mRenameTextField stringValue], buff);
#if 0
// extract the string from the text field into a unicode buffer
unsigned int len = [[mRenameTextField stringValue] length];
PRUnichar* buffer = new PRUnichar[len + 1];
@ -645,11 +653,11 @@
return;
[[mRenameTextField stringValue] getCharacters:buffer];
buffer[len] = (PRUnichar)'\0';
nsXPIDLString buff; buff.Adopt(buffer);
#endif
// stuff it into our bookmarks item. |buff| takes ownership of |buffer| so
// it doesn't have to be deleted manually
BookmarkItem* item = [mOutlineView itemAtRow: [mOutlineView selectedRow]];
nsXPIDLString buff; buff.Adopt(buffer);
[item contentNode]->SetAttr(kNameSpaceID_None, BookmarksService::gNameAtom, buff, PR_TRUE);
mBookmarks->BookmarkChanged([item contentNode]);
}
@ -1034,12 +1042,39 @@ BookmarksService::AddObserver()
NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR, getter_AddRefs(profileDir));
profileDir->Append(NS_LITERAL_STRING("bookmarks.xml"));
PRBool fileExists;
profileDir->Exists(&fileExists);
// If the bookmarks file does not exist, copy from the defaults so we don't
// crash or anything dumb like that.
if (!fileExists) {
nsCOMPtr<nsIFile> defaultBookmarksFile;
NS_GetSpecialDirectory(NS_APP_PROFILE_DEFAULTS_50_DIR, getter_AddRefs(defaultBookmarksFile));
defaultBookmarksFile->Append(NS_LITERAL_STRING("bookmarks.xml"));
// XXX for some reason unknown to me, leaving this code in causes the program to crash
// with 'cannot dereference null COMPtr.'
#if I_WANT_TO_CRASH
PRBool defaultFileExists;
defaultBookmarksFile->Exists(&defaultFileExists);
if (defaultFileExists)
return;
#endif
nsCOMPtr<nsIFile> profileDirectory;
NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR, getter_AddRefs(profileDirectory));
defaultBookmarksFile->CopyToNative(profileDirectory, NS_LITERAL_CSTRING("bookmarks.xml"));
}
nsCAutoString bookmarksFileURL;
NS_GetURLSpecFromFile(profileDir, bookmarksFileURL);
nsCOMPtr<nsIURI> uri;
NS_NewURI(getter_AddRefs(uri), bookmarksFileURL.get());
// XXX this is somewhat lame. we have no way of knowing whether or not the parse succeeded
// or failed. sigh.
nsCOMPtr<nsIXBLService> xblService(do_GetService("@mozilla.org/xbl;1"));
xblService->FetchSyncXMLDocument(uri, &gBookmarks); // The addref is here.
@ -1147,10 +1182,13 @@ BookmarksService::DeleteBookmark(nsIDOMElement* aBookmark)
void
BookmarksService::FlushBookmarks()
{
// XXX we need to insert a mechanism here to ensure that we don't write corrupt
// bookmarks files (e.g. full disk, program crash, whatever), because our
// error handling in the parse stage is NON-EXISTENT.
nsCOMPtr<nsIFile> bookmarksFile;
NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR, getter_AddRefs(bookmarksFile));
bookmarksFile->Append(NS_LITERAL_STRING("bookmarks.xml"));
nsCOMPtr<nsIOutputStream> outputStream;
NS_NewLocalFileOutputStream(getter_AddRefs(outputStream), bookmarksFile);
@ -1586,6 +1624,30 @@ BookmarksService::OpenBookmarkGroup(id aTabView, nsIDOMElement* aFolder)
[[[[aTabView tabViewItemAtIndex: 0] view] getBrowserView] setActive: YES];
}
NSString*
BookmarksService::ResolveKeyword(NSString* aKeyword)
{
nsAutoString keyword;
NSStringTo_nsString(aKeyword, keyword);
if (keyword.IsEmpty())
return [NSString stringWithCString:""];
NSLog(@"str = %s", keyword.get());
nsCOMPtr<nsIDOMDocument> domDoc(do_QueryInterface(gBookmarks));
nsCOMPtr<nsIDOMElement> elt;
domDoc->GetElementById(keyword, getter_AddRefs(elt));
nsCOMPtr<nsIContent> content(do_QueryInterface(elt));
nsAutoString url;
if (content) {
content->GetAttr(kNameSpaceID_None, gHrefAtom, url);
return [NSString stringWithCharacters: url.get() length: url.Length()];
}
return [NSString stringWithCString:""];
}
NSImage*
BookmarksService::CreateIconForBookmark(nsIDOMElement* aElement)
{