CCDetail.m 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001
  1. //
  2. // CCDetail.m
  3. // Nextcloud
  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 "NCBridgeSwift.h"
  29. #define TOOLBAR_HEIGHT 49.0f
  30. #define alertRequestPasswordPDF 1
  31. @interface CCDetail () <NCTextDelegate, UIDocumentInteractionControllerDelegate>
  32. {
  33. AppDelegate *appDelegate;
  34. UIDocumentInteractionController *docController;
  35. UIBarButtonItem *buttonModifyTxt;
  36. UIBarButtonItem *buttonShare;
  37. UIBarButtonItem *buttonDelete;
  38. NSInteger indexNowVisible;
  39. NSString *ocIdNowVisible;
  40. NSString *fileNameExtension;
  41. }
  42. @end
  43. @implementation CCDetail
  44. #pragma --------------------------------------------------------------------------------------------
  45. #pragma mark ===== init =====
  46. #pragma --------------------------------------------------------------------------------------------
  47. - (id)initWithCoder:(NSCoder *)aDecoder
  48. {
  49. if (self = [super initWithCoder:aDecoder]) {
  50. appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  51. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeTheming) name:@"changeTheming" object:nil];
  52. self.metadataDetail = [[tableMetadata alloc] init];
  53. self.photos = [[NSMutableArray alloc] init];
  54. self.photoDataSource = [NSMutableArray new];
  55. indexNowVisible = -1;
  56. ocIdNowVisible = nil;
  57. appDelegate.activeDetail = self;
  58. }
  59. return self;
  60. }
  61. #pragma --------------------------------------------------------------------------------------------
  62. #pragma mark ===== View =====
  63. #pragma --------------------------------------------------------------------------------------------
  64. - (void)viewDidLoad
  65. {
  66. [super viewDidLoad];
  67. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(insertGeocoderLocation:) name:@"insertGeocoderLocation" object:nil];
  68. self.imageBackground.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"logo"] multiplier:2 color:[NCBrandColor.sharedInstance.brand colorWithAlphaComponent:0.4]];
  69. // Change bar bottom line shadow and remove title back button <"title"
  70. self.navigationController.navigationBar.topItem.title = @"";
  71. self.navigationController.navigationBar.translucent = NO;
  72. self.navigationController.navigationBar.barTintColor = NCBrandColor.sharedInstance.brand;
  73. self.navigationController.navigationBar.tintColor = NCBrandColor.sharedInstance.brandText;
  74. [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : NCBrandColor.sharedInstance.brandText}];
  75. self.navigationController.navigationBar.shadowImage = [CCGraphics generateSinglePixelImageWithColor:NCBrandColor.sharedInstance.brand];
  76. [self.navigationController.navigationBar setAlpha:1];
  77. // TabBar
  78. self.tabBarController.tabBar.hidden = YES;
  79. self.tabBarController.tabBar.translucent = YES;
  80. // Open View
  81. if ([self.metadataDetail.fileNameView length] > 0 || [self.metadataDetail.serverUrl length] > 0 || [self.metadataDetail.ocId length] > 0) {
  82. [self viewFile];
  83. }
  84. }
  85. - (void)viewWillAppear:(BOOL)animated
  86. {
  87. [super viewWillAppear:animated];
  88. // TabBar
  89. self.tabBarController.tabBar.hidden = YES;
  90. self.tabBarController.tabBar.translucent = YES;
  91. self.view.backgroundColor = NCBrandColor.sharedInstance.backgroundView;
  92. }
  93. - (void)viewWillDisappear:(BOOL)animated
  94. {
  95. [super viewWillDisappear:animated];
  96. [self.navigationController setNavigationBarHidden:false];
  97. }
  98. - (void)viewDidDisappear:(BOOL)animated
  99. {
  100. [super viewDidDisappear:animated];
  101. // If AVPlayer in play -> Stop
  102. if (appDelegate.player != nil && appDelegate.player.rate != 0) {
  103. [appDelegate.player pause];
  104. }
  105. // remove Observer AVPlayer
  106. if (self.isMediaObserver) {
  107. self.isMediaObserver = NO;
  108. @try{
  109. [[NCViewerMedia sharedInstance] removeObserver];
  110. }@catch(id anException) { }
  111. }
  112. }
  113. - (void)changeTheming
  114. {
  115. [appDelegate changeTheming:self tableView:nil collectionView:nil];
  116. if (self.toolbar) {
  117. self.toolbar.barTintColor = NCBrandColor.sharedInstance.tabBar;
  118. self.toolbar.tintColor = NCBrandColor.sharedInstance.brandElement;
  119. }
  120. self.view.backgroundColor = NCBrandColor.sharedInstance.backgroundView;
  121. }
  122. - (void)changeToDisplayMode
  123. {
  124. if (_readerPDFViewController) {
  125. [self.readerPDFViewController updateContentViews];
  126. }
  127. }
  128. #pragma --------------------------------------------------------------------------------------------
  129. #pragma mark ===== View File =====
  130. #pragma --------------------------------------------------------------------------------------------
  131. - (void)viewFile
  132. {
  133. // Remove all subview except ..
  134. //for (UIView *view in self.view.superview.subviews) {
  135. // NSInteger tag = view.tag;
  136. //}
  137. // Title
  138. self.navigationController.navigationBar.topItem.title = _metadataDetail.fileNameView;
  139. // verifico se esiste l'icona e se la posso creare
  140. if ([[NSFileManager defaultManager] fileExistsAtPath:[CCUtility getDirectoryProviderStorageIconOcId:self.metadataDetail.ocId fileNameView:self.metadataDetail.fileNameView]] == NO) {
  141. [CCGraphics createNewImageFrom:self.metadataDetail.fileNameView ocId:self.metadataDetail.ocId extension:[self.metadataDetail.fileNameView pathExtension] filterGrayScale:NO typeFile:self.metadataDetail.typeFile writeImage:YES];
  142. }
  143. // remove Observer AVPlayer
  144. if (self.isMediaObserver) {
  145. self.isMediaObserver = NO;
  146. [[NCViewerMedia sharedInstance] removeObserver];
  147. }
  148. // IMAGE
  149. if ([self.metadataDetail.typeFile isEqualToString: k_metadataTypeFile_image]) {
  150. self.edgesForExtendedLayout = UIRectEdgeAll;
  151. [self viewImage];
  152. }
  153. // AUDIO VIDEO
  154. if ([self.metadataDetail.typeFile isEqualToString: k_metadataTypeFile_video] || [self.metadataDetail.typeFile isEqualToString: k_metadataTypeFile_audio]) {
  155. self.edgesForExtendedLayout = UIRectEdgeAll;
  156. [self createToolbar];
  157. [[NCViewerMedia sharedInstance] viewMedia:self.metadataDetail detail:self];
  158. }
  159. // DOCUMENT
  160. if ([self.metadataDetail.typeFile isEqualToString: k_metadataTypeFile_document]) {
  161. fileNameExtension = [[self.metadataDetail.fileNameView pathExtension] uppercaseString];
  162. if ([fileNameExtension isEqualToString:@"PDF"]) {
  163. self.edgesForExtendedLayout = UIRectEdgeBottom;
  164. [self createToolbar];
  165. [self viewPDF:@""];
  166. return;
  167. }
  168. // RichDocument
  169. if ([[NCUtility sharedInstance] isRichDocument:self.metadataDetail] && appDelegate.reachability.isReachable) {
  170. [[NCUtility sharedInstance] startActivityIndicatorWithView:self.view bottom:0];
  171. if ([self.metadataDetail.url isEqualToString:@""]) {
  172. [[OCNetworking sharedManager] createLinkRichdocumentsWithAccount:appDelegate.activeAccount fileId:self.metadataDetail.fileId completion:^(NSString *account, NSString *link, NSString *message, NSInteger errorCode) {
  173. if (errorCode == 0 && [account isEqualToString:appDelegate.activeAccount]) {
  174. self.richDocument = [[NCViewerRichdocument alloc] initWithFrame:self.view.bounds configuration:[WKWebViewConfiguration new]];
  175. [self.view addSubview:self.richDocument];
  176. [self.richDocument viewRichDocumentAt:link detail:self metadata:self.metadataDetail];
  177. } else {
  178. [[NCUtility sharedInstance] stopActivityIndicator];
  179. if (errorCode != 0) {
  180. [appDelegate messageNotification:@"_error_" description:message visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError errorCode:errorCode];
  181. } else {
  182. NSLog(@"[LOG] It has been changed user during networking process, error.");
  183. }
  184. [self.navigationController popViewControllerAnimated:YES];
  185. }
  186. }];
  187. } else {
  188. self.richDocument = [[NCViewerRichdocument alloc] initWithFrame:self.view.bounds configuration:[WKWebViewConfiguration new]];
  189. [self.view addSubview:self.richDocument];
  190. [self.richDocument viewRichDocumentAt:self.metadataDetail.url detail:self metadata:self.metadataDetail];
  191. }
  192. return;
  193. }
  194. self.edgesForExtendedLayout = UIRectEdgeBottom;
  195. [self createToolbar];
  196. [[NCViewerDocumentWeb sharedInstance] viewDocumentWebAt:self.metadataDetail detail:self];
  197. }
  198. }
  199. #pragma --------------------------------------------------------------------------------------------
  200. #pragma mark ===== Toolbar =====
  201. #pragma --------------------------------------------------------------------------------------------
  202. - (void)createToolbar
  203. {
  204. CGFloat safeAreaBottom = 0;
  205. if (@available(iOS 11, *)) {
  206. safeAreaBottom = [UIApplication sharedApplication].delegate.window.safeAreaInsets.bottom;
  207. }
  208. self.toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height - TOOLBAR_HEIGHT - safeAreaBottom, self.view.bounds.size.width, TOOLBAR_HEIGHT)];
  209. UIBarButtonItem *flexible = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
  210. UIBarButtonItem *fixedSpaceMini = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:self action:nil];
  211. fixedSpaceMini.width = 25;
  212. buttonModifyTxt = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"actionSheetModify"] style:UIBarButtonItemStylePlain target:self action:@selector(modifyTxtButtonPressed:)];
  213. if (![NCBrandOptions sharedInstance].disable_openin_file) {
  214. self.buttonAction = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"openFile"] style:UIBarButtonItemStylePlain target:self action:@selector(actionButtonPressed:)];
  215. }
  216. buttonShare = [[UIBarButtonItem alloc] initWithImage:[CCGraphics changeThemingColorImage:[UIImage imageNamed:@"share"] width:50 height:50 color:NCBrandColor.sharedInstance.icon] style:UIBarButtonItemStylePlain target:self action:@selector(shareButtonPressed:)];
  217. buttonDelete = [[UIBarButtonItem alloc] initWithImage:[CCGraphics changeThemingColorImage:[UIImage imageNamed:@"trash"] width:50 height:50 color:NCBrandColor.sharedInstance.icon] style:UIBarButtonItemStylePlain target:self action:@selector(deleteButtonPressed:)];
  218. if ([CCUtility isDocumentModifiableExtension:fileNameExtension]) {
  219. if ([CCUtility isFolderEncrypted:_metadataDetail.serverUrl account:appDelegate.activeAccount]) // E2EE
  220. [self.toolbar setItems:[NSArray arrayWithObjects: buttonModifyTxt, flexible, buttonDelete, fixedSpaceMini, self.buttonAction, nil]];
  221. else
  222. [self.toolbar setItems:[NSArray arrayWithObjects: buttonModifyTxt, flexible, buttonDelete, fixedSpaceMini, buttonShare, fixedSpaceMini, self.buttonAction, nil]];
  223. } else {
  224. if ([CCUtility isFolderEncrypted:_metadataDetail.serverUrl account:appDelegate.activeAccount]) // E2EE
  225. [self.toolbar setItems:[NSArray arrayWithObjects: flexible, buttonDelete, fixedSpaceMini, self.buttonAction, nil]];
  226. else
  227. [self.toolbar setItems:[NSArray arrayWithObjects: flexible, buttonDelete, fixedSpaceMini, buttonShare, fixedSpaceMini, self.buttonAction, nil]];
  228. }
  229. [self.toolbar setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin];
  230. self.toolbar.barTintColor = NCBrandColor.sharedInstance.tabBar;
  231. self.toolbar.tintColor = NCBrandColor.sharedInstance.brandElement;
  232. [self.view addSubview:self.toolbar];
  233. }
  234. #pragma --------------------------------------------------------------------------------------------
  235. #pragma mark ===== View Image =====
  236. #pragma --------------------------------------------------------------------------------------------
  237. - (void)viewImage
  238. {
  239. self.photoBrowser = [[MWPhotoBrowser alloc] initWithDelegate:self];
  240. indexNowVisible = -1;
  241. ocIdNowVisible = nil;
  242. [self.photos removeAllObjects];
  243. // if not images, exit
  244. if ([self.photoDataSource count] == 0)
  245. return;
  246. NSUInteger index = 0;
  247. for (tableMetadata *metadata in self.photoDataSource) {
  248. // start from here ?
  249. if (self.metadataDetail.ocId && [metadata.ocId isEqualToString:self.metadataDetail.ocId])
  250. [self.photoBrowser setCurrentPhotoIndex:index];
  251. [self.photos addObject:[MWPhoto photoWithImage:nil]];
  252. // add directory
  253. index++;
  254. }
  255. // PhotoBrowser
  256. if ([NCBrandOptions sharedInstance].disable_openin_file) {
  257. self.photoBrowser.displayActionButton = NO;
  258. } else {
  259. self.photoBrowser.displayActionButton = YES;
  260. }
  261. self.photoBrowser.displayDeleteButton = YES;
  262. if ([CCUtility isFolderEncrypted:_metadataDetail.serverUrl account:appDelegate.activeAccount]) // E2EE
  263. self.photoBrowser.displayShareButton = NO;
  264. else
  265. self.photoBrowser.displayShareButton = YES;
  266. self.photoBrowser.displayNavArrows = YES;
  267. self.photoBrowser.displaySelectionButtons = NO;
  268. self.photoBrowser.alwaysShowControls = NO;
  269. self.photoBrowser.zoomPhotosToFill = NO;
  270. self.photoBrowser.autoPlayOnAppear = NO;
  271. self.photoBrowser.delayToHideElements = 15;
  272. if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad)
  273. self.photoBrowser.enableSwipeToDismiss = NO;
  274. if (self.traitCollection.horizontalSizeClass == UIUserInterfaceSizeClassCompact) {
  275. [self addChildViewController:self.photoBrowser];
  276. [self.view addSubview:self.photoBrowser.view];
  277. [self.photoBrowser didMoveToParentViewController:self];
  278. } else {
  279. [self.navigationController pushViewController:self.photoBrowser animated:NO];
  280. }
  281. }
  282. - (NSUInteger)numberOfPhotosInPhotoBrowser:(MWPhotoBrowser *)photoBrowser
  283. {
  284. return [self.photoDataSource count];
  285. }
  286. - (NSString *)photoBrowser:(MWPhotoBrowser *)photoBrowser titleForPhotoAtIndex:(NSUInteger)index
  287. {
  288. tableMetadata *metadata = [self.photoDataSource objectAtIndex:index];
  289. NSString *titleDir = metadata.fileNameView;
  290. self.title = titleDir;
  291. return titleDir;
  292. }
  293. - (void)photoBrowser:(MWPhotoBrowser *)photoBrowser didDisplayPhotoAtIndex:(NSUInteger)index
  294. {
  295. tableMetadata *metadata = [self.photoDataSource objectAtIndex:index];
  296. indexNowVisible = index;
  297. ocIdNowVisible = metadata.ocId;
  298. photoBrowser.toolbar.hidden = NO;
  299. // Download image ?
  300. if (metadata) {
  301. NSInteger status;
  302. tableMetadata *metadataDB = [[NCManageDatabase sharedInstance] getMetadataWithPredicate:[NSPredicate predicateWithFormat:@"ocId == %@", metadata.ocId]];
  303. if (metadataDB) {
  304. status = metadataDB.status;
  305. } else {
  306. status = k_metadataStatusNormal;
  307. }
  308. if ([CCUtility fileProviderStorageExists:metadata.ocId fileNameView:metadata.fileNameView] == NO && status == k_metadataStatusNormal) {
  309. if ([[NSFileManager defaultManager] fileExistsAtPath:[CCUtility getDirectoryProviderStorageIconOcId:metadata.ocId fileNameView:metadata.fileNameView]] == NO && metadata.hasPreview) {
  310. [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];
  311. CGFloat width = [[NCUtility sharedInstance] getScreenWidthForPreview];
  312. CGFloat height = [[NCUtility sharedInstance] getScreenHeightForPreview];
  313. [[OCNetworking sharedManager] downloadPreviewWithAccount:appDelegate.activeAccount metadata:metadata withWidth:width andHeight:height completion:^(NSString *account, UIImage *image, NSString *message, NSInteger errorCode) {
  314. self.navigationItem.titleView = nil;
  315. self.title = metadata.fileNameView;
  316. if (errorCode == 0 && [account isEqualToString:appDelegate.activeAccount]) {
  317. [self.photoBrowser reloadData];
  318. }
  319. }];
  320. } else {
  321. [self downloadPhotoBrowser:metadata];
  322. }
  323. }
  324. }
  325. // Title
  326. if (metadata)
  327. self.title = metadata.fileNameView;
  328. }
  329. - (id <MWPhoto>)photoBrowser:(MWPhotoBrowser *)photoBrowser photoAtIndex:(NSUInteger)index
  330. {
  331. UIImage *image;
  332. tableMetadata *metadata = [self.photoDataSource objectAtIndex:index];
  333. if (index < self.photos.count) {
  334. if (metadata.ocId) {
  335. UIImage *imagePreview = [UIImage imageWithContentsOfFile:[CCUtility getDirectoryProviderStorageIconOcId:metadata.ocId fileNameView:metadata.fileNameView]];
  336. // if (!imagePreview) imagePreview = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"file_photo"] multiplier:3 color:[NCBrandColor.sharedInstance icon]];
  337. if ([metadata.typeFile isEqualToString: k_metadataTypeFile_image]) {
  338. NSString *fileImage = [CCUtility getDirectoryProviderStorageOcId:metadata.ocId fileNameView:metadata.fileNameView];
  339. NSString *ext = [CCUtility getExtension:metadata.fileNameView];
  340. if ([ext isEqualToString:@"GIF"]) image = [UIImage animatedImageWithAnimatedGIFURL:[NSURL fileURLWithPath:fileImage]];
  341. else image = [UIImage imageWithContentsOfFile:fileImage];
  342. if (image) {
  343. MWPhoto *photo = [MWPhoto photoWithImage:image];
  344. // Location ??
  345. [self setLocationCaptionPhoto:photo ocId:metadata.ocId];
  346. [self.photos replaceObjectAtIndex:index withObject:photo];
  347. } else {
  348. if (metadata.status == k_metadataStatusDownloadError) {
  349. [self.photos replaceObjectAtIndex:index withObject:[MWPhoto photoWithImage:[UIImage imageNamed:@"filePreviewError"]]];
  350. } else {
  351. if (imagePreview)
  352. [self.photos replaceObjectAtIndex:index withObject:[MWPhoto photoWithImage:imagePreview]];
  353. }
  354. }
  355. }
  356. if ([metadata.typeFile isEqualToString: k_metadataTypeFile_video]) {
  357. if ([CCUtility fileProviderStorageExists:metadata.ocId fileNameView:metadata.fileNameView]) {
  358. NSURL *url = [NSURL fileURLWithPath:[CCUtility getDirectoryProviderStorageOcId:metadata.ocId fileNameView:metadata.fileNameView]];
  359. MWPhoto *video = [MWPhoto photoWithImage:[CCGraphics thumbnailImageForVideo:url atTime:1.0]];
  360. video.videoURL = url;
  361. [self.photos replaceObjectAtIndex:index withObject:video];
  362. } else {
  363. if (metadata.status == k_metadataStatusDownloadError) {
  364. [self.photos replaceObjectAtIndex:index withObject:[MWPhoto photoWithImage:[UIImage imageNamed:@"filePreviewError"]]];
  365. } else {
  366. if (imagePreview)
  367. [self.photos replaceObjectAtIndex:index withObject:[MWPhoto photoWithImage:imagePreview]];
  368. }
  369. }
  370. }
  371. if ([metadata.typeFile isEqualToString: k_metadataTypeFile_audio]) {
  372. if ([CCUtility fileProviderStorageExists:metadata.ocId fileNameView:metadata.fileNameView]) {
  373. MWPhoto *audio;
  374. UIImage *audioImage;
  375. NSURL *url = [NSURL fileURLWithPath:[CCUtility getDirectoryProviderStorageOcId:metadata.ocId fileNameView:metadata.fileNameView]];
  376. if ([[NSFileManager defaultManager] fileExistsAtPath:[CCUtility getDirectoryProviderStorageIconOcId:metadata.ocId fileNameView:metadata.fileNameView]]) {
  377. audioImage = [UIImage imageWithContentsOfFile:[CCUtility getDirectoryProviderStorageIconOcId:metadata.ocId fileNameView:metadata.fileNameView]];
  378. } else {
  379. audioImage = [UIImage imageNamed:@"notaMusic"]; //[CCGraphics scaleImage:[UIImage imageNamed:@"notaMusic"] toSize:CGSizeMake(200, 200) isAspectRation:YES];
  380. }
  381. audio = [MWPhoto photoWithImage:audioImage];
  382. audio.videoURL = url;
  383. [self.photos replaceObjectAtIndex:index withObject:audio];
  384. } else {
  385. if (metadata.status == k_metadataStatusDownloadError) {
  386. [self.photos replaceObjectAtIndex:index withObject:[MWPhoto photoWithImage:[UIImage imageNamed:@"filePreviewError"]]];
  387. } else {
  388. if (imagePreview)
  389. [self.photos replaceObjectAtIndex:index withObject:[MWPhoto photoWithImage:imagePreview]];
  390. }
  391. }
  392. }
  393. }
  394. // energy saving memory
  395. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  396. int iPrev = (int)index - 2;
  397. if (iPrev >= 0) {
  398. if ([self.photos objectAtIndex:iPrev] != nil)
  399. [self.photos replaceObjectAtIndex:iPrev withObject:[MWPhoto photoWithImage:nil]];
  400. }
  401. int iNext = (int)index + 2;
  402. if (iNext < _photos.count) {
  403. if ([self.photos objectAtIndex:iNext] != nil)
  404. [self.photos replaceObjectAtIndex:iNext withObject:[MWPhoto photoWithImage:nil]];
  405. }
  406. });
  407. return [self.photos objectAtIndex:index];
  408. }
  409. return nil;
  410. }
  411. - (void)photoBrowser:(MWPhotoBrowser *)photoBrowser actionButtonPressedForPhotoAtIndex:(NSUInteger)index
  412. {
  413. tableMetadata *metadata = [self.photoDataSource objectAtIndex:index];
  414. if (metadata == nil) return;
  415. docController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:[CCUtility getDirectoryProviderStorageOcId:metadata.ocId fileNameView:metadata.fileNameView]]];
  416. docController.delegate = self;
  417. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
  418. [docController presentOptionsMenuFromRect:photoBrowser.view.frame inView:photoBrowser.view animated:YES];
  419. [docController presentOptionsMenuFromBarButtonItem:photoBrowser.actionButton animated:YES];
  420. }
  421. - (void)photoBrowser:(MWPhotoBrowser *)photoBrowser shareButtonPressedForPhotoAtIndex:(NSUInteger)index
  422. {
  423. tableMetadata *metadata = [self.photoDataSource objectAtIndex:index];
  424. [[NCMainCommon sharedInstance] openShareWithViewController:self metadata:metadata indexPage:2];
  425. }
  426. - (void)photoBrowser:(MWPhotoBrowser *)photoBrowser deleteButtonPressedForPhotoAtIndex:(NSUInteger)index deleteButton:(UIBarButtonItem *)deleteButton
  427. {
  428. tableMetadata *metadata = [self.photoDataSource objectAtIndex:index];
  429. if (metadata == nil || [CCUtility fileProviderStorageExists:metadata.ocId fileNameView:metadata.fileNameView] == NO) {
  430. [appDelegate messageNotification:@"_info_" description:@"_file_not_found_" visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeInfo errorCode:0];
  431. return;
  432. }
  433. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  434. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_delete_", nil)
  435. style:UIAlertActionStyleDestructive
  436. handler:^(UIAlertAction *action) {
  437. [self deleteFile:metadata];
  438. }]];
  439. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_cancel_", nil)
  440. style:UIAlertActionStyleCancel
  441. handler:^(UIAlertAction *action) {
  442. }]];
  443. alertController.popoverPresentationController.barButtonItem = deleteButton;
  444. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  445. [alertController.view layoutIfNeeded];
  446. [self.parentViewController presentViewController:alertController animated:YES completion:NULL];
  447. }
  448. - (void)photoBrowserDidFinishPresentation:(MWPhotoBrowser *)photoBrowser
  449. {
  450. [self.navigationController popViewControllerAnimated:YES];
  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.ocId isEqualToString:ocIdNowVisible])
  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 getDirectoryProviderStorageIconOcId:metadata.ocId fileNameView:metadata.fileNameView]] == NO) {
  463. [CCGraphics createNewImageFrom:metadata.fileNameView ocId:metadata.ocId 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. [[NCMainCommon sharedInstance] reloadDatasourceWithServerUrl:metadata.serverUrl ocId:metadataForDownload.ocId action:k_action_MOD];
  481. [appDelegate startLoadAutoDownloadUpload];
  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 *ocId = [[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 ([ocId isEqualToString:ocIdNowVisible]) {
  494. MWPhoto *photo = [self.photos objectAtIndex:indexNowVisible];
  495. [self setLocationCaptionPhoto:photo ocId:ocId];
  496. [self.photoBrowser reloadData];
  497. }
  498. }
  499. - (void)setLocationCaptionPhoto:(MWPhoto *)photo ocId:(NSString *)ocId
  500. {
  501. tableLocalFile *localFile;
  502. // read Geocoder
  503. localFile = [[NCManageDatabase sharedInstance] getTableLocalFileWithPredicate:[NSPredicate predicateWithFormat:@"ocId == %@", ocId]];
  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:@"ocId == %@", ocId]];
  508. if (metadata) {
  509. [[CCExifGeo sharedInstance] setExifLocalTableEtag:metadata];
  510. }
  511. }
  512. [[CCExifGeo sharedInstance] setGeocoderEtag:ocId exifDate:localFile.exifDate latitude:localFile.exifLatitude longitude:localFile.exifLongitude];
  513. localFile = [[NCManageDatabase sharedInstance] getTableLocalFileWithPredicate:[NSPredicate predicateWithFormat:@"ocId == %@", ocId]];
  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 getDirectoryProviderStorageOcId:self.metadataDetail.ocId fileNameView:self.metadataDetail.fileNameView];
  538. if ([CCUtility fileProviderStorageExists:self.metadataDetail.ocId 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 folderocId:tableDirectory.ocId completion:^(NSInteger errorCode, NSString *message) {
  643. if (errorCode == 0) {
  644. // reload data source
  645. [[NCMainCommon sharedInstance] reloadDatasourceWithServerUrl:tableDirectory.serverUrl ocId:metadata.ocId 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.ocId isEqualToString:metadata.ocId]) {
  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 getDirectoryProviderStorageOcId:self.metadataDetail.ocId 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:@"ocId == %@", self.metadataDetail.ocId]];
  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 getDirectoryProviderStorageOcId:self.metadataDetail.ocId 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. else
  703. [docController presentOptionsMenuFromBarButtonItem:sender animated:YES];
  704. }
  705. - (void)shareButtonPressed:(UIBarButtonItem *)sender
  706. {
  707. [[NCMainCommon sharedInstance] openShareWithViewController:self metadata:self.metadataDetail indexPage:2];
  708. }
  709. - (void)deleteButtonPressed:(UIBarButtonItem *)sender
  710. {
  711. if ([self.metadataDetail.fileNameView length] == 0) return;
  712. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  713. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_delete_", nil)
  714. style:UIAlertActionStyleDestructive
  715. handler:^(UIAlertAction *action) {
  716. [self deleteFile:self.metadataDetail];
  717. }]];
  718. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_cancel_", nil)
  719. style:UIAlertActionStyleCancel
  720. handler:^(UIAlertAction *action) {
  721. [alertController dismissViewControllerAnimated:YES completion:nil];
  722. }]];
  723. alertController.popoverPresentationController.barButtonItem = buttonDelete;
  724. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  725. [alertController.view layoutIfNeeded];
  726. [self presentViewController:alertController animated:YES completion:NULL];
  727. }
  728. @end