source: trunk/Cocoa/Pester/Source/PSMovieAlert.m@ 547

Last change on this file since 547 was 544, checked in by Nicholas Riley, 15 years ago

Don't keep QTMovies around (and corresponding files open) for pending alarms; we could run out of file handles this way.

File size: 4.2 KB
Line 
1//
2// PSMovieAlert.m
3// Pester
4//
5// Created by Nicholas Riley on Sat Oct 26 2002.
6// Copyright (c) 2002 Nicholas Riley. All rights reserved.
7//
8
9#import <QTKit/QTKit.h>
10#import <QuickTime/Movies.h>
11#import "PSMovieAlert.h"
12#import "PSMovieAlertController.h"
13#import "NSDictionary-NJRExtensions.h"
14#import "QTMovie-NJRExtensions.h"
15#import "BDAlias.h"
16
17// property list keys
18static NSString * const PLAlertRepetitions = @"times"; // NSString
19static NSString * const PLAlertAlias = @"alias"; // NSData
20
21@implementation PSMovieAlert
22
23+ (PSMovieAlert *)alertWithMovieFileAlias:(BDAlias *)anAlias repetitions:(unsigned short)numReps;
24{
25 return [[[self alloc] initWithMovieFileAlias: anAlias repetitions: numReps] autorelease];
26}
27
28- (id)initWithMovieFileAlias:(BDAlias *)anAlias repetitions:(unsigned int) numReps;
29{
30 if ( (self = [super init]) != nil) {
31 NSString *path = [anAlias fullPath];
32 if (path == nil) {
33 [self release];
34 [NSException raise: PSAlertCreationException format: NSLocalizedString(@"Can't locate media to play as alert.", "Exception message on PSMovieAlert initialization when alias doesn't resolve")];
35 }
36 alias = [anAlias retain];
37 repetitions = numReps;
38 QTMovie *movie = [[QTMovie alloc] initWithFile: path error: NULL];
39 if (movie == nil) {
40 [self release];
41 self = nil;
42 } else {
43 hasAudio = [movie NJR_hasAudio];
44 hasVideo = [movie NJR_hasVideo];
45
46 if (!hasAudio && !hasVideo) {
47 [self release]; self = nil;
48 }
49 }
50 [movie release];
51 }
52
53 return self;
54}
55
56- (BOOL)hasVideo;
57{
58 return hasVideo;
59}
60
61- (BOOL)requiresPesterFrontmost;
62{
63 return hasVideo;
64}
65
66- (QTMovie *)movie;
67{
68 NSString *path = [alias fullPath];
69 if (path == nil)
70 return nil;
71
72 return [[[QTMovie alloc] initWithFile: path error: NULL] autorelease];
73}
74
75- (BDAlias *)movieFileAlias;
76{
77 return alias;
78}
79
80- (unsigned short)repetitions;
81{
82 return repetitions;
83}
84
85- (void)dealloc;
86{
87 [alias release];
88
89 NSLog(@"PSMovieAlert dealloc: %@", self);
90 [super dealloc];
91}
92
93- (NSString *)description;
94{
95 return [NSString stringWithFormat: @"PSMovieAlert (%@%@): %@, repeats %hu times%@", hasAudio ? @"A" : @"", hasVideo ? @"V" : @"", [alias fullPath], repetitions, hasAudio && outputVolume != PSMediaAlertNoVolume ? [NSString stringWithFormat: @" at %.0f%% volume", outputVolume * 100] : @""];
96}
97
98- (void)triggerForAlarm:(PSAlarm *)alarm;
99{
100 [PSMovieAlertController controllerWithAlarm: alarm movieAlert: self];
101}
102
103- (NSAttributedString *)actionDescription;
104{
105 BOOL isStatic = [[self movie] NJR_isStatic];
106 NSMutableAttributedString *string = [[(isStatic ? @"Show " : @"Play ") small] mutableCopy];
107 NSString *kindString = nil, *name = [alias displayNameWithKindString: &kindString];
108 if (name == nil) name = NSLocalizedString(@"<<can't locate media file>>", "Movie alert description surrogate for media display name when alias doesn't resolve");
109 else [string appendAttributedString: [[NSString stringWithFormat: @"%@ ", kindString] small]];
110 [string appendAttributedString: [name underlined]];
111 if (repetitions > 1 && !isStatic) {
112 [string appendAttributedString: [[NSString stringWithFormat: @" %hu times", repetitions] small]];
113 }
114 if (hasAudio && outputVolume != kNoVolume) {
115 [string appendAttributedString: [[NSString stringWithFormat: @" at %.0f%% volume", outputVolume * 100] small]];
116 }
117 return [string autorelease];
118}
119
120#pragma mark property list serialization (Pester 1.1)
121
122- (NSDictionary *)propertyListRepresentation;
123{
124 NSMutableDictionary *plAlert = [[super propertyListRepresentation] mutableCopy];
125 [plAlert setObject: [NSNumber numberWithUnsignedShort: repetitions] forKey: PLAlertRepetitions];
126 [plAlert setObject: [alias aliasData] forKey: PLAlertAlias];
127 return [plAlert autorelease];
128}
129
130- (id)initWithPropertyList:(NSDictionary *)dict;
131{
132 if ( (self = [super initWithPropertyList: dict]) != nil)
133 [self initWithMovieFileAlias: [BDAlias aliasWithData: [dict objectForRequiredKey: PLAlertAlias]]
134 repetitions: [[dict objectForRequiredKey: PLAlertRepetitions] unsignedShortValue]];
135 return self;
136}
137
138@end
Note: See TracBrowser for help on using the repository browser.