CCOffline.m 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789
  1. //
  2. // CCOffline.m
  3. // Crypto Cloud Technology Nextcloud
  4. //
  5. // Created by Marino Faggiana on 16/01/17.
  6. // Copyright (c) 2014 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 "CCOffline.h"
  24. #import "AppDelegate.h"
  25. #import "CCOfflineFileFolder.h"
  26. #pragma GCC diagnostic ignored "-Wundeclared-selector"
  27. @interface CCOffline ()
  28. {
  29. NSMutableArray *dataSource;
  30. }
  31. @end
  32. @implementation CCOffline
  33. #pragma --------------------------------------------------------------------------------------------
  34. #pragma mark ===== Init =====
  35. #pragma --------------------------------------------------------------------------------------------
  36. - (id)initWithCoder:(NSCoder *)aDecoder
  37. {
  38. if (self = [super initWithCoder:aDecoder]) {
  39. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadTable) name:@"reloadTableCCOffline" object:nil];
  40. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(readFileOffline) name:@"readFileOffline" object:nil];
  41. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(initToHome) name:@"initToHomeOffline" object:nil];
  42. app.activeOffline = self;
  43. }
  44. return self;
  45. }
  46. - (id)initWithStyle:(UITableViewStyle)style
  47. {
  48. self = [super initWithStyle:style];
  49. if (self) {
  50. // Custom initialization
  51. }
  52. return self;
  53. }
  54. #pragma --------------------------------------------------------------------------------------------
  55. #pragma mark ===== View =====
  56. #pragma --------------------------------------------------------------------------------------------
  57. - (void)viewDidLoad
  58. {
  59. [super viewDidLoad];
  60. // Create data model
  61. _pageTitles = @[@"Offline", @"Local"];
  62. // Create page view controller
  63. self.pageViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"OfflinePageViewController"];
  64. self.pageViewController.dataSource = self;
  65. CCOfflinePageContent *startingViewController = [self viewControllerAtIndex:0];
  66. NSArray *viewControllers = @[startingViewController];
  67. [self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
  68. // Change the size of page view controller
  69. self.pageViewController.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 30);
  70. [self addChildViewController:_pageViewController];
  71. [self.view addSubview:_pageViewController.view];
  72. [self.pageViewController didMoveToParentViewController:self];
  73. /*
  74. // Custom Cell
  75. [self.tableView registerNib:[UINib nibWithNibName:@"CCCellOffline" bundle:nil] forCellReuseIdentifier:@"OfflineCell"];
  76. // Settings initial
  77. if (!_localServerUrl) {
  78. _typeOfController = @"offline";
  79. _localServerUrl = nil;
  80. }
  81. // dataSource
  82. dataSource = [NSMutableArray new];
  83. // Metadata
  84. _metadata = [CCMetadata new];
  85. self.tableView.emptyDataSetDelegate = self;
  86. self.tableView.emptyDataSetSource = self;
  87. self.tableView.tableFooterView = [UIView new];
  88. // button
  89. UIImage *image;
  90. if ([_typeOfController isEqualToString:@"offline"]) {
  91. image = [UIImage imageNamed:image_navBarLocal];
  92. } else image = [UIImage imageNamed:image_navBarOffline];
  93. UIBarButtonItem *_btn=[[UIBarButtonItem alloc]initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(switchOfflineLocal)];
  94. self.navigationItem.rightBarButtonItem=_btn;
  95. */
  96. }
  97. // Apparirà
  98. - (void)viewWillAppear:(BOOL)animated
  99. {
  100. [super viewWillAppear:animated];
  101. // Color
  102. [CCAspect aspectNavigationControllerBar:self.navigationController.navigationBar hidden:NO];
  103. [CCAspect aspectTabBar:self.tabBarController.tabBar hidden:NO];
  104. // Plus Button
  105. [app plusButtonVisibile:true];
  106. [self reloadTable];
  107. }
  108. #pragma --------------------------------------------------------------------------------------------
  109. #pragma mark ===== Page =====
  110. #pragma --------------------------------------------------------------------------------------------
  111. - (CCOfflinePageContent *)viewControllerAtIndex:(NSUInteger)index
  112. {
  113. if (([self.pageTitles count] == 0) || (index >= [self.pageTitles count])) {
  114. return nil;
  115. }
  116. // Create a new view controller and pass suitable data.
  117. CCOfflinePageContent *pageContentViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"OfflinePageContentViewController"];
  118. // pageContentViewController.imageFile = self.pageImages[index];
  119. // pageContentViewController.titleText = self.pageTitles[index];
  120. // pageContentViewController.pageIndex = index;
  121. return pageContentViewController;
  122. }
  123. - (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
  124. {
  125. NSUInteger index = ((CCOfflinePageContent*) viewController).pageIndex;
  126. if ((index == 0) || (index == NSNotFound)) {
  127. return nil;
  128. }
  129. index--;
  130. return [self viewControllerAtIndex:index];
  131. }
  132. - (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
  133. {
  134. NSUInteger index = ((CCOfflinePageContent*) viewController).pageIndex;
  135. if (index == NSNotFound) {
  136. return nil;
  137. }
  138. index++;
  139. if (index == [self.pageTitles count]) {
  140. return nil;
  141. }
  142. return [self viewControllerAtIndex:index];
  143. }
  144. - (NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController
  145. {
  146. return [self.pageTitles count];
  147. }
  148. - (NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController
  149. {
  150. return 0;
  151. }
  152. #pragma --------------------------------------------------------------------------------------------
  153. #pragma mark ===== Effetti Grafici =====
  154. #pragma --------------------------------------------------------------------------------------------
  155. - (void)forcedSwitchOffline
  156. {
  157. if ([_typeOfController isEqualToString:@"offline"] == NO)
  158. [self switchOfflineLocal];
  159. }
  160. - (void)switchOfflineLocal
  161. {
  162. UIImage *imageBarButton;
  163. if ([_typeOfController isEqualToString:@"offline"]) {
  164. imageBarButton = [UIImage imageNamed:image_navBarOffline];
  165. // img Tab Bar
  166. UITabBarItem *item = [self.tabBarController.tabBar.items objectAtIndex:1];
  167. item.selectedImage = [UIImage imageNamed:image_tabBarLocal];
  168. item.image = [UIImage imageNamed:image_tabBarLocal];
  169. _localServerUrl = [CCUtility getDirectoryLocal];
  170. app.isLocalStorage = true;
  171. } else {
  172. imageBarButton = [UIImage imageNamed:image_navBarLocal];
  173. // Image Tab Bar
  174. UITabBarItem *item = [self.tabBarController.tabBar.items objectAtIndex:1];
  175. item.selectedImage = [UIImage imageNamed:image_tabBarOffline];
  176. item.image = [UIImage imageNamed:image_tabBarOffline];
  177. _localServerUrl = @"offline";
  178. app.isLocalStorage = false;
  179. }
  180. UIBarButtonItem *_btn=[[UIBarButtonItem alloc]initWithImage:imageBarButton style:UIBarButtonItemStylePlain target:self action:@selector(switchOfflineLocal)];
  181. self.navigationItem.rightBarButtonItem=_btn;
  182. // init of Navigation Control
  183. [self.navigationController popToRootViewControllerAnimated:NO];
  184. // refresh
  185. if ([_typeOfController isEqualToString:@"offline"])
  186. [[NSNotificationCenter defaultCenter] postNotificationName:@"initToHomeOffline" object:nil];
  187. else
  188. [self reloadTable];
  189. }
  190. #pragma --------------------------------------------------------------------------------------------
  191. #pragma mark ==== DZNEmptyDataSetSource Methods ====
  192. #pragma --------------------------------------------------------------------------------------------
  193. - (BOOL)emptyDataSetShouldDisplay:(UIScrollView *)scrollView
  194. {
  195. if ([_typeOfController isEqualToString:@"Offline"] || [_localServerUrl isEqualToString:[CCUtility getDirectoryLocal]]) return YES;
  196. else return NO;
  197. }
  198. - (CGFloat)spaceHeightForEmptyDataSet:(UIScrollView *)scrollView
  199. {
  200. return 0.0f;
  201. }
  202. - (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView
  203. {
  204. return - self.navigationController.navigationBar.frame.size.height;
  205. }
  206. - (UIColor *)backgroundColorForEmptyDataSet:(UIScrollView *)scrollView
  207. {
  208. return [UIColor whiteColor];
  209. }
  210. - (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView
  211. {
  212. if ([_typeOfController isEqualToString:@"offline"]) return [UIImage imageNamed:image_brandOffline];
  213. else return [UIImage imageNamed:image_brandLocal];
  214. }
  215. - (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView
  216. {
  217. NSString *text;
  218. if ([_typeOfController isEqualToString:@"offline"]) text = NSLocalizedString(@"_no_files_uploaded_", nil);
  219. else text = NSLocalizedString(@"_no_files_uploaded_", nil);
  220. NSDictionary *attributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:20.0f], NSForegroundColorAttributeName:COLOR_BRAND};
  221. return [[NSAttributedString alloc] initWithString:text attributes:attributes];
  222. }
  223. - (NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView
  224. {
  225. NSString *text;
  226. if ([_typeOfController isEqualToString:@"offline"]) text = NSLocalizedString(@"_tutorial_offline_view_", nil);
  227. else text = NSLocalizedString(@"_tutorial_local_view_", nil);
  228. NSMutableParagraphStyle *paragraph = [NSMutableParagraphStyle new];
  229. paragraph.lineBreakMode = NSLineBreakByWordWrapping;
  230. paragraph.alignment = NSTextAlignmentCenter;
  231. NSDictionary *attributes = @{NSFontAttributeName: [UIFont systemFontOfSize:14.0], NSForegroundColorAttributeName: [UIColor lightGrayColor], NSParagraphStyleAttributeName: paragraph};
  232. return [[NSAttributedString alloc] initWithString:text attributes:attributes];
  233. }
  234. #pragma --------------------------------------------------------------------------------------------
  235. #pragma mark ===== UIDocumentInteractionControllerDelegate =====
  236. #pragma --------------------------------------------------------------------------------------------
  237. - (void)documentInteractionControllerDidDismissOptionsMenu:(UIDocumentInteractionController *)controller
  238. {
  239. // evitiamo il rimando della eventuale photo e/o video
  240. if ([CCCoreData getCameraUploadActiveAccount:app.activeAccount]) {
  241. [CCCoreData setCameraUploadDatePhoto:[NSDate date]];
  242. [CCCoreData setCameraUploadDateVideo:[NSDate date]];
  243. }
  244. }
  245. #pragma --------------------------------------------------------------------------------------------
  246. #pragma mark ==== Comandi ====
  247. #pragma --------------------------------------------------------------------------------------------
  248. - (void)openModel:(CCMetadata *)metadata
  249. {
  250. UIViewController *viewController;
  251. if ([metadata.model isEqualToString:@"cartadicredito"])
  252. viewController = [[CCCartaDiCredito alloc] initWithDelegate:self fileName:metadata.fileName uuid:metadata.uuid rev:metadata.rev fileID:metadata.fileID modelReadOnly:true isLocal:app.isLocalStorage];
  253. if ([metadata.model isEqualToString:@"bancomat"])
  254. viewController = [[CCBancomat alloc] initWithDelegate:self fileName:metadata.fileName uuid:metadata.uuid rev:metadata.rev fileID:metadata.fileID modelReadOnly:true isLocal:app.isLocalStorage];
  255. if ([metadata.model isEqualToString:@"contocorrente"])
  256. viewController = [[CCContoCorrente alloc] initWithDelegate:self fileName:metadata.fileName uuid:metadata.uuid rev:metadata.rev fileID:metadata.fileID modelReadOnly:true isLocal:app.isLocalStorage];
  257. if ([metadata.model isEqualToString:@"accountweb"])
  258. viewController = [[CCAccountWeb alloc] initWithDelegate:self fileName:metadata.fileName uuid:metadata.uuid rev:metadata.rev fileID:metadata.fileID modelReadOnly:true isLocal:app.isLocalStorage];
  259. if ([metadata.model isEqualToString:@"patenteguida"])
  260. viewController = [[CCPatenteGuida alloc] initWithDelegate:self fileName:metadata.fileName uuid:metadata.uuid rev:metadata.rev fileID:metadata.fileID modelReadOnly:true isLocal:app.isLocalStorage];
  261. if ([metadata.model isEqualToString:@"cartaidentita"])
  262. viewController = [[CCCartaIdentita alloc] initWithDelegate:self fileName:metadata.fileName uuid:metadata.uuid rev:metadata.rev fileID:metadata.fileID modelReadOnly:true isLocal:app.isLocalStorage];
  263. if ([metadata.model isEqualToString:@"passaporto"])
  264. viewController = [[CCPassaporto alloc] initWithDelegate:self fileName:metadata.fileName uuid:metadata.uuid rev:metadata.rev fileID:metadata.fileID modelReadOnly:true isLocal:app.isLocalStorage];
  265. if ([metadata.model isEqualToString:@"note"]) {
  266. viewController = [[CCNote alloc] initWithDelegate:self fileName:metadata.fileName uuid:metadata.uuid rev:metadata.rev fileID:metadata.fileID modelReadOnly:true isLocal:app.isLocalStorage];
  267. UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
  268. [self presentViewController:navigationController animated:YES completion:nil];
  269. } else {
  270. UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
  271. [navigationController setModalPresentationStyle:UIModalPresentationFormSheet];
  272. [self presentViewController:navigationController animated:YES completion:nil];
  273. }
  274. }
  275. - (void)openWith:(CCMetadata *)metadata
  276. {
  277. NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/%@", _localServerUrl, metadata.fileNamePrint]];
  278. self.docController = [UIDocumentInteractionController interactionControllerWithURL:url];
  279. self.docController.delegate = self;
  280. [self.docController presentOptionsMenuFromRect:self.view.frame inView:self.view animated:YES];
  281. }
  282. #pragma --------------------------------------------------------------------------------------------
  283. #pragma mark ===== read file Offline for download =====
  284. #pragma---------------------------------------------------------------------------------------------
  285. - (void)readFileFailure:(CCMetadataNet *)metadataNet message:(NSString *)message errorCode:(NSInteger)errorCode
  286. {
  287. // error
  288. }
  289. - (void)readFileSuccess:(CCMetadataNet *)metadataNet metadata:(CCMetadata *)metadata
  290. {
  291. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
  292. [[CCOfflineFileFolder sharedOfflineFileFolder] verifyChangeMedatas:[[NSArray alloc] initWithObjects:metadata, nil] serverUrl:metadataNet.serverUrl directoryID:metadataNet.directoryID account:app.activeAccount offline:NO];
  293. });
  294. [self.tableView performSelector:@selector(reloadData) withObject:nil afterDelay:0.1];
  295. }
  296. - (void)readFileOffline
  297. {
  298. if (app.activeAccount == nil || [CCUtility getHomeServerUrlActiveUrl:app.activeUrl typeCloud:app.typeCloud] == nil) return;
  299. NSArray *metadatas = [[NSArray alloc] init];
  300. //metadatas = [CCCoreData getOfflineWithControlZombie:YES activeAccount:app.activeAccount directoryUser:app.directoryUser];
  301. for (CCMetadata *metadata in metadatas) {
  302. NSString *serverUrl = [CCCoreData getServerUrlFromDirectoryID:metadata.directoryID activeAccount:app.activeAccount];
  303. if (serverUrl == nil) continue;
  304. // if this file is on folder offline skip
  305. if ([CCCoreData isOfflineDirectory:serverUrl activeAccount:app.activeAccount])
  306. continue;
  307. CCMetadataNet *metadataNet = [[CCMetadataNet alloc] initWithAccount:app.activeAccount];
  308. metadataNet.action = actionReadFile;
  309. metadataNet.fileName = metadata.fileName;
  310. metadataNet.fileNamePrint = metadata.fileNamePrint;
  311. metadataNet.serverUrl = serverUrl;
  312. metadataNet.selector = selectorReadFileOffline;
  313. metadataNet.priority = NSOperationQueuePriorityVeryLow;
  314. [app addNetworkingOperationQueue:app.netQueue delegate:self metadataNet:metadataNet];
  315. }
  316. }
  317. #pragma --------------------------------------------------------------------------------------------
  318. #pragma mark ===== Swipe Table -> menu =====
  319. #pragma--------------------------------------------------------------------------------------------
  320. // more
  321. - (NSString *)tableView:(UITableView *)tableView titleForSwipeAccessoryButtonForRowAtIndexPath:(NSIndexPath *)indexPath
  322. {
  323. if ([_typeOfController isEqualToString:@"offline"] == NO) {
  324. NSString *cameraFolderName = [CCCoreData getCameraUploadFolderNameActiveAccount:app.activeAccount];
  325. NSString *cameraFolderPath = [CCCoreData getCameraUploadFolderPathActiveAccount:app.activeAccount activeUrl:app.activeUrl typeCloud:app.typeCloud];
  326. CCMetadata *metadata = [CCUtility insertFileSystemInMetadata:[dataSource objectAtIndex:indexPath.row] directory:_localServerUrl activeAccount:app.activeAccount cameraFolderName:cameraFolderName cameraFolderPath:cameraFolderPath];
  327. if (metadata.directory)
  328. return nil;
  329. else
  330. return NSLocalizedString(@"_more_", nil);
  331. } else return nil;
  332. }
  333. - (void)tableView:(UITableView *)tableView swipeAccessoryButtonPushedForRowAtIndexPath:(NSIndexPath *)indexPath
  334. {
  335. UIImage *iconHeader;
  336. NSString *cameraFolderName = [CCCoreData getCameraUploadFolderNameActiveAccount:app.activeAccount];
  337. NSString *cameraFolderPath = [CCCoreData getCameraUploadFolderPathActiveAccount:app.activeAccount activeUrl:app.activeUrl typeCloud:app.typeCloud];
  338. self.metadata = [CCUtility insertFileSystemInMetadata:[dataSource objectAtIndex:indexPath.row] directory:_localServerUrl activeAccount:app.activeAccount cameraFolderName:cameraFolderName cameraFolderPath:cameraFolderPath];
  339. [self setEditing:NO animated:YES];
  340. AHKActionSheet *actionSheet = [[AHKActionSheet alloc] initWithView:self.view title:nil];
  341. actionSheet.animationDuration = 0.2;
  342. actionSheet.blurRadius = 0.0f;
  343. actionSheet.blurTintColor = [UIColor colorWithWhite:0.0f alpha:0.50f];
  344. actionSheet.buttonHeight = 50.0;
  345. actionSheet.cancelButtonHeight = 50.0f;
  346. actionSheet.separatorHeight = 5.0f;
  347. actionSheet.encryptedButtonTextAttributes = @{ NSFontAttributeName:[UIFont systemFontOfSize:14], NSForegroundColorAttributeName:COLOR_ENCRYPTED };
  348. actionSheet.buttonTextAttributes = @{ NSFontAttributeName:[UIFont systemFontOfSize:14], NSForegroundColorAttributeName:COLOR_GRAY };
  349. actionSheet.cancelButtonTextAttributes = @{ NSFontAttributeName:[UIFont systemFontOfSize:16], NSForegroundColorAttributeName:COLOR_BRAND };
  350. actionSheet.disableButtonTextAttributes = @{ NSFontAttributeName:[UIFont systemFontOfSize:12], NSForegroundColorAttributeName:COLOR_GRAY };
  351. actionSheet.separatorColor = COLOR_SEPARATOR_TABLE;
  352. actionSheet.cancelButtonTitle = NSLocalizedString(@"_cancel_",nil);
  353. // assegnamo l'immagine anteprima se esiste, altrimenti metti quella standars
  354. if ([[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"%@/.%@.ico", _localServerUrl, self.metadata.fileNamePrint]])
  355. iconHeader = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/.%@.ico", _localServerUrl, self.metadata.fileNamePrint]];
  356. else
  357. iconHeader = [UIImage imageNamed:self.metadata.iconName];
  358. [actionSheet addButtonWithTitle: _metadata.fileNamePrint
  359. image: iconHeader
  360. backgroundColor: COLOR_NAVBAR_IOS7
  361. height: 50.0
  362. type: AHKActionSheetButtonTypeDisabled
  363. handler: nil
  364. ];
  365. [actionSheet addButtonWithTitle: NSLocalizedString(@"_open_in_", nil)
  366. image: [UIImage imageNamed:image_actionSheetOpenIn]
  367. backgroundColor: [UIColor whiteColor]
  368. height: 50.0
  369. type: AHKActionSheetButtonTypeDefault
  370. handler: ^(AHKActionSheet *as) {
  371. [self performSelector:@selector(openWith:) withObject:self.metadata];
  372. }];
  373. [actionSheet show];
  374. }
  375. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
  376. {
  377. // close swip
  378. [self setEditing:NO animated:YES];
  379. if ([_typeOfController isEqualToString:@"offline"]) {
  380. NSManagedObject *record = [dataSource objectAtIndex:indexPath.row];
  381. [CCCoreData removeOfflineFileID:[record valueForKey:@"fileID"] activeAccount:app.activeAccount];
  382. }
  383. if ([_typeOfController isEqualToString:@"offline"] == NO) {
  384. NSString *fileNamePath = [NSString stringWithFormat:@"%@/%@", _localServerUrl,[dataSource objectAtIndex:indexPath.row]];
  385. NSString *iconPath = [NSString stringWithFormat:@"%@/.%@.ico", _localServerUrl,[dataSource objectAtIndex:indexPath.row]];
  386. [[NSFileManager defaultManager] removeItemAtPath:fileNamePath error:nil];
  387. [[NSFileManager defaultManager] removeItemAtPath:iconPath error:nil];
  388. }
  389. [self reloadTable];
  390. }
  391. #pragma --------------------------------------------------------------------------------------------
  392. #pragma mark ==== Table ====
  393. #pragma --------------------------------------------------------------------------------------------
  394. - (void)initToHome
  395. {
  396. _typeOfController = @"Offline";
  397. UIBarButtonItem *_btn=[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:image_navBarLocal] style:UIBarButtonItemStylePlain target:self action:@selector(switchOfflineLocal)];
  398. self.navigationItem.rightBarButtonItem=_btn;
  399. [self reloadTable];
  400. }
  401. - (void)reloadTable
  402. {
  403. [dataSource removeAllObjects];
  404. if ([_typeOfController isEqualToString:@"offline"]) {
  405. if (!_localServerUrl) {
  406. dataSource = (NSMutableArray*)[CCCoreData getHomeOfflineActiveAccount:app.activeAccount directoryUser:app.directoryUser];
  407. self.title = @"offline";
  408. } else {
  409. NSString *directoryID = [CCCoreData getDirectoryIDFromServerUrl:_localServerUrl activeAccount:app.activeAccount];
  410. NSArray *recordsTableMetadata = [CCCoreData getTableMetadataWithPredicate:[NSPredicate predicateWithFormat:@"(account == %@) AND (directoryID == %@)", app.activeAccount, directoryID] fieldOrder:[CCUtility getOrderSettings] ascending:[CCUtility getAscendingSettings]];
  411. CCSectionDataSource *sectionDataSource = [CCSection creataDataSourseSectionTableMetadata:recordsTableMetadata listProgressMetadata:nil groupByField:nil replaceDateToExifDate:NO activeAccount:app.activeAccount];
  412. for (NSString *key in sectionDataSource.allRecordsDataSource)
  413. [dataSource insertObject:[sectionDataSource.allRecordsDataSource objectForKey:key] atIndex:0 ];
  414. self.title = [_localServerUrl lastPathComponent];
  415. }
  416. }
  417. if ([_typeOfController isEqualToString:@"local"]) {
  418. NSArray *subpaths = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:_localServerUrl error:nil];
  419. for (NSString *subpath in subpaths)
  420. if (![[subpath lastPathComponent] hasPrefix:@"."]) [dataSource addObject:subpath];
  421. }
  422. [self.tableView reloadData];
  423. }
  424. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  425. {
  426. return 60;
  427. }
  428. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  429. {
  430. return 1;
  431. }
  432. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  433. {
  434. return [dataSource count];
  435. }
  436. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  437. {
  438. CCCellOffline *cell = (CCCellOffline *)[tableView dequeueReusableCellWithIdentifier:@"OfflineCell" forIndexPath:indexPath];
  439. // change color selection
  440. UIView *selectionColor = [[UIView alloc] init];
  441. selectionColor.backgroundColor = COLOR_SELECT_BACKGROUND;
  442. cell.selectedBackgroundView = selectionColor;
  443. // i am in Offline
  444. if ([_typeOfController isEqualToString:@"offline"]) {
  445. self.metadata = [dataSource objectAtIndex:indexPath.row];
  446. cell.fileImageView.image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@.ico", app.directoryUser, self.metadata.fileID]];
  447. }
  448. // i am in local
  449. if ([_typeOfController isEqualToString:@"offline"] == NO) {
  450. NSString *cameraFolderName = [CCCoreData getCameraUploadFolderNameActiveAccount:app.activeAccount];
  451. NSString *cameraFolderPath = [CCCoreData getCameraUploadFolderPathActiveAccount:app.activeAccount activeUrl:app.activeUrl typeCloud:app.typeCloud];
  452. self.metadata = [CCUtility insertFileSystemInMetadata:[dataSource objectAtIndex:indexPath.row] directory:_localServerUrl activeAccount:app.activeAccount cameraFolderName:cameraFolderName cameraFolderPath:cameraFolderPath];
  453. cell.fileImageView.image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/.%@.ico", _localServerUrl, self.metadata.fileNamePrint]];
  454. if (!cell.fileImageView.image) {
  455. UIImage *icon = [CCGraphics createNewImageFrom:self.metadata.fileID directoryUser:_localServerUrl fileNameTo:self.metadata.fileID fileNamePrint:self.metadata.fileNamePrint size:@"m" imageForUpload:NO typeFile:self.metadata.typeFile writePreview:NO optimizedFileName:[CCUtility getOptimizedPhoto]];
  456. if (icon) {
  457. [CCGraphics saveIcoWithFileID:self.metadata.fileNamePrint image:icon writeToFile:[NSString stringWithFormat:@"%@/.%@.ico", _localServerUrl, self.metadata.fileNamePrint] copy:NO move:NO fromPath:nil toPath:nil];
  458. cell.fileImageView.image = icon;
  459. }
  460. }
  461. }
  462. // color and font
  463. if (self.metadata.cryptated) {
  464. cell.labelTitle.textColor = COLOR_ENCRYPTED;
  465. //nameLabel.font = RalewayLight(13.0f);
  466. cell.labelInfoFile.textColor = [UIColor blackColor];
  467. //detailLabel.font = RalewayLight(9.0f);
  468. } else {
  469. cell.labelTitle.textColor = COLOR_CLEAR;
  470. //nameLabel.font = RalewayLight(13.0f);
  471. cell.labelInfoFile.textColor = [UIColor blackColor];
  472. //detailLabel.font = RalewayLight(9.0f);
  473. }
  474. if (self.metadata.directory) {
  475. cell.labelInfoFile.text = [CCUtility dateDiff:self.metadata.date];
  476. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  477. }
  478. // File name
  479. cell.labelTitle.text = self.metadata.fileNamePrint;
  480. cell.labelInfoFile.text = @"";
  481. // Immagine del file, se non c'è l'anteprima mettiamo quella standard
  482. if (cell.fileImageView.image == nil)
  483. cell.fileImageView.image = [UIImage imageNamed:self.metadata.iconName];
  484. cell.statusImageView.image = nil;
  485. // it's encrypted ???
  486. if (self.metadata.cryptated && [self.metadata.type isEqualToString:metadataType_model] == NO)
  487. cell.statusImageView.image = [UIImage imageNamed:image_lock];
  488. // it's in download mode
  489. if ([self.metadata.session length] > 0 && [self.metadata.session rangeOfString:@"download"].location != NSNotFound)
  490. cell.statusImageView.image = [UIImage imageNamed:image_attention];
  491. // text and length
  492. if (self.metadata.directory) {
  493. cell.labelInfoFile.text = [CCUtility dateDiff:self.metadata.date];
  494. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  495. } else {
  496. NSString *date = [CCUtility dateDiff:self.metadata.date];
  497. NSString *length = [CCUtility transformedSize:self.metadata.size];
  498. if ([self.metadata.type isEqualToString:metadataType_model])
  499. cell.labelInfoFile.text = [NSString stringWithFormat:@"%@", date];
  500. if ([self.metadata.type isEqualToString:metadataType_file] || [self.metadata.type isEqualToString:metadataType_local])
  501. cell.labelInfoFile.text = [NSString stringWithFormat:@"%@, %@", date, length];
  502. cell.accessoryType = UITableViewCellAccessoryNone;
  503. }
  504. return cell;
  505. }
  506. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  507. {
  508. // deselect row
  509. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  510. if ([_typeOfController isEqualToString:@"offline"]) {
  511. NSManagedObject *record = [dataSource objectAtIndex:indexPath.row];
  512. self.fileIDPhoto = [record valueForKey:@"fileID"];
  513. self.metadata = [CCCoreData getMetadataWithPreficate:[NSPredicate predicateWithFormat:@"(fileID == %@) AND (account == %@)", self.fileIDPhoto, app.activeAccount] context:nil];
  514. }
  515. if ([_typeOfController isEqualToString:@"local"]) {
  516. NSString *cameraFolderName = [CCCoreData getCameraUploadFolderNameActiveAccount:app.activeAccount];
  517. NSString *cameraFolderPath = [CCCoreData getCameraUploadFolderPathActiveAccount:app.activeAccount activeUrl:app.activeUrl typeCloud:app.typeCloud];
  518. self.metadata = [CCUtility insertFileSystemInMetadata:[dataSource objectAtIndex:indexPath.row] directory:_localServerUrl activeAccount:app.activeAccount cameraFolderName:cameraFolderName cameraFolderPath:cameraFolderPath];
  519. self.fileIDPhoto = self.metadata.fileID;
  520. }
  521. // if is in download [do not touch]
  522. if ([self.metadata.session length] > 0 && [self.metadata.session rangeOfString:@"download"].location != NSNotFound) return;
  523. if (([self.metadata.type isEqualToString:metadataType_file] || [self.metadata.type isEqualToString:metadataType_local]) && self.metadata.directory == NO) {
  524. if ([self shouldPerformSegue])
  525. [self performSegueWithIdentifier:@"segueDetail" sender:self];
  526. }
  527. if ([self.metadata.type isEqualToString:metadataType_model]) [self openModel:self.metadata];
  528. if (self.metadata.directory)
  529. [self performSegueDirectoryWithControlPasscode];
  530. }
  531. -(void)performSegueDirectoryWithControlPasscode
  532. {
  533. CCOffline *vc = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"OfflineViewController"];
  534. NSString *serverUrl = [CCCoreData getServerUrlFromDirectoryID:_metadata.directoryID activeAccount:app.activeAccount];
  535. vc.localServerUrl = [CCUtility stringAppendServerUrl:serverUrl addServerUrl:_metadata.fileName];
  536. vc.typeOfController = _typeOfController;
  537. [self.navigationController pushViewController:vc animated:YES];
  538. }
  539. #pragma --------------------------------------------------------------------------------------------
  540. #pragma mark ===== Navigation ====
  541. #pragma --------------------------------------------------------------------------------------------
  542. - (BOOL)shouldPerformSegue
  543. {
  544. // if i am in background -> exit
  545. if ([[UIApplication sharedApplication] applicationState] == UIApplicationStateBackground) return NO;
  546. // if i am not window -> exit
  547. if (self.view.window == NO)
  548. return NO;
  549. // Collapsed but i am in detail -> exit
  550. if (self.splitViewController.isCollapsed)
  551. if (self.detailViewController.isViewLoaded && self.detailViewController.view.window) return NO;
  552. // Video in run -> exit
  553. if (self.detailViewController.photoBrowser.currentVideoPlayerViewController.isViewLoaded && self.detailViewController.photoBrowser.currentVideoPlayerViewController.view.window) return NO;
  554. return YES;
  555. }
  556. -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
  557. {
  558. id viewController = segue.destinationViewController;
  559. if ([viewController isKindOfClass:[UINavigationController class]]) {
  560. UINavigationController *nav = viewController;
  561. self.detailViewController = (CCDetail *)nav.topViewController;
  562. } else {
  563. self.detailViewController = segue.destinationViewController;
  564. }
  565. self.detailViewController.metadataDetail = self.metadata;
  566. if (app.isLocalStorage) self.detailViewController.sourceDirectory = sorceDirectoryLocal;
  567. else self.detailViewController.sourceDirectory = sorceDirectoryOffline;
  568. self.detailViewController.dateFilterQuery = nil;
  569. self.detailViewController.isCameraUpload = NO;
  570. [self.detailViewController setTitle:self.metadata.fileNamePrint];
  571. }
  572. @end