CCTransfers.m 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  1. //
  2. // CCTransfers.m
  3. // Crypto Cloud Technology Nextcloud
  4. //
  5. // Created by Marino Faggiana on 12/04/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 "CCTransfers.h"
  24. #import "AppDelegate.h"
  25. #import "CCMain.h"
  26. #import "CCDetail.h"
  27. #import "CCSection.h"
  28. #import "CCMetadata.h"
  29. #import "CCTransfersCell.h"
  30. #ifdef CUSTOM_BUILD
  31. #import "CustomSwift.h"
  32. #else
  33. #import "Nextcloud-Swift.h"
  34. #endif
  35. #define download 1
  36. #define downloadwwan 2
  37. #define upload 3
  38. #define uploadwwan 4
  39. @interface CCTransfers ()
  40. {
  41. // Datasource
  42. CCSectionDataSourceMetadata *_sectionDataSource;
  43. }
  44. @end
  45. @implementation CCTransfers
  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. app.activeTransfers = self;
  55. }
  56. return self;
  57. }
  58. - (void)viewDidLoad {
  59. [super viewDidLoad];
  60. // Custom Cell
  61. [_tableView registerNib:[UINib nibWithNibName:@"CCTransfersCell" bundle:nil] forCellReuseIdentifier:@"Cell"];
  62. _tableView.delegate = self;
  63. _tableView.dataSource = self;
  64. _tableView.emptyDataSetDelegate = self;
  65. _tableView.emptyDataSetSource = self;
  66. _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  67. _tableView.backgroundColor = [NCBrandColor sharedInstance].tableBackground;
  68. self.title = NSLocalizedString(@"_transfers_", nil);
  69. [self reloadDatasource];
  70. }
  71. // Apparirà
  72. - (void)viewWillAppear:(BOOL)animated
  73. {
  74. [super viewWillAppear:animated];
  75. // Color
  76. [app aspectNavigationControllerBar:self.navigationController.navigationBar encrypted:NO online:[app.reachability isReachable] hidden:NO];
  77. [app aspectTabBar:self.tabBarController.tabBar hidden:NO];
  78. }
  79. // E' arrivato
  80. - (void)viewDidAppear:(BOOL)animated
  81. {
  82. [super viewDidAppear:animated];
  83. [self reloadDatasource];
  84. }
  85. - (void)changeTheming
  86. {
  87. [app changeTheming:self];
  88. }
  89. #pragma --------------------------------------------------------------------------------------------
  90. #pragma mark ==== DZNEmptyDataSetSource ====
  91. #pragma --------------------------------------------------------------------------------------------
  92. - (CGFloat)spaceHeightForEmptyDataSet:(UIScrollView *)scrollView
  93. {
  94. return 0.0f;
  95. }
  96. - (UIColor *)backgroundColorForEmptyDataSet:(UIScrollView *)scrollView
  97. {
  98. return [UIColor whiteColor];
  99. }
  100. - (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView
  101. {
  102. return [UIImage imageNamed:image_transfersNoRecord];
  103. }
  104. - (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView
  105. {
  106. NSString *text = [NSString stringWithFormat:@"%@", NSLocalizedString(@"_no_transfer_", nil)];
  107. NSDictionary *attributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:20.0f], NSForegroundColorAttributeName:[UIColor lightGrayColor]};
  108. return [[NSAttributedString alloc] initWithString:text attributes:attributes];
  109. }
  110. - (NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView
  111. {
  112. NSString *text = [NSString stringWithFormat:@"%@", NSLocalizedString(@"_no_transfer_sub_", nil)];
  113. NSMutableParagraphStyle *paragraph = [NSMutableParagraphStyle new];
  114. paragraph.lineBreakMode = NSLineBreakByWordWrapping;
  115. paragraph.alignment = NSTextAlignmentCenter;
  116. NSDictionary *attributes = @{NSFontAttributeName: [UIFont systemFontOfSize:14.0], NSForegroundColorAttributeName: [UIColor lightGrayColor], NSParagraphStyleAttributeName: paragraph};
  117. return [[NSAttributedString alloc] initWithString:text attributes:attributes];
  118. }
  119. #pragma --------------------------------------------------------------------------------------------
  120. #pragma mark - ===== Progress & Task Button =====
  121. #pragma --------------------------------------------------------------------------------------------
  122. - (void)triggerProgressTask:(NSNotification *)notification
  123. {
  124. NSDictionary *dict = notification.userInfo;
  125. NSString *fileID = [dict valueForKey:@"fileID"];
  126. BOOL cryptated = [[dict valueForKey:@"cryptated"] boolValue];
  127. float progress = [[dict valueForKey:@"progress"] floatValue];
  128. // Check
  129. if (!fileID)
  130. return;
  131. [app.listProgressMetadata setObject:[NSNumber numberWithFloat:progress] forKey:fileID];
  132. NSIndexPath *indexPath = [_sectionDataSource.fileIDIndexPath objectForKey:fileID];
  133. if (indexPath && indexPath.row == 0) {
  134. CCTransfersCell *cell = (CCTransfersCell *)[_tableView cellForRowAtIndexPath:indexPath];
  135. if (cryptated) cell.progressView.progressTintColor = [NCBrandColor sharedInstance].cryptocloud;
  136. else cell.progressView.progressTintColor = [UIColor blackColor];
  137. cell.progressView.hidden = NO;
  138. [cell.progressView setProgress:progress];
  139. } else {
  140. [self reloadDatasource];
  141. }
  142. }
  143. - (void)reloadTaskButton:(id)sender withEvent:(UIEvent *)event
  144. {
  145. if (app.activeMain == nil)
  146. return;
  147. UITouch * touch = [[event allTouches] anyObject];
  148. CGPoint location = [touch locationInView:_tableView];
  149. NSIndexPath * indexPath = [_tableView indexPathForRowAtPoint:location];
  150. if (indexPath) {
  151. NSString *fileID = [[_sectionDataSource.sectionArrayRow objectForKey:[_sectionDataSource.sections objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];
  152. CCMetadata *metadata = [_sectionDataSource.allRecordsDataSource objectForKey:fileID];
  153. if (metadata)
  154. [app.activeMain reloadTaskButton:metadata];
  155. }
  156. }
  157. - (void)reloadAllTask
  158. {
  159. if (app.activeMain == nil)
  160. return;
  161. for (NSString *key in _sectionDataSource.allRecordsDataSource.allKeys) {
  162. CCMetadata *metadata = [_sectionDataSource.allRecordsDataSource objectForKey:key];
  163. if ([metadata.session containsString:@"download"] && (metadata.sessionTaskIdentifierPlist != k_taskIdentifierDone))
  164. continue;
  165. if ([metadata.session containsString:@"upload"] && (metadata.sessionTaskIdentifier != k_taskIdentifierStop))
  166. continue;
  167. [app.activeMain reloadTaskButton:metadata];
  168. }
  169. }
  170. - (void)cancelTaskButton:(id)sender withEvent:(UIEvent *)event
  171. {
  172. if (app.activeMain == nil)
  173. return;
  174. UITouch * touch = [[event allTouches] anyObject];
  175. CGPoint location = [touch locationInView:_tableView];
  176. NSIndexPath * indexPath = [_tableView indexPathForRowAtPoint:location];
  177. if (indexPath) {
  178. NSString *fileID = [[_sectionDataSource.sectionArrayRow objectForKey:[_sectionDataSource.sections objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];
  179. CCMetadata *metadata = [_sectionDataSource.allRecordsDataSource objectForKey:fileID];
  180. if (metadata)
  181. [app.activeMain cancelTaskButton:metadata reloadTable:YES];
  182. }
  183. }
  184. - (void)cancelAllTask
  185. {
  186. if (app.activeMain == nil)
  187. return;
  188. BOOL lastAndRefresh = NO;
  189. for (NSString *key in _sectionDataSource.allRecordsDataSource.allKeys) {
  190. if ([key isEqualToString:[_sectionDataSource.allRecordsDataSource.allKeys lastObject]])
  191. lastAndRefresh = YES;
  192. CCMetadata *metadata = [_sectionDataSource.allRecordsDataSource objectForKey:key];
  193. if ([metadata.session containsString:@"upload"] && metadata.cryptated && ((metadata.sessionTaskIdentifier == k_taskIdentifierDone && metadata.sessionTaskIdentifierPlist >= 0) || (metadata.sessionTaskIdentifier >= 0 && metadata.sessionTaskIdentifierPlist == k_taskIdentifierDone)))
  194. continue;
  195. [app.activeMain cancelTaskButton:metadata reloadTable:lastAndRefresh];
  196. }
  197. }
  198. - (void)stopTaskButton:(id)sender withEvent:(UIEvent *)event
  199. {
  200. if (app.activeMain == nil)
  201. return;
  202. UITouch * touch = [[event allTouches] anyObject];
  203. CGPoint location = [touch locationInView:_tableView];
  204. NSIndexPath * indexPath = [_tableView indexPathForRowAtPoint:location];
  205. if (indexPath) {
  206. NSString *fileID = [[_sectionDataSource.sectionArrayRow objectForKey:[_sectionDataSource.sections objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];
  207. CCMetadata *metadata = [_sectionDataSource.allRecordsDataSource objectForKey:fileID];
  208. if (metadata)
  209. [app.activeMain stopTaskButton:metadata];
  210. }
  211. }
  212. - (void)stopAllTask
  213. {
  214. if (app.activeMain == nil)
  215. return;
  216. for (NSString *key in _sectionDataSource.allRecordsDataSource.allKeys) {
  217. CCMetadata *metadata = [_sectionDataSource.allRecordsDataSource objectForKey:key];
  218. if ([metadata.session containsString:@"download"]) {
  219. [app.activeMain cancelTaskButton:metadata reloadTable:YES];
  220. continue;
  221. }
  222. if ([metadata.session containsString:@"upload"] && metadata.cryptated && ((metadata.sessionTaskIdentifier == k_taskIdentifierDone && metadata.sessionTaskIdentifierPlist >= 0) || (metadata.sessionTaskIdentifier >= 0 && metadata.sessionTaskIdentifierPlist == k_taskIdentifierDone)))
  223. continue;
  224. [app.activeMain stopTaskButton:metadata];
  225. }
  226. }
  227. #pragma --------------------------------------------------------------------------------------------
  228. #pragma mark - ==== Datasource ====
  229. #pragma --------------------------------------------------------------------------------------------
  230. - (void)reloadDatasource
  231. {
  232. // test
  233. if (app.activeAccount.length == 0)
  234. return;
  235. NSArray *recordsTableMetadata = [CCCoreData getTableMetadataWithPredicate:[NSPredicate predicateWithFormat:@"(account == %@) AND ((session CONTAINS 'upload') OR (session CONTAINS 'download' AND (sessionSelector != 'loadPlist')))", app.activeAccount] fieldOrder:@"sessionTaskIdentifier" ascending:YES];
  236. _sectionDataSource = [CCSectionMetadata creataDataSourseSectionMetadata:recordsTableMetadata listProgressMetadata:app.listProgressMetadata groupByField:@"session" replaceDateToExifDate:NO activeAccount:app.activeAccount];
  237. [_tableView reloadData];
  238. }
  239. #pragma --------------------------------------------------------------------------------------------
  240. #pragma mark - ==== Table ====
  241. #pragma --------------------------------------------------------------------------------------------
  242. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  243. {
  244. return 50;
  245. }
  246. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  247. {
  248. return [[_sectionDataSource.sectionArrayRow allKeys] count];
  249. }
  250. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  251. {
  252. return [[_sectionDataSource.sectionArrayRow objectForKey:[_sectionDataSource.sections objectAtIndex:section]] count];
  253. }
  254. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
  255. {
  256. return 13.0f;
  257. }
  258. -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
  259. {
  260. UIVisualEffectView *visualEffectView;
  261. NSString *titleSection, *numberTitle;
  262. NSInteger typeOfSession = 0;
  263. if ([[_sectionDataSource.sections objectAtIndex:section] isKindOfClass:[NSString class]]) titleSection = [_sectionDataSource.sections objectAtIndex:section];
  264. if ([[_sectionDataSource.sections objectAtIndex:section] isKindOfClass:[NSDate class]]) titleSection = [CCUtility getTitleSectionDate:[_sectionDataSource.sections objectAtIndex:section]];
  265. NSArray *metadatas = [_sectionDataSource.sectionArrayRow objectForKey:[_sectionDataSource.sections objectAtIndex:section]];
  266. NSUInteger rowsCount = [metadatas count];
  267. visualEffectView = [[UIVisualEffectView alloc] init];
  268. visualEffectView.backgroundColor = [UIColor clearColor];
  269. // title section
  270. if ([titleSection isEqualToString:@"_none_"]) {
  271. titleSection = @"";
  272. } else if ([titleSection containsString:@"download"] && ![titleSection containsString:@"wwan"]) {
  273. typeOfSession = download;
  274. titleSection = NSLocalizedString(@"_title_section_download_",nil);
  275. } else if ([titleSection containsString:@"download"] && [titleSection containsString:@"wwan"]) {
  276. typeOfSession = downloadwwan;
  277. titleSection = [NSLocalizedString(@"_title_section_download_",nil) stringByAppendingString:@" Wi-Fi"];
  278. } else if ([titleSection containsString:@"upload"] && ![titleSection containsString:@"wwan"]) {
  279. typeOfSession = upload;
  280. titleSection = NSLocalizedString(@"_title_section_upload_",nil);
  281. } else if ([titleSection containsString:@"upload"] && [titleSection containsString:@"wwan"]) {
  282. typeOfSession = uploadwwan;
  283. titleSection = [NSLocalizedString(@"_title_section_upload_",nil) stringByAppendingString:@" Wi-Fi"];
  284. } else {
  285. titleSection = NSLocalizedString(titleSection,nil);
  286. }
  287. // title label on left
  288. UILabel *titleLabel=[[UILabel alloc]initWithFrame:CGRectMake(8, 3, 0, 13)];
  289. titleLabel.textColor = [UIColor blackColor];
  290. titleLabel.font = [UIFont systemFontOfSize:9];
  291. titleLabel.textAlignment = NSTextAlignmentLeft;
  292. titleLabel.text = titleSection;
  293. titleLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  294. [visualEffectView addSubview:titleLabel];
  295. // element (s) on right
  296. UILabel *elementLabel=[[UILabel alloc]initWithFrame:CGRectMake(-8, 3, 0, 13)];
  297. elementLabel.textColor = [UIColor blackColor];
  298. elementLabel.font = [UIFont systemFontOfSize:9];
  299. elementLabel.textAlignment = NSTextAlignmentRight;
  300. elementLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  301. if ((typeOfSession == download && app.queueNunDownload > rowsCount) || (typeOfSession == downloadwwan && app.queueNumDownloadWWan > rowsCount) ||
  302. (typeOfSession == upload && app.queueNumUpload > rowsCount) || (typeOfSession == uploadwwan && app.queueNumUploadWWan > rowsCount)) {
  303. numberTitle = [NSString stringWithFormat:@"%lu+", (unsigned long)rowsCount];
  304. } else {
  305. numberTitle = [NSString stringWithFormat:@"%lu", (unsigned long)rowsCount];
  306. }
  307. if (rowsCount > 1)
  308. elementLabel.text = [NSString stringWithFormat:@"%@ %@", numberTitle, NSLocalizedString(@"_elements_",nil)];
  309. else
  310. elementLabel.text = [NSString stringWithFormat:@"%@ %@", numberTitle, NSLocalizedString(@"_element_",nil)];
  311. // view
  312. [visualEffectView addSubview:elementLabel];
  313. return visualEffectView;
  314. }
  315. - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
  316. {
  317. NSString *titleSection;
  318. NSString *element_s;
  319. if ([[_sectionDataSource.sections objectAtIndex:section] isKindOfClass:[NSString class]]) titleSection = [_sectionDataSource.sections objectAtIndex:section];
  320. // Prepare view for title in footer
  321. UIView *view = [[UIView alloc] initWithFrame:CGRectZero];
  322. UILabel *titleFooterLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 18)];
  323. titleFooterLabel.textColor = [UIColor blackColor];
  324. titleFooterLabel.font = [UIFont systemFontOfSize:12];
  325. titleFooterLabel.textAlignment = NSTextAlignmentCenter;
  326. // Footer Download
  327. if ([titleSection containsString:@"download"] && ![titleSection containsString:@"wwan"] && titleSection != nil) {
  328. // element or elements ?
  329. if (app.queueNunDownload > 1) element_s = NSLocalizedString(@"_elements_",nil);
  330. else element_s = NSLocalizedString(@"_element_",nil);
  331. // Num record to upload
  332. NSMutableAttributedString *stringFooter= [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:NSLocalizedString(@"_tite_footer_download_", nil), app.queueNunDownload, element_s]];
  333. titleFooterLabel.attributedText = stringFooter;
  334. [view addSubview:titleFooterLabel];
  335. return view;
  336. }
  337. // Footer Download WWAN
  338. if ([titleSection containsString:@"download"] && [titleSection containsString:@"wwan"] && titleSection != nil) {
  339. // element or elements ?
  340. if (app.queueNumDownloadWWan > 1) element_s = NSLocalizedString(@"_elements_",nil);
  341. else element_s = NSLocalizedString(@"_element_",nil);
  342. // Add the symbol WiFi and Num record
  343. NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
  344. attachment.image = [UIImage imageNamed:image_WiFiSmall];
  345. NSAttributedString *attachmentString = [NSAttributedString attributedStringWithAttachment:attachment];
  346. NSMutableAttributedString *stringFooter= [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:NSLocalizedString(@"_tite_footer_download_wwan_", nil), app.queueNumDownloadWWan, element_s]];
  347. [stringFooter insertAttributedString:attachmentString atIndex:0];
  348. titleFooterLabel.attributedText = stringFooter;
  349. [view addSubview:titleFooterLabel];
  350. return view;
  351. }
  352. // Footer Upload
  353. if ([titleSection containsString:@"upload"] && ![titleSection containsString:@"wwan"] && titleSection != nil) {
  354. // element or elements ?
  355. if (app.queueNumUpload > 1) element_s = NSLocalizedString(@"_elements_",nil);
  356. else element_s = NSLocalizedString(@"_element_",nil);
  357. // Num record to upload
  358. NSMutableAttributedString *stringFooter= [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:NSLocalizedString(@"_tite_footer_upload_", nil), app.queueNumUpload, element_s]];
  359. titleFooterLabel.attributedText = stringFooter;
  360. [view addSubview:titleFooterLabel];
  361. return view;
  362. }
  363. // Footer Upload WWAN
  364. if ([titleSection containsString:@"upload"] && [titleSection containsString:@"wwan"] && titleSection != nil) {
  365. // element or elements ?
  366. if (app.queueNumUploadWWan > 1) element_s = NSLocalizedString(@"_elements_",nil);
  367. else element_s = NSLocalizedString(@"_element_",nil);
  368. // Add the symbol WiFi and Num record
  369. NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
  370. attachment.image = [UIImage imageNamed:image_WiFiSmall];
  371. NSAttributedString *attachmentString = [NSAttributedString attributedStringWithAttachment:attachment];
  372. NSMutableAttributedString *stringFooter= [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:NSLocalizedString(@"_tite_footer_upload_wwan_", nil), app.queueNumUploadWWan,element_s]];
  373. [stringFooter insertAttributedString:attachmentString atIndex:0];
  374. titleFooterLabel.attributedText = stringFooter;
  375. [view addSubview:titleFooterLabel];
  376. return view;
  377. }
  378. return nil;
  379. }
  380. - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
  381. {
  382. //NSString *titleSection;
  383. //if ([[_sectionDataSource.sections objectAtIndex:section] isKindOfClass:[NSString class]])
  384. // titleSection = [_sectionDataSource.sections objectAtIndex:section];
  385. //if ([titleSection rangeOfString:@"upload"].location != NSNotFound && [titleSection rangeOfString:@"wwan"].location != NSNotFound && titleSection != nil) return 18.0f;
  386. //else return 0.0f;
  387. return 18.0f;
  388. }
  389. - (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
  390. {
  391. return [_sectionDataSource.sections indexOfObject:title];
  392. }
  393. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  394. {
  395. NSString *dataFile;
  396. NSString *lunghezzaFile;
  397. NSString *fileID = [[_sectionDataSource.sectionArrayRow objectForKey:[_sectionDataSource.sections objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];
  398. CCMetadata *metadata = [_sectionDataSource.allRecordsDataSource objectForKey:fileID];
  399. CCTransfersCell *cell = (CCTransfersCell *)[tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
  400. cell.backgroundColor = [UIColor clearColor];
  401. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  402. // ----------------------------------------------------------------------------------------------------------
  403. // DEFAULT
  404. // ----------------------------------------------------------------------------------------------------------
  405. cell.fileImageView.image = nil;
  406. cell.statusImageView.image = nil;
  407. cell.labelTitle.enabled = YES;
  408. cell.labelTitle.text = @"";
  409. cell.labelInfoFile.enabled = YES;
  410. cell.labelInfoFile.text = @"";
  411. cell.progressView.progress = 0.0;
  412. cell.progressView.hidden = YES;
  413. cell.cancelTaskButton.hidden = YES;
  414. cell.reloadTaskButton.hidden = YES;
  415. cell.stopTaskButton.hidden = YES;
  416. // colori e font
  417. if (metadata.cryptated) {
  418. cell.labelTitle.textColor = [NCBrandColor sharedInstance].cryptocloud;
  419. cell.labelInfoFile.textColor = [UIColor blackColor];
  420. } else {
  421. cell.labelTitle.textColor = [UIColor blackColor];
  422. cell.labelInfoFile.textColor = [UIColor blackColor];
  423. }
  424. // ----------------------------------------------------------------------------------------------------------
  425. // File Name & Folder
  426. // ----------------------------------------------------------------------------------------------------------
  427. // nome del file
  428. cell.labelTitle.text = metadata.fileNamePrint;
  429. // è una directory
  430. if (metadata.directory) {
  431. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  432. cell.labelInfoFile.text = [CCUtility dateDiff:metadata.date];
  433. lunghezzaFile = @" ";
  434. } else {
  435. // è un file
  436. dataFile = [CCUtility dateDiff:metadata.date];
  437. lunghezzaFile = [CCUtility transformedSize:metadata.size];
  438. // Plist ancora da scaricare
  439. if (metadata.cryptated && [metadata.title length] == 0) {
  440. dataFile = @" ";
  441. lunghezzaFile = @" ";
  442. }
  443. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  444. [dateFormatter setDateStyle:NSDateFormatterShortStyle];
  445. [dateFormatter setTimeStyle:NSDateFormatterShortStyle];
  446. cell.accessoryType = UITableViewCellAccessoryNone;
  447. }
  448. // ----------------------------------------------------------------------------------------------------------
  449. // File Image View
  450. // ----------------------------------------------------------------------------------------------------------
  451. // assegnamo l'immagine anteprima se esiste, altrimenti metti quella standars
  452. if ([[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"%@/%@.ico", app.directoryUser, metadata.fileID]]) {
  453. cell.fileImageView.image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@.ico", app.directoryUser, metadata.fileID]];
  454. } else {
  455. cell.fileImageView.image = [UIImage imageNamed:metadata.iconName];
  456. }
  457. // ----------------------------------------------------------------------------------------------------------
  458. // Image Status cyptated & Lock Passcode
  459. // ----------------------------------------------------------------------------------------------------------
  460. // File Cyptated
  461. if (metadata.cryptated && metadata.directory == NO && [metadata.type isEqualToString: k_metadataType_template] == NO) {
  462. cell.statusImageView.image = [UIImage imageNamed:image_lock];
  463. }
  464. // ----------------------------------------------------------------------------------------------------------
  465. // downloadFile
  466. // ----------------------------------------------------------------------------------------------------------
  467. if ([metadata.session length] > 0 && [metadata.session rangeOfString:@"download"].location != NSNotFound) {
  468. if (metadata.cryptated) cell.statusImageView.image = [UIImage imageNamed:image_statusdownloadcrypto];
  469. else cell.statusImageView.image = [UIImage imageNamed:image_statusdownload];
  470. // Fai comparire il RELOAD e lo STOP solo se non è un Task Plist
  471. if (metadata.sessionTaskIdentifierPlist == k_taskIdentifierDone) {
  472. if (metadata.cryptated)[cell.cancelTaskButton setBackgroundImage:[UIImage imageNamed:image_stoptaskcrypto] forState:UIControlStateNormal];
  473. else [cell.cancelTaskButton setBackgroundImage:[UIImage imageNamed:image_stoptask] forState:UIControlStateNormal];
  474. cell.cancelTaskButton.hidden = NO;
  475. if (metadata.cryptated)[cell.reloadTaskButton setBackgroundImage:[UIImage imageNamed:image_reloadtaskcrypto] forState:UIControlStateNormal];
  476. else [cell.reloadTaskButton setBackgroundImage:[UIImage imageNamed:image_reloadtask] forState:UIControlStateNormal];
  477. cell.reloadTaskButton.hidden = NO;
  478. }
  479. cell.labelInfoFile.text = [NSString stringWithFormat:@"%@", lunghezzaFile];
  480. float progress = [[app.listProgressMetadata objectForKey:metadata.fileID] floatValue];
  481. if (progress > 0) {
  482. if (metadata.cryptated) cell.progressView.progressTintColor = [NCBrandColor sharedInstance].cryptocloud;
  483. else cell.progressView.progressTintColor = [UIColor blackColor];
  484. cell.progressView.progress = progress;
  485. cell.progressView.hidden = NO;
  486. }
  487. // ----------------------------------------------------------------------------------------------------------
  488. // downloadFile Error
  489. // ----------------------------------------------------------------------------------------------------------
  490. if (metadata.sessionTaskIdentifier == k_taskIdentifierError || metadata.sessionTaskIdentifierPlist == k_taskIdentifierError) {
  491. cell.statusImageView.image = [UIImage imageNamed:image_statuserror];
  492. if ([metadata.sessionError length] == 0)
  493. cell.labelInfoFile.text = [NSString stringWithFormat:@"%@, %@", NSLocalizedString(@"_error_",nil), NSLocalizedString(@"_file_not_downloaded_",nil)];
  494. else
  495. cell.labelInfoFile.text = [CCError manageErrorKCF:[metadata.sessionError integerValue] withNumberError:NO];
  496. }
  497. }
  498. // ----------------------------------------------------------------------------------------------------------
  499. // uploadFile
  500. // ----------------------------------------------------------------------------------------------------------
  501. if ([metadata.session length] > 0 && [metadata.session rangeOfString:@"upload"].location != NSNotFound) {
  502. if (metadata.cryptated) cell.statusImageView.image = [UIImage imageNamed:image_statusuploadcrypto];
  503. else cell.statusImageView.image = [UIImage imageNamed:image_statusupload];
  504. if (metadata.cryptated)[cell.cancelTaskButton setBackgroundImage:[UIImage imageNamed:image_removetaskcrypto] forState:UIControlStateNormal];
  505. else [cell.cancelTaskButton setBackgroundImage:[UIImage imageNamed:image_removetask] forState:UIControlStateNormal];
  506. cell.cancelTaskButton.hidden = NO;
  507. if (metadata.sessionTaskIdentifier == k_taskIdentifierStop) {
  508. if (metadata.cryptated)[cell.reloadTaskButton setBackgroundImage:[UIImage imageNamed:image_reloadtaskcrypto] forState:UIControlStateNormal];
  509. else [cell.reloadTaskButton setBackgroundImage:[UIImage imageNamed:image_reloadtask] forState:UIControlStateNormal];
  510. if (metadata.cryptated) cell.statusImageView.image = [UIImage imageNamed:image_statusstopcrypto];
  511. else cell.statusImageView.image = [UIImage imageNamed:image_statusstop];
  512. cell.reloadTaskButton.hidden = NO;
  513. cell.stopTaskButton.hidden = YES;
  514. } else {
  515. if (metadata.cryptated)[cell.stopTaskButton setBackgroundImage:[UIImage imageNamed:image_stoptaskcrypto] forState:UIControlStateNormal];
  516. else [cell.stopTaskButton setBackgroundImage:[UIImage imageNamed:image_stoptask] forState:UIControlStateNormal];
  517. cell.stopTaskButton.hidden = NO;
  518. cell.reloadTaskButton.hidden = YES;
  519. }
  520. // se non c'è una preview in bianconero metti l'immagine di default
  521. if ([[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"%@/%@.ico", app.directoryUser, metadata.fileID]] == NO)
  522. cell.fileImageView.image = [UIImage imageNamed:image_uploaddisable];
  523. cell.labelTitle.enabled = NO;
  524. cell.labelInfoFile.text = [NSString stringWithFormat:@"%@", lunghezzaFile];
  525. float progress = [[app.listProgressMetadata objectForKey:metadata.fileID] floatValue];
  526. if (progress > 0) {
  527. if (metadata.cryptated) cell.progressView.progressTintColor = [NCBrandColor sharedInstance].cryptocloud;
  528. else cell.progressView.progressTintColor = [UIColor blackColor];
  529. cell.progressView.progress = progress;
  530. cell.progressView.hidden = NO;
  531. }
  532. // ----------------------------------------------------------------------------------------------------------
  533. // uploadFileError
  534. // ----------------------------------------------------------------------------------------------------------
  535. if (metadata.sessionTaskIdentifier == k_taskIdentifierError || metadata.sessionTaskIdentifierPlist == k_taskIdentifierError) {
  536. cell.labelTitle.enabled = NO;
  537. cell.statusImageView.image = [UIImage imageNamed:image_statuserror];
  538. if ([metadata.sessionError length] == 0)
  539. cell.labelInfoFile.text = [NSString stringWithFormat:@"%@, %@", NSLocalizedString(@"_error_",nil), NSLocalizedString(@"_file_not_uploaded_",nil)];
  540. else
  541. cell.labelInfoFile.text = [CCError manageErrorKCF:[metadata.sessionError integerValue] withNumberError:NO];
  542. }
  543. }
  544. [cell.reloadTaskButton addTarget:self action:@selector(reloadTaskButton:withEvent:) forControlEvents:UIControlEventTouchUpInside];
  545. [cell.cancelTaskButton addTarget:self action:@selector(cancelTaskButton:withEvent:) forControlEvents:UIControlEventTouchUpInside];
  546. [cell.stopTaskButton addTarget:self action:@selector(stopTaskButton:withEvent:) forControlEvents:UIControlEventTouchUpInside];
  547. return cell;
  548. }
  549. @end