CCDetail.m 44 KB

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