CCDetail.m 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991
  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(triggerProgressTask:) name:@"NotificationProgressTask" object:nil];
  53. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeTheming) name:@"changeTheming" object:nil];
  54. self.metadataDetail = [[tableMetadata alloc] init];
  55. self.photos = [[NSMutableArray alloc] init];
  56. self.photoDataSource = [NSMutableArray new];
  57. indexNowVisible = -1;
  58. fileIDNowVisible = nil;
  59. appDelegate.activeDetail = self;
  60. }
  61. return self;
  62. }
  63. #pragma --------------------------------------------------------------------------------------------
  64. #pragma mark ===== View =====
  65. #pragma --------------------------------------------------------------------------------------------
  66. - (void)viewDidLoad
  67. {
  68. [super viewDidLoad];
  69. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(insertGeocoderLocation:) name:@"insertGeocoderLocation" object:nil];
  70. self.imageBackground.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"logo"] multiplier:2 color:[[[NCBrandColor sharedInstance] brand] colorWithAlphaComponent:0.4]];
  71. // Change bar bottom line shadow and remove title back button <"title"
  72. self.navigationController.navigationBar.shadowImage = [CCGraphics generateSinglePixelImageWithColor:[NCBrandColor sharedInstance].brand];
  73. self.navigationController.navigationBar.topItem.title = @"";
  74. // Color Navigation Controller
  75. [appDelegate aspectNavigationControllerBar:self.navigationController.navigationBar online:[appDelegate.reachability isReachable] hidden:NO];
  76. // TabBar
  77. self.tabBarController.tabBar.hidden = YES;
  78. self.tabBarController.tabBar.translucent = YES;
  79. // Open View
  80. if ([self.metadataDetail.fileNameView length] > 0 || [self.metadataDetail.serverUrl length] > 0 || [self.metadataDetail.fileID length] > 0) {
  81. // open view
  82. [self viewFile];
  83. }
  84. }
  85. - (void)viewWillAppear:(BOOL)animated
  86. {
  87. [super viewWillAppear:animated];
  88. self.tabBarController.tabBar.hidden = YES;
  89. self.tabBarController.tabBar.translucent = YES;
  90. }
  91. - (void)viewWillDisappear:(BOOL)animated
  92. {
  93. [super viewWillDisappear:animated];
  94. [self.navigationController setNavigationBarHidden:false];
  95. }
  96. - (void)viewDidDisappear:(BOOL)animated
  97. {
  98. [super viewDidDisappear:animated];
  99. // If AVPlayer in play -> Stop
  100. if (appDelegate.player != nil && appDelegate.player.rate != 0) {
  101. [appDelegate.player pause];
  102. }
  103. // remove Observer AVPlayer
  104. if (self.isMediaObserver) {
  105. self.isMediaObserver = NO;
  106. @try{
  107. [[NCViewerMedia sharedInstance] removeObserver];
  108. }@catch(id anException) { }
  109. }
  110. }
  111. - (void)changeTheming
  112. {
  113. [appDelegate changeTheming:self];
  114. if (self.toolbar) {
  115. self.toolbar.barTintColor = [NCBrandColor sharedInstance].tabBar;
  116. self.toolbar.tintColor = [NCBrandColor sharedInstance].brandElement;
  117. }
  118. }
  119. - (void)changeToDisplayMode
  120. {
  121. if (_readerPDFViewController)
  122. [self.readerPDFViewController updateContentViews];
  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 getDirectoryProviderStorageIconFileID:self.metadataDetail.fileID fileNameView:self.metadataDetail.fileNameView]] == NO) {
  137. [CCGraphics createNewImageFrom:self.metadataDetail.fileNameView fileID:self.metadataDetail.fileID 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 ([[NCViewerRichdocument sharedInstance] isRichDocument:self.metadataDetail]) {
  166. [[NCUtility sharedInstance] startActivityIndicatorWithView:self.view];
  167. if ([self.metadataDetail.url isEqualToString:@""]) {
  168. OCnetworking *ocNetworking = [[OCnetworking alloc] initWithDelegate:nil metadataNet:nil withUser:appDelegate.activeUser withUserID:appDelegate.activeUserID withPassword:appDelegate.activePassword withUrl:appDelegate.activeUrl];
  169. [ocNetworking createLinkRichdocumentsWithFileID:self.metadataDetail.fileID success:^(NSString *link) {
  170. [[NCViewerRichdocument sharedInstance] viewRichDocumentAt:link detail:self];
  171. } failure:^(NSString *message, NSInteger errorCode) {
  172. [[NCUtility sharedInstance] stopActivityIndicator];
  173. [appDelegate messageNotification:@"_error_" description:message visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError errorCode:errorCode];
  174. [self.navigationController popViewControllerAnimated:YES];
  175. }];
  176. } else {
  177. [[NCViewerRichdocument sharedInstance] viewRichDocumentAt:self.metadataDetail.url detail:self];
  178. }
  179. return;
  180. }
  181. self.edgesForExtendedLayout = UIRectEdgeBottom;
  182. [self createToolbar];
  183. [[NCViewerDocumentWeb sharedInstance] viewDocumentWebAt:self.metadataDetail detail:self];
  184. }
  185. }
  186. #pragma --------------------------------------------------------------------------------------------
  187. #pragma mark ===== Toolbar =====
  188. #pragma --------------------------------------------------------------------------------------------
  189. - (void)createToolbar
  190. {
  191. CGFloat safeAreaBottom = 0;
  192. if (@available(iOS 11, *)) {
  193. safeAreaBottom = [UIApplication sharedApplication].delegate.window.safeAreaInsets.bottom;
  194. }
  195. self.toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height - TOOLBAR_HEIGHT - safeAreaBottom, self.view.bounds.size.width, TOOLBAR_HEIGHT)];
  196. UIBarButtonItem *flexible = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
  197. UIBarButtonItem *fixedSpaceMini = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:self action:nil];
  198. fixedSpaceMini.width = 25;
  199. buttonModifyTxt = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"actionSheetModify"] style:UIBarButtonItemStylePlain target:self action:@selector(modifyTxtButtonPressed:)];
  200. if (![NCBrandOptions sharedInstance].disable_openin_file) {
  201. self.buttonAction = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"openFile"] style:UIBarButtonItemStylePlain target:self action:@selector(actionButtonPressed:)];
  202. }
  203. buttonShare = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"share"] style:UIBarButtonItemStylePlain target:self action:@selector(shareButtonPressed:)];
  204. buttonDelete = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"delete"] style:UIBarButtonItemStylePlain target:self action:@selector(deleteButtonPressed:)];
  205. if ([CCUtility isDocumentModifiableExtension:fileNameExtension]) {
  206. if ([CCUtility isFolderEncrypted:_metadataDetail.serverUrl account:appDelegate.activeAccount]) // E2EE
  207. [self.toolbar setItems:[NSArray arrayWithObjects: buttonModifyTxt, flexible, buttonDelete, fixedSpaceMini, self.buttonAction, nil]];
  208. else
  209. [self.toolbar setItems:[NSArray arrayWithObjects: buttonModifyTxt, flexible, buttonDelete, fixedSpaceMini, buttonShare, fixedSpaceMini, self.buttonAction, nil]];
  210. } else {
  211. if ([CCUtility isFolderEncrypted:_metadataDetail.serverUrl account:appDelegate.activeAccount]) // E2EE
  212. [self.toolbar setItems:[NSArray arrayWithObjects: flexible, buttonDelete, fixedSpaceMini, self.buttonAction, nil]];
  213. else
  214. [self.toolbar setItems:[NSArray arrayWithObjects: flexible, buttonDelete, fixedSpaceMini, buttonShare, fixedSpaceMini, self.buttonAction, nil]];
  215. }
  216. [self.toolbar setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin];
  217. self.toolbar.barTintColor = [NCBrandColor sharedInstance].tabBar;
  218. self.toolbar.tintColor = [NCBrandColor sharedInstance].brandElement;
  219. [self.view addSubview:self.toolbar];
  220. }
  221. #pragma --------------------------------------------------------------------------------------------
  222. #pragma mark ===== View Image =====
  223. #pragma --------------------------------------------------------------------------------------------
  224. - (void)viewImage
  225. {
  226. self.photoBrowser = [[MWPhotoBrowser alloc] initWithDelegate:self];
  227. indexNowVisible = -1;
  228. fileIDNowVisible = nil;
  229. [self.photos removeAllObjects];
  230. // if not images, exit
  231. if ([self.photoDataSource count] == 0)
  232. return;
  233. NSUInteger index = 0;
  234. for (tableMetadata *metadata in self.photoDataSource) {
  235. // start from here ?
  236. if (self.metadataDetail.fileID && [metadata.fileID isEqualToString:self.metadataDetail.fileID])
  237. [self.photoBrowser setCurrentPhotoIndex:index];
  238. [self.photos addObject:[MWPhoto photoWithImage:nil]];
  239. // add directory
  240. index++;
  241. }
  242. // PhotoBrowser
  243. if ([NCBrandOptions sharedInstance].disable_openin_file) {
  244. self.photoBrowser.displayActionButton = NO;
  245. } else {
  246. self.photoBrowser.displayActionButton = YES;
  247. }
  248. self.photoBrowser.displayDeleteButton = YES;
  249. if ([CCUtility isFolderEncrypted:_metadataDetail.serverUrl account:appDelegate.activeAccount]) // E2EE
  250. self.photoBrowser.displayShareButton = NO;
  251. else
  252. self.photoBrowser.displayShareButton = YES;
  253. self.photoBrowser.displayNavArrows = YES;
  254. self.photoBrowser.displaySelectionButtons = NO;
  255. self.photoBrowser.alwaysShowControls = NO;
  256. self.photoBrowser.zoomPhotosToFill = NO;
  257. self.photoBrowser.autoPlayOnAppear = NO;
  258. self.photoBrowser.delayToHideElements = 15;
  259. if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad)
  260. self.photoBrowser.enableSwipeToDismiss = NO;
  261. if (self.traitCollection.horizontalSizeClass == UIUserInterfaceSizeClassCompact) {
  262. [self addChildViewController:self.photoBrowser];
  263. [self.view addSubview:self.photoBrowser.view];
  264. [self.photoBrowser didMoveToParentViewController:self];
  265. } else {
  266. [self.navigationController pushViewController:self.photoBrowser animated:NO];
  267. }
  268. }
  269. - (NSUInteger)numberOfPhotosInPhotoBrowser:(MWPhotoBrowser *)photoBrowser
  270. {
  271. return [self.photoDataSource count];
  272. }
  273. - (NSString *)photoBrowser:(MWPhotoBrowser *)photoBrowser titleForPhotoAtIndex:(NSUInteger)index
  274. {
  275. tableMetadata *metadata = [self.photoDataSource objectAtIndex:index];
  276. NSString *titleDir = metadata.fileNameView;
  277. self.title = titleDir;
  278. return titleDir;
  279. }
  280. - (void)photoBrowser:(MWPhotoBrowser *)photoBrowser didDisplayPhotoAtIndex:(NSUInteger)index
  281. {
  282. tableMetadata *metadata = [self.photoDataSource objectAtIndex:index];
  283. indexNowVisible = index;
  284. fileIDNowVisible = metadata.fileID;
  285. photoBrowser.toolbar.hidden = NO;
  286. // Download image ?
  287. if (metadata) {
  288. NSInteger status;
  289. tableMetadata *metadataDB = [[NCManageDatabase sharedInstance] getMetadataWithPredicate:[NSPredicate predicateWithFormat:@"fileID == %@", metadata.fileID]];
  290. if (metadataDB) {
  291. status = metadataDB.status;
  292. } else {
  293. status = k_metadataStatusNormal;
  294. }
  295. if ([CCUtility fileProviderStorageExists:metadata.fileID fileNameView:metadata.fileNameView] == NO && status == k_metadataStatusNormal) {
  296. if ([[NSFileManager defaultManager] fileExistsAtPath:[CCUtility getDirectoryProviderStorageIconFileID:metadata.fileID fileNameView:metadata.fileNameView]] == NO && metadata.hasPreview) {
  297. [CCGraphics addImageToTitle:NSLocalizedString(@"_...loading..._", nil) colorTitle:[NCBrandColor sharedInstance].brandText imageTitle:[CCGraphics changeThemingColorImage:[UIImage imageNamed:@"load"] multiplier:2 color:[NCBrandColor sharedInstance].brandText] imageRight:NO navigationItem:self.navigationItem];
  298. CGFloat width = [[NCUtility sharedInstance] getScreenWidthForPreview];
  299. CGFloat height = [[NCUtility sharedInstance] getScreenHeightForPreview];
  300. OCnetworking *ocNetworking = [[OCnetworking alloc] initWithDelegate:nil metadataNet:nil withUser:appDelegate.activeUser withUserID:appDelegate.activeUserID withPassword:appDelegate.activePassword withUrl:appDelegate.activeUrl];
  301. [ocNetworking downloadPreviewWithMetadata:metadata withWidth:width andHeight:height completion:^(NSString *message, NSInteger errorCode) {
  302. self.navigationItem.titleView = nil;
  303. self.title = metadata.fileNameView;
  304. [self.photoBrowser reloadData];
  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.fileID) {
  321. UIImage *imagePreview = [UIImage imageWithContentsOfFile:[CCUtility getDirectoryProviderStorageIconFileID:metadata.fileID 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 getDirectoryProviderStorageFileID:metadata.fileID 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 fileID:metadata.fileID];
  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.fileID fileNameView:metadata.fileNameView]) {
  344. NSURL *url = [NSURL fileURLWithPath:[CCUtility getDirectoryProviderStorageFileID:metadata.fileID 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.fileID fileNameView:metadata.fileNameView]) {
  359. MWPhoto *audio;
  360. UIImage *audioImage;
  361. NSURL *url = [NSURL fileURLWithPath:[CCUtility getDirectoryProviderStorageFileID:metadata.fileID fileNameView:metadata.fileNameView]];
  362. if ([[NSFileManager defaultManager] fileExistsAtPath:[CCUtility getDirectoryProviderStorageIconFileID:metadata.fileID fileNameView:metadata.fileNameView]]) {
  363. audioImage = [UIImage imageWithContentsOfFile:[CCUtility getDirectoryProviderStorageIconFileID:metadata.fileID 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 getDirectoryProviderStorageFileID:metadata.fileID 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. [appDelegate.activeMain openWindowShare:metadata];
  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.fileID 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)triggerProgressTask:(NSNotification *)notification
  439. {
  440. }
  441. - (void)downloadPhotoBrowserSuccessFailure:(tableMetadata *)metadata selector:(NSString *)selector errorCode:(NSInteger)errorCode
  442. {
  443. // if a message for a directory of these
  444. if (![metadata.fileID isEqualToString:fileIDNowVisible])
  445. return;
  446. // Title
  447. self.navigationItem.titleView = nil;
  448. self.title = metadata.fileNameView;
  449. if (errorCode == 0) {
  450. // verifico se esiste l'icona e se la posso creare
  451. if ([[NSFileManager defaultManager] fileExistsAtPath:[CCUtility getDirectoryProviderStorageIconFileID:metadata.fileID fileNameView:metadata.fileNameView]] == NO) {
  452. [CCGraphics createNewImageFrom:metadata.fileNameView fileID:metadata.fileID extension:[metadata.fileNameView pathExtension] filterGrayScale:NO typeFile:metadata.typeFile writeImage:YES];
  453. }
  454. [self.photoBrowser reloadData];
  455. } else {
  456. [appDelegate messageNotification:@"_download_selected_files_" description:@"_error_download_photobrowser_" visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError errorCode:errorCode];
  457. [self.navigationController popViewControllerAnimated:YES];
  458. }
  459. }
  460. - (void)downloadPhotoBrowser:(tableMetadata *)metadata
  461. {
  462. tableMetadata *metadataForDownload = [[NCManageDatabase sharedInstance] initNewMetadata:metadata];
  463. metadataForDownload.session = k_download_session;
  464. metadataForDownload.sessionError = @"";
  465. metadataForDownload.sessionSelector = selectorLoadViewImage;
  466. metadataForDownload.status = k_metadataStatusWaitDownload;
  467. // Add Metadata for Download
  468. (void)[[NCManageDatabase sharedInstance] addMetadata:metadataForDownload];
  469. [appDelegate performSelectorOnMainThread:@selector(loadAutoDownloadUpload) withObject:nil waitUntilDone:YES];
  470. [[NCMainCommon sharedInstance] reloadDatasourceWithServerUrl:metadata.serverUrl fileID:metadataForDownload.fileID action:k_action_MOD];
  471. [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];
  472. }
  473. - (void)insertGeocoderLocation:(NSNotification *)notification
  474. {
  475. if (notification.userInfo.count == 0)
  476. return;
  477. NSString *fileID = [[notification.userInfo allKeys] objectAtIndex:0];
  478. //NSDate *date = [[notification.userInfo allValues] objectAtIndex:0];
  479. // test [Chrash V 1.14,15]
  480. if (indexNowVisible >= [self.photos count])
  481. return;
  482. if ([fileID isEqualToString:fileIDNowVisible]) {
  483. MWPhoto *photo = [self.photos objectAtIndex:indexNowVisible];
  484. [self setLocationCaptionPhoto:photo fileID:fileID];
  485. [self.photoBrowser reloadData];
  486. }
  487. }
  488. - (void)setLocationCaptionPhoto:(MWPhoto *)photo fileID:(NSString *)fileID
  489. {
  490. tableLocalFile *localFile;
  491. // read Geocoder
  492. localFile = [[NCManageDatabase sharedInstance] getTableLocalFileWithPredicate:[NSPredicate predicateWithFormat:@"fileID == %@", fileID]];
  493. if ([localFile.exifLatitude doubleValue] != 0 || [localFile.exifLongitude doubleValue] != 0) {
  494. // Fix BUG Geo latitude & longitude
  495. if ([localFile.exifLatitude doubleValue] == 9999 || [localFile.exifLongitude doubleValue] == 9999) {
  496. tableMetadata *metadata = [[NCManageDatabase sharedInstance] getMetadataWithPredicate:[NSPredicate predicateWithFormat:@"fileID == %@", fileID]];
  497. if (metadata) {
  498. [[CCExifGeo sharedInstance] setExifLocalTableEtag:metadata];
  499. }
  500. }
  501. [[CCExifGeo sharedInstance] setGeocoderEtag:fileID exifDate:localFile.exifDate latitude:localFile.exifLatitude longitude:localFile.exifLongitude];
  502. localFile = [[NCManageDatabase sharedInstance] getTableLocalFileWithPredicate:[NSPredicate predicateWithFormat:@"fileID == %@", fileID]];
  503. if ([localFile.exifLatitude floatValue] != 0 || [localFile.exifLongitude floatValue] != 0) {
  504. NSString *location = [[NCManageDatabase sharedInstance] getLocationFromGeoLatitude:localFile.exifLatitude longitude:localFile.exifLongitude];
  505. if ([localFile.exifDate isEqualToDate:[NSDate distantPast]] == NO && location) {
  506. NSString *localizedDateTime = [NSDateFormatter localizedStringFromDate:localFile.exifDate dateStyle:NSDateFormatterFullStyle timeStyle:NSDateFormatterMediumStyle];
  507. photo.caption = [NSString stringWithFormat:NSLocalizedString(@"%@\n%@", nil), localizedDateTime, location];
  508. }
  509. }
  510. }
  511. }
  512. #pragma --------------------------------------------------------------------------------------------
  513. #pragma mark ===== View PDF =====
  514. #pragma --------------------------------------------------------------------------------------------
  515. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  516. {
  517. [alertView dismissWithClickedButtonIndex:buttonIndex animated:YES];
  518. [[alertView textFieldAtIndex:0] resignFirstResponder];
  519. if (alertView.tag == alertRequestPasswordPDF) [self performSelector:@selector(viewPDF:) withObject:[alertView textFieldAtIndex:0].text afterDelay:0.3];
  520. }
  521. - (void)viewPDF:(NSString *)password
  522. {
  523. // remove cache PDF
  524. NSString *filePlistReader = [NSString stringWithFormat:@"%@/%@.plist", [CCUtility getDirectoryReaderMetadata], self.metadataDetail.fileNameView.stringByDeletingPathExtension];
  525. [CCUtility removeFileAtPath:filePlistReader];
  526. NSString *fileNamePath = [CCUtility getDirectoryProviderStorageFileID:self.metadataDetail.fileID fileNameView:self.metadataDetail.fileNameView];
  527. if ([CCUtility fileProviderStorageExists:self.metadataDetail.fileID fileNameView:self.metadataDetail.fileNameView] == NO) {
  528. // read file error
  529. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_error_", nil) message:NSLocalizedString(@"_read_file_error_", nil) preferredStyle:UIAlertControllerStyleAlert];
  530. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"_ok_", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {}];
  531. [alertController addAction:okAction];
  532. [self presentViewController:alertController animated:YES completion:nil];
  533. }
  534. CGPDFDocumentRef pdf = CGPDFDocumentCreateWithURL((CFURLRef)[NSURL fileURLWithPath:fileNamePath]);
  535. if (pdf) {
  536. // Encrypted
  537. if (CGPDFDocumentIsEncrypted(pdf) == YES) {
  538. // Try a blank password first, per Apple's Quartz PDF example
  539. if (CGPDFDocumentUnlockWithPassword(pdf, "") == YES) {
  540. // blank password
  541. [self readerPDF:fileNamePath password:@""];
  542. } else {
  543. if ([password length] == 0) {
  544. // password request
  545. UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"_insert_password_pfd_",nil) message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:NSLocalizedString(@"_ok_", nil), nil];
  546. [alertView setAlertViewStyle:UIAlertViewStylePlainTextInput];
  547. alertView.tag = alertRequestPasswordPDF;
  548. [alertView show];
  549. } else {
  550. const char *key = [password UTF8String];
  551. // failure
  552. if (CGPDFDocumentUnlockWithPassword(pdf, key) == NO) {
  553. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_error_", nil) message:NSLocalizedString(@"_password_pdf_error_", nil) preferredStyle:UIAlertControllerStyleAlert];
  554. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"_ok_", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {}];
  555. [alertController addAction:okAction];
  556. [self presentViewController:alertController animated:YES completion:nil];
  557. } else {
  558. // pdf with password
  559. [self readerPDF:fileNamePath password:password];
  560. }
  561. }
  562. }
  563. } else{
  564. // No password
  565. [self readerPDF:fileNamePath password:@""];
  566. }
  567. } else {
  568. // read file error
  569. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_error_", nil) message:NSLocalizedString(@"_read_file_error_", nil) preferredStyle:UIAlertControllerStyleAlert];
  570. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"_ok_", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {}];
  571. [alertController addAction:okAction];
  572. [self presentViewController:alertController animated:YES completion:nil];
  573. }
  574. }
  575. - (void)readerPDF:(NSString *)fileName password:(NSString *)password
  576. {
  577. ReaderDocument *documentPDF = [ReaderDocument withDocumentFilePath:fileName password:password];
  578. CGFloat safeAreaBottom = 0;
  579. if (@available(iOS 11, *)) {
  580. safeAreaBottom = [UIApplication sharedApplication].delegate.window.safeAreaInsets.bottom;
  581. }
  582. if (documentPDF != nil) {
  583. self.readerPDFViewController = [[ReaderViewController alloc] initWithReaderDocument:documentPDF];
  584. self.readerPDFViewController.delegate = self;
  585. self.readerPDFViewController.view.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height - TOOLBAR_HEIGHT - safeAreaBottom);
  586. [self.readerPDFViewController updateContentViews];
  587. [self addChildViewController:self.readerPDFViewController];
  588. [self.view addSubview:self.readerPDFViewController.view];
  589. [self.readerPDFViewController didMoveToParentViewController:self];
  590. } else {
  591. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_error_", nil) message:NSLocalizedString(@"_read_file_error_", nil) preferredStyle:UIAlertControllerStyleAlert];
  592. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"_ok_", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {}];
  593. [alertController addAction:okAction];
  594. [self presentViewController:alertController animated:YES completion:nil];
  595. }
  596. }
  597. - (void)handleSingleTapReader
  598. {
  599. UILayoutGuide *layoutGuide;
  600. CGFloat safeAreaTop = 0;
  601. CGFloat safeAreaBottom = 0;
  602. if (@available(iOS 11, *)) {
  603. layoutGuide = [UIApplication sharedApplication].delegate.window.safeAreaLayoutGuide;
  604. safeAreaTop = [UIApplication sharedApplication].delegate.window.safeAreaInsets.top;
  605. safeAreaBottom = [UIApplication sharedApplication].delegate.window.safeAreaInsets.bottom;
  606. }
  607. self.navigationController.navigationBarHidden = !self.navigationController.navigationBarHidden;
  608. self.toolbar.hidden = !self.toolbar.isHidden;
  609. if (self.toolbar.isHidden) {
  610. self.readerPDFViewController.view.frame = CGRectMake(0, safeAreaTop, self.view.bounds.size.width, self.view.bounds.size.height - safeAreaTop - safeAreaBottom);
  611. } else {
  612. self.readerPDFViewController.view.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height - TOOLBAR_HEIGHT - safeAreaBottom);
  613. }
  614. [self.readerPDFViewController updateContentViews];
  615. }
  616. - (void)handleSwipeUpDown
  617. {
  618. // REMOVE IT'S UNUSABLE
  619. /*
  620. self.navigationController.navigationBarHidden = false; // iOS App is unusable after swipe up or down with PDF in fullscreen #526
  621. [self removeAllView];
  622. [self.navigationController popViewControllerAnimated:YES];
  623. */
  624. }
  625. #pragma --------------------------------------------------------------------------------------------
  626. #pragma mark ===== Delete =====
  627. #pragma --------------------------------------------------------------------------------------------
  628. - (void)deleteFile:(tableMetadata *)metadata
  629. {
  630. tableDirectory *tableDirectory = [[NCManageDatabase sharedInstance] getTableDirectoryWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND e2eEncrypted == 1 AND serverUrl == %@", appDelegate.activeAccount, metadata.serverUrl]];
  631. [[NCMainCommon sharedInstance ] deleteFileWithMetadatas:[[NSArray alloc] initWithObjects:metadata, nil] e2ee:tableDirectory.e2eEncrypted serverUrl:tableDirectory.serverUrl folderFileID:tableDirectory.fileID completion:^(NSInteger errorCode, NSString *message) {
  632. if (errorCode == 0) {
  633. // reload data source
  634. [[NCMainCommon sharedInstance] reloadDatasourceWithServerUrl:tableDirectory.serverUrl fileID:metadata.fileID action:k_action_DEL];
  635. // Not image
  636. if ([self.metadataDetail.typeFile isEqualToString: k_metadataTypeFile_image] == NO) {
  637. // exit
  638. [self.navigationController popViewControllerAnimated:YES];
  639. } else {
  640. for (NSUInteger index=0; index < [self.photoDataSource count] && _photoBrowser; index++ ) {
  641. tableMetadata *metadataTemp = [self.photoDataSource objectAtIndex:index];
  642. if ([metadata isInvalidated] || [metadataTemp.fileID isEqualToString:metadata.fileID]) {
  643. [self.photoDataSource removeObjectAtIndex:index];
  644. [self.photos removeObjectAtIndex:index];
  645. [self.photoBrowser reloadData];
  646. // exit
  647. if ([self.photoDataSource count] == 0) {
  648. [self.navigationController popViewControllerAnimated:YES];
  649. }
  650. }
  651. }
  652. }
  653. } else {
  654. NSLog(@"[LOG] DeleteFileOrFolder failure error %d, %@", (int)errorCode, message);
  655. }
  656. }];
  657. }
  658. #pragma --------------------------------------------------------------------------------------------
  659. #pragma mark ===== ButtonPressed =====
  660. #pragma --------------------------------------------------------------------------------------------
  661. - (void)dismissTextView
  662. {
  663. if (self.webView) {
  664. NSString *fileNamePath = [NSTemporaryDirectory() stringByAppendingString:self.metadataDetail.fileNameView];
  665. [[NSFileManager defaultManager] removeItemAtPath:fileNamePath error:nil];
  666. [[NSFileManager defaultManager] linkItemAtPath:[CCUtility getDirectoryProviderStorageFileID:self.metadataDetail.fileID fileNameView:self.metadataDetail.fileNameView] toPath:fileNamePath error:nil];
  667. [self.webView reload];
  668. }
  669. }
  670. - (void)modifyTxtButtonPressed:(UIBarButtonItem *)sender
  671. {
  672. tableMetadata *metadata = [[NCManageDatabase sharedInstance] getMetadataWithPredicate:[NSPredicate predicateWithFormat:@"fileID == %@", self.metadataDetail.fileID]];
  673. if (metadata) {
  674. UINavigationController* navigationController = [[UIStoryboard storyboardWithName:@"NCText" bundle:nil] instantiateViewControllerWithIdentifier:@"NCText"];
  675. NCText *viewController = (NCText *)navigationController.topViewController;
  676. viewController.metadata = metadata;
  677. viewController.delegate = self;
  678. navigationController.modalPresentationStyle = UIModalPresentationPageSheet;
  679. navigationController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
  680. [self presentViewController:navigationController animated:YES completion:nil];
  681. }
  682. }
  683. - (void)actionButtonPressed:(UIBarButtonItem *)sender
  684. {
  685. if ([self.metadataDetail.fileNameView length] == 0) return;
  686. NSString *filePath = [CCUtility getDirectoryProviderStorageFileID:self.metadataDetail.fileID fileNameView:self.metadataDetail.fileNameView];
  687. docController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:filePath]];
  688. docController.delegate = self;
  689. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
  690. [docController presentOptionsMenuFromRect:self.view.frame inView:self.view animated:YES];
  691. [docController presentOptionsMenuFromBarButtonItem:sender animated:YES];
  692. }
  693. - (void)shareButtonPressed:(UIBarButtonItem *)sender
  694. {
  695. [appDelegate.activeMain openWindowShare:self.metadataDetail];
  696. }
  697. - (void)deleteButtonPressed:(UIBarButtonItem *)sender
  698. {
  699. if ([self.metadataDetail.fileNameView length] == 0) return;
  700. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  701. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_delete_", nil)
  702. style:UIAlertActionStyleDestructive
  703. handler:^(UIAlertAction *action) {
  704. [self deleteFile:self.metadataDetail];
  705. }]];
  706. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_cancel_", nil)
  707. style:UIAlertActionStyleCancel
  708. handler:^(UIAlertAction *action) {
  709. [alertController dismissViewControllerAnimated:YES completion:nil];
  710. }]];
  711. alertController.popoverPresentationController.barButtonItem = buttonDelete;
  712. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  713. [alertController.view layoutIfNeeded];
  714. [self presentViewController:alertController animated:YES completion:NULL];
  715. }
  716. @end