Subversion Repositories Scribus

Rev

Rev 4661 | Rev 4919 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
4655 tsoots 1
/*
2
For general Scribus (>=1.3.2) copyright and licensing information please refer
3
to the COPYING file provided with the program. Following this notice may exist
4
a copyright and/or license notice that predates the release of Scribus 1.3.2
5
for which a new license (GPL+exception) is in place.
6
*/
7
 
8
#include "docim.h"
9
#include "docim.moc"
4661 tsoots 10
#include "gtwriter.h"
4677 cbradney 11
#include "scpaths.h"
4655 tsoots 12
#include "scribusstructs.h"
13
#include <qobject.h>
14
#include <qcstring.h>
15
#include <qmessagebox.h>
16
#include <qprocess.h>
17
#include <qstring.h>
18
#include <qstringlist.h>
19
#include <qtextcodec.h>
20
 
4677 cbradney 21
#if defined(_WIN32)
22
#include <windows.h>
23
#endif
24
 
4661 tsoots 25
bool hasAntiword()
26
{
27
        static bool searched = false, found = false;
28
        if (searched) // searched already in this run
29
                return found;
30
 
31
        QProcess *test = new QProcess();
4677 cbradney 32
#if defined(_WIN32)
33
        test->addArgument( ScPaths::instance().libDir() + "tools/antiword/antiword.exe" );
34
#else
4661 tsoots 35
        test->addArgument("antiword");
4677 cbradney 36
#endif
4661 tsoots 37
        if (test->start())
38
        {
39
                found = true;
40
                test->tryTerminate();
41
#ifndef _WIN32
42
                usleep(5000);
43
#else
44
                Sleep(5);
45
#endif
4677 cbradney 46
                test->kill();  
4661 tsoots 47
        }
4677 cbradney 48
        delete test;
4661 tsoots 49
        searched = true;
50
        return found;
51
}
52
 
4655 tsoots 53
QString FileFormatName()
54
{
4661 tsoots 55
        if (hasAntiword())
56
        return QObject::tr("Word Documents");
57
        else
58
                return QString();
4655 tsoots 59
}
60
 
61
QStringList FileExtensions()
62
{
4661 tsoots 63
        if (hasAntiword())
64
        return QStringList("doc");
65
        else
66
                return QStringList();
4655 tsoots 67
}
68
 
69
void GetText(QString filename, QString encoding, bool textOnly, gtWriter *writer)
70
{
4661 tsoots 71
        if (!hasAntiword())
72
                return;
73
 
4655 tsoots 74
        DocIm *dim = new DocIm(filename, encoding, textOnly, writer);
75
        while (dim->isRunning())
76
        {
77
#ifndef _WIN32
78
                usleep(5000);
79
#else
80
                Sleep(5);
81
#endif
82
        }
83
        delete dim;
84
}
85
 
86
DocIm::DocIm(const QString& fname, const QString& enc, bool textO, gtWriter *w) : QObject()
87
{
88
        filename = fname;
89
        encoding = enc;
90
        writer = w;
91
        textOnly = textO;
92
        failed = false;
93
        QTextCodec *codec;
94
        if (encoding.isEmpty())
95
                codec = QTextCodec::codecForLocale();
96
        else
97
                codec = QTextCodec::codecForName(encoding);
98
        QTextCodec::setCodecForCStrings(codec);
99
        text = "";
100
        error = "";
101
        proc = new QProcess();
4677 cbradney 102
#if defined(_WIN32)
103
        proc->addArgument( ScPaths::instance().libDir() + "tools/antiword/antiword.exe" );
104
        proc->setWorkingDirectory( ScPaths::instance().libDir() + "tools/antiword/" );
105
#else
4655 tsoots 106
        proc->addArgument("antiword");
4677 cbradney 107
#endif
4655 tsoots 108
        proc->addArgument("-t");
109
        proc->addArgument("-w 0");
110
        proc->addArgument(filename);
111
        connect(proc, SIGNAL(readyReadStdout()), this, SLOT(slotReadOutput()));
112
        connect(proc, SIGNAL(readyReadStderr()), this, SLOT(slotReadErr()));
4677 cbradney 113
#if defined(_WIN32)
114
        QStringList envVar;
115
        QString homeDir =  QDir::convertSeparators(ScPaths::instance().libDir() + "tools");
116
        envVar.append( QString("HOME=%1").arg(homeDir) );
117
        if (!proc->start(&envVar))
118
        {
4655 tsoots 119
                failed = true;
4677 cbradney 120
                return;
121
        }      
122
#else
123
        if (proc->start())
124
        {
125
                failed = true;
126
                return;
4655 tsoots 127
        }
4677 cbradney 128
#endif
129
 
130
        while(proc->isRunning())
131
        {
132
        #ifndef _WIN32
133
                usleep(5000);
134
        #else
135
                Sleep(5);
136
        #endif
137
        }
138
 
139
        if (proc->normalExit())
140
        {
141
                toUnicode();
142
                write();
143
        }
144
        else
145
                failed = true;
4655 tsoots 146
}
147
 
148
bool DocIm::isRunning()
149
{
150
        return proc->isRunning();
151
}
152
 
153
void DocIm::write()
154
{
155
        if (!failed)
156
                writer->append(text);
157
        else
158
                QMessageBox::information(0, tr("Importing failed"),
159
                                         tr("Importing Word document failed \n%1").arg(error),
160
                                         QMessageBox::Ok);
161
}
162
 
163
void DocIm::toUnicode()
164
{
165
        QTextCodec *codec;
166
        if (encoding.isEmpty())
167
                codec = QTextCodec::codecForLocale();
168
        else
169
                codec = QTextCodec::codecForName(encoding);
170
        QString dec = codec->toUnicode( text );
171
        text = dec;
172
}
173
 
174
void DocIm::slotReadOutput()
175
{
176
        QByteArray bb = proc->readStdout();
177
        if (bb)
178
                text += QString(bb);
179
}
180
 
181
void DocIm::slotReadErr()
182
{
183
        QByteArray bb = proc->readStderr();
184
        if (bb)
185
                error += QString(bb);
186
}
187
 
188
DocIm::~DocIm()
189
{
4661 tsoots 190
        delete proc;
4655 tsoots 191
}
192
 
193