TonicTones
Src/Api/ImageLoaderManager.cpp
00001 //      ImageLoaderManager.cpp
00002 //      
00003 //      Copyright 2010 Jérémy Laumon <jeremy.laumon@gmail.com>
00004 //      
00005 //      This program is free software; you can redistribute it and/or modify
00006 //      it under the terms of the GNU General Public License as published by
00007 //      the Free Software Foundation; either version 2 of the License, or
00008 //      (at your option) any later version.
00009 //      
00010 //      This program is distributed in the hope that it will be useful,
00011 //      but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 //      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 //      GNU General Public License for more details.
00014 //      
00015 //      You should have received a copy of the GNU General Public License
00016 //      along with this program; if not, write to the Free Software
00017 //      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00018 //      MA 02110-1301, USA.
00019 
00020 #include <ImageLoaderManager.h>
00021 #include <Exceptions.h>
00022 
00023 #define tr(arg) QObject::tr(arg)
00024 
00037 ImageLoaderPtr ImageLoaderManager::getLoader(const QString &fileName)
00038 {
00039     int dotPos = fileName.lastIndexOf('.');
00040     if (dotPos == -1)
00041         throw Exception(tr("Error while loading %1 : No extension found.").arg(QDir(fileName).dirName()));
00042     
00043     QString ext = fileName.right(fileName.size() - (dotPos+1)).toLower();
00044     
00045     if(!loaders.contains(ext))
00046         throw Exception(tr("Error while loading %1 : No suitable loader found.").arg(QDir(fileName).dirName()));
00047         
00048     return loaders[ext]->createLoader(fileName);
00049 }
00050 
00054 void ImageLoaderManager::registerLoaders(const QString& directory)
00055 {
00056     QDir loadersDir(qApp->applicationDirPath());
00057     loadersDir.cd(directory);
00058     if (loadersDir.exists())
00059         foreach (QString fileName, loadersDir.entryList(QDir::Files)) 
00060         {
00061             qDebug("Opening %s...", fileName.toStdString().c_str());
00062             QObject *plugin = QPluginLoader(loadersDir.absoluteFilePath(fileName)).instance();
00063             ImageLoaderFactory *loaderFactory = qobject_cast<ImageLoaderFactory *>(plugin);
00064             if (loaderFactory)
00065             {
00066                 foreach(QString ext, loaderFactory->extensions())
00067                 {
00068                     loaders.insert(ext.toLower(), loaderFactory);
00069                     qDebug("\tAdding support for .%s", ext.toStdString().c_str());
00070                 }
00071             }
00072         }
00073 }
00074 
00078 bool ImageLoaderManager::empty()
00079 {
00080     return loaders.empty();
00081 }
00082 
00083 
 All Classes Functions Variables