Back to index

scribus-ng  1.3.4.dfsg+svn20071115
docim.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 #include "docim.h"
00009 #include "docim.moc"
00010 #include "gtwriter.h"
00011 #include "scpaths.h"
00012 #include "scribusstructs.h"
00013 #include <qobject.h>
00014 #include <qcstring.h>
00015 #include <qmessagebox.h>
00016 #include <qprocess.h>
00017 #include <qstring.h>
00018 #include <qstringlist.h>
00019 #include <qtextcodec.h>
00020 
00021 #if defined(_WIN32)
00022 #include <windows.h>
00023 #endif
00024 
00025 bool hasAntiword()
00026 {
00027        static bool searched = false, found = false;
00028        if (searched) // searched already in this run
00029               return found;
00030 
00031        QProcess *test = new QProcess();
00032 #if defined(_WIN32)
00033        test->addArgument( ScPaths::instance().libDir() + "tools/antiword/antiword.exe" );
00034 #else
00035        test->addArgument("antiword");
00036 #endif
00037        if (test->start())
00038        {
00039               found = true;
00040               test->tryTerminate();
00041 #ifndef _WIN32
00042               usleep(5000);
00043 #else
00044               Sleep(5);
00045 #endif
00046               test->kill(); 
00047        }
00048        delete test;
00049        searched = true;
00050        return found;
00051 }
00052 
00053 QString FileFormatName()
00054 {
00055        if (hasAntiword())
00056        return QObject::tr("Word Documents");
00057        else
00058               return QString::null;
00059 }
00060 
00061 QStringList FileExtensions()
00062 {
00063        if (hasAntiword())
00064        return QStringList("doc");
00065        else
00066               return QStringList();
00067 }
00068 
00069 void GetText(QString filename, QString encoding, bool textOnly, gtWriter *writer)
00070 {
00071        if (!hasAntiword())
00072               return;
00073 
00074        DocIm *dim = new DocIm(filename, encoding, textOnly, writer);
00075        while (dim->isRunning())
00076        {
00077 #ifndef _WIN32
00078               usleep(5000);
00079 #else
00080               Sleep(5);
00081 #endif
00082        }
00083        delete dim;
00084 }
00085 
00086 DocIm::DocIm(const QString& fname, const QString& enc, bool textO, gtWriter *w) : QObject()
00087 {
00088        filename = fname;
00089        encoding = enc;
00090        writer = w;
00091        textOnly = textO;
00092        failed = false;
00093        QTextCodec *codec;
00094        if (encoding.isEmpty())
00095               codec = QTextCodec::codecForLocale();
00096        else
00097               codec = QTextCodec::codecForName(encoding);
00098        QTextCodec::setCodecForCStrings(codec);
00099        text = "";
00100        error = "";
00101        proc = new QProcess();
00102 #if defined(_WIN32)
00103        proc->addArgument( ScPaths::instance().libDir() + "tools/antiword/antiword.exe" );
00104        proc->setWorkingDirectory( ScPaths::instance().libDir() + "tools/antiword/" ); 
00105 #else
00106        proc->addArgument("antiword");
00107 #endif
00108        proc->addArgument("-t");
00109        proc->addArgument("-w 0");
00110        proc->addArgument(filename);
00111        //connect(proc, SIGNAL(readyReadStdout()), this, SLOT(slotReadOutput()));
00112        //connect(proc, SIGNAL(readyReadStderr()), this, SLOT(slotReadErr()));
00113 #if defined(_WIN32)
00114        QStringList envVar;
00115        QString homeDir =  QDir::convertSeparators(ScPaths::instance().libDir() + "tools");
00116        envVar.append( QString("HOME=%1").arg(homeDir) );
00117        if (!proc->start(&envVar))
00118        {
00119               failed = true;
00120               return;
00121        }      
00122 #else
00123        if (!proc->start())
00124        {
00125               failed = true;
00126               return;
00127        }
00128 #endif
00129 
00130        while(proc->isRunning() || proc->canReadLineStdout() || proc->canReadLineStderr())
00131        {
00132               if ( proc->canReadLineStdout() )
00133               {
00134                      QByteArray bo = proc->readStdout();
00135                      if (bo.size() > 0)
00136                             text += QString(bo);
00137               }
00138               else if (proc->canReadLineStderr())
00139               {
00140                      QByteArray be = proc->readStderr();
00141                      if (be.size() > 0)
00142                             error += QString(be);
00143               }
00144               else
00145               {
00146               #ifndef _WIN32
00147                      usleep(5000);
00148               #else
00149                      Sleep(5);
00150               #endif
00151               }
00152        }
00153 
00154        if (proc->normalExit()) 
00155        {
00156               toUnicode();
00157               write();
00158        } 
00159        else
00160               failed = true;
00161 }
00162 
00163 bool DocIm::isRunning()
00164 {
00165        return proc->isRunning();
00166 }
00167 
00168 void DocIm::write()
00169 {
00170        if (!failed)
00171               writer->append(text);
00172        else
00173               QMessageBox::information(0, tr("Importing failed"),
00174                                        tr("Importing Word document failed \n%1").arg(error),
00175                                        QMessageBox::Ok);
00176 }
00177 
00178 void DocIm::toUnicode()
00179 {
00180        QTextCodec *codec;
00181        if (encoding.isEmpty())
00182               codec = QTextCodec::codecForLocale();
00183        else
00184               codec = QTextCodec::codecForName(encoding);
00185        QString dec = codec->toUnicode( text );
00186        text = dec;
00187 }
00188 
00189 DocIm::~DocIm()
00190 {
00191        delete proc;
00192 }
00193 
00194