Source

adium-av / Source / AIContactStatusColoringPlugin.m

Full commit
  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
/* 
 * 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 "AIAbstractListController.h"
#import <Adium/AIContactControllerProtocol.h>
#import "AIContactStatusColoringPlugin.h"
#import <Adium/AIInterfaceControllerProtocol.h>
#import "AIListThemeWindowController.h"
#import <AIUtilities/AIColorAdditions.h>
#import <AIUtilities/AIDictionaryAdditions.h>
#import <AIUtilities/AIMutableOwnerArray.h>
#import <Adium/AIListContact.h>
#import <Adium/AIChat.h>
#import <Adium/AIContentTyping.h>
#import <Adium/AIListObject.h>
#import <Adium/AIMetaContact.h>
#import <Adium/AIListBookmark.h>

@interface AIContactStatusColoringPlugin ()
- (void)addToFlashSet:(AIListObject *)inObject;
- (void)removeFromFlashSet:(AIListObject *)inObject;
- (void)_applyColorToContact:(AIListContact *)inObject;
@end

@implementation AIContactStatusColoringPlugin

#define OFFLINE_IMAGE_OPACITY	0.5f
#define FULL_IMAGE_OPACITY		1.0f
#define	OPACITY_REFRESH			0.2f

#define CONTACT_STATUS_COLORING_DEFAULT_PREFS	@"ContactStatusColoringDefaults"

- (void)installPlugin
{
    //init
    flashingListObjects = [[NSMutableSet alloc] init];
    awayColor = nil;
    idleColor = nil;
    signedOffColor = nil;
    signedOnColor = nil;
    typingColor = nil;
    unviewedContentColor = nil;
    onlineColor = nil;
    awayAndIdleColor = nil;
	offlineColor = nil;
	
    awayInvertedColor = nil;
    idleInvertedColor = nil;
    signedOffInvertedColor = nil;
    signedOnInvertedColor = nil;
    typingInvertedColor = nil;
    unviewedContentInvertedColor = nil;
    onlineInvertedColor = nil;
    awayAndIdleInvertedColor = nil;
	offlineInvertedColor = nil;
	
    awayLabelColor = nil;
    idleLabelColor = nil;
    signedOffLabelColor = nil;
    signedOnLabelColor = nil;
    typingLabelColor = nil;
    unviewedContentLabelColor = nil;
    onlineLabelColor = nil;
    awayAndIdleLabelColor = nil;
	offlineLabelColor = nil;
	
	offlineImageFading = NO;
	
		
	interestedKeysSet = [[NSSet alloc] initWithObjects:
						 KEY_TYPING, KEY_UNVIEWED_CONTENT,  @"listObjectStatusType", @"isIdle",
						 @"isOnline", @"signedOn",  @"signedOff", @"isMobile", nil];

	id<AIPreferenceController> preferenceController = adium.preferenceController;

    //Setup our preferences
    [preferenceController registerDefaults:[NSDictionary dictionaryNamed:CONTACT_STATUS_COLORING_DEFAULT_PREFS
																		forClass:[self class]]
										  forGroup:PREF_GROUP_LIST_THEME];
    
    //Observe preferences and list objects
	[preferenceController registerPreferenceObserver:self forGroup:PREF_GROUP_LIST_THEME];
	[preferenceController registerPreferenceObserver:self forGroup:PREF_GROUP_CONTACT_LIST];
	[[AIContactObserverManager sharedManager] registerListObjectObserver:self];
}

- (void)uninstallPlugin
{
	[adium.preferenceController unregisterPreferenceObserver:self];
	[[AIContactObserverManager sharedManager] unregisterListObjectObserver:self];
	[adium.interfaceController unregisterFlashObserver:self];	
}

- (void)dealloc
{
	[flashingListObjects release];
	[interestedKeysSet release];
	
	[super dealloc];
}

- (NSSet *)updateListObject:(AIListObject *)inObject keys:(NSSet *)inModifiedKeys silent:(BOOL)silent
{
    NSSet		*modifiedAttributes = nil;

	if ([inObject isKindOfClass:[AIListContact class]] || [inObject isKindOfClass:[AIListBookmark class]]) {
		if (inModifiedKeys == nil || [inModifiedKeys intersectsSet:interestedKeysSet]) {
			//Update the contact's text color
			[self _applyColorToContact:(AIListContact *)inObject];
			modifiedAttributes = [NSSet setWithObjects:@"textColor", @"invertedTextColor", @"labelColor", nil];
		}
		
		//Update our flash set
		if (flashUnviewedContentEnabled &&
			(inModifiedKeys == nil || [inModifiedKeys containsObject:KEY_UNVIEWED_CONTENT])) {
			NSInteger unviewedContent = [inObject integerValueForProperty:KEY_UNVIEWED_CONTENT];
			
			if (unviewedContent && ![flashingListObjects containsObject:inObject]) { //Start flashing
				[self addToFlashSet:inObject];
			} else if (!unviewedContent && [flashingListObjects containsObject:inObject]) { //Stop flashing
				[self removeFromFlashSet:inObject];
			}
		}
	}

    return modifiedAttributes;
}

//Applies the correct color to the passed object
- (void)_applyColorToContact:(AIListContact *)inContact
{
    NSColor			*color = nil, *invertedColor = nil, *labelColor = nil;
    NSInteger				unviewedContent;
	CGFloat			opacity = FULL_IMAGE_OPACITY;
	BOOL			isEvent = NO;

    //Prefetch the value for unviewed content, we need it multiple times below
    unviewedContent = [inContact integerValueForProperty:KEY_UNVIEWED_CONTENT];

    //Unviewed content
    if ((!color && !labelColor) && (unviewedContentEnabled && unviewedContent)) {
		/* Use the unviewed content settings if:
		 *	- we aren't flashing or
		 *  - every other flash. */
        if (!flashUnviewedContentEnabled || ([adium.interfaceController flashState] % 2)) {
            color = unviewedContentColor;
            invertedColor = unviewedContentInvertedColor;
            labelColor = unviewedContentLabelColor;
			isEvent = YES;
        }
    }

    //Offline, Signed off, signed on, or typing
    if ((!color && !labelColor)) {
		if (offlineEnabled && (!inContact.online &&
							  ![inContact boolValueForProperty:@"signedOff"])) {
			color = offlineColor;
			invertedColor = offlineInvertedColor;
			labelColor = offlineLabelColor;
			if (offlineImageFading) opacity = OFFLINE_IMAGE_OPACITY;			
			
		} else if (signedOffEnabled && [inContact boolValueForProperty:@"signedOff"]) {
            color = signedOffColor;
            invertedColor = signedOffInvertedColor;
            labelColor = signedOffLabelColor;
			isEvent = YES;

        } else if (signedOnEnabled && [inContact boolValueForProperty:@"signedOn"]) {
			color = signedOnColor;
            invertedColor = signedOnInvertedColor;
            labelColor = signedOnLabelColor;
			isEvent = YES;
			
        } else if (typingEnabled && ([inContact intValueForProperty:KEY_TYPING] == AITyping)) {
            color = typingColor;
            invertedColor = typingInvertedColor;
            labelColor = typingLabelColor;
			isEvent = YES;
			
        }
    }

	if ((!color && !labelColor) && mobileEnabled && inContact.isMobile) {
		color = mobileColor;
		invertedColor = mobileInvertedColor;
		labelColor = mobileLabelColor;		
	}

    if ((!color && !labelColor)) {
		AIStatusSummary statusSummary = [inContact statusSummary];

        //Idle And Away, Away, or Idle
        if (awayAndIdleEnabled && (statusSummary == AIAwayAndIdleStatus)) {
            color = awayAndIdleColor;
            invertedColor = awayAndIdleInvertedColor;
            labelColor = awayAndIdleLabelColor;
        } else if (awayEnabled && ((statusSummary == AIAwayStatus) || (statusSummary == AIAwayAndIdleStatus))) {
            color = awayColor;
            invertedColor = awayInvertedColor;
            labelColor = awayLabelColor;
        } else if (idleEnabled && ((statusSummary == AIIdleStatus) || (statusSummary == AIAwayAndIdleStatus))) {
            color = idleColor;
            invertedColor = idleInvertedColor;
            labelColor = idleLabelColor;
        }
    }

    //Online
    if ((!color && !labelColor) && onlineEnabled && inContact.online) {
        color = onlineColor;
        invertedColor = onlineInvertedColor;
        labelColor = onlineLabelColor;
    }

    //Apply the color and opacity
    [inContact setValue:color
			forProperty:@"textColor"
				 notify:NotifyNever];
    [inContact setValue:invertedColor
			forProperty:@"invertedTextColor"
				 notify:NotifyNever];
    [inContact setValue:labelColor
			forProperty:@"labelColor"
				 notify:NotifyNever];
	[inContact setValue:[NSNumber numberWithDouble:opacity]
			forProperty:@"imageOpacity"
				 notify:NotifyNever];
	[inContact setValue:[NSNumber numberWithBool:isEvent]
			forProperty:@"isEvent"
				 notify:NotifyNever];
}

