CCDetail.m 44 KB

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