1
mirror of https://invent.kde.org/network/falkon.git synced 2024-09-23 18:52:11 +02:00
falkonOfficial/src/lib/rss/rsswidget.cpp

125 lines
3.5 KiB
C++
Raw Normal View History

2011-03-03 18:29:20 +01:00
/* ============================================================
* QupZilla - WebKit based browser
* Copyright (C) 2010-2012 David Rosca <nowrep@gmail.com>
2011-03-03 18:29:20 +01:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* ============================================================ */
#include "rsswidget.h"
#include "ui_rsswidget.h"
2011-03-03 15:24:23 +01:00
#include "mainapplication.h"
#include "tabbedwebview.h"
#include "webpage.h"
2011-03-03 15:24:23 +01:00
#include "rssmanager.h"
#include "rssnotification.h"
#include <QToolTip>
#include <QPushButton>
#include <QWebFrame>
RSSWidget::RSSWidget(WebView* view, QWidget* parent)
: QMenu(parent)
, ui(new Ui::RSSWidget)
, m_view(view)
{
ui->setupUi(this);
#ifndef KDE
// Use light color for QLabels even with Ubuntu Ambiance theme
QPalette pal = palette();
pal.setColor(QPalette::WindowText, QToolTip::palette().color(QPalette::ToolTipText));
ui->label_2->setPalette(pal);
#endif
QWebFrame* frame = m_view->page()->mainFrame();
QWebElementCollection links = frame->findAllElements("link[type=\"application/rss+xml\"]");
for (int i = 0; i < links.count(); i++) {
QWebElement element = links.at(i);
QString title = element.attribute("title");
const QUrl url = QUrl::fromEncoded(element.attribute("href").toUtf8());
if (url.isEmpty()) {
continue;
}
if (title.isEmpty()) {
title = tr("Untitled feed");
}
2011-03-03 15:24:23 +01:00
QPushButton* button = new QPushButton(this);
button->setText(tr("Add"));
button->setToolTip(title);
button->setProperty("rss-url", url);
2011-03-03 15:24:23 +01:00
QLabel* label = new QLabel(this);
#ifndef KDE
label->setPalette(pal);
#endif
label->setText(title);
2011-03-03 15:24:23 +01:00
ui->gridLayout->addWidget(label, i, 0);
ui->gridLayout->addWidget(button, i, 1);
connect(button, SIGNAL(clicked()), this, SLOT(addRss()));
}
}
void RSSWidget::showAt(QWidget* _parent)
{
layout()->invalidate();
layout()->activate();
const QPoint &widgetPos = _parent->mapToGlobal(QPoint(0, 0));
QPoint newPos;
newPos.setX(widgetPos.x() + _parent->width() - width());
newPos.setY(widgetPos.y() + _parent->height());
move(newPos);
show();
}
2011-03-03 15:24:23 +01:00
void RSSWidget::addRss()
{
if (!m_view) {
2011-03-03 15:24:23 +01:00
return;
}
2011-03-17 17:03:04 +01:00
if (QPushButton* button = qobject_cast<QPushButton*>(sender())) {
QUrl url = button->property("rss-url").toUrl();
if (url.isRelative()) {
url = m_view->page()->mainFrame()->baseUrl().resolved(url);
2011-03-03 15:24:23 +01:00
}
if (!url.isValid()) {
2011-03-03 15:24:23 +01:00
return;
}
2011-03-03 15:24:23 +01:00
QString title;
if (button->toolTip().isEmpty()) {
2011-03-03 15:24:23 +01:00
title = m_view->url().host();
}
else {
2011-03-03 15:24:23 +01:00
title = button->toolTip();
}
2011-03-03 15:24:23 +01:00
RSSNotification* notif = new RSSNotification(title, url, m_view);
m_view->addNotification(notif);
close();
2011-03-03 15:24:23 +01:00
}
}
RSSWidget::~RSSWidget()
{
delete ui;
}