NCGlobal.swift 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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. }
  73. // Directory on Group
  74. //
  75. @objc let directoryProviderStorage = "File Provider Storage"
  76. @objc let appApplicationSupport = "Library/Application Support"
  77. @objc let appCertificates = "Library/Application Support/Certificates"
  78. @objc let appDatabaseNextcloud = "Library/Application Support/Nextcloud"
  79. @objc let appScan = "Library/Application Support/Scan"
  80. @objc let appUserData = "Library/Application Support/UserData"
  81. @objc let appDatabaseTalk = "Library/Application Support/Talk"
  82. // Service
  83. //
  84. @objc let serviceShareKeyChain = "Crypto Cloud"
  85. let metadataKeyedUnarchiver = "it.twsweb.nextcloud.metadata"
  86. let refreshTask = "com.nextcloud.refreshTask"
  87. let processingTask = "com.nextcloud.processingTask"
  88. // Name
  89. //
  90. @objc let appName = "files"
  91. @objc let talkName = "talk-message"
  92. // DAV -
  93. //
  94. @objc let dav = "remote.php/dav"
  95. @objc let davfiles = "remote.php/dav/files/"
  96. // Nextcloud version
  97. //
  98. let nextcloudVersion12: Int = 12
  99. let nextcloudVersion15: Int = 15
  100. let nextcloudVersion17: Int = 17
  101. let nextcloudVersion18: Int = 18
  102. let nextcloudVersion20: Int = 20
  103. let nextcloudVersion23: Int = 23
  104. let nextcloudVersion24: Int = 24
  105. let nextcloudVersion25: Int = 25
  106. // Database Realm
  107. //
  108. let databaseDefault = "nextcloud.realm"
  109. let databaseSchemaVersion: UInt64 = 257
  110. let fileAccounts: String = "accounts.json"
  111. // Intro selector
  112. //
  113. @objc let introLogin: Int = 0
  114. let introSignup: Int = 1
  115. // Varie size GUI
  116. //
  117. @objc let heightCellSettings: CGFloat = 50
  118. // Avatar & Preview size
  119. //
  120. let avatarSize: Int = 128 * Int(UIScreen.main.scale)
  121. let avatarSizeRounded: Int = 128
  122. let sizePreview: Int = 1024
  123. let sizeIcon: Int = 512
  124. // E2EE
  125. //
  126. let e2eeMaxFileSize: UInt64 = 500000000 // 500 MB
  127. let e2eePassphraseTest = "more over television factory tendency independence international intellectual impress interest sentence pony"
  128. @objc let e2eeVersion = "1.1"
  129. // Video
  130. //
  131. let maxHTTPCache: Int64 = 10000000000 // 10 GB
  132. let fileNameVideoEncoded: String = "video_encoded.mp4"
  133. // NCSharePaging
  134. //
  135. enum NCSharePagingIndex: Int, CaseIterable {
  136. case activity, sharing
  137. }
  138. // NCViewerProviderContextMenu
  139. //
  140. let maxAutoDownload: UInt64 = 50000000 // 50MB
  141. let maxAutoDownloadCellular: UInt64 = 10000000 // 10MB
  142. // Nextcloud unsupported
  143. //
  144. let nextcloud_unsupported_version: Int = 16
  145. // Layout
  146. //
  147. let layoutList = "typeLayoutList"
  148. let layoutGrid = "typeLayoutGrid"
  149. let layoutViewMove = "LayoutMove"
  150. let layoutViewTrash = "LayoutTrash"
  151. let layoutViewOffline = "LayoutOffline"
  152. let layoutViewFavorite = "LayoutFavorite"
  153. let layoutViewFiles = "LayoutFiles"
  154. let layoutViewViewInFolder = "LayoutViewInFolder"
  155. let layoutViewTransfers = "LayoutTransfers"
  156. let layoutViewRecent = "LayoutRecent"
  157. let layoutViewShares = "LayoutShares"
  158. let layoutViewShareExtension = "LayoutShareExtension"
  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 heightButtonsCommand: CGFloat = 50
  167. let heightButtonsView: 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. // Extension
  190. //
  191. @objc let extensionPreview = "ico"
  192. // ContentPresenter
  193. //
  194. @objc let dismissAfterSecond: TimeInterval = 4
  195. @objc let dismissAfterSecondLong: TimeInterval = 10
  196. // Error
  197. //
  198. @objc let errorRequestExplicityCancelled: Int = 15
  199. @objc let errorNotModified: Int = 304
  200. @objc let errorBadRequest: Int = 400
  201. @objc let errorUnauthorized: Int = 401
  202. @objc let errorForbidden: Int = 403
  203. @objc let errorResourceNotFound: Int = 404
  204. @objc let errordMethodNotSupported: Int = 405
  205. @objc let errorConflict: Int = 409
  206. @objc let errorPreconditionFailed: Int = 412
  207. @objc let errorNCUnauthorized: Int = 997
  208. @objc let errorConnectionLost: Int = -1005
  209. @objc let errorNetworkNotAvailable: Int = -1009
  210. @objc let errorBadServerResponse: Int = -1011
  211. @objc let errorInternalError: Int = -99999
  212. @objc let errorFileNotSaved: Int = -99998
  213. @objc let errorDecodeMetadata: Int = -99997
  214. @objc let errorE2EENotEnabled: Int = -99996
  215. @objc let errorOffline: Int = -99994
  216. @objc let errorCharactersForbidden: Int = -99993
  217. @objc let errorCreationFile: Int = -99992
  218. @objc let errorReadFile: Int = -99991
  219. // Constants to identify the different permissions of a file
  220. //
  221. @objc let permissionShared = "S"
  222. @objc let permissionCanShare = "R"
  223. @objc let permissionMounted = "M"
  224. @objc let permissionFileCanWrite = "W"
  225. @objc let permissionCanCreateFile = "C"
  226. @objc let permissionCanCreateFolder = "K"
  227. @objc let permissionCanDelete = "D"
  228. @objc let permissionCanRename = "N"
  229. @objc let permissionCanMove = "V"
  230. // Share permission
  231. // permissions - (int) 1 = read; 2 = update; 4 = create; 8 = delete; 16 = share; 31 = all
  232. //
  233. @objc let permissionReadShare: Int = 1
  234. @objc let permissionUpdateShare: Int = 2
  235. @objc let permissionCreateShare: Int = 4
  236. @objc let permissionDeleteShare: Int = 8
  237. @objc let permissionShareShare: Int = 16
  238. @objc let permissionMinFileShare: Int = 1
  239. @objc let permissionMaxFileShare: Int = 19
  240. @objc let permissionMinFolderShare: Int = 1
  241. @objc let permissionMaxFolderShare: Int = 31
  242. @objc let permissionDefaultFileRemoteShareNoSupportShareOption: Int = 3
  243. @objc let permissionDefaultFolderRemoteShareNoSupportShareOption: Int = 15
  244. // Filename Mask and Type
  245. //
  246. let keyFileNameMask = "fileNameMask"
  247. let keyFileNameType = "fileNameType"
  248. let keyFileNameAutoUploadMask = "fileNameAutoUploadMask"
  249. let keyFileNameAutoUploadType = "fileNameAutoUploadType"
  250. let keyFileNameOriginal = "fileNameOriginal"
  251. let keyFileNameOriginalAutoUpload = "fileNameOriginalAutoUpload"
  252. // Selector
  253. //
  254. let selectorDownloadFile = "downloadFile"
  255. let selectorDownloadAllFile = "downloadAllFile"
  256. let selectorReadFile = "readFile"
  257. let selectorListingFavorite = "listingFavorite"
  258. let selectorLoadFileView = "loadFileView"
  259. let selectorLoadFileQuickLook = "loadFileQuickLook"
  260. let selectorLoadOffline = "loadOffline"
  261. let selectorOpenIn = "openIn"
  262. let selectorPrint = "print"
  263. let selectorUploadAutoUpload = "uploadAutoUpload"
  264. let selectorUploadAutoUploadAll = "uploadAutoUploadAll"
  265. let selectorUploadFile = "uploadFile"
  266. let selectorUploadFileNODelete = "UploadFileNODelete"
  267. let selectorUploadFileShareExtension = "uploadFileShareExtension"
  268. let selectorSaveAlbum = "saveAlbum"
  269. let selectorSaveAlbumLivePhotoIMG = "saveAlbumLivePhotoIMG"
  270. let selectorSaveAlbumLivePhotoMOV = "saveAlbumLivePhotoMOV"
  271. let selectorSaveAsScan = "saveAsScan"
  272. let selectorOpenDetail = "openDetail"
  273. // Metadata : Status
  274. //
  275. // 1) wait download/upload
  276. // 2) in download/upload
  277. // 3) downloading/uploading
  278. // 4) done or error
  279. //
  280. let metadataStatusNormal: Int = 0
  281. let metadataStatusWaitDownload: Int = -1
  282. let metadataStatusInDownload: Int = -2
  283. let metadataStatusDownloading: Int = -3
  284. let metadataStatusDownloadError: Int = -4
  285. let metadataStatusWaitUpload: Int = 1
  286. let metadataStatusInUpload: Int = 2
  287. let metadataStatusUploading: Int = 3
  288. let metadataStatusUploadError: Int = 4
  289. // Notification Center
  290. //
  291. @objc let notificationCenterApplicationDidEnterBackground = "applicationDidEnterBackground"
  292. let notificationCenterApplicationWillEnterForeground = "applicationWillEnterForeground"
  293. let notificationCenterApplicationDidBecomeActive = "applicationDidBecomeActive"
  294. let notificationCenterApplicationWillResignActive = "applicationWillResignActive"
  295. @objc let notificationCenterInitialize = "initialize" // userInfo?: atStart
  296. @objc let notificationCenterChangeTheming = "changeTheming"
  297. let notificationCenterRichdocumentGrabFocus = "richdocumentGrabFocus"
  298. let notificationCenterReloadDataNCShare = "reloadDataNCShare"
  299. let notificationCenterCloseRichWorkspaceWebView = "closeRichWorkspaceWebView"
  300. let notificationCenterUpdateBadgeNumber = "updateBadgeNumber" // userInfo: counter
  301. let notificationCenterReloadAvatar = "reloadAvatar"
  302. @objc let notificationCenterReloadDataSource = "reloadDataSource" // userInfo: serverUrl?
  303. let notificationCenterReloadDataSourceNetwork = "reloadDataSourceNetwork" // userInfo: serverUrl?
  304. let notificationCenterReloadDataSourceNetworkForced = "reloadDataSourceNetworkForced" // userInfo: serverUrl?
  305. let notificationCenterChangeStatusFolderE2EE = "changeStatusFolderE2EE" // userInfo: serverUrl
  306. let notificationCenterDownloadStartFile = "downloadStartFile" // userInfo: ocId, serverUrl, account
  307. let notificationCenterDownloadedFile = "downloadedFile" // userInfo: ocId, serverUrl, account, selector, error
  308. let notificationCenterDownloadCancelFile = "downloadCancelFile" // userInfo: ocId, serverUrl, account
  309. let notificationCenterUploadStartFile = "uploadStartFile" // userInfo: ocId, serverUrl, account, fileName, sessionSelector
  310. @objc let notificationCenterUploadedFile = "uploadedFile" // userInfo: ocId, serverUrl, account, fileName, ocIdTemp, error
  311. let notificationCenterUploadCancelFile = "uploadCancelFile" // userInfo: ocId, serverUrl, account
  312. let notificationCenterProgressTask = "progressTask" // userInfo: account, ocId, serverUrl, status, progress, totalBytes, totalBytesExpected
  313. let notificationCenterCreateFolder = "createFolder" // userInfo: ocId, serverUrl, account, e2ee
  314. let notificationCenterDeleteFile = "deleteFile" // userInfo: ocId, fileNameView, serverUrl, account, classFile, onlyLocalCache
  315. let notificationCenterRenameFile = "renameFile" // userInfo: ocId, account
  316. let notificationCenterMoveFile = "moveFile" // userInfo: ocId, account, serverUrlFrom
  317. let notificationCenterCopyFile = "copyFile" // userInfo: ocId, serverUrlTo
  318. let notificationCenterFavoriteFile = "favoriteFile" // userInfo: ocId, serverUrl
  319. let notificationCenterMenuSearchTextPDF = "menuSearchTextPDF"
  320. let notificationCenterMenuGotToPageInPDF = "menuGotToPageInPDF"
  321. let notificationCenterMenuDetailClose = "menuDetailClose"
  322. let notificationCenterChangedLocation = "changedLocation"
  323. let notificationStatusAuthorizationChangedLocation = "statusAuthorizationChangedLocation"
  324. let notificationCenterDownloadedThumbnail = "DownloadedThumbnail" // userInfo: ocId
  325. let notificationCenterHidePlayerToolBar = "hidePlayerToolBar" // userInfo: ocId
  326. let notificationCenterShowPlayerToolBar = "showPlayerToolBar" // userInfo: ocId, enableTimerAutoHide
  327. let notificationCenterOpenMediaDetail = "openMediaDetail" // userInfo: ocId
  328. let notificationCenterReloadMediaPage = "reloadMediaPage"
  329. let notificationCenterPlayMedia = "playMedia"
  330. let notificationCenterPauseMedia = "pauseMedia"
  331. // TIP
  332. //
  333. let tipNCViewerPDFThumbnail = "tipncviewerpdfthumbnail"
  334. let tipNCCollectionViewCommonAccountRequest = "tipnccollectionviewcommonaccountrequest"
  335. let tipNCScanAddImage = "tipncscanaddimage"
  336. let tipNCViewerMediaDetailView = "tipncviewermediadetailview"
  337. // ACTION
  338. //
  339. let actionNoAction = "no-action"
  340. let actionUploadAsset = "upload-asset"
  341. let actionScanDocument = "add-scan-document"
  342. let actionTextDocument = "create-text-document"
  343. let actionVoiceMemo = "create-voice-memo"
  344. // WIDGET ACTION
  345. //
  346. let widgetActionNoAction = "nextcloud://open-action?action=no-action"
  347. let widgetActionUploadAsset = "nextcloud://open-action?action=upload-asset"
  348. let widgetActionScanDocument = "nextcloud://open-action?action=add-scan-document"
  349. let widgetActionTextDocument = "nextcloud://open-action?action=create-text-document"
  350. let widgetActionVoiceMemo = "nextcloud://open-action?action=create-voice-memo"
  351. // APPCONFIG
  352. //
  353. let configuration_brand = "brand"
  354. let configuration_serverUrl = "serverUrl"
  355. let configuration_username = "username"
  356. let configuration_password = "password"
  357. let configuration_apppassword = "apppassword"
  358. let configuration_disable_intro = "disable_intro"
  359. let configuration_disable_multiaccount = "disable_multiaccount"
  360. let configuration_disable_crash_service = "disable_crash_service"
  361. let configuration_disable_log = "disable_log"
  362. let configuration_disable_manage_account = "disable_manage_account"
  363. let configuration_disable_more_external_site = "disable_more_external_site"
  364. let configuration_disable_openin_file = "disable_openin_file"
  365. }