FilesData.swift 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. //
  2. // FilesData.swift
  3. // Widget
  4. //
  5. // Created by Marino Faggiana on 25/08/22.
  6. // Copyright © 2022 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 WidgetKit
  24. import NextcloudKit
  25. struct FilesDataEntry: TimelineEntry {
  26. let date: Date
  27. let datas: [FilesData]
  28. let isPlaceholder: Bool
  29. let tile: String
  30. let footerImage: String
  31. let footerText: String
  32. }
  33. struct FilesData: Identifiable, Hashable {
  34. var id: String
  35. var image: UIImage
  36. var title: String
  37. var subTitle: String
  38. var url: URL
  39. }
  40. let filesDatasTest: [FilesData] = [
  41. .init(id: "0", image: UIImage(named: "widget")!, title: "title1", subTitle: "subTitle-description1", url: URL(string: "https://nextcloud.com/")!),
  42. .init(id: "1", image: UIImage(named: "widget")!, title: "title2", subTitle: "subTitle-description2", url: URL(string: "https://nextcloud.com/")!),
  43. .init(id: "2", image: UIImage(named: "widget")!, title: "title3", subTitle: "subTitle-description3", url: URL(string: "https://nextcloud.com/")!),
  44. .init(id: "3", image: UIImage(named: "widget")!, title: "title4", subTitle: "subTitle-description4", url: URL(string: "https://nextcloud.com/")!),
  45. .init(id: "4", image: UIImage(named: "widget")!, title: "title4", subTitle: "subTitle-description4", url: URL(string: "https://nextcloud.com/")!)
  46. ]
  47. func getTitleFilesWidget() -> String {
  48. let hour = Calendar.current.component(.hour, from: Date())
  49. var good = ""
  50. switch hour {
  51. case 6..<12: good = NSLocalizedString("_good_morning_", value: "Good morning", comment: "")
  52. case 12: good = NSLocalizedString("_good_day_", value: "Good day", comment: "")
  53. case 13..<17: good = NSLocalizedString("_good_afternoon_", value: "Good afternoon", comment: "")
  54. case 17..<22: good = NSLocalizedString("_good_evening_", value: "Good evening", comment: "")
  55. default: good = NSLocalizedString("_good_night_", value: "Good night", comment: "")
  56. }
  57. if let account = NCManageDatabase.shared.getActiveAccount() {
  58. return good + ", " + account.displayName
  59. } else {
  60. return good
  61. }
  62. }
  63. func getItems(displaySize: CGSize) -> Int {
  64. let height = Int((displaySize.height - 100) / 50)
  65. return height
  66. }
  67. func getFilesDataEntry(isPreview: Bool, displaySize: CGSize, completion: @escaping (_ entry: FilesDataEntry) -> Void) {
  68. let items = getItems(displaySize: displaySize)
  69. let datasPlaceholder = Array(filesDatasTest[0...items - 1])
  70. let title = getTitleFilesWidget()
  71. if isPreview {
  72. return completion(FilesDataEntry(date: Date(), datas: datasPlaceholder, isPlaceholder: true, tile: title, footerImage: "checkmark.icloud", footerText: NCBrandOptions.shared.brand + " files"))
  73. }
  74. guard let account = NCManageDatabase.shared.getActiveAccount() else {
  75. return completion(FilesDataEntry(date: Date(), datas: datasPlaceholder, isPlaceholder: true, tile: title, footerImage: "xmark.icloud", footerText: NSLocalizedString("_no_active_account_", value: "No account found", comment: "")))
  76. }
  77. func isLive(file: NKFile, files: [NKFile]) -> Bool {
  78. if file.ext.lowercased() != "mov" { return false }
  79. if files.filter({ ($0.fileNameWithoutExt == file.fileNameWithoutExt) && ($0.ext.lowercased() == "jpg") }).first != nil {
  80. return true
  81. }
  82. return false
  83. }
  84. // NETWORKING
  85. let password = CCUtility.getPassword(account.account)!
  86. NKCommon.shared.setup(
  87. account: account.account,
  88. user: account.user,
  89. userId: account.userId,
  90. password: password,
  91. urlBase: account.urlBase,
  92. userAgent: CCUtility.getUserAgent(),
  93. nextcloudVersion: 0,
  94. delegate: NCNetworking.shared)
  95. let requestBodyRecent =
  96. """
  97. <?xml version=\"1.0\"?>
  98. <d:searchrequest xmlns:d=\"DAV:\" xmlns:oc=\"http://owncloud.org/ns\" xmlns:nc=\"http://nextcloud.org/ns\">
  99. <d:basicsearch>
  100. <d:select>
  101. <d:prop>
  102. <d:displayname/>
  103. <d:getcontenttype/>
  104. <d:resourcetype/>
  105. <d:getcontentlength/>
  106. <d:getlastmodified/>
  107. <d:getetag/>
  108. <d:quota-used-bytes/>
  109. <d:quota-available-bytes/>
  110. <permissions xmlns=\"http://owncloud.org/ns\"/>
  111. <id xmlns=\"http://owncloud.org/ns\"/>
  112. <fileid xmlns=\"http://owncloud.org/ns\"/>
  113. <size xmlns=\"http://owncloud.org/ns\"/>
  114. <favorite xmlns=\"http://owncloud.org/ns\"/>
  115. <creation_time xmlns=\"http://nextcloud.org/ns\"/>
  116. <upload_time xmlns=\"http://nextcloud.org/ns\"/>
  117. <is-encrypted xmlns=\"http://nextcloud.org/ns\"/>
  118. <mount-type xmlns=\"http://nextcloud.org/ns\"/>
  119. <owner-id xmlns=\"http://owncloud.org/ns\"/>
  120. <owner-display-name xmlns=\"http://owncloud.org/ns\"/>
  121. <comments-unread xmlns=\"http://owncloud.org/ns\"/>
  122. <has-preview xmlns=\"http://nextcloud.org/ns\"/>
  123. <trashbin-filename xmlns=\"http://nextcloud.org/ns\"/>
  124. <trashbin-original-location xmlns=\"http://nextcloud.org/ns\"/>
  125. <trashbin-deletion-time xmlns=\"http://nextcloud.org/ns\"/>
  126. </d:prop>
  127. </d:select>
  128. <d:from>
  129. <d:scope>
  130. <d:href>%@</d:href>
  131. <d:depth>infinity</d:depth>
  132. </d:scope>
  133. </d:from>
  134. <d:where>
  135. <d:lt>
  136. <d:prop>
  137. <d:getlastmodified/>
  138. </d:prop>
  139. <d:literal>%@</d:literal>
  140. </d:lt>
  141. </d:where>
  142. <d:orderby>
  143. <d:order>
  144. <d:prop>
  145. <d:getlastmodified/>
  146. </d:prop>
  147. <d:descending/>
  148. </d:order>
  149. </d:orderby>
  150. <d:limit>
  151. <d:nresults>50</d:nresults>
  152. </d:limit>
  153. </d:basicsearch>
  154. </d:searchrequest>
  155. """
  156. let dateFormatter = DateFormatter()
  157. dateFormatter.locale = Locale(identifier: "en_US_POSIX")
  158. dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZZZZZ"
  159. let lessDateString = dateFormatter.string(from: Date())
  160. let requestBody = String(format: requestBodyRecent, "/files/" + account.userId, lessDateString)
  161. // LOG
  162. let levelLog = CCUtility.getLogLevel()
  163. let isSimulatorOrTestFlight = NCUtility.shared.isSimulatorOrTestFlight()
  164. let versionNextcloudiOS = String(format: NCBrandOptions.shared.textCopyrightNextcloudiOS, NCUtility.shared.getVersionApp())
  165. NKCommon.shared.levelLog = levelLog
  166. if let pathDirectoryGroup = CCUtility.getDirectoryGroup()?.path {
  167. NKCommon.shared.pathLog = pathDirectoryGroup
  168. }
  169. if isSimulatorOrTestFlight {
  170. NKCommon.shared.writeLog("Start \(NCBrandOptions.shared.brand) widget session with level \(levelLog) " + versionNextcloudiOS + " (Simulator / TestFlight)")
  171. } else {
  172. NKCommon.shared.writeLog("Start \(NCBrandOptions.shared.brand) widget session with level \(levelLog) " + versionNextcloudiOS)
  173. }
  174. NextcloudKit.shared.searchBodyRequest(serverUrl: account.urlBase, requestBody: requestBody, showHiddenFiles: CCUtility.getShowHiddenFiles()) { _, files, data, error in
  175. var datas: [FilesData] = []
  176. for file in files {
  177. guard !file.directory else { continue }
  178. guard !isLive(file: file, files: files) else { continue }
  179. let subTitle = CCUtility.dateDiff(file.date as Date) + " · " + CCUtility.transformedSize(file.size)
  180. // url: nextcloud://open-file?path=Talk/IMG_0000123.jpg&user=marinofaggiana&link=https://cloud.nextcloud.com/f/123
  181. guard var path = NCUtilityFileSystem.shared.getPath(path: file.path, user: file.user, fileName: file.fileName).urlEncoded else { continue }
  182. if path.first == "/" { path = String(path.dropFirst())}
  183. guard let user = file.user.urlEncoded else { continue }
  184. let link = file.urlBase + "/f/" + file.fileId
  185. let urlString = "nextcloud://open-file?path=\(path)&user=\(user)&link=\(link)"
  186. guard let url = URL(string: urlString) else { continue }
  187. // Build Recent Data
  188. var imageRecent = UIImage()
  189. if let image = NCUtility.shared.createFilePreviewImage(ocId: file.ocId, etag: file.etag, fileNameView: file.fileName, classFile: file.classFile, status: 0, createPreviewMedia: false) {
  190. imageRecent = image
  191. } else if !file.iconName.isEmpty {
  192. imageRecent = UIImage(named: file.iconName)!
  193. } else {
  194. imageRecent = UIImage(named: "file")!
  195. }
  196. let data = FilesData.init(id: file.ocId, image: imageRecent, title: file.fileName, subTitle: subTitle, url: url)
  197. datas.append(data)
  198. if datas.count == items { break}
  199. }
  200. if error != .success {
  201. completion(FilesDataEntry(date: Date(), datas: datasPlaceholder, isPlaceholder: true, tile: title, footerImage: "xmark.icloud", footerText: error.errorDescription))
  202. } else if datas.isEmpty {
  203. var footerText = NSLocalizedString("_no_data_available_", comment: "")
  204. let serverVersionMajor = NCManageDatabase.shared.getCapabilitiesServerInt(account: account.account, elements: NCElementsJSON.shared.capabilitiesVersionMajor)
  205. if serverVersionMajor < NCGlobal.shared.nextcloudVersion25 {
  206. footerText = NSLocalizedString("_widget_available_nc25_", comment: "")
  207. }
  208. completion(FilesDataEntry(date: Date(), datas: datasPlaceholder, isPlaceholder: true, tile: title, footerImage: "checkmark.icloud", footerText: footerText))
  209. } else {
  210. completion(FilesDataEntry(date: Date(), datas: datas, isPlaceholder: false, tile: title, footerImage: "checkmark.icloud", footerText: NCBrandOptions.shared.brand + " files"))
  211. }
  212. }
  213. }