summaryrefslogtreecommitdiffstats
blob: 8d467fd90ba2e2617e4c8c56ad593f99ca9da572 (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
/*
 * Copyright (c) 2013 Tobias Markmann
 * Licensed under the simplified BSD license.
 * See Documentation/Licenses/BSD-simplified.txt for more information.
 */

/*
 * Copyright (c) 2016 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */

#include <Swift/QtUI/Roster/RosterTooltip.h>

#include <QApplication>
#include <QObject>
#include <QString>

#include <Swiften/Base/Path.h>

#include <Swift/Controllers/Roster/ContactRosterItem.h>
#include <Swift/Controllers/StatusUtil.h>
#include <Swift/Controllers/Translator.h>

#include <Swift/QtUI/QtResourceHelper.h>
#include <Swift/QtUI/QtScaledAvatarCache.h>
#include <Swift/QtUI/QtSwiftUtil.h>
#include <Swift/QtUI/QtUtilities.h>

using namespace QtUtilities;

namespace Swift {

QString RosterTooltip::buildDetailedTooltip(ContactRosterItem* contact, QtScaledAvatarCache* cachedImageScaler) {
    QString tooltipTemplate;
    if (QApplication::layoutDirection() == Qt::RightToLeft) {
        tooltipTemplate = QString(
            "<table style='white-space:pre'>"
                "<tr>"
                    "<td>"
                        "<img src=\"%1\" />"
                    "</td>"
                    "<td>"
                        "<p style='font-size: 14px'>%3 %2</p>"
                        "<table><tr><td valign='middle'>%5</td><td valign='middle'>%4</td></tr></table>"
                        "%6"
                        "%7"
                        "%8"
                        "%9"
                    "</td>"
                "</tr>"
            "</table>");
    } else {
        tooltipTemplate = QString(
            "<table style='white-space:pre'>"
                "<tr>"
                    "<td>"
                        "<img src=\"%1\" />"
                    "</td>"
                    "<td>"
                        "<p style='font-size: 14px'>%2 %3</p>"
                        "<table><tr><td valign='middle'>%4</td><td valign='middle'>%5</td></tr></table>"
                        "%6"
                        "%7"
                        "%8"
                        "%9"
                    "</td>"
                "</tr>"
            "</table>");
    }
    // prepare tooltip
    QString fullName = P2QSTRING(contact->getDisplayName());

    QString vCardSummary;
    VCard::ref vCard = contact->getVCard();
    if (vCard) {
        fullName = P2QSTRING(vCard->getFullName()).trimmed();
        if (fullName.isEmpty()) {
            fullName = (P2QSTRING(vCard->getGivenName()) + " " + P2QSTRING(vCard->getFamilyName())).trimmed();
        }
        if (fullName.isEmpty()) {
            fullName = P2QSTRING(contact->getDisplayName());
        }
        vCardSummary = buildVCardSummary(vCard);
    } else {
        contact->onVCardRequested();
    }

    QString scaledAvatarPath = cachedImageScaler->getScaledAvatarPath(P2QSTRING(contact->getAvatarPath().empty() ? ":/icons/avatar.svg" : pathToString(contact->getAvatarPath())));

    QString bareJID = contact->getDisplayJID().toString().empty() ? "" : "( " + P2QSTRING(contact->getDisplayJID().toString()) + " )";

    QString presenceIconTag = QString("<img src='%1' />").arg(statusShowTypeToIconPath(contact->getStatusShow()));

    QString statusMessage = contact->getStatusText().empty() ? QObject::tr("(No message)") : P2QSTRING(contact->getStatusText());

    boost::posix_time::ptime idleTime = contact->getIdle();
    QString idleString;
    if (!idleTime.is_not_a_date_time()) {
        idleString = QObject::tr("Idle since %1").arg(P2QSTRING(Swift::Translator::getInstance()->ptimeToHumanReadableString(idleTime)));
        idleString = htmlEscape(idleString) + "<br/>";
    }

    boost::posix_time::ptime lastSeenTime = contact->getOfflineSince();
    QString lastSeen;
    if (!lastSeenTime.is_not_a_date_time()) {
        lastSeen = QObject::tr("Last seen %1").arg(P2QSTRING(Swift::Translator::getInstance()->ptimeToHumanReadableString(lastSeenTime)));
        lastSeen = htmlEscape(lastSeen) + "<br/>";
    }

    QString mucOccupant= P2QSTRING(contact->getMUCAffiliationText());
    if (!mucOccupant.isEmpty()) {
        mucOccupant = htmlEscape(mucOccupant) + "<br/>";
    }

    return tooltipTemplate.arg(scaledAvatarPath, htmlEscape(fullName), htmlEscape(bareJID), presenceIconTag, htmlEscape(statusMessage), mucOccupant, idleString, lastSeen, vCardSummary);
}

QString RosterTooltip::buildVCardSummary(VCard::ref vcard) {
    QString summary;
    summary = "<table>";

    // star | name | content
    QString currentBlock;
    foreach (const VCard::Telephone& tel, vcard->getTelephones()) {
        QString type = tel.isFax ? QObject::tr("Fax") : QObject::tr("Telephone");
        QString field = buildVCardField(tel.isPreferred, type, htmlEscape(P2QSTRING(tel.number)));
        if (tel.isPreferred) {
            currentBlock = field;
            break;
        }
        currentBlock += field;
    }
    summary += currentBlock;

    currentBlock = "";
    foreach (const VCard::EMailAddress& mail, vcard->getEMailAddresses()) {
        QString field = buildVCardField(mail.isPreferred, QObject::tr("E-Mail"), htmlEscape(P2QSTRING(mail.address)));
        if (mail.isPreferred) {
            currentBlock = field;
            break;
        }
        currentBlock += field;
    }
    summary += currentBlock;

    currentBlock = "";
    foreach (const VCard::Organization& org, vcard->getOrganizations()) {
        QString field = buildVCardField(false, QObject::tr("Organization"), htmlEscape(P2QSTRING(org.name)));
        currentBlock += field;
    }
    summary += currentBlock;

    currentBlock = "";
    foreach(const std::string& title, vcard->getTitles()) {
        QString field = buildVCardField(false, QObject::tr("Title"), htmlEscape(P2QSTRING(title)));
        currentBlock += field;
    }
    summary += currentBlock;

    summary += "</table>";
    return summary;
}

QString RosterTooltip::buildVCardField(bool preferred, const QString& name, const QString& content) {
    QString rowTemplate;
    if (QApplication::layoutDirection() == Qt::RightToLeft) {
        rowTemplate = QString("<tr><td>%3</td><td valign='middle'><strong>%2</strong></td><td valign='middle'>%1</td></tr>");
    } else {
        rowTemplate = QString("<tr><td>%1</td><td valign='middle'><strong>%2</strong></td><td valign='middle'>%3</td></tr>");
    }
    return  rowTemplate.arg(preferred ? "<img src=':/icons/star-checked.png' />" : "", name, content);
}

}