Source

adium-gc / Frameworks / Adium Framework / Source / ESObjectWithProperties.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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
/* 
 * Adium is the legal property of its developers, whose names are listed in the copyright file included
 * with this source distribution.
 * 
 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU
 * General Public License as published by the Free Software Foundation; either version 2 of the License,
 * or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
 * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
 * Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with this program; if not,
 * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

#import <Adium/ESObjectWithProperties.h>
#import <AIUtilities/AIMutableOwnerArray.h>
#import <Adium/AIProxyListObject.h>

#import <objc/runtime.h>

@interface ESObjectWithProperties (AIPrivate)
- (void)_applyDelayedProperties:(NSDictionary *)infoDict;
- (id)_valueForProperty:(NSString *)key;
@end

/*!
 * @class ESObjectWithProperties
 * @brief Abstract superclass for objects with a system of properties and display arrays
 *
 * Properties are an abstracted NSMutableDictionary implementation with notification of changed
 * keys and optional delayed, grouped notification.  They allow storage of arbitrary information associate with
 * an ESObjectWithProperties subclass. Such information is not persistent across sessions.
 *
 * Properties are KVO compliant.
 *
 * Display arrays utilize AIMutableOwnerArray.  See its documentation in AIUtilities.framework.
 */
@implementation ESObjectWithProperties

- (void)clearProxyObjects
{
	for (AIProxyListObject *proxy in proxyObjects)
		[AIProxyListObject releaseProxyObject:proxy];
	[proxyObjects release]; proxyObjects = nil;	
}

/*!
 * @brief Deallocate
 */

- (void)finalize
{
  [self clearProxyObjects];
  [super finalize];
}

- (void)dealloc
{
	[self clearProxyObjects];

	[propertiesDictionary release]; propertiesDictionary = nil;
	[changedProperties release]; changedProperties = nil;
	[displayDictionary release]; displayDictionary = nil;

	[super dealloc];
}

//Setting properties ---------------------------------------------------------------------------------------------------
#pragma mark Setting Properties

/*!
 * @brief Set a property
 *
 * @param value The value
 * @param key The property to set the value to.
 * @param notify The notification timing. One of NotifyNow, NotifyLater, or NotifyNever.
 */
- (void)setValue:(id)value forProperty:(NSString *)key notify:(NotifyTiming)notify
{
    NSParameterAssert(key != nil);
    id oldValue = [self _valueForProperty:key];
    if (value == oldValue) { //No need to do all this work just to stay the same
        return;
    }
        
    [self willChangeValueForKey:key];
    	
	Ivar ivar = class_getInstanceVariable([self class], [key UTF8String]);
	
	// fall back to the dictionary
	if (ivar == NULL) {
		
		if (!propertiesDictionary && value) {
			// only allocate the dictionary when we're going to actually use it
			propertiesDictionary = [[NSMutableDictionary alloc] init];
		}
		
		if (value) {
			[propertiesDictionary setObject:value forKey:key];
		} else {
			[propertiesDictionary removeObjectForKey:key];
		}
		
	} else {
		const char *ivarType = ivar_getTypeEncoding(ivar);
		
		// check if it's a primitive type, if so, attempt to unwrap value
		if (ivarType[0] == _C_ID) {
			
			[oldValue release];
			object_setIvar(self, ivar, [value retain]);
			
		} else if (strcmp(ivarType, @encode(BOOL)) == 0) {
			
			BOOL bValue;
			
			if (value) {
				bValue = [value boolValue];
			} else {
				bValue = FALSE;
			}
			
			// must cast twice to avoid gcc's warnings..
			object_setIvar(self, ivar, (void *)(NSInteger)bValue);
			
		} else if (strcmp(ivarType, @encode(NSInteger)) == 0) {
			
			NSInteger iValue;
			
			if (value) {
				iValue = [value integerValue];
			} else {
				iValue = 0;
			}
			
			object_setIvar(self, ivar, (void *)iValue);
			
		} else if (strcmp(ivarType, @encode(int)) == 0) {
			
			int iValue;
			
			if (value) {
				iValue = [(NSNumber *)value intValue];
			} else {
				iValue = 0;
			}
			
			object_setIvar(self, ivar, (void *)(NSInteger)iValue);
		}
	}
    
    [self object:self didChangeValueForProperty:key notify:notify];
    [self didChangeValueForKey:key];
}

/*!
 * @brief Set a property after a delay
 *
 * @param value The value
 * @param key The property to set the value to.
 * @param delay The delay until the change is made
 */
- (void)setValue:(id)value forProperty:(NSString *)key afterDelay:(NSTimeInterval)delay
{
	[self performSelector:@selector(_applyDelayedProperties:)
			   withObject:[NSDictionary dictionaryWithObjectsAndKeys:
				   key, KEY_KEY,
				   value, KEY_VALUE,
				   nil]
			   afterDelay:delay];
}

- (id)valueForUndefinedKey:(NSString *)inKey
{
	return [self valueForProperty:inKey];
}

