Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ContactWriter.kt « vcard4android « bitfire « at « java « main « src - github.com/bitfireAT/vcard4android.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 99ae90d0f3086fa41c2589b3c429de0e14557677 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
package at.bitfire.vcard4android

import at.bitfire.vcard4android.property.*
import at.bitfire.vcard4android.property.CustomScribes.registerCustomScribes
import ezvcard.Ezvcard
import ezvcard.VCard
import ezvcard.VCardVersion
import ezvcard.io.json.JCardWriter
import ezvcard.io.text.VCardWriter
import ezvcard.parameter.ImageType
import ezvcard.parameter.RelatedType
import ezvcard.property.*
import org.apache.commons.lang3.StringUtils
import org.apache.commons.text.WordUtils
import java.io.OutputStream
import java.util.*
import java.util.logging.Level

/**
 * Responsible for converting the [Contact] data class (which is not version-specific)
 * to the vCard that is actually sent to the server.
 *
 * Properties which are not supported by the target vCard version have to be converted appropriately.
 */
class ContactWriter private constructor(val contact: Contact, val version: VCardVersion) {

    private val unknownProperties = LinkedList<VCardProperty>()
    val vCard = VCard()

    /** counter for item ID of labelled properties: 1 means "item1." etc. */
    private var currentItemId = 1

    companion object {

        fun fromContact(contact: Contact, version: VCardVersion) =
            ContactWriter(contact, version)

    }

    init {
        parseUnknownProperties()
        addProperties()
    }

    private fun addProperties() {
        contact.uid?.let { vCard.uid = Uid(it) }
        Contact.productID?.let { vCard.setProductId(it) }

        addKindAndMembers()

        addFormattedName()
        addStructuredName()
        addPhoneticName()
        contact.nickName?.let { nickName -> addLabeledProperty(nickName) }

        if (contact.categories.isNotEmpty())
            vCard.addCategories(Categories().apply {
                values.addAll(contact.categories)
            })

        addOrganization()

        for (phone in contact.phoneNumbers)
            addLabeledProperty(phone)
        for (email in contact.emails)
            addLabeledProperty(email)
        for (impp in contact.impps)
            addLabeledProperty(impp)
        for (url in contact.urls)
            addLabeledProperty(url)

        for (address in contact.addresses)
            addLabeledProperty(address)

        addDates()

        for (relation in contact.relations)
            addRelation(relation)

        contact.note?.let { note -> vCard.addNote(note) }

        for (unknownProperty in unknownProperties)
            vCard.addProperty(unknownProperty)

        contact.photo?.let { photo -> vCard.addPhoto(Photo(photo, ImageType.JPEG)) }

        vCard.revision = Revision.now()
    }

    private fun addDates() {
        contact.birthDay?.let { birthday ->
            // vCard3 doesn't support partial dates
            if (version == VCardVersion.V3_0)
                rewritePartialDate(birthday)

            vCard.birthday = birthday
        }

        contact.anniversary?.let { anniversary ->
            if (version == VCardVersion.V4_0) {
                vCard.anniversary = anniversary

            } else /* version == VCardVersion.V3_0 */ {
                // vCard3 doesn't support partial dates
                rewritePartialDate(anniversary)
                // vCard3 doesn't support ANNIVERSARY, rewrite to X-ABDATE
                addLabeledProperty(LabeledProperty(XAbDate(anniversary.date), XAbLabel.APPLE_ANNIVERSARY))
                vCard.anniversary = null
            }
        }

        for (customDate in contact.customDates) {
            rewritePartialDate(customDate.property)
            addLabeledProperty(customDate)
        }
    }

    private fun addFormattedName() {
        // vCard 3 REQUIRES FN [RFC 2426 p. 29]
        // vCard 4 REQUIRES FN [RFC 6350 6.2.1 FN]
        val fn =
            // use display name, if available
            StringUtils.trimToNull(contact.displayName) ?:
            // no display name, try organization
            contact.organization?.values?.joinToString(" / ") ?:
            // otherwise, try nickname
            contact.nickName?.property?.values?.firstOrNull() ?:
            // otherwise, try email address
            contact.emails.firstOrNull()?.property?.value ?:
            // otherwise, try phone number
            contact.phoneNumbers.firstOrNull()?.property?.text ?:
            // otherwise, try UID or use empty string
            contact.uid ?: ""
        vCard.setFormattedName(fn)
    }

