-
Notifications
You must be signed in to change notification settings - Fork 3
/
Document.m
115 lines (96 loc) · 2.48 KB
/
Document.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
@implementation Document
+(void)closeTransientIfNeeded
{
NSArray<Document*>* documents=NSDocumentController.sharedDocumentController.documents;
if(documents.count!=2)
{
return;
}
for(Document* document in documents)
{
if(document.fileURL||document.documentEdited)
{
continue;
}
[document.windowControllers.firstObject.window performClose:nil];
}
}
-(void)makeWindowControllers
{
if(self.fileURL)
{
Document.closeTransientIfNeeded;
}
[self addWindowController:WindowController.alloc.init.autorelease];
[self loadWithURL:self.fileURL];
}
-(void)loadWithURL:(NSURL*)url
{
NSURL* tempURL=getTempURL();
NSString* type;
if(url)
{
if(![NSFileManager.defaultManager copyItemAtURL:url toURL:tempURL error:nil])
{
alertAbort(@"copy error");
}
type=[NSDocumentController.sharedDocumentController typeForContentsOfURL:url error:nil];
}
else
{
if(![NSFileManager.defaultManager createFileAtPath:tempURL.path contents:nil attributes:nil])
{
alertAbort(@"touch error");
}
type=self.fileType;
}
self.xcodeDocument=getXcodeDocument(tempURL,type);
self.undoManager=self.xcodeDocument.undoManager;
[(WindowController*)self.windowControllers.lastObject replaceDocument:self];
}
-(BOOL)readFromURL:(NSURL*)url ofType:(NSString*)type error:(NSError**)error
{
return true;
}
-(BOOL)writeSafelyToURL:(NSURL*)url ofType:(NSString*)type forSaveOperation:(NSSaveOperationType)operation error:(NSError**)error
{
NSNumber* permissions=[NSFileManager.defaultManager attributesOfItemAtPath:url.path error:nil][NSFilePosixPermissions];
BOOL result=[self.xcodeDocument writeSafelyToURL:url ofType:type forSaveOperation:NSSaveToOperation error:error];
if(permissions)
{
[NSFileManager.defaultManager setAttributes:@{NSFilePosixPermissions:permissions} ofItemAtPath:url.path error:nil];
}
if(!self.fileURL)
{
[self loadWithURL:url];
}
return result;
}
-(void)amySave:(NSMenuItem*)sender
{
[self saveDocument:nil];
}
-(void)encodeRestorableStateWithCoder:(NSCoder*)coder
{
[super encodeRestorableStateWithCoder:coder];
[coder encodeBool:Delegate.shared.projectMode forKey:@"projectMode"];
}
-(void)restoreStateWithCoder:(NSCoder*)coder
{
[super restoreStateWithCoder:coder];
BOOL mode=[coder decodeBoolForKey:@"projectMode"];
if(mode!=Delegate.shared.projectMode)
{
Delegate.shared.projectMode=mode;
dispatch_async(dispatch_get_main_queue(),^()
{
WindowController.syncProjectMode;
});
}
}
-(void)dealloc
{
self.xcodeDocument=nil;
super.dealloc;
}
@end