Renaming bookmarks via manage interface

This commit is contained in:
anthony 2021-01-12 20:05:10 +00:00
parent d1a4382fdf
commit 96d06abdd2
9 changed files with 107 additions and 12 deletions

View File

@ -21,7 +21,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#define TAG_MENU_REMOVE 206
#define TAG_MENU_CANCEL 204
#define TAG_MENU_OPEN 103
#define TAG_MENU_OPEN 104
#define TAG_SUBMENU_HISTORY 500
@interface AppDelegate: NSResponder<NSApplicationDelegate> {

View File

@ -19,7 +19,8 @@
-(BOOL)isUnsortedFolder;
-(void)addChild: (id)child;
-(void)removeChild: (id)child;
-(void)updateChild: (id)child;
-(void)setName: (NSString*)aName;
+(BookmarkFolder*)rootBookmarkFolder;
+(BookmarkFolder*)unsortedBookmarkFolder;

View File

@ -70,16 +70,29 @@ lazy-loaded when requested.
return [name isEqual: UNSORTED_NAME];
}
-(void)updateChild: (id)child {
if ([child isKindOfClass: [Website class]]) {
BOOL ok = NO;
NSString * destPath = [path stringByAppendingPathComponent: [self
pathNameForWebsite: child]];
ok = [[child asDictionary] writeToFile: destPath atomically: YES];
if (!ok) {
NSLog(@"Failed to resave the child");
}
}
}
-(void)addChild: (id)child {
[self initChildrenIfNeeded];
BOOL ok = NO;
if ([child isKindOfClass: [Website class]]) {
NSString * destPath = [path stringByAppendingPathComponent: [self
pathNameForWebsite: child]];
NSString *filename = [self pathNameForWebsite: child];
NSString *destPath = [path stringByAppendingPathComponent: filename];
ok = [[child asDictionary] writeToFile: destPath atomically: YES];
[child setFilename: filename];
} else if ([child isKindOfClass: [BookmarkFolder class]]) {
ok = [[NSFileManager defaultManager] createDirectoryAtPath: path
attributes: nil];
attributes: nil];
}
if (ok) {
[children addObject: child];
@ -111,6 +124,26 @@ lazy-loaded when requested.
}
}
-(void)setName: (NSString*)aName {
if ([aName length] < 1) {
return;
}
[name release];
name = [aName retain];
NSString *toPath = [[path stringByDeletingLastPathComponent]
stringByAppendingPathComponent: aName];
NSError *err = nil;
[[NSFileManager defaultManager] moveItemAtPath: [self path] toPath: toPath error:
&err];
if (err == nil) {
[path release];
path = [toPath retain];
} else {
NSLog(@"Error renaming directory");
}
}
+(BookmarkFolder*)rootBookmarkFolder {
static BookmarkFolder *cachedRootFolder;
if (cachedRootFolder != nil) {
@ -139,6 +172,7 @@ lazy-loaded when requested.
return child;
}
}
return nil;
}
-(NSString*)path {
@ -153,8 +187,14 @@ lazy-loaded when requested.
children = [someChildren retain];
}
-(NSString*)pathNameForWebsite: (Website*)aWebsite {
NSUInteger hash = [[aWebsite name] hash];
return [[NSNumber numberWithUnsignedInt: hash] description];
if ([aWebsite filename] != nil) {
return [aWebsite filename];
} else {
NSTimeInterval time = [[NSDate date] timeIntervalSinceReferenceDate];
NSNumber *num = [NSNumber numberWithDouble: time];
return [num stringValue];
}
}
-(void)initChildrenIfNeeded {
if (children != nil) {
@ -185,7 +225,8 @@ lazy-loaded when requested.
[newChildren addObject: [child autorelease]];
} else {
chDict = [NSDictionary dictionaryWithContentsOfFile: chPath];
child = [[Website alloc] initWithDictionary: chDict];
child = [[Website alloc] initWithDictionary: chDict fromFileNamed:
fileName];
[newChildren addObject: [child autorelease]];
}
}