//Flash all handles with unviewed content
- (void)flash:(int)value
{
    AIListContact	*object;

	NSSet *modifiedKeys = [NSSet setWithObjects:@"Text Color", @"Label Color", @"Inverted Text Color", nil];
	
    for (object in flashingListObjects) {
        [self _applyColorToContact:object];
        
		[[AIContactObserverManager sharedManager] listObjectAttributesChanged:object
																 modifiedKeys:modifiedKeys];
    }
}

/*!
 * @brief Add a handle to the flash set
 */
- (void)addToFlashSet:(AIListObject *)inObject
{
    //Ensure that we're observing the flashing
    if ([flashingListObjects count] == 0) {
        [adium.interfaceController registerFlashObserver:self];
    }

    //Add the contact to our flash set
    [flashingListObjects addObject:inObject];
    [self flash:[adium.interfaceController flashState]];
}

/*!
 * @brief Remove a contact from the flash set
 */
- (void)removeFromFlashSet:(AIListObject *)inObject
{
    //Remove the contact from our flash set
    [flashingListObjects removeObject:inObject];

    //If we have no more flashing contacts, stop observing the flashes
    if ([flashingListObjects count] == 0) {
        [adium.interfaceController unregisterFlashObserver:self];
    }
}

- (void)preferencesChangedForGroup:(NSString *)group key:(NSString *)key
							object:(AIListObject *)object preferenceDict:(NSDictionary *)prefDict firstTime:(BOOL)firstTime
{
	if ([group isEqualToString:PREF_GROUP_LIST_THEME]) {
		//Release the old values..
		[signedOffColor release]; signedOffColor = nil;
		[signedOnColor release]; signedOnColor = nil;
		[awayColor release]; awayColor = nil;
		[idleColor release]; idleColor = nil;
		[typingColor release]; typingColor = nil;
		[unviewedContentColor release]; unviewedContentColor = nil;
		[onlineColor release]; onlineColor = nil;
		[awayAndIdleColor release]; awayAndIdleColor = nil;
		[offlineColor release]; offlineColor = nil;
		[mobileColor release]; mobileColor = nil;
		
		[signedOffInvertedColor release]; signedOffInvertedColor = nil;
		[signedOnInvertedColor release]; signedOnInvertedColor = nil;
		[awayInvertedColor release]; awayInvertedColor = nil;
		[idleInvertedColor release]; idleInvertedColor = nil;
		[typingInvertedColor release]; typingInvertedColor = nil;
		[unviewedContentInvertedColor release]; unviewedContentInvertedColor = nil;
		[onlineInvertedColor release]; onlineInvertedColor = nil;
		[awayAndIdleInvertedColor release]; awayAndIdleInvertedColor = nil;
		[offlineInvertedColor release]; offlineInvertedColor = nil;
		[mobileInvertedColor release]; mobileInvertedColor = nil;
		
		[awayLabelColor release]; awayLabelColor = nil;
		[idleLabelColor release]; idleLabelColor = nil;
		[signedOffLabelColor release]; signedOffLabelColor = nil;
		[signedOnLabelColor release]; signedOnLabelColor = nil;
		[typingLabelColor release]; typingLabelColor = nil;
		[unviewedContentLabelColor release]; unviewedContentLabelColor = nil;
		[onlineLabelColor release]; onlineLabelColor = nil;
		[awayAndIdleLabelColor release]; awayAndIdleLabelColor = nil;
		[offlineLabelColor release]; offlineLabelColor = nil;
		[mobileLabelColor release]; mobileLabelColor = nil;
		
		if ((awayEnabled = [[prefDict objectForKey:KEY_AWAY_ENABLED] boolValue])) {
			awayColor = [[[prefDict objectForKey:KEY_AWAY_COLOR] representedColor] retain];
			awayLabelColor = [[[prefDict objectForKey:KEY_LABEL_AWAY_COLOR] representedColor] retain];
			awayInvertedColor = [[awayColor colorWithInvertedLuminance] retain];
		}
	
		if ((idleEnabled = [[prefDict objectForKey:KEY_IDLE_ENABLED] boolValue])) {
			idleColor = [[[prefDict objectForKey:KEY_IDLE_COLOR] representedColor] retain];
			idleLabelColor = [[[prefDict objectForKey:KEY_LABEL_IDLE_COLOR] representedColor] retain];
			idleInvertedColor = [[idleColor colorWithInvertedLuminance] retain];
		}

		if ((signedOnEnabled = [[prefDict objectForKey:KEY_SIGNED_ON_ENABLED] boolValue])) {
			signedOnColor = [[[prefDict objectForKey:KEY_SIGNED_ON_COLOR] representedColor] retain];	
			signedOnLabelColor = [[[prefDict objectForKey:KEY_LABEL_SIGNED_ON_COLOR] representedColor] retain];
			signedOnInvertedColor = [[signedOnColor colorWithInvertedLuminance] retain];
		}
		
		if ((signedOffEnabled = [[prefDict objectForKey:KEY_SIGNED_OFF_ENABLED] boolValue])) {
			signedOffColor = [[[prefDict objectForKey:KEY_SIGNED_OFF_COLOR] representedColor] retain];
			signedOffLabelColor = [[[prefDict objectForKey:KEY_LABEL_SIGNED_OFF_COLOR] representedColor] retain];
			signedOffInvertedColor = [[signedOffColor colorWithInvertedLuminance] retain];
		}		
		
		if ((typingEnabled = [[prefDict objectForKey:KEY_TYPING_ENABLED] boolValue])) {
			typingColor = [[[prefDict objectForKey:KEY_TYPING_COLOR] representedColor] retain];
			typingLabelColor = [[[prefDict objectForKey:KEY_LABEL_TYPING_COLOR] representedColor] retain];			
			typingInvertedColor = [[typingColor colorWithInvertedLuminance] retain];
		}
		
		if ((unviewedContentEnabled = [[prefDict objectForKey:KEY_UNVIEWED_ENABLED] boolValue])) {
			unviewedContentColor = [[[prefDict objectForKey:KEY_UNVIEWED_COLOR] representedColor] retain];
			unviewedContentLabelColor = [[[prefDict objectForKey:KEY_LABEL_UNVIEWED_COLOR] representedColor] retain];
			unviewedContentInvertedColor = [[unviewedContentColor colorWithInvertedLuminance] retain];			
		}
		
		if ((onlineEnabled = [[prefDict objectForKey:KEY_ONLINE_ENABLED] boolValue])) {
			onlineColor = [[[prefDict objectForKey:KEY_ONLINE_COLOR] representedColor] retain];
			onlineLabelColor = [[[prefDict objectForKey:KEY_LABEL_ONLINE_COLOR] representedColor] retain];
			onlineInvertedColor = [[onlineColor colorWithInvertedLuminance] retain];
		}

		if ((awayAndIdleEnabled = [[prefDict objectForKey:KEY_IDLE_AWAY_ENABLED] boolValue])) {
			awayAndIdleColor = [[[prefDict objectForKey:KEY_IDLE_AWAY_COLOR] representedColor] retain];
			awayAndIdleLabelColor = [[[prefDict objectForKey:KEY_LABEL_IDLE_AWAY_COLOR] representedColor] retain];
			awayAndIdleInvertedColor = [[awayAndIdleColor colorWithInvertedLuminance] retain];			
		}
		
		if ((offlineEnabled = [[prefDict objectForKey:KEY_OFFLINE_ENABLED] boolValue])) {
			offlineColor = [[[prefDict objectForKey:KEY_OFFLINE_COLOR] representedColor] retain];
			offlineLabelColor = [[[prefDict objectForKey:KEY_LABEL_OFFLINE_COLOR] representedColor] retain];
			offlineInvertedColor = [[offlineColor colorWithInvertedLuminance] retain];			
		}
		
		if ((mobileEnabled = [[prefDict objectForKey:KEY_MOBILE_ENABLED] boolValue])) {
			mobileColor = [[[prefDict objectForKey:KEY_MOBILE_COLOR] representedColor] retain];		
			mobileLabelColor = [[[prefDict objectForKey:KEY_LABEL_MOBILE_COLOR] representedColor] retain];
			mobileInvertedColor = [[mobileColor colorWithInvertedLuminance] retain];			
		}

		offlineImageFading = [[prefDict objectForKey:KEY_LIST_THEME_FADE_OFFLINE_IMAGES] boolValue];

		//Update all objects
		if (!firstTime) {
			[[AIContactObserverManager sharedManager] updateAllListObjectsForObserver:self];
		}

	} else if ([group isEqualToString:PREF_GROUP_CONTACT_LIST]) {
		BOOL oldFlashUnviewedContentEnabled = flashUnviewedContentEnabled;
		
		flashUnviewedContentEnabled = [[prefDict objectForKey:KEY_CL_FLASH_UNVIEWED_CONTENT] boolValue];

		if (oldFlashUnviewedContentEnabled && !flashUnviewedContentEnabled) {
			//Clear our flash set if we aren't flashing for unviewed content now but we were before
			for (AIListContact *listContact in [[flashingListObjects copy] autorelease]) {
				[self removeFromFlashSet:listContact];
			}
			
			//Make our colors end up right (if we were on an off-flash) by updating all list objects
			[[AIContactObserverManager sharedManager] updateAllListObjectsForObserver:self];
		} else if (!oldFlashUnviewedContentEnabled && flashUnviewedContentEnabled) {
			if (!firstTime) {
				//Update all list objects so we start flashing
				[[AIContactObserverManager sharedManager] updateAllListObjectsForObserver:self];
			}
		}
	}
}

@end