Main Page | Class Hierarchy | Class List | File List | Class Members | File Members

convert2text.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *                                                                         *
00003  *   convert2text.cpp                                                      *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   Read the file COPYING for details.                                    *
00011  *                                                                         *
00012  *   copyright: (C) 2003 by Jan Mueller                                    *
00013  *   email: janmueller7@hotmail.com                                        *
00014  *                                                                         *
00015  ***************************************************************************/
00016 
00017 #include "convert2text.h"
00018 #include <stdio.h>
00019 #include <kprocess.h>
00020 #include <kglobal.h>
00021 #include <kconfig.h>
00022 #include <qregexp.h>
00023 #include "error.h"
00024 #include <qsettings.h>
00025 #include <qapplication.h>
00026 #include <kmimemagic.h>
00027 
00028 
00029 Convert2Text::Convert2Text(){
00030   //  tmpfiles.setAutoDelete(true);
00031 }
00032 
00033 Convert2Text::~Convert2Text(){
00034   /*
00035     for(unsigned int i=0; i<tmpfiles.count(); i++) {
00036     tmpfiles.at(i)->remove();
00037     }
00038   */
00039 }
00040 
00041 
00042 QString Convert2Text::buf2file(QByteArray b) {
00043   QString fname=tmpnam(0L);
00044   
00045   QFile *f=new QFile(fname);
00046   if (!f->open(IO_WriteOnly) )
00047     throw Error("Kann kein temporäres File öffnen!");
00048   f->writeBlock(b);
00049   f->close();
00050   tmpfiles.append(f);
00051   return fname;
00052 }
00053 
00054 void Convert2Text::slotReceivedStderr(KProcess* proc,char* buf,int len) {
00055   buf[len-1]=0;
00056   debug("Stderr:"+QString(buf)+"\n");
00057   emit output(QString(QCString(buf, len)));
00058 }
00059 
00060 void Convert2Text::slotReceivedStdout(KProcess* proc,char* buf,int len) {
00061   buf[len-1]=0;
00062   debug("Stdout:"+QString(buf)+"\n");
00063   emit output(QString(QCString(buf, len)));
00064 }
00065 
00066 QString Convert2Text::convert(QString cmd, QString infilename) {
00067   //  cout <<"Convert2Text()\n";
00068 
00069   QString outfilename=tmpnam(0L);
00070   KShellProcess proc;
00071   char *c=new char[200];
00072   cmd.replace(QRegExp("#in#"), infilename);
00073   cmd.replace(QRegExp("#out#"), outfilename);
00074 
00075   connect(&proc, SIGNAL(receivedStderr (KProcess*,char*,int)),
00076           this, SLOT(slotReceivedStderr (KProcess*,char*,int)) );
00077   connect(&proc, SIGNAL(receivedStdout (KProcess*,char*,int)),
00078           this, SLOT(slotReceivedStdout (KProcess*,char*,int)) );
00079 
00080   debug("Executing \""+cmd+"\"\n");
00081   proc << cmd;
00082   //  proc<<"html2text /tmp/test.html > /tmp/test.txt";
00083   if(!proc.start(KProcess::DontCare, KProcess::All))
00084     throw Error("Could not start process!");
00085   
00086   while(proc.isRunning()) {
00087     qApp->processEvents();
00088   }
00089   
00090   debug("Convert2Text, exitstatus:"+QString().setNum(proc.exitStatus())+"\n");
00091   
00092   if(!proc.normalExit())
00093     throw Error("Conversion process: Abnormal termination!");
00094 
00095   QFile f(outfilename);
00096   //  QFile f("/tmp/test.txt");
00097   if(!f.open(IO_ReadOnly))
00098     throw Error("Can not convert file!");
00099 
00100   delete c;
00101   QByteArray b=f.readAll();
00102   f.close();
00103   unlink(outfilename);
00104   unlink(infilename);
00105 
00106   return QString(b);
00107 }
00108 
00109 bool Convert2Text::mime2text(QString infilename, QString &out) {
00110   QSettings settings;
00111 
00112 
00113 #ifdef HAS_KDE
00114   KMimeMagicResult *mime=KMimeMagic::self()->findFileType(infilename);
00115   QString mimetype=mime->mimeType();
00116 #endif
00117 
00118   settings.setPath("www.test.de", "Literature");
00119   bool ok;
00120   debug("Convert2Text::mime2text - "+mimetype);
00121   QString s=settings.readEntry("/literature/mimetotext/"+mimetype, QString::null, &ok);
00122   debug("Befehl ist: "+s);
00123   if(!ok) return false;
00124 
00125   if(s=="#direct#") {
00126     QFile f("/tmp/test_convert");
00127     f.open(IO_ReadOnly);
00128     QString b=f.readAll();
00129     f.close();
00130     return QString(b);
00131   }
00132 
00133   out=QString(convert(s, infilename));
00134   return true;
00135 }

Generated on Sat Mar 27 19:20:38 2004 for Literature by doxygen 1.3.6-20040222