File indexing completed on 2024-04-28 04:55:47
0001 /* 0002 This file is part of Choqok, the KDE micro-blogging client 0003 0004 SPDX-FileCopyrightText: 2013-2014 Andrea Scarpino <scarpino@kde.org> 0005 SPDX-FileCopyrightText: 2008-2012 Mehrdad Momeny <mehrdad.momeny@gmail.com> 0006 0007 SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL 0008 */ 0009 0010 #include "pumpiopostwidget.h" 0011 0012 #include <QAction> 0013 #include <QMenu> 0014 #include <QPushButton> 0015 0016 #include <KLocalizedString> 0017 0018 #include "mediamanager.h" 0019 #include "textbrowser.h" 0020 0021 #include "pumpioaccount.h" 0022 #include "pumpiodebug.h" 0023 #include "pumpiomicroblog.h" 0024 #include "pumpiopost.h" 0025 #include "pumpioshowthread.h" 0026 0027 const QIcon PumpIOPostWidget::unFavIcon(Choqok::MediaManager::convertToGrayScale(QIcon::fromTheme(QLatin1String("rating")).pixmap(16))); 0028 0029 class PumpIOPostWidget::Private 0030 { 0031 public: 0032 QPushButton *btnFavorite; 0033 QPushButton *btnReply; 0034 }; 0035 0036 PumpIOPostWidget::PumpIOPostWidget(Choqok::Account *account, Choqok::Post *post, 0037 QWidget *parent): 0038 PostWidget(account, post, parent), d(new Private) 0039 { 0040 mainWidget()->document()->addResource(QTextDocument::ImageResource, 0041 QUrl(QLatin1String("icon://thread")), 0042 QIcon::fromTheme(QLatin1String("go-top")).pixmap(10)); 0043 } 0044 0045 PumpIOPostWidget::~PumpIOPostWidget() 0046 { 0047 delete d; 0048 } 0049 0050 void PumpIOPostWidget::checkAnchor(const QUrl &url) 0051 { 0052 if (url.scheme() == QLatin1String("thread")) { 0053 PumpIOShowThread *thread = new PumpIOShowThread(currentAccount(), currentPost()); 0054 connect(thread, &PumpIOShowThread::forwardReply, this, &PumpIOPostWidget::reply); 0055 thread->resize(this->width(), thread->height() * 3); 0056 thread->show(); 0057 } else { 0058 Choqok::UI::PostWidget::checkAnchor(url); 0059 } 0060 } 0061 0062 QString PumpIOPostWidget::generateSign() 0063 { 0064 QString ss; 0065 0066 PumpIOPost *post = dynamic_cast<PumpIOPost * >(currentPost()); 0067 PumpIOAccount *account = qobject_cast<PumpIOAccount * >(currentAccount()); 0068 PumpIOMicroBlog *microblog = qobject_cast<PumpIOMicroBlog * >(account->microblog()); 0069 if (post) { 0070 ss += QStringLiteral("<b>%1 - </b>").arg(getUsernameHyperlink(currentPost()->author)); 0071 0072 QDateTime time; 0073 if (currentPost()->repeatedDateTime.isNull()) { 0074 time = currentPost()->creationDateTime; 0075 } else { 0076 time = currentPost()->repeatedDateTime; 0077 } 0078 0079 ss += QStringLiteral("<a href=\"%1\" title=\"%2\">%3</a>").arg(currentPost()->link.toDisplayString()) 0080 .arg(time.toString(Qt::DefaultLocaleLongDate)).arg(QStringLiteral("%1")); 0081 0082 if (!post->source.isEmpty()) { 0083 ss += QLatin1String(" - ") + post->source; 0084 } 0085 0086 const QRegExp followers(QLatin1String("/api/user/\\w+/followers")); 0087 if (!post->to.isEmpty()) { 0088 ss += QLatin1String(" - "); 0089 ss += i18n("To:") + QLatin1Char(' '); 0090 0091 for (const QString &id: post->to) { 0092 if (id == PumpIOMicroBlog::PublicCollection) { 0093 ss += i18n("Public") + QLatin1String(", "); 0094 } else if (followers.indexIn(id) != -1) { 0095 ss += QLatin1String("<a href=\"") + QString(id).remove(QLatin1String("/api/user")) + QLatin1String("\">") 0096 + i18n("Followers") + QLatin1String("</a>, "); 0097 } else if (id == QLatin1String("acct:") + account->webfingerID()) { 0098 ss += i18n("You") + QLatin1String(", "); 0099 } else { 0100 ss += QLatin1String("<a href=\"") + microblog->profileUrl(account, post->author.userName).toDisplayString() 0101 + QLatin1String("\">") + PumpIOMicroBlog::userNameFromAcct(id) + QLatin1String("</a>, "); 0102 } 0103 } 0104 0105 if (ss.endsWith(QLatin1String(", "))) { 0106 ss.chop(2); 0107 } 0108 } 0109 0110 if (!post->cc.isEmpty()) { 0111 ss += QLatin1String(" - "); 0112 ss += i18n("CC:") + QLatin1Char(' '); 0113 0114 for (const QString &id: post->cc) { 0115 if (id == PumpIOMicroBlog::PublicCollection) { 0116 ss += i18n("Public") + QLatin1String(", "); 0117 } else if (followers.indexIn(id) != -1) { 0118 ss += QLatin1String("<a href=\"") + QString(id).remove(QLatin1String("/api/user")) + QLatin1String("\">") 0119 + i18n("Followers") + QLatin1String("</a>, "); 0120 } else if (id == QLatin1String("acct:") + account->webfingerID()) { 0121 ss += i18n("You") + QLatin1String(", "); 0122 } else { 0123 ss += QLatin1String("<a href=\"") + microblog->profileUrl(account, post->author.userName).toDisplayString() 0124 + QLatin1String("\">") + PumpIOMicroBlog::userNameFromAcct(id) + QLatin1String("</a>, "); 0125 } 0126 } 0127 0128 if (ss.endsWith(QLatin1String(", "))) { 0129 ss.chop(2); 0130 } 0131 } 0132 0133 if (!post->shares.isEmpty()) { 0134 ss += QLatin1String(" - "); 0135 ss += i18n("Shared by:") + QLatin1Char(' '); 0136 0137 for (const QString &id: post->shares) { 0138 if (id == QLatin1String("acct:") + account->webfingerID()) { 0139 ss += i18n("You") + QLatin1String(", "); 0140 } else { 0141 ss += QLatin1String("<a href=\"") + microblog->profileUrl(account, post->author.userName).toDisplayString() 0142 + QLatin1String("\">") + PumpIOMicroBlog::userNameFromAcct(id) + QLatin1String("</a>, "); 0143 } 0144 } 0145 0146 if (ss.endsWith(QLatin1String(", "))) { 0147 ss.chop(2); 0148 } 0149 } 0150 0151 ss += QLatin1String(" <a href=\"thread://\" title=\"") + i18n("Show conversation") + QLatin1String("\"><img src=\"icon://thread\"/></a>"); 0152 } else { 0153 qCDebug(CHOQOK) << "post is not a PumpIOPost!"; 0154 } 0155 0156 return ss; 0157 } 0158 0159 QString PumpIOPostWidget::getUsernameHyperlink(const Choqok::User &user) const 0160 { 0161 return QStringLiteral("<a href=\"%1\" title=\"%2\">%3</a>") 0162 .arg(user.homePageUrl.toDisplayString()) 0163 .arg(user.description.isEmpty() ? user.realName : user.description.toHtmlEscaped()) 0164 .arg(user.userName); 0165 } 0166 0167 void PumpIOPostWidget::initUi() 0168 { 0169 Choqok::UI::PostWidget::initUi(); 0170 0171 if (isResendAvailable()) { 0172 buttons().value(QLatin1String("btnResend"))->setToolTip(i18nc("@info:tooltip", "Share")); 0173 } 0174 0175 if (isReplyAvailable()) { 0176 d->btnReply = addButton(QLatin1String("btnReply"), i18nc("@info:tooltip", "Reply"), QLatin1String("edit-undo")); 0177 QMenu *replyMenu = new QMenu(d->btnReply); 0178 0179 QAction *replyToAct = new QAction(QIcon::fromTheme(QLatin1String("edit-undo")), i18n("Reply to %1", 0180 currentPost()->author.userName), replyMenu); 0181 replyMenu->addAction(replyToAct); 0182 connect(replyToAct, &QAction::triggered, this, &PumpIOPostWidget::slotReplyTo); 0183 connect(d->btnReply, &QPushButton::clicked, this, &PumpIOPostWidget::slotReplyTo); 0184 } 0185 0186 d->btnFavorite = addButton(QLatin1String("btnFavorite"), i18nc("@info:tooltip", "Like"), QLatin1String("rating")); 0187 d->btnFavorite->setCheckable(true); 0188 connect(d->btnFavorite, &QPushButton::clicked, this, &PumpIOPostWidget::toggleFavorite); 0189 updateFavStat(); 0190 } 0191 0192 void PumpIOPostWidget::toggleFavorite() 0193 { 0194 qCDebug(CHOQOK); 0195 setReadWithSignal(); 0196 PumpIOMicroBlog *microBlog = qobject_cast<PumpIOMicroBlog *>(currentAccount()->microblog()); 0197 connect(microBlog, &PumpIOMicroBlog::favorite, this, &PumpIOPostWidget::slotToggleFavorite); 0198 microBlog->toggleFavorite(currentAccount(), currentPost()); 0199 } 0200 0201 void PumpIOPostWidget::slotToggleFavorite(Choqok::Account *, Choqok::Post *) 0202 { 0203 qCDebug(CHOQOK); 0204 updateFavStat(); 0205 } 0206 0207 void PumpIOPostWidget::slotPostError(Choqok::Account *theAccount, Choqok::Post *post, 0208 Choqok::MicroBlog::ErrorType error, 0209 const QString &errorMessage) 0210 { 0211 Q_UNUSED(error) 0212 0213 qCDebug(CHOQOK); 0214 if (theAccount == currentAccount() && post == currentPost()) { 0215 qCDebug(CHOQOK) << errorMessage; 0216 disconnect(currentAccount()->microblog(), &Choqok::MicroBlog::postRemoved, 0217 this, &PumpIOPostWidget::slotCurrentPostRemoved); 0218 disconnect(currentAccount()->microblog(), &Choqok::MicroBlog::errorPost, 0219 this, &PumpIOPostWidget::slotPostError); 0220 } 0221 } 0222 0223 void PumpIOPostWidget::slotResendPost() 0224 { 0225 qCDebug(CHOQOK); 0226 setReadWithSignal(); 0227 PumpIOMicroBlog *microBlog = qobject_cast<PumpIOMicroBlog *>(currentAccount()->microblog()); 0228 microBlog->share(currentAccount(), currentPost()); 0229 } 0230 0231 bool PumpIOPostWidget::isReplyAvailable() 0232 { 0233 return (currentPost()->type != QLatin1String("comment")); 0234 } 0235 0236 bool PumpIOPostWidget::isResendAvailable() 0237 { 0238 return PostWidget::isResendAvailable() && (currentPost()->type != QLatin1String("comment")); 0239 } 0240 0241 void PumpIOPostWidget::slotReplyTo() 0242 { 0243 qCDebug(CHOQOK); 0244 setReadWithSignal(); 0245 PumpIOPost *post = dynamic_cast<PumpIOPost * >(currentPost()); 0246 if (post->type == QLatin1String("comment")) { 0247 Q_EMIT reply(post->replyToPostId, post->replyToUser.userName, post->replyToObjectType); 0248 } else { 0249 Q_EMIT reply(post->postId, PumpIOMicroBlog::userNameFromAcct(post->author.userId), post->type); 0250 } 0251 } 0252 0253 void PumpIOPostWidget::updateFavStat() 0254 { 0255 d->btnFavorite->setChecked(currentPost()->isFavorited); 0256 if (currentPost()->isFavorited) { 0257 d->btnFavorite->setIcon(QIcon::fromTheme(QLatin1String("rating"))); 0258 } else { 0259 d->btnFavorite->setIcon(unFavIcon); 0260 } 0261 } 0262 0263 #include "moc_pumpiopostwidget.cpp"