CCDetail.m 43 KB

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