Back to index

scribus-ng  1.3.4.dfsg+svn20071115
gtgettext.cpp
Go to the documentation of this file.
00001 /*
00002 For general Scribus (>=1.3.2) copyright and licensing information please refer
00003 to the COPYING file provided with the program. Following this notice may exist
00004 a copyright and/or license notice that predates the release of Scribus 1.3.2
00005 for which a new license (GPL+exception) is in place.
00006 */
00007 /***************************************************************************
00008  *   Copyright (C) 2004 by Riku Leino                                      *
00009  *   tsoots@gmail.com                                                      *
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU General Public License as published by  *
00013  *   the Free Software Foundation; either version 2 of the License, or     *
00014  *   (at your option) any later version.                                   *
00015  *                                                                         *
00016  *   This program is distributed in the hope that it will be useful,       *
00017  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00018  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00019  *   GNU General Public License for more details.                          *
00020  *                                                                         *
00021  *   You should have received a copy of the GNU General Public License     *
00022  *   along with this program; if not, write to the                         *
00023  *   Free Software Foundation, Inc.,                                       *
00024  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00025  ***************************************************************************/
00026 
00027 #include "gtgettext.h"
00028 #include "pluginmanager.h"
00029 #include "scpaths.h"
00030 #include "pageitem.h"
00031 #include "scribusdoc.h"
00032 #include "selection.h"
00033 
00034 extern QPixmap loadIcon(QString nam);
00035 
00036 
00037 gtGetText::gtGetText(ScribusDoc* doc)
00038 {
00039        m_Doc=doc;
00040        loadImporterPlugins();
00041 }
00042 
00043 void gtGetText::launchImporter(int importer, const QString& filename, bool textOnly, 
00044                                                         const QString& encoding, bool append, PageItem* target)
00045 {
00046        struct ImporterData ida;
00047        bool callImporter = true;
00048        if (importer == -1)
00049        {
00050               QString fend = filename.right(filename.length() - filename.findRev(".") - 1);
00051               if (importerMap.find(fend) != importerMap.end())
00052                      ida = *importerMap[fend];
00053               else
00054               {
00055                      dias = new gtDialogs();
00056                      callImporter = dias->runImporterDialog(ilist);
00057                      if (callImporter)
00058                             ida = importers[dias->getImporter()];
00059                      delete dias;
00060               }
00061        }
00062        else
00063        {
00064               ida = importers[importer];
00065        }
00066        PageItem* targetFrame=target;
00067        if (targetFrame==0)
00068               targetFrame=m_Doc->m_Selection->itemAt(0);
00069 
00070        if (targetFrame!=0 && callImporter)
00071               CallDLL(ida, filename, encoding, textOnly, append, targetFrame);
00072 }
00073 
00074 void gtGetText::loadImporterPlugins()
00075 {
00076        QString gtdir = ScPaths::instance().pluginDir();
00077        gtdir += "gettext";
00078        QString libPattern = QString("*.%1*").arg(PluginManager::platformDllExtension());
00079        QDir d(gtdir, libPattern, QDir::Name, QDir::Files | QDir::Executable | QDir::NoSymLinks);
00080 
00081        struct ImporterData ida;
00082        ida.fileFormatName = "";
00083 
00084        if ((d.exists()) && (d.count() != 0))
00085        {
00086               for (uint dc = 0; dc < d.count(); ++dc)
00087               {
00088                      if (DLLName(d[dc], &ida.fileFormatName, &ida.fileEndings))
00089                      {
00090                             // no plugin's "format name" marks "don't load plug"
00091                             if (ida.fileFormatName.isNull())
00092                                    continue;
00093                             ida.soFilePath = d[dc];
00094                             if (ida.soFilePath.left(1) != "/")
00095                                    ida.soFilePath = "/" + ida.soFilePath;
00096                             importers.push_back(ida);
00097                      }
00098               }
00099        }
00100        createMap();
00101 }
00102 
00103 ImportSetup gtGetText::run()
00104 {
00105        QString filters = "";
00106        QString allSupported = QObject::tr("All Supported Formats") + " (";
00107        for (uint i = 0; i < importers.size(); ++i)
00108        {
00109               if (importers[i].fileEndings.count() != 0)
00110               {
00111                      filters += importers[i].fileFormatName + " (";
00112                      for (uint j = 0; j < importers[i].fileEndings.count(); ++j)
00113                      {
00114                             filters += "*." + importers[i].fileEndings[j] + " ";
00115                             allSupported += "*." + importers[i].fileEndings[j] + " ";
00116                      }
00117                      filters = filters.stripWhiteSpace();
00118                      filters += ");;";
00119               }
00120        }
00121        allSupported = allSupported.stripWhiteSpace();
00122        allSupported += ");;";
00123        filters = allSupported + filters;
00124        filters += QObject::tr("All Files (*)");
00125        for (uint i = 0;  i < importers.size(); ++i)
00126               ilist.append(importers[i].fileFormatName);
00127        dias = new gtDialogs();
00128        ImportSetup impsetup;
00129        impsetup.runDialog=false;
00130        if (dias->runFileDialog(filters, ilist))
00131        {
00132               impsetup.runDialog=true;
00133               impsetup.encoding=dias->getEncoding();
00134               impsetup.filename=dias->getFileName();
00135               impsetup.importer=dias->getImporter();
00136               impsetup.textOnly=dias->importTextOnly();
00137 //            launchImporter(dias->getImporter(), dias->getFileName(),
00138 //                           dias->importTextOnly(), dias->getEncoding(), append);
00139        }
00140        delete dias;
00141        return impsetup;
00142 }
00143 
00144 void gtGetText::CallDLL(const ImporterData& idata, const QString& filePath,
00145                         const QString& encoding, bool textOnly, bool append, PageItem* importItem)
00146 {
00147        void* gtplugin;
00148        typedef void (*sdem)(QString filename, QString encoding, bool textOnly, gtWriter *writer);
00149        sdem fp_GetText;
00150        QString pluginFilePath = QString("%1/gettext/%2").arg(ScPaths::instance().pluginDir()).arg(idata.soFilePath);
00151        gtplugin = PluginManager::loadDLL(pluginFilePath);
00152        if (!gtplugin)
00153        {
00154               qWarning("Failed to load plugin %s", pluginFilePath.ascii());
00155               return;
00156        }
00157        fp_GetText = (sdem) PluginManager::resolveSym(gtplugin, "GetText");
00158        if (!fp_GetText)
00159        {
00160               qWarning("Failed to get GetText() from %s", pluginFilePath.ascii());
00161               PluginManager::unloadDLL(gtplugin);
00162               return;
00163        }
00164        gtWriter *w = new gtWriter(append, importItem);
00165        (*fp_GetText)(filePath, encoding, textOnly, w);
00166        delete w;
00167        PluginManager::unloadDLL(gtplugin);
00168 }
00169 
00170 bool gtGetText::DLLName(QString name, QString *ffName, QStringList *fEndings)
00171 {
00172        void* gtplugin;
00173        typedef QString (*sdem0)();
00174        typedef QStringList (*sdem1)();
00175        sdem0 fp_FileFormatName;
00176        sdem1 fp_FileExtensions;
00177        QString pluginFilePath = QString("%1/gettext/%2").arg(ScPaths::instance().pluginDir()).arg(name);
00178        gtplugin = PluginManager::loadDLL(pluginFilePath);
00179        if (!gtplugin)
00180        {
00181               qWarning("Failed to load plugin %s", pluginFilePath.ascii());
00182               return false;
00183        }
00184        fp_FileFormatName = (sdem0) PluginManager::resolveSym( gtplugin, "FileFormatName");
00185        if (!fp_FileFormatName)
00186        {
00187               qWarning("Failed to get FileFormatName() from %s", pluginFilePath.ascii());
00188               PluginManager::unloadDLL(gtplugin);
00189               return false;
00190        }
00191        fp_FileExtensions = (sdem1) PluginManager::resolveSym( gtplugin, "FileExtensions");
00192        if (!fp_FileExtensions)
00193        {
00194               qWarning("Failed to get FileExtensions() from %s", pluginFilePath.ascii());
00195               PluginManager::unloadDLL(gtplugin);
00196               return false;
00197        }
00198        *ffName = (*fp_FileFormatName)();
00199        *fEndings = (*fp_FileExtensions)();
00200        PluginManager::unloadDLL(gtplugin);
00201        return true;
00202 }
00203 
00204 void gtGetText::createMap()
00205 {
00206        for (uint i = 0; i < importers.size(); ++i)
00207        {
00208               for (uint j = 0; j < importers[i].fileEndings.count(); ++j)
00209                             importerMap.insert(importers[i].fileEndings[j], &importers[i]);
00210        }
00211 }
00212 
00213 gtGetText::~gtGetText()
00214 {
00215 
00216 }