VLCCloudStorageTableViewCell.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*****************************************************************************
  2. * VLCCloudStorageTableViewCell.m
  3. * VLC for iOS
  4. *****************************************************************************
  5. * Copyright (c) 2013-2015 VideoLAN. All rights reserved.
  6. * $Id$
  7. *
  8. * Authors: Carola Nitz <nitz.carola # googlemail.com>
  9. * Felix Paul Kühne <fkuehne # videolan.org>
  10. *
  11. * Refer to the COPYING file of the official project for license.
  12. *****************************************************************************/
  13. #import "VLCCloudStorageTableViewCell.h"
  14. @interface VLCCloudStorageTableViewCell ()
  15. {
  16. NSURL *_iconURL;
  17. }
  18. @end
  19. @implementation VLCCloudStorageTableViewCell
  20. + (VLCCloudStorageTableViewCell *)cellWithReuseIdentifier:(NSString *)ident
  21. {
  22. NSArray *nibContentArray = [[NSBundle mainBundle] loadNibNamed:@"VLCCloudStorageTableViewCell" owner:nil options:nil];
  23. NSAssert([nibContentArray count] == 1, @"meh");
  24. NSAssert([[nibContentArray lastObject] isKindOfClass:[VLCCloudStorageTableViewCell class]], @"meh meh");
  25. VLCCloudStorageTableViewCell *cell = (VLCCloudStorageTableViewCell *)[nibContentArray lastObject];
  26. return cell;
  27. }
  28. - (void)setDropboxFile:(DBMetadata *)dropboxFile
  29. {
  30. if (dropboxFile != _dropboxFile)
  31. _dropboxFile = dropboxFile;
  32. [self performSelectorOnMainThread:@selector(_updatedDisplayedInformation)
  33. withObject:nil waitUntilDone:NO];
  34. }
  35. #if TARGET_OS_IOS
  36. - (void)setDriveFile:(GTLDriveFile *)driveFile
  37. {
  38. if (driveFile != _driveFile)
  39. _driveFile = driveFile;
  40. [self performSelectorOnMainThread:@selector(_updatedDisplayedInformation)
  41. withObject:nil waitUntilDone:NO];
  42. }
  43. - (void)setBoxFile:(BoxItem *)boxFile
  44. {
  45. if (boxFile != _boxFile)
  46. _boxFile = boxFile;
  47. [self performSelectorOnMainThread:@selector(_updatedDisplayedInformation)
  48. withObject:nil waitUntilDone:NO];
  49. }
  50. - (void)setOneDriveFile:(VLCOneDriveObject *)oneDriveFile
  51. {
  52. if (oneDriveFile != _oneDriveFile)
  53. _oneDriveFile = oneDriveFile;
  54. [self performSelectorOnMainThread:@selector(_updatedDisplayedInformation)
  55. withObject:nil waitUntilDone:NO];
  56. }
  57. #endif
  58. - (void)_updatedDisplayedInformation
  59. {
  60. if (_dropboxFile != nil) {
  61. if (self.dropboxFile.isDirectory) {
  62. self.folderTitleLabel.text = self.dropboxFile.filename;
  63. self.titleLabel.hidden = self.subtitleLabel.hidden = YES;
  64. self.folderTitleLabel.hidden = NO;
  65. } else {
  66. self.titleLabel.text = self.dropboxFile.filename;
  67. self.subtitleLabel.text = (self.dropboxFile.totalBytes > 0) ? self.dropboxFile.humanReadableSize : @"";
  68. self.titleLabel.hidden = self.subtitleLabel.hidden = NO;
  69. self.folderTitleLabel.hidden = YES;
  70. }
  71. NSString *iconName = self.dropboxFile.icon;
  72. if ([iconName isEqualToString:@"folder_user"] || [iconName isEqualToString:@"folder"] || [iconName isEqualToString:@"folder_public"] || [iconName isEqualToString:@"folder_photos"] || [iconName isEqualToString:@"package"]) {
  73. self.thumbnailView.image = [UIImage imageNamed:@"folder"];
  74. self.downloadButton.hidden = YES;
  75. } else if ([iconName isEqualToString:@"page_white"] || [iconName isEqualToString:@"page_white_text"])
  76. self.thumbnailView.image = [UIImage imageNamed:@"blank"];
  77. else if ([iconName isEqualToString:@"page_white_film"])
  78. self.thumbnailView.image = [UIImage imageNamed:@"movie"];
  79. else if ([iconName isEqualToString:@"page_white_sound"])
  80. self.thumbnailView.image = [UIImage imageNamed:@"audio"];
  81. else {
  82. self.thumbnailView.image = [UIImage imageNamed:@"blank"];
  83. APLog(@"missing icon for type '%@'", self.dropboxFile.icon);
  84. }
  85. }
  86. #if TARGET_OS_IOS
  87. else if(_driveFile != nil){
  88. BOOL isDirectory = [self.driveFile.mimeType isEqualToString:@"application/vnd.google-apps.folder"];
  89. if (isDirectory) {
  90. self.folderTitleLabel.text = self.driveFile.title;
  91. self.titleLabel.hidden = self.subtitleLabel.hidden = YES;
  92. self.folderTitleLabel.hidden = NO;
  93. } else {
  94. self.titleLabel.text = self.driveFile.title;
  95. self.subtitleLabel.text = (self.driveFile.fileSize > 0) ? [NSByteCountFormatter stringFromByteCount:[self.driveFile.fileSize longLongValue] countStyle:NSByteCountFormatterCountStyleFile]: @"";
  96. self.titleLabel.hidden = self.subtitleLabel.hidden = NO;
  97. self.folderTitleLabel.hidden = YES;
  98. }
  99. if (_driveFile.thumbnailLink != nil) {
  100. _iconURL = [NSURL URLWithString:_driveFile.thumbnailLink];
  101. [self performSelectorInBackground:@selector(_updateIconFromURL) withObject:@""];
  102. }
  103. NSString *iconName = self.driveFile.iconLink;
  104. if (isDirectory) {
  105. self.thumbnailView.image = [UIImage imageNamed:@"folder"];
  106. } else if ([iconName isEqualToString:@"https://ssl.gstatic.com/docs/doclist/images/icon_10_audio_list.png"]) {
  107. self.thumbnailView.image = [UIImage imageNamed:@"audio"];
  108. } else if ([iconName isEqualToString:@"https://ssl.gstatic.com/docs/doclist/images/icon_11_video_list.png"]) {
  109. self.thumbnailView.image = [UIImage imageNamed:@"movie"];
  110. } else {
  111. self.thumbnailView.image = [UIImage imageNamed:@"blank"];
  112. APLog(@"missing icon for type '%@'", self.driveFile.iconLink);
  113. }
  114. } else if(_boxFile != nil) {
  115. BOOL isDirectory = [self.boxFile.type isEqualToString:@"folder"];
  116. if (isDirectory) {
  117. self.folderTitleLabel.text = self.boxFile.name;
  118. self.titleLabel.hidden = self.subtitleLabel.hidden = YES;
  119. self.folderTitleLabel.hidden = NO;
  120. } else {
  121. self.titleLabel.text = self.boxFile.name;
  122. self.subtitleLabel.text = (self.boxFile.size > 0) ? [NSByteCountFormatter stringFromByteCount:[self.boxFile.size longLongValue] countStyle:NSByteCountFormatterCountStyleFile]: @"";
  123. self.titleLabel.hidden = self.subtitleLabel.hidden = NO;
  124. self.folderTitleLabel.hidden = YES;
  125. self.downloadButton.hidden = NO;
  126. }
  127. //TODO: correct thumbnails
  128. // if (_boxFile.modelID != nil) {
  129. // //this request needs a token in the header to work
  130. // NSString *thumbnailURLString = [NSString stringWithFormat:@"https://api.box.com/2.0/files/%@/thumbnail.png?min_height=32&min_width=32&max_height=64&max_width=64", _boxFile.modelID];
  131. // _iconURL = [NSURL URLWithString:thumbnailURLString];
  132. // [self performSelectorInBackground:@selector(_updateIconFromURL) withObject:@""];
  133. // }
  134. //TODO:correct icons
  135. if (isDirectory) {
  136. self.thumbnailView.image = [UIImage imageNamed:@"folder"];
  137. } else {
  138. self.thumbnailView.image = [UIImage imageNamed:@"blank"];
  139. APLog(@"missing icon for type '%@'", self.boxFile);
  140. }
  141. } else if(_oneDriveFile != nil) {
  142. if (_oneDriveFile.isFolder) {
  143. self.downloadButton.hidden = YES;
  144. self.folderTitleLabel.text = self.oneDriveFile.name;
  145. self.titleLabel.hidden = self.subtitleLabel.hidden = YES;
  146. self.folderTitleLabel.hidden = NO;
  147. self.thumbnailView.image = [UIImage imageNamed:@"folder"];
  148. } else {
  149. self.downloadButton.hidden = NO;
  150. self.titleLabel.text = self.oneDriveFile.name;
  151. NSMutableString *subtitle = [[NSMutableString alloc] init];
  152. if (self.oneDriveFile.size > 0) {
  153. [subtitle appendString:[NSByteCountFormatter stringFromByteCount:[self.oneDriveFile.size longLongValue] countStyle:NSByteCountFormatterCountStyleFile]];
  154. if (self.oneDriveFile.duration > 0) {
  155. VLCTime *time = [VLCTime timeWithNumber:self.oneDriveFile.duration];
  156. [subtitle appendFormat:@" — %@", [time verboseStringValue]];
  157. }
  158. } else if (self.oneDriveFile.duration > 0) {
  159. VLCTime *time = [VLCTime timeWithNumber:self.oneDriveFile.duration];
  160. [subtitle appendString:[time verboseStringValue]];
  161. }
  162. self.subtitleLabel.text = subtitle;
  163. self.titleLabel.hidden = self.subtitleLabel.hidden = NO;
  164. self.folderTitleLabel.hidden = YES;
  165. if (self.oneDriveFile.isAudio)
  166. self.thumbnailView.image = [UIImage imageNamed:@"audio"];
  167. else if (self.oneDriveFile.isVideo) {
  168. self.thumbnailView.image = [UIImage imageNamed:@"movie"];
  169. NSString *thumbnailURL = _oneDriveFile.thumbnailURL;
  170. if ([thumbnailURL isKindOfClass:[NSString class]]) {
  171. if (thumbnailURL.length > 0) {
  172. _iconURL = [NSURL URLWithString:thumbnailURL];
  173. [self performSelectorInBackground:@selector(_updateIconFromURL) withObject:@""];
  174. }
  175. }
  176. } else
  177. self.thumbnailView.image = [UIImage imageNamed:@"blank"];
  178. }
  179. }
  180. #endif
  181. [self setNeedsDisplay];
  182. }
  183. - (void)_updateIconFromURL
  184. {
  185. NSData *imageData = [[NSData alloc] initWithContentsOfURL:_iconURL];
  186. UIImage *icon = [[UIImage alloc] initWithData:imageData];
  187. if (icon != nil) {
  188. [self performSelectorOnMainThread:@selector(_updateIconOnMainThread:) withObject:icon waitUntilDone:NO];
  189. }
  190. }
  191. - (void)_updateIconOnMainThread:(UIImage *)icon
  192. {
  193. self.thumbnailView.contentMode = UIViewContentModeScaleAspectFit;
  194. self.thumbnailView.image = icon;
  195. }
  196. - (IBAction)triggerDownload:(id)sender
  197. {
  198. if ([self.delegate respondsToSelector:@selector(triggerDownloadForCell:)])
  199. [self.delegate triggerDownloadForCell:self];
  200. }
  201. + (CGFloat)heightOfCell
  202. {
  203. #if TARGET_OS_IOS
  204. if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)
  205. return 80.;
  206. return 48.;
  207. #else
  208. return 107.;
  209. #endif
  210. }
  211. - (void)setIsDownloadable:(BOOL)isDownloadable
  212. {
  213. self.downloadButton.hidden = !isDownloadable;
  214. }
  215. @end