/*!
 * @brief Perform a delayed property change
 *
 * Called as a result of -[ESObjectWithProperties setValue:forProperty:afterDelay:]
 */
- (void)_applyDelayedProperties:(NSDictionary *)infoDict
{
	id				object = [infoDict objectForKey:KEY_VALUE];
	NSString		*key = [infoDict objectForKey:KEY_KEY];
	
	[self setValue:object forProperty:key notify:NotifyNow];
}

/*!
 * @brief Notify of any property changes made with a NotifyTiming of NotifyLater
 *
 * @param silent YES if the notification should be marked as silent
 */
- (void)notifyOfChangedPropertiesSilently:(BOOL)silent
{
    if (changedProperties && [changedProperties count]) {
		//Clear changedProperties in case this status change invokes another, and we re-enter this code
		NSSet	*keys = changedProperties;
		changedProperties = nil;
		
		[self didModifyProperties:keys silent:silent];
		
		[self didNotifyOfChangedPropertiesSilently:silent];
		
		[keys release];
    }
}

//Getting properties ---------------------------------------------------------------------------------------------------
#pragma mark Getting Properties

@synthesize properties = propertiesDictionary;

/*!
 * @brief Compatibility class
 * @result A call to the private class here for safety's sake.
 */
- (id)valueForProperty:(NSString *)key
{
    return [self _valueForProperty:key];
}

/*!
 * @brief Value for a property
 * @result The value associated with the passed key, or nil if none has been set.
 */
- (id)_valueForProperty:(NSString *)key
{
	id ret = nil;
	id value = nil;
	
	Ivar ivar = object_getInstanceVariable(self, [key UTF8String], (void **)&value);
	
	if (ivar == NULL) {
		
		// no dictionary -> this property is certainly nil
		if (propertiesDictionary) {
			ret = [propertiesDictionary objectForKey:key];
		}
		
	} else {
		
		const char *ivarType = ivar_getTypeEncoding(ivar);
		
		// attempt to wrap it, if we know how
		if (strcmp(ivarType, @encode(BOOL)) == 0) {
			ret = [[[NSNumber alloc] initWithBool:(BOOL)(NSInteger)value] autorelease];
			
		} else if (strcmp(ivarType, @encode(NSInteger)) == 0) {
			ret = [[[NSNumber alloc] initWithInteger:(NSInteger)value] autorelease];
			
		} else if (strcmp(ivarType, @encode(int)) == 0) {
			ret = [[[NSNumber alloc] initWithInt:(int)(NSInteger)value] autorelease];
			
		} else if (ivarType[0] != _C_ID) {
			AILogWithSignature(@" *** This ivar is not an object but an %s! Should not use -valueForProperty: @\"%@\" ***", ivarType, key);
			
		} else {
			ret = [[value retain] autorelease];
		}
	}
	
    return ret;
}

/*!
 * @brief Integer for a property
 *
 * @result int value for key, or 0 if no value is set for key
 */
- (NSInteger)integerValueForProperty:(NSString *)key
{
	NSInteger ret = 0;
	
	Ivar ivar = class_getInstanceVariable([self class], [key UTF8String]);
	
	if (ivar == NULL) {
		NSNumber *number = [self numberValueForProperty:key];
		ret = number ? [number integerValue] : 0;
	} else {
		
		const char *ivarType = ivar_getTypeEncoding(ivar);
		
		if (strcmp(ivarType, @encode(NSInteger)) != 0) {
			AILogWithSignature(@"%@'s %@ ivar is not an NSInteger but an %s! Will attempt to cast, but should not use -integerValueForProperty: @\"%@\"", self, key, ivarType, key);
		}
		
		ret = (NSInteger)object_getIvar(self, ivar);
	}
	
    return ret;
}

- (int)intValueForProperty:(NSString *)key
{
	int ret = 0;
	
	Ivar ivar = class_getInstanceVariable([self class], [key UTF8String]);
	
	if (ivar == NULL) {
		NSNumber *number = [self numberValueForProperty:key];
		ret = number ? [number intValue] : 0;
	} else {
		
		const char *ivarType = ivar_getTypeEncoding(ivar);
		
		if (strcmp(ivarType, @encode(int)) != 0) {
			AILogWithSignature(@"%@'s %@ ivar is not an int but an %s! Will attempt to cast, but should not use -intValueForProperty: @\"%@\"", self, key, ivarType, key);
		}
		
		ret = (int)(NSInteger)object_getIvar(self, ivar);
	}
	
    return ret;
}

- (BOOL)boolValueForProperty:(NSString *)key
{
	BOOL ret = FALSE;
	
	Ivar ivar = class_getInstanceVariable([self class], [key UTF8String]);
	
	if (ivar == NULL) {
		NSNumber *number = [self numberValueForProperty:key];
		ret = number ? [number boolValue] : NO;
	} else {
		
		const char *ivarType = ivar_getTypeEncoding(ivar);
		
		if (strcmp(ivarType, @encode(BOOL)) != 0) {
			AILogWithSignature(@"%@'s %@ ivar is not an BOOL but an %s! Will attempt to cast, but should not use -boolValueForProperty: @\"%@\"", self, key, ivarType, key);
		}
		
		ret = (BOOL)(NSInteger)object_getIvar(self, ivar);
	}
	
    return ret;
}

