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( 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 if( !mimeType
.isNull() )
169 handler
->handleURL( mimeType
, dest
);
171 handler
->handleURL( dest
);
173 // for now, local-only handlers have to be synchronous
174 KIO::NetAccess::removeTempFile( file
);
178 if( !mimeType
.isNull() )
179 handler
->handleURL( mimeType
, url
);
181 handler
->handleURL( url
);
187 void MimeTypeHandler::handleURL( const KUrl
&url
) const
192 void MimeTypeHandler::handleURL( const QString
&mimeType
, const KUrl
&url
) const
194 Q_UNUSED( mimeType
);
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
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: