summaryrefslogtreecommitdiff
path: root/bindings/objc/src/SwordBook.mm
blob: 05f622e97c77e4b3bb35ee8f3749db37625cbe60 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*	SwordBook.mm - Sword API wrapper for GenBooks.

    Copyright 2008 Manfred Bergmann
    Based on code by Will Thimbleby

	This program is free software; you can redistribute it and/or modify it under the terms of the
	GNU General Public License as published by the Free Software Foundation version 2.

	This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
	even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
	General Public License for more details. (http://www.gnu.org/licenses/gpl.html)
*/

#import "SwordBook.h"
#import "SwordModule.h"
#import "SwordModuleTreeEntry.h"

@interface SwordBook ()

- (SwordModuleTreeEntry *)_treeEntryForKey:(sword::TreeKeyIdx *)treeKey;

@end

@implementation SwordBook

@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];
    if(self) {
        [self setContents:[NSMutableDictionary dictionary]];
    }
    
    return self;
}

- (void)finalize {
    [super finalize];
}

- (void)dealloc {
    [self setContents:nil];
    
    [super dealloc];
}

- (SwordModuleTreeEntry *)treeEntryForKey:(NSString *)treeKey {
    SwordModuleTreeEntry * ret = nil;
    
    [moduleLock lock];
    if(treeKey == nil) {
        ret = [contents objectForKey:@"root"];
        if(ret == nil) {
            sword::TreeKeyIdx *treeKey = dynamic_cast<sword::TreeKeyIdx*>((sword::SWKey *)*(swModule));
            ret = [self _treeEntryForKey:treeKey];
            // add to content
            [contents setObject:ret forKey:@"root"];
        }
    } else {
        ret = [contents objectForKey:treeKey];
        if(ret == nil) {
            const char *keyStr = [treeKey UTF8String];
            if(![self isUnicode]) {
                keyStr = [treeKey cStringUsingEncoding:NSISOLatin1StringEncoding];
            }
            // position module
            sword::SWKey *mkey = new sword::SWKey(keyStr);
            swModule->setKey(mkey);
            sword::TreeKeyIdx *key = dynamic_cast<sword::TreeKeyIdx*>((sword::SWKey *)*(swModule));
            ret = [self _treeEntryForKey:key];
            // add to content
            [contents setObject:ret forKey:treeKey];
        }
    }
    [moduleLock unlock];
    
    return ret;
}

- (SwordModuleTreeEntry *)_treeEntryForKey:(sword::TreeKeyIdx *)treeKey {
    SwordModuleTreeEntry *ret = [[SwordModuleTreeEntry alloc] init];    
    
	char *treeNodeName = (char *)treeKey->getText();
	NSString *nname = @"";
    
    if(strlen(treeNodeName) == 0) {
        nname = @"root";
    } else {    
        // key encoding depends on module encoding
        nname = [NSString stringWithUTF8String:treeNodeName];
        if(!nname) {
            nname = [NSString stringWithCString:treeNodeName encoding:NSISOLatin1StringEncoding];
        }
    }
    // set name
    [ret setKey:nname];
    NSMutableArray *c = [NSMutableArray array];
    [ret setContent:c];
	
    // if this node has children, walk them
	if(treeKey->hasChildren()) {
        // get first child
		treeKey->firstChild();
        do {
            NSString *subname = @"";
            // key encoding depends on module encoding
            const char *textStr = treeKey->getText();
            subname = [NSString stringWithUTF8String:textStr];
            if(!subname) {
                subname = [NSString stringWithCString:textStr encoding:NSISOLatin1StringEncoding];
            }
            if(subname) {
                [c addObject:subname];            
            }
        }
        while(treeKey->nextSibling());            
	}
	
	return ret;
}

- (void)testLoop {
    SwordModuleTreeEntry *entry = [self treeEntryForKey:nil];
    if([[entry content] count] > 0) {
        for(NSString *subkey in [entry content]) {
            entry = [self treeEntryForKey:subkey];
            if([[entry content] count] > 0) {
            } else {
                DLog(@"Entry: %@", [entry key]);
            }    
        }
    } else {
        DLog(@"Entry: %@", [entry key]);
    }    
}

#pragma mark - SwordModuleAccess

- (long)entryCount {
    // TODO: set value according to maximum entries here
    return 1000;
}

@end