File indexing completed on 2024-04-28 04:55:45
0001 /* 0002 This file is part of Choqok, the KDE micro-blogging client 0003 0004 SPDX-FileCopyrightText: 2017 Andrea Scarpino <scarpino@kde.org> 0005 0006 SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL 0007 */ 0008 0009 #include "mastodonpostwidget.h" 0010 0011 #include <QAction> 0012 #include <QMenu> 0013 #include <QPushButton> 0014 0015 #include <KLocalizedString> 0016 0017 #include "mediamanager.h" 0018 #include "textbrowser.h" 0019 0020 #include "mastodonaccount.h" 0021 #include "mastodondebug.h" 0022 #include "mastodonmicroblog.h" 0023 #include "mastodonpost.h" 0024 0025 const QIcon MastodonPostWidget::unFavIcon(Choqok::MediaManager::convertToGrayScale(QIcon::fromTheme(QLatin1String("rating")).pixmap(16))); 0026 0027 class MastodonPostWidget::Private 0028 { 0029 public: 0030 QPushButton *btnFavorite; 0031 }; 0032 0033 MastodonPostWidget::MastodonPostWidget(Choqok::Account *account, Choqok::Post *post, 0034 QWidget *parent): 0035 PostWidget(account, post, parent), d(new Private) 0036 { 0037 } 0038 0039 MastodonPostWidget::~MastodonPostWidget() 0040 { 0041 delete d; 0042 } 0043 0044 QString MastodonPostWidget::generateSign() 0045 { 0046 QString ss; 0047 0048 MastodonPost *post = dynamic_cast<MastodonPost * >(currentPost()); 0049 MastodonAccount *account = qobject_cast<MastodonAccount * >(currentAccount()); 0050 MastodonMicroBlog *microblog = qobject_cast<MastodonMicroBlog * >(account->microblog()); 0051 if (post) { 0052 ss += QStringLiteral("<b>%1 - </b>").arg(getUsernameHyperlink(currentPost()->author)); 0053 0054 QDateTime time; 0055 if (post->repeatedDateTime.isNull()) { 0056 time = post->creationDateTime; 0057 } else { 0058 time = post->repeatedDateTime; 0059 } 0060 0061 ss += QStringLiteral("<a href=\"%1\" title=\"%2\">%3</a>").arg(post->link.toDisplayString()) 0062 .arg(post->creationDateTime.toString(Qt::DefaultLocaleLongDate)) 0063 .arg(QStringLiteral("%1")); 0064 0065 if (!post->source.isEmpty()) { 0066 ss += QLatin1String(" - ") + post->source; 0067 } 0068 0069 //ReTweet detection 0070 if (!currentPost()->repeatedFromUser.userName.isEmpty()) { 0071 const QString retweet = QLatin1String("<br/>") + 0072 microblog->generateRepeatedByUserTooltip(QStringLiteral("<a href=\"%1\">%2</a>") 0073 .arg(currentPost()->repeatedFromUser.homePageUrl.toDisplayString()) 0074 .arg(microblog->userNameFromAcct(currentPost()->repeatedFromUser.userName))); 0075 ss.append(retweet); 0076 } 0077 } else { 0078 qCDebug(CHOQOK) << "post is not a MastodonPost!"; 0079 } 0080 0081 return ss; 0082 } 0083 0084 QString MastodonPostWidget::getUsernameHyperlink(const Choqok::User &user) const 0085 { 0086 return QStringLiteral("<a href=\"%1\" title=\"%2\">%3</a>") 0087 .arg(user.homePageUrl.toDisplayString()) 0088 .arg(user.description.isEmpty() ? user.realName : user.description.toHtmlEscaped()) 0089 .arg(MastodonMicroBlog::userNameFromAcct(user.userName)); 0090 } 0091 0092 void MastodonPostWidget::initUi() 0093 { 0094 Choqok::UI::PostWidget::initUi(); 0095 0096 if (isResendAvailable()) { 0097 buttons().value(QLatin1String("btnResend"))->setToolTip(i18nc("@info:tooltip", "Boost")); 0098 } 0099 0100 QPushButton *btnRe = addButton(QLatin1String("btnReply"), i18nc("@info:tooltip", "Reply"), QLatin1String("edit-undo")); 0101 connect(btnRe, &QPushButton::clicked, this, &MastodonPostWidget::slotReply); 0102 QMenu *menu = new QMenu(btnRe); 0103 btnRe->setMenu(menu); 0104 0105 QAction *actRep = new QAction(QIcon::fromTheme(QLatin1String("edit-undo")), i18n("Reply to %1", currentPost()->author.userName), menu); 0106 menu->addAction(actRep); 0107 menu->setDefaultAction(actRep); 0108 connect(actRep, &QAction::triggered, this, &MastodonPostWidget::slotReply); 0109 0110 QAction *actWrite = new QAction(QIcon::fromTheme(QLatin1String("document-edit")), i18n("Write to %1", currentPost()->author.userName), menu); 0111 menu->addAction(actWrite); 0112 connect(actWrite, &QAction::triggered, this, &MastodonPostWidget::slotWriteTo); 0113 0114 if (!currentPost()->isPrivate) { 0115 QAction *actReplytoAll = new QAction(i18n("Reply to all"), menu); 0116 menu->addAction(actReplytoAll); 0117 connect(actReplytoAll, &QAction::triggered, this, &MastodonPostWidget::slotReplyToAll); 0118 } 0119 0120 d->btnFavorite = addButton(QLatin1String("btnFavorite"), i18nc("@info:tooltip", "Favourite"), QLatin1String("rating")); 0121 d->btnFavorite->setCheckable(true); 0122 connect(d->btnFavorite, &QPushButton::clicked, this, &MastodonPostWidget::toggleFavorite); 0123 updateFavStat(); 0124 } 0125 0126 void MastodonPostWidget::slotReply() 0127 { 0128 setReadWithSignal(); 0129 if (currentPost()->isPrivate) { 0130 MastodonAccount *account = qobject_cast<MastodonAccount *>(currentAccount()); 0131 MastodonMicroBlog *microblog = qobject_cast<MastodonMicroBlog * >(account->microblog()); 0132 microblog->showDirectMessageDialog(account, currentPost()->author.userName); 0133 } else { 0134 QString replyto = QStringLiteral("@%1").arg(currentPost()->author.userName); 0135 QString postId = currentPost()->postId; 0136 QString username = currentPost()->author.userName; 0137 if (!currentPost()->repeatedFromUser.userName.isEmpty()) { 0138 replyto.prepend(QStringLiteral("@%1 ").arg(currentPost()->repeatedFromUser.userName)); 0139 postId = currentPost()->repeatedPostId; 0140 } 0141 Q_EMIT reply(replyto, postId, username); 0142 } 0143 } 0144 0145 void MastodonPostWidget::slotWriteTo() 0146 { 0147 Q_EMIT reply(QStringLiteral("@%1").arg(currentPost()->author.userName), QString(), currentPost()->author.userName); 0148 } 0149 0150 void MastodonPostWidget::slotReplyToAll() 0151 { 0152 QString txt = QStringLiteral("@%1").arg(currentPost()->author.userName); 0153 Q_EMIT reply(txt, currentPost()->postId, currentPost()->author.userName); 0154 } 0155 0156 void MastodonPostWidget::slotResendPost() 0157 { 0158 qCDebug(CHOQOK); 0159 setReadWithSignal(); 0160 MastodonMicroBlog *microBlog = qobject_cast<MastodonMicroBlog *>(currentAccount()->microblog()); 0161 microBlog->toggleReblog(currentAccount(), currentPost()); 0162 } 0163 0164 void MastodonPostWidget::toggleFavorite() 0165 { 0166 qCDebug(CHOQOK); 0167 setReadWithSignal(); 0168 MastodonMicroBlog *microBlog = qobject_cast<MastodonMicroBlog *>(currentAccount()->microblog()); 0169 connect(microBlog, &MastodonMicroBlog::favorite, this, &MastodonPostWidget::slotToggleFavorite); 0170 microBlog->toggleFavorite(currentAccount(), currentPost()); 0171 } 0172 0173 void MastodonPostWidget::slotToggleFavorite(Choqok::Account *, Choqok::Post *) 0174 { 0175 qCDebug(CHOQOK); 0176 updateFavStat(); 0177 } 0178 0179 void MastodonPostWidget::updateFavStat() 0180 { 0181 d->btnFavorite->setChecked(currentPost()->isFavorited); 0182 if (currentPost()->isFavorited) { 0183 d->btnFavorite->setIcon(QIcon::fromTheme(QLatin1String("rating"))); 0184 } else { 0185 d->btnFavorite->setIcon(unFavIcon); 0186 } 0187 } 0188 0189 #include "moc_mastodonpostwidget.cpp"