/
XeeImageSource.m
299 lines (223 loc) · 6.36 KB
/
XeeImageSource.m
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#import "XeeImageSource.h"
#import <pthread.h>
NSString *XeeErrorDomain=@"XeeErrorDomain";
@implementation XeeImageSource
-(id)init
{
if(self=[super init])
{
delegate=nil;
icon=nil;
sortorder=[[NSUserDefaults standardUserDefaults] integerForKey:@"defaultSortingOrder"];
actionsblocked=NO;
pendingimage=nil;
pendinglistchange=NO;
rand_ordering=NULL;
rand_size=0;
}
return self;
}
-(void)dealloc
{
free(rand_ordering);
[icon release];
[pendingimage release];
[super dealloc];
}
-(void)start {}
-(void)stop {}
-(id)delegate { return delegate; }
-(void)setDelegate:(id)newdelegate { delegate=newdelegate; }
-(NSImage *)icon { return icon; }
-(void)setIcon:(NSImage *)newicon
{
if(icon==newicon) return;
[icon release];
icon=[newicon retain];
}
-(int)numberOfImages { return 0; }
-(int)indexOfCurrentImage { return 0; }
-(NSString *)representedFilename { return nil; }
-(NSString *)descriptiveNameOfCurrentImage { return nil; }
-(NSString *)filenameOfCurrentImage { return nil; }
-(uint64_t)sizeOfCurrentImage { return 0; }
-(NSDate *)dateOfCurrentImage { return nil; }
-(BOOL)isCurrentImageRemote { return NO; }
-(BOOL)isCurrentImageAtPath:(NSString *)path { return NO; }
-(BOOL)canBrowse { return NO; }
-(BOOL)canSort { return NO; }
-(BOOL)canRenameCurrentImage { return NO; }
-(BOOL)canDeleteCurrentImage { return NO; }
-(BOOL)canCopyCurrentImage { return NO; }
-(BOOL)canMoveCurrentImage { return NO; }
-(BOOL)canOpenCurrentImage { return NO; }
-(BOOL)canSaveCurrentImage { return NO; }
-(int)sortOrder { return sortorder==XeeDefaultSortOrder?XeeNameSortOrder:sortorder; }
-(void)setSortOrder:(int)order { sortorder=order; }
-(void)setActionsBlocked:(BOOL)blocked
{
if(actionsblocked&&!blocked)
{
actionsblocked=NO;
if(pendingimage)
{
[self triggerImageChangeAction:pendingimage];
[pendingimage release];
pendingimage=nil;
}
if(pendinglistchange)
{
[self triggerImageListChangeAction];
pendinglistchange=NO;
}
}
else actionsblocked=blocked;
}
-(void)pickImageAtIndex:(int)index next:(int)next { }
-(void)pickImageAtIndex:(int)index
{
int curr=[self indexOfCurrentImage];
int count=[self numberOfImages];
int next;
if(index==count-1) next=count-2;
else if(index==0) next=1;
else if(index<curr) next=index-1;
else next=index+1;
[self pickImageAtIndex:index next:next];
}
-(void)skip:(int)offset
{
int curr=[self indexOfCurrentImage];
int count=[self numberOfImages];
if(curr==NSNotFound)
{
if(offset>=0) [self pickFirstImage];
else [self pickLastImage];
}
else
{
int newpos=curr+offset;
if([[NSUserDefaults standardUserDefaults] boolForKey:@"wrapImageBrowsing"])
{
newpos=(newpos%count+count)%count; // this looks horrible
}
else
{
if(newpos<0) newpos=0;
if(newpos>=count) newpos=count-1;
}
if(newpos!=curr) [self pickImageAtIndex:newpos];
}
}
-(void)pickFirstImage
{
[self pickImageAtIndex:0];
}
-(void)pickLastImage
{
[self pickImageAtIndex:[self numberOfImages]-1];
}
-(void)pickNextImageAtRandom
{
int index=[self indexOfCurrentImage];
if(index==NSNotFound)
{
int count=[self numberOfImages];
if(count) [self pickImageAtIndex:random()%count];
}
else
{
[self updateRandomList];
if(rand_ordering) [self pickImageAtIndex:rand_ordering[index].next
next:rand_ordering[rand_ordering[index].next].next];
}
}
-(void)pickPreviousImageAtRandom
{
int index=[self indexOfCurrentImage];
if(index==NSNotFound)
{
int count=[self numberOfImages];
if(count) [self pickImageAtIndex:random()%count];
}
else
{
[self updateRandomList];
if(rand_ordering) [self pickImageAtIndex:rand_ordering[index].prev
next:rand_ordering[rand_ordering[index].prev].prev];
}
}
-(void)pickCurrentImage
{
[self pickImageAtIndex:[self indexOfCurrentImage]];
}
-(NSError *)renameCurrentImageTo:(NSString *)newname { return [NSError errorWithDomain:XeeErrorDomain code:XeeNotSupportedError userInfo:nil]; }
-(NSError *)deleteCurrentImage { return [NSError errorWithDomain:XeeErrorDomain code:XeeNotSupportedError userInfo:nil]; }
-(NSError *)copyCurrentImageTo:(NSString *)destination { return [NSError errorWithDomain:XeeErrorDomain code:XeeNotSupportedError userInfo:nil]; }
-(NSError *)moveCurrentImageTo:(NSString *)destination { return [NSError errorWithDomain:XeeErrorDomain code:XeeNotSupportedError userInfo:nil]; }
-(NSError *)openCurrentImageInApp:(NSString *)app { return [NSError errorWithDomain:XeeErrorDomain code:XeeNotSupportedError userInfo:nil]; }
-(void)beginSavingImage:(XeeImage *)image {}
-(void)endSavingImage:(XeeImage *)image {}
-(void)updateRandomList
{
int length=[self numberOfImages];
if(rand_ordering&&length==rand_size) return;
free(rand_ordering);
srandom(time(NULL));
int *order=malloc(sizeof(int)*length);
rand_ordering=malloc(sizeof(struct rand_entry)*length);
if(!rand_ordering) return;
rand_size=length;
for(int i=0;i<length;i++) order[i]=i;
for(int i=length-1;i>0;i--)
{
int randindex=random()%i;
int tmp=order[i];
order[i]=order[randindex];
order[randindex]=tmp;
}
for(int i=0;i<length;i++)
{
rand_ordering[order[i]].next=order[(i+1)%length];
rand_ordering[order[i]].prev=order[(i+length-1)%length];
}
free(order);
}
-(void)triggerImageChangeAction:(XeeImage *)image
{
if(actionsblocked)
{
if(image!=pendingimage)
{
[pendingimage release];
pendingimage=[image retain];
}
}
else
{
[delegate xeeImageSource:self imageDidChange:image];
/* if(pthread_main_np()) [delegate xeeImageSource:self imageDidChange:image];
else
{
NSInvocation *invocation=[NSInvocation invocationWithMethodSignature:[delegate methodSignatureForSelector:@selector(XeeImageSource:imageDidChange:)]];
[invocation setArgument:<#(void *)argumentLocation#> atIndex:<#(int)index#>
}
else [delegate performSelectorOnMainThread:@selector(xeeImagePropertiesDidChange:) withObject:self waitUntilDone:NO];
*/
}
}
-(void)triggerImageListChangeAction
{
if(actionsblocked) pendinglistchange=YES;
else [delegate xeeImageSource:self imageListDidChange:[self numberOfImages]];
}
-(NSString *)demandPassword
{
return [delegate xeeImageSourceDemandsPassword:self];
}
@end
@implementation NSObject (XeeImageSourceDelegate)
-(void)xeeImageSource:(XeeImageSource *)source imageListDidChange:(int)num {}
-(void)xeeImageSource:(XeeImageSource *)source imageDidChange:(XeeImage *)newimage {}
-(NSString *)xeeImageSourceDemandsPassword:(XeeImageSource *)source { return nil; }
@end