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

kopete/libkopete

  • sources
  • kde-4.12
  • kdenetwork
  • kopete
  • libkopete
kopetemimetypehandler.cpp
Go to the documentation of this file.
1 /*
2  kopetemimetypehandler.cpp - Kopete mime type handlers
3 
4  Copyright (c) 2004 by Richard Smith <kde@metafoo.co.uk>
5 
6  Kopete (c) 2004 by the Kopete developers <kopete-devel@kde.org>
7 
8  *************************************************************************
9  * *
10  * This library is free software; you can redistribute it and/or *
11  * modify it under the terms of the GNU Lesser General Public *
12  * License as published by the Free Software Foundation; either *
13  * version 2 of the License, or (at your option) any later version. *
14  * *
15  *************************************************************************
16 */
17 
18 #include "kopetemimetypehandler.h"
19 #include "kopeteglobal.h"
20 #include "kopeteuiglobal.h"
21 
22 #include <qwidget.h>
23 
24 #include <kdebug.h>
25 #include <klocale.h>
26 #include <kio/netaccess.h>
27 #include <kmimetype.h>
28 #include <kmessagebox.h>
29 #include <kstandarddirs.h>
30 #include <kemoticons.h>
31 #include <kopeteemoticons.h>
32 
33 namespace Kopete
34 {
35 
36 namespace
37 {
38  static QHash<QString, Kopete::MimeTypeHandler*> g_mimeHandlers;
39  static QHash<QString, Kopete::MimeTypeHandler*> g_protocolHandlers;
40 }
41 
42 class MimeTypeHandler::Private
43 {
44 public:
45  Private( bool carf ) : canAcceptRemoteFiles( carf ) {}
46  bool canAcceptRemoteFiles;
47  QStringList mimeTypes;
48  QStringList protocols;
49 };
50 
51 MimeTypeHandler::MimeTypeHandler( bool canAcceptRemoteFiles )
52  : d( new Private( canAcceptRemoteFiles ) )
53 {
54 }
55 
56 MimeTypeHandler::~MimeTypeHandler()
57 {
58  for( QStringList::iterator it = d->mimeTypes.begin(); it != d->mimeTypes.end(); ++it )
59  g_mimeHandlers.remove( *it );
60 
61  for( QStringList::iterator it = d->protocols.begin(); it != d->protocols.end(); ++it )
62  g_protocolHandlers.remove( *it );
63 
64  delete d;
65 }
66 
67 bool MimeTypeHandler::registerAsMimeHandler( const QString &mimeType )
68 {
69  if( g_mimeHandlers[ mimeType ] )
70  {
71  kWarning(14010) << "Warning: Two mime type handlers attempting"
72  " to handle " << mimeType << endl;
73  return false;
74  }
75 
76  g_mimeHandlers.insert( mimeType, this );
77  d->mimeTypes.append( mimeType );
78 // kDebug(14010) << "Mime type " << mimeType << " registered";
79  return true;
80 }
81 
82 bool MimeTypeHandler::registerAsProtocolHandler( const QString &protocol )
83 {
84  if( g_protocolHandlers[ protocol ] )
85  {
86  kWarning(14010) << "Warning: Two protocol handlers attempting"
87  " to handle " << protocol << endl;
88  return false;
89  }
90 
91  g_protocolHandlers.insert( protocol, this );
92  d->protocols.append( protocol );
93  kDebug(14010) << "Mime type " << protocol << " registered";
94  return true;
95 }
96 
97 const QStringList MimeTypeHandler::mimeTypes() const
98 {
99  return d->mimeTypes;
100 }
101 
102 const QStringList MimeTypeHandler::protocols() const
103 {
104  return d->protocols;
105 }
106 
107 bool MimeTypeHandler::canAcceptRemoteFiles() const
108 {
109  return d->canAcceptRemoteFiles;
110 }
111 
112 bool MimeTypeHandler::dispatchURL( const KUrl &url )
113 {
114  if( url.isEmpty() )
115  return false;
116 
117  QString type = KMimeType::findByUrl( url )->name();
118 
119  MimeTypeHandler *mimeHandler = g_mimeHandlers[ type ];
120 
121  if( mimeHandler )
122  {
123  return dispatchToHandler( url, type, mimeHandler );
124  }
125  else
126  {
127  mimeHandler = g_protocolHandlers[ url.protocol() ];
128 
129  if( mimeHandler )
130  {
131  mimeHandler->handleURL( QString(), url );
132  return true;
133  }
134  else
135  {
136  kDebug(14010) << "No mime type handler can handle this URL: " << url.prettyUrl();
137  return false;
138  }
139  }
140 }
141 
142 bool MimeTypeHandler::dispatchToHandler( const KUrl &url, const QString &mimeType, MimeTypeHandler *handler )
143 {
144  if( !handler->canAcceptRemoteFiles() )
145  {
146  QString file;
147  if( !KIO::NetAccess::download( url, file, Kopete::UI::Global::mainWidget() ) )
148  {
149  QString sorryText;
150  if ( url.isLocalFile() )
151  {
152  sorryText = i18n( "Unable to find the file %1.", url.prettyUrl() );
153  }
154  else
155  {
156  sorryText = i18n( "<qt>Unable to download the requested file;<br />"
157  "please check that address %1 is correct.</qt>",
158  url.prettyUrl() );
159  }
160 
161  KMessageBox::sorry( Kopete::UI::Global::mainWidget(), sorryText );
162  return false;
163  }
164 
165  KUrl dest;
166  dest.setPath( file );
167 
168  handler->handleURL( mimeType, dest );
169 
170  // for now, local-only handlers have to be synchronous
171  KIO::NetAccess::removeTempFile( file );
172  }
173  else
174  {
175  handler->handleURL( mimeType, url );
176  }
177 
178  return true;
179 }
180 
181 void MimeTypeHandler::handleURL( const QString &mimeType, const KUrl &url ) const
182 {
183  Q_UNUSED( mimeType );
184  Q_UNUSED( url );
185 }
186 
187 void MimeTypeHandler::handleURL( const KUrl &url ) const
188 {
189  handleURL( QString(), url );
190 }
191 
192 
193 EmoticonMimeTypeHandler::EmoticonMimeTypeHandler()
194  : MimeTypeHandler( false )
195 {
196  registerAsMimeHandler( QString::fromLatin1("application/x-kopete-emoticons") );
197  registerAsMimeHandler( QString::fromLatin1("application/x-compressed-tar") );
198  registerAsMimeHandler( QString::fromLatin1("application/x-bzip-compressed-tar") );
199 }
200 
201 void EmoticonMimeTypeHandler::handleURL( const QString &, const KUrl &url ) const
202 {
203  Emoticons::self()->installTheme( url.toLocalFile() );
204 }
205 
206 void EmoticonMimeTypeHandler::handleURL( const KUrl &url ) const
207 {
208  handleURL( QString(), url );
209 }
210 
211 
212 } // END namespace Kopete
213 
214 // vim: set noet ts=4 sts=4 sw=4:
Kopete::EmoticonMimeTypeHandler::handleURL
void handleURL(const QString &mimeType, const KUrl &url) const
Handles the URL url, which has the mime type mimeType.
Definition: kopetemimetypehandler.cpp:201
kopetemimetypehandler.h
Kopete::MimeTypeHandler::dispatchURL
static bool dispatchURL(const KUrl &url)
Finds a MimeTypeHandler for a given URL, and tells that handler to handle it.
Definition: kopetemimetypehandler.cpp:112
Kopete::MimeTypeHandler::~MimeTypeHandler
virtual ~MimeTypeHandler()
Definition: kopetemimetypehandler.cpp:56
Kopete::MimeTypeHandler::registerAsProtocolHandler
bool registerAsProtocolHandler(const QString &protocol)
Register this object as the handler of type protocol.
Definition: kopetemimetypehandler.cpp:82
kopeteuiglobal.h
Kopete::MimeTypeHandler
A handler for some set of mime-types A mime type handler is responsible for handling requests to open...
Definition: kopetemimetypehandler.h:36
kopeteemoticons.h
Kopete::Emoticons::self
static KEmoticons * self()
The emoticons container-class by default is a singleton object.
Definition: kopeteemoticons.cpp:31
Kopete::MimeTypeHandler::mimeTypes
const QStringList mimeTypes() const
Returns a list of mime types this object is registered to handle.
Definition: kopetemimetypehandler.cpp:97
Kopete::MimeTypeHandler::canAcceptRemoteFiles
bool canAcceptRemoteFiles() const
Returns true if this handler can accept remote files direcltly; If false, remote files are downloaded...
Definition: kopetemimetypehandler.cpp:107
Kopete::MimeTypeHandler::registerAsMimeHandler
bool registerAsMimeHandler(const QString &mimeType)
Register this object as the handler of type mimeType.
Definition: kopetemimetypehandler.cpp:67
Kopete::UI::Global::mainWidget
KOPETE_EXPORT QWidget * mainWidget()
Returns the main widget - this is the widget that message boxes and KNotify stuff should use as a par...
Definition: kopeteuiglobal.cpp:37
Kopete::EmoticonMimeTypeHandler::EmoticonMimeTypeHandler
EmoticonMimeTypeHandler()
Definition: kopetemimetypehandler.cpp:193
Kopete::MimeTypeHandler::MimeTypeHandler
MimeTypeHandler(bool canAcceptRemoteFiles=false)
Definition: kopetemimetypehandler.cpp:51
Kopete::MimeTypeHandler::protocols
const QStringList protocols() const
Returns a list of protocols this object is registered to handle.
Definition: kopetemimetypehandler.cpp:102
Kopete::MimeTypeHandler::handleURL
virtual KDE_DEPRECATED void handleURL(const KUrl &url) const
Definition: kopetemimetypehandler.cpp:187
kopeteglobal.h
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:53:51 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kopete/libkopete

Skip menu "kopete/libkopete"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdenetwork API Reference

Skip menu "kdenetwork API Reference"
  • kget
  • kopete
  •   kopete
  •   libkopete
  • krdc
  • krfb

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