public
Description: A Mac OS X photo uploader program for my photo album.
Homepage: http://bleu.west.spy.net/~dustin/projects/photoupload/
Clone URL: git://github.com/dustin/photoupload.git
photoupload / DumpMatrix.m
100644 189 lines (163 sloc) 4.879 kb
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
// arch-tag: 46D2A9CC-9196-11D8-A9D9-000A957659CC
 
#import "DumpMatrix.h"
 
@implementation DumpMatrix
 
- (id)init
{
    [super init];
    _storage=[[NSMutableDictionary alloc] init];
 
    return(self);
}
 
-(void)dealloc
{
    [_storage release];
    [super dealloc];
}
 
- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
{
    NSDragOperation sourceDragMask = [sender draggingSourceOperationMask];
    if (sourceDragMask & NSDragOperationLink) {
        return NSDragOperationLink;
    } else if (sourceDragMask & NSDragOperationCopy) {
        return NSDragOperationCopy;
    }
    return NSDragOperationNone;
}
 
-(BOOL)isImage: (NSString *)file
{
    BOOL rv=false;
    NSString *ext=[[file pathExtension] lowercaseString];
 
    rv|=[ext isEqualToString: @"jpg"];
    rv|=[ext isEqualToString: @"jpeg"];
    rv|=[ext isEqualToString: @"gif"];
    rv|=[ext isEqualToString: @"png"];
    
    return(rv);
}
 
-(void)clear
{
    [_storage release];
    _storage=[[NSMutableDictionary alloc] init];
    [self update];
}
 
-(NSArray *)files
{
    return([_storage allKeys]);
}
 
-(void)update
{
    int colcount=[_storage count];
    // If there's not at least one thing to show, show three and disable.
    if(colcount < 1) {
        colcount=3;
        [self setEnabled: FALSE];
    }
    while([self numberOfColumns] > colcount) {
        [self removeColumn:0];
    }
    while([self numberOfColumns] < colcount) {
        [self addColumn];
    }
    while([self numberOfRows]>0) {
        [self removeRow: 0];
    }
    // Create some button cells
    NSMutableArray *cells=[[NSMutableArray alloc] initWithCapacity: [_storage count]];
    NSEnumerator *e=[_storage objectEnumerator];
    id object=nil;
    while(object = [e nextObject]) {
        [cells addObject: object];
    }
    // NSLog(@"%d columns, %d images\n", [self numberOfColumns], [_storage count]);
    [self addRowWithCells: cells];
    [self sizeToCells];
    [self setMode: NSListModeMatrix];
    [self setAllowsEmptySelection: TRUE];
    [self deselectAllCells];
    [cells release];
}
 
-(void)addFile:(NSString *)filename
{
    NSImage *img=[[NSImage alloc] initByReferencingFile: filename];
    [img setName: filename];
    [img setScalesWhenResized: YES];
    SizeScaler *sc=[[SizeScaler alloc] initWithSize: [img size]];
    [img setSize: [sc scaleTo: [self cellSize]]];
    NSButtonCell *cell=[[NSButtonCell alloc] init];
    [cell setImage: img];
    // Get rid of the current cache
    [img recache];
 
    [_storage setObject: cell forKey:filename];
 
    [self setEnabled: TRUE];
 
    [cell release];
    [sc release];
    [img release];
}
 
-(void)removeFile:(NSString *)filename
{
    [_storage removeObjectForKey: filename];
}
 
-(void)removeSelected
{
    NSArray *a=[self selectedCells];
    NSEnumerator *e=[a objectEnumerator];
    id object=nil;
    while(object=[e nextObject]) {
        [self removeFile: [[object image] name]];
    }
    [self update];
}
 
-(NSArray *)getImages: (NSString *)file
{
    NSMutableArray *justFiles=[[NSMutableArray alloc] init];
 
    // Figure out if it's a directory or a file.
    // If it's a directory, recurse. If it's a file, just use it.
    BOOL isDir=FALSE;
    NSFileManager *manager = [NSFileManager defaultManager];
    if ([manager fileExistsAtPath:file isDirectory:&isDir]) {
        if (isDir) {
            NSArray *subpaths = [manager subpathsAtPath:file];
            int i=0;
            for(i=0; i<[subpaths count]; i++) {
                NSString *fullpath=[file
                    stringByAppendingPathComponent: [subpaths objectAtIndex: i]];
                if ([manager fileExistsAtPath:file isDirectory:&isDir]) {
                    [justFiles addObject: fullpath];
                }
            }
        } else {
            [justFiles addObject: file];
        }
    }
 
    NSMutableArray *rv=[[NSMutableArray alloc] init];
    [rv autorelease];
 
    NSEnumerator *nse=[justFiles objectEnumerator];
    id object=nil;
    while(object = [nse nextObject]) {
        // NSLog(@"--- %@\n", object);
        if([self isImage: object]) {
            [rv addObject: object];
        }
    }
    // pathExtension
 
    [justFiles release];
    return(rv);
}
 
-(BOOL)performDragOperation:(id <NSDraggingInfo>)sender
{
    NSPasteboard *pboard=[sender draggingPasteboard];
    NSArray *files = [pboard propertyListForType:NSFilenamesPboardType];
    int i=0;
    for(i=0; i<[files count]; i++) {
        NSString *filename=[files objectAtIndex: i];
        NSArray *subfilenames=[self getImages: filename];
        int j=0;
        for(j=0; j<[subfilenames count]; j++) {
            id subfilename=[subfilenames objectAtIndex: j];
            [self addFile: subfilename];
            // NSLog(@"Subfile %@\n", subfilename);
        }
        // NSLog(@"Got %@\n", filename);
    }
    [self update];
    return (TRUE);
}
 
@end