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

0001 /*
0002  * Person Tooltip
0003  *
0004  * Copyright (C) 2011 David Edmundson <kde@davidedmundson.co.uk>
0005  * Copyright (C) 2013 Martin Klapetek <mklapetek@kde.org>
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 "persontooltip.h"
0023 
0024 #include "ui_persontooltip.h"
0025 #include "ktooltip.h"
0026 
0027 #include <KTp/types.h>
0028 #include <KTp/text-parser.h>
0029 #include <KTp/presence.h>
0030 
0031 #include <QDesktopServices>
0032 #include <QIcon>
0033 #include <QTextDocument>
0034 
0035 #include <KIconLoader>
0036 #include <KLocalizedString>
0037 
0038 bool contactLessThan(const QVariant &left, const QVariant &right)
0039 {
0040     QModelIndex i1 = left.value<QModelIndex>();
0041     QModelIndex i2 = right.value<QModelIndex>();
0042 
0043     return KTp::Presence::sortPriority((Tp::ConnectionPresenceType)i1.data(KTp::ContactPresenceTypeRole).toInt())
0044         < KTp::Presence::sortPriority((Tp::ConnectionPresenceType)i2.data(KTp::ContactPresenceTypeRole).toInt());
0045 }
0046 
0047 //-----------------------------------------------------------------------------
0048 
0049 PersonToolTip::PersonToolTip(const QModelIndex &index) :
0050     QWidget(0),
0051     ui(new Ui::PersonToolTip)
0052 {
0053     ui->setupUi(this);
0054     ui->nameLabel->setText(index.data(Qt::DisplayRole).toString());
0055     ui->avatarLabel->setScaledContents(false);
0056     ui->avatarLabel->setAlignment(Qt::AlignCenter);
0057     ui->contactsLabel->setText(i18n("Contacts"));
0058     ui->contactsWidget->setLayout(new QGridLayout(ui->contactsWidget));
0059     qobject_cast<QGridLayout*>(ui->contactsWidget->layout())->setColumnStretch(1, 500);
0060 
0061     QPixmap avatarPixmap = index.data(KTp::ContactAvatarPixmapRole).value<QPixmap>();
0062     ui->avatarLabel->setPixmap(avatarPixmap.scaled(ui->avatarLabel->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation));
0063 
0064     int smallIconSize = KIconLoader::global()->currentSize(KIconLoader::Small);
0065 
0066     ui->presenceIcon->setPixmap(QIcon::fromTheme(index.data(KTp::ContactPresenceIconRole).toString()).pixmap(smallIconSize, smallIconSize));
0067     ui->presenceLabel->setText(index.data(KTp::ContactPresenceNameRole).toString());
0068     ui->presenceMessageLabel->setText(getTextWithHyperlinks(index.data(KTp::ContactPresenceMessageRole).toString()));
0069 
0070     //collect child indexes for sorting
0071     QList<QVariant> indexes;
0072     for (int i = 0; i < index.model()->rowCount(index); i++) {
0073         indexes << QVariant::fromValue<QModelIndex>(index.child(i, 0));
0074     }
0075 
0076     //sort indexes by presence
0077     qSort(indexes.begin(), indexes.end(), contactLessThan);
0078 
0079     //QLabel row counter
0080     int row = 0;
0081 
0082     Q_FOREACH (const QVariant &v, indexes)
0083     {
0084         QModelIndex i = v.value<QModelIndex>();
0085         QLabel *contactPresenceLabel = new QLabel(ui->contactsWidget);
0086         QIcon presenceIcon = QIcon::fromTheme(i.data(KTp::ContactPresenceIconRole).toString());
0087         contactPresenceLabel->setPixmap(presenceIcon.pixmap(KIconLoader::SizeSmall, KIconLoader::SizeSmall));
0088 
0089         QLabel *contactIdLabel = new QLabel(ui->contactsWidget);
0090         contactIdLabel->setText(i.data(KTp::IdRole).toString());
0091 
0092         qobject_cast<QGridLayout*>(ui->contactsWidget->layout())->addWidget(contactPresenceLabel, row, 0);
0093         qobject_cast<QGridLayout*>(ui->contactsWidget->layout())->addWidget(contactIdLabel, row, 1);
0094 
0095         row++;
0096     }
0097 
0098     connect(ui->presenceMessageLabel, SIGNAL(linkActivated(QString)), this, SLOT(openLink(QString)));
0099 
0100     ui->blockedLabel->setVisible(index.data(KTp::ContactIsBlockedRole).toBool());
0101 }
0102 
0103 PersonToolTip::~PersonToolTip()
0104 {
0105     delete ui;
0106 }
0107 
0108 void PersonToolTip::openLink(QString url)
0109 {
0110     QDesktopServices::openUrl(QUrl(url));
0111     KToolTip::hideTip();
0112 }
0113 
0114 QString PersonToolTip::getTextWithHyperlinks(QString text)
0115 {
0116     KTp::TextUrlData urls = KTp::TextParser::instance()->extractUrlData(text);
0117     QString result;
0118     int position = 0;
0119 
0120     for (int i = 0; i < urls.fixedUrls.size(); ++i) {
0121         QPair<int, int> pair = urls.urlRanges[i];
0122         QString displayLink = text.mid(pair.first, pair.second);
0123         QString fixedLink = urls.fixedUrls[i];
0124 
0125         if (pair.first > position) {
0126             result += QString(text.mid(position, pair.first - position)).toHtmlEscaped();
0127         }
0128 
0129         result += QStringLiteral("<a href=\"%1\">%2</a>").arg(fixedLink.toHtmlEscaped()).arg(displayLink.toHtmlEscaped());
0130         position = pair.first + pair.second;
0131     }
0132 
0133     if (position < text.length()) {
0134         result += QString(text.mid(position).toHtmlEscaped());
0135     }
0136 
0137     return result;
0138 }