-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTLMercatalogControllerShared.m
96 lines (80 loc) · 3.66 KB
/
TLMercatalogControllerShared.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
//
// TLMercatalogControllerShared.m
// Mercatalog
//
// Created by Nathan Vander Wilt on 12/17/08.
// Copyright 2008 __MyCompanyName__. All rights reserved.
//
#import "TLMercatalogControllerShared.h"
#import "TLPhoto.h"
#import "TLOffsetTimestamp.h"
NSString* const TLMercatalogSelectionDidChangeNotification = @"TLMercatalog_SelectionDidChangeNotification";
static NSString* const TLMercatalogInternalPhotosPboardType = @"com.calftrail.mercatalog.photo.pboard";
NSArray* TLMercatalogAcceptedDropTypes() {
return [NSArray arrayWithObjects:TLMercatalogInternalPhotosPboardType, NSFilenamesPboardType, nil];
}
BOOL TLMercatalogWritePhotosToPasteboard(NSArray* photos,
NSPasteboard* pboard, id owner)
{
NSArray* supportedDragTypes = [NSArray arrayWithObjects:
TLMercatalogInternalPhotosPboardType, NSFilesPromisePboardType, nil];
(void)[pboard declareTypes:supportedDragTypes owner:owner];
NSMutableArray* photoPropertyList = [NSMutableArray arrayWithCapacity:[photos count]];
for (TLPhoto* photo in photos) {
NSData* photoPointerAsData = [NSData dataWithBytes:&photo length:sizeof(TLPhoto*)];
[photoPropertyList addObject:photoPointerAsData];
}
BOOL internalsWroteSuccesfully = [pboard setPropertyList:photoPropertyList
forType:TLMercatalogInternalPhotosPboardType];
NSArray* promisedFileTypes = [NSArray arrayWithObject:(id)kUTTypeJPEG];
BOOL filesWroteSuccessfully = [pboard setPropertyList:promisedFileTypes
forType:NSFilesPromisePboardType];
return (internalsWroteSuccesfully && filesWroteSuccessfully);
}
NSArray* TLMercatalogPhotosFromPasteboard(NSPasteboard* pboard) {
NSArray* internalDropTypes = [NSArray arrayWithObject:TLMercatalogInternalPhotosPboardType];
NSString* internalType = [pboard availableTypeFromArray:internalDropTypes];
if (!internalType) return nil;
NSArray* photosAsData = [pboard propertyListForType:internalType];
NSMutableArray* photos = [NSMutableArray arrayWithCapacity:[photosAsData count]];
for (NSData* photoPointerAsData in photosAsData) {
TLPhoto* photo = *(TLPhoto**)[photoPointerAsData bytes];
[photos addObject:photo];
}
return photos;
}
NSArray* TLMercatalogFilesFromPasteboard(NSPasteboard* pboard) {
NSArray* externalDropTypes = [NSArray arrayWithObject:NSFilenamesPboardType];
NSString* externalType = [pboard availableTypeFromArray:externalDropTypes];
if (!externalType) return nil;
NSArray* filenames = [pboard propertyListForType:externalType];
return filenames;
}
BOOL TLMercatalogPhotosAreLocked(NSArray* photos) {
BOOL photosLocked = NO;
for (TLPhoto* photo in photos) {
if ([photo isLocked]) {
photosLocked = YES;
break;
}
}
return photosLocked;
}
NSMapTable* TLMercatalogTimestampPhotos(NSArray* photos, TLTimestamp* firstTimestamp) {
// the first photo in the array is the one under the mouse when drag was initiated
TLPhoto* primaryDraggedPhoto = [photos objectAtIndex:0];
NSDate* primaryPhotoDate = [[primaryDraggedPhoto timestamp] time];
// find offset such that: primaryPhotoTime + offset = mouseTime
NSTimeInterval offset = [[firstTimestamp time] timeIntervalSinceDate:primaryPhotoDate];
NSMapTable* photoTimestamps = [NSMapTable mapTableWithStrongToStrongObjects];
for (TLPhoto* photo in photos) {
NSDate* photoDate = [[photo timestamp] time];
NSDate* adjustedTime = [photoDate addTimeInterval:offset];
NSTimeInterval photoOffset = [adjustedTime timeIntervalSinceDate:[photo originalDate]];
TLOffsetTimestamp* timestamp = [TLOffsetTimestamp timestampWithTime:adjustedTime
accuracy:[firstTimestamp accuracy]
offset:photoOffset];
[photoTimestamps setObject:timestamp forKey:photo];
}
return photoTimestamps;
}