Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

file 177 lines (145 sloc) 4.859 kb
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
//
// QSProxyObject.m
// Quicksilver
//
// Created by Alcor on 1/16/05.
// Copyright 2005 Blacktree. All rights reserved.
//

#import "QSProxyObject.h"
#import "QSRegistry.h"
#import "QSTypes.h"

#import "QSNotifications.h"
#import "QSObject_StringHandling.h"

@interface QSProxyObject ()
- (NSMutableDictionary*)proxyDict;
@end

@implementation QSProxyObject
+ (id)proxyWithDictionary:(NSDictionary*)dictionary {
    return [[[self alloc] initWithDictionary:dictionary] autorelease];
}

+ (id)proxyWithIdentifier:(NSString*)identifier {
    id obj = [QSObject objectWithIdentifier:identifier];
    if (!obj) {
        id rep = [[QSReg tableNamed:@"QSProxies"] objectForKey:identifier];
        if (rep)
            obj = [QSProxyObject objectWithDictionary:rep];
    }
    return obj;
}

- (id)initWithDictionary:(NSDictionary*)dictionary {
    self = [super init];
    if (self) {
        [[self proxyDict] setDictionary:dictionary];
        [self setPrimaryType:QSProxyType];
    }
    return self;
}

- (NSMutableDictionary*)proxyDict {
    id dict = [self objectForType:QSProxyType];
    if (!dict) [self setObject:(dict = [NSMutableDictionary dictionary])
                       forType:QSProxyType];
    return dict;
}

- (id)proxyObjectWithProviderClass:(NSString *)providerClass identifier:(NSString *)ident {
return nil;
}

- (NSObject <QSProxyObjectProvider> *)proxyProvider {
NSString *class = [[data objectForKey:QSProxyType] objectForKey:kQSProxyProviderClass];
return [QSReg getClassInstance:class];
}

- (QSObject*)proxyObject {
id proxy = nil;
if (proxy = [self objectForCache:QSProxyTargetCache])
return proxy;
    
    id provider = [self proxyProvider];
    proxy = [provider resolveProxyObject:self];
    
    if ([self isEqual:proxy]) return nil;
    
    //NSLog(@"Proxy: %@", proxy);
    if (proxy) {
        [self setObject:proxy forCache:QSProxyTargetCache];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(objectIconModified:) name:QSObjectIconModified object:proxy];
    }
    
    NSTimeInterval interval = 3.0f;
    
    if ([provider respondsToSelector:@selector(cacheTimeForProxy:)])
        interval = [[self proxyProvider] cacheTimeForProxy:self];
    
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)interval * NSEC_PER_SEC);
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        [self releaseProxy];
    });
return proxy;
}


- (void)releaseProxy {
//NSLog(@"release proxy");
    [[NSNotificationCenter defaultCenter] removeObserver:self name:QSObjectIconModified object:[cache objectForKey:QSProxyTargetCache]];
[cache removeObjectForKey:QSProxyTargetCache];
}

- (NSArray *)proxyTypes {
NSArray *array = [[data objectForKey:QSProxyType] objectForKey:kQSProxyTypes];
if (array) return array;
return [[self proxyProvider] typesForProxyObject:self];
}

- (BOOL)enabled {
    NSNumber *e = [[self proxyDict] objectForKey:@"enabled"];
    if (e)
        return [e boolValue];
    return YES;
}

- (BOOL)hasChildren {return YES;}

- (NSArray *)types {
return [self proxyTypes];
}

- (BOOL)bypassValidation {
return [[self proxyProvider] respondsToSelector:@selector(bypassValidation)] && [[self proxyProvider] bypassValidation];
}

- (QSObject *)resolvedObject {return [self proxyObject];}

- (NSString *)stringValue {
return [[self resolvedObject] stringValue];
}

- (BOOL)respondsToSelector:(SEL)aSelector {
if ([super respondsToSelector:aSelector]) return YES;
if ([[self resolvedObject] respondsToSelector:aSelector]) return YES;
return NO;
}

- (void)forwardInvocation:(NSInvocation *)invocation {
if ([[self resolvedObject] respondsToSelector:[invocation selector]])
[invocation invokeWithTarget:[self resolvedObject]];
else
[self doesNotRecognizeSelector:[invocation selector]];
}

- (NSMethodSignature*)methodSignatureForSelector:(SEL)sel {
NSMethodSignature *sig = [[self class] instanceMethodSignatureForSelector:sel];
if (sig) return sig;
return [[self resolvedObject] methodSignatureForSelector:sel];
}

- (void)objectIconModified:(NSNotification *)notif
{
    [self updateIcon:[[self proxyObject] icon]];
}

- (BOOL)isProxyObject
{
    return YES;
}

- (id)_safeObjectForType:(id)aKey {
    id object = [data objectForKey:aKey];
    if (!object) {
        object = [[self resolvedObject] _safeObjectForType:aKey];
    }
    return object;
}

- (BOOL)loadIcon
{
    NSString *namedIcon = [self objectForMeta:kQSObjectIconName];
    if (!namedIcon || [namedIcon isEqualToString:@"ProxyIcon"]) {
        // use the resolved object's icon instead
        QSObject *resolved = [self resolvedObject];
[resolved loadIcon];
NSImage *image = [resolved icon];
if (image) {
            [self setIconLoaded:YES];
[self setIcon:image];
return YES;
}
    }
    return [super loadIcon];
}
@end
Something went wrong with that request. Please try again.