Repository navigation
Expand file tree
/
Copy pathExporterManager.m
More file actions
executable file
·208 lines (176 loc) · 8.47 KB
/
Copy pathExporterManager.m
File metadata and controls
executable file
·208 lines (176 loc) · 8.47 KB
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
/*Copyright (c) 2010, Zachary Schneirov. All rights reserved.
This file is part of Notational Velocity.
Notational Velocity is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Notational Velocity is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Notational Velocity. If not, see <http://www.gnu.org/licenses/>. */
#import "ExporterManager.h"
#import "NoteObject.h"
#import "NotationPrefs.h"
#import "NSString_NV.h"
#import "GlobalPrefs.h"
#import "NSFileManager_NV.h"
@implementation ExporterManager
@synthesize currentPanel;
+ (ExporterManager *)sharedManager {
static ExporterManager *man = nil;
if (!man)
man = [[ExporterManager alloc] init];
return man;
}
- (void)awakeFromNib {
int storageFormat = [[[GlobalPrefs defaultPrefs] notationPrefs] notesStorageFormat];
[formatSelectorPopup selectItemWithTag:storageFormat];
}
- (IBAction)formatSelectorChanged:(id)sender {
NSSavePanel *panel = self.currentPanel;
if (!panel) return; // Panel not yet shown or already dismissed
NSInteger storageFormat = [[formatSelectorPopup selectedItem] tag];
[panel setAllowedFileTypes:@[[NotationPrefs pathExtensionForFormat:(int)storageFormat]]];
}
- (void)exportPanelDidEnd:(NSSavePanel *)panel returnCode:(NSModalResponse)returnCode contextInfo:(void *)contextInfo {
NSArray *notes = (NSArray *)contextInfo;
if (!notes || returnCode != NSModalResponseOK) {
return;
}
//write notes in chosen format
NSUInteger i;
NSInteger storageFormat = [[formatSelectorPopup selectedItem] tag];
NSURL *directoryURL = nil;
NSString *filename = nil;
BOOL overwriteNotes = NO;
if ([panel isKindOfClass:[NSOpenPanel class]]) {
directoryURL = [[(NSOpenPanel *)panel URLs] firstObject];
} else {
NSURL *fileURL = [panel URL];
if (!fileURL) {
NSBeep();
return;
}
filename = [fileURL lastPathComponent];
directoryURL = [fileURL URLByDeletingLastPathComponent];
NSAssert([notes count] == 1, @"We returned from a save panel with more than one note?!");
overwriteNotes = [fileURL checkResourceIsReachableAndReturnError:NULL];
}
NSURL *directoryReferenceURL = [[NSFileManager defaultManager] fileReferenceURLForURL:directoryURL error:NULL];
if (!directoryReferenceURL) {
NSBeep();
return;
}
//re-uniqify file names here (if [notes count] > 1)?
for (i=0; i<[notes count]; i++) {
BOOL lastNote = i != [notes count] - 1;
NoteObject *note = [notes objectAtIndex:i];
NSError *exportError = nil;
BOOL exported = [note exportToDirectoryURL:directoryReferenceURL withFilename:filename
usingFormat:(int)storageFormat overwrite:overwriteNotes error:&exportError];
if (!exported && [[exportError domain] isEqualToString:NSCocoaErrorDomain] &&
[exportError code] == NSFileWriteFileExistsError) {
//ask about overwriting
NSString *existingName = filename ? filename : filenameOfNote(note);
existingName = [[existingName stringByDeletingPathExtension] stringByAppendingPathExtension:[NotationPrefs pathExtensionForFormat:(int)storageFormat]];
NSAlert *alert = [[[NSAlert alloc] init] autorelease];
[alert setMessageText:[NSString stringWithFormat:NSLocalizedString(@"A file named quotemark%@quotemark already exists.", nil), existingName]];
[alert setInformativeText:NSLocalizedString(@"Replace its current contents with that of the note?", @"replace the file's contents?")];
[alert addButtonWithTitle:NSLocalizedString(@"Replace", nil)];
[alert addButtonWithTitle:NSLocalizedString(@"Don't Replace", nil)];
if (lastNote) {
[alert addButtonWithTitle:NSLocalizedString(@"Replace All", nil)];
}
NSModalResponse response = [alert runModal];
if (response == NSAlertFirstButtonReturn || response == NSAlertThirdButtonReturn) {
if (response == NSAlertThirdButtonReturn) overwriteNotes = YES;
exportError = nil;
exported = [note exportToDirectoryURL:directoryReferenceURL withFilename:filename
usingFormat:(int)storageFormat overwrite:YES error:&exportError];
} else {
continue;
}
}
if (!exported) {
NSString *exportErrorTitleString = [NSString stringWithFormat:NSLocalizedString(@"The note quotemark%@quotemark couldn't be exported because %@.",nil),
titleOfNote(note), [exportError localizedDescription]];
if (!lastNote) {
NSAlert *errorAlert = [[[NSAlert alloc] init] autorelease];
[errorAlert setMessageText:exportErrorTitleString];
[errorAlert setInformativeText:@""];
[errorAlert addButtonWithTitle:NSLocalizedString(@"OK", nil)];
[errorAlert runModal];
} else {
NSAlert *continueAlert = [[[NSAlert alloc] init] autorelease];
[continueAlert setMessageText:exportErrorTitleString];
[continueAlert setInformativeText:NSLocalizedString(@"Continue exporting?", @"alert title for exporter interruption")];
[continueAlert addButtonWithTitle:NSLocalizedString(@"Continue", @"(exporting notes?)")];
[continueAlert addButtonWithTitle:NSLocalizedString(@"Stop Exporting", @"(notes?)")];
NSModalResponse continueResponse = [continueAlert runModal];
if (continueResponse != NSAlertFirstButtonReturn) break;
}
}
}
}
- (void)exportNotes:(NSArray*)notes forWindow:(NSWindow*)window {
if (!accessoryView) {
NSArray *topLevelObjects = nil;
if (![[NSBundle mainBundle] loadNibNamed:@"ExporterManager" owner:self topLevelObjects:&topLevelObjects]) {
NSLog(@"Failed to load ExporterManager.nib");
NSBeep();
return;
}
[exporterTopLevelObjects release];
exporterTopLevelObjects = [topLevelObjects retain];
}
if ([notes count] == 1) {
NSSavePanel *savePanel = [NSSavePanel savePanel];
self.currentPanel = savePanel;
[savePanel setAccessoryView:accessoryView];
[savePanel setCanCreateDirectories:YES];
[savePanel setCanSelectHiddenExtension:YES];
NSInteger storageFormat = [[formatSelectorPopup selectedItem] tag];
[savePanel setAllowedFileTypes:@[[NotationPrefs pathExtensionForFormat:(int)storageFormat]]];
NSString *filename = filenameOfNote([notes lastObject]);
filename = [filename stringByDeletingPathExtension];
filename = [filename stringByAppendingPathExtension:[NotationPrefs pathExtensionForFormat:(int)[[formatSelectorPopup selectedItem] tag]]];
[savePanel setDirectoryURL:[[NSFileManager defaultManager] homeDirectoryForCurrentUser]];
[savePanel setNameFieldStringValue:filename];
NSArray *retainedNotes = [notes retain];
[savePanel beginSheetModalForWindow:window completionHandler:^(NSModalResponse response) {
[self exportPanelDidEnd:savePanel returnCode:response contextInfo:(void *)retainedNotes];
[retainedNotes release];
self.currentPanel = nil;
}];
} else if ([notes count] > 1) {
NSOpenPanel *openPanel = [NSOpenPanel openPanel];
self.currentPanel = openPanel;
[openPanel setAccessoryView:accessoryView];
[openPanel setCanCreateDirectories:YES];
[openPanel setCanChooseFiles:NO];
[openPanel setCanChooseDirectories:YES];
[openPanel setPrompt:NSLocalizedString(@"Export",@"title of button to export notes from folder selection dialog")];
[openPanel setTitle:NSLocalizedString(@"Export Notes", @"title of export notes dialog")];
[openPanel setMessage:[NSString stringWithFormat:NSLocalizedString(@"Choose a folder into which %lu notes will be exported",nil), (unsigned long)[notes count]]];
NSArray *retainedNotes = [notes retain];
[openPanel beginSheetModalForWindow:window completionHandler:^(NSModalResponse response) {
[self exportPanelDidEnd:openPanel returnCode:response contextInfo:(void *)retainedNotes];
[retainedNotes release];
self.currentPanel = nil;
}];
} else {
NSAlert *alert = [[[NSAlert alloc] init] autorelease];
[alert setMessageText:NSLocalizedString(@"No notes were selected for exporting.", nil)];
[alert setInformativeText:NSLocalizedString(@"You must select at least one note to export.", nil)];
[alert addButtonWithTitle:NSLocalizedString(@"OK", nil)];
[alert runModal];
}
}
- (void)dealloc {
[currentPanel release];
[exporterTopLevelObjects release];
[super dealloc];
}
@end