public
Description: An OS X application to build a standalone web site as an export from my photo album.
Homepage: http://bleu.west.spy.net/~dustin/projects/photosync/
Clone URL: git://github.com/dustin/photosync.git
Search Repo:
dustin (author)
Wed Nov 30 21:51:37 -0800 2005
commit  2dceca4018192b712c3dcbd872354326347bca00
tree    ad7e3af5f358026eee3aca0e8acc7a23e1024380
parent  08dbe9d7485be04072af84b3db49e976a0564577
photosync / Controller.m
100644 237 lines (207 sloc) 5.385 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
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
#import "Controller.h"
#import "LocEditController.h"
#import "Locations.h"
#import "PhotoClient.h"
#import "PhotoSync.h"
#import "SyncTask.h"
 
@implementation Controller
 
- (IBAction)newLocation:(id)sender
{
  Location *loc=[[Location alloc] init];
  [self addLocation: loc];
  [self showEditor: loc];
  [loc release];
}
 
-(void)stopSync:(id)sender
{
  NSLog(@"Stopping.");
  NSEnumerator *e=[stuffToDo objectEnumerator];
    id object=nil;
    while(object = [e nextObject]) {
    [object cancel];
  }
  [stuffToDo removeAllObjects];
  [[NSNotificationCenter defaultCenter]
    postNotificationName: PS_STOP object: self];
}
 
-(void)setButtonAction:(int)to
{
  switch(to) {
    case BUTTON_SYNC:
      [syncButton setTitle:@"Sync"];
      [syncButton setAction:@selector(performSync:)];
      break;
    case BUTTON_STOP:
      [syncButton setTitle:@"Stop"];
      [syncButton setAction:@selector(stopSync:)];
      break;
  }
}
 
-(void)doNextTask:(id)sender
{
  NSLog(@"doNextTask: called.");
  if([stuffToDo count] == 0) {
    NSLog(@"All tasks complete.");
    [self setButtonAction:BUTTON_SYNC];
    [statusText setHidden: YES];
    [progressIndicator setHidden: YES];
#ifdef GNUSTEP
    // more gnustep workarounds
    [statusText display];
    [progressIndicator display];
#endif
  } else {
    NSLog(@"Starting a task");
    // Hide the progress indicator again so it isn't just stuck.
    [progressIndicator setHidden: YES];
    SyncTask *task=[stuffToDo lastObject];
    [task retain];
    [stuffToDo removeLastObject];
 
    [task run];
    NSLog(@"Finished with %@.", task);
    [task release];
  }
}
 
-(void)completedTask:(SyncTask *)task
{
  NSLog(@"Completed task: %@", task);
  [self doNextTask:self];
}
 
-(void)updateStatus:(NSString *)msg with:(int)done of:(int)total
{
  [statusText setHidden:NO];
  if(total != 0) {
    // Deal with the message string first
    if(msg == nil) {
      msg=@"";
    }
    NSString *myStatus=[[NSString alloc] initWithFormat:
      @"%d of %d - %@", done, total, msg];
    [statusText setStringValue:myStatus];
    [myStatus release];
 
    [progressIndicator setIndeterminate: NO];
    [progressIndicator setMaxValue: (double)total];
    [progressIndicator setHidden: NO];
    [progressIndicator setDoubleValue:(double)done];
  } else if(msg != nil) {
    [statusText setStringValue:msg];
  }
#ifdef GNUSTEP
  // this is also a gnustep workaround
  [progressIndicator display];
  [statusText display];
#endif
}
 
-(BOOL)destExists:(Location *)location
{
  NSFileManager *fm=[NSFileManager defaultManager];
  BOOL rv=NO;
  BOOL isDir=NO;
  NSString *destDir=[location destDir];
  if([fm fileExistsAtPath:destDir isDirectory:&isDir] && isDir) {
    rv=YES;
  } else {
    NSRunAlertPanel(@"Destination missing",
      [NSString stringWithFormat:@"Destination is missing: %@", destDir],
        @"OK", nil, nil);
  }
  return(isDir);
}
 
- (IBAction)performSync:(id)sender
{
  NSLog(@"Grabbing index");
  [self setButtonAction:BUTTON_STOP];
  
  // Clear out the work list
  [stuffToDo removeAllObjects];
  NSEnumerator *e=[[locTable dataSource] objectEnumerator];
    id object=nil;
    while(object = [e nextObject]) {
    if([object isActive] && [self destExists:object]) {
      NSLog(@"Setting up task for %@", object);
      SyncTask *st=[[SyncTask alloc] initWithLocation:object
        delegate:self];
      if(st == nil) {
        NSLog(@"Authentication failed");
        NSRunAlertPanel(@"Authentication Failed",
          [NSString stringWithFormat:@"Authentication failed for %@",
            [object url]], @"OK", nil, nil);
      } else {
        NSLog(@"Queuing %@", st);
        [stuffToDo insertObject:st atIndex:0];
      }
      [st release];
    }
    }
 
  [self doNextTask:sender];
}
 
 
- (IBAction)rmLocation:(id)sender
{
  int row=[locTable selectedRow];
  if(row >= 0) {
    [[locTable dataSource] removeItemAt: row];
    [locTable reloadData];
  }
}
 
-(void)initDefaults
{
  NSUserDefaults *def=[NSUserDefaults standardUserDefaults];
  id ob=[def objectForKey:@"locations"];
  if(ob != nil) {
    [[locTable dataSource] loadArray: ob];
    [locTable reloadData];
  }
}
 
-(void)awakeFromNib
{
  [self setWindowFrameAutosaveName: @"MainWindow"];
  [self initDefaults];
 
  stuffToDo=[[NSMutableArray alloc] init];
 
  [locTable setDoubleAction:@selector(editHighlighted:)];
 
  // Set up notifications to update the view
  [[NSNotificationCenter defaultCenter]
    addObserver:self
    selector:@selector(refreshList:)
    name: PS_DID_UPDATE
    object: nil];
 
  [statusText setHidden:YES];
  [progressIndicator setHidden:YES];
 
#ifdef GNUSTEP
  // gorm doesn't currently let me specify the actual type of button cell. I
  // want a switch, so I'll fix it up here.
  id actColumn=[[locTable tableColumnWithIdentifier:@"active"] dataCell];
  [actColumn setButtonType: NSSwitchButton];
  [actColumn setTitle:@""];
#endif
 
  // Don't allow in-memory cache
#ifndef GNUSTEP
  NSURLCache *cache=[NSURLCache sharedURLCache];
  [cache setMemoryCapacity:0];
#endif
}
 
-(void)refreshList:(id)sender
{
  [locTable reloadData];
}
 
-(void)showEditor:(id)loc
{
  [locEdit showWindow:self];
  [locEdit setLocation:loc];
}
 
-(void)editHighlighted:(id)sender
{
  int row=[locTable selectedRow];
  if(row >= 0) {
    [self showEditor: [[locTable dataSource] objectAtIndex: row]];
  }
}
 
-(void)addLocation:(id)loc
{
  [[locTable dataSource] addItem: loc];
  [locTable reloadData];
}
 
-(void)applicationWillTerminate:(id)notification
{
  NSUserDefaults *def=[NSUserDefaults standardUserDefaults];
  [def setObject: [[locTable dataSource] toArray] forKey:@"locations"];
}
 
@end