VLCCloudStorageTableViewCell.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. cell.titleLabel.hidden = YES;
  27. cell.subtitleLabel.hidden = YES;
  28. cell.folderTitleLabel.hidden = YES;
  29. return cell;
  30. }
  31. - (void)setDropboxFile:(DBMetadata *)dropboxFile
  32. {
  33. if (dropboxFile != _dropboxFile)
  34. _dropboxFile = dropboxFile;
  35. [self performSelectorOnMainThread:@selector(_updatedDisplayedInformation)
  36. withObject:nil waitUntilDone:NO];
  37. }
  38. #if TARGET_OS_IOS
  39. - (void)setDriveFile:(GTLDriveFile *)driveFile
  40. {
  41. if (driveFile != _driveFile)
  42. _driveFile = driveFile;
  43. [self performSelectorOnMainThread:@selector(_updatedDisplayedInformation)
  44. withObject:nil waitUntilDone:NO];
  45. }
  46. #endif
  47. - (void)setBoxFile:(BoxItem *)boxFile
  48. {
  49. if (boxFile != _boxFile)
  50. _boxFile = boxFile;
  51. [self performSelectorOnMainThread:@selector(_updatedDisplayedInformation)
  52. withObject:nil waitUntilDone:NO];
  53. }
  54. - (void)setOneDriveFile:(VLCOneDriveObject *)oneDriveFile
  55. {
  56. if (oneDriveFile != _oneDriveFile)
  57. _oneDriveFile = oneDriveFile;
  58. [self performSelectorOnMainThread:@selector(_updatedDisplayedInformation)
  59. withObject:nil waitUntilDone:NO];
  60. }
  61. - (void)_updatedDisplayedInformation
  62. {
  63. if (_dropboxFile != nil) {
  64. if (self.dropboxFile.isDirectory) {
  65. self.folderTitleLabel.text = self.dropboxFile.filename;
  66. self.titleLabel.hidden = self.subtitleLabel.hidden = YES;
  67. self.folderTitleLabel.hidden = NO;
  68. } else {
  69. self.titleLabel.text = self.dropboxFile.filename;
  70. self.subtitleLabel.text = (self.dropboxFile.totalBytes > 0) ? self.dropboxFile.humanReadableSize : @"";
  71. self.titleLabel.hidden = self.subtitleLabel.hidden = NO;
  72. self.folderTitleLabel.hidden = YES;
  73. }
  74. NSString *iconName = self.dropboxFile.icon;
  75. if ([iconName isEqualToString:@"folder_user"] || [iconName isEqualToString:@"folder"] || [iconName isEqualToString:@"folder_public"] || [iconName isEqualToString:@"folder_photos"] || [iconName isEqualToString:@"package"]) {
  76. self.thumbnailView.image = [UIImage imageNamed:@"folder"];
  77. self.downloadButton.hidden = YES;
  78. } else if ([iconName isEqualToString:@"page_white"] || [iconName isEqualToString:@"page_white_text"])
  79. self.thumbnailView.image = [UIImage imageNamed:@"blank"];
  80. else if ([iconName isEqualToString:@"page_white_film"])
  81. self.thumbnailView.image = [UIImage imageNamed:@"movie"];
  82. else if ([iconName isEqualToString:@"page_white_sound"])
  83. self.thumbnailView.image = [UIImage imageNamed:@"audio"];
  84. else {
  85. self.thumbnailView.image = [UIImage imageNamed:@"blank"];
  86. APLog(@"missing icon for type '%@'", self.dropboxFile.icon);
  87. }
  88. }
  89. #if TARGET_OS_IOS
  90. else if(_driveFile != nil){
  91. BOOL isDirectory = [self.driveFile.mimeType isEqualToString:@"application/vnd.google-apps.folder"];
  92. if (isDirectory) {
  93. self.folderTitleLabel.text = self.driveFile.title;
  94. self.titleLabel.hidden = self.subtitleLabel.hidden = YES;
  95. self.folderTitleLabel.hidden = NO;
  96. } else {
  97. self.titleLabel.text = self.driveFile.title;
  98. self.subtitleLabel.text = (self.driveFile.fileSize > 0) ? [NSByteCountFormatter stringFromByteCount:[self.driveFile.fileSize longLongValue] countStyle:NSByteCountFormatterCountStyleFile]: @"";
  99. self.titleLabel.hidden = self.subtitleLabel.hidden = NO;
  100. self.folderTitleLabel.hidden = YES;
  101. }
  102. if (_driveFile.thumbnailLink != nil) {
  103. _iconURL = [NSURL URLWithString:_driveFile.thumbnailLink];
  104. [self performSelectorInBackground:@selector(_updateIconFromURL) withObject:@""];
  105. }
  106. NSString *iconName = self.driveFile.iconLink;
  107. if (isDirectory) {
  108. self.thumbnailView.image = [UIImage imageNamed:@"folder"];
  109. } else if ([iconName isEqualToString:@"https://ssl.gstatic.com/docs/doclist/images/icon_10_audio_list.png"]) {
  110. self.thumbnailView.image = [UIImage imageNamed:@"audio"];
  111. } else if ([iconName isEqualToString:@"https://ssl.gstatic.com/docs/doclist/images/icon_11_video_list.png"]) {
  112. self.thumbnailView.image = [UIImage imageNamed:@"movie"];
  113. } else {
  114. self.thumbnailView.image = [UIImage imageNamed:@"blank"];
  115. APLog(@"missing icon for type '%@'", self.driveFile.iconLink);
  116. }
  117. }
  118. #endif
  119. else if(_boxFile != nil) {
  120. BOOL isDirectory = [self.boxFile.type isEqualToString:@"folder"];
  121. if (isDirectory) {
  122. self.folderTitleLabel.text = self.boxFile.name;
  123. self.titleLabel.hidden = self.subtitleLabel.hidden = YES;
  124. self.folderTitleLabel.hidden = NO;
  125. } else {
  126. self.titleLabel.text = self.boxFile.name;
  127. self.subtitleLabel.text = (self.boxFile.size > 0) ? [NSByteCountFormatter stringFromByteCount:[self.boxFile.size longLongValue] countStyle:NSByteCountFormatterCountStyleFile]: @"";
  128. self.titleLabel.hidden = self.subtitleLabel.hidden = NO;
  129. self.folderTitleLabel.hidden = YES;
  130. self.downloadButton.hidden = NO;
  131. }
  132. //TODO: correct thumbnails
  133. // if (_boxFile.modelID != nil) {
  134. // //this request needs a token in the header to work
  135. // 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];
  136. // _iconURL = [NSURL URLWithString:thumbnailURLString];
  137. // [self performSelectorInBackground:@selector(_updateIconFromURL) withObject:@""];
  138. // }
  139. //TODO:correct icons
  140. if (isDirectory) {
  141. self.thumbnailView.image = [UIImage imageNamed:@"folder"];
  142. } else {
  143. self.thumbnailView.image = [UIImage imageNamed:@"blank"];
  144. APLog(@"missing icon for type '%@'", self.boxFile);
  145. }
  146. } else if(_oneDriveFile != nil) {
  147. if (_oneDriveFile.isFolder) {
  148. self.downloadButton.hidden = YES;
  149. self.folderTitleLabel.text = self.oneDriveFile.name;
  150. self.titleLabel.hidden = self.subtitleLabel.hidden = YES;
  151. self.folderTitleLabel.hidden = NO;
  152. self.thumbnailView.image = [UIImage imageNamed:@"folder"];
  153. } else {
  154. self.downloadButton.hidden = NO;
  155. self.titleLabel.text = self.oneDriveFile.name;
  156. NSMutableString *subtitle = [[NSMutableString alloc] init];
  157. if (self.oneDriveFile.size > 0) {
  158. [subtitle appendString:[NSByteCountFormatter stringFromByteCount:[self.oneDriveFile.size longLongValue] countStyle:NSByteCountFormatterCountStyleFile]];
  159. if (self.oneDriveFile.duration > 0) {
  160. VLCTime *time = [VLCTime timeWithNumber:self.oneDriveFile.duration];
  161. [subtitle appendFormat:@" — %@", [time verboseStringValue]];
  162. }
  163. } else if (self.oneDriveFile.duration > 0) {
  164. VLCTime *time = [VLCTime timeWithNumber:self.oneDriveFile.duration];
  165. [subtitle appendString:[time verboseStringValue]];
  166. }
  167. self.subtitleLabel.text = subtitle;
  168. self.titleLabel.hidden = self.subtitleLabel.hidden = NO;
  169. self.folderTitleLabel.hidden = YES;
  170. if (self.oneDriveFile.isAudio)
  171. self.thumbnailView.image = [UIImage imageNamed:@"audio"];
  172. else if (self.oneDriveFile.isVideo) {
  173. self.thumbnailView.image = [UIImage imageNamed:@"movie"];
  174. NSString *thumbnailURL = _oneDriveFile.thumbnailURL;
  175. if ([thumbnailURL isKindOfClass:[NSString class]]) {
  176. if (thumbnailURL.length > 0) {
  177. _iconURL = [NSURL URLWithString:thumbnailURL];
  178. [self performSelectorInBackground:@selector(_updateIconFromURL) withObject:@""];
  179. }
  180. }
  181. } else
  182. self.thumbnailView.image = [UIImage imageNamed:@"blank"];
  183. }
  184. }
  185. [self setNeedsDisplay];
  186. }
  187. - (void)_updateIconFromURL
  188. {
  189. NSData *imageData = [[NSData alloc] initWithContentsOfURL:_iconURL];
  190. UIImage *icon = [[UIImage alloc] initWithData:imageData];
  191. if (icon != nil) {
  192. [self performSelectorOnMainThread:@selector(_updateIconOnMainThread:) withObject:icon waitUntilDone:NO];
  193. }
  194. }
  195. - (void)_updateIconOnMainThread:(UIImage *)icon
  196. {
  197. self.thumbnailView.contentMode = UIViewContentModeScaleAspectFit;
  198. self.thumbnailView.image = icon;
  199. }
  200. - (IBAction)triggerDownload:(id)sender
  201. {
  202. if ([self.delegate respondsToSelector:@selector(triggerDownloadForCell:)])
  203. [self.delegate triggerDownloadForCell:self];
  204. }
  205. + (CGFloat)heightOfCell
  206. {
  207. #if TARGET_OS_IOS
  208. if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)
  209. return 80.;
  210. return 48.;
  211. #else
  212. return 107.;
  213. #endif
  214. }
  215. - (void)setIsDownloadable:(BOOL)isDownloadable
  216. {
  217. self.downloadButton.hidden = !isDownloadable;
  218. }
  219. @end