CCDetail.m 44 KB

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