/*!
 * @brief NSNumber value for a property
 *
 * @result The NSNumber for this key, or nil if no such key is set or the value is not an NSNumber
 */
- (NSNumber *)numberValueForProperty:(NSString *)key
{
	id obj = [self valueForProperty:key];
	return ((obj && [obj isKindOfClass:[NSNumber class]]) ? obj : nil);
}

//For Subclasses -------------------------------------------------------------------------------------------------------
#pragma mark For Subclasses

/*!
 * @brief Sublcasses should implement this method to take action when a property changes for this object or a contained one
 *
 * @param inObject An object, which may be this object or any object contained by this one
 * @param key The key
 * @param notify A NotifyTiming value determining when notification is desired
 */
- (void)object:(id)inObject didChangeValueForProperty:(NSString *)key notify:(NotifyTiming)notify 
{
	/* If the property changed for the same object receiving this method, we should send out a notification or note it for later.
	 * If we get passed another object, it's just an informative message which shouldn't be triggering notification.
	 */
	if (inObject == self) {
		switch (notify) {
			case NotifyNow: {
				//Send out the notification now
				[self didModifyProperties:[NSSet setWithObject:key]
								   silent:NO];
				break;
			}
			case NotifyLater: {
				//Add this key to changedStatusKeys for later notification 
				if (!changedProperties) changedProperties = [[NSMutableSet alloc] init];
				[changedProperties addObject:key];
				break;
			}
			case NotifyNever: break; //Take no notification action
		}
	}
}

/*!
 * @brief Subclasses should implement this method to respond to a change of a property.
 *
 * The subclass should post appropriate notifications at this time.
 *
 * @param keys The keys
 * @param silent YES indicates that this should not trigger 'noisy' notifications - it is appropriate for notifications as an account signs on and notes tons of contacts.
 */
- (void)didModifyProperties:(NSSet *)keys silent:(BOOL)silent {};


/*!
 * @brief Subclasses should implement this method to respond to a change of properties after notifications have been posted.
 *
 * @param silent YES indicates that this should not trigger 'noisy' notifications - it is appropriate for notifications as an account signs on and notes tons of contacts.
 */
- (void)didNotifyOfChangedPropertiesSilently:(BOOL)silent {};

//Dynamic Display------------------------------------------------------------------------------------------------------
#pragma mark Dynamic Display
//Access to the display arrays for this object.  Will alloc and init an array if none exists.
- (AIMutableOwnerArray *)displayArrayForKey:(NSString *)inKey
{
	if(!displayDictionary) {
		displayDictionary = [[NSMutableDictionary alloc] initWithCapacity:1];
	}
	
    AIMutableOwnerArray	*array = [displayDictionary objectForKey:inKey];
	
    if (!array) {
        array = [[AIMutableOwnerArray alloc] init];
		[array setDelegate:self];
        [displayDictionary setObject:array forKey:inKey];
		[array release];
    }
	
    return array;
}

//With create:YES, this is identical to displayArrayForKey:
//With create:NO, just perform the lookup and return either a mutableOwnerArray or nil
- (AIMutableOwnerArray *)displayArrayForKey:(NSString *)inKey create:(BOOL)create
{
	AIMutableOwnerArray	*array;
	
	if (create) {
		array = [self displayArrayForKey:inKey];
	} else {
		array = [displayDictionary objectForKey:inKey];
	}
	
	return array;
}

- (id)displayArrayObjectForKey:(NSString *)inKey
{
	return ([[displayDictionary objectForKey:inKey] objectValue]);
}

//A mutable owner array (one of our displayArrays) set an object
- (void)mutableOwnerArray:(AIMutableOwnerArray *)inArray didSetObject:(id)anObject withOwner:(id)inOwner priorityLevel:(float)priority
{
	
}

//Naming ---------------------------------------------------------------------------------------------------------------
#pragma mark Naming

//Subclasses should override this to provide a general display name
- (NSString *)displayName
{
	return @"";
}

//Subclasses should override this to provide an ID suitable for comparing using isEqual:
- (NSString *)internalObjectID
{
	return @"";
}

#pragma mark Proxy objects

/*!
 * @brief Return a set of all proxy objects currently alive for this object
 */
- (NSSet *)proxyObjects
{
	return proxyObjects;
}

/*!
 * @brief Note that a proxy object has been created for this object
 */
- (void)noteProxyObject:(id)proxyObject
{
	if (!proxyObjects) proxyObjects = [[NSMutableSet alloc] init];
	[proxyObjects addObject:proxyObject];
}

/*!
 * @brief Notify that a proxy object has been removed for this object
 */
- (void)removeProxyObject:(id)proxyObject
{
	if (proxyObject) {
		[AIProxyListObject releaseProxyObject:proxyObject];
		[proxyObjects removeObject:proxyObject];
	}
}

@end