-
Notifications
You must be signed in to change notification settings - Fork 56
/
MetaEdits.m
370 lines (328 loc) · 10.7 KB
/
MetaEdits.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
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
//
// MetaEdits.m
// MetaZ
//
// Created by Brian Olsen on 24/08/09.
// Copyright 2009 Maven-Group. All rights reserved.
//
#import <MetaZKit/MetaEdits.h>
#import <MetaZKit/MZMethodData.h>
#import <MetaZKit/MZConstants.h>
#import <MetaZKit/MZTag.h>
#import "PureMetaEdits.h"
//#import <MetaZKit/MetaChangeNotification.h>
@implementation MetaEdits
@synthesize undoManager;
@synthesize changes;
@synthesize provider;
@synthesize pure;
#pragma mark - initialization
- (id)initWithProvider:(id<MetaData>)aProvider {
self = [super init];
if(self)
{
undoManager = [[MetaEditsUndoManager alloc] init];
provider = [aProvider retain];
changes = [[NSMutableDictionary alloc] init];
pure = [[PureMetaEdits alloc] initWithEdits:self];
NSArray* tags = [aProvider providedTags];
for(MZTag* tag in tags)
{
NSString* key = [tag identifier];
[provider addObserver: self
forKeyPath: key
options: NSKeyValueObservingOptionPrior|NSKeyValueObservingOptionOld
context: nil];
NSString* changedKey = [key stringByAppendingString:@"Changed"];
[self addMethodGetterForKey:key ofType:1 withObjCType:[tag encoding]];
[self addMethodGetterForKey:changedKey withRealKey:key ofType:2 withObjCType:@encode(BOOL)];
[self addMethodSetterForKey:key ofType:3 withObjCType:[tag encoding]];
[self addMethodSetterForKey:changedKey withRealKey:key ofType:4 withObjCType:@encode(BOOL)];
}
}
return self;
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter]
postNotificationName:MZMetaEditsDeallocating
object:self];
NSArray* tags = [provider providedTags];
for(MZTag *tag in tags)
[provider removeObserver:self forKeyPath: [tag identifier]];
[pure dealloc];
[changes release];
[provider release];
[undoManager release];
[super dealloc];
}
- (id)owner
{
return [provider owner];
}
- (NSArray *)providedTags
{
return [provider providedTags];
}
- (NSString *)loadedFileName
{
return [provider loadedFileName];
}
- (NSString *)savedFileName
{
return [[[self loadedFileName] stringByDeletingLastPathComponent]
stringByAppendingPathComponent:[self fileName]];
}
- (NSString *)savedTempFileName
{
NSString* tempFile = [self savedFileName];
NSString* ext = [tempFile pathExtension];
tempFile = [[tempFile stringByDeletingPathExtension] stringByAppendingString:@"MetaZ"];
if(ext && [ext length] > 0)
return [tempFile stringByAppendingFormat:@".%@", ext];
return tempFile;
}
- (void)prepareForQueue
{
[provider prepareForQueue];
}
- (void)prepareFromQueue
{
[provider prepareFromQueue];
}
-(BOOL)changed
{
/*
int count = [changes objectForKey:MZFileNameTag] != nil ? 1 : 0;
return [changes count] > count;
*/
return [changes count] > 0;
}
-(id)getterValueForKey:(NSString *)aKey
{
id ret = [changes objectForKey:aKey];
if(ret != nil) // Changed
{
MZTag* tag = [MZTag tagForIdentifier:aKey];
return [tag convertObjectForRetrival:ret];
}
return [provider valueForKey:aKey]; //[provider performSelector:NSSelectorFromString(aKey)];
}
-(BOOL)getterChangedForKey:(NSString *)aKey
{
return [changes objectForKey:aKey] != nil;
}
-(void)setterChanged:(BOOL)aValue forKey:(NSString *)aKey
{
id oldValue = [changes objectForKey:aKey];
if(!aValue && oldValue!=nil)
{
[[[self undoManager] prepareWithInvocationTarget:self] setterValue:oldValue forKey:aKey];
MZTag* tag = [MZTag tagForIdentifier:aKey];
NSString* actionKey = [tag localizedName];
if(!actionKey)
actionKey = aKey;
if([[self undoManager] isUndoing])
{
[[self undoManager] setActionName:
[NSString stringWithFormat:
NSLocalizedString(@"Set %@", @"Undo set action"),
actionKey]];
}
else
{
[[self undoManager] setActionName:
[NSString stringWithFormat:
NSLocalizedString(@"Reverted %@", @"Undo reverted action"),
actionKey]];
}
if([changes count] == 1)
[self willChangeValueForKey:@"changed"];
NSString* changedKey = [aKey stringByAppendingString:@"Changed"];
[self willChangeValueForKey:changedKey];
[self willChangeValueForKey:aKey];
[pure willChangeValueForKey:aKey];
[changes removeObjectForKey:aKey];
[self didChangeValueForKey:aKey];
[pure didChangeValueForKey:aKey];
[self didChangeValueForKey:changedKey];
if([changes count] == 0)
[self didChangeValueForKey:@"changed"];
}
else if(aValue && oldValue==nil)
{
[[[self undoManager] prepareWithInvocationTarget:self] setterChanged:NO forKey:aKey];
MZTag* tag = [MZTag tagForIdentifier:aKey];
NSString* actionKey = [tag localizedName];
if(!actionKey)
actionKey = aKey;
[[self undoManager] setActionName:
[NSString stringWithFormat:
NSLocalizedString(@"Set %@", @"Undo set action"),
actionKey]];
if([changes count] == 0)
[self willChangeValueForKey:@"changed"];
NSString* changedKey = [aKey stringByAppendingString:@"Changed"];
[self willChangeValueForKey:changedKey];
[self willChangeValueForKey:aKey];
[pure willChangeValueForKey:aKey];
[self willStoreValueForKey:aKey];
oldValue = [self valueForKey:aKey]; //[self performSelector:NSSelectorFromString(aKey)];
oldValue = [tag convertObjectForStorage:oldValue];
[changes setObject:oldValue forKey:aKey];
[self didStoreValueForKey:aKey];
[self didChangeValueForKey:aKey];
[pure didChangeValueForKey:aKey];
[self didChangeValueForKey:changedKey];
if([changes count] == 1)
[self didChangeValueForKey:@"changed"];
}
}
-(void)setterValue:(id)aValue forKey:(NSString *)aKey
{
MZTag* tag = [MZTag tagForIdentifier:aKey];
id oldValue = [changes objectForKey:aKey];
aValue = [tag convertObjectForStorage:aValue];
id pureValue = [pure valueForKey:aKey];
pureValue = [tag convertObjectForStorage:pureValue];
BOOL pureModified = (pureValue != aValue) &&
!(pureValue && aValue && [pureValue isEqual:aValue]);
/*
id currentValue = [provider valueForKey:aKey];
currentValue = [tag convertObjectForStorage:currentValue];
// If reverted to old value
if([aValue isEqual:currentValue])
{
if(oldValue == nil)
return;
[self setterChanged:NO forKey:aKey];
return;
}
*/
if(oldValue==nil)
{
[[[self undoManager] prepareWithInvocationTarget:self] setterChanged:NO forKey:aKey];
NSString* actionKey = [tag localizedName];
if(!actionKey)
actionKey = aKey;
[[self undoManager] setActionName:
[NSString stringWithFormat:
NSLocalizedString(@"Set %@", @"Undo set action"),
actionKey]];
} else
{
[[[self undoManager] prepareWithInvocationTarget:self] setterValue:oldValue forKey:aKey];
NSString* actionKey = [tag localizedName];
if(!actionKey)
actionKey = aKey;
[[self undoManager] setActionName:
[NSString stringWithFormat:
NSLocalizedString(@"Changed %@", @"Undo changed action"),
actionKey]];
}
NSString* changedKey = [aKey stringByAppendingString:@"Changed"];
if(oldValue == nil)
{
[self willChangeValueForKey:changedKey];
if([changes count] == 0)
[self willChangeValueForKey:@"changed"];
}
[self willChangeValueForKey:aKey];
if(pureModified)
[pure willChangeValueForKey:aKey];
[changes setObject:aValue forKey:aKey];
[self didChangeValueForKey:aKey];
if(pureModified)
[pure didChangeValueForKey:aKey];
if(oldValue == nil)
{
[self didChangeValueForKey:changedKey];
if([changes count] == 1)
[self didChangeValueForKey:@"changed"];
}
}
-(void)handleDataForKey:(NSString *)aKey ofType:(NSUInteger)aType forInvocation:(NSInvocation *)anInvocation
{
if(aType == 1) // Get value
{
id ret = [self getterValueForKey:aKey];
[anInvocation setReturnObject:ret];
return;
}
if( aType == 2) // Get Changed Value
{
BOOL ret = [self getterChangedForKey:aKey];
[anInvocation setReturnValue: &ret];
return;
}
if(aType == 3) // Set Value
{
id value = [anInvocation argumentObjectAtIndex:2];
//[anInvocation getArgument:&value atIndex:2];
[self setterValue:value forKey:aKey];
return;
}
if(aType == 4) // Set Changed value
{
BOOL newChanged;
[anInvocation getArgument:&newChanged atIndex:2];
[self setterChanged:newChanged forKey:aKey];
return;
}
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if(object == provider)
{
id value = [changes objectForKey:keyPath];
if(value == nil)
{
NSNumber * prior = [change objectForKey:NSKeyValueChangeNotificationIsPriorKey];
if([prior boolValue])
[self willChangeValueForKey:keyPath];
else
[self didChangeValueForKey:keyPath];
}
}
}
- (BOOL)isEqual:(id)other
{
return [super isEqual:other];
}
#pragma mark - NSCoding implementation
- (id)initWithCoder:(NSCoder *)decoder
{
id aProvider;
if([decoder allowsKeyedCoding])
aProvider = [decoder decodeObjectForKey:@"provider"];
else
aProvider = [decoder decodeObject];
self = [self initWithProvider:aProvider];
if([decoder allowsKeyedCoding])
[changes addEntriesFromDictionary:[decoder decodeObjectForKey:@"changes"]];
else
[changes addEntriesFromDictionary:[decoder decodeObject]];
return self;
}
- (void)encodeWithCoder:(NSCoder *)encoder
{
if([encoder allowsKeyedCoding])
{
[encoder encodeObject:provider forKey:@"provider"];
[encoder encodeObject:changes forKey:@"changes"];
}
else
{
[encoder encodeObject:provider];
[encoder encodeObject:changes];
}
}
#pragma mark - NSCopying implementation
- (id)copyWithZone:(NSZone *)zone
{
id ret = [[provider copyWithZone:zone] autorelease];
MetaEdits* copy = [[MetaEdits allocWithZone:zone] initWithProvider:ret];
[copy->changes addEntriesFromDictionary:changes];
return copy;
}
@end