CCManageAutoUpload.m 18 KB

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