NCGlobal.swift 23 KB

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