This repository has been archived by the owner on May 24, 2019. It is now read-only.
forked from scrod/nv
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ExporterManager.m
executable file
·159 lines (129 loc) · 7.18 KB
/
ExporterManager.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
/*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 "ExporterManager.h"
#import "NoteObject.h"
#import "NotationPrefs.h"
#import "NSString_NV.h"
#import "GlobalPrefs.h"
@implementation ExporterManager
+ (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 = (NSSavePanel *)[sender window];
int storageFormat = [[formatSelectorPopup selectedItem] tag];
[panel setRequiredFileType:[NotationPrefs pathExtensionForFormat:storageFormat]];
}
- (void)exportPanelDidEnd:(NSSavePanel *)sheet returnCode:(int)returnCode contextInfo:(void *)contextInfo {
NSArray *notes = (NSArray *)contextInfo;
if (returnCode == NSFileHandlingPanelOKButton && notes) {
//write notes in chosen format
unsigned int i;
int result, storageFormat = [[formatSelectorPopup selectedItem] tag];
NSString *directory = nil, *filename = nil;
BOOL overwriteNotes = NO;
if ([sheet isKindOfClass:[NSOpenPanel class]]) {
directory = [sheet filename];
} else {
filename = [[sheet filename] lastPathComponent];
directory = [[sheet filename] stringByDeletingLastPathComponent];
NSAssert([notes count] == 1, @"We returned from a save panel with more than one note?!");
//user wanted us to overwrite this one--otherwise dialog would have been cancelled
if ([[NSFileManager defaultManager] fileExistsAtPath:[sheet filename]]) overwriteNotes = YES;
if ([filename compare:filenameOfNote([notes lastObject]) options:NSCaseInsensitiveSearch] != NSOrderedSame) {
//undo any POSIX-safe crap NSSavePanel gave us--otherwise FSCreateFileUnicode will fail
filename = [filename stringByReplacingOccurrencesOfString:@":" withString:@"/"];
}
}
FSRef directoryRef;
CFURLRef url = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, (CFStringRef)directory, kCFURLPOSIXPathStyle, true);
[(id)url autorelease];
if (!url || !CFURLGetFSRef(url, &directoryRef)) {
NSRunAlertPanel([NSString stringWithFormat:NSLocalizedString(@"The notes couldn't be exported because the directory quotemark%@quotemark couldn't be accessed.",nil),
[directory stringByAbbreviatingWithTildeInPath]], @"", NSLocalizedString(@"OK",nil), nil, nil);
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];
OSStatus err = [note exportToDirectoryRef:&directoryRef withFilename:filename usingFormat:storageFormat overwrite:overwriteNotes];
if (err == dupFNErr) {
//ask about overwriting
NSString *existingName = filename ? filename : filenameOfNote(note);
existingName = [[existingName stringByDeletingPathExtension] stringByAppendingPathExtension:[NotationPrefs pathExtensionForFormat:storageFormat]];
result = NSRunAlertPanel([NSString stringWithFormat:NSLocalizedString(@"A file named quotemark%@quotemark already exists.",nil), existingName],
NSLocalizedString(@"Replace its current contents with that of the note?", @"replace the file's contents?"),
NSLocalizedString(@"Replace",nil), NSLocalizedString(@"Don't Replace",nil), lastNote ? NSLocalizedString(@"Replace All",nil) : nil, nil);
if (result == NSAlertDefaultReturn || result == NSAlertOtherReturn) {
if (result == NSAlertOtherReturn) overwriteNotes = YES;
err = [note exportToDirectoryRef:&directoryRef withFilename:filename usingFormat:storageFormat overwrite:YES];
} else continue;
}
if (err != noErr) {
NSString *exportErrorTitleString = [NSString stringWithFormat:NSLocalizedString(@"The note quotemark%@quotemark couldn't be exported because %@.",nil),
titleOfNote(note), [NSString reasonStringFromCarbonFSError:err]];
if (!lastNote) {
NSRunAlertPanel(exportErrorTitleString, @"", NSLocalizedString(@"OK",nil), nil, nil, nil);
} else {
result = NSRunAlertPanel(exportErrorTitleString, NSLocalizedString(@"Continue exporting?", @"alert title for exporter interruption"),
NSLocalizedString(@"Continue", @"(exporting notes?)"), NSLocalizedString(@"Stop Exporting", @"(notes?)"), nil);
if (result != NSAlertDefaultReturn) break;
}
}
}
FNNotify(&directoryRef, kFNDirectoryModifiedMessage, kFNNoImplicitAllSubscription);
[notes release];
}
}
- (void)exportNotes:(NSArray*)notes forWindow:(NSWindow*)window {
if (!accessoryView) {
if (![NSBundle loadNibNamed:@"ExporterManager" owner:self]) {
NSLog(@"Failed to load ExporterManager.nib");
NSBeep();
return;
}
}
if ([notes count] == 1) {
NSSavePanel *savePanel = [NSSavePanel savePanel];
[savePanel setAccessoryView:accessoryView];
[savePanel setCanCreateDirectories:YES];
[savePanel setCanSelectHiddenExtension:YES];
[self formatSelectorChanged:formatSelectorPopup];
NSString *filename = filenameOfNote([notes lastObject]);
filename = [filename stringByDeletingPathExtension];
filename = [filename stringByAppendingPathExtension:[NotationPrefs pathExtensionForFormat:[[formatSelectorPopup selectedItem] tag]]];
[savePanel beginSheetForDirectory:nil file:filename modalForWindow:window modalDelegate:self
didEndSelector:@selector(exportPanelDidEnd:returnCode:contextInfo:) contextInfo:[notes retain]];
} else if ([notes count] > 1) {
NSOpenPanel *openPanel = [NSOpenPanel 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 %d notes will be exported",nil), [notes count]]];
[openPanel beginSheetForDirectory:nil file:nil types:nil modalForWindow:window modalDelegate:self
didEndSelector:@selector(exportPanelDidEnd:returnCode:contextInfo:) contextInfo:[notes retain]];
} else {
NSRunAlertPanel(NSLocalizedString(@"No notes were selected for exporting.",nil),
NSLocalizedString(@"You must select at least one note to export.",nil), NSLocalizedString(@"OK",nil), NULL, NULL);
}
}
@end