CCDetail.m 46 KB

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