Back to index

scribus-ng  1.3.4.dfsg+svn20071115
txtim.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 #include <qobject.h>
00008 #include <qcstring.h>
00009 #include <qfile.h>
00010 #include <qfileinfo.h>
00011 #include <qstring.h>
00012 #include <qstringlist.h>
00013 #include <qtextcodec.h> 
00014 
00015 #include "txtim.h"
00016 #include "scribusstructs.h"
00017 
00018 QString FileFormatName()
00019 {
00020     return QObject::tr("Text Files");
00021 }
00022 
00023 QStringList FileExtensions()
00024 {
00025     return QStringList("txt");
00026 }
00027 
00028 void GetText(QString filename, QString encoding, bool textOnly, gtWriter *writer)
00029 {
00030        TxtIm* tim = new TxtIm(filename, encoding, textOnly, writer);
00031        tim->write();
00032        delete tim;
00033 }
00034 
00035 TxtIm::TxtIm(const QString& fname, const QString& enc, bool textO, gtWriter *w)
00036 {
00037        filename = fname;
00038        encoding = enc;
00039        writer = w;
00040        textOnly = textO;
00041        loadText();
00042        toUnicode();
00043 }
00044 
00045 void TxtIm::write()
00046 {
00047        writer->append(text);
00048 }
00049 
00050 void TxtIm::loadText()
00051 {
00052        text = "";
00053        QFile f(filename);
00054        QFileInfo fi(f);
00055        if (!fi.exists())
00056               return;
00057        uint posi;
00058        QByteArray bb(f.size());
00059        if (f.open(IO_ReadOnly))
00060        {
00061               f.readBlock(bb.data(), f.size());
00062               f.close();
00063               for (posi = 0; posi < bb.size(); ++posi)
00064                      text += QChar(bb[posi]);
00065        }
00066 }
00067 
00068 void TxtIm::toUnicode()
00069 {
00070        QTextCodec *codec;
00071        if (encoding.isEmpty())
00072               codec = QTextCodec::codecForLocale();
00073        else
00074               codec = QTextCodec::codecForName(encoding);
00075        QString dec = codec->toUnicode( text );
00076        text = dec;
00077 }
00078 
00079 TxtIm::~TxtIm()
00080 {
00081 
00082 }