Show invite menu in wlm chat window immediately
[kdenetwork.git] / kopete / libkopete / kopetemimetypehandler.cpp
blobeeb06a6894912835cee010b554749aad11afaf28
1 /*
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 *************************************************************************
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 *************************************************************************
18 #include "kopetemimetypehandler.h"
19 #include "kopeteglobal.h"
20 #include "kopeteuiglobal.h"
22 #include <qwidget.h>
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>
33 namespace Kopete
36 namespace
38 static QHash<QString, Kopete::MimeTypeHandler*> g_mimeHandlers;
39 static QHash<QString, Kopete::MimeTypeHandler*> g_protocolHandlers;
42 class MimeTypeHandler::Private
44 public:
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 );
64 delete d;
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;
73 return false;
76 g_mimeHandlers.insert( mimeType, this );
77 d->mimeTypes.append( mimeType );
78 // kDebug(14010) << "Mime type " << mimeType << " registered";
79 return true;
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;
88 return false;
91 g_protocolHandlers.insert( protocol, this );
92 d->protocols.append( protocol );
93 kDebug(14010) << "Mime type " << protocol << " registered";
94 return true;
97 const QStringList MimeTypeHandler::mimeTypes() const
99 return d->mimeTypes;
102 const QStringList MimeTypeHandler::protocols() const
104 return d->protocols;
107 bool MimeTypeHandler::canAcceptRemoteFiles() const
109 return d->canAcceptRemoteFiles;
112 bool MimeTypeHandler::dispatchURL( const KUrl &url )
114 if( url.isEmpty() )
115 return false;
117 QString type = KMimeType::findByUrl( url )->name();
119 MimeTypeHandler *mimeHandler = g_mimeHandlers[ type ];
121 if( mimeHandler )
123 return dispatchToHandler( url, type, mimeHandler );
125 else
127 mimeHandler = g_protocolHandlers[ url.protocol() ];
129 if( mimeHandler )
131 mimeHandler->handleURL( url );
132 return true;
134 else
136 kDebug(14010) << "No mime type handler can handle this URL: " << url.prettyUrl();
137 return false;
142 bool MimeTypeHandler::dispatchToHandler( const KUrl &url, const QString &mimeType, MimeTypeHandler *handler )
144 if( !handler->canAcceptRemoteFiles() )
146 QString file;
147 if( !KIO::NetAccess::download( url, file, Kopete::UI::Global::mainWidget() ) )
149 QString sorryText;
150 if ( url.isLocalFile() )
152 sorryText = i18n( "Unable to find the file %1.", url.prettyUrl() );
154 else
156 sorryText = i18n( "<qt>Unable to download the requested file;<br />"
157 "please check that address %1 is correct.</qt>",
158 url.prettyUrl() );
161 KMessageBox::sorry( Kopete::UI::Global::mainWidget(), sorryText );
162 return false;
165 KUrl dest;
166 dest.setPath( file );
168 if( !mimeType.isNull() )
169 handler->handleURL( mimeType, dest );
170 else
171 handler->handleURL( dest );
173 // for now, local-only handlers have to be synchronous
174 KIO::NetAccess::removeTempFile( file );
176 else
178 if( !mimeType.isNull() )
179 handler->handleURL( mimeType, url );
180 else
181 handler->handleURL( url );
184 return true;
187 void MimeTypeHandler::handleURL( const KUrl &url ) const
189 Q_UNUSED( url );
192 void MimeTypeHandler::handleURL( const QString &mimeType, const KUrl &url ) const
194 Q_UNUSED( mimeType );
195 Q_UNUSED( url );
199 EmoticonMimeTypeHandler::EmoticonMimeTypeHandler()
200 : MimeTypeHandler( false )
202 registerAsMimeHandler( QString::fromLatin1("application/x-kopete-emoticons") );
203 registerAsMimeHandler( QString::fromLatin1("application/x-compressed-tar") );
204 registerAsMimeHandler( QString::fromLatin1("application/x-bzip-compressed-tar") );
207 void EmoticonMimeTypeHandler::handleURL( const QString &, const KUrl &url ) const
209 handleURL(url);
212 void EmoticonMimeTypeHandler::handleURL( const KUrl &url ) const
214 Emoticons::self()->installTheme( url.path() );
218 } // END namespace Kopete
220 // vim: set noet ts=4 sts=4 sw=4: