Show webfinger if displayName is unknown for an author.
[larjonas-pumpa.git] / src / objectwidget.cpp
bloba7c967d7cffc2efc106ef91b2c6eba2e05b27e83
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 "objectwidget.h"
22 //------------------------------------------------------------------------------
24 ObjectWidget::ObjectWidget(QASObject* obj, QWidget* parent) :
25 ObjectWidgetWithSignals(parent),
26 m_objectWidget(NULL),
27 m_shortObjectWidget(NULL),
28 m_contextLabel(NULL),
29 m_contextButton(NULL),
30 m_topLayout(NULL),
31 m_object(NULL),
32 m_irtObject(NULL),
33 m_short(false)
35 #ifdef DEBUG_WIDGETS
36 qDebug() << "Creating ObjectWidget";
37 #endif
39 m_layout = new QVBoxLayout;
40 m_layout->setContentsMargins(0, 0, 0, 0);
41 m_layout->setSpacing(0);
43 // Add label with context "Re:" text and "show context" button for
44 // replies.
45 m_topLayout = new QHBoxLayout;
46 m_topLayout->setContentsMargins(0, 0, 0, 0);
48 m_contextLabel = new RichTextLabel(this, true);
49 m_topLayout->addWidget(m_contextLabel, 0, Qt::AlignVCenter);
51 m_topLayout->addSpacing(10);
52 m_contextButton = new TextToolButton(this);
53 connect(m_contextButton, SIGNAL(clicked()), this, SLOT(onShowContext()));
54 m_topLayout->addWidget(m_contextButton, 0, Qt::AlignVCenter);
56 m_layout->addLayout(m_topLayout);
58 m_objectWidget = new FullObjectWidget(m_object, this);
59 ObjectWidgetWithSignals::connectSignals(m_objectWidget, this);
60 connect(m_objectWidget, SIGNAL(lessClicked()),
61 this, SLOT(showLess()));
62 m_layout->addWidget(m_objectWidget);
64 m_shortObjectWidget = new ShortObjectWidget(m_object, this);
65 connect(m_shortObjectWidget, SIGNAL(follow(QString, bool)),
66 this, SIGNAL(follow(QString, bool)));
67 connect(m_shortObjectWidget, SIGNAL(moreClicked()),
68 this, SLOT(showMore()));
69 m_layout->addWidget(m_shortObjectWidget);
71 changeObject(obj);
73 setLayout(m_layout);
76 //------------------------------------------------------------------------------
78 ObjectWidget::~ObjectWidget() {
79 #ifdef DEBUG_WIDGETS
80 qDebug() << "Deleting ObjectWidget" << m_object->id();
81 #endif
84 //------------------------------------------------------------------------------
86 void ObjectWidget::changeObject(QASAbstractObject* obj, bool fullObject) {
87 if (m_object)
88 disconnect(m_object, SIGNAL(changed()), this, SLOT(onChanged()));
89 if (m_irtObject)
90 disconnect(m_irtObject, SIGNAL(changed()),
91 this, SLOT(updateContextLabel()));
92 m_irtObject = NULL;
94 m_object = qobject_cast<QASObject*>(obj);
95 if (!obj)
96 return;
97 m_short = !fullObject;
99 connect(m_object, SIGNAL(changed()), this, SLOT(onChanged()));
101 m_objectWidget->changeObject(obj);
102 m_shortObjectWidget->changeObject(obj);
104 m_contextLabel->setVisible(false);
105 m_contextButton->setVisible(false);
106 if (m_object->type() == "comment" && m_object->inReplyTo()) {
107 m_irtObject = m_object->inReplyTo();
108 connect(m_irtObject, SIGNAL(changed()), this, SLOT(updateContextLabel()));
110 if (!m_irtObject->url().isEmpty())
111 updateContextLabel();
114 if (m_short) {
115 m_contextLabel->setVisible(false);
116 m_contextButton->setVisible(false);
117 m_objectWidget->setVisible(false);
118 m_shortObjectWidget->setVisible(true);
119 } else {
120 m_shortObjectWidget->setVisible(false);
121 m_objectWidget->setVisible(true);
124 QASActor* author = m_object->author();
125 if (author && author->url().isEmpty())
126 refreshObject(m_object);
129 //------------------------------------------------------------------------------
131 void ObjectWidget::showMore() {
132 if (!m_short || !m_shortObjectWidget)
133 return;
135 m_short = false;
136 m_shortObjectWidget->setVisible(false);
137 m_objectWidget->setVisible(true);
138 if (m_irtObject && !m_irtObject->url().isEmpty()) {
139 m_contextLabel->setVisible(true);
140 m_contextButton->setVisible(true);
143 m_objectWidget->updateMenu();
145 emit moreClicked();
148 //------------------------------------------------------------------------------
150 void ObjectWidget::showLess() {
151 if (m_short || !m_objectWidget)
152 return;
154 m_short = true;
155 m_objectWidget->setVisible(false);
156 m_shortObjectWidget->setVisible(true);
157 m_contextLabel->setVisible(false);
158 m_contextButton->setVisible(false);
160 m_shortObjectWidget->updateMenu();
162 emit lessClicked();
165 //------------------------------------------------------------------------------
167 void ObjectWidget::onChanged() {
168 setVisible(!m_object->url().isEmpty());
171 //------------------------------------------------------------------------------
173 void ObjectWidget::updateContextLabel() {
174 if (!m_irtObject || !m_contextLabel)
175 return;
177 QString text = m_irtObject->excerpt();
178 m_contextLabel->setText(tr("Re: ") + text);
179 m_contextButton->setText(tr("show context"));
180 if (!m_short && !m_irtObject->url().isEmpty()) {
181 m_contextLabel->setVisible(true);
182 m_contextButton->setVisible(true);
186 //------------------------------------------------------------------------------
188 void ObjectWidget::onShowContext() {
189 if (!m_irtObject || !m_topLayout)
190 return;
192 emit showContext(m_irtObject);
195 //------------------------------------------------------------------------------
197 void ObjectWidget::refreshTimeLabels() {
198 if (m_objectWidget)
199 m_objectWidget->refreshTimeLabels();
200 if (m_shortObjectWidget)
201 m_shortObjectWidget->refreshTimeLabels();
204 //------------------------------------------------------------------------------
206 void ObjectWidget::disableLessButton() {
207 if (!m_short)
208 m_objectWidget->disableLessButton();