Scribus Developer Documentation - API
logo-pen

Scribus Developer Documentation - API

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 #include "gtdialogs.h"
00034 #include "gtwriter.h"
00035 //Added by qt3to4:
00036 #include <QPixmap>
00037 #include "util_icon.h"
00038 
00039 
00040 gtGetText::gtGetText(ScribusDoc* doc)
00041 {
00042     m_Doc=doc;
00043     loadImporterPlugins();
00044 }
00045 
00046 void gtGetText::launchImporter(int importer, const QString& filename, bool textOnly, 
00047                                 const QString& encoding, bool append, PageItem* target)
00048 {
00049     struct ImporterData ida;
00050     bool callImporter = true;
00051     if (importer == -1)
00052     {
00053         QString fend = filename.right(filename.length() - filename.lastIndexOf(".") - 1);
00054         if (importerMap.find(fend) != importerMap.end())
00055             ida = *importerMap[fend];
00056         else
00057         {
00058             dias = new gtDialogs();
00059             callImporter = dias->runImporterDialog(ilist);
00060             if (callImporter)
00061                 ida = importers[dias->getImporter()];
00062             delete dias;
00063         }
00064     }
00065     else
00066     {
00067         ida = importers[importer];
00068     }
00069     PageItem* targetFrame=target;
00070     if (targetFrame==0)
00071         targetFrame=m_Doc->m_Selection->itemAt(0);
00072 
00073     if (targetFrame!=0 && callImporter)
00074         CallDLL(ida, filename, encoding, textOnly, append, targetFrame);
00075 }
00076 
00077 void gtGetText::loadImporterPlugins()
00078 {
00079     QString gtdir = ScPaths::instance().pluginDir();
00080     gtdir += "gettext";
00081     QString libPattern = QString("*.%1*").arg(PluginManager::platformDllExtension());
00082     QDir d(gtdir, libPattern, QDir::Name, (QDir::Filter) PluginManager::platformDllSearchFlags());
00083 
00084     struct ImporterData ida;
00085     ida.fileFormatName = "";
00086 
00087     if ((d.exists()) && (d.count() != 0))
00088     {
00089         for (uint dc = 0; dc < d.count(); ++dc)
00090         {
00091             if (DLLName(d[dc], &ida.fileFormatName, &ida.fileEndings))
00092             {
00093                 // no plugin's "format name" marks "don't load plug"
00094                 if (ida.fileFormatName.isNull())
00095                     continue;
00096                 ida.soFilePath = d[dc];
00097                 if (ida.soFilePath.left(1) != "/")
00098                     ida.soFilePath = "/" + ida.soFilePath;
00099                 importers.push_back(ida);
00100             }
00101         }
00102     }
00103     createMap();
00104 }
00105 
00106 ImportSetup gtGetText::run()
00107 {
00108     QString filters = "";
00109     QString allSupported = QObject::tr("All Supported Formats") + " (";
00110     for (uint i = 0; i < importers.size(); ++i)
00111     {
00112         if (importers[i].fileEndings.count() != 0)
00113         {
00114             filters += importers[i].fileFormatName + " (";
00115             for (int j = 0; j < importers[i].fileEndings.count(); ++j)
00116             {
00117                 filters += "*." + importers[i].fileEndings[j] + " ";
00118                 allSupported += "*." + importers[i].fileEndings[j] + " ";
00119             }
00120             filters = filters.trimmed();
00121             filters += ");;";
00122         }
00123     }
00124     allSupported = allSupported.trimmed();
00125     allSupported += ");;";
00126     filters = allSupported + filters;
00127     filters += QObject::tr("All Files (*)");
00128     for (uint i = 0;  i < importers.size(); ++i)
00129         ilist.append(importers[i].fileFormatName);
00130     dias = new gtDialogs();
00131     ImportSetup impsetup;
00132     impsetup.runDialog=false;
00133     if (dias->runFileDialog(filters, ilist))
00134     {
00135         impsetup.runDialog=true;
00136         impsetup.encoding=dias->getEncoding();
00137         impsetup.filename=dias->getFileName();
00138         impsetup.importer=dias->getImporter();
00139         impsetup.textOnly=dias->importTextOnly();
00140 //      launchImporter(dias->getImporter(), dias->getFileName(),
00141 //                     dias->importTextOnly(), dias->getEncoding(), append);
00142     }
00143     delete dias;
00144     return impsetup;
00145 }
00146 
00147 void gtGetText::CallDLL(const ImporterData& idata, const QString& filePath,
00148                         const QString& encoding, bool textOnly, bool append, PageItem* importItem)
00149 {
00150     void* gtplugin;
00151     typedef void (*sdem)(QString filename, QString encoding, bool textOnly, gtWriter *writer);
00152     sdem fp_GetText;
00153     QString pluginFilePath = QString("%1/gettext/%2").arg(ScPaths::instance().pluginDir()).arg(idata.soFilePath);
00154     gtplugin = PluginManager::loadDLL(pluginFilePath);
00155     if (!gtplugin)
00156     {
00157         qWarning("Failed to load plugin %s", pluginFilePath.toAscii().constData());
00158         return;
00159     }
00160     fp_GetText = (sdem) PluginManager::resolveSym(gtplugin, "GetText");
00161     if (!fp_GetText)
00162     {
00163         qWarning("Failed to get GetText() from %s", pluginFilePath.toAscii().constData());
00164         PluginManager::unloadDLL(gtplugin);
00165         return;
00166     }
00167     gtWriter *w = new gtWriter(append, importItem);
00168     (*fp_GetText)(filePath, encoding, textOnly, w);
00169     delete w;
00170     PluginManager::unloadDLL(gtplugin);
00171 }
00172 
00173 bool gtGetText::DLLName(QString name, QString *ffName, QStringList *fEndings)
00174 {
00175     void* gtplugin;
00176     typedef QString (*sdem0)();
00177     typedef QStringList (*sdem1)();
00178     sdem0 fp_FileFormatName;
00179     sdem1 fp_FileExtensions;
00180     QString pluginFilePath = QString("%1/gettext/%2").arg(ScPaths::instance().pluginDir()).arg(name);
00181     gtplugin = PluginManager::loadDLL(pluginFilePath);
00182     if (!gtplugin)
00183     {
00184         qWarning("Failed to load plugin %s", pluginFilePath.toAscii().constData());
00185         return false;
00186     }
00187     fp_FileFormatName = (sdem0) PluginManager::resolveSym( gtplugin, "FileFormatName");
00188     if (!fp_FileFormatName)
00189     {
00190         qWarning("Failed to get FileFormatName() from %s", pluginFilePath.toAscii().constData());
00191         PluginManager::unloadDLL(gtplugin);
00192         return false;
00193     }
00194     fp_FileExtensions = (sdem1) PluginManager::resolveSym( gtplugin, "FileExtensions");
00195     if (!fp_FileExtensions)
00196     {
00197         qWarning("Failed to get FileExtensions() from %s", pluginFilePath.toAscii().constData());
00198         PluginManager::unloadDLL(gtplugin);
00199         return false;
00200     }
00201     *ffName = (*fp_FileFormatName)();
00202     *fEndings = (*fp_FileExtensions)();
00203     PluginManager::unloadDLL(gtplugin);
00204     return true;
00205 }
00206 
00207 void gtGetText::createMap()
00208 {
00209     for (uint i = 0; i < importers.size(); ++i)
00210     {
00211         for (int j = 0; j < importers[i].fileEndings.count(); ++j)
00212                 importerMap.insert(importers[i].fileEndings[j], &importers[i]);
00213     }
00214 }
00215 
00216 gtGetText::~gtGetText()
00217 {
00218 
00219 }
Created with personalized Doxygen. This is documentation for GPL software Scribus - Open Source Desktop Publishing.