Example #1
0
KoFilter::ConversionStatus HancomWordImport::convert( const QByteArray& from, const QByteArray& to )
{
  if ( from != "application/x-hwp" )
    return KoFilter::NotImplemented;

  if ( to != "application/vnd.oasis.opendocument.text" )
    return KoFilter::NotImplemented;

  d->inputFile = m_chain->inputFile();
  d->outputFile = m_chain->outputFile();
  d->paragraphs.clear();

  POLE::Storage storage( QFile::encodeName(d->inputFile) );
  if( !storage.open() )
    return KoFilter::WrongFormat;

  POLE::Stream* stream;
  stream = new POLE::Stream( &storage, "/PrvText" );
  if( stream->fail() || (stream->size() == 0) )
  {
    delete stream;
    return KoFilter::WrongFormat;
  }

  int len = stream->size() / 2;
  QString plaindoc;
  plaindoc.reserve( len );

  unsigned char* buf = new unsigned char [stream->size()];
  stream->read( buf, stream->size());
  for(int i = 0; i < len; i++ )
    plaindoc.append( QChar((int)readU16(buf + i*2) ) );
  delete[] buf;
  delete stream;

  // split into paragraphs
  d->paragraphs = QStringList::split( "\n", plaindoc, true );

  // create output store
  KoStore* storeout;
  storeout = KoStore::createStore( d->outputFile, KoStore::Write,
    "application/vnd.oasis.opendocument.text", KoStore::Zip );

  if ( !storeout )
  {
    kWarning() << "Couldn't open the requested file.";
    return KoFilter::FileNotFound;
  }

  if ( !storeout->open( "styles.xml" ) )
  {
    kWarning() << "Couldn't open the file 'styles.xml'.";
    return KoFilter::CreationError;
  }
  storeout->write( d->createStyles() );
  storeout->close();

  if ( !storeout->open( "content.xml" ) )
  {
    kWarning() << "Couldn't open the file 'content.xml'.";
    return KoFilter::CreationError;
  }
  storeout->write( d->createContent() );
  storeout->close();

  // store document manifest
  storeout->enterDirectory( "META-INF" );
  if ( !storeout->open( "manifest.xml" ) )
  {
     kWarning() << "Couldn't open the file 'META-INF/manifest.xml'.";
     return KoFilter::CreationError;
  }
  storeout->write( d->createManifest() );
  storeout->close();

  // we are done!
  d->inputFile.clear();
  d->outputFile.clear();
  delete storeout;

  return KoFilter::OK;
}
Example #2
0
bool OdfReader::readContent(OdfReaderBackend *backend, OdfReaderContext *context)
{
    debugOdfReader << "entering";

    m_backend = backend;
    m_context = context;

    if (m_textReader) {
        m_textReader->setContext(context);
    }

    // ----------------------------------------------------------------
    // Read the body from content.xml

    KoStore *odfStore = m_context->odfStore();

    if (!odfStore->open("content.xml")) {
        errorOdfReader << "Unable to open input file content.xml" << endl;
        return false;
    }
    debugOdfReader << "open content.xml ok";

    KoXmlStreamReader reader;
    prepareForOdfInternal(reader);

    reader.setDevice(odfStore->device());
    bool  foundContent = false;
    while (!reader.atEnd()) {
        reader.readNext();

        if (reader.isStartElement() && reader.qualifiedName() == "office:document-content") {
            foundContent = true;
            break;
        }
    }
    if (!foundContent) {
        errorOdfReader << "Couldn't find the content in content.xml" << endl;
    }

    m_backend->elementOfficeDocumentcontent(reader, m_context);

    // <office:document-content> has the following children in ODF 1.2:
    //          <office:automatic-styles> 3.15.3
    //   [done] <office:body> 3.3
    //          <office:font-face-decls> 3.14
    //          <office:scripts> 3.12.
    while (reader.readNextStartElement()) {
        QString tagName = reader.qualifiedName().toString();
        
        if (tagName == "office:automatic-styles") {
            // We already have the styles in the context.  No need to read them again.
            reader.skipCurrentElement();
        }
        else if (tagName == "office:body") {
            // This is the big one.
            readElementOfficeBody(reader);
        }
        else if (tagName == "office:font-face-decls") {
            // FIXME: Not yet implemented
            reader.skipCurrentElement();
        }
        else if (tagName == "office:scripts") {
            // FIXME: Not yet implemented
            reader.skipCurrentElement();
        }
        else {
            reader.skipCurrentElement();
        }
    }

    m_backend->elementOfficeDocumentcontent(reader, m_context);
    odfStore->close();

    return true;
}
Example #3
0
bool KoScriptingOdfStore::hasFile(const QString &fileName)
{
    KoStore *store = getReadStore();
    return store ? store->hasFile(fileName) : false;
}
Example #4
0
KoFilter::ConversionStatus KWord13Import::convert( const QByteArray& from, const QByteArray& to )
{
    if ( to != "application/vnd.oasis.opendocument.text"
        || from != "application/x-kword" )
    {
        return KoFilter::NotImplemented;
    }

    // We need KimageIO's help in OOWriterWorker::convertUnknownImage


    KWord13Document kwordDocument;

    const QString fileName( m_chain->inputFile() );
    if ( fileName.isEmpty() )
    {
        kError(30520) << "No input file name!";
        return KoFilter::StupidError;
    }

    KoStore* store = KoStore::createStore( fileName, KoStore::Read );
    if ( store && store->hasFile( "maindoc.xml" ) )
    {
        kDebug(30520) <<"Maindoc.xml found in KoStore!";

        // We do not really care about errors while reading/parsing documentinfo
        store->open( "documentinfo.xml" );
        KoStoreDevice ioInfo( store );
        ioInfo.open( QIODevice::ReadOnly );
        kDebug (30520) <<"Processing document info...";
        if ( ! parseInfo ( &ioInfo, kwordDocument ) )
        {
            kWarning(30520) << "Parsing documentinfo.xml has failed. Ignoring!";
        }
        ioInfo.close();
        store->close();

        // ### TODO: error return values
        if ( ! store->open( "maindoc.xml" ) )
        {
            kError(30520) << "Opening root has failed";
            delete store;
            return KoFilter::StupidError;
        }
        KoStoreDevice ioMain( store );
        ioMain.open( QIODevice::ReadOnly );
        kDebug (30520) <<"Processing root...";
        if ( ! parseRoot ( &ioMain, kwordDocument ) )
        {
            kWarning(30520) << "Parsing maindoc.xml has failed! Aborting!";
            delete store;
            return KoFilter::StupidError;
        }
        ioMain.close();
        store->close();

        if ( store->open( "preview.png" ) )
        {

            kDebug(30520) <<"Preview found!";
            KoStoreDevice ioPreview( store );
            ioPreview.open( QIODevice::ReadOnly );
            const QByteArray image ( ioPreview.readAll() );
            if ( image.isNull() )
            {
                kWarning(30520) << "Loading of preview failed! Ignoring!";
            }
            else
            {
                kwordDocument.m_previewFile = new KTemporaryFile();
                kwordDocument.m_previewFile->setSuffix(".png");
                kwordDocument.m_previewFile->open(); // ### TODO check KTemporaryFile
                // ### TODO: check if file is correctly written
                kwordDocument.m_previewFile->write( image );
                kwordDocument.m_previewFile->close();
            }
            ioPreview.close();
            store->close();
        }
        else
        {
            kDebug(30520) <<"No preview found!";
        }
    }
    else
    {
        kWarning(30520) << "Opening store has failed. Trying raw XML file!";
        // Be sure to undefine store
        delete store;
        store = 0;

        QFile file( fileName );
        file.open( QIODevice::ReadOnly );
        if ( ! parseRoot( &file, kwordDocument ) )
        {
            kError(30520) << "Could not process document! Aborting!";
            file.close();
            return KoFilter::StupidError;
        }
        file.close();
    }

    if ( store && ! postParse( store, kwordDocument ) )
    {
        kError(30520) << "Error during post-parsing! Aborting!";
        return  KoFilter::StupidError;
    }

    // We have finished with the input store/file, so close the store (already done for a raw XML file)
    kDebug(30520) <<"Deleting input store...";
    delete store;
    store = 0;
    kDebug(30520) <<"Input store deleted!";

    KWord13OasisGenerator generator;

    kDebug(30520) << __FILE__ <<":" << __LINE__;

    if ( ! generator.prepare( kwordDocument ) )
    {
        kError(30520) << "Could not prepare the OASIS document! Aborting!";
        return KoFilter::StupidError;
    }

    const QString filenameOut ( m_chain->outputFile() );

    if ( filenameOut.isEmpty() )
    {
        kError(30520) << "Empty file name for saving as OASIS! Aborting!";
        return KoFilter::StupidError;
    }

    if ( ! generator.generate( filenameOut, kwordDocument ) )
    {
        kError(30520) << "Could not save as OASIS! Aborting!";
        return KoFilter::StupidError;
    }

    kDebug(30520) <<"Filter has finished!";

    return KoFilter::OK;
}
Example #5
0
KoFilter::ConversionStatus WPGImport::convert( const QByteArray& from, const QByteArray& to )
{
  if ( from != "application/x-wpg" )
    return KoFilter::NotImplemented;

  if ( to != "application/vnd.oasis.opendocument.graphics" )
    return KoFilter::NotImplemented;


  WPXInputStream* input = new libwpg::WPGFileStream( m_chain->inputFile().toLocal8Bit() );
  if (input->isOLEStream())
  {
    WPXInputStream* olestream = input->getDocumentOLEStream();
    if (olestream)
    {
      delete input;
      input = olestream;
    }
  }

  if (!libwpg::WPGraphics::isSupported(input))
  {
    std::cerr << "ERROR: Unsupported file format (unsupported version) or file is encrypted!" << std::endl;
    delete input;
    return KoFilter::NotImplemented;
  }

  // do the conversion
  std::ostringstream tmpStringStream;
  FileOutputHandler tmpHandler(tmpStringStream);
  OdgExporter exporter(&tmpHandler);
  libwpg::WPGraphics::parse(input, &exporter);
  delete input;


  // create output store
  KoStore* storeout;
  storeout = KoStore::createStore( m_chain->outputFile(), KoStore::Write,
    "application/vnd.oasis.opendocument.graphics", KoStore::Zip );

  if ( !storeout )
  {
    kWarning() << "Couldn't open the requested file.";
    return KoFilter::FileNotFound;
  }

#if 0
  if ( !storeout->open( "styles.xml" ) )
  {
    kWarning() << "Couldn't open the file 'styles.xml'.";
    return KoFilter::CreationError;
  }
  //storeout->write( createStyles() );
  storeout->close();
#endif

  if ( !storeout->open( "content.xml" ) )
  {
    kWarning() << "Couldn't open the file 'content.xml'.";
    return KoFilter::CreationError;
  }
  storeout->write(tmpStringStream.str().c_str());
  storeout->close();

  // store document manifest
  storeout->enterDirectory( "META-INF" );
  if ( !storeout->open( "manifest.xml" ) )
  {
     kWarning() << "Couldn't open the file 'META-INF/manifest.xml'.";
     return KoFilter::CreationError;
  }
  storeout->write( createManifest() );
  storeout->close();

  // we are done!
  delete storeout;

  return KoFilter::OK;
}