diff options
Diffstat (limited to 'bindings/objc/src')
44 files changed, 835 insertions, 1102 deletions
diff --git a/bindings/objc/src/DefaultFilterProvider.h b/bindings/objc/src/DefaultFilterProvider.h index 435daa0..7484212 100644 --- a/bindings/objc/src/DefaultFilterProvider.h +++ b/bindings/objc/src/DefaultFilterProvider.h @@ -18,7 +18,6 @@ - (SwordFilter *)newThmlPlainFilter; - (SwordFilter *)newTeiRenderFilter; - (SwordFilter *)newTeiPlainFilter; -- (SwordFilter *)newHtmlPlainFilter; @end @@ -32,6 +31,5 @@ - (SwordFilter *)newThmlPlainFilter; - (SwordFilter *)newTeiRenderFilter; - (SwordFilter *)newTeiPlainFilter; -- (SwordFilter *)newHtmlPlainFilter; @end diff --git a/bindings/objc/src/DefaultFilterProvider.mm b/bindings/objc/src/DefaultFilterProvider.mm index 65d8c0a..aaae02c 100644 --- a/bindings/objc/src/DefaultFilterProvider.mm +++ b/bindings/objc/src/DefaultFilterProvider.mm @@ -5,11 +5,11 @@ #import "DefaultFilterProvider.h" -#import "SwordFilter.h" @implementation DefaultFilterProvider - (SwordFilter *)newOsisRenderFilter { +// return [SwordOsisXHtmlFilter filter]; return [SwordOsisHtmlRefFilter filter]; } diff --git a/bindings/objc/src/FilterProviderFactory.mm b/bindings/objc/src/FilterProviderFactory.mm index 75abc68..6da290c 100644 --- a/bindings/objc/src/FilterProviderFactory.mm +++ b/bindings/objc/src/FilterProviderFactory.mm @@ -9,7 +9,7 @@ @interface FilterProviderFactory () -@property(nonatomic, retain) id <FilterProvider> filterProvider; +@property(nonatomic, strong) id <FilterProvider> filterProvider; @end @@ -33,10 +33,6 @@ return self.filterProvider; } -- (void)dealloc { - self.filterProvider = nil; - [super dealloc]; -} @end
\ No newline at end of file diff --git a/bindings/objc/src/Notifications.h b/bindings/objc/src/Notifications.h index 3972646..556c1fd 100644 --- a/bindings/objc/src/Notifications.h +++ b/bindings/objc/src/Notifications.h @@ -8,9 +8,3 @@ */ // Notification identifiers - -/** -\brief this notification is send, when the modules have changed (updated, added, removed) - */ -#define NotificationModulesChanged @"NotificationModulesChanged" -#define SendNotifyModulesChanged(X) [[NSNotificationCenter defaultCenter] postNotificationName:NotificationModulesChanged object:X]; diff --git a/bindings/objc/src/SwordBible.h b/bindings/objc/src/SwordBible.h index f57d322..ce8a926 100644 --- a/bindings/objc/src/SwordBible.h +++ b/bindings/objc/src/SwordBible.h @@ -26,10 +26,10 @@ typedef enum { }Testament; @interface SwordBible : SwordModule { - NSMutableDictionary *books; + NSDictionary *_books; } -@property (retain, readwrite) NSMutableDictionary *books; +@property (strong, readwrite) NSDictionary *books; // ----------- class methods ------------- + (void)decodeRef:(NSString *)ref intoBook:(NSString **)bookName book:(int *)book chapter:(int *)chapter verse:(int *)verse; @@ -47,11 +47,12 @@ typedef enum { - (NSArray *)bookList; - (NSString *)bookIntroductionFor:(SwordBibleBook *)aBook; -- (NSString *)chapterIntroductionFor:(SwordBibleBook *)aBook chapter:(int)chapter; +- (NSString *)chapterIntroductionIn:(SwordBibleBook *)aBook forChapter:(int)chapter; - (NSString *)moduleIntroduction; // some numbers -- (SwordBibleBook *)bookForLocalizedName:(NSString *)bookName; +- (SwordBibleBook *)bookWithNamePrefix:(NSString *)aPrefix; +- (SwordBibleBook *)bookForName:(NSString *)bookName; - (int)chaptersForBookName:(NSString *)bookName; - (int)versesForChapter:(int)chapter bookName:(NSString *)bookName; - (int)versesForBible; diff --git a/bindings/objc/src/SwordBible.mm b/bindings/objc/src/SwordBible.mm index 03e378e..7b78af5 100644 --- a/bindings/objc/src/SwordBible.mm +++ b/bindings/objc/src/SwordBible.mm @@ -19,7 +19,7 @@ using sword::AttributeValue; @interface SwordBible () -- (void)buildBookList; +- (NSDictionary *)buildBookList; - (BOOL)containsBookNumber:(int)aBookNum; - (NSArray *)textEntriesForReference:(NSString *)aReference context:(int)context textType:(TextPullType)textType; @@ -78,14 +78,14 @@ NSLock *bibleLock = nil; //if abbr contains : or . then we are a verse so return a chapter if([abbr rangeOfString:@":"].location != NSNotFound || [abbr rangeOfString:@"."].location != NSNotFound) { - return [firstBits objectAtIndex:0]; + return firstBits[0]; } //otherwise return a book firstBits = [first componentsSeparatedByString:@" "]; if([firstBits count] > 0) { - return [firstBits objectAtIndex:0]; + return firstBits[0]; } return abbr; @@ -96,22 +96,13 @@ NSLock *bibleLock = nil; that is: book number + testament * 100 */ + (int)bookIndexForSWKey:(sword::VerseKey *)key { - return key->Book() + key->Testament() * 100; + return key->getBookMax() + key->getTestamentMax() * 100; } #pragma mark - Initializers -- (id)initWithName:(NSString *)aName swordManager:(SwordManager *)aManager { - self = [super initWithName:aName swordManager:aManager]; - if(self) { - [self setBooks:nil]; - } - - return self; -} - - (id)initWithSWModule:(sword::SWModule *)aModule swordManager:(SwordManager *)aManager { - self = [super initWithSWModule:aModule swordManager:aManager]; + self = [super initWithSWModule:aModule]; if(self) { [self setBooks:nil]; } @@ -119,20 +110,11 @@ NSLock *bibleLock = nil; return self; } -- (void)finalize { - [super finalize]; -} -- (void)dealloc { - [books release]; - [super dealloc]; -} #pragma mark - Bible information -- (void)buildBookList { - [moduleLock lock]; - +- (NSDictionary *)buildBookList { sword::VersificationMgr *vmgr = sword::VersificationMgr::getSystemVersificationMgr(); const sword::VersificationMgr::System *system = vmgr->getVersificationSystem([[self versification] UTF8String]); @@ -141,15 +123,13 @@ NSLock *bibleLock = nil; for(int i = 0;i < bookCount;i++) { sword::VersificationMgr::Book *book = (sword::VersificationMgr::Book *)system->getBook(i); - SwordBibleBook *bb = [[[SwordBibleBook alloc] initWithBook:book] autorelease]; + SwordBibleBook *bb = [[SwordBibleBook alloc] initWithBook:book]; [bb setNumber:i+1]; NSString *bookName = [bb name]; - [buf setObject:bb forKey:bookName]; + buf[bookName] = bb; } - self.books = buf; - - [moduleLock unlock]; + return [NSDictionary dictionaryWithDictionary:buf]; } - (BOOL)containsBookNumber:(int)aBookNum { @@ -161,17 +141,15 @@ NSLock *bibleLock = nil; return NO; } -- (NSMutableDictionary *)books { - if(books == nil) { - [self buildBookList]; +- (NSDictionary *)books { + if(_books == nil) { + _books = [self buildBookList]; } - return books; + return _books; } -- (void)setBooks:(NSMutableDictionary *)aBooks { - [aBooks retain]; - [books release]; - books = aBooks; +- (void)setBooks:(NSDictionary *)aBooks { + _books = aBooks; } - (NSArray *)bookList { @@ -181,15 +159,15 @@ NSLock *bibleLock = nil; } - (BOOL)hasReference:(NSString *)ref { - [moduleLock lock]; + [self.moduleLock lock]; - sword::VerseKey *key = (sword::VerseKey *)(swModule->CreateKey()); + sword::VerseKey *key = (sword::VerseKey *)(swModule->createKey()); (*key) = [ref UTF8String]; NSString *bookName = [NSString stringWithUTF8String:key->getBookName()]; - int chapter = key->Chapter(); - int verse = key->Verse(); + int chapter = key->getChapterMax(); + int verse = key->getVerseMax(); - SwordBibleBook *bb = [[self books] objectForKey:bookName]; + SwordBibleBook *bb = [self books][bookName]; if(bb) { if(chapter > 0 && chapter < [bb numberOfChapters]) { if(verse > 0 && verse < [bb numberOfVersesForChapter:chapter]) { @@ -198,7 +176,7 @@ NSLock *bibleLock = nil; } } - [moduleLock unlock]; + [self.moduleLock unlock]; return NO; } @@ -208,41 +186,29 @@ NSLock *bibleLock = nil; if(aReference && [aReference length] > 0) { sword::VerseKey vk; - sword::ListKey listKey = vk.ParseVerseList([aReference UTF8String], "Gen1", true); + sword::ListKey listKey = vk.parseVerseList([aReference UTF8String], "Gen1", true); // unfortunately there is no other way then loop though all verses to know how many - for(listKey = sword::TOP; !listKey.Error(); listKey++) ret++; + for(listKey = sword::TOP; !listKey.popError(); listKey++) ret++; } return ret; } - (int)chaptersForBookName:(NSString *)bookName { - [moduleLock lock]; - - int maxChapters; - sword::VerseKey *key = (sword::VerseKey *)swModule->CreateKey(); - (*key) = [bookName UTF8String]; - maxChapters = key->getChapterMax(); - delete key; - - [moduleLock unlock]; - - return maxChapters; + SwordBibleBook *book = [self bookForName:bookName]; + if(book != nil) { + return [book numberOfChapters]; + } + return -1; } - - (int)versesForChapter:(int)chapter bookName:(NSString *)bookName { int ret = -1; - [moduleLock lock]; - - SwordBibleBook *bb = [[self books] objectForKey:bookName]; + SwordBibleBook *bb = [self books][bookName]; if(bb) { ret = [bb numberOfVersesForChapter:chapter]; } - - [moduleLock unlock]; - return ret; } @@ -261,9 +227,18 @@ NSLock *bibleLock = nil; return ret; } -- (SwordBibleBook *)bookForLocalizedName:(NSString *)bookName { +- (SwordBibleBook *)bookForName:(NSString *)bookName { for(SwordBibleBook *book in [[self books] allValues]) { - if([[book localizedName] isEqualToString:bookName]) { + if([[book localizedName] isEqualToString:bookName] || [[book name] isEqualToString:bookName]) { + return book; + } + } + return nil; +} + +- (SwordBibleBook *)bookWithNamePrefix:(NSString *)aPrefix { + for(SwordBibleBook *book in [[self books] allValues]) { + if([[book localizedName] hasPrefix:aPrefix] || [[book name] hasPrefix:aPrefix]) { return book; } } @@ -273,62 +248,69 @@ NSLock *bibleLock = nil; - (NSString *)moduleIntroduction { NSString *ret; + [self.moduleLock lock]; + // save key - SwordVerseKey *save = [(SwordVerseKey *)[self getKeyCopy] autorelease]; + SwordVerseKey *save = (SwordVerseKey *)[self getKeyCopy]; SwordVerseKey *key = [SwordVerseKey verseKeyWithVersification:[self versification]]; [key setHeadings:YES]; - [key setPosition:0]; + [key setTestament:0]; [self setSwordKey:key]; ret = [self renderedText]; // restore old key [self setSwordKey:save]; + [self.moduleLock unlock]; + return ret; } - (NSString *)bookIntroductionFor:(SwordBibleBook *)aBook { NSString *ret; + [self.moduleLock lock]; + // save key - SwordVerseKey *save = [(SwordVerseKey *)[self getKeyCopy] autorelease]; + SwordVerseKey *save = (SwordVerseKey *)[self getKeyCopy]; SwordVerseKey *key = [SwordVerseKey verseKeyWithVersification:[self versification]]; [key setHeadings:YES]; - [key setAutoNormalize:NO]; - [key setTestament:[aBook testament]]; - [key setBook:[aBook numberInTestament]]; - [key setChapter:0]; - [key setVerse:0]; + [key setTestament:(char) [aBook testament]]; + [key setBook:(char) [aBook numberInTestament]]; [self setSwordKey:key]; ret = [self renderedText]; // restore old key [self setSwordKey:save]; + [self.moduleLock unlock]; + return ret; } -- (NSString *)chapterIntroductionFor:(SwordBibleBook *)aBook chapter:(int)chapter { +- (NSString *)chapterIntroductionIn:(SwordBibleBook *)aBook forChapter:(int)chapter { NSString *ret; - + + [self.moduleLock lock]; + // save key - SwordVerseKey *save = [(SwordVerseKey *)[self getKeyCopy] autorelease]; + SwordVerseKey *save = (SwordVerseKey *)[self getKeyCopy]; SwordVerseKey *key = [SwordVerseKey verseKeyWithVersification:[self versification]]; [key setHeadings:YES]; - [key setAutoNormalize:NO]; - [key setTestament:[aBook testament]]; - [key setBook:[aBook numberInTestament]]; + [key setTestament:(char) [aBook testament]]; + [key setBook:(char) [aBook numberInTestament]]; [key setChapter:chapter]; - [key setVerse:0]; [self setSwordKey:key]; ret = [self renderedText]; // restore old key [self setSwordKey:save]; + [self.moduleLock unlock]; + return ret; } @@ -336,7 +318,7 @@ NSLock *bibleLock = nil; SwordBibleTextEntry *ret = nil; if(aKey) { - [moduleLock lock]; + [self.moduleLock lock]; [self setSwordKey:aKey]; if(![self error]) { NSString *txt; @@ -352,32 +334,32 @@ NSLock *bibleLock = nil; ALog(@"nil key"); } - if([swManager globalOption:SW_OPTION_HEADINGS] && [self hasFeature:SWMOD_FEATURE_HEADINGS]) { + if([self.swManager globalOption:SW_OPTION_HEADINGS] && [self hasFeature:SWMOD_FEATURE_HEADINGS]) { NSString *preverseHeading = [self entryAttributeValuePreverse]; if(preverseHeading && [preverseHeading length] > 0) { [ret setPreVerseHeading:preverseHeading]; } } } - [moduleLock unlock]; + [self.moduleLock unlock]; } return ret; } - (NSString *)versification { - NSString *versification = [configEntries objectForKey:SWMOD_CONFENTRY_VERSIFICATION]; + NSString *versification = self.configEntries[SWMOD_CONFENTRY_VERSIFICATION]; if(versification == nil) { versification = [self configFileEntryForConfigKey:SWMOD_CONFENTRY_VERSIFICATION]; if(versification != nil) { - [configEntries setObject:versification forKey:SWMOD_CONFENTRY_VERSIFICATION]; + self.configEntries[SWMOD_CONFENTRY_VERSIFICATION] = versification; } } // if still nil, use KJV versification if(versification == nil) { versification = @"KJV"; - [configEntries setObject:versification forKey:SWMOD_CONFENTRY_VERSIFICATION]; + self.configEntries[SWMOD_CONFENTRY_VERSIFICATION] = versification; } return versification; @@ -386,7 +368,7 @@ NSLock *bibleLock = nil; #pragma mark - SwordModuleAccess - (SwordKey *)createKey { - sword::VerseKey *vk = (sword::VerseKey *)swModule->CreateKey(); + sword::VerseKey *vk = (sword::VerseKey *)swModule->createKey(); SwordVerseKey *newKey = [SwordVerseKey verseKeyWithSWVerseKey:vk makeCopy:YES]; delete vk; @@ -403,9 +385,9 @@ NSLock *bibleLock = nil; - (long)entryCount { swModule->setPosition(sword::TOP); - long verseLowIndex = swModule->Index(); + long verseLowIndex = swModule->getIndex(); swModule->setPosition(sword::BOTTOM); - long verseHighIndex = swModule->Index(); + long verseHighIndex = swModule->getIndex(); return verseHighIndex - verseLowIndex; } @@ -435,7 +417,6 @@ NSLock *bibleLock = nil; SwordListKey *lk = [SwordListKey listKeyWithRef:aReference v11n:[self versification]]; [lk setPosition:SWPOS_TOP]; - [lk setPersist:NO]; SwordVerseKey *vk = [SwordVerseKey verseKeyWithRef:[lk keyText] v11n:[self versification]]; while(![lk error]) { // set current key to vk @@ -466,16 +447,16 @@ NSLock *bibleLock = nil; - (void)writeEntry:(SwordModuleTextEntry *)anEntry { const char *data = [[anEntry text] UTF8String]; - int dLen = strlen(data); + size_t dLen = strlen(data); - [moduleLock lock]; + [self.moduleLock lock]; [self setKeyString:[anEntry key]]; if(![self error]) { swModule->setEntry(data, dLen); // save text to module at current position } else { ALog(@"error at positioning module!"); } - [moduleLock unlock]; + [self.moduleLock unlock]; } @end diff --git a/bindings/objc/src/SwordBibleBook.h b/bindings/objc/src/SwordBibleBook.h index af48bb4..9e77df9 100644 --- a/bindings/objc/src/SwordBibleBook.h +++ b/bindings/objc/src/SwordBibleBook.h @@ -8,11 +8,12 @@ #import <Foundation/Foundation.h> +@class SwordBibleChapter; + #ifdef __cplusplus #include <versificationmgr.h> #include <versekey.h> #include <localemgr.h> -class sword::VersificationMgr::Book; #endif @interface SwordBibleBook : NSObject { @@ -22,16 +23,16 @@ class sword::VersificationMgr::Book; NSString *localizedName; int number; - char numberInTestament; - char testament; + int numberInTestament; + int testament; NSArray *chapters; } @property (readwrite) int number; -@property (readwrite) char numberInTestament; -@property (readwrite) char testament; -@property (retain, readwrite) NSString *localizedName; -@property (retain, readwrite) NSArray *chapters; +@property (readwrite) int numberInTestament; +@property (readwrite) int testament; +@property (strong, readwrite) NSString *localizedName; +@property (strong, readwrite) NSArray *chapters; #ifdef __cplusplus - (id)initWithBook:(sword::VersificationMgr::Book *)aBook; diff --git a/bindings/objc/src/SwordBibleBook.mm b/bindings/objc/src/SwordBibleBook.mm index eeb956b..931963c 100644 --- a/bindings/objc/src/SwordBibleBook.mm +++ b/bindings/objc/src/SwordBibleBook.mm @@ -37,26 +37,30 @@ swBook = aBook; sword::VerseKey vk = sword::VerseKey(aBook->getOSISName()); - [self setTestament:vk.Testament()]; - [self setNumberInTestament:vk.Book()]; + [self setTestament:vk.getTestament()]; + [self setNumberInTestament:vk.getBook()]; // get system localeMgr to be able to translate the english bookName sword::LocaleMgr *lmgr = sword::LocaleMgr::getSystemLocaleMgr(); - self.localizedName = [NSString stringWithUTF8String:lmgr->translate(swBook->getLongName())]; + const char *translated = lmgr->translate(swBook->getLongName()); + self.localizedName = [NSString stringWithUTF8String:translated]; + + // in case we don't have ICU support this still works. + if(self.localizedName == nil) { + self.localizedName = [NSString stringWithCString:translated encoding:NSISOLatin1StringEncoding]; + } + if(self.localizedName == nil) { + DLog(@"Unable to get this bookname: %s", translated); + } } return self; } -- (void)finalize { - [super finalize]; -} - (void)dealloc { - [self setLocalizedName:nil]; [self setChapters:nil]; - - [super dealloc]; + [self setLocalizedName:nil]; } - (NSString *)name { @@ -76,8 +80,6 @@ } - (void)setChapters:(NSArray *)anArray { - [anArray retain]; - [chapters release]; chapters = anArray; } @@ -85,7 +87,7 @@ if(chapters == nil) { NSMutableArray *temp = [NSMutableArray array]; for(int i = 0;i < swBook->getChapterMax();i++) { - [temp addObject:[[[SwordBibleChapter alloc] initWithBook:self andChapter:i+1] autorelease]]; + [temp addObject:[[SwordBibleChapter alloc] initWithBook:self andChapter:i+1]]; } [self setChapters:[NSArray arrayWithArray:temp]]; } @@ -106,7 +108,7 @@ /** we implement this for sorting */ - (NSComparisonResult)compare:(SwordBibleBook *)b { - return [[NSNumber numberWithInt:number] compare:[NSNumber numberWithInt:[b number]]]; + return [@(number) compare:@([b number])]; } @end diff --git a/bindings/objc/src/SwordBibleChapter.h b/bindings/objc/src/SwordBibleChapter.h index 11e4cff..a8a27a9 100644 --- a/bindings/objc/src/SwordBibleChapter.h +++ b/bindings/objc/src/SwordBibleChapter.h @@ -12,12 +12,12 @@ @interface SwordBibleChapter : NSObject { /** the back reference */ - SwordBibleBook *book; + SwordBibleBook *__unsafe_unretained book; int number; } @property (readwrite) int number; -@property (readwrite, assign) SwordBibleBook *book; +@property (readwrite, unsafe_unretained) SwordBibleBook *book; - (id)initWithBook:(SwordBibleBook *)aBook andChapter:(int)aNumber; diff --git a/bindings/objc/src/SwordBibleChapter.m b/bindings/objc/src/SwordBibleChapter.m index 8855227..e4086a1 100644 --- a/bindings/objc/src/SwordBibleChapter.m +++ b/bindings/objc/src/SwordBibleChapter.m @@ -24,12 +24,6 @@ return self; } -- (void)finalize { - [super finalize]; -} -- (void)dealloc { - [super dealloc]; -} @end diff --git a/bindings/objc/src/SwordBibleTextEntry.h b/bindings/objc/src/SwordBibleTextEntry.h index 42969cd..1d326a0 100644 --- a/bindings/objc/src/SwordBibleTextEntry.h +++ b/bindings/objc/src/SwordBibleTextEntry.h @@ -13,7 +13,7 @@ NSString *preVerseHeading; } -@property (readwrite, retain) NSString *preVerseHeading; +@property (readwrite, strong) NSString *preVerseHeading; + (id)textEntryForKey:(NSString *)aKey andText:(NSString *)aText; - (id)initWithKey:(NSString *)aKey andText:(NSString *)aText; diff --git a/bindings/objc/src/SwordBibleTextEntry.m b/bindings/objc/src/SwordBibleTextEntry.m index 115a33c..b5a4a51 100644 --- a/bindings/objc/src/SwordBibleTextEntry.m +++ b/bindings/objc/src/SwordBibleTextEntry.m @@ -13,7 +13,7 @@ @synthesize preVerseHeading; + (id)textEntryForKey:(NSString *)aKey andText:(NSString *)aText { - return [[[SwordBibleTextEntry alloc] initWithKey:aKey andText:aText] autorelease]; + return [[SwordBibleTextEntry alloc] initWithKey:aKey andText:aText]; } - (id)initWithKey:(NSString *)aKey andText:(NSString *)aText { @@ -21,18 +21,11 @@ if(self) { self.key = aKey; self.text = aText; + self.preVerseHeading = @""; } return self; } -- (void)finalize { - [super finalize]; -} -- (void)dealloc { - [self setPreVerseHeading:nil]; - - [super dealloc]; -} @end diff --git a/bindings/objc/src/SwordBook.h b/bindings/objc/src/SwordBook.h index b0c520a..50740f3 100644 --- a/bindings/objc/src/SwordBook.h +++ b/bindings/objc/src/SwordBook.h @@ -26,7 +26,7 @@ NSMutableDictionary *contents; } -@property(readwrite, retain) NSMutableDictionary *contents; +@property(readwrite, strong) NSMutableDictionary *contents; /** return the tree content for the given treeKey diff --git a/bindings/objc/src/SwordBook.mm b/bindings/objc/src/SwordBook.mm index bcee1e4..c11b800 100644 --- a/bindings/objc/src/SwordBook.mm +++ b/bindings/objc/src/SwordBook.mm @@ -24,17 +24,8 @@ @synthesize contents; -- (id)initWithName:(NSString *)aName swordManager:(SwordManager *)aManager { - self = [super initWithName:aName swordManager:aManager]; - if(self) { - [self setContents:[NSMutableDictionary dictionary]]; - } - - return self; -} - - (id)initWithSWModule:(sword::SWModule *)aModule swordManager:(SwordManager *)aManager { - self = [super initWithSWModule:aModule swordManager:aManager]; + self = [super initWithSWModule:aModule]; if(self) { [self setContents:[NSMutableDictionary dictionary]]; } @@ -42,30 +33,20 @@ return self; } -- (void)finalize { - [super finalize]; -} - -- (void)dealloc { - [self setContents:nil]; - - [super dealloc]; -} - - (SwordModuleTreeEntry *)treeEntryForKey:(NSString *)treeKey { SwordModuleTreeEntry * ret; - [moduleLock lock]; + [self.moduleLock lock]; if(treeKey == nil) { - ret = [contents objectForKey:@"root"]; + ret = contents[@"root"]; if(ret == nil) { sword::TreeKeyIdx *tk = dynamic_cast<sword::TreeKeyIdx*>((sword::SWKey *)*(swModule)); ret = [self _treeEntryForKey:tk]; // add to content - [contents setObject:ret forKey:@"root"]; + contents[@"root"] = ret; } } else { - ret = [contents objectForKey:treeKey]; + ret = contents[treeKey]; if(ret == nil) { const char *keyStr = [treeKey UTF8String]; if(![self isUnicode]) { @@ -77,16 +58,16 @@ sword::TreeKeyIdx *key = dynamic_cast<sword::TreeKeyIdx*>((sword::SWKey *)*(swModule)); ret = [self _treeEntryForKey:key]; // add to content - [contents setObject:ret forKey:treeKey]; + contents[treeKey] = ret; } } - [moduleLock unlock]; + [self.moduleLock unlock]; return ret; } - (SwordModuleTreeEntry *)_treeEntryForKey:(sword::TreeKeyIdx *)treeKey { - SwordModuleTreeEntry *ret = [[[SwordModuleTreeEntry alloc] init] autorelease]; + SwordModuleTreeEntry *ret = [[SwordModuleTreeEntry alloc] init]; char *treeNodeName = (char *)treeKey->getText(); NSString *nName; diff --git a/bindings/objc/src/SwordCommentary.h b/bindings/objc/src/SwordCommentary.h index feaf619..95fd93d 100644 --- a/bindings/objc/src/SwordCommentary.h +++ b/bindings/objc/src/SwordCommentary.h @@ -16,8 +16,6 @@ #ifdef __cplusplus #include <rawfiles.h> -class sword::RawFiles; -class sword::SWConfig; #endif diff --git a/bindings/objc/src/SwordCommentary.mm b/bindings/objc/src/SwordCommentary.mm index 3b81738..93a01d0 100644 --- a/bindings/objc/src/SwordCommentary.mm +++ b/bindings/objc/src/SwordCommentary.mm @@ -69,12 +69,6 @@ return [super init]; } -- (void)finalize { - [super finalize]; -} -- (void)dealloc { - [super dealloc]; -} @end diff --git a/bindings/objc/src/SwordDictionary.mm b/bindings/objc/src/SwordDictionary.mm index 4924d00..13d5967 100644 --- a/bindings/objc/src/SwordDictionary.mm +++ b/bindings/objc/src/SwordDictionary.mm @@ -15,7 +15,7 @@ @interface SwordDictionary (/* Private, class continuation */) /** private property */ -@property(readwrite, retain) NSMutableArray *keys; +@property(readwrite, strong) NSMutableArray *keys; @end @interface SwordDictionary (PrivateAPI) @@ -40,17 +40,17 @@ if([keys count] == 0) { NSMutableArray *arr = [NSMutableArray array]; - [moduleLock lock]; + [self.moduleLock lock]; swModule->setSkipConsecutiveLinks(true); *swModule = sword::TOP; swModule->getRawEntry(); while(![self error]) { - char *cStrKeyText = (char *)swModule->KeyText(); + char *cStrKeyText = (char *)swModule->getKeyText(); if(cStrKeyText) { NSString *keyText = [NSString stringWithUTF8String:cStrKeyText]; if(!keyText) { - keyText = [NSString stringWithCString:swModule->KeyText() encoding:NSISOLatin1StringEncoding]; + keyText = [NSString stringWithCString:swModule->getKeyText() encoding:NSISOLatin1StringEncoding]; if(!keyText) { ALog(@"Unable to create NSString instance from string: %s", cStrKeyText); } @@ -66,7 +66,7 @@ (*swModule)++; } - [moduleLock unlock]; + [self.moduleLock unlock]; self.keys = arr; [self writeToCache]; @@ -96,18 +96,9 @@ @synthesize keys; -- (id)initWithName:(NSString *)aName swordManager:(SwordManager *)aManager { - self = [super initWithName:aName swordManager:aManager]; - if(self) { - self.keys = nil; - } - - return self; -} - /** init with given SWModule */ - (id)initWithSWModule:(sword::SWModule *)aModule swordManager:(SwordManager *)aManager { - self = [super initWithSWModule:aModule swordManager:aManager]; + self = [super initWithSWModule:aModule]; if(self) { self.keys = nil; } @@ -115,15 +106,7 @@ return self; } -- (void)finalize { - [super finalize]; -} -- (void)dealloc { - [self setKeys:nil]; - - [super dealloc]; -} - (NSArray *)allKeys { NSArray *ret = self.keys; @@ -141,14 +124,14 @@ - (NSString *)entryForKey:(NSString *)aKey { NSString *ret = nil; - [moduleLock lock]; + [self.moduleLock lock]; [self setKeyString:aKey]; if([self error]) { ALog(@"Error on setting key!"); } else { ret = [self strippedText]; } - [moduleLock unlock]; + [self.moduleLock unlock]; return ret; } @@ -160,13 +143,13 @@ - (id)attributeValueForParsedLinkData:(NSDictionary *)data withTextRenderType:(TextPullType)textType { id ret = nil; - NSString *attrType = [data objectForKey:ATTRTYPE_TYPE]; + NSString *attrType = data[ATTRTYPE_TYPE]; if([attrType isEqualToString:@"scriptRef"] || [attrType isEqualToString:@"scripRef"] || [attrType isEqualToString:@"Greek"] || [attrType isEqualToString:@"Hebrew"] || [attrType hasPrefix:@"strongMorph"] || [attrType hasPrefix:@"robinson"]) { - NSString *key = [data objectForKey:ATTRTYPE_VALUE]; + NSString *key = data[ATTRTYPE_VALUE]; ret = [self strippedTextEntriesForRef:key]; } diff --git a/bindings/objc/src/SwordFilter.mm b/bindings/objc/src/SwordFilter.mm index 7d609bc..450e986 100644 --- a/bindings/objc/src/SwordFilter.mm +++ b/bindings/objc/src/SwordFilter.mm @@ -30,9 +30,6 @@ return self; } -- (void)dealloc { - [super dealloc]; -} - (sword::SWFilter *)swFilter { return swFilter; @@ -42,7 +39,7 @@ @implementation SwordOsisHtmlRefFilter + (SwordOsisHtmlRefFilter *)filter { - return [[[SwordOsisHtmlRefFilter alloc] init] autorelease]; + return [[SwordOsisHtmlRefFilter alloc] init]; } - (id)init { @@ -52,7 +49,7 @@ @implementation SwordOsisXHtmlFilter + (SwordOsisXHtmlFilter *)filter { - return [[[SwordOsisXHtmlFilter alloc] init] autorelease]; + return [[SwordOsisXHtmlFilter alloc] init]; } - (id)init { @@ -62,7 +59,7 @@ @implementation SwordOsisPlainFilter + (SwordOsisPlainFilter *)filter { - return [[[SwordOsisPlainFilter alloc] init] autorelease]; + return [[SwordOsisPlainFilter alloc] init]; } - (id)init { @@ -72,7 +69,7 @@ @implementation SwordThmlHtmlFilter + (SwordThmlHtmlFilter *)filter { - return [[[SwordThmlHtmlFilter alloc] init] autorelease]; + return [[SwordThmlHtmlFilter alloc] init]; } - (id)init { @@ -82,7 +79,7 @@ @implementation SwordThmlPlainFilter + (SwordThmlPlainFilter *)filter { - return [[[SwordThmlPlainFilter alloc] init] autorelease]; + return [[SwordThmlPlainFilter alloc] init]; } - (id)init { @@ -92,7 +89,7 @@ @implementation SwordGbfHtmlFilter + (SwordGbfHtmlFilter *)filter { - return [[[SwordGbfHtmlFilter alloc] init] autorelease]; + return [[SwordGbfHtmlFilter alloc] init]; } - (id)init { @@ -102,7 +99,7 @@ @implementation SwordGbfPlainFilter + (SwordGbfPlainFilter *)filter { - return [[[SwordGbfPlainFilter alloc] init] autorelease]; + return [[SwordGbfPlainFilter alloc] init]; } - (id)init { @@ -112,7 +109,7 @@ @implementation SwordTeiHtmlFilter + (SwordTeiHtmlFilter *)filter { - return [[[SwordTeiHtmlFilter alloc] init] autorelease]; + return [[SwordTeiHtmlFilter alloc] init]; } - (id)init { @@ -122,7 +119,7 @@ @implementation SwordTeiXHtmlFilter + (SwordTeiXHtmlFilter *)filter { - return [[[SwordTeiXHtmlFilter alloc] init] autorelease]; + return [[SwordTeiXHtmlFilter alloc] init]; } - (id)init { @@ -132,7 +129,7 @@ @implementation SwordTeiPlainFilter + (SwordTeiPlainFilter *)filter { - return [[[SwordTeiPlainFilter alloc] init] autorelease]; + return [[SwordTeiPlainFilter alloc] init]; } - (id)init { diff --git a/bindings/objc/src/SwordInstallSource.h b/bindings/objc/src/SwordInstallSource.h index 1b1bdf3..ca19e2f 100644 --- a/bindings/objc/src/SwordInstallSource.h +++ b/bindings/objc/src/SwordInstallSource.h @@ -12,7 +12,6 @@ #ifdef __cplusplus #include <swmgr.h> #include <installmgr.h> -class sword::SWModule; #endif @class SwordManager; @@ -25,11 +24,6 @@ class sword::SWModule; #ifdef __cplusplus sword::InstallSource *swInstallSource; #endif - - BOOL temporarySource; - - /** the sword manager for this source */ - SwordManager *swordManager; } // init @@ -60,7 +54,7 @@ class sword::SWModule; withInstallController:(SwordInstallSourceManager *)sim; /** List of available InstallSources */ -- (NSArray *)listModules; +- (NSDictionary *)allModules; /** List of modules for given type */ - (NSArray *)listModulesForType:(ModuleType)aType; @@ -68,9 +62,8 @@ class sword::SWModule; /** list module types */ - (NSArray *)listModuleTypes; -/** Returns the SwordManager attached to this SwordInstallSourceController */ +/** Returns the SwordManager attached to this SwordInstallSource */ - (SwordManager *)swordManager; -- (void)setSwordManager:(SwordManager *)swManager; #ifdef __cplusplus - (sword::InstallSource *)installSource; diff --git a/bindings/objc/src/SwordInstallSource.mm b/bindings/objc/src/SwordInstallSource.mm index b1f71d3..6870b74 100644 --- a/bindings/objc/src/SwordInstallSource.mm +++ b/bindings/objc/src/SwordInstallSource.mm @@ -12,6 +12,8 @@ @interface SwordInstallSource () +@property (nonatomic) BOOL deleteSwInstallSource; + @end @implementation SwordInstallSource @@ -20,16 +22,8 @@ - (id)init { self = [super init]; if(self) { - temporarySource = NO; - - // at first we have no sword manager - [self setSwordManager:nil]; - - // init InstallMgr swInstallSource = new sword::InstallSource("", ""); - if(swInstallSource == nil) { - ALog(@"Could not init sword install source!"); - } + self.deleteSwInstallSource = YES; } return self; @@ -38,8 +32,7 @@ - (id)initWithType:(NSString *)aType { self = [self init]; if(self) { - // set type - swInstallSource->type = [aType cStringUsingEncoding:NSUTF8StringEncoding]; + swInstallSource->type = [aType UTF8String]; } return self; @@ -49,72 +42,55 @@ - (id)initWithSource:(sword::InstallSource *)is { self = [super init]; if(self) { - temporarySource = YES; - - // at first we have no sword manager - [self setSwordManager:nil]; - swInstallSource = is; + self.deleteSwInstallSource = NO; } return self; } -- (void)finalize { - if(temporarySource == NO) { - //LogL(LOG_DEBUG, @"[SwordInstallSource -finalize] deleting swInstallSource"); - //delete swInstallSource; - } - - [super finalize]; -} - - (void)dealloc { - [swordManager release]; - [super dealloc]; -} - -- (void)setSwordManager:(SwordManager *)swManager { - [swManager retain]; - [swordManager release]; - swordManager = swManager; + ALog(@""); + if(swInstallSource != NULL && self.deleteSwInstallSource) { + ALog(@"Deleting InstallSource"); + delete swInstallSource; + } } -// accessors - (NSString *)caption { const char *str = swInstallSource->caption; - return [[[NSString alloc] initWithCString:str encoding:NSUTF8StringEncoding] autorelease]; + return [[NSString alloc] initWithCString:str encoding:NSUTF8StringEncoding]; } - (void)setCaption:(NSString *)aCaption { - swInstallSource->caption = [aCaption cStringUsingEncoding:NSUTF8StringEncoding]; + swInstallSource->caption = [aCaption UTF8String]; } - (NSString *)type { const char *str = swInstallSource->type; - return [[[NSString alloc] initWithCString:str encoding:NSUTF8StringEncoding] autorelease]; + return [[NSString alloc] initWithCString:str encoding:NSUTF8StringEncoding]; } - (void)setType:(NSString *)aType { - swInstallSource->type = [aType cStringUsingEncoding:NSUTF8StringEncoding]; + swInstallSource->type = [aType UTF8String]; } - (NSString *)source { const char *str = swInstallSource->source; - return [[[NSString alloc] initWithCString:str encoding:NSUTF8StringEncoding] autorelease]; + return [[NSString alloc] initWithCString:str encoding:NSUTF8StringEncoding]; } - (void)setSource:(NSString *)aSource { - swInstallSource->source = [aSource cStringUsingEncoding:NSUTF8StringEncoding]; + swInstallSource->source = [aSource UTF8String]; } - (NSString *)directory { const char *str = swInstallSource->directory; - return [[[NSString alloc] initWithCString:str encoding:NSUTF8StringEncoding] autorelease]; + return [[NSString alloc] initWithCString:str encoding:NSUTF8StringEncoding]; } - (void)setDirectory:(NSString *)aDir { - swInstallSource->directory = [aDir cStringUsingEncoding:NSUTF8StringEncoding]; + swInstallSource->directory = [aDir UTF8String]; } - (BOOL)isLocalSource { @@ -133,60 +109,49 @@ } /** list all modules of this source */ -- (NSArray *)listModules { - NSArray *ret = [NSArray array]; - +- (NSDictionary *)allModules { SwordManager *sm = [self swordManager]; if(sm) { - ret = [sm listModules]; + return [sm allModules]; } else { ALog(@"Have nil SwordManager"); } - - return ret; + return nil; } - (NSArray *)listModulesForType:(ModuleType)aType { - NSArray *ret = [NSArray array]; - SwordManager *sm = [self swordManager]; if(sm) { - ret = [sm modulesForType:aType]; + return [sm modulesForType:aType]; } else { ALog(@"Have nil SwordManager"); - } - - return ret; + } + return nil; } /** list module types */ - (NSArray *)listModuleTypes { - NSArray *ret = [SwordManager moduleTypes]; - return ret; + return [SwordManager moduleTypes]; } -// get associated SwordManager - (SwordManager *)swordManager { - - if(swordManager == nil) { + // create SwordManager from the SWMgr of this source + sword::SWMgr *mgr; + if([self isLocalSource]) { + // create SwordManager from new SWMgr of path + mgr = new sword::SWMgr([[self directory] UTF8String], true, NULL, false, false); + } else { // create SwordManager from the SWMgr of this source - sword::SWMgr *mgr; - if([[self source] isEqualToString:@"localhost"]) { - // create SwordManager from new SWMgr of path - mgr = new sword::SWMgr([[self directory] UTF8String], true, NULL, false, false); - } else { - // create SwordManager from the SWMgr of this source - mgr = swInstallSource->getMgr(); - } - - if(mgr == nil) { - ALog(@"Have a nil SWMgr!"); - } else { - swordManager = [[SwordManager alloc] initWithSWMgr:mgr]; - } + mgr = swInstallSource->getMgr(); + } + + if(mgr == nil) { + ALog(@"Have a nil SWMgr!"); + return nil; + + } else { + return [[SwordManager alloc] initWithSWMgr:mgr]; } - - return swordManager; } /** low level API */ diff --git a/bindings/objc/src/SwordInstallSourceManager.h b/bindings/objc/src/SwordInstallSourceManager.h index ee22c48..9f8922e 100755 --- a/bindings/objc/src/SwordInstallSourceManager.h +++ b/bindings/objc/src/SwordInstallSourceManager.h @@ -13,8 +13,6 @@ #include <installmgr.h> #include <swconfig.h> #include <multimapwdef.h> -class sword::SWModule; -class sword::InstallMgr; using sword::SWModule; using sword::InstallMgr; #endif @@ -37,44 +35,39 @@ typedef enum _ModuleStatusConst { #ifdef __cplusplus sword::InstallMgr *swInstallMgr; #endif - - BOOL createPath; - - NSString *configPath; } -// ------------------- getter / setter ------------------- -@property (retain, readwrite) NSString *configPath; -@property (retain, readwrite) NSString *configFilePath; -@property (retain, readwrite) NSMutableArray *installSourceList; +// ------------------- properties ------------------- /** Dictionary of InstallSources. Key: Caption */ -@property (retain, readwrite) NSMutableDictionary *installSources; +@property (strong, readonly) NSDictionary *installSources; +@property (strong, nonatomic) NSString *ftpUser; +@property (strong, nonatomic) NSString *ftpPassword; +@property (strong, readwrite) NSString *configPath; +@property (readwrite) BOOL createConfigPath; // -------------------- methods -------------------- // initialization -+ (SwordInstallSourceManager *)defaultController; -+ (SwordInstallSourceManager *)defaultControllerWithPath:(NSString *)aPath; -+ (SwordInstallSourceManager *)controllerWithPath:(NSString *)aPath; ++ (SwordInstallSourceManager *)defaultManager; /** -base path of the module installation + base path of the module installation */ -- (id)init; - (id)initWithPath:(NSString *)aPath createPath:(BOOL)create; -/** re-init after adding or removing new modules */ -- (void)reinitialize; +/** marks this manager as the default one / singleton */ +- (void)useAsDefaultManager; + +/** init after adding or removing new modules */ +- (void)initManager; // installation/unInstallation - (int)installModule:(SwordModule *)aModule fromSource:(SwordInstallSource *)is withManager:(SwordManager *)manager; - (int)uninstallModule:(SwordModule *)aModule fromManager:(SwordManager *)swManager; // add/remove install sources -- (void)addInstallSource:(SwordInstallSource *)is; -- (void)addInstallSource:(SwordInstallSource *)is withReinitialize:(BOOL)reinit; -- (void)removeInstallSource:(SwordInstallSource *)is; -- (void)removeInstallSource:(SwordInstallSource *)is withReinitialize:(BOOL)reinit; +- (void)addInstallSource:(SwordInstallSource *)is reload:(BOOL)doReload; +- (void)removeInstallSource:(SwordInstallSource *)is reload:(BOOL)doReload; - (void)updateInstallSource:(SwordInstallSource *)is; - (int)refreshMasterRemoteInstallSourceList; @@ -83,7 +76,7 @@ base path of the module installation - (void)setUserDisclaimerConfirmed:(BOOL)flag; // list modules in sources -- (NSArray *)listModulesForSource:(SwordInstallSource *)is; +- (NSDictionary *)listModulesForSource:(SwordInstallSource *)is; // remote source list - (int)refreshInstallSource:(SwordInstallSource *)is; diff --git a/bindings/objc/src/SwordInstallSourceManager.mm b/bindings/objc/src/SwordInstallSourceManager.mm index 98d6cac..ecdae3a 100755 --- a/bindings/objc/src/SwordInstallSourceManager.mm +++ b/bindings/objc/src/SwordInstallSourceManager.mm @@ -19,73 +19,20 @@ typedef sword::multimapwithdefault<sword::SWBuf, sword::SWBuf, std::less <sword: #define INSTALLSOURCE_SECTION_TYPE_FTP "FTPSource" #define INSTALLSOURCE_SECTION_TYPE_HTTP "HTTPSource" -@implementation SwordInstallSourceManager -@dynamic configPath; +@interface SwordInstallSourceManager () -// ------------------- getter / setter ------------------- -- (NSString *)configPath { - return configPath; -} +@property (strong, readwrite) NSDictionary *installSources; -- (void)setConfigPath:(NSString *)value { - DLog(@""); - - if(configPath != value) { - [configPath release]; - configPath = [value copy]; - - if(value == nil) { - return; - } - - - // check for existence - NSFileManager *fm = [NSFileManager defaultManager]; - BOOL isDir; - if(([fm fileExistsAtPath:configPath] == NO) && createPath == YES) { - // create path - [fm createDirectoryAtPath:configPath withIntermediateDirectories:NO attributes:nil error:NULL]; - } - - if(([fm fileExistsAtPath:configPath isDirectory:&isDir] == YES) && (isDir)) { - // set configFilePath - [self setConfigFilePath:[configPath stringByAppendingPathComponent:@"InstallMgr.conf"]]; - - // check config - if([fm fileExistsAtPath:self.configFilePath] == NO) { - // create config entry - sword::SWConfig config([self.configFilePath cStringUsingEncoding:NSUTF8StringEncoding]); - config["General"]["PassiveFTP"] = "true"; - config.Save(); - - // create default Install source - SwordInstallSource *is = [[[SwordInstallSource alloc] initWithType:INSTALLSOURCE_TYPE_FTP] autorelease]; - [is setCaption:@"CrossWire"]; - [is setSource:@"ftp.crosswire.org"]; - [is setDirectory:@"/pub/sword/raw"]; - - // addInstallSource will reinitialize - [self addInstallSource:is withReinitialize:NO]; - - } - // init installMgr - [self reinitialize]; - - // sync with master install source list - //[self refreshMasterRemoteInstallSourceList]; +@end - } else { - ALog(@"Config path does not exist: %@", configPath); - } - } -} +@implementation SwordInstallSourceManager // -------------------- methods -------------------- +static SwordInstallSourceManager *singleton = nil; // initialization -+ (SwordInstallSourceManager *)defaultController { - static SwordInstallSourceManager *singleton = nil; ++ (SwordInstallSourceManager *)defaultManager { if(singleton == nil) { singleton = [[SwordInstallSourceManager alloc] init]; } @@ -93,27 +40,17 @@ typedef sword::multimapwithdefault<sword::SWBuf, sword::SWBuf, std::less <sword: return singleton; } -+ (SwordInstallSourceManager *)defaultControllerWithPath:(NSString *)aPath { - SwordInstallSourceManager *m = [SwordInstallSourceManager defaultController]; - [m setConfigPath:aPath]; - return m; -} - -+ (SwordInstallSourceManager *)controllerWithPath:(NSString *)aPath { - return [[[SwordInstallSourceManager alloc] initWithPath:aPath createPath:YES] autorelease]; -} - /** base path of the module installation */ - (id)init { self = [super init]; if(self) { - createPath = NO; + [self setCreateConfigPath:NO]; [self setConfigPath:nil]; - [self setConfigFilePath:nil]; - [self setInstallSources:[NSMutableDictionary dictionary]]; - [self setInstallSourceList:[NSMutableArray array]]; + [self setInstallSources:[NSDictionary dictionary]]; + [self setFtpUser:@"ftp"]; + [self setFtpPassword:@"ObjCSword@crosswire.org"]; } return self; @@ -125,103 +62,168 @@ base path of the module installation - (id)initWithPath:(NSString *)aPath createPath:(BOOL)create { self = [self init]; if(self) { - createPath = create; + [self setCreateConfigPath:create]; [self setConfigPath:aPath]; } return self; } -/** re-init after adding or removing new modules */ -- (void)reinitialize { - sword::SWConfig config([self.configFilePath UTF8String]); - config.Load(); +- (void)useAsDefaultManager { + singleton = self; +} - // init installMgr - BOOL disclaimerConfirmed = NO; +- (void)dealloc { + DLog(@""); if(swInstallMgr != nil) { + DLog(@"deleting InstallMgr"); + delete swInstallMgr; + } +} + +/** init after adding or removing new modules */ +- (void)initManager { + [self setupConfig]; + + // safe disclaimer flag + BOOL disclaimerConfirmed = NO; + if(swInstallMgr != NULL) { disclaimerConfirmed = [self userDisclaimerConfirmed]; } - swInstallMgr = new sword::InstallMgr([configPath UTF8String]); - if(swInstallMgr == nil) { - ALog(@"Could not initialize InstallMgr!"); + + if(swInstallMgr == NULL) { + DLog(@"Initializing swInstallMgr"); + swInstallMgr = [self newDefaultInstallMgr]; + if(swInstallMgr == nil) { + ALog(@"Could not initialize InstallMgr!"); + + } else { + [self setUserDisclaimerConfirmed:disclaimerConfirmed]; + + if(![self existsDefaultInstallSource]) { + [self addDefaultInstallSource]; + [self readInstallMgrConf]; + } + + [self setupInstallSources]; + } + } else { - [self setUserDisclaimerConfirmed:disclaimerConfirmed]; - - // empty all lists - [self.installSources removeAllObjects]; - [self.installSourceList removeAllObjects]; - - // init install sources - for(InstallSourceMap::iterator it = swInstallMgr->sources.begin(); it != swInstallMgr->sources.end(); it++) { - sword::InstallSource *sis = it->second; - SwordInstallSource *is = [[[SwordInstallSource alloc] initWithSource:sis] autorelease]; - - [self.installSources setObject:is forKey:[is caption]]; - // also add to list - [self.installSourceList addObject:is]; + ALog(@"Re-initializing swInstallMgr"); + [self reloadManager]; + } +} + +- (void)reloadManager { + [self readInstallMgrConf]; + [self setupInstallSources]; +} + +- (sword::InstallMgr *)newDefaultInstallMgr { + ALog(@"Creating InstallMgr with: %@, %i, %@, %@", [self configPath], 0, [self ftpUser], [self ftpPassword]); + return new sword::InstallMgr( + [[self configPath] UTF8String], + 0, + sword::SWBuf([[self ftpUser] UTF8String]), + sword::SWBuf([[self ftpPassword] UTF8String])); +} + +- (BOOL)existsDefaultInstallSource { + sword::InstallMgr mgr = sword::InstallMgr([[self configPath] UTF8String]); + + for(InstallSourceMap::iterator it = mgr.sources.begin(); it != mgr.sources.end(); it++) { + sword::InstallSource *sis = it->second; + + if([[NSString stringWithCString:sis->caption.c_str() encoding:NSUTF8StringEncoding] isEqualToString:@"CrossWire"]) { + return YES; } } + return NO; } -- (void)finalize { - if(swInstallMgr != nil) { - delete swInstallMgr; +- (void)addDefaultInstallSource { + SwordInstallSource *is = [[SwordInstallSource alloc] initWithType:INSTALLSOURCE_TYPE_FTP]; + [is setCaption:@"CrossWire"]; + [is setSource:@"ftp.crosswire.org"]; + [is setDirectory:@"/pub/sword/raw"]; + + [self addInstallSource:is reload:NO]; +} + +- (void)setupConfig { + if([self configPath] == nil) { + ALog(@"No config path configured!"); + return; } - [super finalize]; + // check for existence + NSFileManager *fm = [NSFileManager defaultManager]; + BOOL isDir; + ALog(@"Checking for config path at: %@", [self configPath]); + if(![fm fileExistsAtPath:[self configPath]] && [self createConfigPath]) { + ALog(@"Config dir doesn't exist, creating it..."); + [fm createDirectoryAtPath:[self configPath] withIntermediateDirectories:NO attributes:nil error:NULL]; + ALog(@"Config dir doesn't exist, creating it...done"); + } + + if([fm fileExistsAtPath:[self configPath] isDirectory:&isDir] && (isDir)) { + // check config + if(![fm fileExistsAtPath:[self createInstallMgrConfPath]]) { + // create config entry + sword::SWConfig config([[self createInstallMgrConfPath] UTF8String]); + config["General"]["PassiveFTP"] = "true"; + config.Save(); + } + + } else { + ALog(@"Config path does not exist: %@", [self configPath]); + } } -- (void)dealloc { - if(swInstallMgr != nil) { - delete swInstallMgr; +- (void)setupInstallSources { + NSMutableDictionary *dict = [NSMutableDictionary dictionary]; + for(InstallSourceMap::iterator it = swInstallMgr->sources.begin(); it != swInstallMgr->sources.end(); it++) { + sword::InstallSource *sis = it->second; + SwordInstallSource *is = [[SwordInstallSource alloc] initWithSource:sis]; + + ALog(@"Adding install source: %@", [is caption]); + dict[[is caption]] = is; } - - [self setConfigPath:nil]; - [self setInstallSources:nil]; - [self setInstallSourceList:nil]; - [self setConfigFilePath:nil]; - - [super dealloc]; + + [self setInstallSources:dict]; } -- (void)addInstallSource:(SwordInstallSource *)is { - [self addInstallSource:is withReinitialize:YES]; +- (NSString *)createInstallMgrConfPath { + return [[self configPath] stringByAppendingPathComponent:@"InstallMgr.conf"]; } -// add/remove install sources -- (void)addInstallSource:(SwordInstallSource *)is withReinitialize:(BOOL)reinit { - - // save at once - sword::SWConfig config([self.configFilePath cStringUsingEncoding:NSUTF8StringEncoding]); +- (void)addInstallSource:(SwordInstallSource *)is reload:(BOOL)doReload { + ALog(@"Adding install source: %@", [is caption]); + + // modify conf file + sword::SWConfig config([[self createInstallMgrConfPath] UTF8String]); if([[is type] isEqualToString:INSTALLSOURCE_TYPE_FTP]) { config["Sources"].insert(ConfigEntMap::value_type(INSTALLSOURCE_SECTION_TYPE_FTP, [[is configEntry] UTF8String])); } else { config["Sources"].insert(ConfigEntMap::value_type(INSTALLSOURCE_SECTION_TYPE_HTTP, [[is configEntry] UTF8String])); } config.Save(); - - if(reinit) - [self reinitialize]; -} -- (void)removeInstallSource:(SwordInstallSource *)is { - [self removeInstallSource:is withReinitialize:NO]; + if(doReload) [self reloadManager]; } -- (void)removeInstallSource:(SwordInstallSource *)is withReinitialize:(BOOL)reinit { - - // remove source - [self.installSources removeObjectForKey:[is caption]]; - [self.installSourceList removeObject:is]; - - // save at once - sword::SWConfig config([self.configFilePath cStringUsingEncoding:NSUTF8StringEncoding]); +- (void)removeInstallSource:(SwordInstallSource *)is reload:(BOOL)doReload { + ALog(@"Removing install source: %@", [is caption]); + NSMutableDictionary *dict = [[self installSources] mutableCopy]; + [dict removeObjectForKey:[is caption]]; + + // modify conf file + sword::SWConfig config([[self createInstallMgrConfPath] UTF8String]); config["Sources"].erase(INSTALLSOURCE_SECTION_TYPE_HTTP); config["Sources"].erase(INSTALLSOURCE_SECTION_TYPE_FTP); - + // build up new - for(SwordInstallSource *sis in self.installSources) { + for(SwordInstallSource *sis in [dict allValues]) { if([[sis type] isEqualToString:INSTALLSOURCE_TYPE_FTP]) { config["Sources"].insert(ConfigEntMap::value_type(INSTALLSOURCE_SECTION_TYPE_FTP, [[sis configEntry] UTF8String])); } else { @@ -229,63 +231,65 @@ base path of the module installation } } config.Save(); - - if(reinit) - [self reinitialize]; + + if(doReload) [self reloadManager]; } - (void)updateInstallSource:(SwordInstallSource *)is { + ALog(@"Updating install source [remove|add]: %@", [is caption]); + // hold a ref to the is + SwordInstallSource *save = is; // first remove, then add again - [self removeInstallSource:is withReinitialize:NO]; - [self addInstallSource:is]; + [self removeInstallSource:save reload:NO]; + [self addInstallSource:save reload:NO]; + save = nil; + + [self reloadManager]; } -// installation/unInstallation - (int)installModule:(SwordModule *)aModule fromSource:(SwordInstallSource *)is withManager:(SwordManager *)manager { - + ALog(@"Installing module: %@, from source: %@", [aModule name], [is caption]); int stat; - if([[is source] isEqualToString:@"localhost"]) { + if([is isLocalSource]) { stat = swInstallMgr->installModule([manager swManager], [[is directory] UTF8String], [[aModule name] UTF8String]); } else { stat = swInstallMgr->installModule([manager swManager], 0, [[aModule name] UTF8String], [is installSource]); } - return stat; } +- (int)uninstallModule:(SwordModule *)aModule fromManager:(SwordManager *)swManager { + ALog(@"Removing module: %@", [aModule name]); + return swInstallMgr->removeModule([swManager swManager], [[aModule name] UTF8String]); +} + - (int)refreshMasterRemoteInstallSourceList { + ALog(@"Refreshing remote install sources from master repo."); int stat = swInstallMgr->refreshRemoteSourceConfiguration(); if(stat) { ALog(@"Unable to refresh with master install source!"); + } else { + [self reloadManager]; } return stat; } -/** - uninstalls a module from a SwordManager - */ -- (int)uninstallModule:(SwordModule *)aModule fromManager:(SwordManager *)swManager { - int stat = swInstallMgr->removeModule([swManager swManager], [[aModule name] UTF8String]); - - return stat; -} - // list modules in sources -- (NSArray *)listModulesForSource:(SwordInstallSource *)is { - return [is listModules]; +- (NSDictionary *)listModulesForSource:(SwordInstallSource *)is { + return [is allModules]; } /** refresh modules of this source refreshing the install source is necessary before installation of */ - (int)refreshInstallSource:(SwordInstallSource *)is { + ALog(@"Refreshing install source:%@", [is caption]); int ret = 1; - if(is == nil) { ALog(@"Install source is nil"); } else { - if([[is source] isEqualToString:@"localhost"] == NO) { + if(![[is source] isEqualToString:@"localhost"]) { ret = swInstallMgr->refreshRemoteSource([is installSource]); } } @@ -294,12 +298,10 @@ base path of the module installation } /** - returns an array of Modules with status set, nil on error + returns an array of Modules with status set */ - (NSArray *)moduleStatusInInstallSource:(SwordInstallSource *)is baseManager:(SwordManager *)baseMgr { - - NSArray *ret = nil; - + ALog(@"Retrieving module status for install source:%@", [is caption]); // get modules map NSMutableArray *ar = [NSMutableArray array]; std::map<sword::SWModule *, int> modStats = swInstallMgr->getModuleStatus(*[baseMgr swManager], *[[is swordManager] swManager]); @@ -309,16 +311,12 @@ base path of the module installation module = it->first; status = it->second; - SwordModule *mod = [[[SwordModule alloc] initWithSWModule:module] autorelease]; + SwordModule *mod = [[SwordModule alloc] initWithSWModule:module]; [mod setStatus:status]; [ar addObject:mod]; } - - if(ar) { - ret = [NSArray arrayWithArray:ar]; - } - - return ret; + + return [NSArray arrayWithArray:ar]; } - (BOOL)userDisclaimerConfirmed { @@ -329,6 +327,10 @@ base path of the module installation swInstallMgr->setUserDisclaimerConfirmed(flag); } +- (void)readInstallMgrConf { + swInstallMgr->readInstallConf(); +} + /** low level access */ - (sword::InstallMgr *)installMgr { return swInstallMgr; diff --git a/bindings/objc/src/SwordKey.mm b/bindings/objc/src/SwordKey.mm index 5254443..fc0449d 100644 --- a/bindings/objc/src/SwordKey.mm +++ b/bindings/objc/src/SwordKey.mm @@ -12,19 +12,19 @@ @implementation SwordKey + (SwordKey *)swordKey { - return [[[SwordKey alloc] init] autorelease]; + return [[SwordKey alloc] init]; } + (SwordKey *)swordKeyWithRef:(NSString *)aRef { - return [[[SwordKey alloc] initWithRef:aRef] autorelease]; + return [[SwordKey alloc] initWithRef:aRef]; } + (SwordKey *)swordKeyWithSWKey:(sword::SWKey *)aSk { - return [[[SwordKey alloc] initWithSWKey:aSk] autorelease]; + return [[SwordKey alloc] initWithSWKey:aSk]; } + (SwordKey *)swordKeyWithSWKey:(sword::SWKey *)aSk makeCopy:(BOOL)copy { - return [[[SwordKey alloc] initWithSWKey:aSk makeCopy:copy] autorelease]; + return [[SwordKey alloc] initWithSWKey:aSk makeCopy:copy]; } - (id)init { @@ -40,8 +40,9 @@ if(self) { if(copy) { if(aSk) { - sk = aSk->clone(); - created = YES; + sk = aSk->clone(); + sk->setPersist(true); + created = YES; } else { created = NO; } @@ -49,7 +50,7 @@ sk = aSk; created = NO; } - } + } return self; } @@ -63,20 +64,11 @@ return self; } -- (void)finalize { - if(created) { - delete sk; - } - - [super finalize]; -} - (void)dealloc { if(created) { delete sk; } - - [super dealloc]; } - (SwordKey *)clone { @@ -86,15 +78,15 @@ #pragma mark - Methods - (void)setPersist:(BOOL)flag { - sk->Persist((int)flag); + sk->setPersist(flag); } - (BOOL)persist { - return (BOOL)sk->Persist(); + return (BOOL)sk->isPersist(); } - (int)error { - return sk->Error(); + return sk->popError(); } - (void)setPosition:(int)aPosition { diff --git a/bindings/objc/src/SwordListKey.mm b/bindings/objc/src/SwordListKey.mm index 07aa8f8..dfe0150 100644 --- a/bindings/objc/src/SwordListKey.mm +++ b/bindings/objc/src/SwordListKey.mm @@ -11,23 +11,23 @@ @implementation SwordListKey + (SwordListKey *)listKeyWithRef:(NSString *)aRef { - return [[[SwordListKey alloc] initWithRef:aRef] autorelease]; + return [[SwordListKey alloc] initWithRef:aRef]; } + (SwordListKey *)listKeyWithRef:(NSString *)aRef v11n:(NSString *)scheme { - return [[[SwordListKey alloc] initWithRef:aRef v11n:scheme] autorelease]; + return [[SwordListKey alloc] initWithRef:aRef v11n:scheme]; } + (SwordListKey *)listKeyWithRef:(NSString *)aRef headings:(BOOL)headings v11n:(NSString *)scheme { - return [[[SwordListKey alloc] initWithRef:aRef headings:headings v11n:scheme] autorelease]; + return [[SwordListKey alloc] initWithRef:aRef headings:headings v11n:scheme]; } + (SwordListKey *)listKeyWithSWListKey:(sword::ListKey *)aLk { - return [[[SwordListKey alloc] initWithSWListKey:aLk] autorelease]; + return [[SwordListKey alloc] initWithSWListKey:aLk]; } + (SwordListKey *)listKeyWithSWListKey:(sword::ListKey *)aLk makeCopy:(BOOL)copy { - return [[[SwordListKey alloc] initWithSWListKey:aLk makeCopy:copy] autorelease]; + return [[SwordListKey alloc] initWithSWListKey:aLk makeCopy:copy]; } - (id)init { @@ -52,49 +52,40 @@ - (SwordListKey *)initWithRef:(NSString *)aRef headings:(BOOL)headings v11n:(NSString *)scheme { sword::VerseKey vk; - vk.Headings((char)headings); + vk.setIntros((char)headings); if(scheme) { vk.setVersificationSystem([scheme UTF8String]); } - sword::ListKey listKey = vk.ParseVerseList([aRef UTF8String], "gen", true); + sword::ListKey listKey = vk.parseVerseList([aRef UTF8String], "gen", true); sword::ListKey *lk = new sword::ListKey(listKey); - return (SwordListKey *) [super initWithSWKey:lk]; -} + lk->setPersist(true); -- (void)finalize { - [super finalize]; -} - -- (void)dealloc { - [super dealloc]; + return (SwordListKey *) [super initWithSWKey:lk]; } - (NSInteger)numberOfVerses { NSInteger ret = 0; - if(sk) { - for(*sk = sword::TOP; !sk->Error(); *sk++) ret++; + for(*sk = sword::TOP; !sk->popError(); *sk++) ret++; } - return ret; } - (void)parse { - } - (void)parseWithHeaders { } - (VerseEnumerator *)verseEnumerator { - return [[[VerseEnumerator alloc] initWithListKey:self] autorelease]; + return [[VerseEnumerator alloc] initWithListKey:self]; } - (BOOL)containsKey:(SwordVerseKey *)aVerseKey { BOOL ret = NO; if(sk) { *sk = [[aVerseKey osisRef] UTF8String]; - ret = !sk->Error(); + ret = !sk->popError(); } return ret; } diff --git a/bindings/objc/src/SwordLocaleManager.h b/bindings/objc/src/SwordLocaleManager.h index dd67a13..2cfbe63 100644 --- a/bindings/objc/src/SwordLocaleManager.h +++ b/bindings/objc/src/SwordLocaleManager.h @@ -25,4 +25,9 @@ */ - (void)initLocaleWithLocaledPath:(NSString *)aPath; +/** + Retrieves the configured default locale name + */ +- (NSString *)getDefaultLocaleName; + @end diff --git a/bindings/objc/src/SwordLocaleManager.mm b/bindings/objc/src/SwordLocaleManager.mm index 18e4eba..1d8d647 100644 --- a/bindings/objc/src/SwordLocaleManager.mm +++ b/bindings/objc/src/SwordLocaleManager.mm @@ -49,10 +49,10 @@ sword::SWBuf locale; for(it = localeList.begin(); it != localeList.end(); ++it) { locale = *it; - NSString *swLoc = [NSString stringWithCString:locale.c_str() encoding:NSUTF8StringEncoding]; - if([swLoc hasPrefix:loc]) { + NSString *swLoc = [NSString stringWithUTF8String:locale.c_str()]; + if([loc hasPrefix:swLoc]) { haveLocale = YES; - lang = loc; + lang = swLoc; break; } } @@ -63,4 +63,12 @@ } } +- (NSString *)getDefaultLocaleName { + sword::LocaleMgr *lManager = sword::LocaleMgr::getSystemLocaleMgr(); + + const char *localeName = lManager->getDefaultLocaleName(); + if(localeName == NULL) return nil; + else return [NSString stringWithUTF8String:localeName]; +} + @end diff --git a/bindings/objc/src/SwordManager.h b/bindings/objc/src/SwordManager.h index daf8ece..33439f1 100644 --- a/bindings/objc/src/SwordManager.h +++ b/bindings/objc/src/SwordManager.h @@ -18,7 +18,6 @@ #include <swmgr.h> // C++ Sword API #include <localemgr.h> #include <versekey.h> -class sword::SWModule; #endif /** the major types as returned in -[SwordModule -typeString] */ @@ -106,21 +105,13 @@ class sword::SWModule; #ifdef __cplusplus sword::SWMgr *swManager; - SwordFilter *plainFilter, *thmlFilter, *gbfFilter, *osisFilter, *teiFilter; - SwordFilter *thmlStripFilter, *gbfStripFilter, *osisStripFilter, *teiStripFilter; #endif - NSDictionary *modules; - NSString *modulesPath; - NSLock *managerLock; - BOOL temporaryManager; } // ------------------- getter / setter ------------------- -@property (retain, readwrite) NSDictionary *modules; -@property (retain, readwrite) NSString *modulesPath; -@property (retain, readwrite) NSLock *managerLock; -@property (readwrite) BOOL temporaryManager; +@property (strong, readwrite) NSString *modulesPath; +@property (strong, readwrite) NSLock *managerLock; // --------------------- methods ----------------------- @@ -143,6 +134,9 @@ class sword::SWModule; */ + (NSArray *)moduleTypes; +/** uses the current instance as default manager */ +- (void)useAsDefaultManager; + /** Initializes this manager for the given module path. */ @@ -154,10 +148,12 @@ class sword::SWModule; - (void)addModulesPath:(NSString*)path; /** - re-init - reloads all modules, filters and such - This will be triggered if new module had been installed in the module installer. + Loads all modules, filters and such */ -- (void)reInit; +- (void)initManager; + +/** should be called to reload all modules and such */ +- (void)reloadManager; /** Set a cipher key for the given module to make it unlocked and in order to render it's text. @@ -174,6 +170,9 @@ class sword::SWModule; */ - (BOOL)globalOption:(NSString *)option; +/** the number of modules */ +- (NSInteger)numberOfModules; + /** List of module names known by this manager */ @@ -182,7 +181,7 @@ class sword::SWModule; /** List of modules known by this manager */ -- (NSArray *)listModules; +- (NSDictionary *)allModules; /** Module list sorted by name diff --git a/bindings/objc/src/SwordManager.mm b/bindings/objc/src/SwordManager.mm index c2962fb..af5b721 100644 --- a/bindings/objc/src/SwordManager.mm +++ b/bindings/objc/src/SwordManager.mm @@ -13,131 +13,38 @@ #import <ObjCSword/ObjCSword.h> #import "Notifications.h" -#import "FilterProviderFactory.h" -#import "DefaultFilterProvider.h" #include "encfiltmgr.h" -#import "SwordFilter.h" using std::string; using std::list; -@interface SwordManager (PrivateAPI) +@interface SwordManager () + +@property (strong, readwrite) NSDictionary *modules; +@property (readwrite) BOOL deleteSWMgr; -- (void)refreshModules; - (void)addFiltersToModule:(SwordModule *)mod; @end -@implementation SwordManager (PrivateAPI) - -- (void)refreshModules { - NSMutableDictionary *dict = [NSMutableDictionary dictionary]; - - // loop over modules - sword::SWModule *mod; - for(sword::ModMap::iterator it = swManager->Modules.begin(); it != swManager->Modules.end(); it++) { - mod = it->second; - - if(mod) { - // temporary instance - SwordModule *swMod = [SwordModule moduleForSWModule:mod]; - NSString *type = [swMod typeString]; - - ModuleType aType = [SwordModule moduleTypeForModuleTypeString:type]; - SwordModule *sm = [SwordModule moduleForType:aType swModule:mod swordManager:self]; - [dict setObject:sm forKey:[[sm name] lowercaseString]]; - - [self addFiltersToModule:sm]; - } - } - - // set modules - self.modules = dict; -} - -- (void)addFiltersToModule:(SwordModule *)mod { - // prepare display filters - - id<FilterProvider> filterProvider = [[FilterProviderFactory providerFactory] get]; - - switch([mod swModule]->Markup()) { - case sword::FMT_GBF: - if(!gbfFilter) { - gbfFilter = [filterProvider newGbfRenderFilter]; - } - if(!gbfStripFilter) { - gbfStripFilter = [filterProvider newGbfPlainFilter]; - } - [mod addRenderFilter:gbfFilter]; - [mod addStripFilter:gbfStripFilter]; - break; - case sword::FMT_THML: - if(!thmlFilter) { - thmlFilter = [filterProvider newThmlRenderFilter]; - } - if(!thmlStripFilter) { - thmlStripFilter = [filterProvider newThmlPlainFilter]; - } - [mod addRenderFilter:thmlFilter]; - [mod addStripFilter:thmlStripFilter]; - break; - case sword::FMT_OSIS: - if(!osisFilter) { - osisFilter = [filterProvider newOsisRenderFilter]; - } - if(!osisStripFilter) { - osisStripFilter = [filterProvider newOsisPlainFilter]; - } - [mod addRenderFilter:osisFilter]; - [mod addStripFilter:osisStripFilter]; - break; - case sword::FMT_TEI: - if(!teiFilter) { - teiFilter = [filterProvider newTeiRenderFilter]; - } - if(!teiStripFilter) { - teiStripFilter = [filterProvider newTeiPlainFilter]; - } - [mod addRenderFilter:teiFilter]; - [mod addStripFilter:teiStripFilter]; - break; - case sword::FMT_PLAIN: - default: - if(!plainFilter) { - plainFilter = [filterProvider newHtmlPlainFilter]; - } - [mod addRenderFilter:plainFilter]; - break; - } -} - -@end @implementation SwordManager -@synthesize modules; -@synthesize modulesPath; -@synthesize managerLock; -@synthesize temporaryManager; - # pragma mark - class methods +static SwordManager *instance = nil; + + (NSArray *)moduleTypes { - return [NSArray arrayWithObjects: - SWMOD_TYPES_BIBLES, - SWMOD_TYPES_COMMENTARIES, - SWMOD_TYPES_DICTIONARIES, - SWMOD_TYPES_GENBOOKS, nil]; + return @[SWMOD_TYPES_BIBLES, SWMOD_TYPES_COMMENTARIES, SWMOD_TYPES_DICTIONARIES, SWMOD_TYPES_GENBOOKS]; } + (SwordManager *)managerWithPath:(NSString *)path { - SwordManager *manager = [[[SwordManager alloc] initWithPath:path] autorelease]; + SwordManager *manager = [[SwordManager alloc] initWithPath:path]; return manager; } + (SwordManager *)defaultManager { - static SwordManager *instance = nil; if(instance == nil) { // use default path instance = [[SwordManager alloc] initWithPath:[[Configuration config] defaultModulePath]]; @@ -146,24 +53,25 @@ using std::list; return instance; } -- (id)initWithPath:(NSString *)path { +- (void)useAsDefaultManager { + instance = self; +} +- (id)initWithPath:(NSString *)path { if((self = [super init])) { - // this is our main swManager - temporaryManager = NO; - + ALog(@"Init with path:%@", path); + self.deleteSWMgr = YES; self.modulesPath = path; + self.managerLock = (id) [[NSRecursiveLock alloc] init]; - self.modules = [NSDictionary dictionary]; - self.managerLock = (NSLock *)[[[NSRecursiveLock alloc] init] autorelease]; - - [self reInit]; + [self initManager]; + // all global options off sword::StringList options = swManager->getGlobalOptions(); sword::StringList::iterator it; for(it = options.begin(); it != options.end(); it++) { [self setGlobalOption:[NSString stringWithCString:it->c_str() encoding:NSUTF8StringEncoding] value:SW_OFF]; - } + } } return self; @@ -172,62 +80,44 @@ using std::list; - (id)initWithSWMgr:(sword::SWMgr *)aSWMgr { self = [super init]; if(self) { + ALog(@"Init with temporary SWMgr"); swManager = aSWMgr; - // this is a temporary swManager - temporaryManager = YES; - - self.modules = [NSDictionary dictionary]; - self.managerLock = [(NSLock *)[[NSRecursiveLock alloc] init] autorelease]; + self.deleteSWMgr = NO; + self.managerLock = (id) [[NSRecursiveLock alloc] init]; - [self refreshModules]; + [self applyFilters]; } return self; } -- (void)finalize { - if(!temporaryManager) { - delete swManager; - } - - [super finalize]; -} - (void)dealloc { - if(!temporaryManager) { + DLog(@""); + if(self.deleteSWMgr) { + // only delete swManager is we created it + // if it came from someplace else then we're not responsible + ALog(@"Deleting SWMgr!"); delete swManager; } - [self setModules:nil]; - [self setModulesPath:nil]; - [self setManagerLock:nil]; - - [gbfFilter release]; - [gbfStripFilter release]; - [thmlFilter release]; - [thmlStripFilter release]; - [osisFilter release]; - [osisStripFilter release]; - [teiFilter release]; - [teiStripFilter release]; - [plainFilter release]; - [super dealloc]; } -- (void)reInit { - [managerLock lock]; - if(modulesPath && [modulesPath length] > 0) { +- (void)initManager { + DLog(@""); + [self.managerLock lock]; + if(self.modulesPath && [self.modulesPath length] > 0) { - // modulePath is the main sw manager - swManager = new sword::SWMgr([modulesPath UTF8String], true, new sword::EncodingFilterMgr(sword::ENC_UTF8)); + NSFileManager *fm = [NSFileManager defaultManager]; + if(![fm fileExistsAtPath:self.modulesPath]) { + [self createModuleFolderTemplate]; + } + + swManager = new sword::SWMgr([self.modulesPath UTF8String], true, new sword::EncodingFilterMgr(sword::ENC_UTF8)); if(!swManager) { ALog(@"Cannot create SWMgr instance for default module path!"); } else { - NSFileManager *fm = [NSFileManager defaultManager]; - NSArray *subDirs = [fm contentsOfDirectoryAtPath:modulesPath error:NULL]; - // for all sub directories add module - BOOL directory; - NSString *fullSubDir; + NSArray *subDirs = [fm contentsOfDirectoryAtPath:self.modulesPath error:NULL]; NSString *subDir; for(subDir in subDirs) { // as long as it's not hidden @@ -235,10 +125,11 @@ using std::list; ![subDir isEqualToString:@"InstallMgr"] && ![subDir isEqualToString:@"mods.d"] && ![subDir isEqualToString:@"modules"]) { - fullSubDir = [modulesPath stringByAppendingPathComponent:subDir]; + NSString *fullSubDir = [self.modulesPath stringByAppendingPathComponent:subDir]; fullSubDir = [fullSubDir stringByStandardizingPath]; //if its a directory + BOOL directory; if([fm fileExistsAtPath:fullSubDir isDirectory:&directory]) { if(directory) { DLog(@"Augmenting folder: %@", fullSubDir); @@ -249,78 +140,125 @@ using std::list; } } - // clear some data - [self refreshModules]; - - SendNotifyModulesChanged(NULL); + [self applyFilters]; } } - [managerLock unlock]; + [self.managerLock unlock]; } -- (void)addModulesPath:(NSString *)path { - [managerLock lock]; - if(swManager == nil) { - swManager = new sword::SWMgr([path UTF8String], true, new sword::EncodingFilterMgr(sword::ENC_UTF8)); - } else { - swManager->augmentModules([path UTF8String]); +- (void)reloadManager { + if(swManager != NULL) { + + swManager->Load(); + [self applyFilters]; } +} + +- (void)createModuleFolderTemplate { + NSFileManager *fm = [NSFileManager defaultManager]; + [fm createDirectoryAtPath:self.modulesPath withIntermediateDirectories:NO attributes:nil error:NULL]; + [fm createDirectoryAtPath:[self.modulesPath stringByAppendingPathComponent:@"mods.d"] withIntermediateDirectories:NO attributes:nil error:NULL]; + [fm createDirectoryAtPath:[self.modulesPath stringByAppendingPathComponent:@"modules"] withIntermediateDirectories:NO attributes:nil error:NULL]; +} + +- (void)addModulesPath:(NSString *)path { + [self.managerLock lock]; + swManager->augmentModules([path UTF8String]); - [self refreshModules]; - [managerLock unlock]; - - SendNotifyModulesChanged(NULL); + [self applyFilters]; + [self.managerLock unlock]; } -- (SwordModule *)moduleWithName:(NSString *)name { - - SwordModule *ret = [modules objectForKey:[name lowercaseString]]; - if(ret == nil) { - sword::SWModule *mod = [self getSWModuleWithName:name]; - if(mod == NULL) { - ALog(@"No module by that name: %@!", name); - } else { - // temporary instance - SwordModule *swMod = [SwordModule moduleForSWModule:mod]; - NSString *type = [swMod typeString]; - - ModuleType aType = [SwordModule moduleTypeForModuleTypeString:type]; - ret = [SwordModule moduleForType:aType swModule:mod swordManager:self]; +- (void)applyFilters { + for(SwordModule *mod in [[self allModules] allValues]) { + [self addFiltersToModule:mod]; + } +} - if(ret != nil) { - NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithDictionary:modules]; - [dict setObject:ret forKey:[name lowercaseString]]; - self.modules = dict; - } - } +- (void)addFiltersToModule:(SwordModule *)mod { + // prepare display filters + + id<FilterProvider> filterProvider = [[FilterProviderFactory providerFactory] get]; + + switch([mod swModule]->getMarkup()) { + case sword::FMT_GBF: + [mod addRenderFilter:[filterProvider newGbfRenderFilter]]; + [mod addStripFilter:[filterProvider newGbfPlainFilter]]; + break; + case sword::FMT_THML: + [mod addRenderFilter:[filterProvider newThmlRenderFilter]]; + [mod addStripFilter:[filterProvider newThmlPlainFilter]]; + break; + case sword::FMT_OSIS: + [mod addRenderFilter:[filterProvider newOsisRenderFilter]]; + [mod addStripFilter:[filterProvider newOsisPlainFilter]]; + break; + case sword::FMT_TEI: + [mod addRenderFilter:[filterProvider newTeiRenderFilter]]; + [mod addStripFilter:[filterProvider newTeiPlainFilter]]; + break; + case sword::FMT_PLAIN: + default: + [mod addRenderFilter:[filterProvider newOsisPlainFilter]]; + break; } +} + +- (SwordModule *)moduleWithName:(NSString *)name { - return ret; + sword::SWModule *mod = [self getSWModuleWithName:name]; + if(mod == NULL) { + ALog(@"No module by that name: %@!", name); + return nil; + + } else { + // temporary instance + NSString *type = [NSString stringWithUTF8String:mod->getType()]; + + ModuleType aType = [SwordModule moduleTypeForModuleTypeString:type]; + return [SwordModule moduleForType:aType swModule:mod]; + } } - (void)setCipherKey:(NSString *)key forModuleNamed:(NSString *)name { - [managerLock lock]; swManager->setCipherKey([name UTF8String], [key UTF8String]); - [managerLock unlock]; } #pragma mark - module access - (void)setGlobalOption:(NSString *)option value:(NSString *)value { - [managerLock lock]; + [self.managerLock lock]; swManager->setGlobalOption([option UTF8String], [value UTF8String]); - [managerLock unlock]; + [self.managerLock unlock]; } - (BOOL)globalOption:(NSString *)option { return [[NSString stringWithUTF8String:swManager->getGlobalOption([option UTF8String])] isEqualToString:SW_ON]; } -- (NSArray *)listModules { - return [modules allValues]; +- (NSInteger)numberOfModules { + return swManager->Modules.size(); } + +- (NSDictionary *)allModules { + NSMutableDictionary *dict = [NSMutableDictionary dictionary]; + sword::SWModule *mod; + for(sword::ModMap::iterator it = swManager->Modules.begin(); it != swManager->Modules.end(); it++) { + mod = it->second; + + if(mod) { + NSString *type = [NSString stringWithUTF8String:mod->getType()]; + + ModuleType aType = [SwordModule moduleTypeForModuleTypeString:type]; + SwordModule *swMod = [SwordModule moduleForType:aType swModule:mod]; + [dict setObject:swMod forKey:[swMod name]]; + } + } + return [NSDictionary dictionaryWithDictionary:dict]; +} + - (NSArray *)moduleNames { - return [modules allKeys]; + return [[self allModules] allKeys]; } - (NSArray *)sortedModuleNames { @@ -329,14 +267,14 @@ using std::list; - (NSArray *)modulesForFeature:(NSString *)feature { NSMutableArray *ret = [NSMutableArray array]; - for(SwordModule *mod in [modules allValues]) { + for(SwordModule *mod in [[self allModules] allValues]) { if([mod hasFeature:feature]) { [ret addObject:mod]; } } // sort - NSArray *sortDescriptors = [NSArray arrayWithObject:[[[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES] autorelease]]; + NSArray *sortDescriptors = @[[[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES]]; [ret sortUsingDescriptors:sortDescriptors]; return [NSArray arrayWithArray:ret]; @@ -344,14 +282,14 @@ using std::list; - (NSArray *)modulesForType:(ModuleType)type { NSMutableArray *ret = [NSMutableArray array]; - for(SwordModule *mod in [modules allValues]) { + for(SwordModule *mod in [[self allModules] allValues]) { if([mod type] == type || type == All) { [ret addObject:mod]; } } // sort - NSArray *sortDescriptors = [NSArray arrayWithObject:[[[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES] autorelease]]; + NSArray *sortDescriptors = @[[[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES]]; [ret sortUsingDescriptors:sortDescriptors]; return [NSArray arrayWithArray:ret]; @@ -359,14 +297,13 @@ using std::list; - (NSArray *)modulesForCategory:(ModuleCategory)cat { NSMutableArray *ret = [NSMutableArray array]; - for(SwordModule *mod in [modules allValues]) { + for(SwordModule *mod in [[self allModules] allValues]) { if([mod category] == cat) { [ret addObject:mod]; } } - // sort - NSArray *sortDescriptors = [NSArray arrayWithObject:[[[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES] autorelease]]; + NSArray *sortDescriptors = @[[[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES]]; [ret sortUsingDescriptors:sortDescriptors]; return [NSArray arrayWithArray:ret]; @@ -379,13 +316,7 @@ using std::list; } - (sword::SWModule *)getSWModuleWithName:(NSString *)moduleName { - sword::SWModule *module; - - [managerLock lock]; - module = swManager->Modules[[moduleName UTF8String]]; - [managerLock unlock]; - - return module; + return swManager->Modules[[moduleName UTF8String]]; } @end diff --git a/bindings/objc/src/SwordModule+Index.mm b/bindings/objc/src/SwordModule+Index.mm index 91dfa06..a19dc60 100644 --- a/bindings/objc/src/SwordModule+Index.mm +++ b/bindings/objc/src/SwordModule+Index.mm @@ -36,12 +36,11 @@ results.sort(); NSMutableArray *retArray = [NSMutableArray array]; - if(results.Count() > 0) { - while(!results.Error()) { + if(results.getCount() > 0) { + while(!results.popError()) { NSString *keyString = [NSString stringWithUTF8String:results.getText()]; SwordModuleTextEntry *entry = [SwordModuleTextEntry textEntryForKey:keyString andText:nil]; [retArray addObject:entry]; - [entry release]; results++; } } diff --git a/bindings/objc/src/SwordModule.h b/bindings/objc/src/SwordModule.h index 9f1e0ad..4660b5f 100644 --- a/bindings/objc/src/SwordModule.h +++ b/bindings/objc/src/SwordModule.h @@ -30,8 +30,7 @@ #define SWPOS_TOP 1 -@class SwordManager, SwordModuleTextEntry, SwordKey; -@class SwordFilter; +@class SwordManager, SwordModuleTextEntry, SwordKey, SwordFilter; typedef enum { TextTypeStripped = 1, @@ -65,21 +64,8 @@ typedef enum { @interface SwordModule : NSObject { - NSMutableDictionary *configEntries; - ModuleType type; ModuleCategory category; - int status; - SwordManager *swManager; - NSRecursiveLock *moduleLock; - NSLock *indexLock; - - /** we store the name separately */ - NSString *typeString; - NSString *name; - NSString *descr; - NSString *lang; - /** yes, we have a delegate to report any action to */ id delegate; @@ -91,15 +77,10 @@ typedef enum { // ------------- properties --------------- @property (readwrite) ModuleType type; @property (readwrite) int status; -@property (retain, readwrite) NSRecursiveLock *moduleLock; -@property (retain, readwrite) NSLock *indexLock; -@property (retain, readwrite) SwordManager *swManager; - -// swmodule properties -@property (retain, readonly) NSString *name; -@property (retain, readonly) NSString *typeString; -@property (retain, readonly) NSString *descr; -@property (retain, readonly) NSString *lang; +@property (strong, readwrite) SwordManager *swManager; +@property (strong, readwrite) NSLock *indexLock; +@property (strong, readwrite) NSRecursiveLock *moduleLock; +@property (strong, readwrite) NSMutableDictionary *configEntries; #ifdef __cplusplus @@ -107,12 +88,11 @@ typedef enum { Convenience initializer */ + (id)moduleForSWModule:(sword::SWModule *)aModule; -+ (id)moduleForSWModule:(sword::SWModule *)aModule swordManager:(SwordManager *)aManager; /** Factory method that creates the correct module type instance for the given type */ -+ (id)moduleForType:(ModuleType)aType swModule:(sword::SWModule *)swModule swordManager:(SwordManager *)aManager; ++ (id)moduleForType:(ModuleType)aType swModule:(sword::SWModule *)swModule; /** Initialize this module with an the SWModule. @@ -121,12 +101,6 @@ typedef enum { - (id)initWithSWModule:(sword::SWModule *)aModule; /** - Initialize this module with an the SWModule and a SwordManager instance. - This initializer should normally not need to be used. - */ -- (id)initWithSWModule:(sword::SWModule *)aModule swordManager:(SwordManager *)aManager; - -/** Retrieve the underlying SWModule instance */ - (sword::SWModule *)swModule; @@ -149,12 +123,6 @@ typedef enum { // ------------- instance methods --------------- -/** - Initializes module with a given name and SwordManager. - The SwordManager is needed because the underlying SWModule is retrieved from SwordManager. - */ -- (id)initWithName:(NSString *)aName swordManager:(SwordManager *)aManager; - /** Adds a render filter to this module */ - (void)addRenderFilter:(SwordFilter *)aFilter; @@ -166,8 +134,16 @@ typedef enum { */ - (NSInteger)error; -// --------------- Conf entries -------------- +/** module name */ +- (NSString *)name; +/** module description */ +- (NSString *)descr; +/** module language */ +- (NSString *)lang; +/** module type string */ +- (NSString *)typeString; +// --------------- Conf entries -------------- /** Module category as string */ @@ -226,13 +202,6 @@ typedef enum { */ - (NSString *)configFileEntryForConfigKey:(NSString *)entryKey; -// ------------------ module unlocking ------------------ - -/** - Unlock this module with a cipher key, if it is encrypted. - */ -- (BOOL)unlock:(NSString *)unlockKey; - // ------------------ module access semaphores ----------------- /** @@ -303,19 +272,11 @@ typedef enum { - (NSString *)entryAttributeValueFootnoteOfType:(NSString *)fnType indexValue:(NSString *)index forKey:(SwordKey *)aKey; - (NSArray *)entryAttributeValuesLemma; +- (NSArray *)entryAttributeValuesLemmaNormalized; // ----------------- Module text access ---------------------- /** - Retrieves a text entry for a given key. - Type can be: "rendered" or "stripped" - */ -- (SwordModuleTextEntry *)textEntryForKey:(SwordKey *)aKey textType:(TextPullType)aType; -/** - Convenience method with a key-string - */ -- (SwordModuleTextEntry *)textEntryForKeyString:(NSString *)aKeyString textType:(TextPullType)aType; -/** Pulls all text entries for the given reference @return Array of SwordModuleTextEntry */ @@ -354,6 +315,9 @@ typedef enum { */ - (NSArray *)renderedTextEntriesForRef:(NSString *)reference; +- (SwordModuleTextEntry *)renderedTextEntryForRef:(NSString *)reference; +- (SwordModuleTextEntry *)strippedTextEntryForRef:(NSString *)reference; + /** number of entries abstract method, should be overriden by subclasses diff --git a/bindings/objc/src/SwordModule.mm b/bindings/objc/src/SwordModule.mm index 12e2ad8..484877f 100644 --- a/bindings/objc/src/SwordModule.mm +++ b/bindings/objc/src/SwordModule.mm @@ -18,56 +18,30 @@ #import "SwordVerseKey.h" #import "SwordBible.h" #import "SwordCommentary.h" -#import "SwordDictionary.h" -#import "SwordBook.h" -#import "SwordFilter.h" +#import "SwordUtil.h" @interface SwordModule () -@property (retain, readwrite) NSString *name; -@property (retain, readwrite) NSString *typeString; -@property (retain, readwrite) NSString *descr; -@property (retain, readwrite) NSString *lang; -@property(readwrite, retain) NSMutableDictionary *configEntries; - -- (void)mainInit; - @end @implementation SwordModule -@synthesize configEntries; -@synthesize type; -@synthesize status; -@synthesize moduleLock; -@synthesize indexLock; -@synthesize swManager; -@synthesize name; -@synthesize typeString; -@synthesize descr; -@synthesize lang; - - + (id)moduleForSWModule:(sword::SWModule *)aModule { - return [[[SwordModule alloc] initWithSWModule:aModule] autorelease]; -} - -+ (id)moduleForSWModule:(sword::SWModule *)aModule swordManager:(SwordManager *)aManager { - return [[[SwordModule alloc] initWithSWModule:aModule swordManager:aManager] autorelease]; + return [[SwordModule alloc] initWithSWModule:aModule]; } -+ (id)moduleForType:(ModuleType)aType swModule:(sword::SWModule *)swModule swordManager:(SwordManager *)aManager { ++ (id)moduleForType:(ModuleType)aType swModule:(sword::SWModule *)swModule { SwordModule *sm; if(aType == Bible) { - sm = [[[SwordBible alloc] initWithSWModule:swModule swordManager:aManager] autorelease]; + sm = [[SwordBible alloc] initWithSWModule:swModule]; } else if(aType == Commentary) { - sm = [[[SwordCommentary alloc] initWithSWModule:swModule swordManager:aManager] autorelease]; + sm = [[SwordCommentary alloc] initWithSWModule:swModule]; } else if(aType == Dictionary) { - sm = [[[SwordDictionary alloc] initWithSWModule:swModule swordManager:aManager] autorelease]; + sm = [[SwordDictionary alloc] initWithSWModule:swModule]; } else if(aType == Genbook) { - sm = [[[SwordBook alloc] initWithSWModule:swModule swordManager:aManager] autorelease]; + sm = [[SwordBook alloc] initWithSWModule:swModule]; } else { - sm = [[[SwordModule alloc] initWithSWModule:swModule swordManager:aManager] autorelease]; + sm = [[SwordModule alloc] initWithSWModule:swModule]; } return sm; @@ -123,38 +97,17 @@ - (void)mainInit { category = Unset; - self.name = [self retrieveName]; - self.typeString = [self retrieveType]; - self.descr = [self retrieveDescr]; - self.lang = [self retrieveLang]; - - self.type = [SwordModule moduleTypeForModuleTypeString:self.typeString]; - self.moduleLock = [[[NSRecursiveLock alloc] init] autorelease]; - self.indexLock = [[[NSLock alloc] init] autorelease]; - self.configEntries = [NSMutableDictionary dictionary]; -} -- (id)initWithName:(NSString *)aName swordManager:(SwordManager *)aManager { - self = [super init]; - if(self) { - swModule = [aManager getSWModuleWithName:aName]; - self.swManager = aManager; - - [self mainInit]; - } - - return self; + self.type = [SwordModule moduleTypeForModuleTypeString:[self typeString]]; + self.moduleLock = [[NSRecursiveLock alloc] init]; + self.indexLock = [[NSLock alloc] init]; + self.configEntries = [NSMutableDictionary dictionary]; } - (id)initWithSWModule:(sword::SWModule *)aModule { - return [self initWithSWModule:aModule swordManager:nil]; -} - -- (id)initWithSWModule:(sword::SWModule *)aModule swordManager:(SwordManager *)aManager { self = [super init]; if(self) { swModule = aModule; - self.swManager = aManager; [self mainInit]; } @@ -162,91 +115,75 @@ return self; } -- (void)finalize { - [super finalize]; -} - -- (void)dealloc { - [self setConfigEntries:nil]; - [self setSwManager:nil]; - [self setModuleLock:nil]; - [self setIndexLock:nil]; - [self setName:nil]; - [self setDescr:nil]; - [self setTypeString:nil]; - [self setLang:nil]; - - [super dealloc]; -} - #pragma mark - Filters - (void)addRenderFilter:(SwordFilter *)aFilter { - swModule->AddRenderFilter([aFilter swFilter]); + swModule->removeRenderFilter([aFilter swFilter]); + swModule->addRenderFilter([aFilter swFilter]); } - (void)addStripFilter:(SwordFilter *)aFilter { - swModule->AddStripFilter([aFilter swFilter]); + swModule->addStripFilter([aFilter swFilter]); } #pragma mark - Module access semaphores - (void)lockModuleAccess { - [moduleLock lock]; + [self.moduleLock lock]; } - (void)unlockModuleAccess { - [moduleLock unlock]; + [self.moduleLock unlock]; } -#pragma mark - Conf entries - -- (NSAttributedString *)fullAboutText { - return [[[NSAttributedString alloc] initWithString:@""] autorelease]; -} - -- (NSInteger)error { - return swModule->Error(); -} - -- (NSString *)retrieveName { - NSString *str = [NSString stringWithCString:swModule->Name() encoding:NSUTF8StringEncoding]; +- (NSString *)name { + NSString *str = [NSString stringWithCString:swModule->getName() encoding:NSUTF8StringEncoding]; if(!str) { - str = [NSString stringWithCString:swModule->Name() encoding:NSISOLatin1StringEncoding]; + str = [NSString stringWithCString:swModule->getName() encoding:NSISOLatin1StringEncoding]; } return str; } -- (NSString *)retrieveDescr { - NSString *str = [NSString stringWithCString:swModule->Description() encoding:NSUTF8StringEncoding]; +- (NSString *)descr { + NSString *str = [NSString stringWithCString:swModule->getDescription() encoding:NSUTF8StringEncoding]; if(!str) { - str = [NSString stringWithCString:swModule->Description() encoding:NSISOLatin1StringEncoding]; + str = [NSString stringWithCString:swModule->getDescription() encoding:NSISOLatin1StringEncoding]; } return str; } -- (NSString *)retrieveLang { - NSString *str = [NSString stringWithCString:swModule->Lang() encoding:NSUTF8StringEncoding]; +- (NSString *)lang { + NSString *str = [NSString stringWithCString:swModule->getLanguage() encoding:NSUTF8StringEncoding]; if(!str) { - str = [NSString stringWithCString:swModule->Lang() encoding:NSISOLatin1StringEncoding]; + str = [NSString stringWithCString:swModule->getLanguage() encoding:NSISOLatin1StringEncoding]; } return str; } -- (NSString *)retrieveType { - NSString *str = [NSString stringWithCString:swModule->Type() encoding:NSUTF8StringEncoding]; +- (NSString *)typeString { + NSString *str = [NSString stringWithCString:swModule->getType() encoding:NSUTF8StringEncoding]; if(!str) { - str = [NSString stringWithCString:swModule->Type() encoding:NSISOLatin1StringEncoding]; + str = [NSString stringWithCString:swModule->getType() encoding:NSISOLatin1StringEncoding]; } return str; } +- (NSAttributedString *)fullAboutText { + return [[NSAttributedString alloc] initWithString:@""]; +} + +- (NSInteger)error { + return swModule->popError(); +} + +#pragma mark - Conf entries + - (NSString *)categoryString { - NSString *cat = [configEntries objectForKey:SWMOD_CONFENTRY_CATEGORY]; + NSString *cat = self.configEntries[SWMOD_CONFENTRY_CATEGORY]; if(cat == nil) { cat = [self configFileEntryForConfigKey:SWMOD_CONFENTRY_CATEGORY]; if(cat != nil) { - [configEntries setObject:cat forKey:SWMOD_CONFENTRY_CATEGORY]; + self.configEntries[SWMOD_CONFENTRY_CATEGORY] = cat; } } @@ -261,11 +198,11 @@ } - (NSString *)cipherKey { - NSString *cipherKey = [configEntries objectForKey:SWMOD_CONFENTRY_CIPHERKEY]; + NSString *cipherKey = self.configEntries[SWMOD_CONFENTRY_CIPHERKEY]; if(cipherKey == nil) { cipherKey = [self configFileEntryForConfigKey:SWMOD_CONFENTRY_CIPHERKEY]; if(cipherKey != nil) { - [configEntries setObject:cipherKey forKey:SWMOD_CONFENTRY_CIPHERKEY]; + self.configEntries[SWMOD_CONFENTRY_CIPHERKEY] = cipherKey; } } @@ -273,11 +210,11 @@ } - (NSString *)version { - NSString *version = [configEntries objectForKey:SWMOD_CONFENTRY_VERSION]; + NSString *version = self.configEntries[SWMOD_CONFENTRY_VERSION]; if(version == nil) { version = [self configFileEntryForConfigKey:SWMOD_CONFENTRY_VERSION]; if(version != nil) { - [configEntries setObject:version forKey:SWMOD_CONFENTRY_VERSION]; + self.configEntries[SWMOD_CONFENTRY_VERSION] = version; } } @@ -285,11 +222,11 @@ } - (NSString *)minVersion { - NSString *minVersion = [configEntries objectForKey:SWMOD_CONFENTRY_MINVERSION]; + NSString *minVersion = self.configEntries[SWMOD_CONFENTRY_MINVERSION]; if(minVersion == nil) { minVersion = [self configFileEntryForConfigKey:SWMOD_CONFENTRY_MINVERSION]; if(minVersion != nil) { - [configEntries setObject:minVersion forKey:SWMOD_CONFENTRY_MINVERSION]; + self.configEntries[SWMOD_CONFENTRY_MINVERSION] = minVersion; } } @@ -298,7 +235,7 @@ /** this might be RTF string but the return value will be converted to UTF8 */ - (NSString *)aboutText { - NSMutableString *aboutText = [configEntries objectForKey:SWMOD_CONFENTRY_ABOUT]; + NSMutableString *aboutText = self.configEntries[SWMOD_CONFENTRY_ABOUT]; if(aboutText == nil) { aboutText = [NSMutableString stringWithString:[self configFileEntryForConfigKey:SWMOD_CONFENTRY_ABOUT]]; if(aboutText != nil) { @@ -311,7 +248,7 @@ [aboutText replaceOccurrencesOfString:@"\\pard" withString:@"" options:0 range:NSMakeRange(0, [aboutText length])]; [aboutText replaceOccurrencesOfString:@"\\par" withString:@"\n" options:0 range:NSMakeRange(0, [aboutText length])]; - NSMutableString *retStr = [[@"" mutableCopy] autorelease]; + NSMutableString *retStr = [@"" mutableCopy]; for(NSUInteger i=0; i<[aboutText length]; i++) { unichar c = [aboutText characterAtIndex:i]; @@ -321,7 +258,7 @@ //we have an unicode character! @try { NSInteger unicodeChar = 0; - NSMutableString *unicodeCharString = [[@"" mutableCopy] autorelease]; + NSMutableString *unicodeCharString = [@"" mutableCopy]; int j = 0; BOOL negative = NO; if ([aboutText characterAtIndex:(i+2)] == '-') { @@ -336,7 +273,7 @@ unicodeChar = [unicodeCharString integerValue]; if (negative) unicodeChar = 65536 - unicodeChar; i += j+2; - [retStr appendFormat:@"%C", unicodeChar]; + [retStr appendFormat:@"%C", (unichar)unicodeChar]; } @catch (NSException * e) { [retStr appendFormat:@"%C", c]; @@ -354,7 +291,7 @@ } else { aboutText = [NSMutableString string]; } - [configEntries setObject:aboutText forKey:SWMOD_CONFENTRY_ABOUT]; + self.configEntries[SWMOD_CONFENTRY_ABOUT] = aboutText; } return aboutText; @@ -367,11 +304,11 @@ - (BOOL)isEditable { BOOL ret = NO; - NSString *editable = [configEntries objectForKey:SWMOD_CONFENTRY_EDITABLE]; + NSString *editable = self.configEntries[SWMOD_CONFENTRY_EDITABLE]; if(editable == nil) { editable = [self configFileEntryForConfigKey:SWMOD_CONFENTRY_EDITABLE]; if(editable != nil) { - [configEntries setObject:editable forKey:SWMOD_CONFENTRY_EDITABLE]; + self.configEntries[SWMOD_CONFENTRY_EDITABLE] = editable; } } @@ -386,11 +323,11 @@ - (BOOL)isRTL { BOOL ret = NO; - NSString *direction = [configEntries objectForKey:SWMOD_CONFENTRY_DIRECTION]; + NSString *direction = self.configEntries[SWMOD_CONFENTRY_DIRECTION]; if(direction == nil) { direction = [self configFileEntryForConfigKey:SWMOD_CONFENTRY_DIRECTION]; if(direction != nil) { - [configEntries setObject:direction forKey:SWMOD_CONFENTRY_DIRECTION]; + self.configEntries[SWMOD_CONFENTRY_DIRECTION] = direction; } } @@ -423,7 +360,7 @@ if(key != nil) { // check user defaults, that's where we store the entered keys NSDictionary *cipherKeys = [[NSUserDefaults standardUserDefaults] objectForKey:DefaultsModuleCipherKeysKey]; - if([key length] == 0 && [[cipherKeys allKeys] containsObject:[self name]] == NO) { + if([key length] == 0 && ![[cipherKeys allKeys] containsObject:[self name]]) { locked = YES; } } @@ -455,7 +392,7 @@ - (NSString *)configFileEntryForConfigKey:(NSString *)entryKey { NSString *result = nil; - [moduleLock lock]; + [self.moduleLock lock]; const char *entryStr = swModule->getConfigEntry([entryKey UTF8String]); if(entryStr) { result = [NSString stringWithUTF8String:entryStr]; @@ -463,29 +400,11 @@ result = [NSString stringWithCString:entryStr encoding:NSISOLatin1StringEncoding]; } } - [moduleLock unlock]; + [self.moduleLock unlock]; return result; } -#pragma mark - Module unlocking - -- (BOOL)unlock:(NSString *)unlockKey { - - if (![self isEncrypted]) { - return NO; - } - - NSMutableDictionary *cipherKeys = [NSMutableDictionary dictionaryWithDictionary: - [[NSUserDefaults standardUserDefaults] objectForKey:DefaultsModuleCipherKeysKey]]; - [cipherKeys setObject:unlockKey forKey:[self name]]; - [[NSUserDefaults standardUserDefaults] setObject:cipherKeys forKey:DefaultsModuleCipherKeysKey]; - - [swManager setCipherKey:unlockKey forModuleNamed:[self name]]; - - return YES; -} - #pragma mark - Module positioning - (void)incKeyPosition { @@ -501,11 +420,12 @@ } - (void)setSwordKey:(SwordKey *)aKey { + swModule->getKey()->setPersist(true); swModule->setKey([aKey swKey]); } - (SwordKey *)createKey { - sword::SWKey *sk = swModule->CreateKey(); + sword::SWKey *sk = swModule->createKey(); SwordKey *newKey = [SwordKey swordKeyWithSWKey:sk makeCopy:YES]; delete sk; @@ -529,25 +449,25 @@ - (id)attributeValueForParsedLinkData:(NSDictionary *)data withTextRenderType:(TextPullType)textType { id ret = nil; - NSString *passage = [data objectForKey:ATTRTYPE_PASSAGE]; + NSString *passage = data[ATTRTYPE_PASSAGE]; if(passage) { passage = [[passage stringByReplacingOccurrencesOfString:@"+" withString:@" "] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; } - NSString *attrType = [data objectForKey:ATTRTYPE_TYPE]; + NSString *attrType = data[ATTRTYPE_TYPE]; if([attrType isEqualToString:@"n"]) { NSString *footnoteText = [self entryAttributeValueFootnoteOfType:attrType - indexValue:[data objectForKey:ATTRTYPE_VALUE] + indexValue:data[ATTRTYPE_VALUE] forKey:[SwordKey swordKeyWithRef:passage]]; ret = footnoteText; } else if([attrType isEqualToString:@"x"] || [attrType isEqualToString:@"scriptRef"] || [attrType isEqualToString:@"scripRef"]) { NSString *key = @""; if([attrType isEqualToString:@"x"]) { key = [self entryAttributeValueFootnoteOfType:attrType - indexValue:[data objectForKey:ATTRTYPE_VALUE] + indexValue:data[ATTRTYPE_VALUE] forKey:[SwordKey swordKeyWithRef:passage]]; } else { - key = [[[data objectForKey:ATTRTYPE_VALUE] stringByReplacingOccurrencesOfString:@"+" - withString:@" "] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; + key = [[data[ATTRTYPE_VALUE] stringByReplacingOccurrencesOfString:@"+" + withString:@" "] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; } if(textType == TextTypeRendered) { ret = [self renderedTextEntriesForRef:key]; @@ -560,7 +480,7 @@ } - (void)setProcessEntryAttributes:(BOOL)flag { - swModule->processEntryAttributes(flag); + swModule->setProcessEntryAttributes(flag); } - (BOOL)processEntryAttributes { @@ -586,7 +506,9 @@ - (NSArray *)entryAttributeValuesLemma { NSMutableArray *array = [NSMutableArray array]; - // parse entry attributes and look for Lemma (String's numbers) + swModule->stripText(); // force processing of key, if it hasn't been done already + + // parse entry attributes and look for Lemma (Strong's numbers) sword::AttributeTypeList::iterator words; sword::AttributeList::iterator word; sword::AttributeValue::iterator strongVal; @@ -611,21 +533,28 @@ return [NSArray arrayWithArray:array]; } +- (NSArray *)entryAttributeValuesLemmaNormalized { + NSArray *lemmas = [self entryAttributeValuesLemma]; + // post process all codes and mormalize the number + // Hebrew keys should have 5 number digits + return [SwordUtil padStrongsNumbers:lemmas]; +} + - (NSString *)entryAttributeValuePreverseForKey:(SwordKey *)aKey { - [moduleLock lock]; + [self.moduleLock lock]; [self setSwordKey:aKey]; - swModule->RenderText(); // force processing of key + swModule->renderText(); // force processing of key NSString *value = [self entryAttributeValuePreverse]; - [moduleLock unlock]; + [self.moduleLock unlock]; return value; } - (NSString *)entryAttributeValueFootnoteOfType:(NSString *)fnType indexValue:(NSString *)index forKey:(SwordKey *)aKey { - [moduleLock lock]; + [self.moduleLock lock]; [self setSwordKey:aKey]; - swModule->RenderText(); // force processing of key + swModule->renderText(); // force processing of key NSString *value = [self entryAttributeValueFootnoteOfType:fnType indexValue:index]; - [moduleLock unlock]; + [self.moduleLock unlock]; return value; } @@ -638,36 +567,36 @@ - (NSString *)renderedText { NSString *ret = @""; - ret = [NSString stringWithUTF8String:swModule->RenderText()]; + ret = [NSString stringWithUTF8String:swModule->renderText()]; if(!ret) { - ret = [NSString stringWithCString:swModule->RenderText() encoding:NSISOLatin1StringEncoding]; + ret = [NSString stringWithCString:swModule->renderText() encoding:NSISOLatin1StringEncoding]; } return ret; } - (NSString *)renderedTextFromString:(NSString *)aString { NSString *ret = @""; - ret = [NSString stringWithUTF8String:swModule->RenderText([aString UTF8String])]; + ret = [NSString stringWithUTF8String:swModule->renderText([aString UTF8String])]; if(!ret) { - ret = [NSString stringWithCString:swModule->RenderText([aString UTF8String]) encoding:NSISOLatin1StringEncoding]; + ret = [NSString stringWithCString:swModule->renderText([aString UTF8String]) encoding:NSISOLatin1StringEncoding]; } return ret; } - (NSString *)strippedText { NSString *ret = @""; - ret = [NSString stringWithUTF8String:swModule->StripText()]; + ret = [NSString stringWithUTF8String:swModule->stripText()]; if(!ret) { - ret = [NSString stringWithCString:swModule->StripText() encoding:NSISOLatin1StringEncoding]; + ret = [NSString stringWithCString:swModule->stripText() encoding:NSISOLatin1StringEncoding]; } return ret; } - (NSString *)strippedTextFromString:(NSString *)aString { NSString *ret = @""; - ret = [NSString stringWithUTF8String:swModule->RenderText([aString UTF8String])]; + ret = [NSString stringWithUTF8String:swModule->renderText([aString UTF8String])]; if(!ret) { - ret = [NSString stringWithCString:swModule->RenderText([aString UTF8String]) encoding:NSISOLatin1StringEncoding]; + ret = [NSString stringWithCString:swModule->renderText([aString UTF8String]) encoding:NSISOLatin1StringEncoding]; } return ret; } @@ -680,11 +609,35 @@ return [self textEntriesForReference:reference textType:TextTypeRendered]; } +- (NSArray *)textEntriesForReference:(NSString *)aReference textType:(TextPullType)textType { + NSArray *ret = nil; + + SwordModuleTextEntry *entry = [self textEntryForKey:[SwordKey swordKeyWithRef:aReference] + textType:textType]; + if(entry) { + ret = @[entry]; + } + + return ret; +} + +- (SwordModuleTextEntry *)renderedTextEntryForRef:(NSString *)reference { + return [self textEntryForKeyString:reference textType:TextTypeRendered]; +} + +- (SwordModuleTextEntry *)strippedTextEntryForRef:(NSString *)reference { + return [self textEntryForKeyString:reference textType:TextTypeStripped]; +} + +- (SwordModuleTextEntry *)textEntryForKeyString:(NSString *)aKeyString textType:(TextPullType)aType { + return [self textEntryForKey:[SwordKey swordKeyWithRef:aKeyString] textType:aType]; +} + - (SwordModuleTextEntry *)textEntryForKey:(SwordKey *)aKey textType:(TextPullType)aType { SwordModuleTextEntry *ret = nil; if(aKey) { - [moduleLock lock]; + [self.moduleLock lock]; [self setSwordKey:aKey]; if(![self error]) { NSString *txt = @""; @@ -700,28 +653,12 @@ ALog(@"Nil key"); } } - [moduleLock unlock]; + [self.moduleLock unlock]; } return ret; } -- (SwordModuleTextEntry *)textEntryForKeyString:(NSString *)aKeyString textType:(TextPullType)aType { - return [self textEntryForKey:[SwordKey swordKeyWithRef:aKeyString] textType:aType]; -} - -- (NSArray *)textEntriesForReference:(NSString *)aReference textType:(TextPullType)textType { - NSArray *ret = nil; - - SwordModuleTextEntry *entry = [self textEntryForKey:[SwordKey swordKeyWithRef:aReference] - textType:textType]; - if(entry) { - ret = [NSArray arrayWithObject:entry]; - } - - return ret; -} - - (void)writeEntry:(SwordModuleTextEntry *)anEntry {} - (long)entryCount { diff --git a/bindings/objc/src/SwordModuleTextEntry.h b/bindings/objc/src/SwordModuleTextEntry.h index 7759be4..f9664fe 100644 --- a/bindings/objc/src/SwordModuleTextEntry.h +++ b/bindings/objc/src/SwordModuleTextEntry.h @@ -15,8 +15,8 @@ NSString *text; } -@property (readwrite, retain) NSString *key; -@property (readwrite, retain) NSString *text; +@property (readwrite, strong) NSString *key; +@property (readwrite, strong) NSString *text; + (id)textEntryForKey:(NSString *)aKey andText:(NSString *)aText; - (id)initWithKey:(NSString *)aKey andText:(NSString *)aText; diff --git a/bindings/objc/src/SwordModuleTextEntry.m b/bindings/objc/src/SwordModuleTextEntry.m index 3dec05d..0d5e3e0 100644 --- a/bindings/objc/src/SwordModuleTextEntry.m +++ b/bindings/objc/src/SwordModuleTextEntry.m @@ -15,7 +15,7 @@ @synthesize text; + (id)textEntryForKey:(NSString *)aKey andText:(NSString *)aText { - return [[[SwordModuleTextEntry alloc] initWithKey:aKey andText:aText] autorelease]; + return [[SwordModuleTextEntry alloc] initWithKey:aKey andText:aText]; } - (id)initWithKey:(NSString *)aKey andText:(NSString *)aText { @@ -28,15 +28,6 @@ return self; } -- (void)finalize { - [super finalize]; -} -- (void)dealloc { - [self setKey:nil]; - [self setText:nil]; - - [super dealloc]; -} @end diff --git a/bindings/objc/src/SwordModuleTreeEntry.h b/bindings/objc/src/SwordModuleTreeEntry.h index cd27cbe..7c75498 100644 --- a/bindings/objc/src/SwordModuleTreeEntry.h +++ b/bindings/objc/src/SwordModuleTreeEntry.h @@ -14,8 +14,8 @@ NSArray *content; } -@property (retain, readwrite) NSString *key; -@property (retain, readwrite) NSArray *content; +@property (strong, readwrite) NSString *key; +@property (strong, readwrite) NSArray *content; - (id)initWithKey:(NSString *)aKey content:(NSArray *)aContent; diff --git a/bindings/objc/src/SwordModuleTreeEntry.m b/bindings/objc/src/SwordModuleTreeEntry.m index fea0b16..9fbba84 100644 --- a/bindings/objc/src/SwordModuleTreeEntry.m +++ b/bindings/objc/src/SwordModuleTreeEntry.m @@ -24,15 +24,6 @@ return self; } -- (void)finalize { - [super finalize]; -} -- (void)dealloc { - [self setKey:nil]; - [self setContent:nil]; - - [super dealloc]; -} @end diff --git a/bindings/objc/src/SwordUtil.h b/bindings/objc/src/SwordUtil.h index 92779bf..512670c 100644 --- a/bindings/objc/src/SwordUtil.h +++ b/bindings/objc/src/SwordUtil.h @@ -17,4 +17,23 @@ */ + (NSDictionary *)dictionaryFromUrl:(NSURL *)aURL; +/** + Pads an array of unpadded Strong's numbers to 5 digits. + I.e. H0430 => H00430, G11 => G00011 + */ ++ (NSArray *)padStrongsNumbers:(NSArray *)unpaddedNumbers; + +/** + Pads a single unpadded Strong's numbers to 5 digits. + I.e. H0430 => H00430, G11 => G00011 + Since an unpadded number can be combined with multiple numbers concatenated by space we'll return an array here. + */ ++ (NSArray *)padStrongsNumber:(NSString *)unpaddedNumber; + +/** + * Pads a string on the left until 5 digits is reached + */ ++ (NSString *)leftPadStrongsFormat:(NSString *)unpadded; + + @end diff --git a/bindings/objc/src/SwordUtil.m b/bindings/objc/src/SwordUtil.m index 9f53870..abf11b2 100644 --- a/bindings/objc/src/SwordUtil.m +++ b/bindings/objc/src/SwordUtil.m @@ -17,12 +17,11 @@ NSString *scheme = [aURL scheme]; if([scheme isEqualToString:@"sword"]) { // in this case host is the module and path the reference - [ret setObject:[aURL host] forKey:ATTRTYPE_MODULE]; - [ret setObject:[[[aURL path] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding] - stringByReplacingOccurrencesOfString:@"/" withString:@""] - forKey:ATTRTYPE_VALUE]; - [ret setObject:@"scriptRef" forKey:ATTRTYPE_TYPE]; - [ret setObject:@"showRef" forKey:ATTRTYPE_ACTION]; + ret[ATTRTYPE_MODULE] = [aURL host]; + ret[ATTRTYPE_VALUE] = [[[aURL path] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding] + stringByReplacingOccurrencesOfString:@"/" withString:@""]; + ret[ATTRTYPE_TYPE] = @"scriptRef"; + ret[ATTRTYPE_ACTION] = @"showRef"; } else if([scheme isEqualToString:@"applewebdata"]) { // in this case NSString *path = [aURL path]; @@ -36,29 +35,73 @@ NSString *action = @""; for(NSString *entry in data) { if([entry hasPrefix:@"type="]) { - type = [[entry componentsSeparatedByString:@"="] objectAtIndex:1]; + type = [entry componentsSeparatedByString:@"="][1]; } else if([entry hasPrefix:@"module="]) { - module = [[entry componentsSeparatedByString:@"="] objectAtIndex:1]; + module = [entry componentsSeparatedByString:@"="][1]; } else if([entry hasPrefix:@"passage="]) { - passage = [[entry componentsSeparatedByString:@"="] objectAtIndex:1]; + passage = [entry componentsSeparatedByString:@"="][1]; } else if([entry hasPrefix:@"action="]) { - action = [[entry componentsSeparatedByString:@"="] objectAtIndex:1]; + action = [entry componentsSeparatedByString:@"="][1]; } else if([entry hasPrefix:@"value="]) { - value = [[entry componentsSeparatedByString:@"="] objectAtIndex:1]; + value = [entry componentsSeparatedByString:@"="][1]; } else { ALog(@"Unknown parameter: %@", entry); } } - [ret setObject:[module stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding] forKey:ATTRTYPE_MODULE]; - [ret setObject:[passage stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding] forKey:ATTRTYPE_PASSAGE]; - [ret setObject:[value stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding] forKey:ATTRTYPE_VALUE]; - [ret setObject:[action stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding] forKey:ATTRTYPE_ACTION]; - [ret setObject:[type stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding] forKey:ATTRTYPE_TYPE]; + ret[ATTRTYPE_MODULE] = [module stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; + ret[ATTRTYPE_PASSAGE] = [passage stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; + ret[ATTRTYPE_VALUE] = [value stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; + ret[ATTRTYPE_ACTION] = [action stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; + ret[ATTRTYPE_TYPE] = [type stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; } } return ret; } ++ (NSArray *)padStrongsNumbers:(NSArray *)unpaddedNumbers { + NSMutableArray *buf = [NSMutableArray array]; + for(NSString *lemma in unpaddedNumbers) { + [buf addObjectsFromArray:[self padStrongsNumber:lemma]]; + } + return [NSArray arrayWithArray:buf]; +} + ++ (NSArray *)padStrongsNumber:(NSString *)unpaddedNumber { + NSMutableArray *buf = [NSMutableArray array]; + // Hebrew + NSString *prefix = nil; + if([unpaddedNumber hasPrefix:@"H"]) { + prefix = @"H"; + } + if([unpaddedNumber hasPrefix:@"G"]) { + prefix = @"G"; + } + + if(prefix != nil) { + // lemma may contain more codes concatenated by space + NSArray *keys = [unpaddedNumber componentsSeparatedByString:@" "]; + for(__strong NSString *key in keys) { + // trim + key = [key stringByReplacingOccurrencesOfString:@" " withString:@""]; + NSArray *keyComps = [key componentsSeparatedByString:prefix]; + if(keyComps.count == 2) { + NSString *keyValue = [self leftPadStrongsFormat:keyComps[1]]; + // add to result array + [buf addObject:[NSString stringWithFormat:@"%@%@", prefix, keyValue]]; + } + } + } else { + [buf addObject:unpaddedNumber]; + } + + return [NSArray arrayWithArray:buf]; +} + ++ (NSString *)leftPadStrongsFormat:(NSString *)unpadded { + int number = [unpadded intValue]; + return [NSString stringWithFormat:@"%005i", number]; +} + @end
\ No newline at end of file diff --git a/bindings/objc/src/SwordVerseKey.h b/bindings/objc/src/SwordVerseKey.h index a72911b..a3235f3 100644 --- a/bindings/objc/src/SwordVerseKey.h +++ b/bindings/objc/src/SwordVerseKey.h @@ -33,7 +33,7 @@ - (SwordVerseKey *)initWithRef:(NSString *)aRef; - (SwordVerseKey *)initWithRef:(NSString *)aRef v11n:(NSString *)scheme; -- (int)index; +- (long)index; - (int)testament; - (void)setTestament:(char)val; - (int)book; @@ -41,7 +41,7 @@ - (int)chapter; - (void)setChapter:(int)val; - (int)verse; -- (void)setVerse:(int)val; +- (void)setVerse:(long)val; - (BOOL)headings; - (void)setHeadings:(BOOL)flag; - (BOOL)autoNormalize; diff --git a/bindings/objc/src/SwordVerseKey.mm b/bindings/objc/src/SwordVerseKey.mm index ca15f17..c745a9b 100644 --- a/bindings/objc/src/SwordVerseKey.mm +++ b/bindings/objc/src/SwordVerseKey.mm @@ -12,27 +12,27 @@ @implementation SwordVerseKey + (SwordVerseKey *)verseKey { - return [[[SwordVerseKey alloc] init] autorelease]; + return [[SwordVerseKey alloc] init]; } + (SwordVerseKey *)verseKeyWithVersification:(NSString *)scheme { - return [[[SwordVerseKey alloc] initWithVersification:scheme] autorelease]; + return [[SwordVerseKey alloc] initWithVersification:scheme]; } + (SwordVerseKey *)verseKeyWithRef:(NSString *)aRef { - return [[[SwordVerseKey alloc] initWithRef:aRef] autorelease]; + return [[SwordVerseKey alloc] initWithRef:aRef]; } + (SwordVerseKey *)verseKeyWithRef:(NSString *)aRef v11n:(NSString *)scheme { - return [[[SwordVerseKey alloc] initWithRef:aRef v11n:scheme] autorelease]; + return [[SwordVerseKey alloc] initWithRef:aRef v11n:scheme]; } + (SwordVerseKey *)verseKeyWithSWVerseKey:(sword::VerseKey *)aVk { - return [[[SwordVerseKey alloc] initWithSWVerseKey:aVk] autorelease]; + return [[SwordVerseKey alloc] initWithSWVerseKey:aVk]; } + (SwordVerseKey *)verseKeyWithSWVerseKey:(sword::VerseKey *)aVk makeCopy:(BOOL)copy { - return [[[SwordVerseKey alloc] initWithSWVerseKey:aVk makeCopy:copy] autorelease]; + return [[SwordVerseKey alloc] initWithSWVerseKey:aVk makeCopy:copy]; } - (id)init { @@ -60,48 +60,46 @@ } - (SwordVerseKey *)initWithRef:(NSString *)aRef v11n:(NSString *)scheme { - sword::VerseKey *vk = new sword::VerseKey(); - self = [super initWithSWKey:vk]; + sword::VerseKey vk; + self = [super initWithSWKey:&vk makeCopy:YES]; if(self) { created = YES; if(scheme) { [self setVersification:scheme]; } - + if(aRef) { [self setKeyText:aRef]; - } - } + } + } return self; } -- (void)finalize { - [super finalize]; -} -- (void)dealloc { - [super dealloc]; -} - (SwordKey *)clone { return [SwordVerseKey verseKeyWithSWVerseKey:(sword::VerseKey *)sk]; } +- (long)index { + return ((sword::VerseKey *)sk)->getIndex(); +} + - (BOOL)headings { - return (BOOL)((sword::VerseKey *)sk)->Headings(); + return (BOOL)((sword::VerseKey *)sk)->isIntros(); } - (void)setHeadings:(BOOL)flag { - ((sword::VerseKey *)sk)->Headings(flag); + ((sword::VerseKey *)sk)->setIntros(flag); } - (BOOL)autoNormalize { - return (BOOL)((sword::VerseKey *)sk)->AutoNormalize(); + return (BOOL)((sword::VerseKey *)sk)->isAutoNormalize(); } - (void)setAutoNormalize:(BOOL)flag { - ((sword::VerseKey *)sk)->AutoNormalize(flag); + ((sword::VerseKey *)sk)->setAutoNormalize(flag); } - (int)testament { @@ -132,8 +130,8 @@ ((sword::VerseKey *)sk)->setChapter(val); } -- (void)setVerse:(int)val { - ((sword::VerseKey *)sk)->setVerse(val); +- (void)setVerse:(long)val { + ((sword::VerseKey *)sk)->setVerse((int)val); } - (NSString *)bookName { diff --git a/bindings/objc/src/SwordVerseManager.h b/bindings/objc/src/SwordVerseManager.h index c9667c5..776d672 100644 --- a/bindings/objc/src/SwordVerseManager.h +++ b/bindings/objc/src/SwordVerseManager.h @@ -10,7 +10,6 @@ #ifdef __cplusplus #include <versificationmgr.h> -class sword::VersificationMgr::Book; #endif #define SW_VERSIFICATION_KJV @"KJV" diff --git a/bindings/objc/src/SwordVerseManager.mm b/bindings/objc/src/SwordVerseManager.mm index 72f26fc..9c661dd 100644 --- a/bindings/objc/src/SwordVerseManager.mm +++ b/bindings/objc/src/SwordVerseManager.mm @@ -12,7 +12,7 @@ @interface SwordVerseManager () -@property (retain, readwrite) NSMutableDictionary *booksPerVersification; +@property (strong, readwrite) NSMutableDictionary *booksPerVersification; @end @@ -39,15 +39,7 @@ return self; } -- (void)finalize { - [super finalize]; -} -- (void)dealloc { - [self setBooksPerVersification:nil]; - - [super dealloc]; -} /** convenience method that returns the books for default scheme (KJV) */ - (NSArray *)books { @@ -56,7 +48,7 @@ /** books for a versification scheme */ - (NSArray *)booksForVersification:(NSString *)verseScheme { - NSArray *ret = [booksPerVersification objectForKey:verseScheme]; + NSArray *ret = booksPerVersification[verseScheme]; if(ret == nil) { // hasn't been initialized yet const sword::VersificationMgr::System *system = verseMgr->getVersificationSystem([verseScheme UTF8String]); @@ -65,13 +57,13 @@ for(int i = 0;i < bookCount;i++) { sword::VersificationMgr::Book *book = (sword::VersificationMgr::Book *)system->getBook(i); - SwordBibleBook *bb = [[[SwordBibleBook alloc] initWithBook:book] autorelease]; + SwordBibleBook *bb = [[SwordBibleBook alloc] initWithBook:book]; [bb setNumber:i+1]; // VerseKey-Book() starts at index 1 // add to array [buf addObject:bb]; } - [booksPerVersification setObject:buf forKey:verseScheme]; + booksPerVersification[verseScheme] = buf; ret = buf; } diff --git a/bindings/objc/src/VerseEnumerator.mm b/bindings/objc/src/VerseEnumerator.mm index cd5c665..e7fc44d 100644 --- a/bindings/objc/src/VerseEnumerator.mm +++ b/bindings/objc/src/VerseEnumerator.mm @@ -10,7 +10,7 @@ #import "SwordListKey.h" @interface VerseEnumerator () -@property (retain, readwrite) SwordListKey *listKey; +@property (strong, readwrite) SwordListKey *listKey; @end @implementation VerseEnumerator @@ -26,18 +26,11 @@ return self; } -- (void)finalize { - [super finalize]; -} -- (void)dealloc { - [listKey release]; - [super dealloc]; -} - (NSArray *)allObjects { NSMutableArray *t = [NSMutableArray array]; - for(*[listKey swListKey] = sword::TOP;![listKey swListKey]->Error(); *[listKey swListKey] += 1) { + for(*[listKey swListKey] = sword::TOP;![listKey swListKey]->popError(); *[listKey swListKey] += 1) { [t addObject:[listKey keyText]]; } // position TOP again @@ -48,7 +41,7 @@ - (NSString *)nextObject { NSString *ret = nil; - if(![listKey swListKey]->Error()) { + if(![listKey swListKey]->popError()) { ret = [listKey keyText]; *[listKey swListKey] += 1; } diff --git a/bindings/objc/src/services/Configuration.m b/bindings/objc/src/services/Configuration.m index 67ec709..b48b1e7 100644 --- a/bindings/objc/src/services/Configuration.m +++ b/bindings/objc/src/services/Configuration.m @@ -27,19 +27,10 @@ return [super init]; } -- (void)finalize { - [super finalize]; -} - -- (void)dealloc { - [impl release]; - [super dealloc]; -} - (void)setImpl:(id<Configuration>)configImpl { impl = (Configuration *)configImpl; - [impl retain]; } #pragma mark Configuration implementation diff --git a/bindings/objc/src/services/OSXConfiguration.m b/bindings/objc/src/services/OSXConfiguration.m index 7d53987..3cfcf07 100644 --- a/bindings/objc/src/services/OSXConfiguration.m +++ b/bindings/objc/src/services/OSXConfiguration.m @@ -20,19 +20,43 @@ } - (NSString *)defaultModulePath { - return [@"~/Library/Application Support/Sword" stringByExpandingTildeInPath]; + NSArray *urls = [[NSFileManager defaultManager] URLsForDirectory:NSApplicationSupportDirectory inDomains:NSUserDomainMask]; + if(urls.count > 0) { + return [[(NSURL *)urls[0] path] stringByAppendingPathComponent:@"Sword"]; + } + return nil; } - (NSString *)defaultAppSupportPath { - return [@"~/Library/Application Support/MacSword" stringByExpandingTildeInPath]; + NSArray *urls = [[NSFileManager defaultManager] URLsForDirectory:NSApplicationSupportDirectory inDomains:NSUserDomainMask]; + if(urls.count > 0) { + NSString *folder = [[(NSURL *)urls[0] path] stringByAppendingPathComponent:@"ObjCSword"]; + if(![[NSFileManager defaultManager] fileExistsAtPath:folder]) { + [[NSFileManager defaultManager] createDirectoryAtPath:folder withIntermediateDirectories:NO attributes:nil error:NULL]; + } + return folder; + } + return nil; } - (NSString *)tempFolder { - return [@"~/Library/Caches/ObjCSword" stringByExpandingTildeInPath]; + NSArray *urls = [[NSFileManager defaultManager] URLsForDirectory:NSCachesDirectory inDomains:NSUserDomainMask]; + if(urls.count > 0) { + NSString *folder = [[(NSURL *)urls[0] path] stringByAppendingPathComponent:@"ObjCSword"]; + if(![[NSFileManager defaultManager] fileExistsAtPath:folder]) { + [[NSFileManager defaultManager] createDirectoryAtPath:folder withIntermediateDirectories:NO attributes:nil error:NULL]; + } + return folder; + } + return nil; } - (NSString *)logFile { - return [@"~/Library/Logs/ObjCSword.log" stringByExpandingTildeInPath]; + NSArray *urls = [[NSFileManager defaultManager] URLsForDirectory:NSLibraryDirectory inDomains:NSUserDomainMask]; + if(urls.count > 0) { + return [[[(NSURL *)urls[0] path] stringByAppendingPathComponent:@"Logs"] stringByAppendingPathComponent:@"ObjCSword.log"]; + } + return nil; } @end |