    private fun addKindAndMembers() {
        if (contact.group) {
            if (version == VCardVersion.V4_0) {         // vCard4
                vCard.kind = Kind.group()
                for (member in contact.members)
                    vCard.addMember(Member("urn:uuid:$member"))
            } else {                                    // "vCard4 as vCard3" (Apple-style)
                vCard.setProperty(XAddressBookServerKind(Kind.GROUP))
                for (member in contact.members)
                    vCard.addProperty(XAddressBookServerMember("urn:uuid:$member"))
            }
        }
    }

    private fun addOrganization() {
        contact.organization?.let { vCard.organization = it }
        contact.jobTitle?.let { vCard.addTitle(it) }
        contact.jobDescription?.let { vCard.addRole(it) }
    }

    private fun addRelation(relation: Related) {
        if (version == VCardVersion.V4_0)
            vCard.addRelated(relation)

        else /* version == VCardVersion.V3_0 */ {
            val name = XAbRelatedNames(relation.text ?: relation.uri)
            var label: String? = null

            val types = LinkedList(relation.types)
            types.remove(CustomType.Related.OTHER)       // ignore this type (has to be inserted by ContactReader when no type is set)

            when {
                types.contains(CustomType.Related.ASSISTANT) ->
                    label = XAbRelatedNames.APPLE_ASSISTANT
                types.contains(CustomType.Related.BROTHER) ->
                    label = XAbRelatedNames.APPLE_BROTHER
                types.contains(RelatedType.CHILD) ->
                    label = XAbRelatedNames.APPLE_CHILD
                types.contains(CustomType.Related.FATHER) ->
                    label = XAbRelatedNames.APPLE_FATHER
                types.contains(RelatedType.FRIEND) ->
                    label = XAbRelatedNames.APPLE_FRIEND
                types.contains(CustomType.Related.MANAGER) ->
                    label = XAbRelatedNames.APPLE_MANAGER
                types.contains(CustomType.Related.MOTHER) ->
                    label = XAbRelatedNames.APPLE_MOTHER
                types.contains(RelatedType.PARENT) ->
                    label = XAbRelatedNames.APPLE_PARENT
                types.contains(CustomType.Related.PARTNER) ->
                    label = XAbRelatedNames.APPLE_PARTNER
                types.contains(CustomType.Related.SISTER) ->
                    label = XAbRelatedNames.APPLE_SISTER
                types.contains(RelatedType.SPOUSE) ->
                    label = XAbRelatedNames.APPLE_SPOUSE

                else -> {
                    if (relation.types.isEmpty())
                        name.addParameter("TYPE", "other")
                    else
                        label = relation.types.joinToString(", ") { type -> WordUtils.capitalize(type.value) }
                }
            }

            addLabeledProperty(LabeledProperty(name, label))
        }
    }

    private fun addStructuredName() {
        val n = StructuredName()

        contact.prefix?.let { prefixesStr ->
            for (prefix in prefixesStr.split(' '))
                n.prefixes += prefix
        }

        n.given = contact.givenName
        contact.middleName?.let { middleNamesStr ->
            for (middleName in middleNamesStr.split(' '))
                n.additionalNames += middleName
        }
        n.family = contact.familyName

        contact.suffix?.let { suffixesStr ->
            for (suffix in suffixesStr.split(' '))
                n.suffixes += suffix
        }

        if (version == VCardVersion.V4_0) {
            // add N only if there's some data in it
            if (n.prefixes.isNotEmpty() || n.given != null || n.additionalNames.isNotEmpty() || n.family != null || n.suffixes.isNotEmpty())
                vCard.structuredName = n

        } else /* version == VCardVersion.V3_0 */ {
            // vCard 3 REQUIRES N [RFC 2426 p. 29]
            vCard.structuredName = n
        }
    }

