CCDetail.m 44 KB

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