CCDetail.m 44 KB

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