ark
archive.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include "archive.h"
00026 #include "archivefactory.h"
00027
00028 #include <QFile>
00029
00030 #include <KDebug>
00031 #include <KMimeType>
00032 #include <KMimeTypeTrader>
00033 #include <KServiceTypeTrader>
00034 #include <KLibLoader>
00035
00036 static bool comparePlugins( const KService::Ptr &p1, const KService::Ptr &p2 )
00037 {
00038 return ( p1->property( "X-KDE-Priority" ).toInt() ) > ( p2->property( "X-KDE-Priority" ).toInt() );
00039 }
00040
00041 namespace Kerfuffle
00042 {
00043 Archive *factory( const QString & filename, const QString & requestedMimeType )
00044 {
00045 kDebug( 1601 ) ;
00046 qRegisterMetaType<ArchiveEntry>( "ArchiveEntry" );
00047 QString mimeType = requestedMimeType.isEmpty()? KMimeType::findByPath( filename )->name() : requestedMimeType;
00048 KService::List offers = KMimeTypeTrader::self()->query( mimeType, "Kerfuffle/Plugin", "(exist Library)" );
00049
00050 if ( offers.isEmpty()) {
00051
00052 kDebug( 1601 ) << "Trying to find the mimetype by looking at file content";
00053
00054 int acc;
00055 QString mimeType = KMimeType::findByFileContent( filename, &acc )->name();
00056 kDebug(1601) << mimeType << acc;
00057 offers = KMimeTypeTrader::self()->query( mimeType, "Kerfuffle/Plugin", "(exist Library)" );
00058
00059 }
00060
00061 qSort( offers.begin(), offers.end(), comparePlugins );
00062
00063 if ( !offers.isEmpty() )
00064 {
00065 QString libraryName = offers[ 0 ]->library();
00066 KLibrary *lib = KLibLoader::self()->library( QFile::encodeName( libraryName ), QLibrary::ExportExternalSymbolsHint );
00067
00068 #if 0
00069
00070 KPluginLoader loader(offers.at(0));
00071 KPluginFactory *factory = loader.factory();
00072 #endif
00073
00074 kDebug( 1601 ) << "Loading library " << libraryName ;
00075 if ( lib )
00076 {
00077 ArchiveFactory *( *pluginFactory )() = ( ArchiveFactory *( * )() )lib->resolveFunction( "pluginFactory" );
00078 if ( pluginFactory )
00079 {
00080 ArchiveFactory *factory = pluginFactory();
00081 Archive *arch = factory->createArchive( filename, 0 );
00082 delete factory;
00083 return arch;
00084 }
00085 }
00086 kDebug( 1601 ) << "Couldn't load library " << libraryName ;
00087 }
00088 kDebug( 1601 ) << "Couldn't find a library capable of handling " << filename ;
00089 return 0;
00090 }
00091
00092 QStringList supportedMimeTypes()
00093 {
00094 QStringList supported;
00095 KService::List offers = KServiceTypeTrader::self()->query( "Kerfuffle/Plugin", "(exist Library)" );
00096
00097 foreach( const KService::Ptr& service, offers )
00098 {
00099 foreach( const QString& mimeType, service->serviceTypes() )
00100 {
00101 if ( !mimeType.contains( "Kerfuffle" ) )
00102 {
00103 supported << mimeType;
00104 }
00105 }
00106 }
00107 return supported;
00108 }
00109
00110 QStringList supportedWriteMimeTypes()
00111 {
00112 QStringList supported;
00113 KService::List offers = KServiceTypeTrader::self()->query( "Kerfuffle/Plugin", "(exist Library) and ([X-KDE-Kerfuffle-ReadWrite] == true)" );
00114
00115 foreach( const KService::Ptr& service, offers )
00116 {
00117 foreach( const QString& mimeType, service->serviceTypes() )
00118 {
00119 if ( !mimeType.contains( "Kerfuffle" ) )
00120 {
00121 supported << mimeType;
00122 }
00123 }
00124 }
00125 kDebug( 1601 ) << "Returning" << supported;
00126 return supported;
00127 }
00128 }