CCTransfers.m 31 KB

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