-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathBSManagedDocument+SSYMetadata.m
149 lines (134 loc) · 5.92 KB
/
BSManagedDocument+SSYMetadata.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
#import "BSManagedDocument+SSYMetadata.h"
#import "NSManagedObjectContext+Cheats.h"
#import "SSYSqliter.h"
#import "NSObject+MoreDescriptions.h"
#import "NSDocument+SSYAutosaveBetter.h"
#import "NSError+DecodeCodes.h"
#import "NSError+MoreDescriptions.h"
@implementation BSManagedDocument (SSYMetadata)
+ (NSDictionary*)metadataAtPath:(NSString*)path {
NSError* error = nil ;
NSDictionary* metadata = nil ;
path = [BSManagedDocument storePathForDocumentPath:path];
SSYSqliter* sqliter = [[SSYSqliter alloc] initWithPath:path
error_p:&error] ;
if ([error involvesCode:SQLITE_ERROR domain:SSYSqliterErrorDomain]) {
// This will happen if the query "SELECT Z_PLIST FROM Z_METADATA"
// returned an error "no such table: Z_METADATA".
// Starting with BookMacster 1.20.5, we log it here and then
// do not return it up the call chain.
NSLog(@"Warning 928-2991 Opening %@ produced error: %@",
path,
[error deepSummary]) ;
error = nil ;
}
else if (sqliter) {
NSString* query = @"SELECT Z_PLIST FROM Z_METADATA" ;
NSData* data = [sqliter firstRowFromQuery:query
error:&error] ;
if ([error involvesCode:SQLITE_ERROR domain:SSYSqliterErrorDomain]) {
/* This will happen if the query "SELECT Z_PLIST FROM Z_METADATA"
returned an error "no such table: Z_METADATA". It occurs
expectedly when attempting to get metadata from Exids and
Settings files when creating a new document.
Starting with BookMacster 1.20.5, we log it or ignore it… */
#if 0
#if DEBUG
NSLog(@"Warning 928-2526 (shown in DEBUG builds only). Query:\n%@\nfrom %@\nproduced error:\n%@",
query,
path,
[error deepSummary]) ;
#else
#warning Ignoring Warning 928-2526 in Release builds.
#endif
#endif
// Ignore it.
error = nil ;
}
else {
if ([data isKindOfClass:[NSData class]]) {
NSError* plistError = nil ;
metadata = [NSPropertyListSerialization propertyListWithData:data
options:NSPropertyListImmutable
format:NULL
error:&plistError] ;
if (!metadata) {
error = [NSError errorWithDomain:SSYManagedObjectContextCheatsErrorDomain
code:315640
userInfo:[NSDictionary dictionaryWithObjectsAndKeys:
@"Could not deserialize metadata", NSLocalizedDescriptionKey,
plistError, NSUnderlyingErrorKey,
nil]] ;
}
else if (![metadata respondsToSelector:@selector(objectForKey:)]) {
error = [NSError errorWithDomain:SSYManagedObjectContextCheatsErrorDomain
code:315641
userInfo:[NSDictionary dictionaryWithObjectsAndKeys:
@"Could not get objects from metadata", NSLocalizedDescriptionKey,
[metadata className], @"Metadata Class",
nil]] ;
metadata = nil ;
}
}
}
}
[sqliter release] ;
if (error) {
error = [NSError errorWithDomain:SSYManagedObjectContextCheatsErrorDomain
code:315650
userInfo:[NSDictionary dictionaryWithObjectsAndKeys:
@"Could not pre-read metadata", NSLocalizedDescriptionKey,
error, NSUnderlyingErrorKey,
path, @"Path",
// key, @"Key", Removed in BookMacster 1.20.5
nil]] ;
}
return metadata ;
}
+ (id)metadataObjectForKey:(NSString*)key
path:(NSString*)path {
return [[self metadataAtPath:(NSString*)path] objectForKey:key] ;
}
- (id)metadataObjectForKey:(NSString*)key {
NSDictionary* metadata = [[self managedObjectContext] metadata1] ;
/* metadata could be nil if store has not been
ed yet. */
id answer = nil;
if (metadata) {
answer = [metadata objectForKey:key] ;
}
else {
// Probably the store has not been configured yet, so [[self managedObjectContext] store1]
// returns nil. Therefore, we are forced to get the metadata by cheating…
NSString* path = [[self fileURL] path] ;
if (path) {
answer = [[self class] metadataObjectForKey:key
path:path] ;
}
}
return answer ;
}
- (BOOL)setMetadataObject:(id)object
forKey:(NSString*)key
error_p:(NSError**)error_p {
NSError* error = nil ;
[[self managedObjectContext] setMetadata1Object:object
forKey:key
error_p:&error] ;
if (error) {
if ([error code] == SSYManagedObjectContextCheatsErrorStoreIsReadOnly) {
// This is expected when operating on store in Versions Browser
}
else {
error = nil ;
}
}
if (error && error_p) {
*error_p = error ;
}
return (error == nil);
}
- (void)addMetadata:(NSDictionary*)moreMetadata {
[[self managedObjectContext] addMetadata1:moreMetadata];
}
@end