Skip to content
Snippets Groups Projects
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
PersistentMail +CoreDataClass.swift 6.95 KiB
//
//  PersistentMail+CoreDataClass.swift
//  enzevalos_iphone
//
//  Created by Oliver Wiese on 27/12/16.
//  This program is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program.  If not, see <https://www.gnu.org/licenses/>.
//  This file was automatically generated and should not be edited.
//


import Foundation
import CoreData
/**
A PersistentMail stores all information about one mail:
 - Sender, receivers
 - subject, date
 - body
 - encryption state
 - signature state
 - attached files, keys
 
*/
@objc(PersistentMail)
open class PersistentMail: NSManagedObject, Mail {
    public var predecessor: PersistentMail? = nil

    var showMessage: Bool = false

    var isSecure: Bool {
        return isEncrypted && isSigned && isCorrectlySigned && !unableToDecrypt && !trouble && keyID != nil
    }
    var encState: EncryptionState {
        get{
            if unableToDecrypt {
                return EncryptionState.UnableToDecrypt
            }
            if !isEncrypted {
                return EncryptionState.NoEncryption
            }
            else if self.decryptedWithOldPrivateKey {
                return EncryptionState.ValidEncryptedWithOldKey
            }
            else {
                return EncryptionState.ValidedEncryptedWithCurrentKey
            }
        }
    }
    
    var sigState: SignatureState {
        get{
            if isSigned && isCorrectlySigned {
                return SignatureState.ValidSignature
            }
            else if isSigned && keyID == nil {
                return SignatureState.NoPublicKey
            }
            else if isSigned {
                return SignatureState.InvalidSignature
            }
            else {
                return SignatureState.NoSignature
            }
           
        }
    }
    
    /**
     This the signed first mail of the signed key?
     */
    var isNewPubKey: Bool {
        guard let signedKey = self.signedKey else {
            return false
        }
        
        if signedKey.counterSignedMails < 2 && self.from.publicKeys.count > 1 && signedKey.fingerprint != self.from.primaryKey?.fingerprint  {
            return true
        }
        return false
    }

    /**
     Just a flag if the user read this mail.
     */
    var isRead: Bool {
        get {
            let value = flag.contains(MCOMessageFlag.seen)
            return value
        }
        set {
            if !newValue {
                flag.remove(MCOMessageFlag.seen)
            } else {
                flag.insert(MCOMessageFlag.seen)
            }
            _ = DataHandler.handler.save(during: "set read flag")
        }
    }

    /**
     Just a flag if the user answered the mail.
     */
    var isAnwered: Bool {
        get {
            let value = flag.contains(MCOMessageFlag.answered)
            return value
        }
        set {
            if !newValue {
                flag.remove(MCOMessageFlag.answered)
            } else {
                flag.insert(MCOMessageFlag.answered)
            }
            _ = DataHandler.handler.save(during: "set answer flag")
        }
    }

    var timeString: String {
        var returnString = ""
        let dateFormatter = DateFormatter()
        dateFormatter.locale = Locale.current
        let mailTime = self.date
        let interval = Date().timeIntervalSince(mailTime as Date)
        switch interval {
        case -55..<55:
            returnString = NSLocalizedString("Now", comment: "New email")
        case 55..<120:
            returnString = NSLocalizedString("OneMinuteAgo", comment: "Email came one minute ago")
        case 120..<24 * 60 * 60:
            dateFormatter.timeStyle = .short
            returnString = dateFormatter.string(from: mailTime as Date)
        case 86400..<172800: //24 * 60 * 60..<48 * 60 * 60:
            returnString = NSLocalizedString("Yesterday", comment: "Email came yesterday")
        case 172800..<259200: //48 * 60 * 60..<72 * 60 * 60:
            returnString = NSLocalizedString("TwoDaysAgo", comment: "Email came two days ago")
        default:
            dateFormatter.dateStyle = .short
            returnString = dateFormatter.string(from: mailTime as Date)
        }
        return returnString
    }

    /**
     Just a short part of the body. Can be used for a preview.
     In case of trouble there is no preview.
     */
    var shortBodyString: String? {
        guard !trouble else {
            return nil
        }

        var message: String? = ""
        if isEncrypted && !unableToDecrypt {
            message = decryptedBody
        } else {
            message = body
        }

        if message != nil {
            message = message!.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
            if message!.count > 50 {
                message = String(message![..<message!.index(message!.startIndex, offsetBy: 50)])
            }
            let messageArray = message!.components(separatedBy: "\n")
            return messageArray.joined(separator: " ")
        } else {
            return nil
        }
    }

    var decryptedWithOldPrivateKey: Bool = false

    func getReceivers() -> [Mail_Address] {
        var receivers = [Mail_Address] ()
        for obj in to {
            receivers.append(obj as! Mail_Address)
        }
        return receivers
    }

    func getCCs() -> [Mail_Address] {
        var receivers = [Mail_Address] ()
        for obj in cc! {
            receivers.append(obj as! Mail_Address)
        }
        return receivers
    }

    func getBCCs() -> [Mail_Address] {
        var receivers = [Mail_Address] ()
        for obj in bcc! {
            receivers.append(obj as! Mail_Address)
        }
        return receivers
    }

    func getSubjectWithFlagsString() -> String {
        let subj: String
        var returnString: String = ""
        if self.subject == nil || (self.subject?.isEmpty)! {
            subj = NSLocalizedString("SubjectNo", comment: "This email has no subject")
        } else {
            subj = subject!
        }
        if self.trouble {
            returnString.append("❗️ ")
        }
        if !self.isRead {
            returnString.append("🔵 ")
        }
        if MCOMessageFlag.answered.isSubset(of: flag) {
            returnString.append("↩️ ")
        }
        if MCOMessageFlag.forwarded.isSubset(of: flag) {
            returnString.append("➡️ ")
        }
        if MCOMessageFlag.flagged.isSubset(of: flag) {
            returnString.append("⭐️ ")
        }
        return "\(returnString)\(subj)"
    }
}