CCOffline.m 33 KB

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