CCUtility.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. //
  2. // CCUtility.m
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 02/02/16.
  6. // Copyright (c) 2016 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  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 "CCUtility.h"
  24. #import "NCBridgeSwift.h"
  25. #import "NSNotificationCenter+MainThread.h"
  26. #import <OpenSSL/OpenSSL.h>
  27. #import <CoreLocation/CoreLocation.h>
  28. #include <sys/stat.h>
  29. @implementation CCUtility
  30. #pragma --------------------------------------------------------------------------------------------
  31. #pragma mark ===== Various =====
  32. #pragma --------------------------------------------------------------------------------------------
  33. + (NSString *)dateDiff:(NSDate *) convertedDate
  34. {
  35. NSDate *todayDate = [NSDate date];
  36. double ti = [convertedDate timeIntervalSinceDate:todayDate];
  37. ti = ti * -1;
  38. if (ti < 60) {
  39. return NSLocalizedString(@"_less_a_minute_", nil);
  40. } else if (ti < 3600) {
  41. int diff = round(ti / 60);
  42. if (diff == 1) {
  43. return NSLocalizedString(@"_a_minute_ago_", nil);
  44. } else {
  45. return [NSString stringWithFormat:NSLocalizedString(@"_minutes_ago_", nil), diff];
  46. }
  47. } else if (ti < 86400) {
  48. int diff = round(ti / 60 / 60);
  49. if (diff == 1) {
  50. return NSLocalizedString(@"_an_hour_ago_", nil);
  51. } else {
  52. return[NSString stringWithFormat:NSLocalizedString(@"_hours_ago_", nil), diff];
  53. }
  54. } else if (ti < 86400 * 30) {
  55. int diff = round(ti / 60 / 60 / 24);
  56. if (diff == 1) {
  57. return NSLocalizedString(@"_a_day_ago_", nil);
  58. } else {
  59. return[NSString stringWithFormat:NSLocalizedString(@"_days_ago_", nil), diff];
  60. }
  61. } else {
  62. // Older than one month
  63. NSDateFormatter *df = [[NSDateFormatter alloc] init];
  64. [df setFormatterBehavior:NSDateFormatterBehavior10_4];
  65. [df setDateStyle:NSDateFormatterMediumStyle];
  66. return [df stringFromDate:convertedDate];
  67. }
  68. }
  69. + (NSString *)createFileName:(NSString *)fileName fileDate:(NSDate *)fileDate fileType:(PHAssetMediaType)fileType keyFileName:(NSString *)keyFileName keyFileNameType:(NSString *)keyFileNameType keyFileNameOriginal:(NSString *)keyFileNameOriginal forcedNewFileName:(BOOL)forcedNewFileName
  70. {
  71. BOOL addFileNameType = NO;
  72. // Original FileName ?
  73. if ([[[NCKeychain alloc] init] getOriginalFileNameWithKey:keyFileNameOriginal] && !forcedNewFileName) {
  74. return fileName;
  75. }
  76. NSString *numberFileName;
  77. if ([fileName length] > 8) numberFileName = [fileName substringWithRange:NSMakeRange(04, 04)];
  78. else numberFileName = [[NCKeychain alloc] init].incrementalNumber;
  79. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  80. [formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
  81. [formatter setDateFormat:@"yy-MM-dd HH-mm-ss"];
  82. NSString *fileNameDate = [formatter stringFromDate:fileDate];
  83. NSString *fileNameType = @"";
  84. if (fileType == PHAssetMediaTypeImage)
  85. fileNameType = NSLocalizedString(@"_photo_", nil);
  86. if (fileType == PHAssetMediaTypeVideo)
  87. fileNameType = NSLocalizedString(@"_video_", nil);
  88. if (fileType == PHAssetMediaTypeAudio)
  89. fileNameType = NSLocalizedString(@"_audio_", nil);
  90. if (fileType == PHAssetMediaTypeUnknown)
  91. fileNameType = NSLocalizedString(@"_unknown_", nil);
  92. // Use File Name Type
  93. if (keyFileNameType)
  94. addFileNameType = [[[NCKeychain alloc] init] getFileNameTypeWithKey:keyFileNameType];
  95. NSString *fileNameExt = [[fileName pathExtension] lowercaseString];
  96. if (keyFileName) {
  97. fileName = [[[NCKeychain alloc] init] getFileNameMaskWithKey:keyFileName];
  98. if ([fileName length] > 0) {
  99. [formatter setDateFormat:@"dd"];
  100. NSString *dayNumber = [formatter stringFromDate:fileDate];
  101. [formatter setDateFormat:@"MMM"];
  102. NSString *month = [formatter stringFromDate:fileDate];
  103. [formatter setDateFormat:@"MM"];
  104. NSString *monthNumber = [formatter stringFromDate:fileDate];
  105. [formatter setDateFormat:@"yyyy"];
  106. NSString *year = [formatter stringFromDate:fileDate];
  107. [formatter setDateFormat:@"yy"];
  108. NSString *yearNumber = [formatter stringFromDate:fileDate];
  109. [formatter setDateFormat:@"HH"];
  110. NSString *hour24 = [formatter stringFromDate:fileDate];
  111. [formatter setDateFormat:@"hh"];
  112. NSString *hour12 = [formatter stringFromDate:fileDate];
  113. [formatter setDateFormat:@"mm"];
  114. NSString *minute = [formatter stringFromDate:fileDate];
  115. [formatter setDateFormat:@"ss"];
  116. NSString *second = [formatter stringFromDate:fileDate];
  117. [formatter setDateFormat:@"a"];
  118. NSString *ampm = [formatter stringFromDate:fileDate];
  119. // Replace string with date
  120. fileName = [fileName stringByReplacingOccurrencesOfString:@"DD" withString:dayNumber];
  121. fileName = [fileName stringByReplacingOccurrencesOfString:@"MMM" withString:month];
  122. fileName = [fileName stringByReplacingOccurrencesOfString:@"MM" withString:monthNumber];
  123. fileName = [fileName stringByReplacingOccurrencesOfString:@"YYYY" withString:year];
  124. fileName = [fileName stringByReplacingOccurrencesOfString:@"YY" withString:yearNumber];
  125. fileName = [fileName stringByReplacingOccurrencesOfString:@"HH" withString:hour24];
  126. fileName = [fileName stringByReplacingOccurrencesOfString:@"hh" withString:hour12];
  127. fileName = [fileName stringByReplacingOccurrencesOfString:@"mm" withString:minute];
  128. fileName = [fileName stringByReplacingOccurrencesOfString:@"ss" withString:second];
  129. fileName = [fileName stringByReplacingOccurrencesOfString:@"ampm" withString:ampm];
  130. if (addFileNameType)
  131. fileName = [NSString stringWithFormat:@"%@%@%@.%@", fileNameType, fileName, numberFileName, fileNameExt];
  132. else
  133. fileName = [NSString stringWithFormat:@"%@%@.%@", fileName, numberFileName, fileNameExt];
  134. fileName = [fileName stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
  135. } else {
  136. if (addFileNameType)
  137. fileName = [NSString stringWithFormat:@"%@ %@ %@.%@", fileNameType, fileNameDate, numberFileName, fileNameExt];
  138. else
  139. fileName = [NSString stringWithFormat:@"%@ %@.%@", fileNameDate, numberFileName, fileNameExt];
  140. }
  141. } else {
  142. if (addFileNameType)
  143. fileName = [NSString stringWithFormat:@"%@ %@ %@.%@", fileNameType, fileNameDate, numberFileName, fileNameExt];
  144. else
  145. fileName = [NSString stringWithFormat:@"%@ %@.%@", fileNameDate, numberFileName, fileNameExt];
  146. }
  147. return fileName;
  148. }
  149. + (NSString *)getTitleSectionDate:(NSDate *)date
  150. {
  151. NSString *title;
  152. NSDate *today = [NSDate date];
  153. NSDate *yesterday = [today dateByAddingTimeInterval: -86400.0];
  154. if ([date isEqualToDate:[CCUtility datetimeWithOutTime:[NSDate distantPast]]]) {
  155. title = NSLocalizedString(@"_no_date_", nil);
  156. } else {
  157. title = [NSDateFormatter localizedStringFromDate:date dateStyle:NSDateFormatterLongStyle timeStyle:0];
  158. if ([date isEqualToDate:[CCUtility datetimeWithOutTime:today]])
  159. title = [NSString stringWithFormat:NSLocalizedString(@"_today_", nil)];
  160. if ([date isEqualToDate:[CCUtility datetimeWithOutTime:yesterday]])
  161. title = [NSString stringWithFormat:NSLocalizedString(@"_yesterday_", nil)];
  162. }
  163. return title;
  164. }
  165. + (NSString *)getMimeType:(NSString *)fileNameView
  166. {
  167. CFStringRef fileUTI = nil;
  168. NSString *returnFileUTI = nil;
  169. if ([fileNameView isEqualToString:@"."]) {
  170. return returnFileUTI;
  171. } else {
  172. CFStringRef fileExtension = (__bridge CFStringRef)[fileNameView pathExtension];
  173. NSString *ext = (__bridge NSString *)fileExtension;
  174. ext = ext.uppercaseString;
  175. fileUTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, fileExtension, NULL);
  176. if (fileUTI != nil) {
  177. returnFileUTI = (__bridge NSString *)fileUTI;
  178. CFRelease(fileUTI);
  179. }
  180. }
  181. return returnFileUTI;
  182. }
  183. #pragma --------------------------------------------------------------------------------------------
  184. #pragma mark ===== Share Permissions =====
  185. #pragma --------------------------------------------------------------------------------------------
  186. + (NSInteger) getPermissionsValueByCanEdit:(BOOL)canEdit andCanCreate:(BOOL)canCreate andCanChange:(BOOL)canChange andCanDelete:(BOOL)canDelete andCanShare:(BOOL)canShare andIsFolder:(BOOL) isFolder
  187. {
  188. NSInteger permissionsValue = NCGlobal.shared.permissionReadShare;
  189. if (canEdit && !isFolder) {
  190. permissionsValue = permissionsValue + NCGlobal.shared.permissionUpdateShare;
  191. }
  192. if (canCreate & isFolder) {
  193. permissionsValue = permissionsValue + NCGlobal.shared.permissionCreateShare;
  194. }
  195. if (canChange && isFolder) {
  196. permissionsValue = permissionsValue + NCGlobal.shared.permissionUpdateShare;
  197. }
  198. if (canDelete & isFolder) {
  199. permissionsValue = permissionsValue + NCGlobal.shared.permissionDeleteShare;
  200. }
  201. if (canShare) {
  202. permissionsValue = permissionsValue + NCGlobal.shared.permissionShareShare;
  203. }
  204. return permissionsValue;
  205. }
  206. + (BOOL) isPermissionToCanCreate:(NSInteger) permissionValue {
  207. BOOL canCreate = ((permissionValue & NCGlobal.shared.permissionCreateShare) > 0);
  208. return canCreate;
  209. }
  210. + (BOOL) isPermissionToCanChange:(NSInteger) permissionValue {
  211. BOOL canChange = ((permissionValue & NCGlobal.shared.permissionUpdateShare) > 0);
  212. return canChange;
  213. }
  214. + (BOOL) isPermissionToCanDelete:(NSInteger) permissionValue {
  215. BOOL canDelete = ((permissionValue & NCGlobal.shared.permissionDeleteShare) > 0);
  216. return canDelete;
  217. }
  218. + (BOOL) isPermissionToCanShare:(NSInteger) permissionValue {
  219. BOOL canShare = ((permissionValue & NCGlobal.shared.permissionShareShare) > 0);
  220. return canShare;
  221. }
  222. + (BOOL) isAnyPermissionToEdit:(NSInteger) permissionValue {
  223. BOOL canCreate = [self isPermissionToCanCreate:permissionValue];
  224. BOOL canChange = [self isPermissionToCanChange:permissionValue];
  225. BOOL canDelete = [self isPermissionToCanDelete:permissionValue];
  226. BOOL canEdit = (canCreate || canChange || canDelete);
  227. return canEdit;
  228. }
  229. + (BOOL) isPermissionToRead:(NSInteger) permissionValue {
  230. BOOL canRead = ((permissionValue & NCGlobal.shared.permissionReadShare) > 0);
  231. return canRead;
  232. }
  233. + (BOOL) isPermissionToReadCreateUpdate:(NSInteger) permissionValue {
  234. BOOL canRead = [self isPermissionToRead:permissionValue];
  235. BOOL canCreate = [self isPermissionToCanCreate:permissionValue];
  236. BOOL canChange = [self isPermissionToCanChange:permissionValue];
  237. BOOL canEdit = (canCreate && canChange && canRead);
  238. return canEdit;
  239. }
  240. #pragma --------------------------------------------------------------------------------------------
  241. #pragma mark ===== Third parts =====
  242. #pragma --------------------------------------------------------------------------------------------
  243. + (NSDate *)datetimeWithOutTime:(NSDate *)datDate
  244. {
  245. if (datDate == nil) return nil;
  246. NSDateComponents* comps = [[NSCalendar currentCalendar] components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:datDate];
  247. datDate = [[NSCalendar currentCalendar] dateFromComponents:comps];
  248. return datDate;
  249. }
  250. + (NSDate *)getATime:(const char *)path
  251. {
  252. struct stat st;
  253. stat(path, &st);
  254. time_t accessed = st.st_atime;
  255. NSDate *date = [NSDate dateWithTimeIntervalSince1970:accessed];
  256. return date;
  257. }
  258. @end