NCGlobal.swift 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. //
  2. // NCGlobal.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 22/02/21.
  6. // Copyright © 2021 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 UIKit
  24. class NCGlobal: NSObject {
  25. @objc static let shared: NCGlobal = {
  26. let instance = NCGlobal()
  27. return instance
  28. }()
  29. func usernameToColor(_ username: String) -> CGColor {
  30. // Normalize hash
  31. let lowerUsername = username.lowercased()
  32. var hash: String
  33. let regex = try! NSRegularExpression(pattern: "^([0-9a-f]{4}-?){8}$")
  34. let matches = regex.matches(
  35. in: username,
  36. range: NSRange(username.startIndex..., in: username))
  37. if !matches.isEmpty {
  38. // Already a md5 hash?
  39. // done, use as is.
  40. hash = lowerUsername
  41. } else {
  42. hash = lowerUsername.md5()
  43. }
  44. hash = hash.replacingOccurrences(of: "[^0-9a-f]", with: "", options: .regularExpression)
  45. // userColors has 18 colors by default
  46. let userColorIx = NCGlobal.hashToInt(hash: hash, maximum: 18)
  47. return NCBrandColor.shared.userColors[userColorIx]
  48. }
  49. // Convert a string to an integer evenly
  50. // hash is hex string
  51. static func hashToInt(hash: String, maximum: Int) -> Int {
  52. let result = hash.compactMap(\.hexDigitValue)
  53. return result.reduce(0, { $0 + $1 }) % maximum
  54. }
  55. // ENUM
  56. //
  57. public enum TypeFilterScanDocument: String {
  58. case document = "document"
  59. case original = "original"
  60. }
  61. // Directory on Group
  62. //
  63. @objc let directoryProviderStorage = "File Provider Storage"
  64. @objc let appApplicationSupport = "Library/Application Support"
  65. @objc let appCertificates = "Library/Application Support/Certificates"
  66. @objc let appDatabaseNextcloud = "Library/Application Support/Nextcloud"
  67. @objc let appScan = "Library/Application Support/Scan"
  68. @objc let appUserData = "Library/Application Support/UserData"
  69. // Service
  70. //
  71. let metadataKeyedUnarchiver = "it.twsweb.nextcloud.metadata"
  72. let refreshTask = "com.nextcloud.refreshTask"
  73. let processingTask = "com.nextcloud.processingTask"
  74. // App
  75. //
  76. let appName = "files"
  77. let appScheme = "nextcloud"
  78. let talkName = "talk-message"
  79. let spreedName = "spreed"
  80. let twoFactorNotificatioName = "twofactor_nextcloud_notification"
  81. // Nextcloud version
  82. //
  83. let nextcloudVersion12: Int = 12
  84. let nextcloudVersion15: Int = 15
  85. let nextcloudVersion17: Int = 17
  86. let nextcloudVersion18: Int = 18
  87. let nextcloudVersion20: Int = 20
  88. let nextcloudVersion23: Int = 23
  89. let nextcloudVersion24: Int = 24
  90. let nextcloudVersion25: Int = 25
  91. let nextcloudVersion26: Int = 26
  92. let nextcloudVersion27: Int = 27
  93. let nextcloudVersion28: Int = 28
  94. // Nextcloud unsupported
  95. //
  96. let nextcloud_unsupported_version: Int = 16
  97. // Intro selector
  98. //
  99. @objc let introLogin: Int = 0
  100. let introSignup: Int = 1
  101. // Varie size GUI
  102. //
  103. @objc let heightCellSettings: CGFloat = 50
  104. // Avatar & Preview size
  105. //
  106. let avatarSize: Int = 128 * Int(UIScreen.main.scale)
  107. let avatarSizeRounded: Int = 128
  108. let sizePreview: Int = 1024
  109. let sizeIcon: Int = 512
  110. // E2EE
  111. //
  112. let e2eePassphraseTest = "more over television factory tendency independence international intellectual impress interest sentence pony"
  113. @objc let e2eeVersions = ["1.1", "1.2", "2.0"]
  114. let e2eeVersionV11 = "1.1"
  115. let e2eeVersionV12 = "1.2"
  116. let e2eeVersionV20 = "2.0"
  117. // CHUNK
  118. let chunkSizeMBCellular = 10000000
  119. let chunkSizeMBEthernetOrWiFi = 100000000
  120. // Video
  121. //
  122. let maxHTTPCache: Int64 = 10000000000 // 10 GB
  123. let fileNameVideoEncoded: String = "video_encoded.mp4"
  124. // NCViewerProviderContextMenu
  125. //
  126. let maxAutoDownload: UInt64 = 50000000 // 50MB
  127. let maxAutoDownloadCellular: UInt64 = 10000000 // 10MB
  128. // Layout
  129. //
  130. let layoutList = "typeLayoutList"
  131. let layoutGrid = "typeLayoutGrid"
  132. let layoutViewTrash = "LayoutTrash"
  133. let layoutViewOffline = "LayoutOffline"
  134. let layoutViewFavorite = "LayoutFavorite"
  135. let layoutViewFiles = "LayoutFiles"
  136. let layoutViewTransfers = "LayoutTransfers"
  137. let layoutViewRecent = "LayoutRecent"
  138. let layoutViewShares = "LayoutShares"
  139. let layoutViewShareExtension = "LayoutShareExtension"
  140. let layoutViewGroupfolders = "LayoutGroupfolders"
  141. // Button Type in Cell list/grid
  142. //
  143. let buttonMoreMore = "more"
  144. let buttonMoreStop = "stop"
  145. let buttonMoreLock = "moreLock"
  146. // Standard height sections header/footer
  147. //
  148. let heightButtonsView: CGFloat = 50
  149. let heightHeaderTransfer: CGFloat = 50
  150. let heightSection: CGFloat = 30
  151. let heightFooter: CGFloat = 1
  152. let heightFooterButton: CGFloat = 30
  153. let endHeightFooter: CGFloat = 85
  154. // Text - OnlyOffice - Collabora - QuickLook
  155. //
  156. let editorText = "text"
  157. let editorOnlyoffice = "onlyoffice"
  158. let editorCollabora = "collabora"
  159. let editorQuickLook = "quicklook"
  160. let onlyofficeDocx = "onlyoffice_docx"
  161. let onlyofficeXlsx = "onlyoffice_xlsx"
  162. let onlyofficePptx = "onlyoffice_pptx"
  163. // Template
  164. //
  165. let templateDocument = "document"
  166. let templateSpreadsheet = "spreadsheet"
  167. let templatePresentation = "presentation"
  168. // Rich Workspace
  169. //
  170. let fileNameRichWorkspace = "Readme.md"
  171. // ContentPresenter
  172. //
  173. @objc let dismissAfterSecond: TimeInterval = 4
  174. @objc let dismissAfterSecondLong: TimeInterval = 10
  175. // Error
  176. //
  177. @objc let errorRequestExplicityCancelled: Int = 15
  178. @objc let errorNotModified: Int = 304
  179. @objc let errorBadRequest: Int = 400
  180. @objc let errorUnauthorized401: Int = 401
  181. @objc let errorForbidden: Int = 403
  182. @objc let errorResourceNotFound: Int = 404
  183. @objc let errorMethodNotSupported: Int = 405
  184. @objc let errorConflict: Int = 409
  185. @objc let errorPreconditionFailed: Int = 412
  186. @objc let errorUnsupportedMediaType: Int = 415
  187. @objc let errorInternalServerError: Int = 500
  188. @objc let errorQuota: Int = 507
  189. @objc let errorUnauthorized997: Int = 997
  190. @objc let errorExplicitlyCancelled: Int = -999
  191. @objc let errorConnectionLost: Int = -1005
  192. @objc let errorNetworkNotAvailable: Int = -1009
  193. @objc let errorBadServerResponse: Int = -1011
  194. @objc let errorInternalError: Int = -99999
  195. @objc let errorFileNotSaved: Int = -99998
  196. @objc let errorOffline: Int = -99997
  197. @objc let errorCharactersForbidden: Int = -99996
  198. @objc let errorCreationFile: Int = -99995
  199. @objc let errorReadFile: Int = -99994
  200. @objc let errorUnauthorizedFilesPasscode: Int = -99993
  201. @objc let errorDisableFilesApp: Int = -99992
  202. @objc let errorUnexpectedResponseFromDB: Int = -99991
  203. // E2EE
  204. @objc let errorE2EENotEnabled: Int = -98000
  205. @objc let errorE2EEVersion: Int = -98001
  206. @objc let errorE2EEKeyChecksums: Int = -98002
  207. @objc let errorE2EEKeyEncodeMetadata: Int = -98003
  208. @objc let errorE2EEKeyDecodeMetadata: Int = -98004
  209. @objc let errorE2EEKeyVerifySignature: Int = -98005
  210. @objc let errorE2EEKeyCiphertext: Int = -98006
  211. @objc let errorE2EEKeyFiledropCiphertext: Int = -98007
  212. @objc let errorE2EEJSon: Int = -98008
  213. @objc let errorE2EELock: Int = -98009
  214. @objc let errorE2EEEncryptFile: Int = -98010
  215. @objc let errorE2EEEncryptPayloadFile: Int = -98011
  216. @objc let errorE2EECounter: Int = -98012
  217. @objc let errorE2EEGenerateKey: Int = -98013
  218. @objc let errorE2EEEncodedKey: Int = -98014
  219. @objc let errorE2EENoUserFound: Int = -98015
  220. @objc let errorE2EEUploadInProgress: Int = -98016
  221. // Constants to identify the different permissions of a file
  222. //
  223. @objc let permissionShared = "S"
  224. @objc let permissionCanShare = "R"
  225. @objc let permissionMounted = "M"
  226. @objc let permissionFileCanWrite = "W"
  227. @objc let permissionCanCreateFile = "C"
  228. @objc let permissionCanCreateFolder = "K"
  229. @objc let permissionCanDelete = "D"
  230. @objc let permissionCanRename = "N"
  231. @objc let permissionCanMove = "V"
  232. // Share permission
  233. // permissions - (int) 1 = read; 2 = update; 4 = create; 8 = delete; 16 = share; 31 = all
  234. //
  235. @objc let permissionReadShare: Int = 1
  236. @objc let permissionUpdateShare: Int = 2
  237. @objc let permissionCreateShare: Int = 4
  238. @objc let permissionDeleteShare: Int = 8
  239. @objc let permissionShareShare: Int = 16
  240. @objc let permissionMinFileShare: Int = 1
  241. @objc let permissionMaxFileShare: Int = 19
  242. @objc let permissionMinFolderShare: Int = 1
  243. @objc let permissionMaxFolderShare: Int = 31
  244. @objc let permissionDefaultFileRemoteShareNoSupportShareOption: Int = 3
  245. @objc let permissionDefaultFolderRemoteShareNoSupportShareOption: Int = 15
  246. // ATTRIBUTES
  247. @objc let permissionDownloadShare: Int = 0
  248. // Filename Mask and Type
  249. //
  250. let keyFileNameMask = "fileNameMask"
  251. let keyFileNameType = "fileNameType"
  252. let keyFileNameAutoUploadMask = "fileNameAutoUploadMask"
  253. let keyFileNameAutoUploadType = "fileNameAutoUploadType"
  254. let keyFileNameOriginal = "fileNameOriginal"
  255. let keyFileNameOriginalAutoUpload = "fileNameOriginalAutoUpload"
  256. // Selector
  257. //
  258. let selectorDownloadFile = "downloadFile"
  259. let selectorReadFile = "readFile"
  260. let selectorListingFavorite = "listingFavorite"
  261. let selectorLoadFileView = "loadFileView"
  262. let selectorLoadFileQuickLook = "loadFileQuickLook"
  263. let selectorOpenIn = "openIn"
  264. let selectorPrint = "print"
  265. let selectorUploadAutoUpload = "uploadAutoUpload"
  266. let selectorUploadAutoUploadAll = "uploadAutoUploadAll"
  267. let selectorUploadFile = "uploadFile"
  268. let selectorUploadFileNODelete = "UploadFileNODelete"
  269. let selectorUploadFileShareExtension = "uploadFileShareExtension"
  270. let selectorSaveAlbum = "saveAlbum"
  271. let selectorSaveAsScan = "saveAsScan"
  272. let selectorOpenDetail = "openDetail"
  273. let selectorSynchronizationOffline = "synchronizationOffline"
  274. // Metadata : Status
  275. //
  276. // 1) wait download/upload
  277. // 2) in download/upload
  278. // 3) downloading/uploading
  279. // 4) done or error
  280. //
  281. let metadataStatusNormal: Int = 0
  282. let metadataStatusWaitDownload: Int = -1
  283. let metadataStatusDownloading: Int = -2
  284. let metadataStatusDownloadError: Int = -3
  285. let metadataStatusWaitUpload: Int = 1
  286. let metadataStatusUploading: Int = 2
  287. let metadataStatusUploadError: Int = 3
  288. // Hidden files included in the read
  289. //
  290. let includeHiddenFiles: [String] = [".LivePhoto"]
  291. // Auto upload subfolder granularity
  292. //
  293. @objc let subfolderGranularityDaily = 2
  294. @objc let subfolderGranularityMonthly = 1
  295. @objc let subfolderGranularityYearly = 0
  296. // Notification Center
  297. //
  298. @objc let notificationCenterApplicationDidEnterBackground = "applicationDidEnterBackground"
  299. @objc let notificationCenterApplicationDidBecomeActive = "applicationDidBecomeActive"
  300. @objc let notificationCenterApplicationWillResignActive = "applicationWillResignActive"
  301. @objc let notificationCenterApplicationWillEnterForeground = "applicationWillEnterForeground"
  302. @objc let notificationCenterChangeUser = "changeUser"
  303. @objc let notificationCenterChangeTheming = "changeTheming"
  304. let notificationCenterRichdocumentGrabFocus = "richdocumentGrabFocus"
  305. let notificationCenterReloadDataNCShare = "reloadDataNCShare"
  306. let notificationCenterCloseRichWorkspaceWebView = "closeRichWorkspaceWebView"
  307. let notificationCenterReloadAvatar = "reloadAvatar"
  308. let notificationCenterCreateMediaCacheEnded = "createMediaCacheEnded"
  309. @objc let notificationCenterReloadDataSource = "reloadDataSource"
  310. let notificationCenterReloadDataSourceNetwork = "reloadDataSourceNetwork"
  311. let notificationCenterChangeStatusFolderE2EE = "changeStatusFolderE2EE" // userInfo: serverUrl
  312. let notificationCenterDownloadStartFile = "downloadStartFile" // userInfo: ocId, serverUrl, account
  313. let notificationCenterDownloadedFile = "downloadedFile" // userInfo: ocId, serverUrl, account, selector, error
  314. let notificationCenterDownloadCancelFile = "downloadCancelFile" // userInfo: ocId, serverUrl, account
  315. let notificationCenterUploadStartFile = "uploadStartFile" // userInfo: ocId, serverUrl, account, fileName, sessionSelector
  316. @objc let notificationCenterUploadedFile = "uploadedFile" // userInfo: ocId, serverUrl, account, fileName, ocIdTemp, error
  317. let notificationCenterUploadedLivePhoto = "uploadedLivePhoto" // userInfo: ocId, serverUrl, account, fileName, ocIdTemp, error
  318. let notificationCenterUploadCancelFile = "uploadCancelFile" // userInfo: ocId, serverUrl, account
  319. let notificationCenterProgressTask = "progressTask" // userInfo: account, ocId, serverUrl, status, chunk, e2eEncrypted, progress, totalBytes, totalBytesExpected
  320. let notificationCenterUpdateBadgeNumber = "updateBadgeNumber"
  321. let notificationCenterCreateFolder = "createFolder" // userInfo: ocId, serverUrl, account, withPush
  322. let notificationCenterDeleteFile = "deleteFile" // userInfo: [ocId], onlyLocalCache, error
  323. let notificationCenterMoveFile = "moveFile" // userInfo: [ocId], error
  324. let notificationCenterCopyFile = "copyFile" // userInfo: [ocId], error
  325. let notificationCenterRenameFile = "renameFile" // userInfo: ocId, account, indexPath
  326. let notificationCenterFavoriteFile = "favoriteFile" // userInfo: ocId, serverUrl
  327. let notificationCenterOperationReadFile = "operationReadFile" // userInfo: ocId
  328. let notificationCenterMenuSearchTextPDF = "menuSearchTextPDF"
  329. let notificationCenterMenuGotToPageInPDF = "menuGotToPageInPDF"
  330. let notificationCenterOpenMediaDetail = "openMediaDetail" // userInfo: ocId
  331. let notificationCenterDismissScanDocument = "dismissScanDocument"
  332. let notificationCenterDismissUploadAssets = "dismissUploadAssets"
  333. let notificationCenterEnableSwipeGesture = "enableSwipeGesture"
  334. let notificationCenterDisableSwipeGesture = "disableSwipeGesture"
  335. // TIP
  336. //
  337. let tipNCViewerPDFThumbnail = "tipncviewerpdfthumbnail"
  338. let tipNCCollectionViewCommonAccountRequest = "tipnccollectionviewcommonaccountrequest"
  339. let tipNCScanAddImage = "tipncscanaddimage"
  340. let tipNCViewerMediaDetailView = "tipncviewermediadetailview"
  341. // ACTION
  342. //
  343. let actionNoAction = "no-action"
  344. let actionUploadAsset = "upload-asset"
  345. let actionScanDocument = "add-scan-document"
  346. let actionTextDocument = "create-text-document"
  347. let actionVoiceMemo = "create-voice-memo"
  348. // WIDGET ACTION
  349. //
  350. let widgetActionNoAction = "nextcloud://open-action?action=no-action"
  351. let widgetActionUploadAsset = "nextcloud://open-action?action=upload-asset"
  352. let widgetActionScanDocument = "nextcloud://open-action?action=add-scan-document"
  353. let widgetActionTextDocument = "nextcloud://open-action?action=create-text-document"
  354. let widgetActionVoiceMemo = "nextcloud://open-action?action=create-voice-memo"
  355. // APPCONFIG
  356. //
  357. let configuration_brand = "brand"
  358. let configuration_serverUrl = "serverUrl"
  359. let configuration_username = "username"
  360. let configuration_password = "password"
  361. let configuration_apppassword = "apppassword"
  362. let configuration_disable_intro = "disable_intro"
  363. let configuration_disable_multiaccount = "disable_multiaccount"
  364. let configuration_disable_crash_service = "disable_crash_service"
  365. let configuration_disable_log = "disable_log"
  366. let configuration_disable_manage_account = "disable_manage_account"
  367. let configuration_disable_more_external_site = "disable_more_external_site"
  368. let configuration_disable_openin_file = "disable_openin_file"
  369. // CAPABILITIES
  370. //
  371. var capabilityServerVersionMajor: Int = 0
  372. @objc var capabilityServerVersion: String = ""
  373. var capabilityFileSharingApiEnabled: Bool = false
  374. var capabilityFileSharingPubPasswdEnforced: Bool = false
  375. var capabilityFileSharingPubExpireDateEnforced: Bool = false
  376. var capabilityFileSharingPubExpireDateDays: Int = 0
  377. var capabilityFileSharingInternalExpireDateEnforced: Bool = false
  378. var capabilityFileSharingInternalExpireDateDays: Int = 0
  379. var capabilityFileSharingRemoteExpireDateEnforced: Bool = false
  380. var capabilityFileSharingRemoteExpireDateDays: Int = 0
  381. var capabilityFileSharingDefaultPermission: Int = 0
  382. var capabilityThemingColor: String = ""
  383. var capabilityThemingColorElement: String = ""
  384. var capabilityThemingColorText: String = ""
  385. @objc var capabilityThemingName: String = ""
  386. @objc var capabilityThemingSlogan: String = ""
  387. @objc var capabilityE2EEEnabled: Bool = false
  388. @objc var capabilityE2EEApiVersion: String = ""
  389. var capabilityRichdocumentsEnabled: Bool = false
  390. var capabilityRichdocumentsMimetypes: [String] = []
  391. var capabilityActivity: [String] = []
  392. var capabilityNotification: [String] = []
  393. var capabilityFilesUndelete: Bool = false
  394. var capabilityFilesLockVersion: String = "" // NC 24
  395. var capabilityFilesComments: Bool = false // NC 20
  396. var capabilityFilesBigfilechunking: Bool = false
  397. @objc var capabilityUserStatusEnabled: Bool = false
  398. var capabilityExternalSites: Bool = false
  399. var capabilityGroupfoldersEnabled: Bool = false // NC27
  400. var isLivePhotoServerAvailable: Bool { // NC28
  401. return capabilityServerVersionMajor >= nextcloudVersion28
  402. }
  403. var capabilitySecurityGuardDiagnostics = false
  404. // MORE NEXTCLOUD APPS
  405. let talkSchemeUrl = "nextcloudtalk://"
  406. let notesSchemeUrl = "nextcloudnotes://"
  407. let talkAppStoreUrl = "https://apps.apple.com/de/app/nextcloud-talk/id1296825574"
  408. let notesAppStoreUrl = "https://apps.apple.com/de/app/nextcloud-notes/id813973264"
  409. let moreAppsUrl = "itms-apps://search.itunes.apple.com/WebObjects/MZSearch.woa/wa/search?media=software&term=nextcloud"
  410. // SNAPSHOT PREVIEW
  411. //
  412. let defaultSnapshotConfiguration = "DefaultPreviewConfiguration"
  413. // FORBIDDEN CHARACTERS
  414. //
  415. let forbiddenCharacters = ["/", "\\", ":", "\"", "|", "?", "*", "<", ">"]
  416. // DIAGNOSTICS CLIENTS
  417. //
  418. let diagnosticIssueSyncConflicts = "sync_conflicts"
  419. let diagnosticIssueProblems = "problems"
  420. let diagnosticIssueVirusDetected = "virus_detected"
  421. let diagnosticIssueE2eeErrors = "e2ee_errors"
  422. let diagnosticProblemsForbidden = "CHARACTERS_FORBIDDEN"
  423. let diagnosticProblemsBadResponse = "BAD_SERVER_RESPONSE"
  424. let diagnosticProblemsUploadServerError = "UploadError.SERVER_ERROR"
  425. // MEDIA LAYOUT
  426. //
  427. let mediaLayoutRatio = "mediaLayoutRatio"
  428. let mediaLayoutSquare = "mediaLayoutSquare"
  429. }