CCDetail.m 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095
  1. //
  2. // CCDetail.m
  3. // Crypto Cloud Technology Nextcloud
  4. //
  5. // Created by Marino Faggiana on 16/01/15.
  6. // Copyright (c) 2014 TWS. All rights reserved.
  7. //
  8. // Author Marino Faggiana <m.faggiana@twsweb.it>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. #import "CCDetail.h"
  24. #import "AppDelegate.h"
  25. #import "CCMain.h"
  26. #define TOOLBAR_HEIGHT 49.0f
  27. #define alertRequestPasswordPDF 1
  28. @interface CCDetail ()
  29. {
  30. UIToolbar *_toolbar;
  31. UIBarButtonItem *_buttonAction;
  32. UIBarButtonItem *_buttonDelete;
  33. BOOL _reload;
  34. NSMutableOrderedSet *_dataSourceDirectoryID;
  35. }
  36. @end
  37. @implementation CCDetail
  38. #pragma --------------------------------------------------------------------------------------------
  39. #pragma mark ===== init =====
  40. #pragma --------------------------------------------------------------------------------------------
  41. - (id)initWithCoder:(NSCoder *)aDecoder
  42. {
  43. if (self = [super initWithCoder:aDecoder]) {
  44. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(insertGeocoderLocation:) name:@"insertGeocoderLocation" object:nil];
  45. self.metadataDetail = [[CCMetadata alloc] init];
  46. self.photos = [[NSMutableArray alloc] init];
  47. self.thumbs = [[NSMutableArray alloc] init];
  48. self.dataSourceImagesVideos = [[NSMutableArray alloc] init];
  49. _dataSourceDirectoryID = [[NSMutableOrderedSet alloc] init];
  50. self.indexNowVisible = 0;
  51. self.fileIDNowVisible = nil;
  52. app.activeDetail = self;
  53. }
  54. return self;
  55. }
  56. #pragma --------------------------------------------------------------------------------------------
  57. #pragma mark ===== View =====
  58. #pragma --------------------------------------------------------------------------------------------
  59. - (void)viewDidLoad
  60. {
  61. [super viewDidLoad];
  62. self.imageBackground.image = [UIImage imageNamed:image_brandSfondoiPad];
  63. if ([self.metadataDetail.fileName length] > 0 || [self.metadataDetail.directoryID length] > 0 || [self.metadataDetail.fileID length] > 0) {
  64. // open view
  65. [self viewFile];
  66. }
  67. }
  68. // Apparirà
  69. - (void)viewWillAppear:(BOOL)animated
  70. {
  71. [super viewWillAppear:animated];
  72. if (self.splitViewController.isCollapsed) {
  73. self.tabBarController.tabBar.hidden = YES;
  74. self.tabBarController.tabBar.translucent = YES;
  75. }
  76. }
  77. - (void)viewDidAppear:(BOOL)animated
  78. {
  79. [super viewDidAppear:animated];
  80. }
  81. // E' scomparso
  82. - (void)viewDidDisappear:(BOOL)animated
  83. {
  84. [super viewDidDisappear:animated];
  85. // remove all
  86. if (self.isMovingFromParentViewController)
  87. [self removeAllView];
  88. [self.navigationController cancelCCProgress];
  89. }
  90. - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
  91. {
  92. [coordinator animateAlongsideTransition:nil completion:^(id<UIViewControllerTransitionCoordinatorContext> context) {
  93. }];
  94. [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
  95. }
  96. // remove all view
  97. - (void)removeAllView
  98. {
  99. // Audio
  100. if (app.player)
  101. [app.player removeFromSuperview];
  102. // Document
  103. if (_webView) {
  104. [_webView removeFromSuperview];
  105. _webView.delegate = nil;
  106. _webView = nil;
  107. }
  108. // PDF
  109. if (_readerPDFViewController) {
  110. [_readerPDFViewController.view removeFromSuperview];
  111. _readerPDFViewController.delegate = nil;
  112. _readerPDFViewController = nil;
  113. }
  114. // Photo-Video
  115. if (_photoBrowser) {
  116. [_photos removeAllObjects];
  117. [_thumbs removeAllObjects];
  118. _photoBrowser.delegate = nil;
  119. _photoBrowser = nil;
  120. }
  121. [self.navigationController popToRootViewControllerAnimated:NO];
  122. }
  123. - (void)changeToDisplayMode
  124. {
  125. if (_readerPDFViewController)
  126. [self.readerPDFViewController updateContentViews];
  127. }
  128. - (void)createToolbar
  129. {
  130. _toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height - TOOLBAR_HEIGHT, self.view.bounds.size.width, TOOLBAR_HEIGHT)];
  131. UIBarButtonItem *flexible = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
  132. UIBarButtonItem *fixedSpaceMini = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:self action:nil];
  133. fixedSpaceMini.width = 25;
  134. _buttonAction = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:image_actionSheetOpenIn] style:UIBarButtonItemStylePlain target:self action:@selector(actionButtonPressed:)];
  135. _buttonDelete = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:self action:@selector(deleteButtonPressed:)];
  136. if (self.sourceDirectory == sorceDirectoryAccount)
  137. [_toolbar setItems:[NSArray arrayWithObjects: flexible, _buttonDelete, fixedSpaceMini, _buttonAction, nil]];
  138. else
  139. [_toolbar setItems:[NSArray arrayWithObjects: flexible, _buttonAction, nil]];
  140. [_toolbar setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin];
  141. [self.view addSubview:_toolbar];
  142. }
  143. #pragma --------------------------------------------------------------------------------------------
  144. #pragma mark ===== UIDocumentInteractionControllerDelegate =====
  145. #pragma --------------------------------------------------------------------------------------------
  146. - (void)documentInteractionControllerDidDismissOptionsMenu:(UIDocumentInteractionController *)controller
  147. {
  148. // evitiamo il rimando della eventuale photo e/o video
  149. if ([CCCoreData getCameraUploadActiveAccount:app.activeAccount]) {
  150. [CCCoreData setCameraUploadDatePhoto:[NSDate date]];
  151. [CCCoreData setCameraUploadDateVideo:[NSDate date]];
  152. }
  153. }
  154. #pragma --------------------------------------------------------------------------------------------
  155. #pragma mark ===== View File =====
  156. #pragma --------------------------------------------------------------------------------------------
  157. - (void)viewFile
  158. {
  159. // verifico se esiste l'icona e se la posso creare
  160. if ([[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"%@/%@.ico", app.directoryUser, self.metadataDetail.fileID]] == NO) {
  161. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),^{
  162. [CCGraphics createNewImageFrom:self.metadataDetail.fileID directoryUser:app.directoryUser fileNameTo:self.metadataDetail.fileID fileNamePrint:self.metadataDetail.fileNamePrint size:@"m" imageForUpload:NO typeFile:self.metadataDetail.typeFile writePreview:YES optimizedFileName:[CCUtility getOptimizedPhoto]];
  163. });
  164. }
  165. if ([self.metadataDetail.typeFile isEqualToString:metadataTypeFile_image] || [self.metadataDetail.typeFile isEqualToString:metadataTypeFile_video]) {
  166. self.edgesForExtendedLayout = UIRectEdgeAll;
  167. [self viewImage];
  168. }
  169. if ([self.metadataDetail.typeFile isEqualToString:metadataTypeFile_audio]) {
  170. self.edgesForExtendedLayout = UIRectEdgeBottom;
  171. [self viewAudio];
  172. [self createToolbar];
  173. [CCAspect aspectNavigationControllerBar:self.navigationController.navigationBar hidden:NO];
  174. }
  175. if ([self.metadataDetail.typeFile isEqualToString:metadataTypeFile_document]) {
  176. NSString *ext = [[self.metadataDetail.fileNamePrint pathExtension] lowercaseString];
  177. if ([ext isEqualToString:@"pdf"]) {
  178. self.edgesForExtendedLayout = UIRectEdgeBottom;
  179. [self viewPDF:@""];
  180. [self createToolbar];
  181. [CCAspect aspectNavigationControllerBar:self.navigationController.navigationBar hidden:NO];
  182. } else {
  183. self.edgesForExtendedLayout = UIRectEdgeBottom;
  184. [self viewDocument];
  185. [self createToolbar];
  186. [CCAspect aspectNavigationControllerBar:self.navigationController.navigationBar hidden:NO];
  187. }
  188. }
  189. }
  190. #pragma --------------------------------------------------------------------------------------------
  191. #pragma mark ===== View Audio =====
  192. #pragma --------------------------------------------------------------------------------------------
  193. - (void)viewAudio
  194. {
  195. NSString *fileName;
  196. if (self.sourceDirectory == sorceDirectoryLocal) {
  197. fileName = [NSString stringWithFormat:@"%@/%@", self.metadataDetail.directoryID, self.metadataDetail.fileNamePrint];
  198. } else {
  199. fileName = [NSString stringWithFormat:@"%@/%@", [CCUtility getDirectoryAudio], self.metadataDetail.fileNamePrint];
  200. [[NSFileManager defaultManager] removeItemAtPath:fileName error:nil];
  201. [[NSFileManager defaultManager] linkItemAtPath:[NSString stringWithFormat:@"%@/%@", app.directoryUser, self.metadataDetail.fileID] toPath:fileName error:nil];
  202. }
  203. app.player.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height - TOOLBAR_HEIGHT);
  204. app.player.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  205. app.player.nextButton.hidden = true;
  206. app.player.previousButton.hidden = true;
  207. app.player.fullscreenButton_.hidden = true;
  208. LMMediaItem *item = [[LMMediaItem alloc] initWithInfo:@{LMMediaItemInfoURLKey:[NSURL fileURLWithPath:fileName], LMMediaItemInfoContentTypeKey:@(LMMediaItemContentTypeVideo)}];
  209. item.title = self.metadataDetail.fileNamePrint;
  210. item.artist = _brand_;
  211. [app.player.mediaPlayer removeAllMediaInQueue];
  212. [app.player.mediaPlayer addMedia:item];
  213. [app.player.mediaPlayer play];
  214. // Info
  215. NSMutableDictionary *songInfo = [[NSMutableDictionary alloc] init];
  216. [songInfo setObject:app.player.mediaPlayer.nowPlayingItem.title forKey:MPMediaItemPropertyTitle];
  217. [songInfo setObject:app.player.mediaPlayer.nowPlayingItem.artist forKey:MPMediaItemPropertyArtist];
  218. [[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:songInfo];
  219. [self.view addSubview:app.player];
  220. }
  221. #pragma --------------------------------------------------------------------------------------------
  222. #pragma mark ===== View Document =====
  223. #pragma --------------------------------------------------------------------------------------------
  224. - (void)viewDocument
  225. {
  226. NSString *fileName;
  227. if (self.sourceDirectory == sorceDirectoryLocal) {
  228. fileName = [NSString stringWithFormat:@"%@/%@", self.metadataDetail.directoryID, self.metadataDetail.fileNamePrint];
  229. } else {
  230. fileName = [NSTemporaryDirectory() stringByAppendingString:self.metadataDetail.fileNamePrint];
  231. [[NSFileManager defaultManager] removeItemAtPath:fileName error:nil];
  232. [[NSFileManager defaultManager] linkItemAtPath:[NSString stringWithFormat:@"%@/%@", app.directoryUser, self.metadataDetail.fileID] toPath:fileName error:nil];
  233. }
  234. NSString *ext=@"";
  235. ext = [CCUtility getExtension:self.metadataDetail.fileNamePrint];
  236. self.webView = [[UIWebView alloc] initWithFrame:(CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height - TOOLBAR_HEIGHT))];
  237. self.webView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  238. [self.webView setBackgroundColor:[UIColor whiteColor]];
  239. [self.webView setOpaque:NO];
  240. if ( [ext isEqualToString:@"CSS"] || [ext isEqualToString:@"PY"] || [ext isEqualToString:@"XML"] || [ext isEqualToString:@"JS"] ) {
  241. NSMutableURLRequest *headRequest = [NSMutableURLRequest requestWithURL:[NSURL fileURLWithPath:fileName]];
  242. [headRequest setHTTPMethod:@"HEAD"];
  243. NSHTTPURLResponse *headResponse;
  244. NSError *error = nil;
  245. [NSURLConnection sendSynchronousRequest:headRequest returningResponse:&headResponse error:&error];
  246. if (error != nil) {
  247. NSLog(@"[LOG] loadURLWithString %@",[error localizedDescription]);
  248. }
  249. NSString *dataFile = [[NSString alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL fileURLWithPath:fileName]] encoding:NSASCIIStringEncoding];
  250. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
  251. [self.webView loadHTMLString:[NSString stringWithFormat:@"<div style='font-size:%@;font-family:%@;'><pre>%@",@"40",@"Sans-Serif",dataFile] baseURL:nil];
  252. }else{
  253. [self.webView loadHTMLString:[NSString stringWithFormat:@"<div style='font-size:%@;font-family:%@;'><pre>%@",@"20",@"Sans-Serif",dataFile] baseURL:nil];
  254. }
  255. } else if ([ext isEqualToString:@"TXT"] ) {
  256. NSMutableURLRequest *headRequest = [NSMutableURLRequest requestWithURL:[NSURL fileURLWithPath:fileName]];
  257. [headRequest setHTTPMethod:@"HEAD"];
  258. NSHTTPURLResponse *headResponse;
  259. NSError *error = nil;
  260. [NSURLConnection sendSynchronousRequest:headRequest returningResponse:&headResponse error:&error];
  261. if (error != nil) {
  262. NSLog(@"[LOG] loadURLWithString %@",[error localizedDescription]);
  263. }
  264. NSString *mimeType = [headResponse MIMEType];
  265. [self.webView loadData:[NSData dataWithContentsOfURL: [NSURL fileURLWithPath:fileName]] MIMEType:mimeType textEncodingName:@"utf-8" baseURL:[NSURL URLWithString:@""]];
  266. } else {
  267. [self.webView loadRequest:[NSMutableURLRequest requestWithURL:[NSURL fileURLWithPath:fileName]]];
  268. }
  269. [self.webView setScalesPageToFit:YES];
  270. [self.view addSubview:self.webView];
  271. }
  272. #pragma --------------------------------------------------------------------------------------------
  273. #pragma mark ===== View Image =====
  274. #pragma --------------------------------------------------------------------------------------------
  275. - (void)viewImage
  276. {
  277. BOOL startDownloadBrowseImages = NO;
  278. self.photoBrowser = [[MWPhotoBrowser alloc] initWithDelegate:self];
  279. _reload = NO;
  280. CCMetadata *metadata = [[CCMetadata alloc] init];
  281. [self.photos removeAllObjects];
  282. [self.thumbs removeAllObjects];
  283. [_dataSourceDirectoryID removeAllObjects];
  284. // Favorite
  285. if (self.sourceDirectory == sorceDirectoryFavorite) {
  286. self.dataSourceImagesVideos = (NSMutableArray *)[CCCoreData getTableMetadataWithPredicate:[NSPredicate predicateWithFormat:@"(fileID == %@) AND (account == %@)", self.metadataDetail.fileID, app.activeAccount] fieldOrder:[CCUtility getOrderSettings] ascending:[CCUtility getAscendingSettings]];
  287. }
  288. // Local
  289. if (self.sourceDirectory == sorceDirectoryLocal) {
  290. NSString *cameraFolderName = [CCCoreData getCameraUploadFolderNameActiveAccount:app.activeAccount];
  291. NSString *cameraFolderPath = [CCCoreData getCameraUploadFolderPathActiveAccount:app.activeAccount activeUrl:app.activeUrl typeCloud:app.typeCloud];
  292. metadata = [CCUtility insertFileSystemInMetadata:self.metadataDetail.fileID directory:self.metadataDetail.directoryID activeAccount:app.activeAccount cameraFolderName:cameraFolderName cameraFolderPath:cameraFolderPath];
  293. self.dataSourceImagesVideos = [[NSMutableArray alloc] initWithObjects:metadata, nil];
  294. }
  295. // if not images, exit
  296. if ([self.dataSourceImagesVideos count] == 0) return;
  297. NSUInteger index = 0;
  298. for (CCMetadata *metadata in self.dataSourceImagesVideos) {
  299. // start from here ?
  300. if (self.metadataDetail.fileID && [metadata.fileID isEqualToString:self.metadataDetail.fileID])
  301. [self.photoBrowser setCurrentPhotoIndex:index];
  302. // here we can execute the first browse images
  303. if ([self.metadataDetail.sessionSelector isEqualToString:selectorBrowseImages] && startDownloadBrowseImages == NO && [[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"%@/%@", app.directoryUser, metadata.fileID]] == NO && [metadata.session length] == 0) {
  304. NSString *serverUrl = [CCCoreData getServerUrlFromDirectoryID:metadata.directoryID activeAccount:metadata.account];
  305. [[CCNetworking sharedNetworking] downloadFile:metadata serverUrl:serverUrl downloadData:YES downloadPlist:NO selector:selectorBrowseImages selectorPost:nil session:download_session taskStatus:taskStatusResume delegate:nil];
  306. startDownloadBrowseImages = YES;
  307. }
  308. if (metadata.cryptated) {
  309. [self.photos addObject:[MWPhoto photoWithImage:[UIImage imageNamed:image_filePreviewDownloadCrypto]]];
  310. [self.thumbs addObject:[MWPhoto photoWithImage:[UIImage imageNamed:image_filePreviewDownloadCrypto]]];
  311. } else {
  312. [self.photos addObject:[MWPhoto photoWithImage:[UIImage imageNamed:image_filePreviewDownload]]];
  313. MWPhoto *thumb = [MWPhoto photoWithImage:[UIImage imageNamed:image_filePreviewDownload]];
  314. if ([metadata.typeFile isEqualToString:metadataTypeFile_video]) thumb.isVideo = YES;
  315. [self.thumbs addObject:thumb];
  316. }
  317. // add directory
  318. [_dataSourceDirectoryID addObject:metadata.directoryID];
  319. index++;
  320. }
  321. // PhotoBrowser
  322. self.photoBrowser.displayActionButton = YES;
  323. if (self.sourceDirectory == sorceDirectoryAccount) self.photoBrowser.displayDeleteButton = YES;
  324. else self.photoBrowser.displayDeleteButton = NO;
  325. self.photoBrowser.displayNavArrows = YES;
  326. self.photoBrowser.displaySelectionButtons = NO;
  327. self.photoBrowser.alwaysShowControls = NO;
  328. self.photoBrowser.zoomPhotosToFill = NO;
  329. self.photoBrowser.startOnGrid = NO;
  330. self.photoBrowser.autoPlayOnAppear = NO;
  331. self.photoBrowser.delayToHideElements = 15;
  332. self.photoBrowser.enableGrid = YES;
  333. if (self.traitCollection.horizontalSizeClass == UIUserInterfaceSizeClassCompact) {
  334. [self addChildViewController:self.photoBrowser];
  335. [self.view addSubview:self.photoBrowser.view];
  336. [self.photoBrowser didMoveToParentViewController:self];
  337. } else {
  338. [self.navigationController pushViewController:self.photoBrowser animated:NO];
  339. }
  340. }
  341. - (NSUInteger)numberOfPhotosInPhotoBrowser:(MWPhotoBrowser *)photoBrowser
  342. {
  343. return [self.dataSourceImagesVideos count];
  344. }
  345. - (NSString *)photoBrowser:(MWPhotoBrowser *)photoBrowser titleForPhotoAtIndex:(NSUInteger)index
  346. {
  347. NSString *titleDir = [NSString stringWithFormat:@"%lu %@ %lu", (unsigned long)index+1, NSLocalizedString(@"of", nil), (unsigned long)self.photos.count];
  348. self.title = titleDir;
  349. return titleDir;
  350. }
  351. - (void)photoBrowser:(MWPhotoBrowser *)photoBrowser didDisplayPhotoAtIndex:(NSUInteger)index
  352. {
  353. CCMetadata *metadata = [self.dataSourceImagesVideos objectAtIndex:index];
  354. NSString *directory;
  355. self.indexNowVisible = index;
  356. self.fileIDNowVisible = metadata.fileID;
  357. photoBrowser.toolbar.hidden = NO;
  358. if (_reload) {
  359. [self.photoBrowser performSelector:@selector(reloadData) withObject:nil afterDelay:0.1];
  360. _reload = NO;
  361. }
  362. if (self.sourceDirectory == sorceDirectoryLocal) directory = self.metadataDetail.directoryID;
  363. else directory = app.directoryUser;
  364. // Download self.metadataSegue.sessionSelector = selectorBrowseImages;
  365. if (metadata && [[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"%@/%@", directory, metadata.fileID]] == NO && [metadata.session length] == 0 && [self.metadataDetail.sessionSelector isEqualToString:selectorBrowseImages] == NO)
  366. [self performSelector:@selector(downloadPhotoBrowser:) withObject:metadata afterDelay:0.1];
  367. }
  368. - (id <MWPhoto>)photoBrowser:(MWPhotoBrowser *)photoBrowser photoAtIndex:(NSUInteger)index
  369. {
  370. NSString *directory;
  371. UIImage *image;
  372. if (self.sourceDirectory == sorceDirectoryLocal) directory = self.metadataDetail.directoryID;
  373. else directory = app.directoryUser;
  374. CCMetadata *metadata = [self.dataSourceImagesVideos objectAtIndex:index];
  375. //NSLog(@"[LOG] photoBrowser: photoAtIndex : %lu ---- di totali photo : %lu", (unsigned long)index, (unsigned long)_photos.count);
  376. if (index < self.photos.count) {
  377. if (metadata.fileID) {
  378. if ([metadata.typeFile isEqualToString:metadataTypeFile_image]) {
  379. NSString *fileImage = [NSString stringWithFormat:@"%@/%@", directory, metadata.fileID];
  380. NSString *ext = [CCUtility getExtension:metadata.fileNamePrint];
  381. if ([ext isEqualToString:@"GIF"]) image = [UIImage animatedImageWithAnimatedGIFURL:[NSURL fileURLWithPath:fileImage]];
  382. else image = [UIImage imageWithContentsOfFile:fileImage];
  383. if (image) {
  384. MWPhoto *photo = [MWPhoto photoWithImage:image];
  385. // Location ??
  386. [self setLocationCaptionPhoto:photo fileID:metadata.fileID];
  387. if (!photo.caption)
  388. photo.caption = metadata.fileNamePrint;
  389. [self.photos replaceObjectAtIndex:index withObject:photo];
  390. } else {
  391. if ([metadata.sessionError length] > 0 ) {
  392. [self.photos replaceObjectAtIndex:index withObject:[MWPhoto photoWithImage:[UIImage imageNamed:image_filePreviewError]]];
  393. } else {
  394. NSURL *url = [[NSBundle mainBundle] URLForResource:@"loading" withExtension:@"gif"];
  395. [self.photos replaceObjectAtIndex:index withObject:[MWPhoto photoWithImage:[UIImage animatedImageWithAnimatedGIFURL:url]]];
  396. }
  397. }
  398. }
  399. if ([metadata.typeFile isEqualToString:metadataTypeFile_video]) {
  400. if ([[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"%@/%@", directory, metadata.fileID]]) {
  401. // remove and make the simbolic link in temp
  402. NSString *toPath = [NSTemporaryDirectory() stringByAppendingString:metadata.fileNamePrint];
  403. [[NSFileManager defaultManager] removeItemAtPath:toPath error:nil];
  404. [[NSFileManager defaultManager] linkItemAtPath:[NSString stringWithFormat:@"%@/%@", directory, metadata.fileID] toPath:toPath error:nil];
  405. NSURL *url = [NSURL fileURLWithPath:toPath];
  406. MWPhoto *video = [MWPhoto photoWithImage:[CCGraphics thumbnailImageForVideo:url atTime:1.0]];
  407. video.caption = metadata.fileNamePrint;
  408. video.videoURL = url;
  409. [self.photos replaceObjectAtIndex:index withObject:video];
  410. } else {
  411. if ([metadata.sessionError length] > 0 ) {
  412. [self.photos replaceObjectAtIndex:index withObject:[MWPhoto photoWithImage:[UIImage imageNamed:image_filePreviewError]]];
  413. } else {
  414. NSURL *url = [[NSBundle mainBundle] URLForResource:@"loading" withExtension:@"gif"];
  415. [self.photos replaceObjectAtIndex:index withObject:[MWPhoto photoWithImage:[UIImage animatedImageWithAnimatedGIFURL:url]]];
  416. }
  417. }
  418. }
  419. }
  420. // energy saving memory
  421. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  422. int iPrev = (int)index - 2;
  423. if (iPrev >= 0) {
  424. if ([self.photos objectAtIndex:iPrev] != nil)
  425. [self.photos replaceObjectAtIndex:iPrev withObject:[MWPhoto photoWithImage:nil]];
  426. }
  427. int iNext = (int)index + 2;
  428. if (iNext < _photos.count) {
  429. if ([self.photos objectAtIndex:iNext] != nil)
  430. [self.photos replaceObjectAtIndex:iNext withObject:[MWPhoto photoWithImage:nil]];
  431. }
  432. });
  433. return [self.photos objectAtIndex:index];
  434. }
  435. return nil;
  436. }
  437. - (void)gridWillAppear:(MWPhotoBrowser *)photoBrowser
  438. {
  439. // reload [BUG]
  440. [self.photoBrowser reloadData];
  441. // in grid clear all MWPhoto : energy saving memory
  442. if (self.photos.count > 1 && [self.dataSourceImagesVideos count] > 0) {
  443. for(int index=0; index < self.photos.count; index++)
  444. [_photos replaceObjectAtIndex:index withObject:[MWPhoto photoWithImage:nil]];
  445. }
  446. self.title = [NSString stringWithFormat:@"Album %lu %@", (unsigned long)_photos.count, NSLocalizedString(@"photos", nil)];
  447. }
  448. - (void)gridWillDisappear:(MWPhotoBrowser *)photoBrowser
  449. {
  450. //
  451. photoBrowser.toolbar.hidden = NO;
  452. }
  453. - (void)setControlsHidden:(BOOL)hidden animated:(BOOL)animated permanent:(BOOL)permanent
  454. {
  455. // Control Center
  456. if (self.splitViewController.isCollapsed)
  457. [app.controlCenter setControlCenterHidden:hidden];
  458. self.navigationController.navigationBar.hidden = hidden;
  459. }
  460. - (id <MWPhoto>)photoBrowser:(MWPhotoBrowser *)photoBrowser thumbPhotoAtIndex:(NSUInteger)index
  461. {
  462. NSString *directory;
  463. CCMetadata *metadata = [self.dataSourceImagesVideos objectAtIndex:index];
  464. if (self.sourceDirectory == sorceDirectoryLocal) directory = self.metadataDetail.directoryID;
  465. else directory = app.directoryUser;
  466. if (index < self.thumbs.count) {
  467. if (metadata.fileID) {
  468. UIImage *image;
  469. if (self.sourceDirectory == sorceDirectoryLocal) {
  470. image = [CCGraphics createNewImageFrom:metadata.fileID directoryUser:directory fileNameTo:metadata.fileID fileNamePrint:metadata.fileNamePrint size:@"m" imageForUpload:NO typeFile:metadata.typeFile writePreview:NO optimizedFileName:[CCUtility getOptimizedPhoto]];
  471. } else {
  472. NSString *fileImage = [NSString stringWithFormat:@"%@/%@.ico", directory, metadata.fileID];
  473. image = [UIImage animatedImageWithAnimatedGIFURL:[NSURL fileURLWithPath:fileImage]];
  474. }
  475. if (image) {
  476. MWPhoto *thumb = [MWPhoto photoWithImage:image];
  477. if ([metadata.typeFile isEqualToString:metadataTypeFile_video]) thumb.isVideo = YES;
  478. [self.thumbs replaceObjectAtIndex:index withObject:thumb];
  479. }
  480. }
  481. return [self.thumbs objectAtIndex:index];
  482. }
  483. return nil;
  484. }
  485. - (void)photoBrowser:(MWPhotoBrowser *)photoBrowser actionButtonPressedForPhotoAtIndex:(NSUInteger)index
  486. {
  487. NSString *filePath;
  488. CCMetadata *metadata = [self.dataSourceImagesVideos objectAtIndex:index];
  489. if (metadata == nil) return;
  490. if (self.sourceDirectory == sorceDirectoryLocal) {
  491. filePath = [NSString stringWithFormat:@"%@/%@", self.metadataDetail.directoryID, self.metadataDetail.fileNamePrint];
  492. } else {
  493. filePath = [NSString stringWithFormat:@"%@/%@", app.directoryUser, metadata.fileNamePrint];
  494. [[NSFileManager defaultManager] removeItemAtPath:filePath error:nil];
  495. [[NSFileManager defaultManager] linkItemAtPath:[NSString stringWithFormat:@"%@/%@", app.directoryUser, metadata.fileID] toPath:filePath error:nil];
  496. }
  497. self.docController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:filePath]];
  498. self.docController.delegate = self;
  499. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) [self.docController presentOptionsMenuFromRect:photoBrowser.view.frame inView:photoBrowser.view animated:YES];
  500. [self.docController presentOptionsMenuFromBarButtonItem:photoBrowser.actionButton animated:YES];
  501. }
  502. - (void)photoBrowser:(MWPhotoBrowser *)photoBrowser deleteButtonPressedForPhotoAtIndex:(NSUInteger)index deleteButton:(UIBarButtonItem *)deleteButton
  503. {
  504. CCMetadata *metadata = [self.dataSourceImagesVideos objectAtIndex:index];
  505. if (metadata == nil || [[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"%@/%@", app.directoryUser, metadata.fileID]] == NO) return;
  506. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  507. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_delete_", nil)
  508. style:UIAlertActionStyleDestructive
  509. handler:^(UIAlertAction *action) {
  510. [self.delegate deleteFileOrFolder:metadata numFile:1 ofFile:1];
  511. }]];
  512. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_cancel_", nil)
  513. style:UIAlertActionStyleCancel
  514. handler:^(UIAlertAction *action) {
  515. [alertController dismissViewControllerAnimated:YES completion:nil];
  516. }]];
  517. alertController.popoverPresentationController.barButtonItem = deleteButton;
  518. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  519. [alertController.view layoutIfNeeded];
  520. [self.parentViewController presentViewController:alertController animated:YES completion:NULL];
  521. }
  522. - (void)progressTask:(NSString *)fileID serverUrl:(NSString *)serverUrl cryptated:(BOOL)cryptated progress:(float)progress
  523. {
  524. if (progress == 0)
  525. [self.navigationController cancelCCProgress];
  526. else
  527. [self.navigationController setCCProgressPercentage:progress*100 andTintColor:COLOR_BRAND];
  528. }
  529. - (void)downloadPhotoBrowserFailure:(NSInteger)errorCode
  530. {
  531. [app messageNotification:@"_download_selected_files_" description:@"_error_download_photobrowser_" visible:YES delay:dismissAfterSecond type:TWMessageBarMessageTypeError];
  532. [self.photoBrowser reloadData];
  533. [self.navigationController cancelCCProgress];
  534. }
  535. - (void)downloadPhotoBrowserSuccess:(CCMetadata *)metadataVar selector:(NSString *)selector
  536. {
  537. NSUInteger index = 0;
  538. BOOL startDownloadBrowseImages;
  539. BOOL indexFound = NO;
  540. // if a message for a directory of these
  541. if (![_dataSourceDirectoryID containsObject:metadataVar.directoryID])
  542. return;
  543. // is selector Browse Images ?
  544. startDownloadBrowseImages = ([selector isEqualToString:selectorBrowseImages] ? NO : YES);
  545. for (NSUInteger i=0; i < [self.dataSourceImagesVideos count]; i++ ) {
  546. CCMetadata *metadata = [self.dataSourceImagesVideos objectAtIndex:i];
  547. // search index
  548. if ([metadataVar.fileID isEqualToString:metadata.fileID]) {
  549. index = i;
  550. indexFound = YES;
  551. }
  552. // next download browse imgage
  553. if (startDownloadBrowseImages == NO && [[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"%@/%@", app.directoryUser, metadata.fileID]] == NO && [metadata.session length] == 0) {
  554. NSString *serverUrl = [CCCoreData getServerUrlFromDirectoryID:metadata.directoryID activeAccount:metadata.account];
  555. [[CCNetworking sharedNetworking] downloadFile:metadata serverUrl:serverUrl downloadData:YES downloadPlist:NO selector:selectorBrowseImages selectorPost:nil session:download_session taskStatus:taskStatusResume delegate:nil];
  556. startDownloadBrowseImages = YES;
  557. }
  558. if (startDownloadBrowseImages && indexFound) break;
  559. }
  560. // do not reload is Video on air
  561. if (_photoBrowser.currentVideoPlayerViewController.isViewLoaded && _photoBrowser.currentVideoPlayerViewController.view.window) return;
  562. //NSLog(@"[LOG] Add Download Photo Browser");
  563. if ([metadataVar.fileID isEqualToString:self.fileIDNowVisible] || [self.photoBrowser isGridReload:index]) {
  564. [self.photoBrowser reloadData];
  565. _reload = NO;
  566. } else {
  567. _reload = YES;
  568. }
  569. [self.navigationController cancelCCProgress];
  570. }
  571. - (void)downloadPhotoBrowser:(CCMetadata *)metadata
  572. {
  573. NSString *serverUrl = [CCCoreData getServerUrlFromDirectoryID:metadata.directoryID activeAccount:metadata.account];
  574. [[CCNetworking sharedNetworking] downloadFile:metadata serverUrl:serverUrl downloadData:YES downloadPlist:NO selector:selectorLoadViewImage selectorPost:nil session:download_session taskStatus:taskStatusResume delegate:nil];
  575. }
  576. - (void)insertGeocoderLocation:(NSNotification *)notification
  577. {
  578. NSString *fileID = [[notification.userInfo allKeys] objectAtIndex:0];
  579. //NSDate *date = [[notification.userInfo allValues] objectAtIndex:0];
  580. dispatch_async(dispatch_get_main_queue(), ^{
  581. if ([fileID isEqualToString:self.fileIDNowVisible]) {
  582. MWPhoto *photo = [self.photos objectAtIndex:self.indexNowVisible];
  583. [self setLocationCaptionPhoto:photo fileID:fileID];
  584. if (![self.photoBrowser isGridController]) {
  585. [self.photoBrowser hideControls];
  586. [self.photoBrowser showControls];
  587. }
  588. }
  589. });
  590. }
  591. - (void)setLocationCaptionPhoto:(MWPhoto *)photo fileID:(NSString *)fileID
  592. {
  593. TableLocalFile *localFile;
  594. // read Geocoder
  595. localFile = [CCCoreData getLocalFileWithFileID:fileID activeAccount:app.activeAccount];
  596. if ([localFile.exifLatitude doubleValue] != 0 || [localFile.exifLongitude doubleValue] != 0) {
  597. // Fix BUG Geo latitude & longitude
  598. if ([localFile.exifLatitude doubleValue] == 9999 || [localFile.exifLongitude doubleValue] == 9999) {
  599. CCMetadata *metadata = [CCCoreData getMetadataWithPreficate:[NSPredicate predicateWithFormat:@"(fileID == %@) AND (account == %@)", fileID, app.activeAccount] context:nil];
  600. [CCExifGeo setExifLocalTableFileID:metadata directoryUser:app.directoryUser activeAccount:app.activeAccount];
  601. }
  602. [CCExifGeo setGeocoderFileID:fileID exifDate:localFile.exifDate latitude:localFile.exifLatitude longitude:localFile.exifLongitude];
  603. localFile = [CCCoreData getLocalFileWithFileID:fileID activeAccount:app.activeAccount];
  604. if ([localFile.exifLatitude floatValue] != 0 || [localFile.exifLongitude floatValue] != 0) {
  605. NSString *location = [CCCoreData getLocationFromGeoLatitude:localFile.exifLatitude longitude:localFile.exifLongitude];
  606. if ([localFile.exifDate isEqualToDate:[NSDate distantPast]] == NO && location) {
  607. NSString *localizedDateTime = [NSDateFormatter localizedStringFromDate:localFile.exifDate dateStyle:NSDateFormatterFullStyle timeStyle:NSDateFormatterMediumStyle];
  608. photo.caption = [NSString stringWithFormat:NSLocalizedString(@"%@\n%@", nil), localizedDateTime, location];
  609. }
  610. }
  611. }
  612. }
  613. #pragma --------------------------------------------------------------------------------------------
  614. #pragma mark ===== View PDF =====
  615. #pragma --------------------------------------------------------------------------------------------
  616. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  617. {
  618. [alertView dismissWithClickedButtonIndex:buttonIndex animated:YES];
  619. [[alertView textFieldAtIndex:0] resignFirstResponder];
  620. if (alertView.tag == alertRequestPasswordPDF) [self performSelector:@selector(viewPDF:) withObject:[alertView textFieldAtIndex:0].text afterDelay:0.3];
  621. }
  622. - (void)viewPDF:(NSString *)password
  623. {
  624. NSString *fileName;
  625. if (self.sourceDirectory == sorceDirectoryLocal) {
  626. fileName = [NSString stringWithFormat:@"%@/%@", self.metadataDetail.directoryID, self.metadataDetail.fileNamePrint];
  627. } else {
  628. fileName = [NSTemporaryDirectory() stringByAppendingString:self.metadataDetail.fileNamePrint];
  629. [[NSFileManager defaultManager] removeItemAtPath:fileName error:nil];
  630. [[NSFileManager defaultManager] linkItemAtPath:[NSString stringWithFormat:@"%@/%@", app.directoryUser, self.metadataDetail.fileID] toPath:fileName error:nil];
  631. }
  632. if ([[NSFileManager defaultManager] fileExistsAtPath:fileName isDirectory:nil] == NO) {
  633. // read file error
  634. UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"_error_", nil) message:NSLocalizedString(@"_read_file_error_", nil) delegate:nil cancelButtonTitle:nil otherButtonTitles:NSLocalizedString(@"_ok_", nil), nil];
  635. [alertView show];
  636. }
  637. CGPDFDocumentRef pdf = CGPDFDocumentCreateWithURL((CFURLRef)[NSURL fileURLWithPath:fileName]);
  638. if (pdf) {
  639. // Encrypted
  640. if (CGPDFDocumentIsEncrypted(pdf) == YES) {
  641. // Try a blank password first, per Apple's Quartz PDF example
  642. if (CGPDFDocumentUnlockWithPassword(pdf, "") == YES) {
  643. // blank password
  644. [self readerPDF:fileName password:@""];
  645. } else {
  646. if ([password length] == 0) {
  647. // password request
  648. UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"_insert_password_pfd_",nil) message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:NSLocalizedString(@"_ok_", nil), nil];
  649. [alertView setAlertViewStyle:UIAlertViewStylePlainTextInput];
  650. alertView.tag = alertRequestPasswordPDF;
  651. [alertView show];
  652. } else {
  653. const char *key = [password UTF8String];
  654. // failure
  655. if (CGPDFDocumentUnlockWithPassword(pdf, key) == NO) {
  656. UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"_error_", nil) message:NSLocalizedString(@"_password_pdf_error_", nil) delegate:nil cancelButtonTitle:nil otherButtonTitles:NSLocalizedString(@"_ok_", nil), nil];
  657. [alertView show];
  658. } else {
  659. // pdf with password
  660. [self readerPDF:fileName password:password];
  661. }
  662. }
  663. }
  664. } else{
  665. // No password
  666. [self readerPDF:fileName password:@""];
  667. }
  668. } else {
  669. // read file error
  670. UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"_error_", nil) message:NSLocalizedString(@"_read_file_error_", nil) delegate:nil cancelButtonTitle:nil otherButtonTitles:NSLocalizedString(@"_ok_", nil), nil];
  671. [alertView show];
  672. }
  673. }
  674. - (void)readerPDF:(NSString *)fileName password:(NSString *)password
  675. {
  676. ReaderDocument *documentPDF = [ReaderDocument withDocumentFilePath:fileName password:password];
  677. if (documentPDF != nil) {
  678. self.readerPDFViewController = [[ReaderViewController alloc] initWithReaderDocument:documentPDF];
  679. self.readerPDFViewController.view.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height - TOOLBAR_HEIGHT);
  680. [self.readerPDFViewController updateContentViews];
  681. [self addChildViewController:self.readerPDFViewController];
  682. [self.view addSubview:self.readerPDFViewController.view];
  683. [self.readerPDFViewController didMoveToParentViewController:self];
  684. } else {
  685. UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"_error_", nil) message:NSLocalizedString(@"_read_file_error_", nil) delegate:nil cancelButtonTitle:nil otherButtonTitles:NSLocalizedString(@"_ok_", nil), nil];
  686. [alertView show];
  687. }
  688. }
  689. #pragma --------------------------------------------------------------------------------------------
  690. #pragma mark ===== Delete =====
  691. #pragma --------------------------------------------------------------------------------------------
  692. - (void)deleteFileFailure:(NSInteger)errorCode
  693. {
  694. NSLog(@"[LOG] delete failure");
  695. }
  696. - (void)deleteFileSuccess:(CCMetadata *)metadataVar metadataNetVar:(CCMetadataNet *)metadataNetVar
  697. {
  698. // if a message for a directory of these
  699. if (![_dataSourceDirectoryID containsObject:metadataVar.directoryID])
  700. return;
  701. // if we are not in browserPhoto and it's removed photo/video in preview then "< Back"
  702. if (!self.photoBrowser && [self.metadataDetail.fileID isEqualToString:metadataVar.fileID]) {
  703. if ([metadataVar.typeFile isEqualToString:metadataTypeFile_audio])
  704. [app.player.mediaPlayer stop];
  705. NSArray *viewsToRemove = [self.view subviews];
  706. for (id element in viewsToRemove) {
  707. if ([element isMemberOfClass:[UIView class]] || [element isMemberOfClass:[UIToolbar class]] || [element isMemberOfClass:[LMMediaPlayerView class]])
  708. [element removeFromSuperview];
  709. }
  710. self.title = @"";
  711. [self.navigationController popViewControllerAnimated:YES];
  712. } else {
  713. // only photoBrowser if exists
  714. for (NSUInteger index=0; index < [self.dataSourceImagesVideos count] && _photoBrowser; index++ ) {
  715. CCMetadata *metadata = [self.dataSourceImagesVideos objectAtIndex:index];
  716. // ricerca index
  717. if ([metadataVar.fileID isEqualToString:metadata.fileID]) {
  718. [self.dataSourceImagesVideos removeObjectAtIndex:index];
  719. [self.photos removeObjectAtIndex:index];
  720. [self.thumbs removeObjectAtIndex:index];
  721. [self.photoBrowser reloadData];
  722. if ([self.dataSourceImagesVideos count] == 1) {
  723. self.title = [NSString stringWithFormat:@"1 %@ 1", NSLocalizedString(@"of", nil)];
  724. }
  725. if ([self.dataSourceImagesVideos count] == 0) {
  726. self.title = @"";
  727. [self.navigationController popViewControllerAnimated:YES];
  728. }
  729. break;
  730. }
  731. }
  732. }
  733. }
  734. #pragma --------------------------------------------------------------------------------------------
  735. #pragma mark ===== ButtonPressed =====
  736. #pragma --------------------------------------------------------------------------------------------
  737. - (void)actionButtonPressed:(UIBarButtonItem *)sender
  738. {
  739. NSString *filePath;
  740. if ([self.metadataDetail.fileNamePrint length] == 0) return;
  741. if (self.sourceDirectory == sorceDirectoryLocal) {
  742. filePath = [NSString stringWithFormat:@"%@/%@", self.metadataDetail.directoryID, self.metadataDetail.fileNamePrint];
  743. } else {
  744. filePath = [NSString stringWithFormat:@"%@%@", NSTemporaryDirectory(), self.metadataDetail.fileNamePrint];
  745. }
  746. self.docController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:filePath]];
  747. self.docController.delegate = self;
  748. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) [self.docController presentOptionsMenuFromRect:self.view.frame inView:self.view animated:YES];
  749. [self.docController presentOptionsMenuFromBarButtonItem:sender animated:YES];
  750. }
  751. - (void)deleteButtonPressed:(UIBarButtonItem *)sender
  752. {
  753. if ([self.metadataDetail.fileNamePrint length] == 0) return;
  754. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  755. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_delete_", nil)
  756. style:UIAlertActionStyleDestructive
  757. handler:^(UIAlertAction *action) {
  758. [self.delegate deleteFileOrFolder:self.metadataDetail numFile:1 ofFile:1];
  759. }]];
  760. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_cancel_", nil)
  761. style:UIAlertActionStyleCancel
  762. handler:^(UIAlertAction *action) {
  763. [alertController dismissViewControllerAnimated:YES completion:nil];
  764. }]];
  765. alertController.popoverPresentationController.barButtonItem = _buttonDelete;
  766. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  767. [alertController.view layoutIfNeeded];
  768. [self presentViewController:alertController animated:YES completion:NULL];
  769. }
  770. @end