• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepim API Reference
  • KDE Home
  • Contact Us
 

akregator

  • sources
  • kde-4.14
  • kdepim
  • akregator
  • src
feediconmanager.cpp
Go to the documentation of this file.
1 /*
2  This file is part of Akregator.
3 
4  Copyright (C) 2004 Sashmit Bhaduri <smt@vfemail.net>
5  2005 Frank Osterfeld <osterfeld@kde.org>
6 
7  This program is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation; either version 2 of the License, or
10  (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 
21  As a special exception, permission is given to link this program
22  with any edition of Qt, and distribute the resulting executable,
23  without including the source code for Qt in the source distribution.
24 */
25 
26 #include "feediconmanager.h"
27 
28 #include <kapplication.h>
29 #include <kdebug.h>
30 #include <kstandarddirs.h>
31 #include <kurl.h>
32 
33 #include <QIcon>
34 #include <QMultiHash>
35 #include <QtDBus/QtDBus>
36 
37 #include <cassert>
38 
39 #define FAVICONINTERFACE "org.kde.FavIcon"
40 
41 
42 using namespace Akregator;
43 
44 FaviconListener::~FaviconListener() {}
45 
46 class FeedIconManager::Private
47 {
48  FeedIconManager* const q;
49 public:
50 
51  static FeedIconManager* m_instance;
52 
53  explicit Private( FeedIconManager* qq );
54  ~Private();
55 
56 
57  void loadIcon( const QString& url );
58  QString iconLocation( const KUrl& ) const;
59 
60  QHash<FaviconListener*,QString> m_listeners;
61  QMultiHash<QString, FaviconListener*> urlDict;
62  QDBusInterface *m_favIconsModule;
63 };
64 
65 namespace {
66 
67 QString getIconUrl( const KUrl& url )
68 {
69  return QLatin1String("http://") + url.host() + QLatin1Char('/');
70 }
71 
72 }
73 
74 FeedIconManager::Private::Private( FeedIconManager* qq ) : q( qq )
75 {
76  QDBusConnection::sessionBus().registerObject(QLatin1String("/FeedIconManager"), q, QDBusConnection::ExportScriptableSlots);
77  m_favIconsModule = new QDBusInterface(QLatin1String("org.kde.kded"), QLatin1String("/modules/favicons"), QLatin1String(FAVICONINTERFACE));
78  Q_ASSERT( m_favIconsModule );
79  q->connect( m_favIconsModule, SIGNAL(iconChanged(bool,QString,QString)),
80  q, SLOT(slotIconChanged(bool,QString,QString)) );
81 }
82 
83 FeedIconManager::Private::~Private()
84 {
85  delete m_favIconsModule;
86 }
87 
88 FeedIconManager *FeedIconManager::Private::m_instance = 0;
89 
90 
91 QString FeedIconManager::Private::iconLocation(const KUrl & url) const
92 {
93  QDBusReply<QString> reply = m_favIconsModule->call( QLatin1String("iconForUrl"), url.url() );
94  return reply.isValid() ? reply.value() : QString();
95 }
96 
97 
98 void FeedIconManager::Private::loadIcon( const QString & url_ )
99 {
100  const KUrl url(url_);
101 
102  QString iconFile = iconLocation( url );
103 
104  if ( iconFile.isEmpty() ) // cache miss
105  {
106  const QDBusReply<void> reply = m_favIconsModule->call( QLatin1String("downloadHostIcon"), url.url() );
107  if ( !reply.isValid() )
108  kWarning() << "Couldn't reach favicon service. Request favicon for " << url << " failed";
109  }
110  else {
111  q->slotIconChanged( false, url.host(), iconFile );
112  }
113 }
114 
115 FeedIconManager* FeedIconManager::self()
116 {
117  static FeedIconManager instance;
118  if (!Private::m_instance)
119  Private::m_instance = &instance;
120  return Private::m_instance;
121 }
122 
123 void FeedIconManager::addListener( const KUrl& url, FaviconListener* listener )
124 {
125  assert( listener );
126  removeListener( listener );
127  const QString iconUrl = getIconUrl( url );
128  d->m_listeners.insert( listener, iconUrl );
129  d->urlDict.insert( iconUrl, listener );
130  d->urlDict.insert( url.host(), listener );
131  QMetaObject::invokeMethod( this, "loadIcon", Qt::QueuedConnection, Q_ARG( QString, iconUrl ) );
132 }
133 
134 void FeedIconManager::removeListener( FaviconListener* listener )
135 {
136  assert( listener );
137  if ( !d->m_listeners.contains( listener ) )
138  return;
139  const QString url = d->m_listeners.value( listener );
140  d->urlDict.remove( KUrl( url ).host(), listener );
141  d->urlDict.remove( url, listener );
142  d->m_listeners.remove( listener );
143 }
144 
145 FeedIconManager::FeedIconManager()
146  : QObject()
147  , d( new Private( this ) )
148 {
149 }
150 
151 FeedIconManager::~FeedIconManager()
152 {
153  delete d;
154 }
155 
156 void FeedIconManager::slotIconChanged( bool isHost,
157  const QString& hostOrUrl,
158  const QString& iconName )
159 {
160  Q_UNUSED( isHost );
161  const QIcon icon( KGlobal::dirs()->findResource( "cache", iconName+QLatin1String(".png") ) );
162  Q_FOREACH( FaviconListener* const l, d->urlDict.values( hostOrUrl ) )
163  l->setFavicon( icon );
164 }
165 
166 #include "moc_feediconmanager.cpp"
Akregator::FeedIconManager::removeListener
void removeListener(FaviconListener *listener)
Definition: feediconmanager.cpp:134
QDBusReply
QDBusConnection::registerObject
bool registerObject(const QString &path, QObject *object, QFlags< QDBusConnection::RegisterOption > options)
QDBusReply::isValid
bool isValid() const
QDBusConnection::sessionBus
QDBusConnection sessionBus()
Akregator::FeedIconManager::~FeedIconManager
~FeedIconManager()
Definition: feediconmanager.cpp:151
QHash
Definition: feedlist.h:41
QObject
QDBusReply::value
Type value() const
QString::isEmpty
bool isEmpty() const
QString
QDBusInterface
QLatin1Char
QMetaObject::invokeMethod
bool invokeMethod(QObject *obj, const char *member, Qt::ConnectionType type, QGenericReturnArgument ret, QGenericArgument val0, QGenericArgument val1, QGenericArgument val2, QGenericArgument val3, QGenericArgument val4, QGenericArgument val5, QGenericArgument val6, QGenericArgument val7, QGenericArgument val8, QGenericArgument val9)
QLatin1String
Akregator::FeedIconManager::addListener
void addListener(const KUrl &url, FaviconListener *listener)
Definition: feediconmanager.cpp:123
FAVICONINTERFACE
#define FAVICONINTERFACE
Definition: feediconmanager.cpp:39
Akregator::FaviconListener::setFavicon
virtual void setFavicon(const QIcon &icon)=0
Akregator::FaviconListener::~FaviconListener
virtual ~FaviconListener()
Definition: feediconmanager.cpp:44
feediconmanager.h
Akregator::FaviconListener
Definition: feediconmanager.h:40
Akregator::FeedIconManager::self
static FeedIconManager * self()
Definition: feediconmanager.cpp:115
QMultiHash
QIcon
Akregator::FeedIconManager
Definition: feediconmanager.h:48
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:34:00 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

akregator

Skip menu "akregator"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer
  • pimprint

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal