forked from scrod/nv
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDeletionManager.m
executable file
·252 lines (189 loc) · 6.7 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
/*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 "NotationPrefs.h"
#import "GlobalPrefs.h"
#import "NSCollection_utils.h"
//class for managing notifications of external deletion of note files
@implementation DeletionManager
- (id)init {
if ([super init]) {
deletedNotes = [[NSMutableArray alloc] init];
needsToShowSheet = NO;
}
return self;
}
- (void)awakeFromNib {
//[window setMaxSize:NSMakeSize(371, 0)];
mainWindow = [[NSApp delegate] window];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(windowDidEndSheet:)
name:NSWindowDidEndSheetNotification object:mainWindow];
}
+ (DeletionManager *)sharedManager {
static DeletionManager *man = nil;
if (!man)
man = [[DeletionManager alloc] init];
return man;
}
- (void)dealloc {
[deletedNotes release];
[super dealloc];
}
- (void)setDelegate:(id)aDelegate {
delegate = aDelegate;
}
- (id)delegate {
return delegate;
}
- (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]) {
[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 _updateSheetForNotes];
}
}
}
- (void)addDeletedNote:(NoteObject*)aNote {
if (aNote) {
if (![deletedNotes count]) {
[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 _updateSheetForNotes];
}
//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];
}
}
- (void)_updateSheetForNotes {
[tableView reloadData];
[window setFrame:[self windowSizeForNotes] display:[window isVisible] animate:[window isVisible]];
}
- (void)processDeletedNotes {
if ([[[GlobalPrefs defaultPrefs] notationPrefs] confirmFileDeletion]) {
[self showSheetForDeletedNotes];
} else {
[self removeDeletedNotes];
}
}
- (NSRect)windowSizeForNotes {
float oldHeight = 0.0;
float newHeight = 0.0;
NSRect newFrame = [window 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 = [window frame].size.height - oldHeight + newHeight;
newFrame.origin.y = newFrame.origin.y + newFrame.size.height - newHeight;
newFrame.size.height = newHeight;
return newFrame;
}
- (void)showSheetForDeletedNotes {
if (!window) {
if (![NSBundle loadNibNamed:@"DeletionManager" owner:self]) {
NSLog(@"Failed to load DeletionManager.nib");
NSBeep();
return;
}
}
//sort notes by title
[deletedNotes sortUnstableUsingFunction:compareTitleString];
needsToShowSheet = YES;
[window setFrame:[self windowSizeForNotes] display:NO];
[NSApp beginSheet:window modalForWindow:mainWindow modalDelegate:self
didEndSelector:@selector(sheetDidEnd:returnCode:contextInfo:) contextInfo:NULL];
[NSApp cancelUserAttentionRequest:0];
if ([mainWindow attachedSheet] == window)
needsToShowSheet = NO;
}
- (void)sheetDidEnd:(NSWindow *)sheet returnCode:(int)returnCode contextInfo:(void *)contextInfo {
}
- (void)removeDeletedNotes {
//for purposes of generating useful undo messages
if ([deletedNotes count] > 1) {
if ([delegate respondsToSelector:@selector(removeNotes:)])
[delegate removeNotes:deletedNotes];
} else if ([deletedNotes count] == 1) {
if ([delegate respondsToSelector:@selector(removeNote:)])
[delegate removeNote:[deletedNotes lastObject]];
} else {
NSLog(@"No deleted notes?!");
}
[deletedNotes removeAllObjects];
}
- (IBAction)deleteAction:(id)sender {
[self removeDeletedNotes];
[NSApp endSheet:window returnCode:1];
[window close];
}
- (IBAction)restoreAction:(id)sender {
unsigned int i;
for (i=0; i<[deletedNotes count]; i++) {
[[deletedNotes objectAtIndex:i] makeNoteDirtyUpdateTime:NO updateFile:YES];
}
[delegate synchronizeNoteChanges:nil];
//cancel any file synchronization that's about to run after the sheet to make sure that it doesn't catch files before rewriting
[NSObject cancelPreviousPerformRequestsWithTarget:delegate selector:@selector(synchronizeNotesFromDirectory) object:nil];
[deletedNotes removeAllObjects];
[NSApp endSheet:window returnCode:0];
[window close];
}
- (void)windowDidEndSheet:(NSNotification *)aNotification {
if (needsToShowSheet) {
//we didn't show ourselves and we still need to--now's the time!
NSLog(@"trying to show sheet again");
[self showSheetForDeletedNotes];
}
}
- (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];
}
@end