Created
June 22, 2010 10:21
-
-
Save yene/448288 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// MyApplication.m | |
// IdleTimer | |
// | |
// Created by Yannick Weiss on 21.06.10. | |
// Copyright 2010 __MyCompanyName__. All rights reserved. | |
// | |
#import "MyApplication.h" | |
#import "IdleTimerContainer.h" | |
@implementation MyApplication | |
- (id) init | |
{ | |
self = [super init]; | |
if (self != nil) { | |
[self resetValues]; | |
idleTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerInvocation) userInfo:nil repeats:YES]; | |
timerInvocations = [[NSMutableArray array] retain]; | |
} | |
return self; | |
} | |
- (void) dealloc | |
{ | |
[idleTimer release]; | |
[timerInvocations release]; | |
[super dealloc]; | |
} | |
- (void)sendEvent:(NSEvent *)anEvent; | |
{ | |
[super sendEvent:anEvent]; | |
// you can filter events here | |
[self resetValues]; | |
} | |
- (void)timerInvocation; | |
{ | |
elapsedSeconds++; | |
if (elapsedSeconds == 5 ) { | |
isUser5SecondsIdle = YES; | |
} | |
if (elapsedSeconds == 10 ) { | |
isUser10SecondsIdle = YES; | |
} | |
for (IdleTimerContainer *temp in timerInvocations) { | |
if (elapsedSeconds == temp.seconds) { | |
[temp perform]; | |
} | |
} | |
NSLog(@"Timer: %i", elapsedSeconds); | |
} | |
- (void)resetValues; | |
{ | |
isUserIdle = NO; | |
isUser5SecondsIdle = NO; | |
isUser10SecondsIdle = NO; | |
elapsedSeconds = 0; | |
NSLog(@"reset"); | |
} | |
- (void)addIdleTimerWithSeconds:(int)seconds target:(id)target selector:(SEL)selector; | |
{ | |
if (seconds <= 1) return; | |
IdleTimerContainer *foo = [[IdleTimerContainer alloc] initWithSeconds:seconds target:target selector:selector]; | |
[timerInvocations addObject:foo]; | |
[foo release]; | |
} | |
- (void)removeIdleTimerForTarget:(id)target; | |
{ | |
for (IdleTimerContainer *temp in timerInvocations) { | |
if ([target isEqual:temp.target]) { | |
[timerInvocations removeObject:temp]; | |
} | |
} | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment