|
11 | 11 | @implementation CDEDropStoreViewController (Private)
|
12 | 12 |
|
13 | 13 | - (BOOL)isValidStoreAtURL:(NSURL *)URL error:(NSError **)error {
|
14 |
| - NSAssert(self.managedObjectModel != nil, @"We need a managed object model."); |
15 |
| - |
16 |
| - if(URL == nil) { |
17 |
| - NSLog(@"Drop has no URL."); |
18 |
| - return NO; |
19 |
| - } |
20 |
| - BOOL result = [self.managedObjectModel isCompatibleWithStoreAtURL:URL error_cde:error]; |
21 |
| - |
22 |
| - return result; |
| 14 | + NSAssert(self.managedObjectModel != nil, @"We need a managed object model."); |
| 15 | + |
| 16 | + if(URL == nil) { |
| 17 | + NSLog(@"Drop has no URL."); |
| 18 | + return NO; |
| 19 | + } |
| 20 | + BOOL result = [self.managedObjectModel isCompatibleWithStoreAtURL:URL error_cde:error]; |
| 21 | + |
| 22 | + return result; |
23 | 23 | }
|
24 | 24 |
|
25 | 25 | - (BOOL)dropIsValidStore:(id<NSDraggingInfo>)drop error:(NSError **)error {
|
26 |
| - NSAssert(self.managedObjectModel != nil, @"We need a managed object model."); |
27 |
| - |
28 |
| - NSPasteboard *pasteboard = [drop draggingPasteboard]; |
29 |
| - NSURL *URL = [NSURL URLFromPasteboard:pasteboard]; |
30 |
| - return [self isValidStoreAtURL:URL error:error]; |
| 26 | + NSAssert(self.managedObjectModel != nil, @"We need a managed object model."); |
| 27 | + |
| 28 | + NSPasteboard *pasteboard = [drop draggingPasteboard]; |
| 29 | + NSURL *URL = [NSURL URLFromPasteboard:pasteboard]; |
| 30 | + return [self isValidStoreAtURL:URL error:error]; |
31 | 31 | }
|
32 | 32 |
|
33 | 33 | @end
|
34 | 34 |
|
35 | 35 | @implementation CDEDropStoreViewController
|
36 | 36 |
|
37 | 37 | - (void)setValidStoreURL:(NSURL *)newValue {
|
38 |
| - if(newValue != _validStoreURL) { |
39 |
| - _validStoreURL = [newValue copy]; |
40 |
| - [self view]; |
41 |
| - [self.pathActionLabelController setPath:self.validStoreURL.path]; |
42 |
| - [self.dropZoneView setURL:self.validStoreURL]; |
43 |
| - } |
| 38 | + if(newValue != _validStoreURL) { |
| 39 | + _validStoreURL = [newValue copy]; |
| 40 | + [self view]; |
| 41 | + [self.pathActionLabelController setPath:self.validStoreURL.path]; |
| 42 | + [self.dropZoneView setURL:self.validStoreURL]; |
| 43 | + } |
44 | 44 | }
|
45 | 45 |
|
46 | 46 | - (void)setEditing:(BOOL)newValue {
|
47 |
| - _editing = newValue; |
48 |
| - [self view]; |
49 |
| - NSString *localizedInfoKey = @"DropStoreInfoTextWhenCreatingNewProject"; |
50 |
| - if(self.isEditing) { |
51 |
| - localizedInfoKey = @"DropStoreInfoTextWhenEditingProject"; |
52 |
| - } |
53 |
| - self.infoTextField.stringValue = NSLocalizedString(localizedInfoKey, nil); |
| 47 | + _editing = newValue; |
| 48 | + [self view]; |
| 49 | + NSString *localizedInfoKey = @"DropStoreInfoTextWhenCreatingNewProject"; |
| 50 | + if(self.isEditing) { |
| 51 | + localizedInfoKey = @"DropStoreInfoTextWhenEditingProject"; |
| 52 | + } |
| 53 | + self.infoTextField.stringValue = NSLocalizedString(localizedInfoKey, nil); |
54 | 54 | }
|
55 | 55 |
|
56 | 56 | #pragma mark Creating
|
57 | 57 | - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
|
58 |
| - self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; |
59 |
| - |
60 |
| - if(self) { |
61 |
| - self.validStoreURL = nil; |
62 |
| - self.delegate = nil; |
63 |
| - self.managedObjectModel = nil; |
64 |
| - self.editing = NO; |
65 |
| - } |
66 |
| - return self; |
| 58 | + self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; |
| 59 | + |
| 60 | + if(self) { |
| 61 | + self.validStoreURL = nil; |
| 62 | + self.delegate = nil; |
| 63 | + self.managedObjectModel = nil; |
| 64 | + self.editing = NO; |
| 65 | + } |
| 66 | + return self; |
67 | 67 | }
|
68 | 68 |
|
69 | 69 | - (id)init {
|
70 |
| - return [self initWithNibName:NSStringFromClass([self class]) bundle:nil]; |
| 70 | + return [self initWithNibName:NSStringFromClass([self class]) bundle:nil]; |
71 | 71 | }
|
72 | 72 |
|
73 | 73 | #pragma mark Actions
|
74 | 74 | - (IBAction)createNewStore:(id)sender {
|
75 |
| - NSSavePanel *panel = [NSSavePanel savePanel]; |
76 |
| - [panel beginSheetModalForWindow:self.view.window completionHandler:^(NSInteger result) { |
77 |
| - if(result != NSFileHandlingPanelOKButton) { |
78 |
| - return; |
79 |
| - } |
80 |
| - NSURL *URL = [panel URL]; |
81 |
| - |
82 |
| - NSURL *temp = [[NSURL fileURLWithPath:NSTemporaryDirectory()] URLByAppendingPathComponent:[[NSUUID UUID] UUIDString] isDirectory:YES]; |
83 |
| - NSFileManager *fileManager = [NSFileManager new]; |
84 |
| - NSError *error = nil; |
85 |
| - if([fileManager createDirectoryAtURL:temp withIntermediateDirectories:YES attributes:nil error:&error] == NO) { |
86 |
| - NSLog(@"Failed to create new store: %@", error); |
87 |
| - [[NSDocumentController sharedDocumentController] presentError:error]; |
88 |
| - return; |
89 |
| - } |
90 |
| - temp = [temp URLByAppendingPathComponent:[URL lastPathComponent] isDirectory:NO]; |
91 |
| - |
92 |
| - NSPersistentStoreCoordinator *coordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:self.managedObjectModel]; |
93 |
| - error = nil; |
94 |
| - NSPersistentStore *store = [coordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:temp options:nil error:&error]; |
95 |
| - if(store == nil) { |
96 |
| - [[NSDocumentController sharedDocumentController] presentError:error]; |
97 |
| - return; |
98 |
| - } |
99 |
| - error = nil; |
100 |
| - NSData *storeData = [NSData dataWithContentsOfURL:temp options:0 error:&error]; |
101 |
| - if(storeData == nil) { |
102 |
| - NSLog(@"Failed to create new store (read failed): %@", error); |
103 |
| - [[NSDocumentController sharedDocumentController] presentError:error]; |
104 |
| - return; |
105 |
| - } |
106 |
| - [storeData writeToURL:URL atomically:YES]; |
107 |
| - [self setValidStoreURL:URL]; |
108 |
| - [self.delegate dropStoreViewController:self didChangeStoreURL:self.validStoreURL]; |
109 |
| - }]; |
| 75 | + NSSavePanel *panel = [NSSavePanel savePanel]; |
| 76 | + NSInteger result = [panel runModal]; |
| 77 | + if(result != NSFileHandlingPanelOKButton) { |
| 78 | + return; |
| 79 | + } |
| 80 | + NSURL *URL = [panel URL]; |
| 81 | + |
| 82 | + NSURL *temp = [[NSURL fileURLWithPath:NSTemporaryDirectory()] URLByAppendingPathComponent:[[NSUUID UUID] UUIDString] isDirectory:YES]; |
| 83 | + NSFileManager *fileManager = [NSFileManager new]; |
| 84 | + NSError *error = nil; |
| 85 | + if([fileManager createDirectoryAtURL:temp withIntermediateDirectories:YES attributes:nil error:&error] == NO) { |
| 86 | + NSLog(@"Failed to create new store: %@", error); |
| 87 | + [[NSDocumentController sharedDocumentController] presentError:error]; |
| 88 | + return; |
| 89 | + } |
| 90 | + temp = [temp URLByAppendingPathComponent:[URL lastPathComponent] isDirectory:NO]; |
| 91 | + |
| 92 | + NSPersistentStoreCoordinator *coordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:self.managedObjectModel]; |
| 93 | + error = nil; |
| 94 | + NSPersistentStore *store = [coordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:temp options:nil error:&error]; |
| 95 | + if(store == nil) { |
| 96 | + [[NSDocumentController sharedDocumentController] presentError:error]; |
| 97 | + return; |
| 98 | + } |
| 99 | + error = nil; |
| 100 | + NSData *storeData = [NSData dataWithContentsOfURL:temp options:0 error:&error]; |
| 101 | + if(storeData == nil) { |
| 102 | + NSLog(@"Failed to create new store (read failed): %@", error); |
| 103 | + [[NSDocumentController sharedDocumentController] presentError:error]; |
| 104 | + return; |
| 105 | + } |
| 106 | + [storeData writeToURL:URL atomically:YES]; |
| 107 | + [self setValidStoreURL:URL]; |
| 108 | + [self.delegate dropStoreViewController:self didChangeStoreURL:self.validStoreURL]; |
110 | 109 | }
|
111 | 110 |
|
112 | 111 | #pragma mark CDEDropZoneViewDelegate
|
113 | 112 | - (NSString *)titleForDropZoneView:(CDEDropZoneView *)view {
|
114 |
| - return @"Drop your Persistent Store here.\n\nIf you don't have one click on \"Create Store File\"."; |
| 113 | + return @"Drop your Persistent Store here.\n\nIf you don't have one click on \"Create Store File\"."; |
115 | 114 | }
|
116 | 115 |
|
117 | 116 | - (NSDragOperation)dropZoneView:(CDEDropZoneView *)view validateDrop:(id<NSDraggingInfo>)info {
|
118 |
| - NSError *error = nil; |
119 |
| - BOOL isValid = [self dropIsValidStore:info error:&error]; |
120 |
| - if(isValid == NO) { |
121 |
| - self.dropZoneView.displayedError = error; |
122 |
| - return NSDragOperationNone; |
123 |
| - } |
124 |
| - return NSDragOperationEvery; |
| 117 | + NSError *error = nil; |
| 118 | + BOOL isValid = [self dropIsValidStore:info error:&error]; |
| 119 | + if(isValid == NO) { |
| 120 | + self.dropZoneView.displayedError = error; |
| 121 | + return NSDragOperationNone; |
| 122 | + } |
| 123 | + return NSDragOperationEvery; |
125 | 124 | }
|
126 | 125 |
|
127 | 126 | - (BOOL)dropZoneView:(CDEDropZoneView *)view acceptDrop:(id<NSDraggingInfo>)info {
|
128 |
| - NSError *error = nil; |
129 |
| - BOOL isValid = [self dropIsValidStore:info error:&error]; |
130 |
| - if(isValid == NO) { |
131 |
| - self.dropZoneView.displayedError = error; |
132 |
| - return NO; |
133 |
| - } |
134 |
| - return YES; |
| 127 | + NSError *error = nil; |
| 128 | + BOOL isValid = [self dropIsValidStore:info error:&error]; |
| 129 | + if(isValid == NO) { |
| 130 | + self.dropZoneView.displayedError = error; |
| 131 | + return NO; |
| 132 | + } |
| 133 | + return YES; |
135 | 134 | }
|
136 | 135 |
|
137 | 136 | - (void)dropZoneView:(CDEDropZoneView *)view didChangeURL:(NSURL *)newURL {
|
138 |
| - NSError *error = nil; |
139 |
| - BOOL validStore = [self isValidStoreAtURL:newURL error:&error]; |
140 |
| - if(validStore) { |
141 |
| - self.validStoreURL = newURL; |
142 |
| - self.dropZoneView.displayedError = nil; |
143 |
| - [self.delegate dropStoreViewController:self didChangeStoreURL:newURL]; |
144 |
| - } else { |
145 |
| - self.dropZoneView.displayedError = error; |
146 |
| - self.validStoreURL = nil; |
147 |
| - } |
| 137 | + NSError *error = nil; |
| 138 | + BOOL validStore = [self isValidStoreAtURL:newURL error:&error]; |
| 139 | + if(validStore) { |
| 140 | + self.validStoreURL = newURL; |
| 141 | + self.dropZoneView.displayedError = nil; |
| 142 | + [self.delegate dropStoreViewController:self didChangeStoreURL:newURL]; |
| 143 | + } else { |
| 144 | + self.dropZoneView.displayedError = error; |
| 145 | + self.validStoreURL = nil; |
| 146 | + } |
148 | 147 | }
|
149 | 148 |
|
150 | 149 | @end
|
0 commit comments