CCDetail.m 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090
  1. //
  2. // CCDetail.m
  3. // Nextcloud iOS
  4. //
  5. // Created by Marino Faggiana on 16/01/15.
  6. // Copyright (c) 2017 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 "NCUchardet.h"
  27. #import "NCBridgeSwift.h"
  28. #define TOOLBAR_HEIGHT 49.0f
  29. #define alertRequestPasswordPDF 1
  30. @interface CCDetail ()
  31. {
  32. AppDelegate *appDelegate;
  33. UIToolbar *_toolbar;
  34. UIBarButtonItem *_buttonModifyTxt;
  35. UIBarButtonItem *_buttonAction;
  36. UIBarButtonItem *_buttonShare;
  37. UIBarButtonItem *_buttonDelete;
  38. NSInteger _indexNowVisible;
  39. NSString *_fileIDNowVisible;
  40. NSMutableOrderedSet *_dataSourceDirectoryID;
  41. NSString *_fileNameExtension;
  42. CCHud *_hud;
  43. }
  44. @end
  45. @implementation CCDetail
  46. #pragma --------------------------------------------------------------------------------------------
  47. #pragma mark ===== init =====
  48. #pragma --------------------------------------------------------------------------------------------
  49. - (id)initWithCoder:(NSCoder *)aDecoder
  50. {
  51. if (self = [super initWithCoder:aDecoder]) {
  52. appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  53. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(triggerProgressTask:) name:@"NotificationProgressTask" object:nil];
  54. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeTheming) name:@"changeTheming" object:nil];
  55. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(backNavigationController) name:@"detailBack" object:nil];
  56. self.metadataDetail = [[tableMetadata alloc] init];
  57. self.photos = [[NSMutableArray alloc] init];
  58. self.dataSourceImagesVideos = [[NSMutableArray alloc] init];
  59. _dataSourceDirectoryID = [[NSMutableOrderedSet alloc] init];
  60. _hud = [[CCHud alloc] initWithView:[[[UIApplication sharedApplication] delegate] window]];
  61. _indexNowVisible = -1;
  62. _fileIDNowVisible = nil;
  63. appDelegate.activeDetail = self;
  64. }
  65. return self;
  66. }
  67. #pragma --------------------------------------------------------------------------------------------
  68. #pragma mark ===== View =====
  69. #pragma --------------------------------------------------------------------------------------------
  70. - (void)viewDidLoad
  71. {
  72. [super viewDidLoad];
  73. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(insertGeocoderLocation:) name:@"insertGeocoderLocation" object:nil];
  74. self.imageBackground.image = [UIImage imageNamed:@"backgroundDetail"];
  75. // Change bar bottom line shadow
  76. self.navigationController.navigationBar.shadowImage = [CCGraphics generateSinglePixelImageWithColor:[NCBrandColor sharedInstance].brand];
  77. if ([self.metadataDetail.fileNameView length] > 0 || [self.metadataDetail.directoryID length] > 0 || [self.metadataDetail.fileID length] > 0) {
  78. // open view
  79. [self viewFile];
  80. }
  81. }
  82. // Apparirà
  83. - (void)viewWillAppear:(BOOL)animated
  84. {
  85. [super viewWillAppear:animated];
  86. if (self.splitViewController.isCollapsed) {
  87. self.tabBarController.tabBar.hidden = YES;
  88. self.tabBarController.tabBar.translucent = YES;
  89. }
  90. if (self.splitViewController.isCollapsed)
  91. [appDelegate plusButtonVisibile:false];
  92. }
  93. // E' scomparso
  94. - (void)viewDidDisappear:(BOOL)animated
  95. {
  96. [super viewDidDisappear:animated];
  97. // remove all
  98. if (self.isMovingFromParentViewController)
  99. [self removeAllView];
  100. }
  101. - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
  102. {
  103. [coordinator animateAlongsideTransition:nil completion:^(id<UIViewControllerTransitionCoordinatorContext> context) {
  104. }];
  105. [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
  106. }
  107. // remove all view
  108. - (void)removeAllView
  109. {
  110. // Document
  111. if (_webView) {
  112. [_webView removeFromSuperview];
  113. _webView = nil;
  114. }
  115. // PDF
  116. if (_readerPDFViewController) {
  117. [_readerPDFViewController.view removeFromSuperview];
  118. _readerPDFViewController.delegate = nil;
  119. _readerPDFViewController = nil;
  120. }
  121. // Photo-Video-Audio
  122. if (_photoBrowser) {
  123. [_photos removeAllObjects];
  124. _photoBrowser.delegate = nil;
  125. _photoBrowser = nil;
  126. }
  127. // ToolBar
  128. if (_toolbar) {
  129. [_toolbar removeFromSuperview];
  130. _toolbar = nil;
  131. }
  132. // Title
  133. self.title = nil;
  134. }
  135. - (void)backNavigationController
  136. {
  137. [self removeAllView];
  138. [self.navigationController popViewControllerAnimated:NO];
  139. }
  140. - (void)changeToDisplayMode
  141. {
  142. if (_readerPDFViewController)
  143. [self.readerPDFViewController updateContentViews];
  144. }
  145. - (void)createToolbar
  146. {
  147. CGFloat safeAreaBottom = 0;
  148. NSString *serverUrl = [[NCManageDatabase sharedInstance] getServerUrl:_metadataDetail.directoryID];
  149. if (!serverUrl)
  150. return;
  151. if (@available(iOS 11, *)) {
  152. safeAreaBottom = [UIApplication sharedApplication].delegate.window.safeAreaInsets.bottom;
  153. }
  154. _toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height - TOOLBAR_HEIGHT - safeAreaBottom, self.view.bounds.size.width, TOOLBAR_HEIGHT)];
  155. UIBarButtonItem *flexible = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
  156. UIBarButtonItem *fixedSpaceMini = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:self action:nil];
  157. fixedSpaceMini.width = 25;
  158. _buttonModifyTxt = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"actionSheetModify"] style:UIBarButtonItemStylePlain target:self action:@selector(modifyTxtButtonPressed:)];
  159. _buttonAction = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"openFile"] style:UIBarButtonItemStylePlain target:self action:@selector(actionButtonPressed:)];
  160. _buttonShare = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"share"] style:UIBarButtonItemStylePlain target:self action:@selector(shareButtonPressed:)];
  161. _buttonDelete = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:self action:@selector(deleteButtonPressed:)];
  162. if ([CCUtility isDocumentModifiableExtension:_fileNameExtension]) {
  163. if ([CCUtility isFolderEncrypted:serverUrl account:appDelegate.activeAccount]) // E2EE
  164. [_toolbar setItems:[NSArray arrayWithObjects: _buttonModifyTxt, flexible, _buttonDelete, fixedSpaceMini, _buttonAction, nil]];
  165. else
  166. [_toolbar setItems:[NSArray arrayWithObjects: _buttonModifyTxt, flexible, _buttonDelete, fixedSpaceMini, _buttonShare, fixedSpaceMini, _buttonAction, nil]];
  167. } else {
  168. if ([CCUtility isFolderEncrypted:serverUrl account:appDelegate.activeAccount]) // E2EE
  169. [_toolbar setItems:[NSArray arrayWithObjects: flexible, _buttonDelete, fixedSpaceMini, _buttonAction, nil]];
  170. else
  171. [_toolbar setItems:[NSArray arrayWithObjects: flexible, _buttonDelete, fixedSpaceMini, _buttonShare, fixedSpaceMini, _buttonAction, nil]];
  172. }
  173. [_toolbar setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin];
  174. _toolbar.barTintColor = [NCBrandColor sharedInstance].tabBar;
  175. _toolbar.tintColor = [NCBrandColor sharedInstance].brandElement;
  176. [self.view addSubview:_toolbar];
  177. }
  178. - (void)changeTheming
  179. {
  180. [appDelegate changeTheming:self];
  181. if (_toolbar) {
  182. _toolbar.barTintColor = [NCBrandColor sharedInstance].tabBar;
  183. _toolbar.tintColor = [NCBrandColor sharedInstance].brandElement;
  184. }
  185. }
  186. #pragma --------------------------------------------------------------------------------------------
  187. #pragma mark ===== View File =====
  188. #pragma --------------------------------------------------------------------------------------------
  189. - (void)viewFile
  190. {
  191. // verifico se esiste l'icona e se la posso creare
  192. if ([[NSFileManager defaultManager] fileExistsAtPath:[CCUtility getDirectoryProviderStorageIconFileID:self.metadataDetail.fileID fileNameView:self.metadataDetail.fileNameView]] == NO) {
  193. [CCGraphics createNewImageFrom:self.metadataDetail.fileNameView fileID:self.metadataDetail.fileID extension:[self.metadataDetail.fileNameView pathExtension] size:@"m" imageForUpload:NO typeFile:self.metadataDetail.typeFile writeImage:YES optimizedFileName:[CCUtility getOptimizedPhoto]];
  194. }
  195. if ([self.metadataDetail.typeFile isEqualToString: k_metadataTypeFile_image] || [self.metadataDetail.typeFile isEqualToString: k_metadataTypeFile_video] || [self.metadataDetail.typeFile isEqualToString: k_metadataTypeFile_audio]) {
  196. self.edgesForExtendedLayout = UIRectEdgeAll;
  197. [self viewImageVideoAudio];
  198. }
  199. if ([self.metadataDetail.typeFile isEqualToString: k_metadataTypeFile_document]) {
  200. _fileNameExtension = [[self.metadataDetail.fileNameView pathExtension] uppercaseString];
  201. if ([_fileNameExtension isEqualToString:@"PDF"]) {
  202. self.edgesForExtendedLayout = UIRectEdgeBottom;
  203. [self viewPDF:@""];
  204. [self createToolbar];
  205. [appDelegate aspectNavigationControllerBar:self.navigationController.navigationBar online:[appDelegate.reachability isReachable] hidden:NO];
  206. } else {
  207. self.edgesForExtendedLayout = UIRectEdgeBottom;
  208. [self viewDocument];
  209. [self createToolbar];
  210. [appDelegate aspectNavigationControllerBar:self.navigationController.navigationBar online:[appDelegate.reachability isReachable] hidden:NO];
  211. }
  212. }
  213. }
  214. #pragma --------------------------------------------------------------------------------------------
  215. #pragma mark ===== View Document =====
  216. #pragma --------------------------------------------------------------------------------------------
  217. - (void)viewDocument
  218. {
  219. CGFloat safeAreaBottom = 0;
  220. if (@available(iOS 11, *)) {
  221. safeAreaBottom = [UIApplication sharedApplication].delegate.window.safeAreaInsets.bottom;
  222. }
  223. if ([CCUtility fileProviderStorageExists:self.metadataDetail.fileID fileName:self.metadataDetail.fileNameView] == NO) {
  224. [self backNavigationController];
  225. return;
  226. }
  227. NSString *fileNamePath = [NSTemporaryDirectory() stringByAppendingString:self.metadataDetail.fileNameView];
  228. [[NSFileManager defaultManager] removeItemAtPath:fileNamePath error:nil];
  229. [[NSFileManager defaultManager] linkItemAtPath:[CCUtility getDirectoryProviderStorageFileID:self.metadataDetail.fileID fileName:self.metadataDetail.fileNameView] toPath:fileNamePath error:nil];
  230. NSURL *url = [NSURL fileURLWithPath:fileNamePath];
  231. WKPreferences *wkPreferences = [[WKPreferences alloc] init];
  232. wkPreferences.javaScriptEnabled = true;
  233. WKWebViewConfiguration *wkConfig = [[WKWebViewConfiguration alloc] init];
  234. wkConfig.preferences = wkPreferences;
  235. self.webView = [[WKWebView alloc] initWithFrame:(CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height - TOOLBAR_HEIGHT - safeAreaBottom)) configuration:wkConfig];
  236. self.webView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  237. [self.webView setBackgroundColor:[NCBrandColor sharedInstance].backgroundView];
  238. [self.webView setOpaque:NO];
  239. if ( [_fileNameExtension isEqualToString:@"CSS"] || [_fileNameExtension isEqualToString:@"PY"] || [_fileNameExtension isEqualToString:@"XML"] || [_fileNameExtension isEqualToString:@"JS"] ) {
  240. NSString *dataFile = [[NSString alloc] initWithData:[NSData dataWithContentsOfURL:url] encoding:NSASCIIStringEncoding];
  241. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
  242. [self.webView loadHTMLString:[NSString stringWithFormat:@"<div style='font-size:%@;font-family:%@;'><pre>%@",@"40",@"Sans-Serif",dataFile] baseURL:nil];
  243. }else{
  244. [self.webView loadHTMLString:[NSString stringWithFormat:@"<div style='font-size:%@;font-family:%@;'><pre>%@",@"20",@"Sans-Serif",dataFile] baseURL:nil];
  245. }
  246. } else if ([CCUtility isDocumentModifiableExtension:_fileNameExtension]) {
  247. NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
  248. NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:nil delegateQueue:nil];
  249. NSMutableURLRequest *headRequest = [NSMutableURLRequest requestWithURL:url];
  250. [headRequest setHTTPMethod:@"HEAD"];
  251. NSURLSessionDataTask *task = [session dataTaskWithRequest:headRequest completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
  252. dispatch_async(dispatch_get_main_queue(), ^{
  253. NSString *encodingName = [[NCUchardet sharedNUCharDet] encodingStringDetectWithData:data];
  254. [self.webView loadData:[NSData dataWithContentsOfURL: url] MIMEType:response.MIMEType characterEncodingName:encodingName baseURL:url];
  255. });
  256. }];
  257. [task resume];
  258. } else {
  259. [self.webView loadRequest:[NSMutableURLRequest requestWithURL:url]];
  260. }
  261. [self.view addSubview:self.webView];
  262. }
  263. #pragma --------------------------------------------------------------------------------------------
  264. #pragma mark ===== View Image =====
  265. #pragma --------------------------------------------------------------------------------------------
  266. - (void)viewImageVideoAudio
  267. {
  268. self.photoBrowser = [[MWPhotoBrowser alloc] initWithDelegate:self];
  269. _indexNowVisible = -1;
  270. _fileIDNowVisible = nil;
  271. [self.photos removeAllObjects];
  272. [_dataSourceDirectoryID removeAllObjects];
  273. // if not images, exit
  274. if ([self.dataSourceImagesVideos count] == 0)
  275. return;
  276. // test
  277. NSString *serverUrl = [[NCManageDatabase sharedInstance] getServerUrl:_metadataDetail.directoryID];
  278. if (!serverUrl)
  279. return;
  280. NSUInteger index = 0;
  281. for (tableMetadata *metadata in self.dataSourceImagesVideos) {
  282. // start from here ?
  283. if (self.metadataDetail.fileID && [metadata.fileID isEqualToString:self.metadataDetail.fileID])
  284. [self.photoBrowser setCurrentPhotoIndex:index];
  285. [self.photos addObject:[MWPhoto photoWithImage:nil]];
  286. // add directory
  287. [_dataSourceDirectoryID addObject:metadata.directoryID];
  288. index++;
  289. }
  290. // PhotoBrowser
  291. self.photoBrowser.displayActionButton = YES;
  292. self.photoBrowser.displayDeleteButton = YES;
  293. if ([CCUtility isFolderEncrypted:serverUrl account:appDelegate.activeAccount]) // E2EE
  294. self.photoBrowser.displayShareButton = NO;
  295. else
  296. self.photoBrowser.displayShareButton = YES;
  297. self.photoBrowser.displayNavArrows = YES;
  298. self.photoBrowser.displaySelectionButtons = NO;
  299. self.photoBrowser.alwaysShowControls = NO;
  300. self.photoBrowser.zoomPhotosToFill = NO;
  301. self.photoBrowser.autoPlayOnAppear = NO;
  302. self.photoBrowser.delayToHideElements = 15;
  303. if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad)
  304. self.photoBrowser.enableSwipeToDismiss = NO;
  305. if (self.traitCollection.horizontalSizeClass == UIUserInterfaceSizeClassCompact) {
  306. [self addChildViewController:self.photoBrowser];
  307. [self.view addSubview:self.photoBrowser.view];
  308. [self.photoBrowser didMoveToParentViewController:self];
  309. } else {
  310. [self.navigationController pushViewController:self.photoBrowser animated:NO];
  311. }
  312. }
  313. - (NSUInteger)numberOfPhotosInPhotoBrowser:(MWPhotoBrowser *)photoBrowser
  314. {
  315. return [self.dataSourceImagesVideos count];
  316. }
  317. - (NSString *)photoBrowser:(MWPhotoBrowser *)photoBrowser titleForPhotoAtIndex:(NSUInteger)index
  318. {
  319. tableMetadata *metadata = [self.dataSourceImagesVideos objectAtIndex:index];
  320. NSString *titleDir = metadata.fileNameView;
  321. self.title = titleDir;
  322. return titleDir;
  323. }
  324. - (void)photoBrowser:(MWPhotoBrowser *)photoBrowser didDisplayPhotoAtIndex:(NSUInteger)index
  325. {
  326. tableMetadata *metadata = [self.dataSourceImagesVideos objectAtIndex:index];
  327. _indexNowVisible = index;
  328. _fileIDNowVisible = metadata.fileID;
  329. photoBrowser.toolbar.hidden = NO;
  330. // Download image ?
  331. if (metadata) {
  332. tableMetadata *metadataDB = [[NCManageDatabase sharedInstance] getMetadataWithPredicate:[NSPredicate predicateWithFormat:@"fileID == %@", metadata.fileID]];
  333. if ([CCUtility fileProviderStorageExists:metadata.fileID fileName:metadata.fileNameView] == NO && metadataDB.status == k_metadataStatusNormal) {
  334. [self downloadPhotoBrowser:metadata];
  335. }
  336. }
  337. // Title
  338. if (metadata)
  339. self.title = metadata.fileNameView;
  340. }
  341. - (id <MWPhoto>)photoBrowser:(MWPhotoBrowser *)photoBrowser photoAtIndex:(NSUInteger)index
  342. {
  343. UIImage *image;
  344. tableMetadata *metadata = [self.dataSourceImagesVideos objectAtIndex:index];
  345. if (index < self.photos.count) {
  346. if (metadata.fileID) {
  347. if ([metadata.typeFile isEqualToString: k_metadataTypeFile_image]) {
  348. NSString *fileImage = [CCUtility getDirectoryProviderStorageFileID:metadata.fileID fileName:metadata.fileNameView];
  349. NSString *ext = [CCUtility getExtension:metadata.fileNameView];
  350. if ([ext isEqualToString:@"GIF"]) image = [UIImage animatedImageWithAnimatedGIFURL:[NSURL fileURLWithPath:fileImage]];
  351. else image = [UIImage imageWithContentsOfFile:fileImage];
  352. if (image) {
  353. MWPhoto *photo = [MWPhoto photoWithImage:image];
  354. // Location ??
  355. [self setLocationCaptionPhoto:photo fileID:metadata.fileID];
  356. [self.photos replaceObjectAtIndex:index withObject:photo];
  357. } else {
  358. if (metadata.status == k_metadataStatusDownloadError) {
  359. [self.photos replaceObjectAtIndex:index withObject:[MWPhoto photoWithImage:[UIImage imageNamed:@"filePreviewError"]]];
  360. }
  361. }
  362. }
  363. if ([metadata.typeFile isEqualToString: k_metadataTypeFile_video]) {
  364. if ([CCUtility fileProviderStorageExists:metadata.fileID fileName:metadata.fileNameView]) {
  365. NSURL *url = [NSURL fileURLWithPath:[CCUtility getDirectoryProviderStorageFileID:metadata.fileID fileName:metadata.fileNameView]];
  366. MWPhoto *video = [MWPhoto photoWithImage:[CCGraphics thumbnailImageForVideo:url atTime:1.0]];
  367. video.videoURL = url;
  368. [self.photos replaceObjectAtIndex:index withObject:video];
  369. } else {
  370. if (metadata.status == k_metadataStatusDownloadError) {
  371. [self.photos replaceObjectAtIndex:index withObject:[MWPhoto photoWithImage:[UIImage imageNamed:@"filePreviewError"]]];
  372. }
  373. }
  374. }
  375. if ([metadata.typeFile isEqualToString: k_metadataTypeFile_audio]) {
  376. if ([CCUtility fileProviderStorageExists:metadata.fileID fileName:metadata.fileNameView]) {
  377. MWPhoto *audio;
  378. UIImage *audioImage;
  379. NSURL *url = [NSURL fileURLWithPath:[CCUtility getDirectoryProviderStorageFileID:metadata.fileID fileName:metadata.fileNameView]];
  380. if ([[NSFileManager defaultManager] fileExistsAtPath:[CCUtility getDirectoryProviderStorageIconFileID:metadata.fileID fileNameView:metadata.fileNameView]]) {
  381. audioImage = [UIImage imageWithContentsOfFile:[CCUtility getDirectoryProviderStorageIconFileID:metadata.fileID fileNameView:metadata.fileNameView]];
  382. } else {
  383. audioImage = [UIImage imageNamed:@"notaMusic"]; //[CCGraphics scaleImage:[UIImage imageNamed:@"notaMusic"] toSize:CGSizeMake(200, 200) isAspectRation:YES];
  384. }
  385. audio = [MWPhoto photoWithImage:audioImage];
  386. audio.videoURL = url;
  387. [self.photos replaceObjectAtIndex:index withObject:audio];
  388. } else {
  389. if (metadata.status == k_metadataStatusDownloadError) {
  390. [self.photos replaceObjectAtIndex:index withObject:[MWPhoto photoWithImage:[UIImage imageNamed:@"filePreviewError"]]];
  391. }
  392. }
  393. }
  394. }
  395. // energy saving memory
  396. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  397. int iPrev = (int)index - 2;
  398. if (iPrev >= 0) {
  399. if ([self.photos objectAtIndex:iPrev] != nil)
  400. [self.photos replaceObjectAtIndex:iPrev withObject:[MWPhoto photoWithImage:nil]];
  401. }
  402. int iNext = (int)index + 2;
  403. if (iNext < _photos.count) {
  404. if ([self.photos objectAtIndex:iNext] != nil)
  405. [self.photos replaceObjectAtIndex:iNext withObject:[MWPhoto photoWithImage:nil]];
  406. }
  407. });
  408. return [self.photos objectAtIndex:index];
  409. }
  410. return nil;
  411. }
  412. - (void)photoBrowser:(MWPhotoBrowser *)photoBrowser actionButtonPressedForPhotoAtIndex:(NSUInteger)index
  413. {
  414. tableMetadata *metadata = [self.dataSourceImagesVideos objectAtIndex:index];
  415. if (metadata == nil) return;
  416. self.docController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:[CCUtility getDirectoryProviderStorageFileID:metadata.fileID fileName:metadata.fileNameView]]];
  417. self.docController.delegate = self;
  418. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
  419. [self.docController presentOptionsMenuFromRect:photoBrowser.view.frame inView:photoBrowser.view animated:YES];
  420. [self.docController presentOptionsMenuFromBarButtonItem:photoBrowser.actionButton animated:YES];
  421. }
  422. - (void)photoBrowser:(MWPhotoBrowser *)photoBrowser shareButtonPressedForPhotoAtIndex:(NSUInteger)index
  423. {
  424. tableMetadata *metadata = [self.dataSourceImagesVideos objectAtIndex:index];
  425. [appDelegate.activeMain openWindowShare:metadata];
  426. }
  427. - (void)photoBrowser:(MWPhotoBrowser *)photoBrowser deleteButtonPressedForPhotoAtIndex:(NSUInteger)index deleteButton:(UIBarButtonItem *)deleteButton
  428. {
  429. tableMetadata *metadata = [self.dataSourceImagesVideos objectAtIndex:index];
  430. if (metadata == nil || [CCUtility fileProviderStorageExists:metadata.fileID fileName:metadata.fileNameView] == NO) {
  431. [appDelegate messageNotification:@"_info_" description:@"_file_not_found_" visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeInfo errorCode:0];
  432. return;
  433. }
  434. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  435. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_delete_", nil)
  436. style:UIAlertActionStyleDestructive
  437. handler:^(UIAlertAction *action) {
  438. [self deleteFile:metadata];
  439. }]];
  440. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_cancel_", nil)
  441. style:UIAlertActionStyleCancel
  442. handler:^(UIAlertAction *action) {
  443. [alertController dismissViewControllerAnimated:YES completion:nil];
  444. }]];
  445. alertController.popoverPresentationController.barButtonItem = deleteButton;
  446. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  447. [alertController.view layoutIfNeeded];
  448. [self.parentViewController presentViewController:alertController animated:YES completion:NULL];
  449. }
  450. - (void)photoBrowserDidFinishPresentation:(MWPhotoBrowser *)photoBrowser
  451. {
  452. [self removeAllView];
  453. [self.navigationController popViewControllerAnimated:YES];
  454. }
  455. - (void)triggerProgressTask:(NSNotification *)notification
  456. {
  457. NSDictionary *dict = notification.userInfo;
  458. NSString *fileID = [dict valueForKey:@"fileID"];
  459. //NSString *serverUrl = [dict valueForKey:@"serverUrl"];
  460. //long status = [[dict valueForKey:@"status"] longValue];
  461. float progress = [[dict valueForKey:@"progress"] floatValue];
  462. //long long totalBytes = [[dict valueForKey:@"totalBytes"] longLongValue];
  463. //long long totalBytesExpected = [[dict valueForKey:@"totalBytesExpected"] longLongValue];
  464. if ([fileID isEqualToString:_fileIDNowVisible])
  465. [_hud progress:progress];
  466. }
  467. - (void)downloadPhotoBrowserSuccessFailure:(tableMetadata *)metadata selector:(NSString *)selector errorCode:(NSInteger)errorCode
  468. {
  469. // if a message for a directory of these
  470. if (![metadata.fileID isEqualToString:_fileIDNowVisible])
  471. return;
  472. [_hud hideHud];
  473. if (errorCode == 0) {
  474. // verifico se esiste l'icona e se la posso creare
  475. if ([[NSFileManager defaultManager] fileExistsAtPath:[CCUtility getDirectoryProviderStorageIconFileID:metadata.fileID fileNameView:metadata.fileNameView]] == NO)
  476. [CCGraphics createNewImageFrom:metadata.fileNameView fileID:metadata.fileID extension:[metadata.fileNameView pathExtension] size:@"m" imageForUpload:NO typeFile:metadata.typeFile writeImage:YES optimizedFileName:[CCUtility getOptimizedPhoto]];
  477. [self.photoBrowser reloadData];
  478. } else {
  479. [appDelegate messageNotification:@"_download_selected_files_" description:@"_error_download_photobrowser_" visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError errorCode:errorCode];
  480. [self backNavigationController];
  481. }
  482. }
  483. - (void)downloadPhotoBrowser:(tableMetadata *)metadata
  484. {
  485. NSString *serverUrl = [[NCManageDatabase sharedInstance] getServerUrl:metadata.directoryID];
  486. if (serverUrl) {
  487. [_hud visibleHudTitle:@"" mode:MBProgressHUDModeDeterminate color:[NCBrandColor sharedInstance].brandElement];
  488. metadata.session = k_download_session;
  489. metadata.sessionError = @"";
  490. metadata.sessionSelector = selectorLoadViewImage;
  491. metadata.status = k_metadataStatusWaitDownload;
  492. // Add Metadata for Download
  493. (void)[[NCManageDatabase sharedInstance] addMetadata:metadata];
  494. [appDelegate performSelectorOnMainThread:@selector(loadAutoDownloadUpload) withObject:nil waitUntilDone:YES];
  495. }
  496. }
  497. - (void)insertGeocoderLocation:(NSNotification *)notification
  498. {
  499. if (notification.userInfo.count == 0)
  500. return;
  501. NSString *fileID = [[notification.userInfo allKeys] objectAtIndex:0];
  502. //NSDate *date = [[notification.userInfo allValues] objectAtIndex:0];
  503. // test [Chrash V 1.14,15]
  504. if (_indexNowVisible >= [self.photos count])
  505. return;
  506. if ([fileID isEqualToString:_fileIDNowVisible]) {
  507. MWPhoto *photo = [self.photos objectAtIndex:_indexNowVisible];
  508. [self setLocationCaptionPhoto:photo fileID:fileID];
  509. [self.photoBrowser reloadData];
  510. }
  511. }
  512. - (void)setLocationCaptionPhoto:(MWPhoto *)photo fileID:(NSString *)fileID
  513. {
  514. tableLocalFile *localFile;
  515. // read Geocoder
  516. localFile = [[NCManageDatabase sharedInstance] getTableLocalFileWithPredicate:[NSPredicate predicateWithFormat:@"fileID == %@", fileID]];
  517. if ([localFile.exifLatitude doubleValue] != 0 || [localFile.exifLongitude doubleValue] != 0) {
  518. // Fix BUG Geo latitude & longitude
  519. if ([localFile.exifLatitude doubleValue] == 9999 || [localFile.exifLongitude doubleValue] == 9999) {
  520. tableMetadata *metadata = [[NCManageDatabase sharedInstance] getMetadataWithPredicate:[NSPredicate predicateWithFormat:@"fileID == %@", fileID]];
  521. if (metadata) {
  522. [[CCExifGeo sharedInstance] setExifLocalTableEtag:metadata];
  523. }
  524. }
  525. [[CCExifGeo sharedInstance] setGeocoderEtag:fileID exifDate:localFile.exifDate latitude:localFile.exifLatitude longitude:localFile.exifLongitude];
  526. localFile = [[NCManageDatabase sharedInstance] getTableLocalFileWithPredicate:[NSPredicate predicateWithFormat:@"fileID == %@", fileID]];
  527. if ([localFile.exifLatitude floatValue] != 0 || [localFile.exifLongitude floatValue] != 0) {
  528. NSString *location = [[NCManageDatabase sharedInstance] getLocationFromGeoLatitude:localFile.exifLatitude longitude:localFile.exifLongitude];
  529. if ([localFile.exifDate isEqualToDate:[NSDate distantPast]] == NO && location) {
  530. NSString *localizedDateTime = [NSDateFormatter localizedStringFromDate:localFile.exifDate dateStyle:NSDateFormatterFullStyle timeStyle:NSDateFormatterMediumStyle];
  531. photo.caption = [NSString stringWithFormat:NSLocalizedString(@"%@\n%@", nil), localizedDateTime, location];
  532. }
  533. }
  534. }
  535. }
  536. #pragma --------------------------------------------------------------------------------------------
  537. #pragma mark ===== View PDF =====
  538. #pragma --------------------------------------------------------------------------------------------
  539. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  540. {
  541. [alertView dismissWithClickedButtonIndex:buttonIndex animated:YES];
  542. [[alertView textFieldAtIndex:0] resignFirstResponder];
  543. if (alertView.tag == alertRequestPasswordPDF) [self performSelector:@selector(viewPDF:) withObject:[alertView textFieldAtIndex:0].text afterDelay:0.3];
  544. }
  545. - (void)viewPDF:(NSString *)password
  546. {
  547. NSString *fileNamePath = [CCUtility getDirectoryProviderStorageFileID:self.metadataDetail.fileID fileName:self.metadataDetail.fileNameView];
  548. if ([CCUtility fileProviderStorageExists:self.metadataDetail.fileID fileName:self.metadataDetail.fileNameView] == NO) {
  549. // read file error
  550. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_error_", nil) message:NSLocalizedString(@"_read_file_error_", nil) preferredStyle:UIAlertControllerStyleAlert];
  551. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"_ok_", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {}];
  552. [alertController addAction:okAction];
  553. [self presentViewController:alertController animated:YES completion:nil];
  554. }
  555. CGPDFDocumentRef pdf = CGPDFDocumentCreateWithURL((CFURLRef)[NSURL fileURLWithPath:fileNamePath]);
  556. if (pdf) {
  557. // Encrypted
  558. if (CGPDFDocumentIsEncrypted(pdf) == YES) {
  559. // Try a blank password first, per Apple's Quartz PDF example
  560. if (CGPDFDocumentUnlockWithPassword(pdf, "") == YES) {
  561. // blank password
  562. [self readerPDF:fileNamePath password:@""];
  563. } else {
  564. if ([password length] == 0) {
  565. // password request
  566. UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"_insert_password_pfd_",nil) message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:NSLocalizedString(@"_ok_", nil), nil];
  567. [alertView setAlertViewStyle:UIAlertViewStylePlainTextInput];
  568. alertView.tag = alertRequestPasswordPDF;
  569. [alertView show];
  570. } else {
  571. const char *key = [password UTF8String];
  572. // failure
  573. if (CGPDFDocumentUnlockWithPassword(pdf, key) == NO) {
  574. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_error_", nil) message:NSLocalizedString(@"_password_pdf_error_", nil) preferredStyle:UIAlertControllerStyleAlert];
  575. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"_ok_", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {}];
  576. [alertController addAction:okAction];
  577. [self presentViewController:alertController animated:YES completion:nil];
  578. } else {
  579. // pdf with password
  580. [self readerPDF:fileNamePath password:password];
  581. }
  582. }
  583. }
  584. } else{
  585. // No password
  586. [self readerPDF:fileNamePath password:@""];
  587. }
  588. } else {
  589. // read file error
  590. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_error_", nil) message:NSLocalizedString(@"_read_file_error_", nil) preferredStyle:UIAlertControllerStyleAlert];
  591. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"_ok_", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {}];
  592. [alertController addAction:okAction];
  593. [self presentViewController:alertController animated:YES completion:nil];
  594. }
  595. }
  596. - (void)readerPDF:(NSString *)fileName password:(NSString *)password
  597. {
  598. ReaderDocument *documentPDF = [ReaderDocument withDocumentFilePath:fileName password:password];
  599. CGFloat safeAreaBottom = 0;
  600. if (@available(iOS 11, *)) {
  601. safeAreaBottom = [UIApplication sharedApplication].delegate.window.safeAreaInsets.bottom;
  602. }
  603. if (documentPDF != nil) {
  604. self.readerPDFViewController = [[ReaderViewController alloc] initWithReaderDocument:documentPDF];
  605. self.readerPDFViewController.delegate = self;
  606. self.readerPDFViewController.view.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height - TOOLBAR_HEIGHT - safeAreaBottom);
  607. [self.readerPDFViewController updateContentViews];
  608. [self addChildViewController:self.readerPDFViewController];
  609. [self.view addSubview:self.readerPDFViewController.view];
  610. [self.readerPDFViewController didMoveToParentViewController:self];
  611. } else {
  612. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_error_", nil) message:NSLocalizedString(@"_read_file_error_", nil) preferredStyle:UIAlertControllerStyleAlert];
  613. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"_ok_", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {}];
  614. [alertController addAction:okAction];
  615. [self presentViewController:alertController animated:YES completion:nil];
  616. }
  617. }
  618. - (void)handleSingleTapReader
  619. {
  620. UILayoutGuide *layoutGuide;
  621. CGFloat safeAreaTop = 0;
  622. CGFloat safeAreaBottom = 0;
  623. if (@available(iOS 11, *)) {
  624. layoutGuide = [UIApplication sharedApplication].delegate.window.safeAreaLayoutGuide;
  625. safeAreaTop = [UIApplication sharedApplication].delegate.window.safeAreaInsets.top;
  626. safeAreaBottom = [UIApplication sharedApplication].delegate.window.safeAreaInsets.bottom;
  627. }
  628. self.navigationController.navigationBarHidden = !self.navigationController.navigationBarHidden;
  629. _toolbar.hidden = !_toolbar.isHidden;
  630. if (_toolbar.isHidden) {
  631. self.readerPDFViewController.view.frame = CGRectMake(0, safeAreaTop, self.view.bounds.size.width, self.view.bounds.size.height - safeAreaTop - safeAreaBottom);
  632. } else {
  633. self.readerPDFViewController.view.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height - TOOLBAR_HEIGHT - safeAreaBottom);
  634. }
  635. [self.readerPDFViewController updateContentViews];
  636. }
  637. - (void)handleSwipeUpDown
  638. {
  639. // REMOVE IT'S UNUSABLE
  640. /*
  641. self.navigationController.navigationBarHidden = false; // iOS App is unusable after swipe up or down with PDF in fullscreen #526
  642. [self removeAllView];
  643. [self.navigationController popViewControllerAnimated:YES];
  644. */
  645. }
  646. #pragma --------------------------------------------------------------------------------------------
  647. #pragma mark ===== Delete =====
  648. #pragma --------------------------------------------------------------------------------------------
  649. - (void)deleteFile:(tableMetadata *)metadata
  650. {
  651. NSArray *metadatas = [[NSArray alloc] initWithObjects:metadata, nil];
  652. tableDirectory *tableDirectory = [[NCManageDatabase sharedInstance] getTableDirectoryWithPredicate:[NSPredicate predicateWithFormat:@"directoryID == %@", metadata.directoryID]];
  653. if (!tableDirectory)
  654. return;
  655. if ([CCUtility isFolderEncrypted:tableDirectory.serverUrl account:metadata.account]) {
  656. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  657. NSError *error = [[NCNetworkingEndToEnd sharedManager] lockEndToEndFolderEncryptedOnServerUrl:tableDirectory.serverUrl fileID:tableDirectory.fileID user:appDelegate.activeUser userID:appDelegate.activeUserID password:appDelegate.activePassword url:appDelegate.activeUrl];
  658. dispatch_async(dispatch_get_main_queue(), ^{
  659. if (error == nil) {
  660. [self deleteFile:metadatas e2ee:true];
  661. } else {
  662. [appDelegate messageNotification:@"_delete_" description:error.localizedDescription visible:true delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError errorCode:k_CCErrorInternalError];
  663. return;
  664. }
  665. });
  666. });
  667. } else {
  668. [self deleteFile:metadatas e2ee:false];
  669. }
  670. }
  671. - (void)deleteFile:(NSArray *)metadatas e2ee:(BOOL)e2ee
  672. {
  673. NSInteger numDelete = metadatas.count;
  674. __block NSInteger cont = 0;
  675. OCnetworking *ocNetworking = [[OCnetworking alloc] initWithDelegate:nil metadataNet:nil withUser:appDelegate.activeUser withUserID:appDelegate.activeUserID withPassword:appDelegate.activePassword withUrl:appDelegate.activeUrl];
  676. for (tableMetadata *metadata in metadatas) {
  677. NSString *serverUrl = [[NCManageDatabase sharedInstance] getServerUrl:metadata.directoryID];
  678. if (!serverUrl)
  679. continue;
  680. [ocNetworking deleteFileOrFolder:metadata.fileName serverUrl:serverUrl completion:^(NSString *message, NSInteger errorCode) {
  681. if (errorCode == 0 || errorCode == 404) {
  682. [[NSFileManager defaultManager] removeItemAtPath:[CCUtility getDirectoryProviderStorageFileID:metadata.fileID] error:nil];
  683. [[NCManageDatabase sharedInstance] deleteMetadataWithPredicate:[NSPredicate predicateWithFormat:@"fileID == %@", metadata.fileID] clearDateReadDirectoryID:metadata.directoryID];
  684. [[NCManageDatabase sharedInstance] deleteLocalFileWithPredicate:[NSPredicate predicateWithFormat:@"fileID == %@", metadata.fileID]];
  685. [[NCManageDatabase sharedInstance] deletePhotosWithFileID:metadata.fileID];
  686. [[appDelegate activePhotos].fileIDHide addObject:metadata.fileID];
  687. // Directory ?
  688. if (metadata.directory) {
  689. [[NCManageDatabase sharedInstance] deleteDirectoryAndSubDirectoryWithServerUrl:[CCUtility stringAppendServerUrl:serverUrl addFileName:metadata.fileName]];
  690. }
  691. // E2EE (if exists the record)
  692. if (e2ee) {
  693. [[NCManageDatabase sharedInstance] deleteE2eEncryptionWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@ AND fileNameIdentifier == %@", metadata.account, serverUrl, metadata.fileName]];
  694. }
  695. }
  696. if (++cont == numDelete) {
  697. if (e2ee) {
  698. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  699. [[NCNetworkingEndToEnd sharedManager] rebuildAndSendEndToEndMetadataOnServerUrl:serverUrl account:appDelegate.activeAccount user:appDelegate.activeUser userID:appDelegate.activeUserID password:appDelegate.activePassword url:appDelegate.activeUrl];
  700. dispatch_async(dispatch_get_main_queue(), ^{
  701. // ONLY for this View
  702. [self deleteFile:metadata message:message errorCode:errorCode];
  703. });
  704. });
  705. } else {
  706. // ONLY for this View
  707. [self deleteFile:metadata message:message errorCode:errorCode];
  708. }
  709. }
  710. }];
  711. }
  712. }
  713. - (void)deleteFile:(tableMetadata *)metadata message:(NSString *)message errorCode:(NSInteger)errorCode
  714. {
  715. if (errorCode == 0) {
  716. // reload Main
  717. [appDelegate.activeMain reloadDatasource];
  718. // If removed document (web) or PDF close
  719. if (_webView || _readerPDFViewController)
  720. [self removeAllView];
  721. // if a message for a directory of these
  722. if (![_dataSourceDirectoryID containsObject:metadata.directoryID])
  723. return;
  724. // if we are not in browserPhoto and it's removed photo/video in preview then "< Back"
  725. if (!self.photoBrowser && [self.metadataDetail.fileID isEqualToString:metadata.fileID]) {
  726. NSArray *viewsToRemove = [self.view subviews];
  727. for (id element in viewsToRemove) {
  728. if ([element isMemberOfClass:[UIView class]] || [element isMemberOfClass:[UIToolbar class]])
  729. [element removeFromSuperview];
  730. }
  731. self.title = @"";
  732. [self.navigationController popViewControllerAnimated:YES];
  733. } else {
  734. // only photoBrowser if exists
  735. for (NSUInteger index=0; index < [self.dataSourceImagesVideos count] && _photoBrowser; index++ ) {
  736. tableMetadata *metadataTemp = [self.dataSourceImagesVideos objectAtIndex:index];
  737. if ([metadata isInvalidated] || [metadataTemp.fileID isEqualToString:metadata.fileID]) {
  738. [self.dataSourceImagesVideos removeObjectAtIndex:index];
  739. [self.photos removeObjectAtIndex:index];
  740. [self.photoBrowser reloadData];
  741. // Title
  742. if ([self.dataSourceImagesVideos count] == 0) {
  743. self.title = @"";
  744. [self.navigationController popViewControllerAnimated:YES];
  745. }
  746. }
  747. }
  748. }
  749. } else {
  750. NSLog(@"[LOG] DeleteFileOrFolder failure error %d, %@", (int)errorCode, message);
  751. }
  752. }
  753. #pragma --------------------------------------------------------------------------------------------
  754. #pragma mark ===== ButtonPressed =====
  755. #pragma --------------------------------------------------------------------------------------------
  756. - (void)modifyTxtButtonPressed:(UIBarButtonItem *)sender
  757. {
  758. UINavigationController* navigationController = [[UIStoryboard storyboardWithName:@"NCText" bundle:nil] instantiateViewControllerWithIdentifier:@"NCText"];
  759. NCText *viewController = (NCText *)navigationController.topViewController;
  760. viewController.metadata = self.metadataDetail;
  761. navigationController.modalPresentationStyle = UIModalPresentationPageSheet;
  762. navigationController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
  763. [self presentViewController:navigationController animated:YES completion:nil];
  764. }
  765. - (void)actionButtonPressed:(UIBarButtonItem *)sender
  766. {
  767. if ([self.metadataDetail.fileNameView length] == 0) return;
  768. NSString *filePath = [NSString stringWithFormat:@"%@%@", NSTemporaryDirectory(), self.metadataDetail.fileNameView];
  769. self.docController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:filePath]];
  770. self.docController.delegate = self;
  771. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
  772. [self.docController presentOptionsMenuFromRect:self.view.frame inView:self.view animated:YES];
  773. [self.docController presentOptionsMenuFromBarButtonItem:sender animated:YES];
  774. }
  775. - (void)shareButtonPressed:(UIBarButtonItem *)sender
  776. {
  777. [appDelegate.activeMain openWindowShare:self.metadataDetail];
  778. }
  779. - (void)deleteButtonPressed:(UIBarButtonItem *)sender
  780. {
  781. if ([self.metadataDetail.fileNameView length] == 0) return;
  782. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  783. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_delete_", nil)
  784. style:UIAlertActionStyleDestructive
  785. handler:^(UIAlertAction *action) {
  786. [self deleteFile:self.metadataDetail];
  787. }]];
  788. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_cancel_", nil)
  789. style:UIAlertActionStyleCancel
  790. handler:^(UIAlertAction *action) {
  791. [alertController dismissViewControllerAnimated:YES completion:nil];
  792. }]];
  793. alertController.popoverPresentationController.barButtonItem = _buttonDelete;
  794. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  795. [alertController.view layoutIfNeeded];
  796. [self presentViewController:alertController animated:YES completion:NULL];
  797. }
  798. @end