2
kopetemimetypehandler.cpp - Kopete mime type handlers
4
Copyright (c) 2004 by Richard Smith <kde@metafoo.co.uk>
6
Kopete (c) 2004 by the Kopete developers <kopete-devel@kde.org>
8
*************************************************************************
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. *
15
*************************************************************************
18
#include "kopetemimetypehandler.h"
19
#include "kopeteglobal.h"
20
#include "kopeteuiglobal.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>
38
static QHash<QString, Kopete::MimeTypeHandler*> g_mimeHandlers;
39
static QHash<QString, Kopete::MimeTypeHandler*> g_protocolHandlers;
42
class MimeTypeHandler::Private
45
Private( bool carf ) : canAcceptRemoteFiles( carf ) {}
46
bool canAcceptRemoteFiles;
47
QStringList mimeTypes;
48
QStringList protocols;
51
MimeTypeHandler::MimeTypeHandler( bool canAcceptRemoteFiles )
52
: d( new Private( canAcceptRemoteFiles ) )
56
MimeTypeHandler::~MimeTypeHandler()
58
for( QStringList::iterator it = d->mimeTypes.begin(); it != d->mimeTypes.end(); ++it )
59
g_mimeHandlers.remove( *it );
61
for( QStringList::iterator it = d->protocols.begin(); it != d->protocols.end(); ++it )
62
g_protocolHandlers.remove( *it );
67
bool MimeTypeHandler::registerAsMimeHandler( const QString &mimeType )
69
if( g_mimeHandlers[ mimeType ] )
71
kWarning(14010) << "Warning: Two mime type handlers attempting"
72
" to handle " << mimeType << endl;
76
g_mimeHandlers.insert( mimeType, this );
77
d->mimeTypes.append( mimeType );
78
// kDebug(14010) << "Mime type " << mimeType << " registered";
82
bool MimeTypeHandler::registerAsProtocolHandler( const QString &protocol )
84
if( g_protocolHandlers[ protocol ] )
86
kWarning(14010) << "Warning: Two protocol handlers attempting"
87
" to handle " << protocol << endl;
91
g_protocolHandlers.insert( protocol, this );
92
d->protocols.append( protocol );
93
kDebug(14010) << "Mime type " << protocol << " registered";
97
const QStringList MimeTypeHandler::mimeTypes() const
102
const QStringList MimeTypeHandler::protocols() const
107
bool MimeTypeHandler::canAcceptRemoteFiles() const
109
return d->canAcceptRemoteFiles;
112
bool MimeTypeHandler::dispatchURL( const KUrl &url )
117
QString type = KMimeType::findByUrl( url )->name();
119
MimeTypeHandler *mimeHandler = g_mimeHandlers[ type ];
123
return dispatchToHandler( url, type, mimeHandler );
127
mimeHandler = g_protocolHandlers[ url.protocol() ];
131
mimeHandler->handleURL( QString(), url );
136
kDebug(14010) << "No mime type handler can handle this URL: " << url.prettyUrl();
142
bool MimeTypeHandler::dispatchToHandler( const KUrl &url, const QString &mimeType, MimeTypeHandler *handler )
144
if( !handler->canAcceptRemoteFiles() )
147
if( !KIO::NetAccess::download( url, file, Kopete::UI::Global::mainWidget() ) )
150
if ( url.isLocalFile() )
152
sorryText = i18n( "Unable to find the file %1.", url.prettyUrl() );
156
sorryText = i18n( "<qt>Unable to download the requested file;<br />"
157
"please check that address %1 is correct.</qt>",
161
KMessageBox::sorry( Kopete::UI::Global::mainWidget(), sorryText );
166
dest.setPath( file );
168
handler->handleURL( mimeType, dest );
170
// for now, local-only handlers have to be synchronous
171
KIO::NetAccess::removeTempFile( file );
175
handler->handleURL( mimeType, url );
181
void MimeTypeHandler::handleURL( const QString &mimeType, const KUrl &url ) const
183
Q_UNUSED( mimeType );
188
EmoticonMimeTypeHandler::EmoticonMimeTypeHandler()
189
: MimeTypeHandler( false )
191
registerAsMimeHandler( QString::fromLatin1("application/x-kopete-emoticons") );
192
registerAsMimeHandler( QString::fromLatin1("application/x-compressed-tar") );
193
registerAsMimeHandler( QString::fromLatin1("application/x-bzip-compressed-tar") );
196
void EmoticonMimeTypeHandler::handleURL( const QString &, const KUrl &url ) const
198
Emoticons::self()->installTheme( url.toLocalFile() );
202
} // END namespace Kopete
204
// vim: set noet ts=4 sts=4 sw=4: