forked from amaxwell/tlutility
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TLMBackupDataSource.m
342 lines (294 loc) · 12.7 KB
/
TLMBackupDataSource.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
//
// TLMBackupDataSource.m
// TeX Live Utility
//
/*
This software is Copyright (c) 2010-2016
Adam Maxwell. All rights reserved.
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 Adam Maxwell nor the names of any
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
OWNER 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.
*/
#import "TLMBackupDataSource.h"
#import "TLMBackupNode.h"
#import "TLMInfoController.h"
#import "TLMLogServer.h"
#import "TLMBackupCell.h"
#import "TLMEnvironment.h"
@implementation TLMBackupDataSource
@synthesize outlineView = _outlineView;
@synthesize _controller;
@synthesize statusWindow = _statusWindow;
@synthesize backupNodes = _backupNodes;
@synthesize _searchField;
@synthesize refreshing = _refreshing;
@synthesize needsUpdate = _needsUpdate;
- (id)init
{
self = [super init];
if (self) {
_displayedBackupNodes = [NSMutableArray new];
_sortDescriptors = [NSMutableArray new];
}
return self;
}
- (void)dealloc
{
_controller = nil;
[_outlineView setDelegate:nil];
[_outlineView setDataSource:nil];
[_outlineView release];
[_backupNodes release];
[_displayedBackupNodes release];
[_searchField release];
[_sortDescriptors release];
[_statusWindow release];
[super dealloc];
}
- (void)awakeFromNib
{
[_outlineView setFontNamePreferenceKey:@"TLMBackupListTableFontName"
sizePreferenceKey:@"TLMBackupListTableFontSize"];
[_outlineView disableOutlineCells];
[_outlineView setDoubleAction:@selector(showInfo:)];
[_outlineView setTarget:self];
}
- (NSDate *)_dateForName:(NSString *)name version:(NSNumber *)version backupDir:(NSString *)backupDir
{
NSFileManager *dfm = [NSFileManager defaultManager];
NSString *fileName = [NSString stringWithFormat:@"%@.r%@.tar.xz", name, version];
fileName = [backupDir stringByAppendingPathComponent:fileName];
return [[dfm attributesOfItemAtPath:fileName error:NULL] fileModificationDate];
}
- (void)setBackupNodes:(NSArray *)nodes
{
[_backupNodes autorelease];
_backupNodes = [nodes copy];
NSString *backupDir = [[[TLMEnvironment currentEnvironment] backupDirectory] path];
for (TLMBackupNode *node in _backupNodes) {
for (NSUInteger i = 0; i < [node numberOfVersions]; i++) {
TLMBackupNode *child = [node versionAtIndex:i];
[child setDate:[self _dateForName:[child name] version:[child version] backupDir:backupDir]];
}
}
[self search:nil];
}
- (BOOL)validateUserInterfaceItem:(id <NSValidatedUserInterfaceItem>)anItem;
{
SEL action = [anItem action];
if (@selector(showInfo:) == action)
return [[[TLMInfoController sharedInstance] window] isVisible] == NO;
else if (@selector(refreshList:) == action)
return NO == _refreshing;
else
return YES;
}
- (IBAction)search:(id)sender;
{
NSString *searchString = [_searchField stringValue];
NSArray *selectedItems = [_outlineView selectedItems];
if (nil == searchString || [searchString isEqualToString:@""]) {
[_displayedBackupNodes setArray:_backupNodes];
}
else {
[_displayedBackupNodes removeAllObjects];
for (TLMBackupNode *node in _backupNodes) {
if ([node matchesSearchString:searchString])
[_displayedBackupNodes addObject:node];
}
}
[_displayedBackupNodes sortUsingDescriptors:_sortDescriptors];
[_outlineView reloadData];
// restore previously selected packages, if possible
NSMutableIndexSet *indexes = [NSMutableIndexSet indexSet];
for (id item in selectedItems) {
NSInteger idx = [_outlineView rowForItem:item];
if (-1 != idx)
[indexes addIndex:idx];
}
[_outlineView selectRowIndexes:indexes byExtendingSelection:NO];
}
- (id)selectedItem
{
id selectedItem = nil;
if ([_outlineView selectedRow] != -1) {
selectedItem = [_outlineView itemAtRow:[_outlineView selectedRow]];
if ([_outlineView parentForItem:selectedItem])
selectedItem = [_outlineView parentForItem:selectedItem];
}
return selectedItem;
}
- (IBAction)showInfo:(id)sender;
{
if ([self selectedItem] != nil)
[[TLMInfoController sharedInstance] showInfoForPackage:[self selectedItem] location:[_controller serverURL]];
else if ([[[TLMInfoController sharedInstance] window] isVisible] == NO) {
[[TLMInfoController sharedInstance] showInfoForPackage:nil location:[_controller serverURL]];
[[TLMInfoController sharedInstance] showWindow:nil];
}
}
- (IBAction)refreshList:(id)sender;
{
[_controller refreshBackupList];
}
static inline BOOL __TLMIsParentNode(id obj)
{
return [(TLMBackupNode *)obj version] == nil;
}
- (void)restoreAlertDidEnd:(NSAlert *)alert returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo
{
TLMBackupNode *node = [(id)contextInfo autorelease];
if (NSAlertFirstButtonReturn == returnCode) {
[_controller restorePackage:[node name] version:[node version]];
}
else {
TLMLog(__func__, @"User cancelled restore action of %@", [node name]);
}
}
- (void)restoreAction:(id)sender
{
TLMBackupNode *clickedNode = [_outlineView itemAtRow:[_outlineView clickedRow]];
// mainly for my own testing
if ([[NSApp currentEvent] modifierFlags] & NSAlternateKeyMask) {
TLMLog(__func__, @"User bypassed restore alert for %@", [clickedNode name]);
[_controller restorePackage:[clickedNode name] version:[clickedNode version]];
}
else {
NSAlert *alert = [[NSAlert new] autorelease];
[alert setMessageText:[NSString stringWithFormat:NSLocalizedString(@"Are you sure you want to restore %@ from backup?", @"alert title"), [clickedNode name]]];
[alert setInformativeText:NSLocalizedString(@"You can always update to the latest version or restore a different one to undo this change.", @"alert message")];
[alert addButtonWithTitle:NSLocalizedString(@"Restore", @"button title")];
[alert addButtonWithTitle:NSLocalizedString(@"Cancel", @"button title")];
[alert beginSheetModalForWindow:[_outlineView window]
modalDelegate:self
didEndSelector:@selector(restoreAlertDidEnd:returnCode:contextInfo:)
contextInfo:[clickedNode retain]];
}
}
#pragma mark NSOutlineView datasource
- (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)anIndex ofItem:(TLMBackupNode *)item;
{
return (nil == item) ? [_displayedBackupNodes objectAtIndex:anIndex] : [item versionAtIndex:anIndex];
}
- (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(TLMBackupNode *)item;
{
return __TLMIsParentNode(item);
}
- (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(TLMBackupNode *)item;
{
return (nil == item) ? [_displayedBackupNodes count] : [item numberOfVersions];
}
- (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item;
{
NSString *colName = [tableColumn identifier];
if ([colName isEqualToString:@"name"])
return __TLMIsParentNode(item) ? (id)[(TLMBackupNode *)item name] : (id)[(TLMBackupNode *)item version];
else if ([colName isEqualToString:@"date"])
return [item date];
return nil;
}
#pragma mark NSOutlineView delegate
- (void)outlineView:(TLMOutlineView *)outlineView willDisplayCell:(id)cell forTableColumn:(NSTableColumn *)tableColumn item:(TLMBackupNode *)item;
{
NSFont *defaultFont = [outlineView defaultFont];
if (__TLMIsParentNode(item)) {
[cell setFont:[NSFont boldSystemFontOfSize:[defaultFont pointSize]]];
}
else if (defaultFont) {
[cell setFont:defaultFont];
}
}
- (NSCell *)outlineView:(TLMOutlineView *)outlineView dataCellForTableColumn:(NSTableColumn *)tableColumn item:(id)item
{
id cell = [tableColumn dataCellForRow:[outlineView rowForItem:item]];
if (cell && __TLMIsParentNode(item) == NO && [[tableColumn identifier] isEqualToString:@"name"]) {
TLMBackupCell *backupCell = [[[TLMBackupCell alloc] initTextCell:@""] autorelease];
[backupCell setTarget:self];
[backupCell setAction:@selector(restoreAction:)];
[backupCell setBackgroundStyle:[cell backgroundStyle]];
[backupCell setLineBreakMode:[cell lineBreakMode]];
[backupCell setWraps:[cell wraps]];
[backupCell setAlignment:[cell alignment]];
[backupCell setScrollable:[cell isScrollable]];
[backupCell setControlSize:[cell controlSize]];
[backupCell setTruncatesLastVisibleLine:[cell truncatesLastVisibleLine]];
cell = backupCell;
}
return cell;
}
- (void)outlineView:(NSOutlineView *)outlineView didClickTableColumn:(NSTableColumn *)tableColumn;
{
/*
Keep versions sorted in ascending order, and only allow sorting by name. This code
was copied from another datasource, so it's overly general in multiple column support,
but not general enough in object support (uses localizedCaseInsensitiveCompare:).
That could be changed, but who wants to change the backup sort order, anyway?
*/
if ([[tableColumn identifier] isEqualToString:@"name"] == NO)
return;
_sortAscending = !_sortAscending;
for (NSTableColumn *col in [outlineView tableColumns])
[outlineView setIndicatorImage:nil inTableColumn:col];
NSImage *image = _sortAscending ? [NSImage imageNamed:@"NSAscendingSortIndicator"] : [NSImage imageNamed:@"NSDescendingSortIndicator"];
[outlineView setIndicatorImage:image inTableColumn:tableColumn];
NSString *key = [tableColumn identifier];
NSSortDescriptor *sort = nil;
// names are all string keys, so do a simple comparison
sort = [[NSSortDescriptor alloc] initWithKey:key ascending:_sortAscending selector:@selector(localizedCaseInsensitiveCompare:)];
[sort autorelease];
// make sure we're not duplicating any descriptors (possibly with reversed order)
NSUInteger cnt = [_sortDescriptors count];
while (cnt--) {
if ([[[_sortDescriptors objectAtIndex:cnt] key] isEqualToString:key])
[_sortDescriptors removeObjectAtIndex:cnt];
}
// push the new sort descriptor, which is correctly ascending/descending
if (sort) [_sortDescriptors insertObject:sort atIndex:0];
// pop the last sort descriptor, if we have more sort descriptors than table columns
while ((NSInteger)[_sortDescriptors count] > [outlineView numberOfColumns])
[_sortDescriptors removeLastObject];
NSArray *selectedItems = [_outlineView selectedItems];
[_displayedBackupNodes sortUsingDescriptors:_sortDescriptors];
[outlineView reloadData];
// restore selection
NSMutableIndexSet *selRows = [NSMutableIndexSet indexSet];
for (id item in selectedItems) {
NSInteger row = [_outlineView rowForItem:item];
if (row != -1)
[selRows addIndex:row];
}
[outlineView selectRowIndexes:selRows byExtendingSelection:NO];
}
- (void)outlineViewSelectionDidChange:(NSNotification *)notification;
{
if ([[[TLMInfoController sharedInstance] window] isVisible]) {
// reset for multiple selection or empty selection
if ([_outlineView numberOfSelectedRows] != 1)
[[TLMInfoController sharedInstance] showInfoForPackage:nil location:[_controller serverURL]];
else
[self showInfo:nil];
}
// toolbar updating is somewhat erratic, so force it to validate here
[[[_controller window] toolbar] validateVisibleItems];
}
@end