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.

ProgressDialog.cpp 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /***************************************************************************
  2. ProgressDialog.cpp - description
  3. -------------------
  4. begin : Die Mai 15 15:34:19 CEST 2001
  5. copyright : (C) 2001 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. // QT Includes
  17. #include <tqlayout.h>
  18. #include <tqprogressbar.h>
  19. #include <tqtimer.h>
  20. #include <tqtooltip.h>
  21. // KDE includes
  22. #include <tdeapplication.h>
  23. #include <kiconloader.h>
  24. #include <tdelistview.h>
  25. #include <tdelocale.h>
  26. #include <tdepopupmenu.h>
  27. #include <kpushbutton.h>
  28. #include <krun.h>
  29. #include <tqfile.h>
  30. // Own includes
  31. #include "ProgressDialog.h"
  32. #include "krenameimpl.h"
  33. #include "undodialog.h"
  34. #include "fileoperation.h"
  35. #define MNU_ERROR_ID 33999
  36. #define MNU_DONE_ID 33998
  37. // update user interface every 1/2 second
  38. #define TIMER_INTERVAL 500
  39. ProgressDialog::ProgressDialog( TQWidget* parent, const char* name, WFlags fl )
  40. : TQWidget( parent, name, fl )
  41. {
  42. renamedFiles = NULL;
  43. resize( 400, 380 );
  44. setCaption( i18n("Progress") );
  45. setIcon( BarIcon( "krename" ) );
  46. ProgressDialogLayout = new TQVBoxLayout( this, 11, 6 );
  47. Layout1 = new TQHBoxLayout( 0, 0, 6 );
  48. TQHBoxLayout* Layout2 = new TQHBoxLayout( 0, 6, 6 );
  49. bar = new TQProgressBar( this );
  50. bar->setProgress( 0 );
  51. buttonCancel = new KPushButton( i18n("&Cancel"), this );
  52. display = new TDEListView( this );
  53. display->addColumn( i18n("Messages") );
  54. display->addColumn( "sort" );
  55. display->setColumnWidthMode( 0, TQListView::Maximum );
  56. display->setColumnWidthMode( 1, TQListView::Manual );
  57. display->setColumnWidth( 1, 0 );
  58. display->setSorting( -1 );
  59. display->setUpdatesEnabled( false );
  60. TQSpacerItem* spacer = new TQSpacerItem( 20, 20, TQSizePolicy::Minimum, TQSizePolicy::Minimum );
  61. buttonUndo = new KPushButton( this );
  62. buttonUndo->setText( i18n("&Undo") );
  63. buttonUndo->setEnabled( false );
  64. mnuButton = new TDEPopupMenu( this );
  65. mnuButton->insertItem( i18n("Restart KRename..."), this, TQT_SLOT( restart() ));
  66. mnuButton->insertSeparator();
  67. mnuButton->insertItem( i18n("Rename Processed Files &Again..."), this, TQT_SLOT( again() ), 0, MNU_DONE_ID );
  68. mnuButton->insertItem( i18n("Rename Unprocessed Files &Again..."), this, TQT_SLOT( unAgain() ), 0, MNU_ERROR_ID );
  69. mnuButton->insertItem( i18n("Rename All Files &Again..."), this, TQT_SLOT( allAgain() ));
  70. buttonRestart = new KPushButton( this );
  71. buttonRestart->setText( i18n( "&Rename More..." ) );
  72. buttonRestart->setPopup( mnuButton );
  73. buttonRestart->setEnabled( false );
  74. buttonOpenDest = new KPushButton( this );
  75. buttonOpenDest->setText( i18n("&Open Destination...") );
  76. buttonClose = new KPushButton( this );
  77. buttonClose->setText( i18n( "&Close" ) );
  78. buttonClose->setEnabled( false );
  79. Layout1->addItem( spacer );
  80. Layout1->addWidget( buttonUndo );
  81. Layout1->addWidget( buttonRestart );
  82. Layout1->addWidget( buttonOpenDest );
  83. Layout1->addWidget( buttonClose );
  84. Layout1->addItem( spacer );
  85. Layout2->addWidget( bar );
  86. Layout2->addWidget( buttonCancel );
  87. Layout2->setStretchFactor( bar, 2 );
  88. ProgressDialogLayout->addLayout( Layout2 );
  89. ProgressDialogLayout->addWidget( display );
  90. ProgressDialogLayout->addLayout( Layout1 );
  91. connect( buttonClose, TQT_SIGNAL(clicked()), this, TQT_SLOT(quitAll()));
  92. connect( buttonOpenDest, TQT_SIGNAL(clicked()), this, TQT_SLOT(openDest()));
  93. connect( buttonCancel, TQT_SIGNAL( clicked()), this, TQT_SLOT(canceled() ));
  94. connect( buttonUndo, TQT_SIGNAL( clicked() ), this, TQT_SLOT( undo() ) );
  95. m_count = 0;
  96. m_canceled = false;
  97. m_timer = new TQTimer( this, "m_timer" );
  98. connect( m_timer, TQT_SIGNAL( timeout() ), this, TQT_SLOT( slotTimer() ) );
  99. hide();
  100. }
  101. ProgressDialog::~ProgressDialog()
  102. {
  103. delete m_timer;
  104. if( renamedFiles )
  105. delete [] renamedFiles;
  106. }
  107. void ProgressDialog::print( TQString text, TQString pixmap )
  108. {
  109. if( !m_timer->isActive() )
  110. {
  111. TDEApplication::setOverrideCursor( TQt::waitCursor );
  112. m_timer->start( TIMER_INTERVAL );
  113. }
  114. // set an icon if we have none yet
  115. if( pixmap.isNull() )
  116. pixmap = "ok";
  117. TDEListViewItem* item = new TDEListViewItem( display, text, count() );
  118. item->setPixmap( 0, SmallIcon(pixmap) );
  119. display->insertItem( item );
  120. }
  121. void ProgressDialog::error( TQString text )
  122. {
  123. if( !m_timer->isActive() )
  124. {
  125. TDEApplication::setOverrideCursor( TQt::waitCursor );
  126. m_timer->start( TIMER_INTERVAL );
  127. }
  128. //TODO: simplify this (breaks i18n)
  129. TDEListViewItem* item = new TDEListViewItem( display, i18n("Error: %1").arg( TQString() ) + simplify( text ), count() );
  130. item->setPixmap( 0, SmallIcon("cancel") );
  131. display->insertItem( item );
  132. }
  133. void ProgressDialog::warning( TQString text )
  134. {
  135. if( !m_timer->isActive() )
  136. {
  137. TDEApplication::setOverrideCursor( TQt::waitCursor );
  138. m_timer->start( TIMER_INTERVAL );
  139. }
  140. TDEListViewItem* item = new TDEListViewItem( display, i18n("Warning: %1").arg( TQString() ) + simplify( text ), count() );
  141. item->setPixmap( 0, SmallIcon("idea") );
  142. display->insertItem( item );
  143. }
  144. void ProgressDialog::quitAll()
  145. {
  146. TDEApplication::exit(0);
  147. }
  148. void ProgressDialog::done( int errors, int successfull, bool allowundo )
  149. {
  150. m_timer->stop();
  151. TDEApplication::restoreOverrideCursor();
  152. display->setUpdatesEnabled( true );
  153. display->setSorting( 1, true );
  154. display->sort();
  155. display->setSorting( -1 );
  156. display->triggerUpdate();
  157. display->ensureItemVisible( display->lastItem() );
  158. bar->setProgress( bar->totalSteps() );
  159. mnuButton->setItemEnabled( MNU_ERROR_ID, errors );
  160. mnuButton->setItemEnabled( MNU_DONE_ID, successfull );
  161. buttonClose->setEnabled( true );
  162. buttonRestart->setEnabled( true );
  163. if( allowundo )
  164. buttonUndo->setEnabled( true );
  165. buttonCancel->setEnabled( false );
  166. kapp->processEvents( 0 );
  167. }
  168. void ProgressDialog::show()
  169. {
  170. TQWidget::show();
  171. int w = kapp->desktop()->width();
  172. int h = kapp->desktop()->height();
  173. move( (w-width())/2,(h-height())/2 );
  174. }
  175. void ProgressDialog::restart()
  176. {
  177. TQWidget* krename = KRenameImpl::launch( TQRect( 0, 0, 0, 0 ), KURL::List() );
  178. krename->show();
  179. close();
  180. }
  181. void ProgressDialog::again()
  182. {
  183. KURL::List list;
  184. for( unsigned int i = 0; i < m_size; i++ )
  185. if( !renamedFiles[i].error )
  186. list.append( renamedFiles[i].dst );
  187. TQWidget* krename = KRenameImpl::launch( TQRect( 0, 0, 0, 0 ), list );
  188. krename->show();
  189. close();
  190. }
  191. void ProgressDialog::unAgain()
  192. {
  193. KURL::List list;
  194. for( unsigned int i = 0; i < m_size; i++ )
  195. if( renamedFiles[i].error )
  196. list.append( renamedFiles[i].src );
  197. TQWidget* krename = KRenameImpl::launch( TQRect( 0, 0, 0, 0 ), list );
  198. krename->show();
  199. close();
  200. }
  201. void ProgressDialog::allAgain()
  202. {
  203. KURL::List list;
  204. for( unsigned int i = 0; i < m_size; i++ )
  205. list.append( renamedFiles[i].error ? renamedFiles[i].src : renamedFiles[i].dst );
  206. TQWidget* krename = KRenameImpl::launch( TQRect( 0, 0, 0, 0 ), list );
  207. krename->show();
  208. close();
  209. }
  210. void ProgressDialog::openDest()
  211. {
  212. new KRun( m_dest );
  213. }
  214. TQString ProgressDialog::count()
  215. {
  216. TQString s;
  217. return s.sprintf( "%0*i", 7, ++m_count );
  218. }
  219. void ProgressDialog::setProgressTotalSteps( int t )
  220. {
  221. bar->setTotalSteps( t );
  222. }
  223. void ProgressDialog::setProgress( int p )
  224. {
  225. m_timer->start( TIMER_INTERVAL );
  226. bar->setProgress( p );
  227. }
  228. void ProgressDialog::canceled()
  229. {
  230. warning( i18n("User pressed cancel!") );
  231. warning( i18n("Aborting...") );
  232. m_canceled = true;
  233. }
  234. const TQString ProgressDialog::simplify( const TQString & text )
  235. {
  236. // make error messages fit in one line!
  237. TQString t( text );
  238. t.remove( '\n' );
  239. return t;
  240. }
  241. void ProgressDialog::undo()
  242. {
  243. KURL::List list;
  244. FileOperation fop;
  245. m_timer->start( TIMER_INTERVAL );
  246. TDEApplication::setOverrideCursor( TQt::waitCursor );
  247. for( unsigned int i = 0; i < m_size; i++ )
  248. {
  249. setProgress( i );
  250. list.append( renamedFiles[i].src );
  251. if( !renamedFiles[i].error )
  252. {
  253. if( renamedFiles[i].dir ) {
  254. /** handle renamed directories and their contents
  255. */
  256. TQString ddir = renamedFiles[i].dst.path();
  257. TQString sdir = renamedFiles[i].src.path();
  258. for( unsigned int c = i+1; c < m_size; c++ ) {
  259. TQString dpath = renamedFiles[c].dst.path();
  260. TQString spath = renamedFiles[c].src.path();
  261. if( spath.startsWith( ddir ) )
  262. {
  263. spath = sdir + spath.right( spath.length() - ddir.length() );
  264. renamedFiles[c].src.setPath( spath );
  265. }
  266. if( dpath.startsWith( ddir ) )
  267. {
  268. dpath = sdir + dpath.right( dpath.length() - ddir.length() );
  269. renamedFiles[c].dst.setPath( dpath );
  270. }
  271. }
  272. }
  273. if(!fop.start( renamedFiles[i].dst, renamedFiles[i].src, MOVE, false ))
  274. error( i18n("Undo: ") + fop.error() );
  275. }
  276. }
  277. print( i18n("Undoing the renaming operation has been completed."), "undo" );
  278. m_timer->stop();
  279. TDEApplication::restoreOverrideCursor();
  280. TQWidget* krename = KRenameImpl::launch( TQRect( 0, 0, 0, 0 ), list );
  281. krename->show();
  282. close();
  283. }
  284. void ProgressDialog::slotTimer()
  285. {
  286. kapp->processEvents( 0 );
  287. }
  288. #include "ProgressDialog.moc"