forked from phildow/Journler
-
Notifications
You must be signed in to change notification settings - Fork 1
/
DropBoxDialog.m
601 lines (471 loc) · 17.8 KB
/
DropBoxDialog.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
/*
Redistribution and use in source and binary forms, with or without modification, are permitted
provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list of conditions
and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions
and the following disclaimer in the documentation and/or other materials provided with the
distribution.
* Neither the name of the author nor the names of its contributors may be used to endorse or
promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
// Basically, you can use the code in your free, commercial, private and public projects
// as long as you include the above notice and attribute the code to Philip Dow / Sprouted
// If you use this code in an app send me a note. I'd love to know how the code is used.
// Please also note that this copyright does not supersede any other copyrights applicable to
// open source code used herein. While explicit credit has been given in the Journler about box,
// it may be lacking in some instances in the source code. I will remedy this in future commits,
// and if you notice any please point them out.
#import "DropBoxDialog.h"
#import "JournlerJournal.h"
#import "JournlerEntry.h"
#import "FoldersController.h"
#import "CollectionsSourceList.h"
#import "DropBoxFoldersController.h"
#import "DropBoxSourceList.h"
#import <SproutedUtilities/SproutedUtilities.h>
#import <SproutedInterface/SproutedInterface.h>
typedef void (*DropBoxDidEndIMP)(id, SEL, id, int, id);
static NSSortDescriptor *FoldersByIndexSortPrototype()
{
static NSSortDescriptor *descriptor = nil;
if ( descriptor == nil )
{
descriptor = [[NSSortDescriptor alloc] initWithKey:@"index" ascending:YES selector:@selector(compare:)];
}
return descriptor;
}
@implementation DropBoxDialog
- (id) initWithJournal:(JournlerJournal*)aJournal delegate:(id)aDelegate mode:(NSInteger)dropboxMode didEndSelector:(SEL)aSelector
{
if ( self = [super initWithWindowNibName:@"DropBoxDialog"] )
{
didEndSelector = aSelector;
journal = [aJournal retain];
delegate = aDelegate;
mode = dropboxMode;
canCancelImport = YES;
[self retain];
}
return self;
}
- (void) windowDidLoad
{
NSString *defaultCategory;
// visual goodness
[[self window] setMovableByWindowBackground:NO];
//[gradientBackground setControlTint:NSGraphiteControlTint];
if ( ![gradientBackground respondsToSelector:@selector(addTrackingArea:)] )
{
// on 10.4 change the appearance of the buttons and window
static NSInteger kBottomBarHeight = 42;
static NSInteger kMinButtonWidth = 96;
[[self window] setBackgroundColor:[NSColor colorWithCalibratedWhite:0.98 alpha:0.98]];
[returnButton setBezelStyle:NSRoundedBezelStyle]; //NSRegularSquareBezelStyle
[cancelButton setBezelStyle:NSRoundedBezelStyle];
[returnButton setFont:[NSFont systemFontOfSize:[NSFont systemFontSizeForControlSize:NSRegularControlSize]]];
[cancelButton setFont:[NSFont systemFontOfSize:[NSFont systemFontSizeForControlSize:NSRegularControlSize]]];
[returnButton sizeToFit];
[cancelButton sizeToFit];
[[rememberFolderSelectionCheckbox cell] setControlSize:NSRegularControlSize];
[rememberFolderSelectionCheckbox setFont:[NSFont systemFontOfSize:[NSFont systemFontSizeForControlSize:NSRegularControlSize]]];
[rememberFolderSelectionCheckbox sizeToFit];
NSRect aFrame = [returnButton frame];
NSRect superBounds = [[returnButton superview] bounds];
aFrame.size.width += 18;
if ( aFrame.size.width < kMinButtonWidth ) kMinButtonWidth = kMinButtonWidth;
aFrame.origin.x = superBounds.size.width - 16 - aFrame.size.width;
aFrame.origin.y = kBottomBarHeight/2 - aFrame.size.height/2 - 1;
[returnButton setFrame:aFrame];
aFrame = [cancelButton frame];
aFrame.size.width += 18;
if ( aFrame.size.width < kMinButtonWidth ) kMinButtonWidth = kMinButtonWidth;
aFrame.origin.x = [returnButton frame].origin.x - aFrame.size.width;
aFrame.origin.y = kBottomBarHeight/2 - aFrame.size.height/2 - 1;
[cancelButton setFrame:aFrame];
}
//if ( [[self window] respondsToSelector:@selector(contentBorderThicknessForEdge:)] )
// [[self window] setContentBorderThickness:43.0 forEdge:NSMinYEdge];
// the folders controller must know the actual root (vs. the roots children)
[sourceController setRootCollection:[[self journal] valueForKey:@"rootCollection"]];
// populate the list with regular, smart folders and the journal
NSPredicate *folderFilter = [NSPredicate predicateWithFormat:@"isRegularFolder == YES OR isSmartFolder == YES"];
NSArray *filteredFolders = [[[self journal] rootFolders] filteredArrayUsingPredicate:folderFilter];
[sourceController setContent:filteredFolders];
// set the sort descriptors on the source list
[sourceController setSortDescriptors:[NSArray arrayWithObject:FoldersByIndexSortPrototype()]];
// prepare the categories and select the default
NSArray *categoriesList = [[NSUserDefaults standardUserDefaults] arrayForKey:@"Journler Categories List"];
[categoryField addItemsWithObjectValues: [categoriesList sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] ];
// category
defaultCategory = [JournlerEntry dropBoxCategory];
if ( defaultCategory != nil )
{
[categoryField addItemWithObjectValue:defaultCategory];
[categoryField selectItemWithObjectValue:defaultCategory];
}
if ( [categoryField numberOfVisibleItems] > [categoryField numberOfItems] )
[categoryField setNumberOfVisibleItems:[categoryField numberOfItems]];
// content appearance
[[[filesTable enclosingScrollView] verticalScroller] setControlTint:NSGraphiteControlTint];
[[[sourceList enclosingScrollView] verticalScroller] setControlTint:NSGraphiteControlTint];
[[tagsField cell] setControlTint:NSGraphiteControlTint];
[[categoryField cell] setControlTint:NSGraphiteControlTint];
[[tagsField cell] setFocusRingType:NSFocusRingTypeNone];
[[categoryField cell] setFocusRingType:NSFocusRingTypeNone];
[[self window] setDefaultButtonCell:[returnButton cell]];
// source list state
NSArray *sourceListState;
NSData *sourceListStateData;
if ( ( sourceListStateData = [[NSUserDefaults standardUserDefaults] dataForKey:@"DropBoxSourceListState"] ) != nil
&& ( sourceListState = [NSKeyedUnarchiver unarchiveObjectWithData:sourceListStateData] ) != nil )
[sourceList restoreStateFromArray:sourceListState];
// maintains the drop box selection
NSArray *indexPaths;
NSData *indexPathsData;
if ( ( indexPathsData = [[NSUserDefaults standardUserDefaults] dataForKey:@"DropBoxSourceListSelection"] ) != nil
&& ( indexPaths = [NSKeyedUnarchiver unarchiveObjectWithData:indexPathsData] ) != nil
&& [[NSUserDefaults standardUserDefaults] boolForKey:@"DropBoxSourceListRememberSelection"] )
{
[sourceController setSelectionIndexPaths:indexPaths];
if ( [sourceList selectedRow] != -1 ) [sourceList scrollRowToVisible:[sourceList selectedRow]];
}
if ( tags != nil ) [tagsField setObjectValue:tags];
if ( category != nil ) [categoryField setStringValue:category];
}
- (void) dealloc
{
#ifdef __DEBUG__
NSLog(@"%s",__PRETTY_FUNCTION__);
#endif
[activeApplication release];
[journal release];
[content release];
[representedObject release];
[tagCompletions release];
[super dealloc];
}
- (void) windowWillClose:(NSNotification*)aNotification
{
if ( [self window] == [aNotification object] )
{
NSArray *sourceListState = [sourceList stateArray];
NSData *sourceListStateData = [NSKeyedArchiver archivedDataWithRootObject:sourceListState];
[[NSUserDefaults standardUserDefaults] setObject:sourceListStateData forKey:@"DropBoxSourceListState"];
// maintains the drop box selection
NSArray *indexPaths = [sourceController selectionIndexPaths];
NSData *indexPathsData = [NSKeyedArchiver archivedDataWithRootObject:indexPaths];
[[NSUserDefaults standardUserDefaults] setObject:indexPathsData forKey:@"DropBoxSourceListSelection"];
[sourceList setDelegate:nil];
[sourceController unbind:@"contentArray"];
[sourceController setContent:nil];
[cancelButton unbind:@"hidden"];
[self autorelease];
}
}
#pragma mark -
+ (NSArray*) contentForFilenames:(NSArray*)filenames
{
// load the files
NSMutableArray *fileObjects = [NSMutableArray arrayWithCapacity:[filenames count]];
for ( NSString *aFilename in filenames )
{
// keys: title, filename, icon, description
NSMutableDictionary *objectDictionary = [NSMutableDictionary dictionary];
NSString *title = [[aFilename lastPathComponent] stringByDeletingPathExtension];
NSImage *icon = [[NSWorkspace sharedWorkspace] iconForFile:aFilename];
[icon setSize:NSMakeSize(32,32)];
[objectDictionary setValue:icon forKey:@"icon"];
[objectDictionary setValue:title forKey:@"title"];
[objectDictionary setValue:aFilename forKey:@"filename"];
[objectDictionary setValue:aFilename forKey:@"representedObject"];
[fileObjects addObject:objectDictionary];
}
return fileObjects;
}
+ (NSArray*) contentForEntries:(NSArray*)entries
{
NSMutableArray *entryObjects = [NSMutableArray arrayWithCapacity:[entries count]];
for ( JournlerEntry *anEntry in entries )
{
// keys: title, filename, icon, description
NSMutableDictionary *objectDictionary = [NSMutableDictionary dictionary];
//NSString *title = [[aFilename lastPathComponent] stringByDeletingPathExtension];
NSImage *icon = nil;
if ( [[anEntry resources] count] == 0 )
icon = [anEntry icon];
else
icon = [[[anEntry resources] objectAtIndex:0] icon];
// readjust the size
icon = [icon imageWithWidth:32 height:32];
[objectDictionary setValue:icon forKey:@"icon"];
[objectDictionary setValue:[anEntry title] forKey:@"title"];
[objectDictionary setValue:anEntry forKey:@"representedObject"];
//[objectDictionary setValue:aFilename forKey:@"filename"];
[entryObjects addObject:objectDictionary];
}
return entryObjects;
}
#pragma mark -
- (JournlerJournal*) journal
{
return journal;
}
- (void) setJournal:(JournlerJournal*)aJournal
{
if ( journal != aJournal )
{
[journal release];
journal = [aJournal retain];
}
}
- (NSDictionary*) activeApplication
{
return activeApplication;
}
- (void) setActiveApplication:(NSDictionary*)aDictionary
{
if ( activeApplication != aDictionary )
{
[activeApplication release];
activeApplication = [aDictionary copyWithZone:[self zone]];
}
}
- (NSArray*)content
{
return content;
}
- (void) setContent:(NSArray*)anArray
{
if ( content != anArray )
{
[content release];
content = [anArray copyWithZone:[self zone]];
[self setMultipleFiles:([anArray count] > 0)];
}
}
- (id) representedObject
{
return representedObject;
}
- (void) setRepresentedObject:(id)anObject
{
if ( representedObject != anObject )
{
[representedObject release];
representedObject = [anObject retain];
}
}
- (BOOL) multipleFiles
{
return multipleFiles;
}
- (void) setMultipleFiles:(BOOL)multiple
{
multipleFiles = multiple;
if ( multipleFiles == YES ) [noteField setStringValue:NSLocalizedString(@"dropbox note singular",@"")];
else [noteField setStringValue:NSLocalizedString(@"dropbox note plural",@"")];
}
- (BOOL) canCancelImport
{
return canCancelImport;
}
- (void) setCanCancelImport:(BOOL)canCancel
{
canCancelImport = canCancel;
}
- (BOOL) shouldDeleteOriginal
{
return shouldDeleteOriginal;
}
- (void) setShouldDeleteOriginal:(BOOL)deletesOriginal
{
shouldDeleteOriginal = deletesOriginal;
}
- (NSArray*) tagCompletions
{
return tagCompletions;
}
- (void) setTagCompletions:(NSArray*)anArray
{
if ( tagCompletions != anArray )
{
[tagCompletions release];
tagCompletions = [anArray copyWithZone:[self zone]];
}
}
#pragma mark -
- (IBAction) runClose:(id)sender
{
//[delegate dropBox:self didDenyContent:[self content]];
[self _endWithCode:NSRunAbortedResponse];
}
- (IBAction) doImport:(id)sender
{
//[delegate dropBox:self didAcceptContent:[self content]];
[self _endWithCode:NSRunStoppedResponse];
}
- (void) _endWithCode:(NSInteger)code
{
DropBoxDidEndIMP didEnd;
didEnd = (DropBoxDidEndIMP)[delegate methodForSelector:didEndSelector];
didEnd(delegate, didEndSelector, self, code, [self content]);
}
#pragma mark -
- (IBAction) showWindow:(id)sender
{
NSDictionary *theActiveApplication = [[NSWorkspace sharedWorkspace] activeApplication];
[self setActiveApplication:theActiveApplication];
[[self window] center];
// activate ourselves
[NSApp activateIgnoringOtherApps:YES];
// fade all our windows out
//[self fadeOutAllWindows:[NSArray arrayWithObject:[dropBoxDialog window]]];
//[dropBoxDialog fadeWindowIn:self];
//result = [NSApp runModalForWindow:[dropBoxDialog window]];
[[self window] setLevel:NSModalPanelWindowLevel];
[super showWindow:self];
//[[dropBoxDialog window] orderFront:self];
//[[dropBoxDialog window] center];
//[[dropBoxDialog window] orderFrontRegardless];
[[self window] makeKeyAndOrderFront:self];
}
- (IBAction) changeFolderSelectionMemory:(id)sender
{
[[NSUserDefaults standardUserDefaults] setBool:( [sender state] == NSOnState ) forKey:@"DropBoxSourceListRememberSelection"];
if ( [sender state] == NSOffState )
{
[sourceList deselectAll:self];
[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"DropBoxSourceListSelection"];
}
}
#pragma mark -
#pragma mark Fading
- (void) fadeWindowOut:(id)sender
{
NSDictionary *aDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
[self window], NSViewAnimationTargetKey,
NSViewAnimationFadeOutEffect, NSViewAnimationEffectKey, nil];
NSViewAnimation *animation = [[[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObject:aDictionary]] autorelease];
[animation setDelegate:self];
[animation setDuration:0.15];
[animation startAnimation];
}
- (void) fadeWindowIn:(id)sender
{
[[self window] setAlphaValue:0];
NSDictionary *aDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
[self window], NSViewAnimationTargetKey,
NSViewAnimationFadeInEffect, NSViewAnimationEffectKey, nil];
NSViewAnimation *animation = [[[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObject:aDictionary]] autorelease];
[animation setDuration:0.15];
[animation startAnimation];
}
- (void)animationDidEnd:(NSAnimation*)animation
{
[[self window] close];
}
#pragma mark -
#pragma mark Table View Delegation
- (BOOL)tableView:(NSTableView *)aTableView shouldSelectRow:(NSInteger)rowIndex
{
return YES;
}
- (void)tableView:(NSTableView *)aTableView willDisplayCell:(id)aCell forTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex
{
// keys: title, filename, icon, description
[(ImageAndTextCell*)aCell setImageSize:NSMakeSize(32,32)];
[(ImageAndTextCell*)aCell setImage:[[[filesController arrangedObjects] objectAtIndex:rowIndex] valueForKey:@"icon"]];
// selection (so color can determine colors, font, etc)
[(ImageAndTextCell*)aCell setSelected:( [[aTableView selectedRowIndexes] containsIndex:rowIndex] )];
}
- (NSString *)tableView:(NSTableView *)aTableView toolTipForCell:(NSCell *)aCell
rect:(NSRectPointer)rect tableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)row mouseLocation:(NSPoint)mouseLocation
{
NSString *tooltip;
NSString *description = [[[filesController arrangedObjects] objectAtIndex:row] valueForKey:@"description"];
if ( description != nil )
tooltip = description;
else
tooltip = [[[filesController arrangedObjects] objectAtIndex:row] valueForKey:@"title"];
return tooltip;
}
#pragma mark -
#pragma mark JournlerConditionController Delegate (NSTokenField)
- (NSArray *)tokenField:(NSTokenField *)tokenField completionsForSubstring:(NSString *)substring
indexOfToken:(NSInteger)tokenIndex indexOfSelectedItem:(NSInteger *)selectedIndex
{
//NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self beginswith[cd] %@", substring];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self beginswith %@", substring];
NSArray *completions = [[self tagCompletions] filteredArrayUsingPredicate:predicate];
return completions;
}
#pragma mark -
- (NSArray*) tags
{
/*
NSString *theTags = [tagsField stringValue];
if ( [theTags length] == 0 )
return nil;
else
return theTags;
*/
NSArray *theTags = [tagsField objectValue];
if ( [theTags count] == 0 )
return nil;
else
return theTags;
}
- (void) setTags:(NSArray*)anArray
{
if ( anArray != nil )
{
if ( [self isWindowLoaded] )
[tagsField setObjectValue:anArray];
else
tags = [anArray copyWithZone:[self zone]];
}
}
- (NSString*) category
{
NSString *theCategory = [categoryField stringValue];
if ( [theCategory length] == 0 )
return nil;
else
return theCategory;
}
- (void) setCategory:(NSString*)aCategory
{
if ( aCategory != nil )
{
if ( [self isWindowLoaded] )
[categoryField setStringValue:aCategory];
else
category = [aCategory copyWithZone:[self zone]];
}
}
#pragma mark -
- (JournlerCollection*) selectedFolder
{
NSArray *selectedObjects = [sourceController selectedObjects];
if ( [selectedObjects count] == 0 )
return nil;
else
return [selectedObjects objectAtIndex:0];
}
- (NSArray*) selectedFolders
{
NSArray *selectedObjects = [sourceController selectedObjects];
if ( [selectedObjects count] == 0 )
return nil;
else return selectedObjects;
}
@end