aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/core/renderer/MCAddressDisplay.cpp
blob: 1f8518d18a15d0e7cc6596f52ae431c71984f98a (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
//
//  MCAddressDisplay.cpp
//  testUI
//
//  Created by DINH Viêt Hoà on 1/27/13.
//  Copyright (c) 2013 MailCore. All rights reserved.
//

#include "MCAddressDisplay.h"

using namespace mailcore;

String * AddressDisplay::sanitizeDisplayName(String * displayName)
{
    if (displayName->hasPrefix(MCSTR("\"")) && displayName->hasSuffix(MCSTR("\""))) {
        String * unquotedDisplayName = displayName->substringWithRange(RangeMake(1, displayName->length() - 2));
        if (unquotedDisplayName->locationOfString(MCSTR("\"")) != -1) {
            return displayName;
        }
        else {
            return unquotedDisplayName;
        }
    }
    else {
        return displayName;
    }
}

String * AddressDisplay::displayStringForAddress(Address * address)
{
    if (address->displayName() != NULL) {
        return String::stringWithUTF8Format("%s <%s>",
                                            MCUTF8(AddressDisplay::sanitizeDisplayName(address->displayName())),
                                            MCUTF8(address->mailbox()));
    }
    else {
        return address->mailbox();
    }
}

String * AddressDisplay::shortDisplayStringForAddress(Address * address)
{
    if ((address->displayName() != NULL) && (address->displayName()->length() > 0)) {
        return sanitizeDisplayName(address->displayName());
    }
    else if (address->mailbox()) {
        return address->mailbox();
    }
    else {
        return MCSTR("invalid");
    }
}

String * AddressDisplay::veryShortDisplayStringForAddress(Address * address)
{
    if ((address->displayName() != NULL) && (address->displayName()->length() > 0)) {
        Array * components;
        String * senderName;
        
        senderName = sanitizeDisplayName(address->displayName());
        senderName = (String *) senderName->copy()->autorelease();
        
        senderName->replaceOccurrencesOfString(MCSTR(","), MCSTR(" "));
        senderName->replaceOccurrencesOfString(MCSTR("'"), MCSTR(" "));
        senderName->replaceOccurrencesOfString(MCSTR("\""), MCSTR(" "));
        components = senderName->componentsSeparatedByString(MCSTR(" "));
        if (components->count() == 0) {
            return MCLOCALIZEDSTRING(MCSTR("invalid"));
        }
        return (String *) components->objectAtIndex(0);
    }
    else if (address->mailbox()) {
        Array * components = address->mailbox()->componentsSeparatedByString(MCSTR("@"));
        if (components->count() == 0) {
            return MCSTR("");
        }
        return (String *) components->objectAtIndex(0);
    }
    else {
        return MCLOCALIZEDSTRING(MCSTR("invalid"));
    }
}

String * AddressDisplay::displayStringForAddresses(Array * addresses)
{
    String * result = String::string();
    if (addresses == NULL) {
        return result;
    }
    for(unsigned int i = 0 ; i < addresses->count() ; i ++) {
        Address * address = (Address *) addresses->objectAtIndex(i);
        if (i != 0) {
            result->appendString(MCSTR(", "));
        }
        result->appendString(AddressDisplay::displayStringForAddress(address));
    }
    return result;
}

String * AddressDisplay::shortDisplayStringForAddresses(Array * addresses)
{
    String * result = String::string();
    for(unsigned int i = 0 ; i < addresses->count() ; i ++) {
        Address * address = (Address *) addresses->objectAtIndex(i);
        if (i != 0) {
            result->appendString(MCSTR(", "));
        }
        result->appendString(shortDisplayStringForAddress(address));
    }
    return result;
}

String * AddressDisplay::veryShortDisplayStringForAddresses(Array * addresses)
{
    String * result = String::string();
    for(unsigned int i = 0 ; i < addresses->count() ; i ++) {
        Address * address = (Address *) addresses->objectAtIndex(i);
        if (i != 0) {
            result->appendString(MCSTR(", "));
        }
        result->appendString(veryShortDisplayStringForAddress(address));
    }
    return result;
}