summaryrefslogtreecommitdiff
path: root/bindings/objc/src/SwordInstallSource.mm
blob: 5c2c9aaf530f3dee4261e3a27743fe9a10ddb420 (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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
//
//  SwordInstallSource.mm
//  Eloquent
//
//  Created by Manfred Bergmann on 13.08.07.
//  Copyright 2007 __MyCompanyName__. All rights reserved.
//

#import "SwordInstallSource.h"
#import "SwordInstallSourceController.h"
#import "SwordManager.h"

@interface SwordInstallSource (PrivateAPI)

- (void)setSwordManager:(SwordManager *)swManager;

@end

@implementation SwordInstallSource (PrivateAPI)

- (void)setSwordManager:(SwordManager *)swManager {
    [swManager retain];
    [swordManager release];
    swordManager = swManager;
}

@end

@implementation SwordInstallSource

// init
- (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!");
        }
    }
    
    return self;
}

- (id)initWithType:(NSString *)aType {
    self = [self init];
    if(self) {
        // set type
        swInstallSource->type = [aType cStringUsingEncoding:NSUTF8StringEncoding];
    }
    
    return self;
}

/** init with given source */
- (id)initWithSource:(sword::InstallSource *)is {
    self = [super init];
    if(self) {
        temporarySource = YES;
        
        // at first we have no sword manager
        [self setSwordManager:nil];
        
        swInstallSource = is;
    }
    
    return self;
}

- (void)finalize {
    if(temporarySource == NO) {
        //LogL(LOG_DEBUG, @"[SwordInstallSource -finalize] deleting swInstalSource");
        //delete swInstallSource;
    }
    
    [super finalize];
}

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

// accessors
- (NSString *)caption {
    const char *str = swInstallSource->caption;
    return [[[NSString alloc] initWithCString:str encoding:NSUTF8StringEncoding] autorelease];
}

- (void)setCaption:(NSString *)aCaption {
    swInstallSource->caption = [aCaption cStringUsingEncoding:NSUTF8StringEncoding];
}

- (NSString *)type {
    const char *str = swInstallSource->type;
    return [[[NSString alloc] initWithCString:str encoding:NSUTF8StringEncoding] autorelease];
}

- (void)setType:(NSString *)aType {
    swInstallSource->type = [aType cStringUsingEncoding:NSUTF8StringEncoding];
}

- (NSString *)source {
    const char *str = swInstallSource->source;
    return [[[NSString alloc] initWithCString:str encoding:NSUTF8StringEncoding] autorelease];
}

- (void)setSource:(NSString *)aSource {
    swInstallSource->source = [aSource cStringUsingEncoding:NSUTF8StringEncoding];
}

- (NSString *)directory {
    const char *str = swInstallSource->directory;
    return [[[NSString alloc] initWithCString:str encoding:NSUTF8StringEncoding] autorelease];
}

- (void)setDirectory:(NSString *)aDir {
    swInstallSource->directory = [aDir cStringUsingEncoding:NSUTF8StringEncoding];
}

- (BOOL)isLocalSource {
    return [[self source] isEqualToString:@"localhost"];
}

// get config entry
- (NSString *)configEntry {
    return [NSString stringWithFormat:@"%@|%@|%@", [self caption], [self source], [self directory]];
}

/** install module */
- (void)installModuleWithName:(NSString *)mName usingManager:(SwordManager *)swManager withInstallController:(SwordInstallSourceController *)sim {
    sword::InstallMgr *im = [sim installMgr];
    im->installModule([swManager swManager], 0, [mName UTF8String], swInstallSource);
}

/** list all modules of this source */
- (NSArray *)listModules {
    NSArray *ret = nil;
    
    DLog(@"");    
    
    SwordManager *sm = [self swordManager];
    if(sm) {
        ret = [sm listModules];
    } else {
        ALog(@"Have nil SwordManager");        
    }
    
    return ret;
}

/** list module types */
- (NSArray *)listModuleTypes {
    NSArray *ret = nil;
    
    DLog(@"");
    ret = [SwordManager moduleTypes];
    
    return ret;    
}

// get associated SwordManager
- (SwordManager *)swordManager {

    if(swordManager == nil) {
        // create SwordManager from the SWMgr of this source
        sword::SWMgr *mgr;
        if([[self source] isEqualToString:@"localhost"]) {
            // create SwordManager from new SWMgr of path
            mgr = (sword::SWMgr *)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];    
        }
    }
    
    return swordManager;
}

/** low level API */
- (sword::InstallSource *)installSource {
    return swInstallSource;
}

@end