CCDetail.m 44 KB

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