CCDetail.m 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128
  1. //
  2. // CCDetail.m
  3. // Crypto Cloud Technology Nextcloud
  4. //
  5. // Created by Marino Faggiana on 16/01/15.
  6. // Copyright (c) 2017 TWS. All rights reserved.
  7. //
  8. // Author Marino Faggiana <m.faggiana@twsweb.it>
  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 "NCBridgeSwift.h"
  27. #define TOOLBAR_HEIGHT 49.0f
  28. #define alertRequestPasswordPDF 1
  29. @interface CCDetail () <CCActionsDeleteDelegate>
  30. {
  31. AppDelegate *appDelegate;
  32. UIToolbar *_toolbar;
  33. UIBarButtonItem *_buttonModifyTxt;
  34. UIBarButtonItem *_buttonAction;
  35. UIBarButtonItem *_buttonShare;
  36. UIBarButtonItem *_buttonDelete;
  37. NSInteger _indexNowVisible;
  38. NSString *_fileIDNowVisible;
  39. BOOL _reload;
  40. NSMutableOrderedSet *_dataSourceDirectoryID;
  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. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(triggerProgressTask:) name:@"NotificationProgressTask" object:nil];
  52. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeTheming) name:@"changeTheming" object:nil];
  53. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(backNavigationController) name:@"detailBack" object:nil];
  54. self.metadataDetail = [[tableMetadata alloc] init];
  55. self.photos = [[NSMutableArray alloc] init];
  56. self.thumbs = [[NSMutableArray alloc] init];
  57. self.dataSourceImagesVideos = [[NSMutableArray alloc] init];
  58. _dataSourceDirectoryID = [[NSMutableOrderedSet alloc] init];
  59. _indexNowVisible = -1;
  60. _fileIDNowVisible = nil;
  61. app.activeDetail = self;
  62. }
  63. return self;
  64. }
  65. #pragma --------------------------------------------------------------------------------------------
  66. #pragma mark ===== View =====
  67. #pragma --------------------------------------------------------------------------------------------
  68. - (void)viewDidLoad
  69. {
  70. [super viewDidLoad];
  71. appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  72. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(insertGeocoderLocation:) name:@"insertGeocoderLocation" object:nil];
  73. self.imageBackground.image = [UIImage imageNamed:@"backgroundDetail"];
  74. if ([self.metadataDetail.fileName length] > 0 || [self.metadataDetail.directoryID length] > 0 || [self.metadataDetail.fileID length] > 0) {
  75. // open view
  76. [self viewFile];
  77. }
  78. }
  79. // Apparirà
  80. - (void)viewWillAppear:(BOOL)animated
  81. {
  82. [super viewWillAppear:animated];
  83. if (self.splitViewController.isCollapsed) {
  84. self.tabBarController.tabBar.hidden = YES;
  85. self.tabBarController.tabBar.translucent = YES;
  86. }
  87. if (self.splitViewController.isCollapsed)
  88. [app plusButtonVisibile:false];
  89. }
  90. // E' scomparso
  91. - (void)viewDidDisappear:(BOOL)animated
  92. {
  93. [super viewDidDisappear:animated];
  94. // remove all
  95. if (self.isMovingFromParentViewController)
  96. [self removeAllView];
  97. }
  98. - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
  99. {
  100. [coordinator animateAlongsideTransition:nil completion:^(id<UIViewControllerTransitionCoordinatorContext> context) {
  101. }];
  102. [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
  103. }
  104. // remove all view
  105. - (void)removeAllView
  106. {
  107. // Audio
  108. if (appDelegate.player)
  109. [appDelegate.player removeFromSuperview];
  110. // Document
  111. if (_webView) {
  112. [_webView removeFromSuperview];
  113. _webView = nil;
  114. }
  115. // PDF
  116. if (_readerPDFViewController) {
  117. [_readerPDFViewController.view removeFromSuperview];
  118. _readerPDFViewController.delegate = nil;
  119. _readerPDFViewController = nil;
  120. }
  121. // Photo-Video
  122. if (_photoBrowser) {
  123. [_photos removeAllObjects];
  124. [_thumbs removeAllObjects];
  125. _photoBrowser.delegate = nil;
  126. _photoBrowser = nil;
  127. }
  128. // ToolBar
  129. _toolbar = nil;
  130. [self.navigationController popToRootViewControllerAnimated:NO];
  131. self.navigationController.navigationBarHidden = YES;
  132. }
  133. - (void)backNavigationController
  134. {
  135. [self.navigationController popViewControllerAnimated:NO];
  136. }
  137. - (void)changeToDisplayMode
  138. {
  139. if (_readerPDFViewController)
  140. [self.readerPDFViewController updateContentViews];
  141. }
  142. - (void)createToolbar
  143. {
  144. _toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height - TOOLBAR_HEIGHT, self.view.bounds.size.width, TOOLBAR_HEIGHT)];
  145. UIBarButtonItem *flexible = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
  146. UIBarButtonItem *fixedSpaceMini = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:self action:nil];
  147. fixedSpaceMini.width = 25;
  148. _buttonModifyTxt = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"actionSheetModify"] style:UIBarButtonItemStylePlain target:self action:@selector(modifyTxtButtonPressed:)];
  149. _buttonAction = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"actionSheetOpenIn"] style:UIBarButtonItemStylePlain target:self action:@selector(actionButtonPressed:)];
  150. _buttonShare = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"actionSheetShare"] style:UIBarButtonItemStylePlain target:self action:@selector(shareButtonPressed:)];
  151. _buttonDelete = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:self action:@selector(deleteButtonPressed:)];
  152. if ([_fileNameExtension isEqualToString:@"TXT"])
  153. [_toolbar setItems:[NSArray arrayWithObjects: _buttonModifyTxt, flexible, _buttonDelete, fixedSpaceMini, _buttonShare, fixedSpaceMini, _buttonAction, nil]];
  154. else
  155. [_toolbar setItems:[NSArray arrayWithObjects: flexible, _buttonDelete, fixedSpaceMini, _buttonShare, fixedSpaceMini, _buttonAction, nil]];
  156. [_toolbar setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin];
  157. _toolbar.barTintColor = [NCBrandColor sharedInstance].tabBar;
  158. _toolbar.tintColor = [NCBrandColor sharedInstance].brand;
  159. [self.view addSubview:_toolbar];
  160. }
  161. - (void)changeTheming
  162. {
  163. if (self.isViewLoaded && self.view.window)
  164. [app changeTheming:self];
  165. if (_toolbar)
  166. [self createToolbar];
  167. }
  168. #pragma --------------------------------------------------------------------------------------------
  169. #pragma mark ===== View File =====
  170. #pragma --------------------------------------------------------------------------------------------
  171. - (void)viewFile
  172. {
  173. // verifico se esiste l'icona e se la posso creare
  174. if ([[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"%@/%@.ico", app.directoryUser, self.metadataDetail.fileID]] == NO) {
  175. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),^{
  176. [CCGraphics createNewImageFrom:self.metadataDetail.fileID directoryUser:app.directoryUser fileNameTo:self.metadataDetail.fileID fileNamePrint:self.metadataDetail.fileNamePrint size:@"m" imageForUpload:NO typeFile:self.metadataDetail.typeFile writePreview:YES optimizedFileName:[CCUtility getOptimizedPhoto]];
  177. });
  178. }
  179. if ([self.metadataDetail.typeFile isEqualToString: k_metadataTypeFile_image] || [self.metadataDetail.typeFile isEqualToString: k_metadataTypeFile_video]) {
  180. self.edgesForExtendedLayout = UIRectEdgeAll;
  181. [self viewImage];
  182. }
  183. if ([self.metadataDetail.typeFile isEqualToString: k_metadataTypeFile_audio]) {
  184. self.edgesForExtendedLayout = UIRectEdgeBottom;
  185. [self viewAudio];
  186. [self createToolbar];
  187. [app aspectNavigationControllerBar:self.navigationController.navigationBar encrypted:NO online:[app.reachability isReachable] hidden:NO];
  188. }
  189. if ([self.metadataDetail.typeFile isEqualToString: k_metadataTypeFile_document]) {
  190. _fileNameExtension = [[self.metadataDetail.fileNamePrint pathExtension] uppercaseString];
  191. if ([_fileNameExtension isEqualToString:@"PDF"]) {
  192. self.edgesForExtendedLayout = UIRectEdgeBottom;
  193. [self viewPDF:@""];
  194. [self createToolbar];
  195. [app aspectNavigationControllerBar:self.navigationController.navigationBar encrypted:NO online:[app.reachability isReachable] hidden:NO];
  196. } else {
  197. self.edgesForExtendedLayout = UIRectEdgeBottom;
  198. [self viewDocument];
  199. [self createToolbar];
  200. [app aspectNavigationControllerBar:self.navigationController.navigationBar encrypted:NO online:[app.reachability isReachable] hidden:NO];
  201. }
  202. }
  203. }
  204. #pragma --------------------------------------------------------------------------------------------
  205. #pragma mark ===== View Audio =====
  206. #pragma --------------------------------------------------------------------------------------------
  207. - (void)viewAudio
  208. {
  209. NSString *fileName;
  210. if (_sourceDirectoryLocal) {
  211. fileName = [NSString stringWithFormat:@"%@/%@", self.metadataDetail.directoryID, self.metadataDetail.fileNamePrint];
  212. } else {
  213. fileName = [NSString stringWithFormat:@"%@/%@", [CCUtility getDirectoryAudio], self.metadataDetail.fileNamePrint];
  214. [[NSFileManager defaultManager] removeItemAtPath:fileName error:nil];
  215. [[NSFileManager defaultManager] linkItemAtPath:[NSString stringWithFormat:@"%@/%@", app.directoryUser, self.metadataDetail.fileID] toPath:fileName error:nil];
  216. }
  217. appDelegate.player.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height - TOOLBAR_HEIGHT);
  218. appDelegate.player.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  219. appDelegate.player.nextButton.hidden = true;
  220. appDelegate.player.previousButton.hidden = true;
  221. appDelegate.player.fullscreenButton_.hidden = true;
  222. LMMediaItem *item = [[LMMediaItem alloc] initWithInfo:@{LMMediaItemInfoURLKey:[NSURL fileURLWithPath:fileName], LMMediaItemInfoContentTypeKey:@(LMMediaItemContentTypeVideo)}];
  223. item.title = self.metadataDetail.fileNamePrint;
  224. item.artist = [NCBrandOptions sharedInstance].brand;
  225. [appDelegate.player.mediaPlayer removeAllMediaInQueue];
  226. [appDelegate.player.mediaPlayer addMedia:item];
  227. [appDelegate.player.mediaPlayer play];
  228. // Info
  229. NSMutableDictionary *songInfo = [[NSMutableDictionary alloc] init];
  230. [songInfo setObject:app.player.mediaPlayer.nowPlayingItem.title forKey:MPMediaItemPropertyTitle];
  231. [songInfo setObject:app.player.mediaPlayer.nowPlayingItem.artist forKey:MPMediaItemPropertyArtist];
  232. [[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:songInfo];
  233. [self.view addSubview:appDelegate.player];
  234. }
  235. #pragma --------------------------------------------------------------------------------------------
  236. #pragma mark ===== View Document =====
  237. #pragma --------------------------------------------------------------------------------------------
  238. - (void)viewDocument
  239. {
  240. NSString *fileName;
  241. if (_sourceDirectoryLocal) {
  242. fileName = [NSString stringWithFormat:@"%@/%@", self.metadataDetail.directoryID, self.metadataDetail.fileNamePrint];
  243. } else {
  244. fileName = [NSTemporaryDirectory() stringByAppendingString:self.metadataDetail.fileNamePrint];
  245. [[NSFileManager defaultManager] removeItemAtPath:fileName error:nil];
  246. [[NSFileManager defaultManager] linkItemAtPath:[NSString stringWithFormat:@"%@/%@", app.directoryUser, self.metadataDetail.fileID] toPath:fileName error:nil];
  247. }
  248. NSURL *url = [NSURL fileURLWithPath:fileName];
  249. WKPreferences *wkPreferences = [[WKPreferences alloc] init];
  250. wkPreferences.javaScriptEnabled = false;
  251. WKWebViewConfiguration *wkConfig = [[WKWebViewConfiguration alloc] init];
  252. wkConfig.preferences = wkPreferences;
  253. self.webView = [[WKWebView alloc] initWithFrame:(CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height - TOOLBAR_HEIGHT)) configuration:wkConfig];
  254. self.webView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  255. [self.webView setBackgroundColor:[UIColor whiteColor]];
  256. [self.webView setOpaque:NO];
  257. if ( [_fileNameExtension isEqualToString:@"CSS"] || [_fileNameExtension isEqualToString:@"PY"] || [_fileNameExtension isEqualToString:@"XML"] || [_fileNameExtension isEqualToString:@"JS"] ) {
  258. NSMutableURLRequest *headRequest = [NSMutableURLRequest requestWithURL:[NSURL fileURLWithPath:fileName]];
  259. [headRequest setHTTPMethod:@"HEAD"];
  260. NSHTTPURLResponse *headResponse;
  261. NSError *error = nil;
  262. [NSURLConnection sendSynchronousRequest:headRequest returningResponse:&headResponse error:&error];
  263. if (error != nil) {
  264. NSLog(@"[LOG] loadURLWithString %@",[error localizedDescription]);
  265. }
  266. NSString *dataFile = [[NSString alloc] initWithData:[NSData dataWithContentsOfURL:url] encoding:NSASCIIStringEncoding];
  267. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
  268. [self.webView loadHTMLString:[NSString stringWithFormat:@"<div style='font-size:%@;font-family:%@;'><pre>%@",@"40",@"Sans-Serif",dataFile] baseURL:nil];
  269. }else{
  270. [self.webView loadHTMLString:[NSString stringWithFormat:@"<div style='font-size:%@;font-family:%@;'><pre>%@",@"20",@"Sans-Serif",dataFile] baseURL:nil];
  271. }
  272. } else if ([_fileNameExtension isEqualToString:@"TXT"]) {
  273. NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
  274. NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:nil delegateQueue:nil];
  275. NSMutableURLRequest *headRequest = [NSMutableURLRequest requestWithURL:url];
  276. [headRequest setHTTPMethod:@"HEAD"];
  277. NSURLSessionDataTask *task = [session dataTaskWithRequest:headRequest completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
  278. [self.webView loadData:[NSData dataWithContentsOfURL: url] MIMEType:response.MIMEType characterEncodingName:@"utf-8" baseURL:url];
  279. }];
  280. [task resume];
  281. } else {
  282. [self.webView loadRequest:[NSMutableURLRequest requestWithURL:url]];
  283. }
  284. [self.view addSubview:self.webView];
  285. }
  286. #pragma --------------------------------------------------------------------------------------------
  287. #pragma mark ===== View Image =====
  288. #pragma --------------------------------------------------------------------------------------------
  289. - (void)viewImage
  290. {
  291. self.photoBrowser = [[MWPhotoBrowser alloc] initWithDelegate:self];
  292. _reload = NO;
  293. _indexNowVisible = -1;
  294. _fileIDNowVisible = nil;
  295. [self.photos removeAllObjects];
  296. [self.thumbs removeAllObjects];
  297. [_dataSourceDirectoryID removeAllObjects];
  298. // if not images, exit
  299. if ([self.dataSourceImagesVideos count] == 0) return;
  300. NSUInteger index = 0;
  301. for (tableMetadata *metadata in self.dataSourceImagesVideos) {
  302. // start from here ?
  303. if (self.metadataDetail.fileID && [metadata.fileID isEqualToString:self.metadataDetail.fileID])
  304. [self.photoBrowser setCurrentPhotoIndex:index];
  305. if (metadata.cryptated) {
  306. [self.photos addObject:[MWPhoto photoWithImage:[UIImage imageNamed:@"filePreviewDownloadCrypto"]]];
  307. [self.thumbs addObject:[MWPhoto photoWithImage:[UIImage imageNamed:@"filePreviewDownloadCrypto"]]];
  308. } else {
  309. [self.photos addObject:[MWPhoto photoWithImage:[UIImage imageNamed:@"filePreviewDownload"]]];
  310. MWPhoto *thumb = [MWPhoto photoWithImage:[UIImage imageNamed:@"filePreviewDownload"]];
  311. if ([metadata.typeFile isEqualToString: k_metadataTypeFile_video]) thumb.isVideo = YES;
  312. [self.thumbs addObject:thumb];
  313. }
  314. // add directory
  315. [_dataSourceDirectoryID addObject:metadata.directoryID];
  316. index++;
  317. }
  318. // PhotoBrowser
  319. self.photoBrowser.displayActionButton = YES;
  320. self.photoBrowser.displayDeleteButton = YES;
  321. self.photoBrowser.displayNavArrows = YES;
  322. self.photoBrowser.displaySelectionButtons = NO;
  323. self.photoBrowser.alwaysShowControls = NO;
  324. self.photoBrowser.zoomPhotosToFill = NO;
  325. self.photoBrowser.startOnGrid = NO;
  326. self.photoBrowser.autoPlayOnAppear = NO;
  327. self.photoBrowser.delayToHideElements = 15;
  328. self.photoBrowser.enableGrid = YES;
  329. if (self.traitCollection.horizontalSizeClass == UIUserInterfaceSizeClassCompact) {
  330. [self addChildViewController:self.photoBrowser];
  331. [self.view addSubview:self.photoBrowser.view];
  332. [self.photoBrowser didMoveToParentViewController:self];
  333. } else {
  334. [self.navigationController pushViewController:self.photoBrowser animated:NO];
  335. }
  336. }
  337. - (NSUInteger)numberOfPhotosInPhotoBrowser:(MWPhotoBrowser *)photoBrowser
  338. {
  339. return [self.dataSourceImagesVideos count];
  340. }
  341. - (NSString *)photoBrowser:(MWPhotoBrowser *)photoBrowser titleForPhotoAtIndex:(NSUInteger)index
  342. {
  343. tableMetadata *metadata = [self.dataSourceImagesVideos objectAtIndex:index];
  344. NSString *titleDir = metadata.fileNamePrint;
  345. self.title = titleDir;
  346. return titleDir;
  347. }
  348. - (void)photoBrowser:(MWPhotoBrowser *)photoBrowser didDisplayPhotoAtIndex:(NSUInteger)index
  349. {
  350. tableMetadata *metadata = [self.dataSourceImagesVideos objectAtIndex:index];
  351. NSString *directory;
  352. _indexNowVisible = index;
  353. _fileIDNowVisible = metadata.fileID;
  354. photoBrowser.toolbar.hidden = NO;
  355. if (_sourceDirectoryLocal)
  356. directory = self.metadataDetail.directoryID;
  357. else
  358. directory = app.directoryUser;
  359. // Download image ?
  360. if (metadata) {
  361. tableMetadata *metadataDB = [[NCManageDatabase sharedInstance] getMetadataWithPredicate:[NSPredicate predicateWithFormat:@"fileID = %@", metadata.fileID]];
  362. if ([[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"%@/%@", directory, metadata.fileID]] == NO && [metadataDB.session length] == 0)
  363. [self downloadPhotoBrowser:metadata];
  364. }
  365. // Title
  366. if (metadata && !photoBrowser.isGridController)
  367. self.title = metadata.fileNamePrint;
  368. if (_reload) {
  369. [self.photoBrowser performSelector:@selector(reloadData) withObject:nil];
  370. _reload = NO;
  371. }
  372. }
  373. - (id <MWPhoto>)photoBrowser:(MWPhotoBrowser *)photoBrowser photoAtIndex:(NSUInteger)index
  374. {
  375. NSString *directory;
  376. UIImage *image;
  377. if (_sourceDirectoryLocal)
  378. directory = self.metadataDetail.directoryID;
  379. else
  380. directory = app.directoryUser;
  381. tableMetadata *metadata = [self.dataSourceImagesVideos objectAtIndex:index];
  382. if (index < self.photos.count) {
  383. if (metadata.fileID) {
  384. if ([metadata.typeFile isEqualToString: k_metadataTypeFile_image]) {
  385. NSString *fileImage = [NSString stringWithFormat:@"%@/%@", directory, metadata.fileID];
  386. NSString *ext = [CCUtility getExtension:metadata.fileNamePrint];
  387. if ([ext isEqualToString:@"GIF"]) image = [UIImage animatedImageWithAnimatedGIFURL:[NSURL fileURLWithPath:fileImage]];
  388. else image = [UIImage imageWithContentsOfFile:fileImage];
  389. if (image) {
  390. MWPhoto *photo = [MWPhoto photoWithImage:image];
  391. // Location ??
  392. [self setLocationCaptionPhoto:photo fileID:metadata.fileID];
  393. [self.photos replaceObjectAtIndex:index withObject:photo];
  394. } else {
  395. if ([metadata.sessionError length] > 0 ) {
  396. [self.photos replaceObjectAtIndex:index withObject:[MWPhoto photoWithImage:[UIImage imageNamed:@"filePreviewError"]]];
  397. } else {
  398. image = [CCGraphics drawText:[NSLocalizedString(@"_loading_", nil) stringByAppendingString:@"..."] inImage:[UIImage imageNamed:@"button1000x200"] colorText:[UIColor darkGrayColor] sizeOfFont:50];
  399. [self.photos replaceObjectAtIndex:index withObject:[MWPhoto photoWithImage:image]];
  400. }
  401. }
  402. }
  403. if ([metadata.typeFile isEqualToString: k_metadataTypeFile_video]) {
  404. if ([[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"%@/%@", directory, metadata.fileID]]) {
  405. // remove and make the simbolic link in temp
  406. NSString *toPath = [NSTemporaryDirectory() stringByAppendingString:metadata.fileNamePrint];
  407. [[NSFileManager defaultManager] removeItemAtPath:toPath error:nil];
  408. [[NSFileManager defaultManager] linkItemAtPath:[NSString stringWithFormat:@"%@/%@", directory, metadata.fileID] toPath:toPath error:nil];
  409. NSURL *url = [NSURL fileURLWithPath:toPath];
  410. MWPhoto *video = [MWPhoto photoWithImage:[CCGraphics thumbnailImageForVideo:url atTime:1.0]];
  411. video.videoURL = url;
  412. [self.photos replaceObjectAtIndex:index withObject:video];
  413. } else {
  414. if ([metadata.sessionError length] > 0 ) {
  415. [self.photos replaceObjectAtIndex:index withObject:[MWPhoto photoWithImage:[UIImage imageNamed:@"filePreviewError"]]];
  416. } else {
  417. [self.photos replaceObjectAtIndex:index withObject:[MWPhoto photoWithImage:[CCGraphics drawText:[NSLocalizedString(@"_loading_", nil) stringByAppendingString:@"..."] inImage:[UIImage imageNamed:@"button1000x200"] colorText:[UIColor darkGrayColor] sizeOfFont:50]]];
  418. }
  419. }
  420. }
  421. }
  422. // energy saving memory
  423. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  424. int iPrev = (int)index - 2;
  425. if (iPrev >= 0) {
  426. if ([self.photos objectAtIndex:iPrev] != nil)
  427. [self.photos replaceObjectAtIndex:iPrev withObject:[MWPhoto photoWithImage:nil]];
  428. }
  429. int iNext = (int)index + 2;
  430. if (iNext < _photos.count) {
  431. if ([self.photos objectAtIndex:iNext] != nil)
  432. [self.photos replaceObjectAtIndex:iNext withObject:[MWPhoto photoWithImage:nil]];
  433. }
  434. });
  435. return [self.photos objectAtIndex:index];
  436. }
  437. return nil;
  438. }
  439. - (void)gridWillAppear:(MWPhotoBrowser *)photoBrowser
  440. {
  441. // reload [BUG]
  442. [self.photoBrowser reloadData];
  443. // in grid clear all MWPhoto : energy saving memory
  444. if (self.photos.count > 1 && [self.dataSourceImagesVideos count] > 0) {
  445. for(int index=0; index < self.photos.count; index++)
  446. [_photos replaceObjectAtIndex:index withObject:[MWPhoto photoWithImage:nil]];
  447. }
  448. self.title = [NSString stringWithFormat:@"Album %lu %@", (unsigned long)_photos.count, NSLocalizedString(@"photos", nil)];
  449. }
  450. - (void)gridWillDisappear:(MWPhotoBrowser *)photoBrowser
  451. {
  452. //
  453. photoBrowser.toolbar.hidden = NO;
  454. }
  455. - (id <MWPhoto>)photoBrowser:(MWPhotoBrowser *)photoBrowser thumbPhotoAtIndex:(NSUInteger)index
  456. {
  457. NSString *directory;
  458. tableMetadata *metadata = [self.dataSourceImagesVideos objectAtIndex:index];
  459. if (_sourceDirectoryLocal)
  460. directory = self.metadataDetail.directoryID;
  461. else
  462. directory = app.directoryUser;
  463. if (index < self.thumbs.count) {
  464. if (metadata.fileID) {
  465. UIImage *image;
  466. if (_sourceDirectoryLocal) {
  467. image = [CCGraphics createNewImageFrom:metadata.fileID directoryUser:directory fileNameTo:metadata.fileID fileNamePrint:metadata.fileNamePrint size:@"m" imageForUpload:NO typeFile:metadata.typeFile writePreview:NO optimizedFileName:[CCUtility getOptimizedPhoto]];
  468. } else {
  469. NSString *fileImage = [NSString stringWithFormat:@"%@/%@.ico", directory, metadata.fileID];
  470. image = [UIImage animatedImageWithAnimatedGIFURL:[NSURL fileURLWithPath:fileImage]];
  471. }
  472. if (image) {
  473. MWPhoto *thumb = [MWPhoto photoWithImage:image];
  474. if ([metadata.typeFile isEqualToString: k_metadataTypeFile_video]) thumb.isVideo = YES;
  475. [self.thumbs replaceObjectAtIndex:index withObject:thumb];
  476. }
  477. }
  478. return [self.thumbs objectAtIndex:index];
  479. }
  480. return nil;
  481. }
  482. - (void)photoBrowser:(MWPhotoBrowser *)photoBrowser actionButtonPressedForPhotoAtIndex:(NSUInteger)index
  483. {
  484. NSString *filePath;
  485. tableMetadata *metadata = [self.dataSourceImagesVideos objectAtIndex:index];
  486. if (metadata == nil) return;
  487. if (_sourceDirectoryLocal) {
  488. filePath = [NSString stringWithFormat:@"%@/%@", self.metadataDetail.directoryID, self.metadataDetail.fileNamePrint];
  489. } else {
  490. filePath = [NSString stringWithFormat:@"%@/%@", app.directoryUser, metadata.fileNamePrint];
  491. [[NSFileManager defaultManager] removeItemAtPath:filePath error:nil];
  492. [[NSFileManager defaultManager] linkItemAtPath:[NSString stringWithFormat:@"%@/%@", app.directoryUser, metadata.fileID] toPath:filePath error:nil];
  493. }
  494. self.docController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:filePath]];
  495. self.docController.delegate = self;
  496. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
  497. [self.docController presentOptionsMenuFromRect:photoBrowser.view.frame inView:photoBrowser.view animated:YES];
  498. [self.docController presentOptionsMenuFromBarButtonItem:photoBrowser.actionButton animated:YES];
  499. }
  500. - (void)photoBrowser:(MWPhotoBrowser *)photoBrowser shareButtonPressedForPhotoAtIndex:(NSUInteger)index
  501. {
  502. tableMetadata *metadata = [self.dataSourceImagesVideos objectAtIndex:index];
  503. [app.activeMain openWindowShare:metadata];
  504. }
  505. - (void)photoBrowser:(MWPhotoBrowser *)photoBrowser deleteButtonPressedForPhotoAtIndex:(NSUInteger)index deleteButton:(UIBarButtonItem *)deleteButton
  506. {
  507. tableMetadata *metadata = [self.dataSourceImagesVideos objectAtIndex:index];
  508. if (metadata == nil || [[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"%@/%@", app.directoryUser, metadata.fileID]] == NO) {
  509. [app messageNotification:@"_info_" description:@"_file_not_found_" visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeInfo errorCode:0];
  510. return;
  511. }
  512. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  513. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_delete_", nil)
  514. style:UIAlertActionStyleDestructive
  515. handler:^(UIAlertAction *action) {
  516. [[CCActions sharedInstance] deleteFileOrFolder:metadata delegate:self];
  517. }]];
  518. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_cancel_", nil)
  519. style:UIAlertActionStyleCancel
  520. handler:^(UIAlertAction *action) {
  521. [alertController dismissViewControllerAnimated:YES completion:nil];
  522. }]];
  523. alertController.popoverPresentationController.barButtonItem = deleteButton;
  524. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  525. [alertController.view layoutIfNeeded];
  526. [self.parentViewController presentViewController:alertController animated:YES completion:NULL];
  527. }
  528. - (void)triggerProgressTask:(NSNotification *)notification
  529. {
  530. NSDictionary *dict = notification.userInfo;
  531. float progress = [[dict valueForKey:@"progress"] floatValue];
  532. if (progress == 0)
  533. [self.navigationController cancelCCProgress];
  534. else
  535. [self.navigationController setCCProgressPercentage:progress*100 andTintColor:[NCBrandColor sharedInstance].navigationBarProgress];
  536. }
  537. - (void)downloadPhotoBrowserFailure:(NSInteger)errorCode
  538. {
  539. [app messageNotification:@"_download_selected_files_" description:@"_error_download_photobrowser_" visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError errorCode:errorCode];
  540. [self.photoBrowser reloadData];
  541. }
  542. - (void)downloadPhotoBrowserSuccess:(tableMetadata *)metadataVar selector:(NSString *)selector
  543. {
  544. NSUInteger index = 0;
  545. // if a message for a directory of these
  546. if (![_dataSourceDirectoryID containsObject:metadataVar.directoryID])
  547. return;
  548. for (NSUInteger i=0; i < [self.dataSourceImagesVideos count]; i++ ) {
  549. tableMetadata *metadata = [self.dataSourceImagesVideos objectAtIndex:i];
  550. // search index
  551. if ([metadataVar.fileID isEqualToString:metadata.fileID]) {
  552. index = i;
  553. break;
  554. }
  555. }
  556. // do not reload is Video on air
  557. if (_photoBrowser.currentVideoPlayerViewController.isViewLoaded && _photoBrowser.currentVideoPlayerViewController.view.window) return;
  558. if ([metadataVar.fileID isEqualToString:_fileIDNowVisible] || [self.photoBrowser isGridReload:index]) {
  559. [self.photoBrowser reloadData];
  560. _reload = NO;
  561. } else {
  562. _reload = YES;
  563. }
  564. }
  565. - (void)downloadPhotoBrowser:(tableMetadata *)metadata
  566. {
  567. NSString *serverUrl = [[NCManageDatabase sharedInstance] getServerUrl:metadata.directoryID];
  568. if (serverUrl)
  569. [[CCNetworking sharedNetworking] downloadFile:metadata.fileID serverUrl:serverUrl downloadData:YES downloadPlist:NO selector:selectorLoadViewImage selectorPost:nil session:k_download_session taskStatus:k_taskStatusResume delegate:nil];
  570. }
  571. - (void)insertGeocoderLocation:(NSNotification *)notification
  572. {
  573. if (notification.userInfo.count == 0)
  574. return;
  575. NSString *fileID = [[notification.userInfo allKeys] objectAtIndex:0];
  576. //NSDate *date = [[notification.userInfo allValues] objectAtIndex:0];
  577. // test [Chrash V 1.14,15]
  578. if (_indexNowVisible >= [self.photos count])
  579. return;
  580. if ([fileID isEqualToString:_fileIDNowVisible]) {
  581. MWPhoto *photo = [self.photos objectAtIndex:_indexNowVisible];
  582. [self setLocationCaptionPhoto:photo fileID:fileID];
  583. if (![self.photoBrowser isGridController]) {
  584. //[self.photoBrowser hideControls];
  585. //[self.photoBrowser showControls];
  586. }
  587. }
  588. }
  589. - (void)setLocationCaptionPhoto:(MWPhoto *)photo fileID:(NSString *)fileID
  590. {
  591. tableLocalFile *localFile;
  592. // read Geocoder
  593. localFile = [[NCManageDatabase sharedInstance] getTableLocalFileWithPredicate:[NSPredicate predicateWithFormat:@"fileID = %@", fileID]];
  594. if ([localFile.exifLatitude doubleValue] != 0 || [localFile.exifLongitude doubleValue] != 0) {
  595. // Fix BUG Geo latitude & longitude
  596. if ([localFile.exifLatitude doubleValue] == 9999 || [localFile.exifLongitude doubleValue] == 9999) {
  597. tableMetadata *metadata = [[NCManageDatabase sharedInstance] getMetadataWithPredicate:[NSPredicate predicateWithFormat:@"fileID = %@", fileID]];
  598. if (metadata) {
  599. [[CCExifGeo sharedInstance] setExifLocalTableEtag:metadata directoryUser:app.directoryUser activeAccount:app.activeAccount];
  600. }
  601. }
  602. [[CCExifGeo sharedInstance] setGeocoderEtag:fileID exifDate:localFile.exifDate latitude:localFile.exifLatitude longitude:localFile.exifLongitude];
  603. localFile = [[NCManageDatabase sharedInstance] getTableLocalFileWithPredicate:[NSPredicate predicateWithFormat:@"fileID = %@", fileID]];
  604. if ([localFile.exifLatitude floatValue] != 0 || [localFile.exifLongitude floatValue] != 0) {
  605. NSString *location = [[NCManageDatabase sharedInstance] getLocationFromGeoLatitude:localFile.exifLatitude longitude:localFile.exifLongitude];
  606. if ([localFile.exifDate isEqualToDate:[NSDate distantPast]] == NO && location) {
  607. NSString *localizedDateTime = [NSDateFormatter localizedStringFromDate:localFile.exifDate dateStyle:NSDateFormatterFullStyle timeStyle:NSDateFormatterMediumStyle];
  608. photo.caption = [NSString stringWithFormat:NSLocalizedString(@"%@\n%@", nil), localizedDateTime, location];
  609. }
  610. }
  611. }
  612. }
  613. #pragma --------------------------------------------------------------------------------------------
  614. #pragma mark ===== View PDF =====
  615. #pragma --------------------------------------------------------------------------------------------
  616. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  617. {
  618. [alertView dismissWithClickedButtonIndex:buttonIndex animated:YES];
  619. [[alertView textFieldAtIndex:0] resignFirstResponder];
  620. if (alertView.tag == alertRequestPasswordPDF) [self performSelector:@selector(viewPDF:) withObject:[alertView textFieldAtIndex:0].text afterDelay:0.3];
  621. }
  622. - (void)viewPDF:(NSString *)password
  623. {
  624. NSString *fileName;
  625. if (_sourceDirectoryLocal) {
  626. fileName = [NSString stringWithFormat:@"%@/%@", self.metadataDetail.directoryID, self.metadataDetail.fileNamePrint];
  627. } else {
  628. fileName = [NSTemporaryDirectory() stringByAppendingString:self.metadataDetail.fileNamePrint];
  629. [[NSFileManager defaultManager] removeItemAtPath:fileName error:nil];
  630. [[NSFileManager defaultManager] linkItemAtPath:[NSString stringWithFormat:@"%@/%@", app.directoryUser, self.metadataDetail.fileID] toPath:fileName error:nil];
  631. }
  632. if ([[NSFileManager defaultManager] fileExistsAtPath:fileName isDirectory:nil] == NO) {
  633. // read file error
  634. UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"_error_", nil) message:NSLocalizedString(@"_read_file_error_", nil) delegate:nil cancelButtonTitle:nil otherButtonTitles:NSLocalizedString(@"_ok_", nil), nil];
  635. [alertView show];
  636. }
  637. CGPDFDocumentRef pdf = CGPDFDocumentCreateWithURL((CFURLRef)[NSURL fileURLWithPath:fileName]);
  638. if (pdf) {
  639. // Encrypted
  640. if (CGPDFDocumentIsEncrypted(pdf) == YES) {
  641. // Try a blank password first, per Apple's Quartz PDF example
  642. if (CGPDFDocumentUnlockWithPassword(pdf, "") == YES) {
  643. // blank password
  644. [self readerPDF:fileName password:@""];
  645. } else {
  646. if ([password length] == 0) {
  647. // password request
  648. UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"_insert_password_pfd_",nil) message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:NSLocalizedString(@"_ok_", nil), nil];
  649. [alertView setAlertViewStyle:UIAlertViewStylePlainTextInput];
  650. alertView.tag = alertRequestPasswordPDF;
  651. [alertView show];
  652. } else {
  653. const char *key = [password UTF8String];
  654. // failure
  655. if (CGPDFDocumentUnlockWithPassword(pdf, key) == NO) {
  656. UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"_error_", nil) message:NSLocalizedString(@"_password_pdf_error_", nil) delegate:nil cancelButtonTitle:nil otherButtonTitles:NSLocalizedString(@"_ok_", nil), nil];
  657. [alertView show];
  658. } else {
  659. // pdf with password
  660. [self readerPDF:fileName password:password];
  661. }
  662. }
  663. }
  664. } else{
  665. // No password
  666. [self readerPDF:fileName password:@""];
  667. }
  668. } else {
  669. // read file error
  670. UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"_error_", nil) message:NSLocalizedString(@"_read_file_error_", nil) delegate:nil cancelButtonTitle:nil otherButtonTitles:NSLocalizedString(@"_ok_", nil), nil];
  671. [alertView show];
  672. }
  673. }
  674. - (void)readerPDF:(NSString *)fileName password:(NSString *)password
  675. {
  676. ReaderDocument *documentPDF = [ReaderDocument withDocumentFilePath:fileName password:password];
  677. if (documentPDF != nil) {
  678. self.readerPDFViewController = [[ReaderViewController alloc] initWithReaderDocument:documentPDF];
  679. self.readerPDFViewController.delegate = self;
  680. self.readerPDFViewController.view.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height - TOOLBAR_HEIGHT);
  681. [self.readerPDFViewController updateContentViews];
  682. [self addChildViewController:self.readerPDFViewController];
  683. [self.view addSubview:self.readerPDFViewController.view];
  684. [self.readerPDFViewController didMoveToParentViewController:self];
  685. } else {
  686. UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"_error_", nil) message:NSLocalizedString(@"_read_file_error_", nil) delegate:nil cancelButtonTitle:nil otherButtonTitles:NSLocalizedString(@"_ok_", nil), nil];
  687. [alertView show];
  688. }
  689. }
  690. - (void)handleSingleTapReader
  691. {
  692. self.navigationController.navigationBarHidden = !self.navigationController.navigationBarHidden;
  693. _toolbar.hidden = !_toolbar.isHidden;
  694. if (_toolbar.isHidden) {
  695. self.readerPDFViewController.view.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height);
  696. } else {
  697. self.readerPDFViewController.view.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height - TOOLBAR_HEIGHT);
  698. }
  699. [self.readerPDFViewController updateContentViews];
  700. }
  701. #pragma --------------------------------------------------------------------------------------------
  702. #pragma mark ===== Delete =====
  703. #pragma --------------------------------------------------------------------------------------------
  704. - (void)deleteFileOrFolderFailure:(CCMetadataNet *)metadataNet message:(NSString *)message errorCode:(NSInteger)errorCode
  705. {
  706. NSLog(@"[LOG] delete failure");
  707. }
  708. - (void)deleteFileOrFolderSuccess:(CCMetadataNet *)metadataNet
  709. {
  710. // reload Main
  711. [app.activeMain reloadDatasource];
  712. // If removed document (web) or PDF close
  713. if (_webView || _readerPDFViewController)
  714. [self removeAllView];
  715. // if a message for a directory of these
  716. if (![_dataSourceDirectoryID containsObject:metadataNet.directoryID])
  717. return;
  718. // if we are not in browserPhoto and it's removed photo/video in preview then "< Back"
  719. if (!self.photoBrowser && [self.metadataDetail.fileID isEqualToString:metadataNet.fileID]) {
  720. [app.player.mediaPlayer stop];
  721. NSArray *viewsToRemove = [self.view subviews];
  722. for (id element in viewsToRemove) {
  723. if ([element isMemberOfClass:[UIView class]] || [element isMemberOfClass:[UIToolbar class]] || [element isMemberOfClass:[LMMediaPlayerView class]])
  724. [element removeFromSuperview];
  725. }
  726. self.title = @"";
  727. [self.navigationController popViewControllerAnimated:YES];
  728. } else {
  729. // only photoBrowser if exists
  730. for (NSUInteger index=0; index < [self.dataSourceImagesVideos count] && _photoBrowser; index++ ) {
  731. tableMetadata *metadata = [self.dataSourceImagesVideos objectAtIndex:index];
  732. // ricerca index
  733. if ([metadata.fileID isEqualToString:metadataNet.fileID]) {
  734. [self.dataSourceImagesVideos removeObjectAtIndex:index];
  735. [self.photos removeObjectAtIndex:index];
  736. [self.thumbs removeObjectAtIndex:index];
  737. [self.photoBrowser reloadData];
  738. // Title
  739. if ([self.dataSourceImagesVideos count] == 0) {
  740. self.title = @"";
  741. [self.navigationController popViewControllerAnimated:YES];
  742. }
  743. break;
  744. }
  745. }
  746. }
  747. }
  748. #pragma --------------------------------------------------------------------------------------------
  749. #pragma mark ===== ButtonPressed =====
  750. #pragma --------------------------------------------------------------------------------------------
  751. - (void)modifyTxtButtonPressed:(UIBarButtonItem *)sender
  752. {
  753. UINavigationController* navigationController = [[UIStoryboard storyboardWithName:@"NCText" bundle:nil] instantiateViewControllerWithIdentifier:@"NCText"];
  754. NCText *viewController = (NCText *)navigationController.topViewController;
  755. viewController.metadata = self.metadataDetail;
  756. [navigationController setModalPresentationStyle:UIModalPresentationFormSheet];
  757. [self presentViewController:navigationController animated:YES completion:nil];
  758. }
  759. - (void)actionButtonPressed:(UIBarButtonItem *)sender
  760. {
  761. NSString *filePath;
  762. if ([self.metadataDetail.fileNamePrint length] == 0) return;
  763. if (_sourceDirectoryLocal) {
  764. filePath = [NSString stringWithFormat:@"%@/%@", self.metadataDetail.directoryID, self.metadataDetail.fileNamePrint];
  765. } else {
  766. filePath = [NSString stringWithFormat:@"%@%@", NSTemporaryDirectory(), self.metadataDetail.fileNamePrint];
  767. }
  768. self.docController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:filePath]];
  769. self.docController.delegate = self;
  770. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
  771. [self.docController presentOptionsMenuFromRect:self.view.frame inView:self.view animated:YES];
  772. [self.docController presentOptionsMenuFromBarButtonItem:sender animated:YES];
  773. }
  774. - (void)shareButtonPressed:(UIBarButtonItem *)sender
  775. {
  776. [app.activeMain openWindowShare:self.metadataDetail];
  777. }
  778. - (void)deleteButtonPressed:(UIBarButtonItem *)sender
  779. {
  780. if ([self.metadataDetail.fileNamePrint length] == 0) return;
  781. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  782. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_delete_", nil)
  783. style:UIAlertActionStyleDestructive
  784. handler:^(UIAlertAction *action) {
  785. [[CCActions sharedInstance] deleteFileOrFolder:self.metadataDetail delegate:self];
  786. }]];
  787. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_cancel_", nil)
  788. style:UIAlertActionStyleCancel
  789. handler:^(UIAlertAction *action) {
  790. [alertController dismissViewControllerAnimated:YES completion:nil];
  791. }]];
  792. alertController.popoverPresentationController.barButtonItem = _buttonDelete;
  793. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  794. [alertController.view layoutIfNeeded];
  795. [self presentViewController:alertController animated:YES completion:NULL];
  796. }
  797. @end