-
Notifications
You must be signed in to change notification settings - Fork 1
/
GBChannel.m
179 lines (136 loc) · 5.53 KB
/
GBChannel.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
// This file is part of hdhomerunner.
// hdhomerunner 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 3 of the License, or
// (at your option) any later version.
// hdhomerunner 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, see <http://www.gnu.org/licenses/>.
//
// GBChannel.m
// hdhomerunner
//
// Created by Gregory Barchard on 7/22/07.
// Copyright 2007 __MyCompanyName__. All rights reserved.
//
#import "GBChannel.h"
@implementation GBChannel
-(id)init{
if(self = [super init]){
properties = [[NSMutableDictionary alloc] initWithCapacity:0];
[properties setValue:@"New Channel" forKey:@"description"];
[properties setObject:[[NSNumber alloc] initWithInt:0] forKey:@"channel"];
[properties setObject:[[NSNumber alloc] initWithInt:0] forKey:@"program"];
status = [[NSDictionary alloc] initWithObjectsAndKeys:[[NSImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"offline" ofType:@"tiff"]], @"Offline",
[[NSImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"idle" ofType:@"tiff"]], @"Idle",
[[NSImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"playing" ofType:@"tiff"]], @"Playing",
[[NSImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"recording" ofType:@"tiff"]], @"Recording", nil];
status_key = [[NSString alloc] initWithString:@"Offline"];
tuners = [[NSMutableArray alloc] initWithCapacity:0];
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self selector: @selector(tunerWillPlayChannel:) name:@"GBTunerWillPlayChannel" object:nil];
[nc addObserver:self selector: @selector(tunerWillStopPlayingChannel:) name:@"GBTunerWillStopPlayingChannel" object:nil];
}
return self;
}
-(id)initWithDictionary:(NSDictionary *)newProperties{
[self init];
[self setProperties:newProperties];
return self;
}
- (id)copyWithZone:(NSZone *)zone{
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:[properties valueForKey:@"description"], @"description",
[properties objectForKey:@"channel"], @"channel",
[properties objectForKey:@"program"], @"program", nil];
GBChannel *copy = [[GBChannel alloc] initWithDictionary:dict];
return copy;
}
-(NSDictionary *)properties{
return properties;
}
-(void)setProperties:(NSDictionary *)newProperties{
[self willChangeValueForKey:@"properties"];
if([newProperties valueForKey:@"description"]){
[properties setValue:[newProperties valueForKey:@"description"] forKey:@"description"];
}
if([newProperties valueForKey:@"channel"]){
[properties setValue:[newProperties valueForKey:@"channel"] forKey:@"channel"];
}
if([newProperties valueForKey:@"program"]){
[properties setValue:[newProperties valueForKey:@"program"] forKey:@"program"];
}
[self didChangeValueForKey:@"properties"];
}
- (void)encodeWithCoder:(NSCoder *)encoder{
[encoder encodeObject:properties forKey:@"channel"];
}
- (id)initWithCoder:(NSCoder *)decoder{
[self init];
if([decoder containsValueForKey:@"channel"]){
[self setProperties:[decoder decodeObjectForKey:@"channel"]];
}
return self;
}
-(NSImage *)status{
return [status objectForKey:status_key];
}
-(void)setStatus:(NSString *)newStatusKey{
if(![newStatusKey isEqual:status_key] && [status valueForKey:newStatusKey]){
[self willChangeValueForKey:@"status"];
[status_key autorelease];
status_key = [newStatusKey copy];
//NSLog(@"set channel status = %@", status_key);
[self didChangeValueForKey:@"status"];
}
}
-(NSArray *)statusKeys{
return [status allKeys];
}
-(void)updateStatus{
//NSLog(@"updatestatus count %i", [tuners count]);
if([tuners count] > 0){
[self setStatus:@"Playing"];
} else {
[self setStatus:@"Offline"];
}
}
-(void)tunerWillStopPlayingChannel:(NSNotification *)notification{
//NSLog(@"channel got the message about stop playing");
GBChannel *tmp = [[notification userInfo]
objectForKey:@"channel"];
if([self isEqual:tmp] && [tuners containsObject:[notification object]]){
NSLog(@"we are stopping this channel %@", [[self properties] valueForKey:@"description"]);
[tuners removeObject:[notification object]];
[self updateStatus];
}
}
-(void)tunerWillPlayChannel:(NSNotification *)notification{
//NSLog(@"channel got the message about playing");
GBChannel *tmp = [[notification userInfo]
objectForKey:@"channel"];
if([self isEqual:tmp]){
NSLog(@"we are playing this channel %@", [[self properties] valueForKey:@"description"]);
[tuners addObject:[notification object]];
[self updateStatus];
}
}
-(BOOL)isEqual:(GBChannel *)obj{
BOOL result = NO;
//if([properties isEqual:[obj properties]]){
if([[properties objectForKey:@"channel"] isEqualToNumber:[[obj properties] objectForKey:@"channel"]] && [[properties objectForKey:@"program"] isEqualToNumber:[[obj properties] objectForKey:@"program"]]){
result = YES;
}
return result;
}
-(void)dealloc{
[[NSNotificationCenter defaultCenter] removeObserver: self];
[properties release];
[status release];
[tuners release];
[status_key release];
[super dealloc];
}
@end