View File

@ -8,4 +8,5 @@
}
-(void)search: (id)sender;
-(void)clearSearch: (id)sender;
-(void)newFolder: (id)sender;
@end

View File

@ -1,6 +1,7 @@
#import <AppKit/AppKit.h>
#import "BookmarksWindowController.h"
#import "BookmarkFolder.h"
#import "Website.h"
@implementation BookmarksWindowController
@ -31,6 +32,10 @@
[self onWindowAppeared];
}
-(void)newFolder: (id)sender {
NSLog(@"create new folder");
}
-(void)showWindow: (id)sender {
[self onWindowAppeared];
[super showWindow: sender];
@ -78,4 +83,22 @@
}
}
-(BOOL)outlineView: (NSOutlineView*)outlineView shouldEditTableColumn: (NSTableColumn*)tableColumn item: (id)item {
return YES;
}
-(void)outlineView: (NSOutlineView*)outlineView willDisplayCell: (id)cell forTableColumn: (NSTableColumn*)tableColumn item: (id)item {
[cell setEditable: YES];
}
-(void)outlineView: (NSOutlineView*)outlineView setObjectValue: (id)object forTableColumn: (NSTableColumn*)tableColumn byItem: (id)item {
if ([item isKindOfClass: [Website class]]) {
[(Website*)item setName: object];
BookmarkFolder *folder = [outlineView parentForItem: item];
[folder updateChild: item];
} else if ([item isKindOfClass: [BookmarkFolder class]]) {
[(BookmarkFolder*)item setName: object];
}
}
@end

View File

@ -14,16 +14,20 @@ struct website_data {
@class BookmarkFolder;
@interface Website: NSObject {
NSString *filename;
long fileOffset;
struct website_data *data;
}
-(id)initWithName: (NSString*)aName url: (NSString*)aUrl;
-(id)initWithData: (struct website_data*)someData atFileOffset: (long)aFileOffset;
-(id)initWithDictionary: (NSDictionary*)aDictionary;
-(id)initWithDictionary: (NSDictionary*)aDictionary fromFileNamed: (NSString*)aFilename;
-(NSString*)name;
-(NSString*)url;
-(void)setName: (NSString*)aName;
-(long)fileOffset;
-(NSString*)filename;
-(void)setFilename: (NSString*)aFilename;
-(NSDictionary*)asDictionary;

View File

@ -30,14 +30,18 @@
return self;
}
-(id)initWithDictionary: (NSDictionary*)aDictionary {
-(id)initWithDictionary: (NSDictionary*)aDictionary fromFileNamed: (NSString*)aFilename {
NSString *aName = [aDictionary objectForKey: @"name"];
NSString *aUrl = [aDictionary objectForKey: @"url"];
return [self initWithName: aName url: aUrl];
if ([self initWithName: aName url: aUrl] != nil) {
filename = [aFilename retain];
}
return self;
}
-(void)dealloc {
free(data);
[filename release];
[super dealloc];
}
@ -50,6 +54,26 @@
data->len_url];
}
-(void)setName: (NSString*)aName {
NSString *url = [self url];
int nlen = [aName length];
int urlen = data->len_url;
data = realloc(data, sizeof (struct website_data) + nlen + urlen);
data->len_name = nlen;
data->len_url = urlen;
memcpy(data->data, [aName cString], nlen);
memcpy(data->data + nlen, [url cString], urlen);
fileOffset = -1;
}
-(NSString*)filename {
return filename;
}
-(void)setFilename: (NSString*)aFilename {
[filename release];
filename = [aFilename retain];
}
-(NSDictionary*)asDictionary {
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[dict setObject: [self name] forKey: @"name"];

View File

@ -14,10 +14,11 @@
};
FirstResponder = {
Actions = (
"bookmarkPage:",
"didTapNewWindow:",
"findNext:",
"findPrevious:",
"bookmarkPage:",
"newFolder:",
"remove:",
"removeAll:",
"showAll:",