CCManageAutoUpload.m 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. //
  2. // CCManageAutoUpload.m
  3. // Nextcloud iOS
  4. //
  5. // Created by Marino Faggiana on 01/09/15.
  6. // Copyright (c) 2017 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 "CCManageAutoUpload.h"
  24. #import "NCAutoUpload.h"
  25. #import "AppDelegate.h"
  26. #import "NCBridgeSwift.h"
  27. @implementation CCManageAutoUpload
  28. // From Settings
  29. - (id)initWithCoder:(NSCoder *)aDecoder
  30. {
  31. if (self = [super initWithCoder:aDecoder]) {
  32. [self initializeForm];
  33. }
  34. return self;
  35. }
  36. // From Photos
  37. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  38. {
  39. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  40. if (self) {
  41. UIBarButtonItem *doneButton = [[UIBarButtonItem alloc ] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(done:)];
  42. self.navigationItem.rightBarButtonItem = doneButton;
  43. [self initializeForm];
  44. }
  45. return self;
  46. }
  47. - (void)initializeForm
  48. {
  49. XLFormDescriptor *form ;
  50. XLFormSectionDescriptor *section;
  51. XLFormRowDescriptor *row;
  52. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountActive];
  53. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeTheming) name:@"changeTheming" object:nil];
  54. form = [XLFormDescriptor formDescriptorWithTitle:NSLocalizedString(@"_settings_autoupload_", nil)];
  55. // Auto Upload
  56. section = [XLFormSectionDescriptor formSection];
  57. [form addFormSection:section];
  58. section.footerTitle = NSLocalizedString(@"_autoupload_description_", nil);
  59. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"autoUpload" rowType:XLFormRowDescriptorTypeBooleanSwitch title:NSLocalizedString(@"_autoupload_", nil)];
  60. if (tableAccount.autoUpload) row.value = @1;
  61. else row.value = @0;
  62. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  63. [section addFormRow:row];
  64. // Auto Upload Photo
  65. section = [XLFormSectionDescriptor formSection];
  66. [form addFormSection:section];
  67. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"autoUploadImage" rowType:XLFormRowDescriptorTypeBooleanSwitch title:NSLocalizedString(@"_autoupload_photos_", nil)];
  68. row.hidden = [NSString stringWithFormat:@"$%@==0", @"autoUpload"];
  69. if (tableAccount.autoUploadImage) row.value = @1;
  70. else row.value = @0;
  71. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  72. [section addFormRow:row];
  73. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"autoUploadWWAnPhoto" rowType:XLFormRowDescriptorTypeBooleanSwitch title:NSLocalizedString(@"_wifi_only_", nil)];
  74. row.hidden = [NSString stringWithFormat:@"$%@==0", @"autoUpload"];
  75. if (tableAccount.autoUploadWWAnPhoto) row.value = @1;
  76. else row.value = @0;
  77. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  78. [section addFormRow:row];
  79. // Auto Upload Video
  80. section = [XLFormSectionDescriptor formSection];
  81. [form addFormSection:section];
  82. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"autoUploadVideo" rowType:XLFormRowDescriptorTypeBooleanSwitch title:NSLocalizedString(@"_autoupload_videos_", nil)];
  83. row.hidden = [NSString stringWithFormat:@"$%@==0", @"autoUpload"];
  84. if (tableAccount.autoUploadVideo) row.value = @1;
  85. else row.value = @0;
  86. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  87. [section addFormRow:row];
  88. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"autoUploadWWAnVideo" rowType:XLFormRowDescriptorTypeBooleanSwitch title:NSLocalizedString(@"_wifi_only_", nil)];
  89. row.hidden = [NSString stringWithFormat:@"$%@==0", @"autoUpload"];
  90. if (tableAccount.autoUploadWWAnVideo) row.value = @1;
  91. else row.value = @0;
  92. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  93. [section addFormRow:row];
  94. // Auto Upload Background
  95. section = [XLFormSectionDescriptor formSection];
  96. [form addFormSection:section];
  97. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"autoUploadBackground" rowType:XLFormRowDescriptorTypeBooleanSwitch title:NSLocalizedString(@"_autoupload_background_", nil)];
  98. row.hidden = [NSString stringWithFormat:@"$%@==0", @"autoUpload"];
  99. if (tableAccount.autoUploadBackground) row.value = @1;
  100. else row.value = @0;
  101. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  102. [section addFormRow:row];
  103. // Auto Upload Full
  104. section = [XLFormSectionDescriptor formSection];
  105. [form addFormSection:section];
  106. NSString *title = NSLocalizedString(@"_autoupload_fullphotos_", nil);
  107. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"autoUploadFull" rowType:XLFormRowDescriptorTypeBooleanSwitch title:title];
  108. row.hidden = [NSString stringWithFormat:@"$%@==0", @"autoUpload"];
  109. row.value = 0;
  110. if (tableAccount.autoUploadFull) row.value = @1;
  111. else row.value = @0;
  112. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  113. [section addFormRow:row];
  114. // Auto Upload create subfolder
  115. section = [XLFormSectionDescriptor formSection];
  116. [form addFormSection:section];
  117. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"autoUploadCreateSubfolder" rowType:XLFormRowDescriptorTypeBooleanSwitch title:NSLocalizedString(@"_autoupload_create_subfolder_", nil)];
  118. row.hidden = [NSString stringWithFormat:@"$%@==0", @"autoUpload"];
  119. if (tableAccount.autoUploadCreateSubfolder) row.value = @1;
  120. else row.value = @0;
  121. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  122. [section addFormRow:row];
  123. // Auto Upload Format Compatibility
  124. section = [XLFormSectionDescriptor formSection];
  125. [form addFormSection:section];
  126. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"autoUploadFormatCompatibility" rowType:XLFormRowDescriptorTypeBooleanSwitch title:NSLocalizedString(@"_autoupload_format_compatibility_", nil)];
  127. row.hidden = [NSString stringWithFormat:@"$%@==0", @"autoUpload"];
  128. if (tableAccount.autoUploadCreateSubfolder) row.value = @1;
  129. else row.value = @0;
  130. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  131. [section addFormRow:row];
  132. // Auto Upload file name
  133. section = [XLFormSectionDescriptor formSection];
  134. [form addFormSection:section];
  135. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"autoUploadFileName" rowType:XLFormRowDescriptorTypeButton title:NSLocalizedString(@"_autoupload_filenamemask_", nil)];
  136. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  137. row.action.viewControllerClass = [NCManageAutoUploadFileName class];
  138. [section addFormRow:row];
  139. // end
  140. section = [XLFormSectionDescriptor formSection];
  141. [form addFormSection:section];
  142. self.form = form;
  143. }
  144. // Apparirà
  145. - (void)viewWillAppear:(BOOL)animated
  146. {
  147. [super viewWillAppear:animated];
  148. self.tableView.backgroundColor = [NCBrandColor sharedInstance].tableBackground;
  149. self.tableView.showsVerticalScrollIndicator = NO;
  150. // Color
  151. [app aspectNavigationControllerBar:self.navigationController.navigationBar online:[app.reachability isReachable] hidden:NO];
  152. [app aspectTabBar:self.tabBarController.tabBar hidden:NO];
  153. // Request permission for camera roll access
  154. [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
  155. switch (status) {
  156. case PHAuthorizationStatusRestricted:
  157. NSLog(@"[LOG] user can't grant access to camera roll");
  158. break;
  159. case PHAuthorizationStatusDenied:
  160. NSLog(@"[LOG] user denied access to camera roll");
  161. break;
  162. default:
  163. break;
  164. }
  165. }];
  166. [self reloadForm];
  167. }
  168. - (void)changeTheming
  169. {
  170. if (self.isViewLoaded && self.view.window)
  171. [app changeTheming:self];
  172. }
  173. -(void)formRowDescriptorValueHasChanged:(XLFormRowDescriptor *)rowDescriptor oldValue:(id)oldValue newValue:(id)newValue
  174. {
  175. [super formRowDescriptorValueHasChanged:rowDescriptor oldValue:oldValue newValue:newValue];
  176. tableAccount *account = [[NCManageDatabase sharedInstance] getAccountActive];
  177. if ([rowDescriptor.tag isEqualToString:@"autoUpload"]) {
  178. if ([[rowDescriptor.value valueData] boolValue] == YES) {
  179. [[NCManageDatabase sharedInstance] setAccountAutoUploadProperty:@"autoUpload" state:YES];
  180. // Default
  181. [[NCManageDatabase sharedInstance] setAccountAutoUploadFileName:nil];
  182. [[NCManageDatabase sharedInstance] setAccountAutoUploadDirectory:nil activeUrl:app.activeUrl];
  183. // verifichiamo che almeno uno dei servizi (foto video) siano attivi, in caso contrario attiviamo le foto
  184. if (account.autoUploadImage == NO && account.autoUploadVideo == NO) {
  185. [[NCManageDatabase sharedInstance] setAccountAutoUploadProperty:@"autoUploadImage" state:YES];
  186. [[NCManageDatabase sharedInstance] setAccountAutoUploadProperty:@"autoUploadVideo" state:YES];
  187. }
  188. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
  189. [[NCAutoUpload sharedInstance] alignPhotoLibrary];
  190. });
  191. } else {
  192. [[NCManageDatabase sharedInstance] setAccountAutoUploadProperty:@"autoUpload" state:NO];
  193. [[NCManageDatabase sharedInstance] setAccountAutoUploadProperty:@"autoUploadFull" state:NO];
  194. // remove
  195. [[NCManageDatabase sharedInstance] clearTable:[tableQueueUpload class] account:app.activeAccount];
  196. }
  197. [self reloadForm];
  198. }
  199. if ([rowDescriptor.tag isEqualToString:@"autoUploadBackground"]) {
  200. if ([[rowDescriptor.value valueData] boolValue] == YES) {
  201. BOOL isLocationIsEnabled = NO;
  202. [[NCAutoUpload sharedInstance] checkIfLocationIsEnabled];
  203. if(isLocationIsEnabled == YES) {
  204. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_autoupload_background_title_", nil) message:NSLocalizedString(@"_autoupload_background_msg_", nil) preferredStyle:UIAlertControllerStyleAlert];
  205. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"_ok_", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {}];
  206. [alertController addAction:okAction];
  207. [self presentViewController:alertController animated:YES completion:nil];
  208. [[NCManageDatabase sharedInstance] setAccountAutoUploadProperty:@"autoUploadBackground" state:YES];
  209. } else {
  210. [self reloadForm];
  211. }
  212. } else {
  213. [[NCManageDatabase sharedInstance] setAccountAutoUploadProperty:@"autoUploadBackground" state:NO];
  214. [[CCManageLocation sharedInstance] stopSignificantChangeUpdates];
  215. }
  216. }
  217. if ([rowDescriptor.tag isEqualToString:@"autoUploadFull"]) {
  218. if ([[rowDescriptor.value valueData] boolValue] == YES) {
  219. [[NCAutoUpload sharedInstance] setupAutoUploadFull];
  220. [[NCManageDatabase sharedInstance] setAccountAutoUploadProperty:@"autoUploadFull" state:YES];
  221. } else {
  222. [[NCManageDatabase sharedInstance] clearTable:[tableQueueUpload class] account:app.activeAccount];
  223. [[CCNetworking sharedNetworking] settingSessionsDownload:NO upload:YES taskStatus:k_taskStatusCancel activeAccount:app.activeAccount activeUser:app.activeUser activeUrl:app.activeUrl];
  224. [[NCManageDatabase sharedInstance] setAccountAutoUploadProperty:@"autoUploadFull" state:NO];
  225. }
  226. }
  227. if ([rowDescriptor.tag isEqualToString:@"autoUploadImage"]) {
  228. [[NCManageDatabase sharedInstance] setAccountAutoUploadProperty:@"autoUploadImage" state:[[rowDescriptor.value valueData] boolValue]];
  229. if ([[rowDescriptor.value valueData] boolValue] == YES) {
  230. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
  231. [[NCAutoUpload sharedInstance] alignPhotoLibrary];
  232. });
  233. }
  234. }
  235. if ([rowDescriptor.tag isEqualToString:@"autoUploadWWAnPhoto"]) {
  236. [[NCManageDatabase sharedInstance] setAccountAutoUploadProperty:@"autoUploadWWAnPhoto" state:[[rowDescriptor.value valueData] boolValue]];
  237. }
  238. if ([rowDescriptor.tag isEqualToString:@"autoUploadVideo"]) {
  239. [[NCManageDatabase sharedInstance] setAccountAutoUploadProperty:@"autoUploadVideo" state:[[rowDescriptor.value valueData] boolValue]];
  240. if ([[rowDescriptor.value valueData] boolValue] == YES){
  241. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
  242. [[NCAutoUpload sharedInstance] alignPhotoLibrary];
  243. });
  244. }
  245. }
  246. if ([rowDescriptor.tag isEqualToString:@"autoUploadWWAnVideo"]) {
  247. [[NCManageDatabase sharedInstance] setAccountAutoUploadProperty:@"autoUploadWWAnVideo" state:[[rowDescriptor.value valueData] boolValue]];
  248. }
  249. if ([rowDescriptor.tag isEqualToString:@"autoUploadFormatCompatibility"]) {
  250. [[NCManageDatabase sharedInstance] setAccountAutoUploadProperty:@"autoUploadFormatCompatibility" state:[[rowDescriptor.value valueData] boolValue]];
  251. }
  252. if ([rowDescriptor.tag isEqualToString:@"autoUploadCreateSubfolder"]) {
  253. [[NCManageDatabase sharedInstance] setAccountAutoUploadProperty:@"autoUploadCreateSubfolder" state:[[rowDescriptor.value valueData] boolValue]];
  254. }
  255. }
  256. - (void)done:(XLFormRowDescriptor *)sender
  257. {
  258. [self dismissViewControllerAnimated:YES completion:nil];
  259. }
  260. - (void)reloadForm
  261. {
  262. self.form.delegate = nil;
  263. XLFormRowDescriptor *rowAutoUpload = [self.form formRowWithTag:@"autoUpload"];
  264. XLFormRowDescriptor *rowAutoUploadImage = [self.form formRowWithTag:@"autoUploadImage"];
  265. XLFormRowDescriptor *rowAutoUploadWWAnPhoto = [self.form formRowWithTag:@"autoUploadWWAnPhoto"];
  266. XLFormRowDescriptor *rowAutoUploadVideo = [self.form formRowWithTag:@"autoUploadVideo"];
  267. XLFormRowDescriptor *rowAutoUploadWWAnVideo = [self.form formRowWithTag:@"autoUploadWWAnVideo"];
  268. XLFormRowDescriptor *rowAutoUploadBackground = [self.form formRowWithTag:@"autoUploadBackground"];
  269. XLFormRowDescriptor *rowAutoUploadFull = [self.form formRowWithTag:@"autoUploadFull"];
  270. XLFormRowDescriptor *rowAutoUploadCreateSubfolder = [self.form formRowWithTag:@"autoUploadCreateSubfolder"];
  271. XLFormRowDescriptor *rowAutoUploadFormatCompatibility = [self.form formRowWithTag:@"autoUploadFormatCompatibility"];
  272. XLFormRowDescriptor *rowAutoUploadFileName = [self.form formRowWithTag:@"autoUploadFileName"];
  273. // - STATUS ---------------------
  274. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountActive];
  275. if (tableAccount.autoUpload)
  276. [rowAutoUpload setValue:@1]; else [rowAutoUpload setValue:@0];
  277. if (tableAccount.autoUploadImage)
  278. [rowAutoUploadImage setValue:@1]; else [rowAutoUploadImage setValue:@0];
  279. if (tableAccount.autoUploadWWAnPhoto)
  280. [rowAutoUploadWWAnPhoto setValue:@1]; else [rowAutoUploadWWAnPhoto setValue:@0];
  281. if (tableAccount.autoUploadVideo)
  282. [rowAutoUploadVideo setValue:@1]; else [rowAutoUploadVideo setValue:@0];
  283. if (tableAccount.autoUploadWWAnVideo)
  284. [rowAutoUploadWWAnVideo setValue:@1]; else [rowAutoUploadWWAnVideo setValue:@0];
  285. if (tableAccount.autoUploadBackground)
  286. [rowAutoUploadBackground setValue:@1]; else [rowAutoUploadBackground setValue:@0];
  287. if (tableAccount.autoUploadFull)
  288. [rowAutoUploadFull setValue:@1]; else [rowAutoUploadFull setValue:@0];
  289. if (tableAccount.autoUploadFormatCompatibility)
  290. [rowAutoUploadFormatCompatibility setValue:@1]; else [rowAutoUploadFormatCompatibility setValue:@0];
  291. if (tableAccount.autoUploadCreateSubfolder)
  292. [rowAutoUploadCreateSubfolder setValue:@1]; else [rowAutoUploadCreateSubfolder setValue:@0];
  293. // - HIDDEN --------------------------------------------------------------------------
  294. rowAutoUploadImage.hidden = [NSString stringWithFormat:@"$%@==0", @"autoUpload"];
  295. rowAutoUploadWWAnPhoto.hidden = [NSString stringWithFormat:@"$%@==0", @"autoUpload"];
  296. rowAutoUploadVideo.hidden = [NSString stringWithFormat:@"$%@==0", @"autoUpload"];
  297. rowAutoUploadWWAnVideo.hidden = [NSString stringWithFormat:@"$%@==0", @"autoUpload"];
  298. rowAutoUploadBackground.hidden = [NSString stringWithFormat:@"$%@==0", @"autoUpload"];
  299. rowAutoUploadFull.hidden = [NSString stringWithFormat:@"$%@==0", @"autoUpload"];
  300. rowAutoUploadCreateSubfolder.hidden = [NSString stringWithFormat:@"$%@==0", @"autoUpload"];
  301. rowAutoUploadFormatCompatibility.hidden = [NSString stringWithFormat:@"$%@==0", @"autoUpload"];
  302. rowAutoUploadFileName.hidden = [NSString stringWithFormat:@"$%@==0", @"autoUpload"];
  303. // -----------------------------------------------------------------------------------
  304. [self.tableView reloadData];
  305. self.form.delegate = self;
  306. }
  307. - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
  308. {
  309. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountActive];
  310. NSString *sectionName;
  311. switch (section)
  312. {
  313. case 0:
  314. sectionName = NSLocalizedString(@"_autoupload_description_", nil);
  315. break;
  316. case 3:
  317. if (tableAccount.autoUpload) sectionName = NSLocalizedString(@"_autoupload_description_background_", nil);
  318. else sectionName = @"";
  319. break;
  320. case 4:
  321. if (tableAccount.autoUpload) sectionName = NSLocalizedString(@"_autoupload_fullphotos_footer_", nil);
  322. else sectionName = @"";
  323. break;
  324. case 5:
  325. if (tableAccount.autoUpload) sectionName = NSLocalizedString(@"_autoupload_create_subfolder_footer_", nil);
  326. else sectionName = @"";
  327. break;
  328. case 6:
  329. if (tableAccount.autoUpload) sectionName = NSLocalizedString(@"_autoupload_format_compatibility_footer_", nil);
  330. else sectionName = @"";
  331. break;
  332. case 7:
  333. if (tableAccount.autoUpload) sectionName = NSLocalizedString(@"_autoupload_filenamemask_footer_", nil);
  334. else sectionName = @"";
  335. break;
  336. }
  337. return sectionName;
  338. }
  339. @end