From 78fba7391c602b0bca4ed8b61713ae86382e8c0c Mon Sep 17 00:00:00 2001 From: Alexander Heinrich Date: Fri, 6 Aug 2021 11:46:56 +0200 Subject: [PATCH] Checking if the Mail plug-in is installed in the correct version. Otherwise the new mail plug-in will be installed --- .../HaystackApp/FileManager.swift | 10 ++++- .../Mail Plugin/MailPluginManager.swift | 37 +++++++++++++++++-- 2 files changed, 42 insertions(+), 5 deletions(-) diff --git a/OpenHaystack/OpenHaystack/HaystackApp/FileManager.swift b/OpenHaystack/OpenHaystack/HaystackApp/FileManager.swift index 893c0e5..2cdcdfb 100644 --- a/OpenHaystack/OpenHaystack/HaystackApp/FileManager.swift +++ b/OpenHaystack/OpenHaystack/HaystackApp/FileManager.swift @@ -30,8 +30,14 @@ extension FileManager { if isDir.boolValue == true { try self.copyFolder(from: fileURL, to: to.appendingPathComponent(file)) } else { - // Copy file - try FileManager.default.copyItem(at: fileURL, to: to.appendingPathComponent(file)) + do { + // Copy file + try FileManager.default.copyItem(at: fileURL, to: to.appendingPathComponent(file)) + } catch { + if fileURL.lastPathComponent != "CodeResources" { + throw error + } + } } } } diff --git a/OpenHaystack/OpenHaystack/HaystackApp/Mail Plugin/MailPluginManager.swift b/OpenHaystack/OpenHaystack/HaystackApp/Mail Plugin/MailPluginManager.swift index 77a8e31..01e93f6 100644 --- a/OpenHaystack/OpenHaystack/HaystackApp/Mail Plugin/MailPluginManager.swift +++ b/OpenHaystack/OpenHaystack/HaystackApp/Mail Plugin/MailPluginManager.swift @@ -20,8 +20,36 @@ struct MailPluginManager { let pluginURL = FileManager.default.homeDirectoryForCurrentUser.appendingPathComponent("Library/Mail/Bundles").appendingPathComponent(mailBundleName + ".mailbundle") + let localPluginURL = Bundle.main.url(forResource: mailBundleName, withExtension: "mailbundle")! + var isMailPluginInstalled: Bool { - return FileManager.default.fileExists(atPath: pluginURL.path) + //Check if the plug-in is compatible by comparing the IDs + guard FileManager.default.fileExists(atPath: pluginURL.path) else { + return false + } + + let infoPlistURL = pluginURL.appendingPathComponent("Contents/Info.plist") + let localInfoPlistURL = localPluginURL.appendingPathComponent("Contents/Info.plist") + + guard let infoPlistData = try? Data(contentsOf: infoPlistURL), + let infoPlistDict = try? PropertyListSerialization.propertyList(from: infoPlistData, options: [], format: nil) as? [String: AnyHashable], + let localInfoPlistData = try? Data(contentsOf: localInfoPlistURL), + let localInfoPlistDict = try? PropertyListSerialization.propertyList(from: localInfoPlistData, options: [], format: nil) as? [String: AnyHashable] + else { return false } + + //Compare the supported plug-ins + let uuidEntries = localInfoPlistDict.keys.filter({ $0.contains("PluginCompatibilityUUIDs") }) + for uuidEntry in uuidEntries { + guard let localEntry = localInfoPlistDict[uuidEntry] as? [String], + let installedEntry = infoPlistDict[uuidEntry] as? [String] + else { return false } + + if localEntry != installedEntry { + return false + } + } + + return true } /// Shows a NSSavePanel to install the mail plugin at the required place. @@ -58,9 +86,12 @@ struct MailPluginManager { throw PluginError.permissionNotGranted } - let localPluginURL = Bundle.main.url(forResource: mailBundleName, withExtension: "mailbundle")! - do { + //Remove old plug-ins first + if FileManager.default.fileExists(atPath: pluginURL.path) { + try FileManager.default.removeItem(at: pluginURL) + } + try FileManager.default.createDirectory(at: pluginsFolderURL, withIntermediateDirectories: true, attributes: nil) } catch { print(error.localizedDescription)