CCDetail.m 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012
  1. //
  2. // CCDetail.m
  3. // Nextcloud iOS
  4. //
  5. // Created by Marino Faggiana on 16/01/15.
  6. // Copyright (c) 2017 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  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. #import <KTVHTTPCache/KTVHTTPCache.h>
  29. #import "NCBridgeSwift.h"
  30. #define TOOLBAR_HEIGHT 49.0f
  31. #define alertRequestPasswordPDF 1
  32. @interface CCDetail () <NCTextDelegate, UIDocumentInteractionControllerDelegate>
  33. {
  34. AppDelegate *appDelegate;
  35. UIDocumentInteractionController *docController;
  36. UIBarButtonItem *buttonModifyTxt;
  37. UIBarButtonItem *buttonShare;
  38. UIBarButtonItem *buttonDelete;
  39. NSInteger indexNowVisible;
  40. NSString *fileIDNowVisible;
  41. NSString *fileNameExtension;
  42. }
  43. @end
  44. @implementation CCDetail
  45. #pragma --------------------------------------------------------------------------------------------
  46. #pragma mark ===== init =====
  47. #pragma --------------------------------------------------------------------------------------------
  48. - (id)initWithCoder:(NSCoder *)aDecoder
  49. {
  50. if (self = [super initWithCoder:aDecoder]) {
  51. appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  52. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeTheming) name:@"changeTheming" object:nil];
  53. self.metadataDetail = [[tableMetadata alloc] init];
  54. self.photos = [[NSMutableArray alloc] init];
  55. self.photoDataSource = [NSMutableArray new];
  56. indexNowVisible = -1;
  57. fileIDNowVisible = nil;
  58. appDelegate.activeDetail = self;
  59. }
  60. return self;
  61. }
  62. #pragma --------------------------------------------------------------------------------------------
  63. #pragma mark ===== View =====
  64. #pragma --------------------------------------------------------------------------------------------
  65. - (void)viewDidLoad
  66. {
  67. [super viewDidLoad];
  68. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(insertGeocoderLocation:) name:@"insertGeocoderLocation" object:nil];
  69. self.imageBackground.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"logo"] multiplier:2 color:[[[NCBrandColor sharedInstance] brand] colorWithAlphaComponent:0.4]];
  70. // Change bar bottom line shadow and remove title back button <"title"
  71. self.navigationController.navigationBar.shadowImage = [CCGraphics generateSinglePixelImageWithColor:[NCBrandColor sharedInstance].brand];
  72. self.navigationController.navigationBar.topItem.title = @"";
  73. // Color Navigation Controller
  74. [appDelegate aspectNavigationControllerBar:self.navigationController.navigationBar online:[appDelegate.reachability isReachable] hidden:NO];
  75. // TabBar
  76. self.tabBarController.tabBar.hidden = YES;
  77. self.tabBarController.tabBar.translucent = YES;
  78. // Open View
  79. if ([self.metadataDetail.fileNameView length] > 0 || [self.metadataDetail.serverUrl length] > 0 || [self.metadataDetail.fileID length] > 0) {
  80. // open view
  81. [self viewFile];
  82. }
  83. }
  84. - (void)viewWillAppear:(BOOL)animated
  85. {
  86. [super viewWillAppear:animated];
  87. self.tabBarController.tabBar.hidden = YES;
  88. self.tabBarController.tabBar.translucent = YES;
  89. }
  90. - (void)viewWillDisappear:(BOOL)animated
  91. {
  92. [super viewWillDisappear:animated];
  93. [self.navigationController setNavigationBarHidden:false];
  94. }
  95. - (void)viewDidDisappear:(BOOL)animated
  96. {
  97. [super viewDidDisappear:animated];
  98. // If AVPlayer in play -> Stop
  99. if (appDelegate.player != nil && appDelegate.player.rate != 0) {
  100. [appDelegate.player pause];
  101. }
  102. // remove Observer AVPlayer
  103. if (self.isMediaObserver) {
  104. self.isMediaObserver = NO;
  105. @try{
  106. [[NCViewerMedia sharedInstance] removeObserver];
  107. }@catch(id anException) { }
  108. }
  109. }
  110. - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
  111. {
  112. [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
  113. [coordinator animateAlongsideTransition:nil completion:^(id<UIViewControllerTransitionCoordinatorContext> context) {
  114. if (self.imagemeter) {
  115. [self.imagemeter audio];
  116. }
  117. }];
  118. }
  119. - (void)changeTheming
  120. {
  121. [appDelegate changeTheming:self];
  122. if (self.toolbar) {
  123. self.toolbar.barTintColor = [NCBrandColor sharedInstance].tabBar;
  124. self.toolbar.tintColor = [NCBrandColor sharedInstance].brandElement;
  125. }
  126. }
  127. - (void)changeToDisplayMode
  128. {
  129. if (_readerPDFViewController)
  130. [self.readerPDFViewController updateContentViews];
  131. }
  132. #pragma --------------------------------------------------------------------------------------------
  133. #pragma mark ===== View File =====
  134. #pragma --------------------------------------------------------------------------------------------
  135. - (void)viewFile
  136. {
  137. // Remove all subview except ..
  138. //for (UIView *view in self.view.superview.subviews) {
  139. // NSInteger tag = view.tag;
  140. //}
  141. // Title
  142. self.navigationController.navigationBar.topItem.title = _metadataDetail.fileNameView;
  143. // verifico se esiste l'icona e se la posso creare
  144. if ([[NSFileManager defaultManager] fileExistsAtPath:[CCUtility getDirectoryProviderStorageIconFileID:self.metadataDetail.fileID fileNameView:self.metadataDetail.fileNameView]] == NO) {
  145. [CCGraphics createNewImageFrom:self.metadataDetail.fileNameView fileID:self.metadataDetail.fileID extension:[self.metadataDetail.fileNameView pathExtension] filterGrayScale:NO typeFile:self.metadataDetail.typeFile writeImage:YES];
  146. }
  147. // remove Observer AVPlayer
  148. if (self.isMediaObserver) {
  149. self.isMediaObserver = NO;
  150. [[NCViewerMedia sharedInstance] removeObserver];
  151. }
  152. // IMAGE
  153. if ([self.metadataDetail.typeFile isEqualToString: k_metadataTypeFile_image]) {
  154. self.edgesForExtendedLayout = UIRectEdgeAll;
  155. [self viewImage];
  156. }
  157. // AUDIO VIDEO
  158. if ([self.metadataDetail.typeFile isEqualToString: k_metadataTypeFile_video] || [self.metadataDetail.typeFile isEqualToString: k_metadataTypeFile_audio]) {
  159. self.edgesForExtendedLayout = UIRectEdgeAll;
  160. [self createToolbar];
  161. [[NCViewerMedia sharedInstance] viewMedia:self.metadataDetail detail:self];
  162. }
  163. // DOCUMENT
  164. if ([self.metadataDetail.typeFile isEqualToString: k_metadataTypeFile_document]) {
  165. fileNameExtension = [[self.metadataDetail.fileNameView pathExtension] uppercaseString];
  166. if ([fileNameExtension isEqualToString:@"PDF"]) {
  167. self.edgesForExtendedLayout = UIRectEdgeBottom;
  168. [self createToolbar];
  169. [self viewPDF:@""];
  170. return;
  171. }
  172. // RichDocument
  173. if ([[NCViewerRichdocument sharedInstance] isRichDocument:self.metadataDetail]) {
  174. [[NCUtility sharedInstance] startActivityIndicatorWithView:self.view bottom:0];
  175. if ([self.metadataDetail.url isEqualToString:@""]) {
  176. [[OCNetworking sharedManager] createLinkRichdocumentsWithAccount:appDelegate.activeAccount fileID:self.metadataDetail.fileID completion:^(NSString *account, NSString *link, NSString *message, NSInteger errorCode) {
  177. if (errorCode == 0 && [account isEqualToString:appDelegate.activeAccount]) {
  178. [[NCViewerRichdocument sharedInstance] viewRichDocumentAt:link detail:self];
  179. } else {
  180. [[NCUtility sharedInstance] stopActivityIndicator];
  181. if (errorCode != 0) {
  182. [appDelegate messageNotification:@"_error_" description:message visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError errorCode:errorCode];
  183. } else {
  184. NSLog(@"[LOG] It has been changed user during networking process, error.");
  185. }
  186. [self.navigationController popViewControllerAnimated:YES];
  187. }
  188. }];
  189. } else {
  190. [[NCViewerRichdocument sharedInstance] viewRichDocumentAt:self.metadataDetail.url detail:self];
  191. }
  192. return;
  193. }
  194. self.edgesForExtendedLayout = UIRectEdgeBottom;
  195. [self createToolbar];
  196. [[NCViewerDocumentWeb sharedInstance] viewDocumentWebAt:self.metadataDetail detail:self];
  197. }
  198. // IMI File
  199. if ([self.metadataDetail.typeFile isEqualToString: k_metadataTypeFile_imagemeter]) {
  200. self.imagemeter = [[NCViewerImagemeter alloc] initWithMetadata:self.metadataDetail detail:self];
  201. [self createToolbar];
  202. }
  203. }
  204. #pragma --------------------------------------------------------------------------------------------
  205. #pragma mark ===== Toolbar =====
  206. #pragma --------------------------------------------------------------------------------------------
  207. - (void)createToolbar
  208. {
  209. CGFloat safeAreaBottom = 0;
  210. if (@available(iOS 11, *)) {
  211. safeAreaBottom = [UIApplication sharedApplication].delegate.window.safeAreaInsets.bottom;
  212. }
  213. self.toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height - TOOLBAR_HEIGHT - safeAreaBottom, self.view.bounds.size.width, TOOLBAR_HEIGHT)];
  214. UIBarButtonItem *flexible = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
  215. UIBarButtonItem *fixedSpaceMini = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:self action:nil];
  216. fixedSpaceMini.width = 25;
  217. buttonModifyTxt = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"actionSheetModify"] style:UIBarButtonItemStylePlain target:self action:@selector(modifyTxtButtonPressed:)];
  218. if (![NCBrandOptions sharedInstance].disable_openin_file) {
  219. self.buttonAction = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"openFile"] style:UIBarButtonItemStylePlain target:self action:@selector(actionButtonPressed:)];
  220. }
  221. buttonShare = [[UIBarButtonItem alloc] initWithImage:[CCGraphics changeThemingColorImage:[UIImage imageNamed:@"share"] width:50 height:50 color:[NCBrandColor sharedInstance].icon] style:UIBarButtonItemStylePlain target:self action:@selector(shareButtonPressed:)];
  222. buttonDelete = [[UIBarButtonItem alloc] initWithImage:[CCGraphics changeThemingColorImage:[UIImage imageNamed:@"trash"] width:50 height:50 color:[NCBrandColor sharedInstance].icon] style:UIBarButtonItemStylePlain target:self action:@selector(deleteButtonPressed:)];
  223. if ([CCUtility isDocumentModifiableExtension:fileNameExtension]) {
  224. if ([CCUtility isFolderEncrypted:_metadataDetail.serverUrl account:appDelegate.activeAccount]) // E2EE
  225. [self.toolbar setItems:[NSArray arrayWithObjects: buttonModifyTxt, flexible, buttonDelete, fixedSpaceMini, self.buttonAction, nil]];
  226. else
  227. [self.toolbar setItems:[NSArray arrayWithObjects: buttonModifyTxt, flexible, buttonDelete, fixedSpaceMini, buttonShare, fixedSpaceMini, self.buttonAction, nil]];
  228. } else {
  229. if ([CCUtility isFolderEncrypted:_metadataDetail.serverUrl account:appDelegate.activeAccount]) // E2EE
  230. [self.toolbar setItems:[NSArray arrayWithObjects: flexible, buttonDelete, fixedSpaceMini, self.buttonAction, nil]];
  231. else
  232. [self.toolbar setItems:[NSArray arrayWithObjects: flexible, buttonDelete, fixedSpaceMini, buttonShare, fixedSpaceMini, self.buttonAction, nil]];
  233. }
  234. [self.toolbar setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin];
  235. self.toolbar.barTintColor = [NCBrandColor sharedInstance].tabBar;
  236. self.toolbar.tintColor = [NCBrandColor sharedInstance].brandElement;
  237. [self.view addSubview:self.toolbar];
  238. }
  239. #pragma --------------------------------------------------------------------------------------------
  240. #pragma mark ===== View Image =====
  241. #pragma --------------------------------------------------------------------------------------------
  242. - (void)viewImage
  243. {
  244. self.photoBrowser = [[MWPhotoBrowser alloc] initWithDelegate:self];
  245. indexNowVisible = -1;
  246. fileIDNowVisible = nil;
  247. [self.photos removeAllObjects];
  248. // if not images, exit
  249. if ([self.photoDataSource count] == 0)
  250. return;
  251. NSUInteger index = 0;
  252. for (tableMetadata *metadata in self.photoDataSource) {
  253. // start from here ?
  254. if (self.metadataDetail.fileID && [metadata.fileID isEqualToString:self.metadataDetail.fileID])
  255. [self.photoBrowser setCurrentPhotoIndex:index];
  256. [self.photos addObject:[MWPhoto photoWithImage:nil]];
  257. // add directory
  258. index++;
  259. }
  260. // PhotoBrowser
  261. if ([NCBrandOptions sharedInstance].disable_openin_file) {
  262. self.photoBrowser.displayActionButton = NO;
  263. } else {
  264. self.photoBrowser.displayActionButton = YES;
  265. }
  266. self.photoBrowser.displayDeleteButton = YES;
  267. if ([CCUtility isFolderEncrypted:_metadataDetail.serverUrl account:appDelegate.activeAccount]) // E2EE
  268. self.photoBrowser.displayShareButton = NO;
  269. else
  270. self.photoBrowser.displayShareButton = YES;
  271. self.photoBrowser.displayNavArrows = YES;
  272. self.photoBrowser.displaySelectionButtons = NO;
  273. self.photoBrowser.alwaysShowControls = NO;
  274. self.photoBrowser.zoomPhotosToFill = NO;
  275. self.photoBrowser.autoPlayOnAppear = NO;
  276. self.photoBrowser.delayToHideElements = 15;
  277. if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad)
  278. self.photoBrowser.enableSwipeToDismiss = NO;
  279. if (self.traitCollection.horizontalSizeClass == UIUserInterfaceSizeClassCompact) {
  280. [self addChildViewController:self.photoBrowser];
  281. [self.view addSubview:self.photoBrowser.view];
  282. [self.photoBrowser didMoveToParentViewController:self];
  283. } else {
  284. [self.navigationController pushViewController:self.photoBrowser animated:NO];
  285. }
  286. }
  287. - (NSUInteger)numberOfPhotosInPhotoBrowser:(MWPhotoBrowser *)photoBrowser
  288. {
  289. return [self.photoDataSource count];
  290. }
  291. - (NSString *)photoBrowser:(MWPhotoBrowser *)photoBrowser titleForPhotoAtIndex:(NSUInteger)index
  292. {
  293. tableMetadata *metadata = [self.photoDataSource objectAtIndex:index];
  294. NSString *titleDir = metadata.fileNameView;
  295. self.title = titleDir;
  296. return titleDir;
  297. }
  298. - (void)photoBrowser:(MWPhotoBrowser *)photoBrowser didDisplayPhotoAtIndex:(NSUInteger)index
  299. {
  300. tableMetadata *metadata = [self.photoDataSource objectAtIndex:index];
  301. indexNowVisible = index;
  302. fileIDNowVisible = metadata.fileID;
  303. photoBrowser.toolbar.hidden = NO;
  304. // Download image ?
  305. if (metadata) {
  306. NSInteger status;
  307. tableMetadata *metadataDB = [[NCManageDatabase sharedInstance] getMetadataWithPredicate:[NSPredicate predicateWithFormat:@"fileID == %@", metadata.fileID]];
  308. if (metadataDB) {
  309. status = metadataDB.status;
  310. } else {
  311. status = k_metadataStatusNormal;
  312. }
  313. if ([CCUtility fileProviderStorageExists:metadata.fileID fileNameView:metadata.fileNameView] == NO && status == k_metadataStatusNormal) {
  314. if ([[NSFileManager defaultManager] fileExistsAtPath:[CCUtility getDirectoryProviderStorageIconFileID:metadata.fileID fileNameView:metadata.fileNameView]] == NO && metadata.hasPreview) {
  315. [CCGraphics addImageToTitle:NSLocalizedString(@"_...loading..._", nil) colorTitle:[NCBrandColor sharedInstance].brandText imageTitle:[CCGraphics changeThemingColorImage:[UIImage imageNamed:@"load"] multiplier:2 color:[NCBrandColor sharedInstance].brandText] imageRight:NO navigationItem:self.navigationItem];
  316. CGFloat width = [[NCUtility sharedInstance] getScreenWidthForPreview];
  317. CGFloat height = [[NCUtility sharedInstance] getScreenHeightForPreview];
  318. [[OCNetworking sharedManager] downloadPreviewWithAccount:appDelegate.activeAccount metadata:metadata withWidth:width andHeight:height completion:^(NSString *account, UIImage *image, NSString *message, NSInteger errorCode) {
  319. self.navigationItem.titleView = nil;
  320. self.title = metadata.fileNameView;
  321. if (errorCode == 0 && [account isEqualToString:appDelegate.activeAccount]) {
  322. [self.photoBrowser reloadData];
  323. }
  324. }];
  325. } else {
  326. [self downloadPhotoBrowser:metadata];
  327. }
  328. }
  329. }
  330. // Title
  331. if (metadata)
  332. self.title = metadata.fileNameView;
  333. }
  334. - (id <MWPhoto>)photoBrowser:(MWPhotoBrowser *)photoBrowser photoAtIndex:(NSUInteger)index
  335. {
  336. UIImage *image;
  337. tableMetadata *metadata = [self.photoDataSource objectAtIndex:index];
  338. if (index < self.photos.count) {
  339. if (metadata.fileID) {
  340. UIImage *imagePreview = [UIImage imageWithContentsOfFile:[CCUtility getDirectoryProviderStorageIconFileID:metadata.fileID fileNameView:metadata.fileNameView]];
  341. // if (!imagePreview) imagePreview = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"file_photo"] multiplier:3 color:[[NCBrandColor sharedInstance] icon]];
  342. if ([metadata.typeFile isEqualToString: k_metadataTypeFile_image]) {
  343. NSString *fileImage = [CCUtility getDirectoryProviderStorageFileID:metadata.fileID fileNameView:metadata.fileNameView];
  344. NSString *ext = [CCUtility getExtension:metadata.fileNameView];
  345. if ([ext isEqualToString:@"GIF"]) image = [UIImage animatedImageWithAnimatedGIFURL:[NSURL fileURLWithPath:fileImage]];
  346. else image = [UIImage imageWithContentsOfFile:fileImage];
  347. if (image) {
  348. MWPhoto *photo = [MWPhoto photoWithImage:image];
  349. // Location ??
  350. [self setLocationCaptionPhoto:photo fileID:metadata.fileID];
  351. [self.photos replaceObjectAtIndex:index withObject:photo];
  352. } else {
  353. if (metadata.status == k_metadataStatusDownloadError) {
  354. [self.photos replaceObjectAtIndex:index withObject:[MWPhoto photoWithImage:[UIImage imageNamed:@"filePreviewError"]]];
  355. } else {
  356. if (imagePreview)
  357. [self.photos replaceObjectAtIndex:index withObject:[MWPhoto photoWithImage:imagePreview]];
  358. }
  359. }
  360. }
  361. if ([metadata.typeFile isEqualToString: k_metadataTypeFile_video]) {
  362. if ([CCUtility fileProviderStorageExists:metadata.fileID fileNameView:metadata.fileNameView]) {
  363. NSURL *url = [NSURL fileURLWithPath:[CCUtility getDirectoryProviderStorageFileID:metadata.fileID fileNameView:metadata.fileNameView]];
  364. MWPhoto *video = [MWPhoto photoWithImage:[CCGraphics thumbnailImageForVideo:url atTime:1.0]];
  365. video.videoURL = url;
  366. [self.photos replaceObjectAtIndex:index withObject:video];
  367. } else {
  368. if (metadata.status == k_metadataStatusDownloadError) {
  369. [self.photos replaceObjectAtIndex:index withObject:[MWPhoto photoWithImage:[UIImage imageNamed:@"filePreviewError"]]];
  370. } else {
  371. if (imagePreview)
  372. [self.photos replaceObjectAtIndex:index withObject:[MWPhoto photoWithImage:imagePreview]];
  373. }
  374. }
  375. }
  376. if ([metadata.typeFile isEqualToString: k_metadataTypeFile_audio]) {
  377. if ([CCUtility fileProviderStorageExists:metadata.fileID fileNameView:metadata.fileNameView]) {
  378. MWPhoto *audio;
  379. UIImage *audioImage;
  380. NSURL *url = [NSURL fileURLWithPath:[CCUtility getDirectoryProviderStorageFileID:metadata.fileID fileNameView:metadata.fileNameView]];
  381. if ([[NSFileManager defaultManager] fileExistsAtPath:[CCUtility getDirectoryProviderStorageIconFileID:metadata.fileID fileNameView:metadata.fileNameView]]) {
  382. audioImage = [UIImage imageWithContentsOfFile:[CCUtility getDirectoryProviderStorageIconFileID:metadata.fileID fileNameView:metadata.fileNameView]];
  383. } else {
  384. audioImage = [UIImage imageNamed:@"notaMusic"]; //[CCGraphics scaleImage:[UIImage imageNamed:@"notaMusic"] toSize:CGSizeMake(200, 200) isAspectRation:YES];
  385. }
  386. audio = [MWPhoto photoWithImage:audioImage];
  387. audio.videoURL = url;
  388. [self.photos replaceObjectAtIndex:index withObject:audio];
  389. } else {
  390. if (metadata.status == k_metadataStatusDownloadError) {
  391. [self.photos replaceObjectAtIndex:index withObject:[MWPhoto photoWithImage:[UIImage imageNamed:@"filePreviewError"]]];
  392. } else {
  393. if (imagePreview)
  394. [self.photos replaceObjectAtIndex:index withObject:[MWPhoto photoWithImage:imagePreview]];
  395. }
  396. }
  397. }
  398. }
  399. // energy saving memory
  400. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  401. int iPrev = (int)index - 2;
  402. if (iPrev >= 0) {
  403. if ([self.photos objectAtIndex:iPrev] != nil)
  404. [self.photos replaceObjectAtIndex:iPrev withObject:[MWPhoto photoWithImage:nil]];
  405. }
  406. int iNext = (int)index + 2;
  407. if (iNext < _photos.count) {
  408. if ([self.photos objectAtIndex:iNext] != nil)
  409. [self.photos replaceObjectAtIndex:iNext withObject:[MWPhoto photoWithImage:nil]];
  410. }
  411. });
  412. return [self.photos objectAtIndex:index];
  413. }
  414. return nil;
  415. }
  416. - (void)photoBrowser:(MWPhotoBrowser *)photoBrowser actionButtonPressedForPhotoAtIndex:(NSUInteger)index
  417. {
  418. tableMetadata *metadata = [self.photoDataSource objectAtIndex:index];
  419. if (metadata == nil) return;
  420. docController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:[CCUtility getDirectoryProviderStorageFileID:metadata.fileID fileNameView:metadata.fileNameView]]];
  421. docController.delegate = self;
  422. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
  423. [docController presentOptionsMenuFromRect:photoBrowser.view.frame inView:photoBrowser.view animated:YES];
  424. [docController presentOptionsMenuFromBarButtonItem:photoBrowser.actionButton animated:YES];
  425. }
  426. - (void)photoBrowser:(MWPhotoBrowser *)photoBrowser shareButtonPressedForPhotoAtIndex:(NSUInteger)index
  427. {
  428. tableMetadata *metadata = [self.photoDataSource objectAtIndex:index];
  429. [appDelegate.activeMain readShareWithAccount:appDelegate.activeAccount openWindow:YES metadata:metadata];
  430. }
  431. - (void)photoBrowser:(MWPhotoBrowser *)photoBrowser deleteButtonPressedForPhotoAtIndex:(NSUInteger)index deleteButton:(UIBarButtonItem *)deleteButton
  432. {
  433. tableMetadata *metadata = [self.photoDataSource objectAtIndex:index];
  434. if (metadata == nil || [CCUtility fileProviderStorageExists:metadata.fileID fileNameView:metadata.fileNameView] == NO) {
  435. [appDelegate messageNotification:@"_info_" description:@"_file_not_found_" visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeInfo errorCode:0];
  436. return;
  437. }
  438. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  439. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_delete_", nil)
  440. style:UIAlertActionStyleDestructive
  441. handler:^(UIAlertAction *action) {
  442. [self deleteFile:metadata];
  443. }]];
  444. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_cancel_", nil)
  445. style:UIAlertActionStyleCancel
  446. handler:^(UIAlertAction *action) {
  447. }]];
  448. alertController.popoverPresentationController.barButtonItem = deleteButton;
  449. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  450. [alertController.view layoutIfNeeded];
  451. [self.parentViewController presentViewController:alertController animated:YES completion:NULL];
  452. }
  453. - (void)photoBrowserDidFinishPresentation:(MWPhotoBrowser *)photoBrowser
  454. {
  455. [self.navigationController popViewControllerAnimated:YES];
  456. }
  457. - (void)downloadPhotoBrowserSuccessFailure:(tableMetadata *)metadata selector:(NSString *)selector errorCode:(NSInteger)errorCode
  458. {
  459. // if a message for a directory of these
  460. if (![metadata.fileID isEqualToString:fileIDNowVisible])
  461. return;
  462. // Title
  463. self.navigationItem.titleView = nil;
  464. self.title = metadata.fileNameView;
  465. if (errorCode == 0) {
  466. // verifico se esiste l'icona e se la posso creare
  467. if ([[NSFileManager defaultManager] fileExistsAtPath:[CCUtility getDirectoryProviderStorageIconFileID:metadata.fileID fileNameView:metadata.fileNameView]] == NO) {
  468. [CCGraphics createNewImageFrom:metadata.fileNameView fileID:metadata.fileID extension:[metadata.fileNameView pathExtension] filterGrayScale:NO typeFile:metadata.typeFile writeImage:YES];
  469. }
  470. [self.photoBrowser reloadData];
  471. } else {
  472. [appDelegate messageNotification:@"_download_selected_files_" description:@"_error_download_photobrowser_" visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError errorCode:errorCode];
  473. [self.navigationController popViewControllerAnimated:YES];
  474. }
  475. }
  476. - (void)downloadPhotoBrowser:(tableMetadata *)metadata
  477. {
  478. tableMetadata *metadataForDownload = [[NCManageDatabase sharedInstance] initNewMetadata:metadata];
  479. metadataForDownload.session = k_download_session;
  480. metadataForDownload.sessionError = @"";
  481. metadataForDownload.sessionSelector = selectorLoadViewImage;
  482. metadataForDownload.status = k_metadataStatusWaitDownload;
  483. // Add Metadata for Download
  484. (void)[[NCManageDatabase sharedInstance] addMetadata:metadataForDownload];
  485. [[NCMainCommon sharedInstance] reloadDatasourceWithServerUrl:metadata.serverUrl fileID:metadataForDownload.fileID action:k_action_MOD];
  486. [appDelegate startLoadAutoDownloadUpload];
  487. [CCGraphics addImageToTitle:NSLocalizedString(@"_...loading..._", nil) colorTitle:[NCBrandColor sharedInstance].brandText imageTitle:[CCGraphics changeThemingColorImage:[UIImage imageNamed:@"load"] multiplier:2 color:[NCBrandColor sharedInstance].brandText] imageRight:NO navigationItem:self.navigationItem];
  488. }
  489. - (void)insertGeocoderLocation:(NSNotification *)notification
  490. {
  491. if (notification.userInfo.count == 0)
  492. return;
  493. NSString *fileID = [[notification.userInfo allKeys] objectAtIndex:0];
  494. //NSDate *date = [[notification.userInfo allValues] objectAtIndex:0];
  495. // test [Chrash V 1.14,15]
  496. if (indexNowVisible >= [self.photos count])
  497. return;
  498. if ([fileID isEqualToString:fileIDNowVisible]) {
  499. MWPhoto *photo = [self.photos objectAtIndex:indexNowVisible];
  500. [self setLocationCaptionPhoto:photo fileID:fileID];
  501. [self.photoBrowser reloadData];
  502. }
  503. }
  504. - (void)setLocationCaptionPhoto:(MWPhoto *)photo fileID:(NSString *)fileID
  505. {
  506. tableLocalFile *localFile;
  507. // read Geocoder
  508. localFile = [[NCManageDatabase sharedInstance] getTableLocalFileWithPredicate:[NSPredicate predicateWithFormat:@"fileID == %@", fileID]];
  509. if ([localFile.exifLatitude doubleValue] != 0 || [localFile.exifLongitude doubleValue] != 0) {
  510. // Fix BUG Geo latitude & longitude
  511. if ([localFile.exifLatitude doubleValue] == 9999 || [localFile.exifLongitude doubleValue] == 9999) {
  512. tableMetadata *metadata = [[NCManageDatabase sharedInstance] getMetadataWithPredicate:[NSPredicate predicateWithFormat:@"fileID == %@", fileID]];
  513. if (metadata) {
  514. [[CCExifGeo sharedInstance] setExifLocalTableEtag:metadata];
  515. }
  516. }
  517. [[CCExifGeo sharedInstance] setGeocoderEtag:fileID exifDate:localFile.exifDate latitude:localFile.exifLatitude longitude:localFile.exifLongitude];
  518. localFile = [[NCManageDatabase sharedInstance] getTableLocalFileWithPredicate:[NSPredicate predicateWithFormat:@"fileID == %@", fileID]];
  519. if ([localFile.exifLatitude floatValue] != 0 || [localFile.exifLongitude floatValue] != 0) {
  520. NSString *location = [[NCManageDatabase sharedInstance] getLocationFromGeoLatitude:localFile.exifLatitude longitude:localFile.exifLongitude];
  521. if ([localFile.exifDate isEqualToDate:[NSDate distantPast]] == NO && location) {
  522. NSString *localizedDateTime = [NSDateFormatter localizedStringFromDate:localFile.exifDate dateStyle:NSDateFormatterFullStyle timeStyle:NSDateFormatterMediumStyle];
  523. photo.caption = [NSString stringWithFormat:NSLocalizedString(@"%@\n%@", nil), localizedDateTime, location];
  524. }
  525. }
  526. }
  527. }
  528. #pragma --------------------------------------------------------------------------------------------
  529. #pragma mark ===== View PDF =====
  530. #pragma --------------------------------------------------------------------------------------------
  531. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  532. {
  533. [alertView dismissWithClickedButtonIndex:buttonIndex animated:YES];
  534. [[alertView textFieldAtIndex:0] resignFirstResponder];
  535. if (alertView.tag == alertRequestPasswordPDF) [self performSelector:@selector(viewPDF:) withObject:[alertView textFieldAtIndex:0].text afterDelay:0.3];
  536. }
  537. - (void)viewPDF:(NSString *)password
  538. {
  539. // remove cache PDF
  540. NSString *filePlistReader = [NSString stringWithFormat:@"%@/%@.plist", [CCUtility getDirectoryReaderMetadata], self.metadataDetail.fileNameView.stringByDeletingPathExtension];
  541. [CCUtility removeFileAtPath:filePlistReader];
  542. NSString *fileNamePath = [CCUtility getDirectoryProviderStorageFileID:self.metadataDetail.fileID fileNameView:self.metadataDetail.fileNameView];
  543. if ([CCUtility fileProviderStorageExists:self.metadataDetail.fileID fileNameView:self.metadataDetail.fileNameView] == NO) {
  544. // read file error
  545. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_error_", nil) message:NSLocalizedString(@"_read_file_error_", nil) preferredStyle:UIAlertControllerStyleAlert];
  546. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"_ok_", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {}];
  547. [alertController addAction:okAction];
  548. [self presentViewController:alertController animated:YES completion:nil];
  549. }
  550. CGPDFDocumentRef pdf = CGPDFDocumentCreateWithURL((CFURLRef)[NSURL fileURLWithPath:fileNamePath]);
  551. if (pdf) {
  552. // Encrypted
  553. if (CGPDFDocumentIsEncrypted(pdf) == YES) {
  554. // Try a blank password first, per Apple's Quartz PDF example
  555. if (CGPDFDocumentUnlockWithPassword(pdf, "") == YES) {
  556. // blank password
  557. [self readerPDF:fileNamePath password:@""];
  558. } else {
  559. if ([password length] == 0) {
  560. // password request
  561. UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"_insert_password_pfd_",nil) message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:NSLocalizedString(@"_ok_", nil), nil];
  562. [alertView setAlertViewStyle:UIAlertViewStylePlainTextInput];
  563. alertView.tag = alertRequestPasswordPDF;
  564. [alertView show];
  565. } else {
  566. const char *key = [password UTF8String];
  567. // failure
  568. if (CGPDFDocumentUnlockWithPassword(pdf, key) == NO) {
  569. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_error_", nil) message:NSLocalizedString(@"_password_pdf_error_", nil) preferredStyle:UIAlertControllerStyleAlert];
  570. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"_ok_", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {}];
  571. [alertController addAction:okAction];
  572. [self presentViewController:alertController animated:YES completion:nil];
  573. } else {
  574. // pdf with password
  575. [self readerPDF:fileNamePath password:password];
  576. }
  577. }
  578. }
  579. } else{
  580. // No password
  581. [self readerPDF:fileNamePath password:@""];
  582. }
  583. } else {
  584. // read file error
  585. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_error_", nil) message:NSLocalizedString(@"_read_file_error_", nil) preferredStyle:UIAlertControllerStyleAlert];
  586. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"_ok_", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {}];
  587. [alertController addAction:okAction];
  588. [self presentViewController:alertController animated:YES completion:nil];
  589. }
  590. }
  591. - (void)readerPDF:(NSString *)fileName password:(NSString *)password
  592. {
  593. ReaderDocument *documentPDF = [ReaderDocument withDocumentFilePath:fileName password:password];
  594. CGFloat safeAreaBottom = 0;
  595. if (@available(iOS 11, *)) {
  596. safeAreaBottom = [UIApplication sharedApplication].delegate.window.safeAreaInsets.bottom;
  597. }
  598. if (documentPDF != nil) {
  599. self.readerPDFViewController = [[ReaderViewController alloc] initWithReaderDocument:documentPDF];
  600. self.readerPDFViewController.delegate = self;
  601. self.readerPDFViewController.view.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height - TOOLBAR_HEIGHT - safeAreaBottom);
  602. [self.readerPDFViewController updateContentViews];
  603. [self addChildViewController:self.readerPDFViewController];
  604. [self.view addSubview:self.readerPDFViewController.view];
  605. [self.readerPDFViewController didMoveToParentViewController:self];
  606. } else {
  607. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_error_", nil) message:NSLocalizedString(@"_read_file_error_", nil) preferredStyle:UIAlertControllerStyleAlert];
  608. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"_ok_", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {}];
  609. [alertController addAction:okAction];
  610. [self presentViewController:alertController animated:YES completion:nil];
  611. }
  612. }
  613. - (void)handleSingleTapReader
  614. {
  615. UILayoutGuide *layoutGuide;
  616. CGFloat safeAreaTop = 0;
  617. CGFloat safeAreaBottom = 0;
  618. if (@available(iOS 11, *)) {
  619. layoutGuide = [UIApplication sharedApplication].delegate.window.safeAreaLayoutGuide;
  620. safeAreaTop = [UIApplication sharedApplication].delegate.window.safeAreaInsets.top;
  621. safeAreaBottom = [UIApplication sharedApplication].delegate.window.safeAreaInsets.bottom;
  622. }
  623. self.navigationController.navigationBarHidden = !self.navigationController.navigationBarHidden;
  624. self.toolbar.hidden = !self.toolbar.isHidden;
  625. if (self.toolbar.isHidden) {
  626. self.readerPDFViewController.view.frame = CGRectMake(0, safeAreaTop, self.view.bounds.size.width, self.view.bounds.size.height - safeAreaTop - safeAreaBottom);
  627. } else {
  628. self.readerPDFViewController.view.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height - TOOLBAR_HEIGHT - safeAreaBottom);
  629. }
  630. [self.readerPDFViewController updateContentViews];
  631. }
  632. - (void)handleSwipeUpDown
  633. {
  634. // REMOVE IT'S UNUSABLE
  635. /*
  636. self.navigationController.navigationBarHidden = false; // iOS App is unusable after swipe up or down with PDF in fullscreen #526
  637. [self removeAllView];
  638. [self.navigationController popViewControllerAnimated:YES];
  639. */
  640. }
  641. #pragma --------------------------------------------------------------------------------------------
  642. #pragma mark ===== Delete =====
  643. #pragma --------------------------------------------------------------------------------------------
  644. - (void)deleteFile:(tableMetadata *)metadata
  645. {
  646. tableDirectory *tableDirectory = [[NCManageDatabase sharedInstance] getTableDirectoryWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND e2eEncrypted == 1 AND serverUrl == %@", appDelegate.activeAccount, metadata.serverUrl]];
  647. [[NCMainCommon sharedInstance ] deleteFileWithMetadatas:[[NSArray alloc] initWithObjects:metadata, nil] e2ee:tableDirectory.e2eEncrypted serverUrl:tableDirectory.serverUrl folderFileID:tableDirectory.fileID completion:^(NSInteger errorCode, NSString *message) {
  648. if (errorCode == 0) {
  649. // reload data source
  650. [[NCMainCommon sharedInstance] reloadDatasourceWithServerUrl:tableDirectory.serverUrl fileID:metadata.fileID action:k_action_DEL];
  651. // Not image
  652. if ([self.metadataDetail.typeFile isEqualToString: k_metadataTypeFile_image] == NO) {
  653. // exit
  654. [self.navigationController popViewControllerAnimated:YES];
  655. } else {
  656. for (NSUInteger index=0; index < [self.photoDataSource count] && _photoBrowser; index++ ) {
  657. tableMetadata *metadataTemp = [self.photoDataSource objectAtIndex:index];
  658. if ([metadata isInvalidated] || [metadataTemp.fileID isEqualToString:metadata.fileID]) {
  659. [self.photoDataSource removeObjectAtIndex:index];
  660. [self.photos removeObjectAtIndex:index];
  661. [self.photoBrowser reloadData];
  662. // exit
  663. if ([self.photoDataSource count] == 0) {
  664. [self.navigationController popViewControllerAnimated:YES];
  665. }
  666. }
  667. }
  668. }
  669. } else {
  670. NSLog(@"[LOG] DeleteFileOrFolder failure error %d, %@", (int)errorCode, message);
  671. }
  672. }];
  673. }
  674. #pragma --------------------------------------------------------------------------------------------
  675. #pragma mark ===== ButtonPressed =====
  676. #pragma --------------------------------------------------------------------------------------------
  677. - (void)dismissTextView
  678. {
  679. if (self.webView) {
  680. NSString *fileNamePath = [NSTemporaryDirectory() stringByAppendingString:self.metadataDetail.fileNameView];
  681. [[NSFileManager defaultManager] removeItemAtPath:fileNamePath error:nil];
  682. [[NSFileManager defaultManager] linkItemAtPath:[CCUtility getDirectoryProviderStorageFileID:self.metadataDetail.fileID fileNameView:self.metadataDetail.fileNameView] toPath:fileNamePath error:nil];
  683. [self.webView reload];
  684. }
  685. }
  686. - (void)modifyTxtButtonPressed:(UIBarButtonItem *)sender
  687. {
  688. tableMetadata *metadata = [[NCManageDatabase sharedInstance] getMetadataWithPredicate:[NSPredicate predicateWithFormat:@"fileID == %@", self.metadataDetail.fileID]];
  689. if (metadata) {
  690. UINavigationController* navigationController = [[UIStoryboard storyboardWithName:@"NCText" bundle:nil] instantiateViewControllerWithIdentifier:@"NCText"];
  691. NCText *viewController = (NCText *)navigationController.topViewController;
  692. viewController.metadata = metadata;
  693. viewController.delegate = self;
  694. navigationController.modalPresentationStyle = UIModalPresentationPageSheet;
  695. navigationController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
  696. [self presentViewController:navigationController animated:YES completion:nil];
  697. }
  698. }
  699. - (void)actionButtonPressed:(UIBarButtonItem *)sender
  700. {
  701. if ([self.metadataDetail.fileNameView length] == 0) return;
  702. NSString *filePath = [CCUtility getDirectoryProviderStorageFileID:self.metadataDetail.fileID fileNameView:self.metadataDetail.fileNameView];
  703. docController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:filePath]];
  704. docController.delegate = self;
  705. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
  706. [docController presentOptionsMenuFromRect:self.view.frame inView:self.view animated:YES];
  707. [docController presentOptionsMenuFromBarButtonItem:sender animated:YES];
  708. }
  709. - (void)shareButtonPressed:(UIBarButtonItem *)sender
  710. {
  711. [appDelegate.activeMain readShareWithAccount:appDelegate.activeAccount openWindow:YES metadata:self.metadataDetail];
  712. }
  713. - (void)deleteButtonPressed:(UIBarButtonItem *)sender
  714. {
  715. if ([self.metadataDetail.fileNameView length] == 0) return;
  716. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  717. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_delete_", nil)
  718. style:UIAlertActionStyleDestructive
  719. handler:^(UIAlertAction *action) {
  720. [self deleteFile:self.metadataDetail];
  721. }]];
  722. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_cancel_", nil)
  723. style:UIAlertActionStyleCancel
  724. handler:^(UIAlertAction *action) {
  725. [alertController dismissViewControllerAnimated:YES completion:nil];
  726. }]];
  727. alertController.popoverPresentationController.barButtonItem = buttonDelete;
  728. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  729. [alertController.view layoutIfNeeded];
  730. [self presentViewController:alertController animated:YES completion:NULL];
  731. }
  732. @end