kio Library API Documentation

defaultprogress.cpp

00001 /* This file is part of the KDE libraries
00002    Copyright (C) 2000 Matej Koss <koss@miesto.sk>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License version 2 as published by the Free Software Foundation.
00007 
00008    This library is distributed in the hope that it will be useful,
00009    but WITHOUT ANY WARRANTY; without even the implied warranty of
00010    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011    Library General Public License for more details.
00012 
00013    You should have received a copy of the GNU Library General Public License
00014    along with this library; see the file COPYING.LIB.  If not, write to
00015    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00016    Boston, MA 02111-1307, USA.
00017 */
00018 
00019 #include <qtimer.h>
00020 #include <qlayout.h>
00021 #include <qtooltip.h>
00022 #include <qdatetime.h>
00023 #include <qcheckbox.h>
00024 
00025 #include <kapplication.h>
00026 #include <kdebug.h>
00027 #include <kdialog.h>
00028 #include <kstringhandler.h>
00029 #include <kglobal.h>
00030 #include <klocale.h>
00031 #include <kiconloader.h>
00032 #include <kprocess.h>
00033 #include <kpushbutton.h>
00034 #include <kstandarddirs.h>
00035 #include <kstdguiitem.h>
00036 #include <klineedit.h>
00037 #include <kwin.h>
00038 
00039 #include "jobclasses.h"
00040 #include "defaultprogress.h"
00041 
00042 namespace KIO {
00043 
00044 class DefaultProgress::DefaultProgressPrivate
00045 {
00046 public:
00047   bool keepOpenChecked;
00048   bool noCaptionYet;
00049   KPushButton *cancelClose;
00050   KPushButton *openFile;
00051   KPushButton *openLocation;
00052   QCheckBox   *keepOpen;
00053   KURL        location;
00054   QTime       startTime;
00055 };
00056 
00057 DefaultProgress::DefaultProgress( bool showNow )
00058   : ProgressBase( 0 ),
00059   m_iTotalSize(0), m_iTotalFiles(0), m_iTotalDirs(0),
00060   m_iProcessedSize(0), m_iProcessedDirs(0), m_iProcessedFiles(0)
00061 {
00062   init();
00063 
00064   if ( showNow ) {
00065     show();
00066   }
00067 }
00068 
00069 DefaultProgress::DefaultProgress( QWidget* parent, const char* /*name*/ )
00070   : ProgressBase( parent ),
00071   m_iTotalSize(0), m_iTotalFiles(0), m_iTotalDirs(0),
00072   m_iProcessedSize(0), m_iProcessedDirs(0), m_iProcessedFiles(0)
00073 {
00074     init();
00075 }
00076 
00077 bool DefaultProgress::keepOpen() const
00078 {
00079     return d->keepOpenChecked;
00080 }
00081 
00082 void DefaultProgress::init()
00083 {
00084   d = new DefaultProgressPrivate;
00085 
00086 #ifdef Q_WS_X11 //FIXME(E): Remove once all the KWin::foo calls have been ported to QWS
00087   // Set a useful icon for this window!
00088   KWin::setIcons( winId(),
00089           KGlobal::iconLoader()->loadIcon( "filesave", KIcon::NoGroup, 32 ),
00090           KGlobal::iconLoader()->loadIcon( "filesave", KIcon::NoGroup, 16 ) );
00091 #endif
00092 
00093   QVBoxLayout *topLayout = new QVBoxLayout( this, KDialog::marginHint(),
00094                                             KDialog::spacingHint() );
00095   topLayout->addStrut( 360 );   // makes dlg at least that wide
00096 
00097   QGridLayout *grid = new QGridLayout( 2, 3 );
00098   topLayout->addLayout(grid);
00099   grid->addColSpacing(1, KDialog::spacingHint());
00100   // filenames or action name
00101   grid->addWidget(new QLabel(i18n("Source:"), this), 0, 0);
00102 
00103   sourceEdit = new KLineEdit(this);
00104   sourceEdit->setReadOnly (true);
00105   grid->addWidget(sourceEdit, 0, 2);
00106 
00107   destInvite = new QLabel(i18n("Destination:"), this);
00108   grid->addWidget(destInvite, 1, 0);
00109 
00110   destEdit = new KLineEdit(this);
00111   destEdit->setReadOnly (true);
00112   grid->addWidget(destEdit, 1, 2);
00113 
00114   m_pProgressBar = new KProgress(this);
00115   topLayout->addWidget( m_pProgressBar );
00116 
00117   // processed info
00118   QHBoxLayout *hBox = new QHBoxLayout();
00119   topLayout->addLayout(hBox);
00120 
00121   sizeLabel = new QLabel(this);
00122   hBox->addWidget(sizeLabel);
00123 
00124   resumeLabel = new QLabel(this);
00125   hBox->addWidget(resumeLabel);
00126 
00127   progressLabel = new QLabel( this );
00128 /*  progressLabel->setSizePolicy( QSizePolicy( QSizePolicy::MinimumExpanding,
00129                                              QSizePolicy::Preferred ) );*/
00130   progressLabel->setAlignment( QLabel::AlignRight );
00131   hBox->addWidget( progressLabel );
00132 
00133   hBox = new QHBoxLayout();
00134   topLayout->addLayout(hBox);
00135 
00136   speedLabel = new QLabel(this);
00137   hBox->addWidget(speedLabel, 1);
00138 
00139   QFrame *line = new QFrame( this );
00140   line->setFrameShape( QFrame::HLine );
00141   line->setFrameShadow( QFrame::Sunken );
00142   topLayout->addWidget( line );
00143 
00144   d->keepOpen = new QCheckBox( i18n("&Keep this window open after transfer is complete"), this);
00145   connect( d->keepOpen, SIGNAL( toggled(bool) ), SLOT( slotKeepOpenToggled(bool) ) );
00146   topLayout->addWidget(d->keepOpen);
00147   d->keepOpen->hide();
00148 
00149   hBox = new QHBoxLayout();
00150   topLayout->addLayout(hBox);
00151 
00152   d->openFile = new KPushButton( i18n("Open &File"), this );
00153   connect( d->openFile, SIGNAL( clicked() ), SLOT( slotOpenFile() ) );
00154   hBox->addWidget( d->openFile );
00155   d->openFile->setEnabled(false);
00156   d->openFile->hide();
00157 
00158   d->openLocation = new KPushButton( i18n("Open &Destination"), this );
00159   connect( d->openLocation, SIGNAL( clicked() ), SLOT( slotOpenLocation() ) );
00160   hBox->addWidget( d->openLocation );
00161   d->openLocation->hide();
00162 
00163   hBox->addStretch(1);
00164 
00165   d->cancelClose = new KPushButton( KStdGuiItem::cancel(), this );
00166   connect( d->cancelClose, SIGNAL( clicked() ), SLOT( slotStop() ) );
00167   hBox->addWidget( d->cancelClose );
00168 
00169   resize( sizeHint() );
00170   setMaximumHeight(sizeHint().height());
00171 
00172   d->keepOpenChecked = false;
00173   d->noCaptionYet = true;
00174   setCaption(i18n("Progress Dialog")); // show something better than kio_uiserver
00175 }
00176 
00177 DefaultProgress::~DefaultProgress()
00178 {
00179   delete d;
00180 }
00181 
00182 void DefaultProgress::slotTotalSize( KIO::Job*, KIO::filesize_t bytes )
00183 {
00184   m_iTotalSize = bytes;
00185   if (d->startTime.isNull())
00186     d->startTime.start();
00187 }
00188 
00189 
00190 void DefaultProgress::slotTotalFiles( KIO::Job*, unsigned long files )
00191 {
00192   m_iTotalFiles = files;
00193   showTotals();
00194 }
00195 
00196 
00197 void DefaultProgress::slotTotalDirs( KIO::Job*, unsigned long dirs )
00198 {
00199   m_iTotalDirs = dirs;
00200   showTotals();
00201 }
00202 
00203 void DefaultProgress::showTotals()
00204 {
00205   // Show the totals in the progress label, if we still haven't
00206   // processed anything. This is useful when the stat'ing phase
00207   // of CopyJob takes a long time (e.g. over networks).
00208   if ( m_iProcessedFiles == 0 && m_iProcessedDirs == 0 )
00209   {
00210     QString tmps;
00211     if ( m_iTotalDirs > 1 )
00212       // that we have a singular to translate looks weired but is only logical
00213       tmps = i18n("%n folder", "%n folders", m_iTotalDirs) + "   ";
00214     tmps += i18n("%n file", "%n files", m_iTotalFiles);
00215     progressLabel->setText( tmps );
00216   }
00217 }
00218 
00219 void DefaultProgress::slotPercent( KIO::Job*, unsigned long percent )
00220 {
00221   QString tmp(i18n( "%1% of %2 ").arg( percent ).arg( KIO::convertSize(m_iTotalSize)));
00222   m_pProgressBar->setValue( percent );
00223   switch(mode) {
00224   case Copy:
00225     tmp.append(i18n(" (Copying)"));
00226     break;
00227   case Move:
00228     tmp.append(i18n(" (Moving)"));
00229     break;
00230   case Delete:
00231     tmp.append(i18n(" (Deleting)"));
00232     break;
00233   case Create:
00234     tmp.append(i18n(" (Creating)"));
00235     break;
00236   }
00237 
00238   setCaption( tmp );
00239   d->noCaptionYet = false;
00240 }
00241 
00242 
00243 void DefaultProgress::slotInfoMessage( KIO::Job*, const QString & msg )
00244 {
00245   speedLabel->setText( msg );
00246   speedLabel->setAlignment( speedLabel->alignment() & ~Qt::WordBreak );
00247 }
00248 
00249 
00250 void DefaultProgress::slotProcessedSize( KIO::Job*, KIO::filesize_t bytes ) {
00251   m_iProcessedSize = bytes;
00252 
00253   QString tmp;
00254   tmp = i18n( "%1 of %2 complete").arg( KIO::convertSize(bytes) ).arg( KIO::convertSize(m_iTotalSize));
00255   sizeLabel->setText( tmp );
00256 }
00257 
00258 
00259 void DefaultProgress::slotProcessedDirs( KIO::Job*, unsigned long dirs )
00260 {
00261   m_iProcessedDirs = dirs;
00262 
00263   QString tmps;
00264   tmps = i18n("%1 / %n folder", "%1 / %n folders", m_iTotalDirs).arg( m_iProcessedDirs );
00265   tmps += "   ";
00266   tmps += i18n("%1 / %n file", "%1 / %n files", m_iTotalFiles).arg( m_iProcessedFiles );
00267   progressLabel->setText( tmps );
00268 }
00269 
00270 
00271 void DefaultProgress::slotProcessedFiles( KIO::Job*, unsigned long files )
00272 {
00273   m_iProcessedFiles = files;
00274 
00275   QString tmps;
00276   if ( m_iTotalDirs > 1 ) {
00277     tmps = i18n("%1 / %n folder", "%1 / %n folders", m_iTotalDirs).arg( m_iProcessedDirs );
00278     tmps += "   ";
00279   }
00280   tmps += i18n("%1 / %n file", "%1 / %n files", m_iTotalFiles).arg( m_iProcessedFiles );
00281   progressLabel->setText( tmps );
00282 }
00283 
00284 
00285 void DefaultProgress::slotSpeed( KIO::Job*, unsigned long bytes_per_second )
00286 {
00287   if ( bytes_per_second == 0 ) {
00288     speedLabel->setText( i18n( "Stalled") );
00289   } else {
00290     QTime remaining = KIO::calculateRemaining( m_iTotalSize, m_iProcessedSize, bytes_per_second );
00291     speedLabel->setText( i18n( "%1/s ( %2 remaining )").arg( KIO::convertSize( bytes_per_second )).arg( remaining.toString() ) );
00292   }
00293 }
00294 
00295 
00296 void DefaultProgress::slotCopying( KIO::Job*, const KURL& from, const KURL& to )
00297 {
00298     if ( d->noCaptionYet ) {
00299     setCaption(i18n("Copy File(s) Progress"));
00300     d->noCaptionYet = false;
00301   }
00302   mode = Copy;
00303   sourceEdit->setSqueezedText(from.prettyURL());
00304   sourceEdit->home (false);
00305   setDestVisible( true );
00306   checkDestination( to );
00307   destEdit->setSqueezedText(to.prettyURL());
00308   destEdit->home (false);
00309 }
00310 
00311 
00312 void DefaultProgress::slotMoving( KIO::Job*, const KURL& from, const KURL& to )
00313 {
00314   if ( d->noCaptionYet ) {
00315     setCaption(i18n("Move File(s) Progress"));
00316     d->noCaptionYet = false;
00317   }
00318   mode = Move;
00319   sourceEdit->setSqueezedText(from.prettyURL());
00320   sourceEdit->home (false);
00321   setDestVisible( true );
00322   checkDestination( to );
00323   destEdit->setSqueezedText(to.prettyURL());
00324   destEdit->home (false);
00325 }
00326 
00327 
00328 void DefaultProgress::slotCreatingDir( KIO::Job*, const KURL& dir )
00329 {
00330   if ( d->noCaptionYet ) {
00331     setCaption(i18n("Creating Folder"));
00332     d->noCaptionYet = false;
00333   }
00334   mode = Create;
00335   sourceEdit->setSqueezedText(dir.prettyURL());
00336   sourceEdit->home (false);
00337   setDestVisible( false );
00338 }
00339 
00340 
00341 void DefaultProgress::slotDeleting( KIO::Job*, const KURL& url )
00342 {
00343   if ( d->noCaptionYet ) {
00344     setCaption(i18n("Delete File(s) Progress"));
00345     d->noCaptionYet = false;
00346   }
00347   mode = Delete;
00348   sourceEdit->setSqueezedText(url.prettyURL());
00349   sourceEdit->home (false);
00350   setDestVisible( false );
00351 }
00352 
00353 void DefaultProgress::slotTransferring( KIO::Job*, const KURL& url )
00354 {
00355   if ( d->noCaptionYet ) {
00356     setCaption(i18n("Loading Progress"));
00357     d->noCaptionYet = false;
00358   }
00359   sourceEdit->setSqueezedText(url.prettyURL());
00360   sourceEdit->home (false);
00361   setDestVisible( false );
00362 }
00363 
00364 void DefaultProgress::slotStating( KIO::Job*, const KURL& url )
00365 {
00366   setCaption(i18n("Examining File Progress"));
00367   sourceEdit->setSqueezedText(url.prettyURL());
00368   sourceEdit->home (false);
00369   setDestVisible( false );
00370 }
00371 
00372 void DefaultProgress::slotMounting( KIO::Job*, const QString & dev, const QString & point )
00373 {
00374   setCaption(i18n("Mounting %1").arg(dev));
00375   sourceEdit->setSqueezedText(point);
00376   sourceEdit->home (false);
00377   setDestVisible( false );
00378 }
00379 
00380 void DefaultProgress::slotUnmounting( KIO::Job*, const QString & point )
00381 {
00382   setCaption(i18n("Unmounting"));
00383   sourceEdit->setSqueezedText(point);
00384   sourceEdit->home (false);
00385   setDestVisible( false );
00386 }
00387 
00388 void DefaultProgress::slotCanResume( KIO::Job*, KIO::filesize_t resume )
00389 {
00390   if ( resume ) {
00391     resumeLabel->setText( i18n("Resuming from %1").arg(KIO::number(resume)) );
00392   } else {
00393     resumeLabel->setText( i18n("Not resumable") );
00394   }
00395 }
00396 
00397 void DefaultProgress::setDestVisible( bool visible )
00398 {
00399   // We can't hide the destInvite/destEdit labels,
00400   // because it screws up the QGridLayout.
00401   if (visible)
00402   {
00403       destInvite->show();
00404       destEdit->show();
00405 
00406     destInvite->setText( i18n("Destination:") );
00407   }
00408   else
00409   {
00410       destInvite->hide();
00411       destEdit->hide();
00412     destInvite->setText( QString::null );
00413     destEdit->setText( QString::null );
00414   }
00415 }
00416 
00417 void DefaultProgress::slotClean() {
00418   if (d->keepOpenChecked) {
00419     slotPercent(0, 100);
00420     d->cancelClose->setText( KStdGuiItem::close().text() );
00421     d->openFile->setEnabled(true);
00422     slotProcessedSize(0, m_iTotalSize);
00423     d->keepOpen->setEnabled(false);
00424     if (!d->startTime.isNull()) {
00425       int s = d->startTime.elapsed();
00426       if (!s)
00427     s = 1;
00428       speedLabel->setText(i18n("%1/s (done)").arg(KIO::convertSize(1000 * m_iTotalSize / s)));
00429     }
00430     setOnlyClean(false);
00431   }
00432   else
00433     hide();
00434 }
00435 
00436 void DefaultProgress::slotKeepOpenToggled(bool keepopen)
00437 {
00438   d->keepOpenChecked=keepopen;
00439 }
00440 
00441 void DefaultProgress::checkDestination(const KURL& dest) {
00442   bool ok = true;
00443   if ( dest.isLocalFile() ) {
00444       QString path = dest.path( -1 );
00445       QStringList tmpDirs = KGlobal::dirs()->resourceDirs( "tmp" );
00446       for ( QStringList::Iterator it = tmpDirs.begin() ; ok && it != tmpDirs.end() ; ++it )
00447           if ( path.contains( *it ) )
00448               ok = false; // it's in the tmp resource
00449   }
00450 
00451   if ( ok ) {
00452     d->openFile->show();
00453     d->openLocation->show();
00454     d->keepOpen->show();
00455     d->location=dest;
00456   }
00457 }
00458 
00459 void DefaultProgress::slotOpenFile()
00460 {
00461   KProcess proc;
00462   proc << "konqueror" << d->location.prettyURL();
00463   proc.start(KProcess::DontCare);
00464 }
00465 
00466 void DefaultProgress::slotOpenLocation()
00467 {
00468   KProcess proc;
00469   d->location.setFileName("");
00470   proc << "konqueror" << d->location.prettyURL();
00471   proc.start(KProcess::DontCare);
00472 }
00473 
00474 void DefaultProgress::virtual_hook( int id, void* data )
00475 { ProgressBase::virtual_hook( id, data ); }
00476 
00477 } /* namespace */
00478 
00479 #include "defaultprogress.moc"
KDE Logo
This file is part of the documentation for kio Library Version 3.2.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed May 12 09:06:11 2004 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2003