NCGlobal.swift 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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. // Struct for Progress
  56. //
  57. struct progressType {
  58. var progress: Float
  59. var totalBytes: Int64
  60. var totalBytesExpected: Int64
  61. }
  62. // Struct for LayoutForView
  63. //
  64. struct layoutForViewType {
  65. var layout: String
  66. var sort: String
  67. var ascending: Bool
  68. var groupBy: String
  69. var directoryOnTop: Bool
  70. var titleButtonHeader: String
  71. var itemForLine: Int
  72. var imageBackgroud: String
  73. var imageBackgroudContentMode: String
  74. }
  75. // Directory on Group
  76. //
  77. @objc let appDatabaseNextcloud = "Library/Application Support/Nextcloud"
  78. @objc let appApplicationSupport = "Library/Application Support"
  79. @objc let appUserData = "Library/Application Support/UserData"
  80. @objc let appCertificates = "Library/Application Support/Certificates"
  81. @objc let appScan = "Library/Application Support/Scan"
  82. @objc let appBackground = "Library/Application Support/Background"
  83. @objc let directoryProviderStorage = "File Provider Storage"
  84. // Service
  85. //
  86. @objc let serviceShareKeyChain = "Crypto Cloud"
  87. let metadataKeyedUnarchiver = "it.twsweb.nextcloud.metadata"
  88. let refreshTask = "com.nextcloud.refreshTask"
  89. let processingTask = "com.nextcloud.processingTask"
  90. // Nextcloud version
  91. //
  92. let nextcloudVersion12: Int = 12
  93. let nextcloudVersion15: Int = 15
  94. let nextcloudVersion17: Int = 17
  95. let nextcloudVersion18: Int = 18
  96. let nextcloudVersion20: Int = 20
  97. let nextcloudVersion23: Int = 23
  98. // Database Realm
  99. //
  100. let databaseDefault = "nextcloud.realm"
  101. let databaseSchemaVersion: UInt64 = 218
  102. // Intro selector
  103. //
  104. @objc let introLogin: Int = 0
  105. let introSignup: Int = 1
  106. // Varie size GUI
  107. //
  108. @objc let heightCellSettings: CGFloat = 50
  109. // Avatar & Preview size
  110. //
  111. let avatarSize: Int = 128 * Int(UIScreen.main.scale)
  112. let avatarSizeRounded: Int = 128
  113. let sizePreview: Int = 1024
  114. let sizeIcon: Int = 512
  115. // E2EE
  116. //
  117. let e2eeMaxFileSize: UInt64 = 500000000 // 500 MB
  118. let e2eePassphraseTest = "more over television factory tendency independence international intellectual impress interest sentence pony"
  119. @objc let e2eeVersion = "1.1"
  120. // Video
  121. //
  122. let maxHTTPCache: Int64 = 10000000000 // 10 GB
  123. let fileNameVideoEncoded: String = "video_encoded.mp4"
  124. // NCSharePaging
  125. //
  126. enum NCSharePagingIndex: Int, CaseIterable {
  127. case activity, sharing
  128. }
  129. // NCViewerProviderContextMenu
  130. //
  131. let maxAutoDownload: UInt64 = 50000000 // 50MB
  132. let maxAutoDownloadCellular: UInt64 = 10000000 // 10MB
  133. // Nextcloud unsupported
  134. //
  135. let nextcloud_unsupported_version: Int = 16
  136. // Layout
  137. //
  138. let layoutList = "typeLayoutList"
  139. let layoutGrid = "typeLayoutGrid"
  140. let layoutViewMove = "LayoutMove"
  141. let layoutViewTrash = "LayoutTrash"
  142. let layoutViewOffline = "LayoutOffline"
  143. let layoutViewFavorite = "LayoutFavorite"
  144. let layoutViewFiles = "LayoutFiles"
  145. let layoutViewViewInFolder = "LayoutViewInFolder"
  146. let layoutViewTransfers = "LayoutTransfers"
  147. let layoutViewRecent = "LayoutRecent"
  148. let layoutViewShares = "LayoutShares"
  149. let layoutViewShareExtension = "LayoutShareExtension"
  150. // Button Type in Cell list/grid
  151. //
  152. let buttonMoreMore = "more"
  153. let buttonMoreStop = "stop"
  154. let buttonMoreLock = "moreLock"
  155. // Text - OnlyOffice - Collabora - QuickLook
  156. //
  157. let editorText = "text"
  158. let editorOnlyoffice = "onlyoffice"
  159. let editorCollabora = "collabora"
  160. let editorQuickLook = "quicklook"
  161. let onlyofficeDocx = "onlyoffice_docx"
  162. let onlyofficeXlsx = "onlyoffice_xlsx"
  163. let onlyofficePptx = "onlyoffice_pptx"
  164. // Template
  165. //
  166. let templateDocument = "document"
  167. let templateSpreadsheet = "spreadsheet"
  168. let templatePresentation = "presentation"
  169. // Rich Workspace
  170. //
  171. let fileNameRichWorkspace = "Readme.md"
  172. // Extension
  173. @objc let extensionPreview = "ico"
  174. // ContentPresenter
  175. //
  176. @objc let dismissAfterSecond: TimeInterval = 4
  177. @objc let dismissAfterSecondLong: TimeInterval = 10
  178. // Error
  179. //
  180. @objc let errorNoError: Int = 0
  181. @objc let errorRequestExplicityCancelled: Int = 15
  182. @objc let errorNotModified: Int = 304
  183. @objc let errorBadRequest: Int = 400
  184. @objc let errorResourceNotFound: Int = 404
  185. @objc let errordMethodNotSupported: Int = 405
  186. @objc let errorConflict: Int = 409
  187. @objc let errorConnectionLost: Int = -1005
  188. @objc let errorBadServerResponse: Int = -1011
  189. @objc let errorInternalError: Int = -99999
  190. @objc let errorFileNotSaved: Int = -99998
  191. @objc let errorDecodeMetadata: Int = -99997
  192. @objc let errorE2EENotEnabled: Int = -99996
  193. @objc let errorOffline: Int = -99994
  194. @objc let errorCharactersForbidden: Int = -99993
  195. @objc let errorCreationFile: Int = -99992
  196. @objc let errorReadFile: Int = -99991
  197. @objc let errorGeneric: Int = -99990
  198. // Constants to identify the different permissions of a file
  199. //
  200. @objc let permissionShared = "S"
  201. @objc let permissionCanShare = "R"
  202. @objc let permissionMounted = "M"
  203. @objc let permissionFileCanWrite = "W"
  204. @objc let permissionCanCreateFile = "C"
  205. @objc let permissionCanCreateFolder = "K"
  206. @objc let permissionCanDelete = "D"
  207. @objc let permissionCanRename = "N"
  208. @objc let permissionCanMove = "V"
  209. // Share permission
  210. // permissions - (int) 1 = read; 2 = update; 4 = create; 8 = delete; 16 = share; 31 = all
  211. //
  212. @objc let permissionReadShare: Int = 1
  213. @objc let permissionUpdateShare: Int = 2
  214. @objc let permissionCreateShare: Int = 4
  215. @objc let permissionDeleteShare: Int = 8
  216. @objc let permissionShareShare: Int = 16
  217. @objc let permissionMinFileShare: Int = 1
  218. @objc let permissionMaxFileShare: Int = 19
  219. @objc let permissionMinFolderShare: Int = 1
  220. @objc let permissionMaxFolderShare: Int = 31
  221. @objc let permissionDefaultFileRemoteShareNoSupportShareOption: Int = 3
  222. @objc let permissionDefaultFolderRemoteShareNoSupportShareOption: Int = 15
  223. // Filename Mask and Type
  224. //
  225. let keyFileNameMask = "fileNameMask"
  226. let keyFileNameType = "fileNameType"
  227. let keyFileNameAutoUploadMask = "fileNameAutoUploadMask"
  228. let keyFileNameAutoUploadType = "fileNameAutoUploadType"
  229. let keyFileNameOriginal = "fileNameOriginal"
  230. let keyFileNameOriginalAutoUpload = "fileNameOriginalAutoUpload"
  231. // Selector
  232. //
  233. let selectorDownloadFile = "downloadFile"
  234. let selectorDownloadAllFile = "downloadAllFile"
  235. let selectorReadFile = "readFile"
  236. let selectorListingFavorite = "listingFavorite"
  237. let selectorLoadFileView = "loadFileView"
  238. let selectorLoadFileQuickLook = "loadFileQuickLook"
  239. let selectorLoadOffline = "loadOffline"
  240. let selectorOpenIn = "openIn"
  241. let selectorPrint = "print"
  242. let selectorUploadAutoUpload = "uploadAutoUpload"
  243. let selectorUploadAutoUploadAll = "uploadAutoUploadAll"
  244. let selectorUploadFile = "uploadFile"
  245. let selectorUploadFileShareExtension = "uploadFileShareExtension"
  246. let selectorSaveAlbum = "saveAlbum"
  247. let selectorSaveBackground = "saveBackground"
  248. let selectorSaveAlbumLivePhotoIMG = "saveAlbumLivePhotoIMG"
  249. let selectorSaveAlbumLivePhotoMOV = "saveAlbumLivePhotoMOV"
  250. let selectorSaveAsScan = "saveAsScan"
  251. let selectorOpenDetail = "openDetail"
  252. // Metadata : Status
  253. //
  254. // 1) wait download/upload
  255. // 2) in download/upload
  256. // 3) downloading/uploading
  257. // 4) done or error
  258. //
  259. let metadataStatusNormal: Int = 0
  260. let metadataStatusWaitDownload: Int = -1
  261. let metadataStatusInDownload: Int = -2
  262. let metadataStatusDownloading: Int = -3
  263. let metadataStatusDownloadError: Int = -4
  264. let metadataStatusWaitUpload: Int = 1
  265. let metadataStatusInUpload: Int = 2
  266. let metadataStatusUploading: Int = 3
  267. let metadataStatusUploadError: Int = 4
  268. // Notification Center
  269. //
  270. @objc let notificationCenterApplicationDidEnterBackground = "applicationDidEnterBackground"
  271. let notificationCenterApplicationWillEnterForeground = "applicationWillEnterForeground"
  272. let notificationCenterApplicationDidBecomeActive = "applicationDidBecomeActive"
  273. let notificationCenterApplicationWillResignActive = "applicationWillResignActive"
  274. @objc let notificationCenterInitialize = "initialize"
  275. @objc let notificationCenterChangeTheming = "changeTheming"
  276. let notificationCenterRichdocumentGrabFocus = "richdocumentGrabFocus"
  277. let notificationCenterReloadDataNCShare = "reloadDataNCShare"
  278. let notificationCenterCloseRichWorkspaceWebView = "closeRichWorkspaceWebView"
  279. let notificationCenterUpdateBadgeNumber = "updateBadgeNumber"
  280. let notificationCenterReloadAvatar = "reloadAvatar"
  281. let notificationCenterOpenFileViewInFolder = "openFileViewInFolder" // userInfo: serverUrl, fileName
  282. @objc let notificationCenterReloadDataSource = "reloadDataSource" // userInfo: ocId?, serverUrl?
  283. let notificationCenterReloadDataSourceNetworkForced = "reloadDataSourceNetworkForced" // userInfo: serverUrl?
  284. let notificationCenterChangeStatusFolderE2EE = "changeStatusFolderE2EE" // userInfo: serverUrl
  285. let notificationCenterDownloadStartFile = "downloadStartFile" // userInfo: ocId
  286. let notificationCenterDownloadedFile = "downloadedFile" // userInfo: ocId, selector, errorCode, errorDescription
  287. let notificationCenterDownloadCancelFile = "downloadCancelFile" // userInfo: ocId
  288. let notificationCenterUploadStartFile = "uploadStartFile" // userInfo: ocId
  289. @objc let notificationCenterUploadedFile = "uploadedFile" // userInfo: ocId, ocIdTemp, errorCode, errorDescription
  290. let notificationCenterUploadCancelFile = "uploadCancelFile" // userInfo: ocId, serverUrl, account
  291. let notificationCenterProgressTask = "progressTask" // userInfo: account, ocId, serverUrl, status, progress, totalBytes, totalBytesExpected
  292. let notificationCenterCreateFolder = "createFolder" // userInfo: ocId
  293. let notificationCenterDeleteFile = "deleteFile" // userInfo: ocId, fileNameView, classFile, onlyLocalCache
  294. let notificationCenterRenameFile = "renameFile" // userInfo: ocId, errorCode, errorDescription
  295. let notificationCenterMoveFile = "moveFile" // userInfo: ocId, serverUrlTo
  296. let notificationCenterCopyFile = "copyFile" // userInfo: ocId, serverUrlFrom
  297. let notificationCenterFavoriteFile = "favoriteFile" // userInfo: ocId
  298. let notificationCenterMenuSearchTextPDF = "menuSearchTextPDF"
  299. let notificationCenterMenuGotToPageInPDF = "menuGotToPageInPDF"
  300. let notificationCenterMenuDetailClose = "menuDetailClose"
  301. let notificationCenterChangedLocation = "changedLocation"
  302. let notificationStatusAuthorizationChangedLocation = "statusAuthorizationChangedLocation"
  303. let notificationCenterDownloadedThumbnail = "DownloadedThumbnail" // userInfo: ocId
  304. let notificationCenterHidePlayerToolBar = "hidePlayerToolBar" // userInfo: ocId
  305. let notificationCenterShowPlayerToolBar = "showPlayerToolBar" // userInfo: ocId, enableTimerAutoHide
  306. let notificationCenterOpenMediaDetail = "openMediaDetail" // userInfo: ocId
  307. let notificationCenterReloadMediaPage = "reloadMediaPage"
  308. let notificationCenterPlayMedia = "playMedia"
  309. let notificationCenterPauseMedia = "pauseMedia"
  310. // Tip
  311. //
  312. let tipNCViewerPDFThumbnail = "tipncviewerpdfthumbnail"
  313. }