CCTransfers.m 31 KB

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