KOffice – TDE office suite
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

163 lines
5.5KB

  1. /* This file is part of the KDE project
  2. Copyright (C) 2001-2006 David Faure <faure@kde.org>
  3. Copyright (C) 2004 Nicolas GOUTTE <goutte@kde.org>
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public
  6. License as published by the Free Software Foundation; either
  7. version 2 of the License, or (at your option) any later version.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Library General Public License for more details.
  12. You should have received a copy of the GNU Library General Public License
  13. along with this library; see the file COPYING.LIB. If not, write to
  14. the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  15. * Boston, MA 02110-1301, USA.
  16. */
  17. #include <tqfile.h>
  18. #include <tdeaboutdata.h>
  19. #include <kimageio.h>
  20. #include <tdecmdlineargs.h>
  21. #include <tdelocale.h>
  22. #include <tdeglobal.h>
  23. #include <kmimetype.h>
  24. #include <tdeapplication.h>
  25. #include <kdebug.h>
  26. #include <tdeio/netaccess.h>
  27. #include <KoFilterManager.h>
  28. #include "koconverter.h"
  29. static const TDECmdLineOptions options[]=
  30. {
  31. {"+in", I18N_NOOP("Input file"),0},
  32. {"+out", I18N_NOOP("Output file"),0},
  33. {"backup", I18N_NOOP("Make a backup of the destination file"),0},
  34. {"batch", I18N_NOOP("Batch mode: do not show dialogs"),0},
  35. {"interactive", I18N_NOOP("Interactive mode: show dialogs (default)"),0},
  36. {"mimetype <mime>", I18N_NOOP("Mimetype of the output file"),0},
  37. TDECmdLineLastOption
  38. };
  39. bool convert( const KURL & uIn, const TQString & /*inputFormat*/, const KURL & uOut, const TQString & outputFormat, const bool batch )
  40. {
  41. KoFilterManager* manager = new KoFilterManager( uIn.path() );
  42. ProgressObject progressObj;
  43. TQObject::connect(manager, TQT_SIGNAL(sigProgress(int)), &progressObj, TQT_SLOT(slotProgress(int)));
  44. manager->setBatchMode( batch );
  45. TQCString mime( outputFormat.latin1() );
  46. KoFilter::ConversionStatus status = manager->exp0rt( uOut.path(), mime );
  47. progressObj.slotProgress(-1);
  48. delete manager;
  49. return status == KoFilter::OK;
  50. }
  51. void ProgressObject::slotProgress(int /* progress */ )
  52. {
  53. // Well, we could have a nifty "=====> " progress bar, but with all the
  54. // debug output, it would be badly messed up :)
  55. // kdDebug() << "ProgressObject::slotProgress " << progress << endl;
  56. }
  57. int main( int argc, char **argv )
  58. {
  59. TDEAboutData aboutData( "koconverter", I18N_NOOP("KOConverter"), "1.4",
  60. I18N_NOOP("KOffice Document Converter"),
  61. TDEAboutData::License_GPL,
  62. I18N_NOOP("(c) 2001-2004 KOffice developers") );
  63. aboutData.addAuthor("David Faure",0, "faure@kde.org");
  64. aboutData.addAuthor("Nicolas Goutte",0, "goutte@kde.org");
  65. TDECmdLineArgs::init( argc, argv, &aboutData);
  66. TDECmdLineArgs::addCmdLineOptions( options );
  67. TDEApplication app;
  68. // Install the libkoffice* translations
  69. TDEGlobal::locale()->insertCatalogue("koffice");
  70. KImageIO::registerFormats();
  71. // Get the command line arguments which we have to parse
  72. TDECmdLineArgs *args= TDECmdLineArgs::parsedArgs();
  73. if ( args->count() == 2 )
  74. {
  75. KURL uIn = args->url( 0 );
  76. KURL uOut = args->url( 1 );
  77. // Are we in batch mode or in interactive mode.
  78. bool batch = args->isSet("batch");
  79. if ( args->isSet("interactive") )
  80. batch = false;
  81. if ( args->isSet("backup") )
  82. {
  83. // Code form koDocument.cc
  84. TDEIO::UDSEntry entry;
  85. if ( TDEIO::NetAccess::stat( uOut, entry, 0L ) ) // this file exists => backup
  86. {
  87. kdDebug() << "Making backup..." << endl;;
  88. KURL backup( uOut );
  89. backup.setPath( uOut.path() + '~' );
  90. TDEIO::NetAccess::file_copy( uOut, backup, -1, true /*overwrite*/, false /*resume*/, 0L );
  91. }
  92. }
  93. KMimeType::Ptr inputMimetype = KMimeType::findByURL( uIn );
  94. if ( inputMimetype->name() == KMimeType::defaultMimeType() )
  95. {
  96. kdError() << i18n("Mimetype for input file %1 not found!").arg(uIn.prettyURL()) << endl;
  97. return 1;
  98. }
  99. KMimeType::Ptr outputMimetype;
  100. if ( args->isSet("mimetype") )
  101. {
  102. TQString mime = TQString::fromLatin1( args->getOption("mimetype") );
  103. outputMimetype = KMimeType::mimeType( mime );
  104. if ( outputMimetype->name() == KMimeType::defaultMimeType() )
  105. {
  106. kdError() << i18n("Mimetype not found %1").arg(mime) << endl;
  107. return 1;
  108. }
  109. }
  110. else
  111. {
  112. outputMimetype = KMimeType::findByURL( uOut, 0, false, true /* file doesn't exist */ );
  113. if ( outputMimetype->name() == KMimeType::defaultMimeType() )
  114. {
  115. kdError() << i18n("Mimetype not found, try using the -mimetype option") << endl;
  116. return 1;
  117. }
  118. }
  119. TQApplication::setOverrideCursor( TQt::waitCursor );
  120. bool ok = convert( uIn, inputMimetype->name(), uOut, outputMimetype->name(), batch );
  121. TQApplication::restoreOverrideCursor();
  122. if ( ok )
  123. {
  124. return 0;
  125. }
  126. else
  127. {
  128. kdError() << i18n("*** The conversion failed! ***") << endl;
  129. return 2;
  130. }
  131. }
  132. TDECmdLineArgs::usage(i18n("Two arguments required"));
  133. return 3;
  134. }
  135. #include "koconverter.moc"