File indexing completed on 2022-11-23 11:29:39

0001 /*
0002  * Contact Tooltip
0003  *
0004  * Copyright (C) 2011 David Edmundson <kde@davidedmundson.co.uk>
0005  * Copyright (C) 2011 Geoffry Song <goffrie@gmail.com>
0006  *
0007  * This library is free software; you can redistribute it and/or
0008  * modify it under the terms of the GNU Lesser General Public
0009  * License as published by the Free Software Foundation; either
0010  * version 2.1 of the License, or (at your option) any later version.
0011  *
0012  * This library is distributed in the hope that it will be useful,
0013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0015  * Lesser General Public License for more details.
0016  *
0017  * You should have received a copy of the GNU Lesser General Public
0018  * License along with this library; if not, write to the Free Software
0019  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
0020  */
0021 
0022 #include "contacttooltip.h"
0023 
0024 #include "ui_contacttooltip.h"
0025 #include "ktooltip.h"
0026 #include "ktp-contactlist-debug.h"
0027 
0028 #include <KTp/types.h>
0029 #include <KTp/text-parser.h>
0030 #include <KTp/presence.h>
0031 
0032 #include <QDesktopServices>
0033 #include <QIcon>
0034 #include <QTextDocument>
0035 #include <QDebug>
0036 
0037 #include <KLocalizedString>
0038 #include <KIconLoader>
0039 
0040 ContactToolTip::ContactToolTip(const QModelIndex &index) :
0041     QWidget(0),
0042     ui(new Ui::ContactToolTip)
0043 {
0044     ui->setupUi(this);
0045     ui->nameLabel->setText(index.data(Qt::DisplayRole).toString());
0046     ui->idLabel->setText(index.data(KTp::IdRole).toString());
0047     ui->avatarLabel->setScaledContents(false);
0048     ui->avatarLabel->setAlignment(Qt::AlignCenter);
0049 
0050     QPixmap avatarPixmap(qvariant_cast<QPixmap>(index.data(KTp::ContactAvatarPixmapRole)));
0051     if (!avatarPixmap.isNull()) {
0052         ui->avatarLabel->setPixmap(avatarPixmap.scaled(ui->avatarLabel->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation));
0053     }
0054 
0055     KTp::Presence presence(Tp::Presence((Tp::ConnectionPresenceType)index.data(KTp::ContactPresenceTypeRole).toUInt(),
0056                                         QString(), //the presence name is not needed, saves one call to the model
0057                                         index.data(KTp::ContactPresenceMessageRole).toString()));
0058 
0059     QString presenceMessage = presence.statusMessage();
0060     QString presenceText = presence.displayString();
0061 
0062     if (index.data(KTp::ContactPresenceTypeRole).toInt() == Tp::ConnectionPresenceTypeError) {
0063         presenceText = i18nc("This is an IM user status", "Error Getting Presence");
0064 
0065         /** if the presence is error, the message might containt server's error,
0066         *   so let's print it out and unset it so it won't be displayed to user
0067         */
0068         qCDebug(KTP_CONTACTLIST_MODULE) << presenceMessage;
0069         presenceMessage.clear();
0070     }
0071 
0072     int smallIconSize = KIconLoader::global()->currentSize(KIconLoader::Small);
0073 
0074     ui->presenceIcon->setPixmap(presence.icon().pixmap(smallIconSize, smallIconSize));
0075     ui->presenceLabel->setText(presenceText);
0076     ui->presenceMessageLabel->setText(getTextWithHyperlinks(presenceMessage));
0077     ui->blockedLabel->setVisible(index.data(KTp::ContactIsBlockedRole).toBool());
0078 
0079     const Tp::AccountPtr account = index.data(KTp::AccountRole).value<Tp::AccountPtr>();
0080     if (!account.isNull()) {
0081         ui->accountLabel->setText(i18n("Account: %1", account->displayName()));
0082     }
0083 
0084     connect(ui->presenceMessageLabel, SIGNAL(linkActivated(QString)), this, SLOT(openLink(QString)));
0085 }
0086 
0087 ContactToolTip::~ContactToolTip()
0088 {
0089     delete ui;
0090 }
0091 
0092 void ContactToolTip::openLink(QString url)
0093 {
0094     QDesktopServices::openUrl(QUrl(url));
0095     KToolTip::hideTip();
0096 }
0097 
0098 QString ContactToolTip::getTextWithHyperlinks(QString text)
0099 {
0100     KTp::TextUrlData urls = KTp::TextParser::instance()->extractUrlData(text);
0101     QString result;
0102     int position = 0;
0103 
0104     for (int i = 0; i < urls.fixedUrls.size(); ++i) {
0105         QPair<int, int> pair = urls.urlRanges[i];
0106         QString displayLink = text.mid(pair.first, pair.second);
0107         QString fixedLink = urls.fixedUrls[i];
0108 
0109         if (pair.first > position) {
0110             result += QString(text.mid(position, pair.first - position)).toHtmlEscaped();
0111         }
0112 
0113         result += QStringLiteral("<a href=\"%1\">%2</a>").arg(fixedLink.toHtmlEscaped()).arg(displayLink.toHtmlEscaped());
0114         position = pair.first + pair.second;
0115     }
0116 
0117     if (position < text.length()) {
0118         result += QString(text.mid(position).toHtmlEscaped());
0119     }
0120 
0121     return result;
0122 }