Exemplo n.º 1
0
void test_block(const QString &fileName)
{
    QIODevice *dev = KFilterDev::deviceForFile(fileName);
    if(!dev)
    {
        kdWarning() << "dev=0" << endl;
        return;
    }
    if(!dev->open(IO_ReadOnly))
    {
        kdWarning() << "open failed " << endl;
        return;
    }

    // This is what KGzipDev::readAll could do, if QIODevice::readAll was virtual....

    QByteArray array(1024);
    int n;
    while((n = dev->readBlock(array.data(), array.size())))
    {
        kdDebug() << "readBlock returned " << n << endl << endl;
        // QCString s(array,n+1); // Terminate with 0 before printing
        // printf("%s", s.data());

        kdDebug() << "dev.at = " << dev->at() << endl;
        // kdDebug() << "f.at = " << f.at() << endl;
    }
    dev->close();
    delete dev;
}
Exemplo n.º 2
0
KDE_EXPORT void kimgio_eps_read(QImageIO *image)
{
    kdDebug(399) << "kimgio EPS: starting..." << endl;

    FILE *ghostfd;
    int x1, y1, x2, y2;
    // QTime dt;
    // dt.start();

    QString cmdBuf;
    QString tmp;

    QIODevice *io = image->ioDevice();
    Q_UINT32 ps_offset, ps_size;

    // find start of PostScript code
    if(!seekToCodeStart(io, ps_offset, ps_size))
        return;

    // find bounding box
    if(!bbox(io, &x1, &y1, &x2, &y2))
    {
        kdError(399) << "kimgio EPS: no bounding box found!" << endl;
        return;
    }

    KTempFile tmpFile;
    tmpFile.setAutoDelete(true);

    if(tmpFile.status() != 0)
    {
        kdError(399) << "kimgio EPS: no temp file!" << endl;
        return;
    }
    tmpFile.close(); // Close the file, we just want the filename

    // x1, y1 -> translation
    // x2, y2 -> new size

    x2 -= x1;
    y2 -= y1;
    // kdDebug(399) << "origin point: " << x1 << "," << y1 << "  size:" << x2 << "," << y2 << endl;
    double xScale = 1.0;
    double yScale = 1.0;
    bool needsScaling = false;
    int wantedWidth = x2;
    int wantedHeight = y2;

    if(image->parameters())
    {
        // Size forced by the caller
        QStringList params = QStringList::split(':', image->parameters());
        if(params.count() >= 2 && x2 != 0.0 && y2 != 0.0)
        {
            wantedWidth = params[0].toInt();
            xScale = (double)wantedWidth / (double)x2;
            wantedHeight = params[1].toInt();
            yScale = (double)wantedHeight / (double)y2;
            // kdDebug(399) << "wanted size:" << wantedWidth << "x" << wantedHeight << endl;
            // kdDebug(399) << "scaling:" << xScale << "," << yScale << endl;
            needsScaling = true;
        }
    }

    // create GS command line

    cmdBuf = "gs -sOutputFile=";
    cmdBuf += tmpFile.name();
    cmdBuf += " -q -g";
    tmp.setNum(wantedWidth);
    cmdBuf += tmp;
    tmp.setNum(wantedHeight);
    cmdBuf += "x";
    cmdBuf += tmp;
    cmdBuf +=
        " -dSAFER -dPARANOIDSAFER -dNOPAUSE -sDEVICE=ppm -c "
        "0 0 moveto "
        "1000 0 lineto "
        "1000 1000 lineto "
        "0 1000 lineto "
        "1 1 254 255 div setrgbcolor fill "
        "0 0 0 setrgbcolor - -c showpage quit";

    // run ghostview

    ghostfd = popen(QFile::encodeName(cmdBuf), "w");

    if(ghostfd == 0)
    {
        kdError(399) << "kimgio EPS: no GhostScript?" << endl;
        return;
    }

    fprintf(ghostfd, "\n%d %d translate\n", -qRound(x1 * xScale), -qRound(y1 * yScale));
    if(needsScaling)
        fprintf(ghostfd, "%g %g scale\n", xScale, yScale);

    // write image to gs

    io->reset();       // Go back to start of file to give all the file to GhostScript
    if(ps_offset > 0L) // We have an offset
        io->at(ps_offset);
    QByteArray buffer(io->readAll());

    // If we have no MS-DOS EPS file or if the size seems wrong, then choose the buffer size
    if(ps_size <= 0L || ps_size > buffer.size())
        ps_size = buffer.size();

    fwrite(buffer.data(), sizeof(char), ps_size, ghostfd);
    buffer.resize(0);

    pclose(ghostfd);

    // load image
    QImage myimage;
    if(myimage.load(tmpFile.name()))
    {
        image->setImage(myimage);
        image->setStatus(0);
        kdDebug(399) << "kimgio EPS: success!" << endl;
    }
    else
        kdError(399) << "kimgio EPS: no image!" << endl;

    // kdDebug(399) << "Loading EPS took " << (float)(dt.elapsed()) / 1000 << " seconds" << endl;
    return;
}
Exemplo n.º 3
0
ConversionStatus GettextImportPlugin::load(const QString& filename, const QString&)
{
   kdDebug( KBABEL ) << k_funcinfo << endl;
   
   if ( filename.isEmpty() ) {
      kdDebug(KBABEL) << "fatal error: empty filename to open" << endl;
      return NO_FILE;
   }

   QFileInfo info(filename);

   if(!info.exists() || info.isDir())
      return NO_FILE;

   if(!info.isReadable())
      return NO_PERMISSIONS;

   QFile file(filename);

   if ( !file.open( IO_ReadOnly ) )
      return NO_PERMISSIONS;
   
   uint oldPercent = 0;
   emit signalResetProgressBar(i18n("loading file"),100);

   QByteArray ba = file.readAll();
   file.close();

   // find codec for file
   bool hadCodec;
   QTextCodec* codec=codecForArray( ba, &hadCodec );
   
   bool recoveredErrorInHeader = false;
   
   QTextStream stream(ba,IO_ReadOnly);

   if(codec)
      stream.setCodec(codec);
   else
   {
      kdWarning() << "No encoding declared or found, using UTF-8" << endl;
      stream.setEncoding( QTextStream::UnicodeUTF8 );
#ifdef __GNUC__
# warning Default UTF-8 encoding needs to be improved
#endif
      // Templates define CHARSET, so if e make it a recoverable error, the template is not loadable anymore, as for templates recoverable errors are disqualifying.
      //recoveredErrorInHeader = true;
   }
   
   QIODevice *dev = stream.device();
   int fileSize = dev->size();

   // if somethings goes wrong with the parsing, we don't have deleted the old contents
   CatalogItem tempHeader;
   QStringList tempObsolete;


   kdDebug(KBABEL) << "start parsing..." << endl;

   // first read header
   const ConversionStatus status = readHeader(stream);


   if ( status == RECOVERED_PARSE_ERROR )
   {
      kdDebug( KBABEL ) << "Recovered error in header entry" << endl;
      recoveredErrorInHeader = true;
   }
   else if ( status != OK )
   {
      emit signalClearProgressBar();

      kdDebug( KBABEL ) << "Parse error in header entry" << endl;
      return status;
   }
   
   kdDebug() << "HEADER MSGID: " << _msgid << endl;
   kdDebug() << "HEADER MSGSTR: " << _msgstr << endl;
   if ( !_msgid.isEmpty() && !_msgid.first().isEmpty() )
   {
      // The header must have an empty msgid
      kdWarning(KBABEL) << "Header entry has non-empty msgid. Creating a temporary header! " << _msgid << endl;
      tempHeader.setMsgid( QStringList() );
      QStringList tmp;
      tmp.append(
         "Content-Type: text/plain; charset=UTF-8\\n" // Unknown charset
         "Content-Transfer-Encoding: 8bit\\n"
         "Mime-Version: 1.0" );
      tempHeader.setMsgstr( tmp );
      // We keep the comment of the first entry, as it might really be a header comment (at least partially)
      const QString comment( "# Header entry was created by KBabel!\n#\n" + _comment );
      tempHeader.setComment( comment );
      recoveredErrorInHeader = true;
   }
   else
   {
      tempHeader.setMsgid( _msgid );
      tempHeader.setMsgstr( _msgstr );
      tempHeader.setComment( _comment );
   }
   if(tempHeader.isFuzzy())
   {
      tempHeader.removeFuzzy();
   }

   // check if header seems to indicate docbook content generated by xml2pot
   const bool docbookContent = (tempHeader.msgstr().find("application/x-xml2pot") != tempHeader.msgstr().end());

   // now parse the rest of the file
   uint counter=0;
   QValueList<uint> errorIndex;
   bool recoveredError=false;
   bool docbookFile=false;

   while( !stream.eof() )
   {
      kapp->processEvents(10);
      if( isStopped() )
      {
         return STOPPED;
      }

      const ConversionStatus success=readEntry(stream);

      if(success==OK)
      {
         if( _obsolete )
         {
               tempObsolete.append(_comment);
         }
         else
         {
               CatalogItem tempCatItem;
               tempCatItem.setMsgctxt( _msgctxt );
               tempCatItem.setMsgid( _msgid );
               tempCatItem.setMsgstr( _msgstr );
               tempCatItem.setComment( _comment );
               tempCatItem.setGettextPluralForm( _gettextPluralForm );
               
               // add new entry to the list of entries
               appendCatalogItem(tempCatItem);
               // check if first comment seems to indicate a docbook source file
               if(counter==0)
                  docbookFile = ( tempCatItem.comment().find(".docbook") != -1 );
         }
      }
      else if(success==RECOVERED_PARSE_ERROR)
      {
         kdDebug( KBABEL ) << "Recovered parse error in entry: " << counter << endl;
         recoveredError=true;
         errorIndex.append(counter);
         
         CatalogItem tempCatItem;
         tempCatItem.setMsgctxt( _msgctxt );
         tempCatItem.setMsgid( _msgid );
         tempCatItem.setMsgstr( _msgstr );
         tempCatItem.setComment( _comment );
         tempCatItem.setGettextPluralForm( _gettextPluralForm );

         // add new entry to the list of entries
         appendCatalogItem(tempCatItem);
      }
      else if ( success == PARSE_ERROR )
      {
         kdDebug( KBABEL ) << "Parse error in entry: " << counter << endl;
         return PARSE_ERROR;
      }
      else
      {
         kdWarning( KBABEL ) << "Unknown success status, assumig parse error " << success << endl;
         return PARSE_ERROR;
      }
      counter++;

      const uint newPercent = (100*dev->at())/fileSize;
      if(newPercent > oldPercent)
      {
         oldPercent = newPercent;
         emit signalProgress(oldPercent);
      }
   }


   // to be sure it is set to 100, if someone don't connect to
   // signalClearProgressBar()
   emit signalProgress(100);

   emit signalClearProgressBar();


   // ### TODO: can we check that there is no useful entry?
   if ( !counter )
   {
      // Empty file? (Otherwise, there would be a try of getting an entry and the count would be 1 !)
      kdDebug( KBABEL ) << k_funcinfo << " Empty file?" << endl;
      return PARSE_ERROR;
   }

   kdDebug(KBABEL) << k_funcinfo << " ready" << endl;

   // We have succesfully loaded the file (perhaps with recovered errors)
   
   setGeneratedFromDocbook(docbookContent || docbookFile);

   setHeader(tempHeader);
   setCatalogExtraData(tempObsolete);
   setErrorIndex(errorIndex);

   if(hadCodec)
      setFileCodec(codec);
   else
      setFileCodec(0);
   
   setMimeTypes( "application/x-gettext" );

   if ( recoveredErrorInHeader )
   {
      kdDebug( KBABEL ) << k_funcinfo << " Returning: header error" << endl;
      return RECOVERED_HEADER_ERROR;
   }
   else if ( recoveredError )
   {
      kdDebug( KBABEL ) << k_funcinfo << " Returning: recovered parse error" << endl;
      return RECOVERED_PARSE_ERROR;
   }
   else
   {
      kdDebug( KBABEL ) << k_funcinfo << " Returning: OK! :-)" << endl;
      return OK;
   }
}