    private fun addPhoneticName() {
        contact.phoneticGivenName?.let { firstName ->
            vCard.addProperty(XPhoneticFirstName(firstName))
        }
        contact.phoneticMiddleName?.let { middleName ->
            vCard.addProperty(XPhoneticMiddleName(middleName))
        }
        contact.phoneticFamilyName?.let { lastName ->
            vCard.addProperty(XPhoneticLastName(lastName))
        }
    }


    // helpers

    fun addLabeledProperty(labeledProperty: LabeledProperty<*>) {
        val property = labeledProperty.property

        if (labeledProperty.label != null) {
            // property with label -> group property and label with item ID
            val itemId = getNextItemId()

            // 1. add property with item ID
            property.group = itemId

            // 2. add label with same item ID
            val label = XAbLabel(labeledProperty.label)
            label.group = itemId
            vCard.addProperty(label)
        }

        vCard.addProperty(property)
    }

    private fun getNextItemId(): String {
        var id: String

        // increase ID until there is one which is not already used by an unknown property
        do {
            id = "item${currentItemId++}"
        } while (unknownProperties.any { it.group == id })

        return id
    }

    private fun parseUnknownProperties() {
        try {
            contact.unknownProperties?.let {
                Ezvcard.parse(it).first()?.let { vCard ->
                    unknownProperties.addAll(vCard.properties)
                }
            }
        } catch (e: Exception) {
            Constants.log.log(Level.WARNING, "Couldn't parse original vCard with retained properties", e)
        }
    }

    fun<T: DateOrTimeProperty> rewritePartialDate(prop: T) {
        // vCard 3 doesn't understand partial dates:
        //   1. the syntax is different (vCard 3: 2021-03-07, partial date: 20210307)
        //   2. vCard 3 doesn't understand dates without year
        val partial = prop.partialDate
        if (version == VCardVersion.V3_0 && prop.date == null && partial != null) {
            val originalYear = partial.year
            val year = originalYear ?: Contact.DATE_PARAMETER_OMIT_YEAR_DEFAULT

            // use full date format
            val fakeCal = GregorianCalendar.getInstance()
            fakeCal.timeInMillis = 0    // reset everything, including milliseconds
            fakeCal.set(year, partial.month - 1, partial.date, 0, 0, 0)
            prop.setDate(fakeCal, false)

            if (originalYear == null)
                prop.addParameter(Contact.DATE_PARAMETER_OMIT_YEAR, Contact.DATE_PARAMETER_OMIT_YEAR_DEFAULT.toString())
        }
    }


    /**
     * Validates and writes the vCard to an output stream.
     *
     * @param stream    target output stream
     * @param jCard     *true*: write as jCard; *false*: write as vCard
     */
    fun writeCard(stream: OutputStream, jCard: Boolean) {
        validate()

        val writer =
            if (jCard)
                JCardWriter(stream).apply {
                    isAddProdId = Contact.productID == null
                    registerCustomScribes()

                    // allow properties that are not defined in this vCard version
                    isVersionStrict = false
                }
            else
                VCardWriter(stream, version).apply {
                    isAddProdId = Contact.productID == null
                    registerCustomScribes()

                    // include trailing semicolons for maximum compatibility
                    isIncludeTrailingSemicolons = true

                    // use caret encoding for parameter values (RFC 6868)
                    isCaretEncodingEnabled = true

                    // allow properties that are not defined in this vCard version
                    isVersionStrict = false
                }

        writer.write(vCard)
        writer.flush()
    }

    private fun validate() {
        // validate vCard and log results
        val validation = vCard.validate(version)
        if (!validation.isEmpty) {
            val msgs = LinkedList<String>()
            for ((key, warnings) in validation)
                msgs += "  * " + key?.javaClass?.simpleName + " - " + warnings?.joinToString(" | ")
            Constants.log.log(Level.WARNING, "vCard validation warnings", msgs.joinToString(","))
        }
    }

}