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
RevLine 
[34]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
[544]9#import <QTKit/QTKit.h>
[34]10#import <QuickTime/Movies.h>
11#import "PSMovieAlert.h"
12#import "PSMovieAlertController.h"
[53]13#import "NSDictionary-NJRExtensions.h"
[543]14#import "QTMovie-NJRExtensions.h"
[53]15#import "BDAlias.h"
[34]16
[53]17// property list keys
18static NSString * const PLAlertRepetitions = @"times"; // NSString
19static NSString * const PLAlertAlias = @"alias"; // NSData
20
[34]21@implementation PSMovieAlert
22
23+ (PSMovieAlert *)alertWithMovieFileAlias:(BDAlias *)anAlias repetitions:(unsigned short)numReps;
24{
[53]25 return [[[self alloc] initWithMovieFileAlias: anAlias repetitions: numReps] autorelease];
[34]26}
27
[53]28- (id)initWithMovieFileAlias:(BDAlias *)anAlias repetitions:(unsigned int) numReps;
[34]29{
30 if ( (self = [super init]) != nil) {
[53]31 NSString *path = [anAlias fullPath];
32 if (path == nil) {
33 [self release];
[103]34 [NSException raise: PSAlertCreationException format: NSLocalizedString(@"Can't locate media to play as alert.", "Exception message on PSMovieAlert initialization when alias doesn't resolve")];
[53]35 }
[34]36 alias = [anAlias retain];
37 repetitions = numReps;
[544]38 QTMovie *movie = [[QTMovie alloc] initWithFile: path error: NULL];
[34]39 if (movie == nil) {
40 [self release];
41 self = nil;
42 } else {
[543]43 hasAudio = [movie NJR_hasAudio];
44 hasVideo = [movie NJR_hasVideo];
[34]45
46 if (!hasAudio && !hasVideo) {
47 [self release]; self = nil;
48 }
49 }
[544]50 [movie release];
[34]51 }
52
53 return self;
54}
55
56- (BOOL)hasVideo;
57{
58 return hasVideo;
59}
60
[53]61- (BOOL)requiresPesterFrontmost;
62{
63 return hasVideo;
64}
65
[543]66- (QTMovie *)movie;
[34]67{
[544]68 NSString *path = [alias fullPath];
69 if (path == nil)
70 return nil;
71
72 return [[[QTMovie alloc] initWithFile: path error: NULL] autorelease];
[34]73}
74
[53]75- (BDAlias *)movieFileAlias;
76{
77 return alias;
78}
79
[34]80- (unsigned short)repetitions;
81{
82 return repetitions;
83}
84
85- (void)dealloc;
86{
87 [alias release];
[544]88
89 NSLog(@"PSMovieAlert dealloc: %@", self);
[34]90 [super dealloc];
91}
92
93- (NSString *)description;
94{
[544]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] : @""];
[34]96}
97
98- (void)triggerForAlarm:(PSAlarm *)alarm;
99{
100 [PSMovieAlertController controllerWithAlarm: alarm movieAlert: self];
101}
102
[53]103- (NSAttributedString *)actionDescription;
104{
[544]105 BOOL isStatic = [[self movie] NJR_isStatic];
[53]106 NSMutableAttributedString *string = [[(isStatic ? @"Show " : @"Play ") small] mutableCopy];
107 NSString *kindString = nil, *name = [alias displayNameWithKindString: &kindString];
[103]108 if (name == nil) name = NSLocalizedString(@"<<can't locate media file>>", "Movie alert description surrogate for media display name when alias doesn't resolve");
[53]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 }
[133]114 if (hasAudio && outputVolume != kNoVolume) {
115 [string appendAttributedString: [[NSString stringWithFormat: @" at %.0f%% volume", outputVolume * 100] small]];
116 }
[53]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{
[133]132 if ( (self = [super initWithPropertyList: dict]) != nil)
133 [self initWithMovieFileAlias: [BDAlias aliasWithData: [dict objectForRequiredKey: PLAlertAlias]]
134 repetitions: [[dict objectForRequiredKey: PLAlertRepetitions] unsignedShortValue]];
135 return self;
[53]136}
137
[34]138@end
Note: See TracBrowser for help on using the repository browser.