Show webfinger if displayName is unknown for an author.
[larjonas-pumpa.git] / src / actorwidget.cpp
blobca07bf12551c52c0db6a3205674579757632dafa
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 "actorwidget.h"
21 #include "filedownloader.h"
23 #include <QMessageBox>
24 #include <QDesktopServices>
26 //------------------------------------------------------------------------------
28 ActorWidget::ActorWidget(QASActor* a, QWidget* parent, bool small) :
29 QToolButton(parent), m_actor(a)
31 #ifdef DEBUG_WIDGETS
32 qDebug() << "Creating ActorWidget" << (m_actor ? m_actor->id() : "NULL");
33 #endif
34 int max_size = small ? 32 : 64;
36 setStyleSheet("QToolButton::menu-indicator { "
37 "subcontrol-origin: padding; "
38 "subcontrol-position: bottom right; "
39 "}");
40 setIconSize(QSize(max_size, max_size));
41 setFocusPolicy(Qt::NoFocus);
42 setPopupMode(QToolButton::InstantPopup);
43 setAutoRaise(true);
45 m_menu = new QMenu(this);
46 m_menuTitleAction = new QAction(this);
47 connect(m_menuTitleAction, SIGNAL(triggered()), this, SLOT(onMenuTitle()));
49 m_followAction = new QAction(this);
50 connect(m_followAction, SIGNAL(triggered()), this, SLOT(onFollowAuthor()));
52 m_hideAuthorAction = new QAction(this);
53 connect(m_hideAuthorAction, SIGNAL(triggered()), this, SLOT(onHideAuthor()));
55 createMenu();
57 onImageChanged();
60 //------------------------------------------------------------------------------
62 ActorWidget::~ActorWidget() {
63 #ifdef DEBUG_WIDGETS
64 qDebug() << "Deleting ActorWidget" << m_actor->id();
65 #endif
68 //------------------------------------------------------------------------------
70 void ActorWidget::setActor(QASActor* a) {
71 if (m_actor == a)
72 return;
74 m_actor = a;
75 onImageChanged();
76 createMenu();
79 //------------------------------------------------------------------------------
81 void ActorWidget::onImageChanged() {
82 m_url = m_actor ? m_actor->imageUrl() : "";
83 updatePixmap();
86 //------------------------------------------------------------------------------
88 void ActorWidget::updatePixmap() {
89 FileDownloadManager* fdm = FileDownloadManager::getManager();
91 if (fdm->hasFile(m_url)) {
92 setIcon(QIcon(fdm->pixmap(m_url, ":/images/image_broken.png")));
93 } else {
95 if (m_url.isEmpty()) {
96 setIcon(QIcon(":/images/default.png"));
97 } else {
98 setIcon(QIcon(":/images/image_downloading.png"));
99 FileDownloader* fd = fdm->download(m_url);
100 connect(fd, SIGNAL(fileReady()), this, SLOT(updatePixmap()),
101 Qt::UniqueConnection);
106 //------------------------------------------------------------------------------
108 void ActorWidget::createMenu() {
109 if (!m_actor) {
110 setMenu(NULL);
111 return;
114 bool isYou = m_actor->isYou();
116 m_menuTitleAction->setText(m_actor->displayNameOrWebFinger());
117 m_menu->clear();
118 m_menu->addAction(m_menuTitleAction);
119 m_menu->addSeparator();
121 if (!isYou)
122 m_menu->addAction(m_followAction);
124 m_menu->addAction(m_hideAuthorAction);
126 updateMenu();
128 setMenu(m_menu);
131 //------------------------------------------------------------------------------
133 void ActorWidget::updateMenu() {
134 if (!m_actor)
135 return;
137 m_followAction->setText(m_actor->followed() ? tr("stop following") :
138 tr("follow"));
139 m_hideAuthorAction->setText(m_actor->isHidden() ?
140 tr("stop minimising posts") :
141 tr("auto-minimise posts"));
144 //------------------------------------------------------------------------------
146 void ActorWidget::onFollowAuthor() {
147 bool doFollow = !m_actor->followed();
148 if (!doFollow) {
149 QString msg = QString(tr("Are you sure you want to stop following %1?")).
150 arg(m_actor->displayNameOrWebFinger());
151 int ret = QMessageBox::warning(this, CLIENT_FANCY_NAME, msg,
152 QMessageBox::Cancel | QMessageBox::Yes,
153 QMessageBox::Cancel);
154 if (ret != QMessageBox::Yes)
155 return;
158 updateMenu();
159 if (!m_actor->isYou())
160 emit follow(m_actor->id(), doFollow);
163 //------------------------------------------------------------------------------
165 void ActorWidget::onHideAuthor() {
166 bool doHide = !m_actor->isHidden();
167 m_actor->setHidden(doHide);
169 updateMenu();
170 if (doHide)
171 emit lessClicked();
172 else
173 emit moreClicked();
176 //------------------------------------------------------------------------------
178 void ActorWidget::onMenuTitle() {
179 if (m_actor && !m_actor->url().isEmpty())
180 QDesktopServices::openUrl(m_actor->url());