PageRenderTime 98ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/FeedReader/gui/FeedReaderFeedItem.cpp

https://gitlab.com/cave/RetroShare
C++ | 189 lines | 128 code | 36 blank | 25 comment | 19 complexity | 282ddc7faf022761f73841707f894a59 MD5 | raw file
Possible License(s): 0BSD, GPL-2.0
  1. /****************************************************************
  2. * RetroShare is distributed under the following license:
  3. *
  4. * Copyright (C) 2012 by Thunder
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor,
  19. * Boston, MA 02110-1301, USA.
  20. ****************************************************************/
  21. #include <QMenu>
  22. #include <QUrl>
  23. #include <QClipboard>
  24. #include <QDesktopServices>
  25. #include "FeedReaderFeedItem.h"
  26. #include "ui_FeedReaderFeedItem.h"
  27. #include "FeedReaderNotify.h"
  28. #include "util/DateTime.h"
  29. #include "gui/feeds/FeedHolder.h"
  30. /** Constructor */
  31. FeedReaderFeedItem::FeedReaderFeedItem(RsFeedReader *feedReader, FeedReaderNotify *notify, FeedHolder *parent, const FeedInfo &feedInfo, const FeedMsgInfo &msgInfo)
  32. : FeedItem(NULL), mFeedReader(feedReader), mNotify(notify), mParent(parent), ui(new Ui::FeedReaderFeedItem)
  33. {
  34. /* Invoke the Qt Designer generated object setup routine */
  35. ui->setupUi(this);
  36. setAttribute(Qt::WA_DeleteOnClose, true);
  37. connect(ui->expandButton, SIGNAL(clicked(void)), this, SLOT(toggle(void)));
  38. connect(ui->clearButton, SIGNAL(clicked(void)), this, SLOT(removeItem(void)));
  39. connect(ui->readAndClearButton, SIGNAL(clicked()), this, SLOT(readAndClearItem()));
  40. connect(ui->linkButton, SIGNAL(clicked()), this, SLOT(openLink()));
  41. connect(mNotify, SIGNAL(msgChanged(QString,QString,int)), this, SLOT(msgChanged(QString,QString,int)), Qt::QueuedConnection);
  42. ui->expandFrame->hide();
  43. mFeedId = feedInfo.feedId;
  44. mMsgId = msgInfo.msgId;
  45. if (feedInfo.icon.empty()) {
  46. ui->feedIconLabel->hide();
  47. } else {
  48. /* use icon from feed */
  49. QPixmap pixmap;
  50. if (pixmap.loadFromData(QByteArray::fromBase64(feedInfo.icon.c_str()))) {
  51. ui->feedIconLabel->setPixmap(pixmap.scaled(16, 16, Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
  52. } else {
  53. ui->feedIconLabel->hide();
  54. }
  55. }
  56. ui->titleLabel->setText(QString::fromUtf8(feedInfo.name.c_str()));
  57. ui->msgTitleLabel->setText(QString::fromUtf8(msgInfo.title.c_str()));
  58. ui->descriptionLabel->setText(QString::fromUtf8((msgInfo.descriptionTransformed.empty() ? msgInfo.description : msgInfo.descriptionTransformed).c_str()));
  59. ui->dateTimeLabel->setText(DateTime::formatLongDateTime(msgInfo.pubDate));
  60. /* build menu for link button */
  61. mLink = QString::fromUtf8(msgInfo.link.c_str());
  62. if (mLink.isEmpty()) {
  63. ui->linkButton->setEnabled(false);
  64. } else {
  65. QMenu *menu = new QMenu(this);
  66. QAction *action = menu->addAction(tr("Open link in browser"), this, SLOT(openLink()));
  67. menu->addAction(tr("Copy link to clipboard"), this, SLOT(copyLink()));
  68. QFont font = action->font();
  69. font.setBold(true);
  70. action->setFont(font);
  71. ui->linkButton->setMenu(menu);
  72. }
  73. }
  74. FeedReaderFeedItem::~FeedReaderFeedItem()
  75. {
  76. delete(ui);
  77. }
  78. void FeedReaderFeedItem::toggle()
  79. {
  80. expand(ui->expandFrame->isHidden());
  81. }
  82. void FeedReaderFeedItem::doExpand(bool open)
  83. {
  84. if (mParent) {
  85. mParent->lockLayout(this, true);
  86. }
  87. if (open) {
  88. ui->expandFrame->show();
  89. ui->expandButton->setIcon(QIcon(QString(":/images/edit_remove24.png")));
  90. ui->expandButton->setToolTip(tr("Hide"));
  91. setMsgRead();
  92. } else {
  93. ui->expandFrame->hide();
  94. ui->expandButton->setIcon(QIcon(QString(":/images/edit_add24.png")));
  95. ui->expandButton->setToolTip(tr("Expand"));
  96. }
  97. emit sizeChanged(this);
  98. if (mParent) {
  99. mParent->lockLayout(this, false);
  100. }
  101. }
  102. void FeedReaderFeedItem::removeItem()
  103. {
  104. mParent->lockLayout(this, true);
  105. hide();
  106. mParent->lockLayout(this, false);
  107. if (mParent) {
  108. mParent->deleteFeedItem(this, 0);
  109. }
  110. }
  111. /*********** SPECIFIC FUNCTIONS ***********************/
  112. void FeedReaderFeedItem::readAndClearItem()
  113. {
  114. setMsgRead();
  115. removeItem();
  116. }
  117. void FeedReaderFeedItem::setMsgRead()
  118. {
  119. disconnect(mNotify, SIGNAL(msgChanged(QString,QString,int)), this, SLOT(msgChanged(QString,QString,int)));
  120. mFeedReader->setMessageRead(mFeedId, mMsgId, true);
  121. connect(mNotify, SIGNAL(msgChanged(QString,QString,int)), this, SLOT(msgChanged(QString,QString,int)), Qt::QueuedConnection);
  122. }
  123. void FeedReaderFeedItem::msgChanged(const QString &feedId, const QString &msgId, int /*type*/)
  124. {
  125. if (feedId.toStdString() != mFeedId) {
  126. return;
  127. }
  128. if (msgId.toStdString() != mMsgId) {
  129. return;
  130. }
  131. FeedMsgInfo msgInfo;
  132. if (!mFeedReader->getMsgInfo(mFeedId, mMsgId, msgInfo)) {
  133. return;
  134. }
  135. if (!msgInfo.flag.isnew) {
  136. close();
  137. return;
  138. }
  139. }
  140. void FeedReaderFeedItem::copyLink()
  141. {
  142. if (mLink.isEmpty()) {
  143. return;
  144. }
  145. QApplication::clipboard()->setText(mLink);
  146. }
  147. void FeedReaderFeedItem::openLink()
  148. {
  149. if (mLink.isEmpty()) {
  150. return;
  151. }
  152. QDesktopServices::openUrl(QUrl(mLink));
  153. }