CCDetail.m 44 KB

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