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#import "TimeBasedMapping.h"
#import "CompoundItem.h"
#import "DirectoryItem.h"
#import "PlainFileItem.h"
#import "PreferencesPanelControl.h"
#import "TreeWriter.h"
@interface TimeBasedMapping (PrivateMethods)
- (void) initTimeBounds:(DirectoryItem *)treeRoot;
- (void) visitItemToDetermineTimeBounds:(Item *)item;
@end // @interface TimeBasedMapping (PrivateMethods)
@implementation TimeBasedMapping
const int secondsPerDay = 60 * 60 * 24;
// Set minimum time granularity to a minute
const int minTimeDelta = 60;
// Overrides designated initialiser
- (instancetype) initWithFileItemMappingScheme:(NSObject <FileItemMappingScheme> *)schemeVal {
NSAssert(NO, @"Use other initialiser instead");
return [self initWithFileItemMappingScheme: nil tree: nil];
}
- (instancetype) initWithFileItemMappingScheme:(NSObject <FileItemMappingScheme> *)schemeVal
tree:(DirectoryItem *)tree {
if (self = [super initWithFileItemMappingScheme: schemeVal]) {
[self initTimeBounds: tree];
}
return self;
}
- (NSUInteger) hashForFileItem:(PlainFileItem *)item atDepth:(NSUInteger)depth {
CFAbsoluteTime itemTime = nowTime - [self timeForFileItem: item];
CFAbsoluteTime refTime = nowTime - minTime;
NSUInteger hash = 0;
while (YES) {
if (itemTime > refTime) {
return hash;
}
hash++;
refTime = refTime / 2;
if (refTime < minTimeDelta) {
return hash;
}
}
}
- (BOOL) canProvideLegend {
return YES;
}
//----------------------------------------------------------------------------
// Implementation of LegendProvidingFileItemMapping
- (NSString *)descriptionForHash: (NSUInteger)hash {
CFAbsoluteTime lowerBound = 0;
CFAbsoluteTime upperBound = minTime;
NSUInteger i = hash;
while (i > 0) {
lowerBound = upperBound;
upperBound = lowerBound + (nowTime - lowerBound) / 2;
i--;
}
int maxDelta = (int) floor((nowTime - lowerBound) / secondsPerDay);
int minDelta = (int) ceil((nowTime - upperBound) / secondsPerDay);
if (hash == 0) {
NSString *fmt = NSLocalizedString(@"%d days ago or more",
@"Legend for Time-based mapping schemes.");
return [NSString stringWithFormat: fmt, minDelta];
} else if (minDelta < maxDelta) {
NSString *fmt = NSLocalizedString(@"%d - %d days ago",
@"Legend for Time-based mapping schemes.");
return [NSString stringWithFormat: fmt, minDelta, maxDelta];
} else {
NSString *fmt = NSLocalizedString(@"%d days ago",
@"Legend for Time-based mapping schemes.");
return [NSString stringWithFormat: fmt, maxDelta];
}
}
- (NSString *) descriptionForRemainingHashes {
return NSLocalizedString(@"More recent",
@"Legend for Time-based mapping schemes.");
}
@end // @implementation TimeBasedMapping
@implementation TimeBasedMapping (PrivateMethods)
- (void) initTimeBounds:(DirectoryItem *)treeRoot {
minTime = 0;
maxTime = 0;
[self visitItemToDetermineTimeBounds: treeRoot];
nowTime = CFAbsoluteTimeGetCurrent();
if (maxTime > nowTime) {
NSLog(@"Maximum time is in the future.");
}
if (minTime > nowTime) {
NSLog(@"Minimum time is in the future.");
}
// Check if the preferences override the minimum.
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSString *minTimeBoundString = [userDefaults stringForKey: MinimumTimeBoundForColorMappingKey];
CFAbsoluteTime minTimeBound;
static CFDateFormatterRef dateFormatter = NULL;
if (dateFormatter == NULL) {
// Lazily create formatter
dateFormatter = CFDateFormatterCreate(kCFAllocatorDefault,
NULL,
kCFDateFormatterNoStyle,
kCFDateFormatterNoStyle);
CFDateFormatterSetFormat(dateFormatter, (CFStringRef)@"dd/MM/yyyy HH:mm");
}
Boolean ok = CFDateFormatterGetAbsoluteTimeFromString(dateFormatter,
(CFStringRef) minTimeBoundString,
NULL,
&minTimeBound);
if (! ok) {
NSLog(@"Failed to parse preference value for %@: %@",
MinimumTimeBoundForColorMappingKey,
minTimeBoundString);
} else if (minTimeBound > nowTime) {
NSLog(@"Ignoring preference value for %@. It occurs in the future.",
MinimumTimeBoundForColorMappingKey);
} else if (minTime < minTimeBound) {
minTime = minTimeBound;
NSLog(@"Basing minTime on value specified in preferences.");
}
NSLog(@"minTime=%@, maxTime=%@",
[FileItem stringForTime: minTime],
[FileItem stringForTime: maxTime]);
}
- (void) visitItemToDetermineTimeBounds:(Item *)item {
if ([item isVirtual]) {
[self visitItemToDetermineTimeBounds: ((CompoundItem *)item).first];
[self visitItemToDetermineTimeBounds: ((CompoundItem *)item).second];
}
else {
FileItem *fileItem = (FileItem *)item;
if ([fileItem isPhysical]) {
// Only consider actual files.
CFAbsoluteTime itemTime = [self timeForFileItem: fileItem];
if (itemTime != 0) {
if (minTime == 0 || itemTime < minTime) {
minTime = itemTime;
}
if (maxTime == 0 || itemTime > maxTime) {
maxTime = itemTime;
}
}
}
if ([fileItem isDirectory]) {
[self visitItemToDetermineTimeBounds: ((DirectoryItem *)fileItem).contents];
}
}
}
@end // @implementation TimeBasedMapping (PrivateMethods)