Show webfinger if displayName is unknown for an author.
[larjonas-pumpa.git] / src / activitywidget.cpp
blob41442e243d184e5d0429deb36098c623e2d6e08f
1 /*
2 Copyright 2013-2015 Mats Sjöberg
4 This file is part of the Pumpa programme.
6 Pumpa is free software: you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 Pumpa is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
14 License for more details.
16 You should have received a copy of the GNU General Public License
17 along with Pumpa. If not, see <http://www.gnu.org/licenses/>.
20 #include "activitywidget.h"
21 #include "texttoolbutton.h"
22 #include "pumpa_defines.h"
24 #include <QDebug>
26 //------------------------------------------------------------------------------
28 ActivityWidget::ActivityWidget(QASActivity* a, bool fullObject,
29 QWidget* parent) :
30 ObjectWidgetWithSignals(parent),
31 m_objectWidget(NULL),
32 m_activity(NULL)
34 #ifdef DEBUG_WIDGETS
35 qDebug() << "Creating ActivityWidget";
36 #endif
38 m_textLabel = new RichTextLabel(this);
39 connect(m_textLabel, SIGNAL(linkHovered(const QString&)),
40 this, SIGNAL(linkHovered(const QString&)));
42 m_objectWidget = new ObjectWidget(NULL, this);
43 ObjectWidgetWithSignals::connectSignals(m_objectWidget, this);
45 connect(m_objectWidget, SIGNAL(showContext(QASObject*)),
46 this, SIGNAL(showContext(QASObject*)));
48 QVBoxLayout* layout = new QVBoxLayout;
49 layout->setContentsMargins(0, 0, 0, 0);
50 layout->addWidget(m_textLabel, 0, Qt::AlignTop);
52 layout->addWidget(m_objectWidget, 0, Qt::AlignTop);
53 layout->addWidget(new QLabel("<hr />"));
55 changeObject(a, fullObject);
57 setLayout(layout);
60 //------------------------------------------------------------------------------
62 ActivityWidget::~ActivityWidget() {
63 #ifdef DEBUG_WIDGETS
64 qDebug() << "Deleting ActivityWidget" << m_activity->id();
65 #endif
68 //------------------------------------------------------------------------------
70 void ActivityWidget::changeObject(QASAbstractObject* aObj, bool fullObject) {
71 m_activity = qobject_cast<QASActivity*>(aObj);
73 if (!m_activity)
74 return;
76 const QString verb = m_activity->verb();
77 QASObject* obj = m_activity->object();
78 QString objType = obj->type();
80 m_objectWidget->changeObject(obj, fullObject);
81 m_objectWidget->setActivity(m_activity);
83 bool objectVisible = !obj->content().isEmpty() ||
84 !obj->displayName().isEmpty()
85 || (objType == "image" && !obj->imageUrl().isEmpty())
86 || obj->isDeleted();
88 m_objectWidget->setVisible(objectVisible);
90 updateText();
93 //------------------------------------------------------------------------------
95 void ActivityWidget::onObjectChanged() {
96 updateText();
99 //------------------------------------------------------------------------------
101 void ActivityWidget::refreshTimeLabels() {
102 if (m_objectWidget)
103 m_objectWidget->refreshTimeLabels();
106 //------------------------------------------------------------------------------
108 void ActivityWidget::updateText() {
109 QString verb = m_activity->verb();
110 QString text = m_activity->content();
111 QString objType = m_activity->object()->type();
113 QString generatorName = m_activity->generatorName();
114 if (!generatorName.isEmpty() && (verb != "share"))
115 text += QString(tr(" via %1")).arg(generatorName);
117 if (verb == "share")
118 text = QString::fromUtf8("\342\231\273") + " " + text;
120 if (verb == "post") {
121 QString toStr = recipientsToString(m_activity->to());
122 QString ccStr = recipientsToString(m_activity->cc());
123 if (!toStr.isEmpty())
124 text += " " + tr("To:") +" " + toStr;
126 if (!ccStr.isEmpty())
127 text += " " + tr("CC:") + " " + ccStr;
130 m_textLabel->setText(text);
133 //------------------------------------------------------------------------------
135 QString ActivityWidget::recipientsToString(QASObjectList* rec) {
136 if (!rec)
137 return "";
139 QStringList ret;
141 for (size_t i=0; i<rec->size(); ++i) {
142 QASObject* r = rec->at(i);
143 if (r->type() == "collection" && r->id() == PUBLIC_RECIPIENT_ID) {
144 ret << tr("Public");
145 } else {
146 QString name = r->displayName();
147 QString url = r->url();
149 if (name.isEmpty())
150 continue;
152 if (url.isEmpty())
153 ret << name;
154 else
155 ret << QString("<a href=\"%1\">%2</a>").arg(url).arg(name);
159 return ret.join(", ");