/core/externals/update-engine/externals/gdata-objectivec-client/Source/Clients/YouTube/GDataYouTubeMediaElements.m

http://macfuse.googlecode.com/ · Objective C · 269 lines · 173 code · 57 blank · 39 comment · 4 complexity · 4caaa994501d3036d09b9a5e418b1f7e MD5 · raw file

  1. /* Copyright (c) 2008 Google Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. //
  16. // GDataYouTubeMediaElements.m
  17. //
  18. #if !GDATA_REQUIRE_SERVICE_INCLUDES || GDATA_INCLUDE_YOUTUBE_SERVICE
  19. #define GDATAYOUTUBEMEDIAELEMENTS_DEFINE_GLOBALS 1
  20. #import "GDataYouTubeMediaElements.h"
  21. #import "GDataYouTubeConstants.h"
  22. // yt:format attribute
  23. @interface GDataYouTubeFormatAttribute : GDataAttribute <GDataExtension>
  24. @end
  25. @implementation GDataYouTubeFormatAttribute
  26. + (NSString *)extensionElementURI { return kGDataNamespaceYouTube; }
  27. + (NSString *)extensionElementPrefix { return kGDataNamespaceYouTubePrefix; }
  28. + (NSString *)extensionElementLocalName { return @"format"; }
  29. @end
  30. @implementation GDataMediaContent (YouTubeExtensions)
  31. // media content with YouTube's addition of an integer format attribute,
  32. // like yt:format="1"
  33. - (NSNumber *)youTubeFormatNumber {
  34. NSString *str = [self attributeValueForExtensionClass:[GDataYouTubeFormatAttribute class]];
  35. NSNumber *number = [NSNumber numberWithInt:[str intValue]];
  36. return number;
  37. }
  38. - (void)setYouTubeFormatNumber:(NSNumber *)num {
  39. [self setAttributeValue:[num stringValue] forExtensionClass:[GDataYouTubeFormatAttribute class]];
  40. }
  41. @end
  42. // yt:name attribute
  43. @interface GDataYouTubeNameAttribute : GDataAttribute <GDataExtension>
  44. @end
  45. @implementation GDataYouTubeNameAttribute
  46. + (NSString *)extensionElementURI { return kGDataNamespaceYouTube; }
  47. + (NSString *)extensionElementPrefix { return kGDataNamespaceYouTubePrefix; }
  48. + (NSString *)extensionElementLocalName { return @"name"; }
  49. @end
  50. @implementation GDataMediaThumbnail (YouTubeExtensions)
  51. // media thumbnail with YouTube's addition of a name attribute,
  52. // like yt:name="default"
  53. - (NSString *)youTubeName {
  54. NSString *str = [self attributeValueForExtensionClass:[GDataYouTubeNameAttribute class]];
  55. return str;
  56. }
  57. - (void)setYouTubeName:(NSString *)str {
  58. [self setAttributeValue:str forExtensionClass:[GDataYouTubeNameAttribute class]];
  59. }
  60. @end
  61. // yt:country attribute
  62. @interface GDataYouTubeCountryAttribute : GDataAttribute <GDataExtension>
  63. @end
  64. @implementation GDataYouTubeCountryAttribute
  65. + (NSString *)extensionElementURI { return kGDataNamespaceYouTube; }
  66. + (NSString *)extensionElementPrefix { return kGDataNamespaceYouTubePrefix; }
  67. + (NSString *)extensionElementLocalName { return @"country"; }
  68. @end
  69. @implementation GDataMediaRating (YouTubeExtensions)
  70. // media rating with YouTube's addition of a country attribute,
  71. // like yt:country="USA"
  72. - (NSString *)youTubeCountry {
  73. NSString *str = [self attributeValueForExtensionClass:[GDataYouTubeCountryAttribute class]];
  74. return str;
  75. }
  76. - (void)setYouTubeCountry:(NSString *)str {
  77. [self setAttributeValue:str
  78. forExtensionClass:[GDataYouTubeCountryAttribute class]];
  79. }
  80. @end
  81. // type attribute extension to media credit (v2.0)
  82. @interface GDataYouTubeTypeAttribute : GDataAttribute <GDataExtension>
  83. @end
  84. @implementation GDataYouTubeTypeAttribute
  85. + (NSString *)extensionElementURI { return kGDataNamespaceYouTube; }
  86. + (NSString *)extensionElementPrefix { return kGDataNamespaceYouTubePrefix; }
  87. + (NSString *)extensionElementLocalName { return @"type"; }
  88. @end
  89. @implementation GDataMediaCredit (YouTubeExtensions)
  90. // media credit with YouTube's addition of a type attribute,
  91. // like yt:type="partner"
  92. - (NSString *)youTubeCreditType {
  93. NSString *str = [self attributeValueForExtensionClass:[GDataYouTubeTypeAttribute class]];
  94. return str;
  95. }
  96. - (void)setYouTubeCreditType:(NSString *)str {
  97. [self setAttributeValue:str
  98. forExtensionClass:[GDataYouTubeTypeAttribute class]];
  99. }
  100. @end
  101. @implementation GDataYouTubeMediaGroup
  102. // a media group with YouTube extensions
  103. - (void)addExtensionDeclarations {
  104. [super addExtensionDeclarations];
  105. [self addExtensionDeclarationForParentClass:[self class]
  106. childClasses:
  107. [GDataYouTubeAspectRatio class],
  108. [GDataYouTubeDuration class],
  109. [GDataYouTubePrivate class],
  110. [GDataYouTubeVideoID class],
  111. [GDataYouTubeUploadedDate class],
  112. nil];
  113. // add the yt:format attribute to GDataMediaContent
  114. [self addAttributeExtensionDeclarationForParentClass:[GDataMediaContent class]
  115. childClass:[GDataYouTubeFormatAttribute class]];
  116. // add the yt:country attribute to GDataMediaRating
  117. [self addAttributeExtensionDeclarationForParentClass:[GDataMediaRating class]
  118. childClass:[GDataYouTubeCountryAttribute class]];
  119. // add the yt:type attribute to GDataMediaCredit
  120. [self addAttributeExtensionDeclarationForParentClass:[GDataMediaCredit class]
  121. childClass:[GDataYouTubeTypeAttribute class]];
  122. // add the yt:name attribute to GDataMediaThumbnail
  123. [self addAttributeExtensionDeclarationForParentClass:[GDataMediaThumbnail class]
  124. childClass:[GDataYouTubeNameAttribute class]];
  125. }
  126. #if !GDATA_SIMPLE_DESCRIPTIONS
  127. - (NSMutableArray *)itemsForDescription {
  128. static struct GDataDescriptionRecord descRecs[] = {
  129. { @"duration", @"duration", kGDataDescValueLabeled },
  130. { @"videoID", @"videoID", kGDataDescValueLabeled },
  131. { @"aspectRatio", @"aspectRatio", kGDataDescValueLabeled },
  132. { @"uploaded", @"uploadedDate", kGDataDescValueLabeled },
  133. { @"private", @"isPrivate", kGDataDescBooleanPresent },
  134. { nil, nil, (GDataDescRecTypes)0 }
  135. };
  136. NSMutableArray *items = [super itemsForDescription];
  137. [self addDescriptionRecords:descRecs toItems:items];
  138. return items;
  139. }
  140. #endif
  141. #pragma mark -
  142. - (NSNumber *)duration {
  143. GDataYouTubeDuration *obj = [self objectForExtensionClass:[GDataYouTubeDuration class]];
  144. return [obj intNumberValue];
  145. }
  146. - (void)setDuration:(NSNumber *)num {
  147. GDataYouTubeDuration *obj = [GDataYouTubeDuration valueWithNumber:num];
  148. [self setObject:obj forExtensionClass:[GDataYouTubeDuration class]];
  149. }
  150. - (BOOL)isPrivate {
  151. GDataYouTubePrivate *obj = [self objectForExtensionClass:[GDataYouTubePrivate class]];
  152. return (obj != nil);
  153. }
  154. - (void)setIsPrivate:(BOOL)flag {
  155. if (flag) {
  156. GDataYouTubePrivate *private = [GDataYouTubePrivate implicitValue];
  157. [self setObject:private forExtensionClass:[GDataYouTubePrivate class]];
  158. } else {
  159. [self setObject:nil forExtensionClass:[GDataYouTubePrivate class]];
  160. }
  161. }
  162. // videoID available in v2.0
  163. - (NSString *)videoID {
  164. GDataYouTubeVideoID *obj;
  165. obj = [self objectForExtensionClass:[GDataYouTubeVideoID class]];
  166. return [obj stringValue];
  167. }
  168. - (void)setVideoID:(NSString *)str {
  169. GDataYouTubeVideoID *obj = [GDataYouTubeVideoID valueWithString:str];
  170. [self setObject:obj forExtensionClass:[GDataYouTubeVideoID class]];
  171. }
  172. // uploadedDate available in v2.0
  173. - (GDataDateTime *)uploadedDate {
  174. GDataYouTubeUploadedDate *obj;
  175. obj = [self objectForExtensionClass:[GDataYouTubeUploadedDate class]];
  176. return [obj dateTimeValue];
  177. }
  178. - (void)setUploadedDate:(GDataDateTime *)dateTime {
  179. GDataYouTubeUploadedDate *obj;
  180. obj = [GDataYouTubeUploadedDate valueWithDateTime:dateTime];
  181. [self setObject:obj forExtensionClass:[GDataYouTubeUploadedDate class]];
  182. }
  183. // aspectRatio available in v2.0
  184. - (NSString *)aspectRatio {
  185. GDataYouTubeAspectRatio *obj;
  186. obj = [self objectForExtensionClass:[GDataYouTubeAspectRatio class]];
  187. return [obj stringValue];
  188. }
  189. - (void)setAspectRatio:(NSString *)str {
  190. GDataYouTubeAspectRatio *obj = [GDataYouTubeAspectRatio valueWithString:str];
  191. [self setObject:obj forExtensionClass:[GDataYouTubeAspectRatio class]];
  192. }
  193. // convenience accessors
  194. - (GDataMediaThumbnail *)highQualityThumbnail {
  195. // the HQ thumbnail is the one lacking a time attribute
  196. NSArray *array = [self mediaThumbnails];
  197. GDataMediaThumbnail *obj = [GDataUtilities firstObjectFromArray:array
  198. withValue:nil
  199. forKeyPath:@"time"];
  200. return obj;
  201. }
  202. - (GDataMediaContent *)mediaContentWithFormatNumber:(NSInteger)formatNumber {
  203. NSArray *mediaContents = [self mediaContents];
  204. NSNumber *formatNum = [NSNumber numberWithInteger:formatNumber];
  205. GDataMediaContent *content;
  206. content = [GDataUtilities firstObjectFromArray:mediaContents
  207. withValue:formatNum
  208. forKeyPath:@"youTubeFormatNumber"];
  209. return content;
  210. }
  211. @end
  212. #endif // !GDATA_REQUIRE_SERVICE_INCLUDES || GDATA_INCLUDE_YOUTUBE_SERVICE