KRename – powerful batch renamer
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.

commandplugin.cpp 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /***************************************************************************
  2. commandplugin.cpp - description
  3. -------------------
  4. begin : Son Jan 5 2003
  5. copyright : (C) 2003 by Dominik Seichter
  6. email : domseichter@web.de
  7. ***************************************************************************/
  8. /***************************************************************************
  9. * *
  10. * This program is free software; you can redistribute it and/or modify *
  11. * it under the terms of the GNU General Public License as published by *
  12. * the Free Software Foundation; either version 2 of the License, or *
  13. * (at your option) any later version. *
  14. * *
  15. ***************************************************************************/
  16. #include "commandplugin.h"
  17. // QT includes
  18. #include <tqcheckbox.h>
  19. #include <tqlabel.h>
  20. #include <tqlayout.h>
  21. // KDE includes
  22. #include <tdeapplication.h>
  23. #include <tdeconfig.h>
  24. #include <kiconloader.h>
  25. #include <tdelocale.h>
  26. #include <klineedit.h>
  27. #include <tdelistbox.h>
  28. #include <tdemessagebox.h>
  29. #include <kpushbutton.h>
  30. #include <kprocess.h>
  31. const TQString CommandPlugin::getName() const
  32. {
  33. return i18n("Command Plugin");
  34. }
  35. const TQString CommandPlugin::getAccelName() const
  36. {
  37. return i18n("&Command Plugin");
  38. }
  39. const int CommandPlugin::type() const
  40. {
  41. return TYPE_FINAL_FILE;
  42. }
  43. bool CommandPlugin::checkError()
  44. {
  45. if( commandline->text().isEmpty() ) {
  46. KMessageBox::error( 0, i18n("You did not specify a command to execute.") );
  47. return false;
  48. }
  49. return true;
  50. }
  51. void CommandPlugin::drawInterface( TQWidget* w, TQVBoxLayout* l )
  52. {
  53. m_widget = w;
  54. TQHBoxLayout* hb = new TQHBoxLayout( 0, 0, 6 );
  55. TQVBoxLayout* vb = new TQVBoxLayout( 0, 0, 6 );
  56. TQLabel* la = new TQLabel( w );
  57. la->setText( i18n("<b>Command Plugin</b>") );
  58. l->addWidget( la );
  59. la = new TQLabel( w );
  60. la->setText( i18n( "<qt>Executes a shell command on every file after it has been renamed. "
  61. "Add %1 to the command line arguments to get the filename of the renamed file.</qt>") );
  62. l->addWidget( la );
  63. l->addWidget( new TQLabel( i18n("Command:"), w ) );
  64. commandline = new KLineEdit( w );
  65. l->addWidget( commandline );
  66. checkNoBlock = new TQCheckBox( i18n("&Execute without blocking (not recommended)"), w );
  67. l->addWidget( checkNoBlock );
  68. buttonAdd = new KPushButton( i18n("&Add"), w );
  69. buttonRemove = new KPushButton( i18n("&Remove"), w );
  70. hb->addWidget( buttonAdd );
  71. hb->addWidget( buttonRemove );
  72. vb->addLayout( hb );
  73. list = new TDEListBox( w );
  74. vb->addWidget( list );
  75. vb->setStretchFactor( list, 2 );
  76. l->addLayout( vb );
  77. connect( buttonAdd, TQT_SIGNAL( clicked() ), this, TQT_SLOT( add() ) );
  78. connect( buttonRemove, TQT_SIGNAL( clicked() ), this, TQT_SLOT( remove() ) );
  79. connect( list, TQT_SIGNAL( executed( TQListBoxItem* ) ), this, TQT_SLOT( exec() ) );
  80. TDEConfig* conf = kapp->config();
  81. conf->setGroup("CommandPlugin");
  82. list->insertStringList( conf->readListEntry("commandlines" ) );
  83. TQStringList examples;
  84. examples.append( "chmod 0444 %1" );
  85. examples.append( "convert %1 %1.png" );
  86. examples.append( "echo %1 >> $HOME/file.list" );
  87. // examples.append( ")
  88. for( unsigned int i = 0; i < examples.count(); i++ )
  89. if( !list->findItem( examples[i] ) )
  90. list->insertItem( examples[i] );
  91. }
  92. void CommandPlugin::fillStructure()
  93. {
  94. command = commandline->text();
  95. noblock = checkNoBlock->isChecked();
  96. }
  97. TQString CommandPlugin::processFile( BatchRenamer* b, int i, TQString, int )
  98. {
  99. TQString filename = b->files()[i].dst.name;
  100. TQString c = command;
  101. KShellProcess proc;
  102. c = c.replace( "%1", KShellProcess::quote( filename ) );
  103. proc << c;
  104. if( noblock )
  105. proc.start( TDEProcess::DontCare, TDEProcess::NoCommunication );
  106. else
  107. proc.start( TDEProcess::Block, TDEProcess::NoCommunication );
  108. proc.resume();
  109. if( !noblock && proc.exitStatus() )
  110. return command.arg( filename ) + i18n(" exited with error: %1").arg( proc.exitStatus() );
  111. return TQString();
  112. }
  113. void CommandPlugin::finished()
  114. {
  115. TDEConfig* conf = kapp->config();
  116. conf->setGroup("CommandPlugin");
  117. TQStringList slist;
  118. for( unsigned int i = 0; i < list->count(); i++ )
  119. slist.append( list->text( i ) );
  120. conf->writeEntry("commandlines", slist );
  121. conf->sync();
  122. return;
  123. }
  124. void CommandPlugin::add()
  125. {
  126. if( !commandline->text().isEmpty() ) {
  127. for( unsigned int i = 0; i < list->count(); i++ )
  128. if( list->text( i ) == commandline->text() )
  129. return;
  130. list->insertItem( commandline->text() );
  131. }
  132. }
  133. void CommandPlugin::remove()
  134. {
  135. unsigned int i = 0;
  136. do {
  137. if(list->isSelected( i ))
  138. list->removeItem( i );
  139. else
  140. i++;
  141. } while( i < list->count() );
  142. }
  143. void CommandPlugin::exec()
  144. {
  145. for( unsigned int i = 0; i < list->count(); i++ )
  146. if( list->isSelected( i ) )
  147. commandline->setText( list->text( i ) );
  148. }
  149. const TQPixmap CommandPlugin::getIcon() const
  150. {
  151. return kapp->iconLoader()->loadIcon( "konsole", TDEIcon::Small );
  152. }
  153. #include "commandplugin.moc"