-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
DeletionManager.m
executable file
·280 lines (213 loc) · 8.31 KB
/
DeletionManager.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
/*Copyright (c) 2010, Zachary Schneirov. 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 Notational Velocity nor the names of its contributors may be used to endorse
or promote products derived from this software without specific prior written permission. */
#import "DeletionManager.h"
#import "NoteObject.h"
#import "NotationController.h"
#import "NotationDirectoryManager.h"
#import "NotationPrefs.h"
#import "NSCollection_utils.h"
//class for managing notifications of external deletion of note files
@implementation DeletionManager
- (id)init {
if (self=[super init]) {
deletedNotes = [[NSMutableArray alloc] init];
return self;
}
return nil;
}
- (id)initWithNotationController:(NotationController*)aNotationController {
if (self=[self init]) {
notationController = [aNotationController retain];
return self;
}
return nil;
}
- (void)awakeFromNib {
//[window setMaxSize:NSMakeSize(371, 0)];
NSAssert(notationController != nil, @"attempting to awake DeletionManager without a NotationController");
[window setFloatingPanel:YES];
[window setDelegate:self];
}
- (void)dealloc {
[notationController release];
notationController = nil;
[deletedNotes release];
[super dealloc];
}
- (NotationController*)notationController {
return notationController;
}
- (BOOL)noteFileIsAlreadyDeleted:(NoteObject*)aNote {
unsigned count = [deletedNotes count];
if (count > 0) {
unsigned int i;
for (i=0; i<count; i++) {
NoteObject *curNote = [deletedNotes objectAtIndex:i];
if (compareFilename(&curNote, &aNote) == kCFCompareEqualTo) {
return YES;
}
}
}
return NO;
}
- (void)addDeletedNotes:(NSArray*)array {
if ([array count] > 0) {
if (![deletedNotes count]) {
//canceling the delayed selector would not be necessary if updateForVerifiedExistingNote
//did not have the potential to clear out deletedNotes before the selector posted to the next run loop
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(processDeletedNotes) object:nil];
[self performSelector:@selector(processDeletedNotes) withObject:nil afterDelay:0];
}
BOOL didAddDeletedNote = NO;
unsigned int i;
for (i=0; i<[array count]; i++) {
NoteObject *aNote = [array objectAtIndex:i];
if (![self noteFileIsAlreadyDeleted:aNote]) {
[deletedNotes addObject:aNote];
didAddDeletedNote = YES;
}
}
[array makeObjectsPerformSelector:@selector(invalidateFSRef)];
if (didAddDeletedNote) {
[self _updatePanelForNotes];
}
}
hasDeletedNotes = [deletedNotes count] != 0;
}
- (void)addDeletedNote:(NoteObject*)aNote {
if (aNote) {
if (![deletedNotes count]) {
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(processDeletedNotes) object:nil];
[self performSelector:@selector(processDeletedNotes) withObject:nil afterDelay:0];
}
//filter dups or remove these notes from allNotes before adding them here!
if (![self noteFileIsAlreadyDeleted:aNote]) {
[deletedNotes addObject:aNote];
[self _updatePanelForNotes];
}
//clear fsref to ensure that files are re-created if they are restored
//if they are to be deleted, we don't care about them, anyway--they should already be gone
[aNote invalidateFSRef];
}
hasDeletedNotes = [deletedNotes count] != 0;
}
- (void)_updatePanelForNotes {
[tableView reloadData];
[window setFrame:[self windowSizeForNotesFromSender:window] display:NO];
}
void updateForVerifiedDeletedNote(DeletionManager *self, NoteObject *missingNote) {
//called from [NotationController removeNote:]
//just to underscore that this is really the same type of operation, call through to the other method
updateForVerifiedExistingNote(self, missingNote);
}
void updateForVerifiedExistingNote(DeletionManager *self, NoteObject *goodNote) {
//if there are deleted notes currently being shown and goodNote is among them, then update the dialog appropriately, dismissing it if necessary
if (!self->hasDeletedNotes) return;
NSUInteger priorNoteCount = [self->deletedNotes count];
[self->deletedNotes removeObjectIdenticalTo:goodNote];
NSUInteger latterNoteCount = [self->deletedNotes count];
self->hasDeletedNotes = latterNoteCount != 0;
if (latterNoteCount != priorNoteCount) {
[self _updatePanelForNotes];
if (!self->hasDeletedNotes) {
[self cancelPanelReturningCode:1];
}
}
}
- (void)processDeletedNotes {
if ([[notationController notationPrefs] confirmFileDeletion]) {
[self showPanelForDeletedNotes];
} else {
[self removeDeletedNotes];
}
}
- (NSRect)windowSizeForNotesFromSender:(id)sender {
float oldHeight = 0.0;
float newHeight = 0.0;
NSRect newFrame = [sender frame];
NSSize intercellSpacing = [tableView intercellSpacing];
int numRows = MIN(20, [tableView numberOfRows]);
newHeight = MAX(2, numRows) * ([tableView rowHeight] + intercellSpacing.height);
oldHeight = [[[tableView enclosingScrollView] contentView] frame].size.height;
newHeight = [sender frame].size.height - oldHeight + newHeight;
newFrame.origin.y = newFrame.origin.y + newFrame.size.height - newHeight;
newFrame.size.height = newHeight;
return newFrame;
}
- (void)showPanelForDeletedNotes {
if (![deletedNotes count]) {
NSLog(@"showPanelForDeletedNotes was asked to display without deleted notes");
return;
}
if (!window) {
if (![NSBundle loadNibNamed:@"DeletionManager" owner:self]) {
NSLog(@"Failed to load DeletionManager.nib");
NSBeep();
return;
}
}
[confirmDeletionButton setState:![[notationController notationPrefs] confirmFileDeletion]];
//sort notes by title
[deletedNotes sortUnstableUsingFunction:compareTitleString];
[window setFrame:[self windowSizeForNotesFromSender:window] display:NO];
if (![window isVisible])
[window center];
[window makeKeyAndOrderFront:nil];
[NSApp cancelUserAttentionRequest:0];
}
- (void)removeDeletedNotes {
//for purposes of generating useful undo messages
if ([deletedNotes count] > 1) {
[notationController removeNotes:[[deletedNotes copy] autorelease]];
} else if ([deletedNotes count] == 1) {
[notationController removeNote:[deletedNotes lastObject]];
} else {
NSLog(@"No deleted notes?!");
}
}
- (void)cancelPanelReturningCode:(NSInteger)code {
[window close];
}
- (IBAction)changeConfirmDeletion:(id)sender {
[[notationController notationPrefs] setConfirmsFileDeletion:![confirmDeletionButton state]];
[[NSNotificationCenter defaultCenter] postNotificationName:NotationPrefsDidChangeNotification object:nil];
}
- (IBAction)deleteAction:(id)sender {
[self removeDeletedNotes];
}
- (IBAction)restoreAction:(id)sender {
//force-write the files
unsigned int i;
for (i=0; i<[deletedNotes count]; i++) {
[[deletedNotes objectAtIndex:i] makeNoteDirtyUpdateTime:NO updateFile:YES];
}
[notationController synchronizeNoteChanges:nil];
//force-synchronize directory to get notationcontroller to tell DeletionManager that the file now exists via updateForVerifiedExistingNote
//if restoring the file did not result in the dialog being dismissed, then it was not actually restored
[NSObject cancelPreviousPerformRequestsWithTarget:notationController selector:@selector(synchronizeNotesFromDirectory) object:nil];
[notationController synchronizeNotesFromDirectory];
}
- (BOOL)tableView:(NSTableView *)aTableView shouldEditTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex {
return NO;
}
- (BOOL)tableView:(NSTableView *)aTableView shouldSelectRow:(NSInteger)rowIndex {
return NO;
}
- (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex {
return filenameOfNote((NoteObject *)[deletedNotes objectAtIndex:rowIndex]);
}
- (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView {
return [deletedNotes count];
}
- (NSRect)windowWillUseStandardFrame:(NSWindow *)sender defaultFrame:(NSRect)defaultFrame {
return [self windowSizeForNotesFromSender:sender];
}
@end