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

akregator

  • sources
  • kde-4.12
  • 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 <QList>
35 #include <QMultiHash>
36 #include <QPixmap>
37 #include <QtDBus/QtDBus>
38 
39 #include <cassert>
40 
41 #define FAVICONINTERFACE "org.kde.FavIcon"
42 
43 
44 using namespace Akregator;
45 
46 FaviconListener::~FaviconListener() {}
47 
48 class FeedIconManager::Private
49 {
50  FeedIconManager* const q;
51 public:
52 
53  static FeedIconManager* m_instance;
54 
55  explicit Private( FeedIconManager* qq );
56  ~Private();
57 
58 
59  void loadIcon( const QString& url );
60  QString iconLocation( const KUrl& ) const;
61 
62  QHash<FaviconListener*,QString> m_listeners;
63  QMultiHash<QString, FaviconListener*> urlDict;
64  QDBusInterface *m_favIconsModule;
65 };
66 
67 namespace {
68 
69 QString getIconUrl( const KUrl& url )
70 {
71  return QLatin1String("http://") + url.host() + QLatin1Char('/');
72 }
73 
74 }
75 
76 FeedIconManager::Private::Private( FeedIconManager* qq ) : q( qq )
77 {
78  QDBusConnection::sessionBus().registerObject(QLatin1String("/FeedIconManager"), q, QDBusConnection::ExportScriptableSlots);
79  m_favIconsModule = new QDBusInterface(QLatin1String("org.kde.kded"), QLatin1String("/modules/favicons"), QLatin1String(FAVICONINTERFACE));
80  Q_ASSERT( m_favIconsModule );
81  q->connect( m_favIconsModule, SIGNAL(iconChanged(bool,QString,QString)),
82  q, SLOT(slotIconChanged(bool,QString,QString)) );
83 }
84 
85 FeedIconManager::Private::~Private()
86 {
87  delete m_favIconsModule;
88 }
89 
90 FeedIconManager *FeedIconManager::Private::m_instance = 0;
91 
92 
93 QString FeedIconManager::Private::iconLocation(const KUrl & url) const
94 {
95  QDBusReply<QString> reply = m_favIconsModule->call( QLatin1String("iconForUrl"), url.url() );
96  return reply.isValid() ? reply.value() : QString();
97 }
98 
99 
100 void FeedIconManager::Private::loadIcon( const QString & url_ )
101 {
102  const KUrl url(url_);
103 
104  QString iconFile = iconLocation( url );
105 
106  if ( iconFile.isEmpty() ) // cache miss
107  {
108  const QDBusReply<void> reply = m_favIconsModule->call( QLatin1String("downloadHostIcon"), url.url() );
109  if ( !reply.isValid() )
110  kWarning() << "Couldn't reach favicon service. Request favicon for " << url << " failed";
111  }
112  else {
113  q->slotIconChanged( false, url.host(), iconFile );
114  }
115 }
116 
117 FeedIconManager* FeedIconManager::self()
118 {
119  static FeedIconManager instance;
120  if (!Private::m_instance)
121  Private::m_instance = &instance;
122  return Private::m_instance;
123 }
124 
125 void FeedIconManager::addListener( const KUrl& url, FaviconListener* listener )
126 {
127  assert( listener );
128  removeListener( listener );
129  const QString iconUrl = getIconUrl( url );
130  d->m_listeners.insert( listener, iconUrl );
131  d->urlDict.insert( iconUrl, listener );
132  d->urlDict.insert( url.host(), listener );
133  QMetaObject::invokeMethod( this, "loadIcon", Qt::QueuedConnection, Q_ARG( QString, iconUrl ) );
134 }
135 
136 void FeedIconManager::removeListener( FaviconListener* listener )
137 {
138  assert( listener );
139  if ( !d->m_listeners.contains( listener ) )
140  return;
141  const QString url = d->m_listeners.value( listener );
142  d->urlDict.remove( KUrl( url ).host(), listener );
143  d->urlDict.remove( url, listener );
144  d->m_listeners.remove( listener );
145 }
146 
147 FeedIconManager::FeedIconManager()
148  : QObject()
149  , d( new Private( this ) )
150 {
151 }
152 
153 FeedIconManager::~FeedIconManager()
154 {
155  delete d;
156 }
157 
158 void FeedIconManager::slotIconChanged( bool isHost,
159  const QString& hostOrUrl,
160  const QString& iconName )
161 {
162  Q_UNUSED( isHost );
163  const QIcon icon( KGlobal::dirs()->findResource( "cache", iconName+QLatin1String(".png") ) );
164  Q_FOREACH( FaviconListener* const l, d->urlDict.values( hostOrUrl ) )
165  l->setFavicon( icon );
166 }
167 
168 #include "feediconmanager.moc"
Akregator::FeedIconManager::removeListener
void removeListener(FaviconListener *listener)
Definition: feediconmanager.cpp:136
QHash
Definition: feedlist.h:40
QObject
Akregator::FeedIconManager::~FeedIconManager
~FeedIconManager()
Definition: feediconmanager.cpp:153
Akregator::FeedIconManager::addListener
void addListener(const KUrl &url, FaviconListener *listener)
Definition: feediconmanager.cpp:125
FAVICONINTERFACE
#define FAVICONINTERFACE
Definition: feediconmanager.cpp:41
Akregator::FaviconListener::setFavicon
virtual void setFavicon(const QIcon &icon)=0
Akregator::FaviconListener::~FaviconListener
virtual ~FaviconListener()
Definition: feediconmanager.cpp:46
feediconmanager.h
Akregator::FaviconListener
Definition: feediconmanager.h:41
Akregator::FeedIconManager::self
static FeedIconManager * self()
Definition: feediconmanager.cpp:117
Akregator::FeedIconManager
Definition: feediconmanager.h:49
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:58:14 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

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