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 <kwin.h>
00037 
00038 #include "jobclasses.h"
00039 #include "defaultprogress.h"
00040 
00041 namespace KIO {
00042 
00043 class DefaultProgress::DefaultProgressPrivate
00044 {
00045 public:
00046   bool keepOpenChecked;
00047   bool noCaptionYet;
00048   KPushButton *cancelClose;
00049   KPushButton *openFile;
00050   KPushButton *openLocation;
00051   QCheckBox   *keepOpen;
00052   KURL        location;
00053 };
00054 
00055 DefaultProgress::DefaultProgress( bool showNow )
00056   : ProgressBase( 0 ),
00057   m_iTotalSize(0), m_iTotalFiles(0), m_iTotalDirs(0),
00058   m_iProcessedSize(0), m_iProcessedDirs(0), m_iProcessedFiles(0)
00059 {
00060   init();
00061 
00062   if ( showNow ) {
00063     show();
00064   }
00065 }
00066 
00067 DefaultProgress::DefaultProgress( QWidget* parent, const char* /*name*/ )
00068   : ProgressBase( parent ),
00069   m_iTotalSize(0), m_iTotalFiles(0), m_iTotalDirs(0),
00070   m_iProcessedSize(0), m_iProcessedDirs(0), m_iProcessedFiles(0)
00071 {
00072     init();
00073 }
00074 
00075 void DefaultProgress::init()
00076 {
00077   d = new DefaultProgressPrivate;
00078 
00079 #ifdef Q_WS_X11 //FIXME(E): Remove once all the KWin::foo calls have been ported to QWS
00080   // Set a useful icon for this window!
00081   KWin::setIcons( winId(),
00082           KGlobal::iconLoader()->loadIcon( "filesave", KIcon::NoGroup, 32 ),
00083           KGlobal::iconLoader()->loadIcon( "filesave", KIcon::NoGroup, 16 ) );
00084 #endif
00085 
00086   QVBoxLayout *topLayout = new QVBoxLayout( this, KDialog::marginHint(),
00087                                             KDialog::spacingHint() );
00088   topLayout->addStrut( 360 );   // makes dlg at least that wide
00089 
00090   QGridLayout *grid = new QGridLayout( 2, 3 );
00091   topLayout->addLayout(grid);
00092   grid->addColSpacing(1, KDialog::spacingHint());
00093   // filenames or action name
00094   grid->addWidget(new QLabel(i18n("Source:"), this), 0, 0);
00095 
00096   sourceLabel = new KSqueezedTextLabel(this);
00097   grid->addWidget(sourceLabel, 0, 2);
00098 
00099   destInvite = new QLabel(i18n("Destination:"), this);
00100   grid->addWidget(destInvite, 1, 0);
00101 
00102   destLabel = new KSqueezedTextLabel(this);
00103   grid->addWidget(destLabel, 1, 2);
00104 
00105   m_pProgressBar = new KProgress(this);
00106   topLayout->addWidget( m_pProgressBar );
00107 
00108   // processed info
00109   QHBoxLayout *hBox = new QHBoxLayout();
00110   topLayout->addLayout(hBox);
00111 
00112   sizeLabel = new QLabel(this);
00113   hBox->addWidget(sizeLabel);
00114 
00115   resumeLabel = new QLabel(this);
00116   hBox->addWidget(resumeLabel);
00117 
00118   progressLabel = new QLabel( this );
00119 /*  progressLabel->setSizePolicy( QSizePolicy( QSizePolicy::MinimumExpanding,
00120                                              QSizePolicy::Preferred ) );*/
00121   progressLabel->setAlignment( QLabel::AlignRight );
00122   hBox->addWidget( progressLabel );
00123 
00124   hBox = new QHBoxLayout();
00125   topLayout->addLayout(hBox);
00126 
00127   speedLabel = new QLabel(this);
00128   hBox->addWidget(speedLabel, 1);
00129 
00130   QFrame *line = new QFrame( this );
00131   line->setFrameShape( QFrame::HLine );
00132   line->setFrameShadow( QFrame::Sunken );
00133   topLayout->addWidget( line );
00134 
00135   d->keepOpen = new QCheckBox( i18n("&Keep this window open after download is complete"), this);
00136   connect( d->keepOpen, SIGNAL( toggled(bool) ), SLOT( slotKeepOpenToggled(bool) ) );
00137   topLayout->addWidget(d->keepOpen);
00138   d->keepOpen->hide();
00139 
00140   hBox = new QHBoxLayout();
00141   topLayout->addLayout(hBox);
00142 
00143   d->openFile = new KPushButton( i18n("Open &File"), this );
00144   connect( d->openFile, SIGNAL( clicked() ), SLOT( slotOpenFile() ) );
00145   hBox->addWidget( d->openFile );
00146   d->openFile->setEnabled(false);
00147   d->openFile->hide();
00148 
00149   d->openLocation = new KPushButton( i18n("Open &Destination"), this );
00150   connect( d->openLocation, SIGNAL( clicked() ), SLOT( slotOpenLocation() ) );
00151   hBox->addWidget( d->openLocation );
00152   d->openLocation->hide();
00153 
00154   hBox->addStretch(1);
00155 
00156   d->cancelClose = new KPushButton( KStdGuiItem::cancel(), this );
00157   connect( d->cancelClose, SIGNAL( clicked() ), SLOT( slotStop() ) );
00158   hBox->addWidget( d->cancelClose );
00159 
00160   resize( sizeHint() );
00161   setMaximumHeight(sizeHint().height());
00162 
00163   d->keepOpenChecked = false;
00164   d->noCaptionYet = true;
00165   setCaption(i18n("Progress Dialog")); // show something better than kio_uiserver
00166 }
00167 
00168 DefaultProgress::~DefaultProgress()
00169 {
00170   delete d;
00171 }
00172 
00173 void DefaultProgress::slotTotalSize( KIO::Job*, KIO::filesize_t bytes )
00174 {
00175   m_iTotalSize = bytes;
00176 }
00177 
00178 
00179 void DefaultProgress::slotTotalFiles( KIO::Job*, unsigned long files )
00180 {
00181   m_iTotalFiles = files;
00182   showTotals();
00183 }
00184 
00185 
00186 void DefaultProgress::slotTotalDirs( KIO::Job*, unsigned long dirs )
00187 {
00188   m_iTotalDirs = dirs;
00189   showTotals();
00190 }
00191 
00192 void DefaultProgress::showTotals()
00193 {
00194   // Show the totals in the progress label, if we still haven't
00195   // processed anything. This is useful when the stat'ing phase
00196   // of CopyJob takes a long time (e.g. over networks).
00197   if ( m_iProcessedFiles == 0 && m_iProcessedDirs == 0 )
00198   {
00199     QString tmps;
00200     if ( m_iTotalDirs > 1 )
00201       // that we have a singular to translate looks weired but is only logical
00202       tmps = i18n("%n directory", "%n directories", m_iTotalDirs) + "   ";
00203     tmps += i18n("%n file", "%n files", m_iTotalFiles);
00204     progressLabel->setText( tmps );
00205   }
00206 }
00207 
00208 void DefaultProgress::slotPercent( KIO::Job*, unsigned long percent )
00209 {
00210   QString tmp(i18n( "%1% of %2 ").arg( percent ).arg( KIO::convertSize(m_iTotalSize)));
00211   m_pProgressBar->setValue( percent );
00212   switch(mode) {
00213   case Copy:
00214     tmp.append(i18n(" (Copying)"));
00215     break;
00216   case Move:
00217     tmp.append(i18n(" (Moving)"));
00218     break;
00219   case Delete:
00220     tmp.append(i18n(" (Deleting)"));
00221     break;
00222   case Create:
00223     tmp.append(i18n(" (Creating)"));
00224     break;
00225   }
00226 
00227   setCaption( tmp );
00228   d->noCaptionYet = false;
00229 }
00230 
00231 
00232 void DefaultProgress::slotInfoMessage( KIO::Job*, const QString & msg )
00233 {
00234   speedLabel->setText( msg );
00235   speedLabel->setAlignment( speedLabel->alignment() & ~Qt::WordBreak );
00236 }
00237 
00238 
00239 void DefaultProgress::slotProcessedSize( KIO::Job*, KIO::filesize_t bytes ) {
00240   m_iProcessedSize = bytes;
00241 
00242   QString tmp;
00243   tmp = i18n( "%1 of %2 complete").arg( KIO::convertSize(bytes) ).arg( KIO::convertSize(m_iTotalSize));
00244   sizeLabel->setText( tmp );
00245 }
00246 
00247 
00248 void DefaultProgress::slotProcessedDirs( KIO::Job*, unsigned long dirs )
00249 {
00250   m_iProcessedDirs = dirs;
00251 
00252   QString tmps;
00253   tmps = i18n("%1 / %n directory", "%1 / %n directories", m_iTotalDirs).arg( m_iProcessedDirs );
00254   tmps += "   ";
00255   tmps += i18n("%1 / %n file", "%1 / %n files", m_iTotalFiles).arg( m_iProcessedFiles );
00256   progressLabel->setText( tmps );
00257 }
00258 
00259 
00260 void DefaultProgress::slotProcessedFiles( KIO::Job*, unsigned long files )
00261 {
00262   m_iProcessedFiles = files;
00263 
00264   QString tmps;
00265   if ( m_iTotalDirs > 1 ) {
00266     tmps = i18n("%1 / %n directory", "%1 / %n directories", m_iTotalDirs).arg( m_iProcessedDirs );
00267     tmps += "   ";
00268   }
00269   tmps += i18n("%1 / %n file", "%1 / %n files", m_iTotalFiles).arg( m_iProcessedFiles );
00270   progressLabel->setText( tmps );
00271 }
00272 
00273 
00274 void DefaultProgress::slotSpeed( KIO::Job*, unsigned long bytes_per_second )
00275 {
00276   if ( bytes_per_second == 0 ) {
00277     speedLabel->setText( i18n( "Stalled") );
00278   } else {
00279     QTime remaining = KIO::calculateRemaining( m_iTotalSize, m_iProcessedSize, bytes_per_second );
00280     speedLabel->setText( i18n( "%1/s ( %2 remaining )").arg( KIO::convertSize( bytes_per_second )).arg( remaining.toString() ) );
00281   }
00282 }
00283 
00284 
00285 void DefaultProgress::slotCopying( KIO::Job*, const KURL& from, const KURL& to )
00286 {
00287   if ( d->noCaptionYet ) {
00288     setCaption(i18n("Copy File(s) Progress"));
00289     d->noCaptionYet = false;
00290   }
00291   mode = Copy;
00292   sourceLabel->setText(from.prettyURL());
00293   setDestVisible( true );
00294   checkDestination( to );
00295   destLabel->setText(to.prettyURL());
00296 }
00297 
00298 
00299 void DefaultProgress::slotMoving( KIO::Job*, const KURL& from, const KURL& to )
00300 {
00301   if ( d->noCaptionYet ) {
00302     setCaption(i18n("Move File(s) Progress"));
00303     d->noCaptionYet = false;
00304   }
00305   mode = Move;
00306   sourceLabel->setText(from.prettyURL());
00307   setDestVisible( true );
00308   checkDestination( to );
00309   destLabel->setText(to.prettyURL());
00310 }
00311 
00312 
00313 void DefaultProgress::slotCreatingDir( KIO::Job*, const KURL& dir )
00314 {
00315   if ( d->noCaptionYet ) {
00316     setCaption(i18n("Creating Directory"));
00317     d->noCaptionYet = false;
00318   }
00319   mode = Create;
00320   sourceLabel->setText(dir.prettyURL());
00321   setDestVisible( false );
00322 }
00323 
00324 
00325 void DefaultProgress::slotDeleting( KIO::Job*, const KURL& url )
00326 {
00327   if ( d->noCaptionYet ) {
00328     setCaption(i18n("Delete File(s) Progress"));
00329     d->noCaptionYet = false;
00330   }
00331   mode = Delete;
00332   sourceLabel->setText(url.prettyURL());
00333   setDestVisible( false );
00334 }
00335 
00336 void DefaultProgress::slotTransferring( KIO::Job*, const KURL& url )
00337 {
00338   if ( d->noCaptionYet ) {
00339     setCaption(i18n("Loading Progress"));
00340     d->noCaptionYet = false;
00341   }
00342   sourceLabel->setText(url.prettyURL());
00343   setDestVisible( false );
00344 }
00345 
00346 void DefaultProgress::slotStating( KIO::Job*, const KURL& url )
00347 {
00348   setCaption(i18n("Examining File Progress"));
00349   sourceLabel->setText(url.prettyURL());
00350   setDestVisible( false );
00351 }
00352 
00353 void DefaultProgress::slotMounting( KIO::Job*, const QString & dev, const QString & point )
00354 {
00355   setCaption(i18n("Mounting %1").arg(dev));
00356   sourceLabel->setText(point);
00357   setDestVisible( false );
00358 }
00359 
00360 void DefaultProgress::slotUnmounting( KIO::Job*, const QString & point )
00361 {
00362   setCaption(i18n("Unmounting"));
00363   sourceLabel->setText(point);
00364   setDestVisible( false );
00365 }
00366 
00367 void DefaultProgress::slotCanResume( KIO::Job*, KIO::filesize_t resume )
00368 {
00369   if ( resume ) {
00370     resumeLabel->setText( i18n("Resuming from %1").arg(KIO::number(resume)) );
00371   } else {
00372     resumeLabel->setText( i18n("Not resumable") );
00373   }
00374 }
00375 
00376 void DefaultProgress::setDestVisible( bool visible )
00377 {
00378   // We can't hide the destInvite/destLabel labels,
00379   // because it screws up the QGridLayout.
00380   if (visible)
00381   {
00382     destInvite->setText( i18n("Destination:") );
00383   }
00384   else
00385   {
00386     destInvite->setText( QString::null );
00387     destLabel->setText( QString::null );
00388   }
00389 }
00390 
00391 void DefaultProgress::slotClean() {
00392   if (d->keepOpenChecked) {
00393     slotPercent(0, 100);
00394     d->cancelClose->setText( KStdGuiItem::close().text() );
00395     d->openFile->setEnabled(true);
00396     setOnlyClean(false);
00397   }
00398   else
00399     hide();
00400 }
00401 
00402 void DefaultProgress::slotKeepOpenToggled(bool keepopen)
00403 {
00404   d->keepOpenChecked=keepopen;
00405 }
00406 
00407 void DefaultProgress::checkDestination(const KURL& dest) {
00408   bool ok = true;
00409   if ( dest.isLocalFile() ) {
00410       QString path = dest.path( -1 );
00411       QStringList tmpDirs = KGlobal::dirs()->resourceDirs( "tmp" );
00412       for ( QStringList::Iterator it = tmpDirs.begin() ; ok && it != tmpDirs.end() ; ++it )
00413           if ( path.contains( *it ) )
00414               ok = false; // it's in the tmp resource
00415   }
00416 
00417   if ( ok ) {
00418     d->openFile->show();
00419     d->openLocation->show();
00420     d->keepOpen->show();
00421     d->location=dest;
00422   }
00423 }
00424 
00425 void DefaultProgress::slotOpenFile()
00426 {
00427   KProcess proc;
00428   proc << "konqueror" << d->location.prettyURL();
00429   proc.start(KProcess::DontCare);
00430 }
00431 
00432 void DefaultProgress::slotOpenLocation()
00433 {
00434   KProcess proc;
00435   d->location.setFileName("");
00436   proc << "konqueror" << d->location.prettyURL();
00437   proc.start(KProcess::DontCare);
00438 }
00439 
00440 void DefaultProgress::virtual_hook( int id, void* data )
00441 { ProgressBase::virtual_hook( id, data ); }
00442 
00443 } /* namespace */
00444 
00445 #include "defaultprogress.moc"
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.5.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Jan 28 13:12:44 2004 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001