# kio Library API Documentation

Overview | Class Hierarchy | Classes | Classes (annotated) | Members | Source Files

# renamedlg.cpp

    00001 /* This file is part of the KDE libraries
    00002     Copyright (C) 2000 Stephan Kulow <coolo@kde.org>
    00003                        David Faure <faure@kde.org>
    00004                   2001 Holger Freyther <freyther@kde.org>
    00005
    00006     This library is free software; you can redistribute it and/or
    00007     modify it under the terms of the GNU Library General Public
    00008     License as published by the Free Software Foundation; either
    00009     version 2 of the License, or (at your option) any later version.
    00010
    00011     This library is distributed in the hope that it will be useful,
    00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
    00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00014     Library General Public License for more details.
    00015
    00016     You should have received a copy of the GNU Library General Public License
    00017     along with this library; see the file COPYING.LIB.  If not, write to
    00018     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    00019     Boston, MA 02111-1307, USA.
    00020 */
    00021
    00022 #include "kio/renamedlg.h"
    00023 #include "kio/renamedlgplugin.h"
    00024 #include <stdio.h>
    00025 #include <assert.h>
    00026
    00027 #include <qfileinfo.h>
    00028 #include <qlabel.h>
    00029 #include <qlayout.h>
    00030 #include <qlineedit.h>
    00031
    00032 #include <kmessagebox.h>
    00033 #include <kpushbutton.h>
    00034 #include <kapplication.h>
    00035 #include <kio/global.h>
    00036 #include <ktrader.h>
    00037 #include <klibloader.h>
    00038 #include <kdialog.h>
    00039 #include <klocale.h>
    00040 #include <kglobal.h>
    00041 #include <kdebug.h>
    00042 #include <kurl.h>
    00043 #include <kmimetype.h>
    00044 #include <kwin.h>
    00045 #include <kstringhandler.h>
    00046 #include <kstdguiitem.h>
    00047 #include <kguiitem.h>
    00048
    00049 using namespace KIO;
    00050
    00051 class RenameDlg::RenameDlgPrivate
    00052 {
    00053  public:
    00054   RenameDlgPrivate(){
    00055     b0 = 0L;
    00056     b1 = b2 = b3 = b4 = b5 = b6 = b7 = b8 = 0L;
    00057     m_pLineEdit=0L;
    00058     m_pLayout=0L;
    00059   }
    00060   KPushButton *b0;
    00061   QPushButton *b1;
    00062   QPushButton *b2;
    00063   QPushButton *b3;
    00064   QPushButton *b4;
    00065   QPushButton *b5;
    00066   QPushButton *b6;
    00067   QPushButton *b7;
    00068   QPushButton *b8; //why isn't it an array
    00069   QLineEdit* m_pLineEdit;
    00070   QVBoxLayout* m_pLayout; // ### doesn't need to be here
    00071   QString src;
    00072   QString dest;
    00073   QString mimeSrc;
    00074   QString mimeDest;
    00075   bool modal;
    00076   bool plugin;
    00077 };
    00078
    00079 RenameDlg::RenameDlg(QWidget *parent, const QString & _caption,
    00080                      const QString &_src, const QString &_dest,
    00081                      RenameDlg_Mode _mode,
    00082                      KIO::filesize_t sizeSrc,
    00083                      KIO::filesize_t sizeDest,
    00084                      time_t ctimeSrc,
    00085                      time_t ctimeDest,
    00086                      time_t mtimeSrc,
    00087                      time_t mtimeDest,
    00088                      bool _modal)
    00089   : QDialog ( parent, "KIO::RenameDialog" , _modal )
    00090 {
    00091     d = new RenameDlgPrivate( );
    00092     d->modal = _modal;
    00093     // Set "StaysOnTop", because this dialog is typically used in kio_uiserver,
    00094     // i.e. in a separate process.
    00095     // ####### This isn't the case anymore - remove?
    00096 #ifndef Q_WS_QWS //FIXME(E): Implement for QT Embedded
    00097     if (d->modal)
    00098         KWin::setState( winId(), NET::StaysOnTop );
    00099 #endif
    00100
    00101     d->src = _src;
    00102     d->dest = _dest;
    00103     d->plugin = false;
    00104
    00105
    00106     setCaption( _caption );
    00107
    00108     d->b0 = new KPushButton( KStdGuiItem::cancel(), this );
    00109     connect(d->b0, SIGNAL(clicked()), this, SLOT(b0Pressed()));
    00110
    00111     if ( ! (_mode & M_NORENAME ) ) {
    00112         d->b1 = new QPushButton( i18n( "&Rename" ), this );
    00113         d->b1->setEnabled(false);
    00114         d->b8 = new QPushButton( i18n( "&Propose" ), this );
    00115         connect(d->b8, SIGNAL(clicked()), this, SLOT(b8Pressed()));
    00116         connect(d->b1, SIGNAL(clicked()), this, SLOT(b1Pressed()));
    00117     }
    00118
    00119     if ( ( _mode & M_MULTI ) && ( _mode & M_SKIP ) ) {
    00120         d->b2 = new QPushButton( i18n( "&Skip" ), this );
    00121         connect(d->b2, SIGNAL(clicked()), this, SLOT(b2Pressed()));
    00122
    00123         d->b3 = new QPushButton( i18n( "&Auto Skip" ), this );
    00124         connect(d->b3, SIGNAL(clicked()), this, SLOT(b3Pressed()));
    00125     }
    00126
    00127     if ( _mode & M_OVERWRITE ) {
    00128         d->b4 = new QPushButton( i18n( "&Overwrite" ), this );
    00129         connect(d->b4, SIGNAL(clicked()), this, SLOT(b4Pressed()));
    00130
    00131         if ( _mode & M_MULTI ) {
    00132             d->b5 = new QPushButton( i18n( "O&verwrite All" ), this );
    00133             connect(d->b5, SIGNAL(clicked()), this, SLOT(b5Pressed()));
    00134         }
    00135     }
    00136
    00137     if ( _mode & M_RESUME ) {
    00138         d->b6 = new QPushButton( i18n( "&Resume" ), this );
    00139         connect(d->b6, SIGNAL(clicked()), this, SLOT(b6Pressed()));
    00140
    00141         if ( _mode & M_MULTI )
    00142         {
    00143             d->b7 = new QPushButton( i18n( "R&esume All" ), this );
    00144             connect(d->b7, SIGNAL(clicked()), this, SLOT(b7Pressed()));
    00145         }
    00146     }
    00147
    00148     d->m_pLayout = new QVBoxLayout( this, KDialog::marginHint(),
    00149                                     KDialog::spacingHint() );
    00150     d->m_pLayout->addStrut( 360 );      // makes dlg at least that wide
    00151
    00152     // User tries to overwrite a file with itself ?
    00153     if ( _mode & M_OVERWRITE_ITSELF ) {
    00154         QLabel *lb = new QLabel( i18n( "This action would overwrite '%1' with itself.\n"
    00155                                        "Do you want to rename it instead?" ).arg( KStringHandler::csqueeze( d->src,100 ) ), this );
    00156         d->m_pLayout->addWidget( lb );
    00157     }
    00158     else if ( _mode & M_OVERWRITE ) {
    00159
    00160         // Figure out the mimetype and load one plugin
    00161         // (This is the only mode that is handled by plugins)
    00162         pluginHandling();
    00163         KTrader::OfferList plugin_offers;
    00164         if( d->mimeSrc != KMimeType::defaultMimeType()   ){
    00165             plugin_offers = KTrader::self()->query(d->mimeSrc, "'RenameDlg/Plugin' in ServiceTypes");
    00166
    00167         }else if(d->mimeDest != KMimeType::defaultMimeType() ) {
    00168             plugin_offers = KTrader::self()->query(d->mimeDest, "'RenameDlg/Plugin' in ServiceTypes");
    00169         }
    00170         if(!plugin_offers.isEmpty() ){
    00171             kdDebug(7024) << "Offers" << endl;
    00172             KTrader::OfferList::ConstIterator it = plugin_offers.begin();
    00173             KTrader::OfferList::ConstIterator end = plugin_offers.end();
    00174             for( ; it != end; ++it ){
    00175                 QString libName = (*it)->library();
    00176                 if( libName.isEmpty() ){
    00177                     kdDebug(7024) << "lib is empty" << endl;
    00178                     continue;
    00179                 }
    00180                 KLibrary *lib = KLibLoader::self()->library(libName.local8Bit() );
    00181                 if(!lib) {
    00182                     continue;
    00183                 }
    00184                 KLibFactory *factory = lib->factory();
    00185                 if(!factory){
    00186                     lib->unload();
    00187                     continue;
    00188                 }
    00189                 QObject *obj = factory->create( this, (*it)->name().latin1() );
    00190                 if(!obj) {
    00191                     lib->unload();
    00192                     continue;
    00193                 }
    00194                 RenameDlgPlugin *plugin = static_cast<RenameDlgPlugin *>(obj);
    00195                 if(!plugin ){
    00196                     delete obj;
    00197                     continue;
    00198                 }
    00199                 if( plugin->initialize( _mode, _src, _dest, d->mimeSrc,
    00200                                         d->mimeDest, sizeSrc, sizeDest,
    00201                                         ctimeSrc, ctimeDest,
    00202                                         mtimeSrc, mtimeDest ) ) {
    00203                     d->plugin = true;
    00204                     d->m_pLayout->addWidget(plugin );
    00205                     kdDebug(7024) << "RenameDlgPlugin" << endl;
    00206                     break;
    00207                 } else {
    00208                     delete obj;
    00209                 }
    00210             }
    00211
    00212         }
    00213
    00214         if( !d->plugin ){
    00215             // No plugin found, build default dialog
    00216             QGridLayout * gridLayout = new QGridLayout( 0L, 9, 2, KDialog::marginHint(),
    00217                                                         KDialog::spacingHint() );
    00218             d->m_pLayout->addLayout(gridLayout);
    00219             gridLayout->setColStretch(0,0);
    00220             gridLayout->setColStretch(1,10);
    00221
    00222             QString sentence1;
    00223             if (mtimeDest < mtimeSrc)
    00224                 sentence1 = i18n("An older item named '%1' already exists.");
    00225             else if (mtimeDest == mtimeSrc)
    00226                 sentence1 = i18n("A similar file named '%1' already exists.");
    00227             else
    00228                 sentence1 = i18n("A newer item named '%1' already exists.");
    00229             QLabel * lb1 = new QLabel( sentence1.arg(KStringHandler::csqueeze(d->dest,100)), this );
    00230             gridLayout->addMultiCellWidget( lb1, 0, 0, 0, 1 ); // takes the complete first line
    00231
    00232             lb1 = new QLabel( this );
    00233             lb1->setPixmap( KMimeType::pixmapForURL( d->dest ) );
    00234             gridLayout->addMultiCellWidget( lb1, 1, 3, 0, 0 ); // takes the first column on rows 1-3
    00235
    00236             int row = 1;
    00237             if ( sizeDest != (KIO::filesize_t)-1 )
    00238             {
    00239                 QLabel * lb = new QLabel( i18n("size %1").arg( KIO::convertSize(sizeDest) ), this );
    00240                 gridLayout->addWidget( lb, row, 1 );
    00241                 row++;
    00242
    00243             }
    00244             if ( ctimeDest != (time_t)-1 )
    00245             {
    00246                 QDateTime dctime; dctime.setTime_t( ctimeDest );
    00247                 QLabel * lb = new QLabel( i18n("created on %1").arg( KGlobal::locale()->formatDateTime(dctime) ), this );
    00248                 gridLayout->addWidget( lb, row, 1 );
    00249                 row++;
    00250             }
    00251             if ( mtimeDest != (time_t)-1 )
    00252             {
    00253                 QDateTime dmtime; dmtime.setTime_t( mtimeDest );
    00254                 QLabel * lb = new QLabel( i18n("modified on %1").arg( KGlobal::locale()->formatDateTime(dmtime) ), this );
    00255                 gridLayout->addWidget( lb, row, 1 );
    00256                 row++;
    00257             }
    00258
    00259             if ( !d->src.isEmpty() )
    00260             {
    00261                 // rows 1 to 3 are the details (size/ctime/mtime), row 4 is empty
    00262                 gridLayout->addRowSpacing( 4, 20 );
    00263
    00264                 QLabel * lb2 = new QLabel( i18n("The source file is '%1'").arg(d->src), this );
    00265                 gridLayout->addMultiCellWidget( lb2, 5, 5, 0, 1 ); // takes the complete first line
    00266
    00267                 lb2 = new QLabel( this );
    00268                 lb2->setPixmap( KMimeType::pixmapForURL( d->src ) );
    00269                 gridLayout->addMultiCellWidget( lb2, 6, 8, 0, 0 ); // takes the first column on rows 6-8
    00270
    00271                 row = 6;
    00272
    00273                 if ( sizeSrc != (KIO::filesize_t)-1 )
    00274                 {
    00275                     QLabel * lb = new QLabel( i18n("size %1").arg( KIO::convertSize(sizeSrc) ), this );
    00276                     gridLayout->addWidget( lb, row, 1 );
    00277                     row++;
    00278                 }
    00279                 if ( ctimeSrc != (time_t)-1 )
    00280                 {
    00281                     QDateTime dctime; dctime.setTime_t( ctimeSrc );
    00282                     QLabel * lb = new QLabel( i18n("created on %1").arg( KGlobal::locale()->formatDateTime(dctime) ), this );
    00283                     gridLayout->addWidget( lb, row, 1 );
    00284                     row++;
    00285                 }
    00286                 if ( mtimeSrc != (time_t)-1 )
    00287                 {
    00288                     QDateTime dmtime; dmtime.setTime_t( mtimeSrc );
    00289                     QLabel * lb = new QLabel( i18n("modified on %1").arg( KGlobal::locale()->formatDateTime(dmtime) ), this );
    00290                     gridLayout->addWidget( lb, row, 1 );
    00291                     row++;
    00292                 }
    00293             }
    00294         }
    00295     }
    00296     else
    00297     {
    00298         // I wonder when this happens. And 'dest' isn't shown at all here...
    00299         // Sounds like the case where we don't want to allow overwriting, the existing
    00300         // file must be preserved. This doesn't happen in KIO though. (David)
    00301         QString sentence1;
    00302         if (mtimeDest < mtimeSrc)
    00303             sentence1 = i18n("An older item than '%1' already exists.\n").arg(d->src);
    00304         else if (mtimeDest == mtimeSrc)
    00305             sentence1 = i18n("A similar file named '%1' already exists.").arg(d->src);
    00306         else
    00307             sentence1 = i18n("A newer item than '%1' already exists.\n").arg(d->src);
    00308
    00309         QLabel *lb = new QLabel( sentence1 + i18n("Do you want to use another file name?"), this );
    00310         d->m_pLayout->addWidget(lb);
    00311     }
    00312     d->m_pLineEdit = new QLineEdit( this );
    00313     d->m_pLayout->addWidget( d->m_pLineEdit );
    00314     QString fileName = KURL(d->dest).fileName();
    00315     d->m_pLineEdit->setText( KIO::decodeFileName( fileName ) );
    00316     if (d->b1)
    00317         connect(d->m_pLineEdit, SIGNAL(textChanged(const QString &)),
    00318                 SLOT(enableRenameButton(const QString &)));
    00319
    00320     d->m_pLayout->addSpacing( 10 );
    00321
    00322     QHBoxLayout* layout = new QHBoxLayout();
    00323     d->m_pLayout->addLayout( layout );
    00324
    00325     layout->addStretch(1);
    00326
    00327     if ( d->b1 )
    00328         layout->addWidget( d->b1 );
    00329     if( d->b8 )
    00330         layout->addWidget( d->b8 );
    00331     if ( d->b2 )
    00332         layout->addWidget( d->b2 );
    00333     if ( d->b3 )
    00334         layout->addWidget( d->b3 );
    00335     if ( d->b4 )
    00336         layout->addWidget( d->b4 );
    00337     if ( d->b5 )
    00338         layout->addWidget( d->b5 );
    00339     if ( d->b6 )
    00340         layout->addWidget( d->b6 );
    00341     if ( d->b7 )
    00342         layout->addWidget( d->b7 );
    00343
    00344
    00345     d->b0->setDefault( true );
    00346     layout->addWidget( d->b0 );
    00347
    00348     resize( sizeHint() );
    00349 }
    00350
    00351 RenameDlg::~RenameDlg()
    00352 {
    00353   delete d;
    00354   // no need to delete Pushbuttons,... qt will do this
    00355 }
    00356
    00357 void RenameDlg::enableRenameButton(const QString &newDest)
    00358 {
    00359   if (newDest != d->dest)
    00360   {
    00361     d->b1->setEnabled(true);
    00362     d->b1->setDefault(true);
    00363   }
    00364   else
    00365     d->b1->setEnabled(false);
    00366 }
    00367
    00368 KURL RenameDlg::newDestURL()
    00369 {
    00370   KURL newDest( d->dest );
    00371   QString fileName = d->m_pLineEdit->text();
    00372   newDest.setFileName( KIO::encodeFileName( fileName ) );
    00373   return newDest;
    00374 }
    00375
    00376 void RenameDlg::b0Pressed()
    00377 {
    00378   done( 0 );
    00379 }
    00380
    00381 // Rename
    00382 void RenameDlg::b1Pressed()
    00383 {
    00384   if ( d->m_pLineEdit->text()  == "" )
    00385     return;
    00386
    00387   KURL u = newDestURL();
    00388   if ( u.isMalformed() )
    00389   {
    00390     KMessageBox::error( this, i18n( "Malformed URL\n%1" ).arg( u.prettyURL() ) );
    00391     return;
    00392   }
    00393
    00394   done( 1 );
    00395 }
    00396 // Propose button clicked
    00397 void RenameDlg::b8Pressed()
    00398 {
    00399   int pos;
    00400
    00401   /* no name to play with */
    00402   if ( d->m_pLineEdit->text().isEmpty() )
    00403     return;
    00404
    00405   QString basename, dotSuffix, tmp;
    00406   QFileInfo info ( d->m_pLineEdit->text() );
    00407   basename = info.baseName();
    00408   dotSuffix = info.extension();
    00409
    00410   if ( !dotSuffix.isEmpty() )
    00411     dotSuffix = '.' + dotSuffix;
    00412
    00413   pos = basename.findRev('_' );
    00414   if(pos != -1 ){
    00415     bool ok;
    00416     tmp = basename.right( basename.length() - (pos + 1) );
    00417     int number = tmp.toInt( &ok, 10 );
    00418     if ( !ok ) {// ok there is no number
    00419       basename.append("_1" );
    00420       d->m_pLineEdit->setText(basename + dotSuffix );
    00421       return;
    00422     }
    00423     else {
    00424      // yes there's allready a number behind the _ so increment it by one
    00425       QString tmp2 = QString::number ( number + 1 );
    00426       basename.replace( pos+1, tmp.length() ,tmp2);
    00427       d->m_pLineEdit->setText( basename + dotSuffix );
    00428       return;
    00429     }
    00430   }
    00431   else // no underscore yet
    00432   {
    00433     d->m_pLineEdit->setText( basename + "_1" + dotSuffix );
    00434     return;
    00435
    00436   }
    00437   return; // we should never return from here jic
    00438 }
    00439
    00440 void RenameDlg::b2Pressed()
    00441 {
    00442   done( 2 );
    00443 }
    00444
    00445 void RenameDlg::b3Pressed()
    00446 {
    00447   done( 3 );
    00448 }
    00449
    00450 void RenameDlg::b4Pressed()
    00451 {
    00452   done( 4 );
    00453 }
    00454
    00455 void RenameDlg::b5Pressed()
    00456 {
    00457   done( 5 );
    00458 }
    00459
    00460 void RenameDlg::b6Pressed()
    00461 {
    00462   done( 6 );
    00463 }
    00464
    00465 void RenameDlg::b7Pressed()
    00466 {
    00467   done( 7 );
    00468 }
    00475 void RenameDlg::pluginHandling()
    00476 {
    00477   d->mimeSrc = mime( d->src );
    00478   d->mimeDest = mime(d->dest );
    00479
    00480   kdDebug(7024) << "Source Mimetype: "<< d->mimeSrc << endl;
    00481   kdDebug(7024) << "Dest Mimetype: "<< d->mimeDest << endl;
    00482 }
    00483 QString RenameDlg::mime( const QString &src )
    00484 {
    00485   KMimeType::Ptr type = KMimeType::findByURL(src );
    00486   //if( type->name() == KMimeType::defaultMimeType() ){ // ok no mimetype
    00487     //    QString ty = KIO::NetAccess::mimetype(d->src );
    00488     // return ty;
    00489     return type->name();
    00490 }
    00491
    00492
    00493 RenameDlg_Result KIO::open_RenameDlg( const QString & _caption,
    00494                                       const QString & _src, const QString & _dest,
    00495                                       RenameDlg_Mode _mode,
    00496                                       QString& _new,
    00497                                       KIO::filesize_t sizeSrc,
    00498                                       KIO::filesize_t sizeDest,
    00499                                       time_t ctimeSrc,
    00500                                       time_t ctimeDest,
    00501                                       time_t mtimeSrc,
    00502                                       time_t mtimeDest)
    00503 {
    00504   Q_ASSERT(kapp);
    00505
    00506   RenameDlg dlg( 0L, _caption, _src, _dest, _mode,
    00507                  sizeSrc, sizeDest, ctimeSrc, ctimeDest, mtimeSrc, mtimeDest,
    00508                  true /*modal*/ );
    00509   int i = dlg.exec();
    00510   _new = dlg.newDestURL().path();
    00511
    00512   return (RenameDlg_Result)i;
    00513 }
    00514
    00515 #include "renamedlg.moc"
    00516
    00517
    00518
    00519
    00520

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:14:37 2004 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001