00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "kio/renamedialog.h"
00023 #include "kio/renamedialogplugin.h"
00024 #include <stdio.h>
00025 #include <assert.h>
00026
00027 #include <QtCore/QDate>
00028 #include <QtCore/QFileInfo>
00029 #include <QtGui/QLabel>
00030 #include <QtGui/QLayout>
00031 #include <QtCore/QDir>
00032
00033 #include <klineedit.h>
00034 #include <kmessagebox.h>
00035 #include <kpushbutton.h>
00036 #include <kio/global.h>
00037 #include <kmimetypetrader.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 <kseparator.h>
00045 #include <kstringhandler.h>
00046 #include <kstandardguiitem.h>
00047 #include <kguiitem.h>
00048 #include <ksqueezedtextlabel.h>
00049
00050 using namespace KIO;
00051
00053 class RenameDialog::RenameDialogPrivate
00054 {
00055 public:
00056 RenameDialogPrivate(){
00057 bCancel = 0;
00058 bRename = bSkip = bAutoSkip = bOverwrite = bOverwriteAll = 0;
00059 bResume = bResumeAll = bSuggestNewName = 0;
00060 m_pLineEdit = 0;
00061 }
00062 KPushButton *bCancel;
00063 QPushButton *bRename;
00064 QPushButton *bSkip;
00065 QPushButton *bAutoSkip;
00066 QPushButton *bOverwrite;
00067 QPushButton *bOverwriteAll;
00068 QPushButton *bResume;
00069 QPushButton *bResumeAll;
00070 QPushButton *bSuggestNewName;
00071 KLineEdit* m_pLineEdit;
00072 KUrl src;
00073 KUrl dest;
00074 QString mimeSrc;
00075 QString mimeDest;
00076 bool plugin;
00077 };
00078
00079 RenameDialog::RenameDialog(QWidget *parent, const QString & _caption,
00080 const KUrl &_src, const KUrl &_dest,
00081 RenameDialog_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 : QDialog ( parent ), d(new RenameDialogPrivate)
00089 {
00090 setObjectName( "KIO::RenameDialog" );
00091
00092 d->src = _src;
00093 d->dest = _dest;
00094 d->plugin = false;
00095
00096 setWindowTitle( _caption );
00097
00098 d->bCancel = new KPushButton( KStandardGuiItem::cancel(), this );
00099 connect(d->bCancel, SIGNAL(clicked()), this, SLOT(cancelPressed()));
00100
00101 if ( ! (_mode & M_NORENAME ) ) {
00102 d->bRename = new QPushButton( i18n( "&Rename" ), this );
00103 d->bRename->setEnabled(false);
00104 d->bSuggestNewName = new QPushButton( i18n( "Suggest New &Name" ), this );
00105 connect(d->bSuggestNewName, SIGNAL(clicked()), this, SLOT(suggestNewNamePressed()));
00106 connect(d->bRename, SIGNAL(clicked()), this, SLOT(renamePressed()));
00107 }
00108
00109 if ( ( _mode & M_MULTI ) && ( _mode & M_SKIP ) ) {
00110 d->bSkip = new QPushButton( i18n( "&Skip" ), this );
00111 d->bSkip->setToolTip((_mode & M_ISDIR) ? i18n("Do not copy or move this folder, skip to the next item instead")
00112 : i18n("Do not copy or move this file, skip to the next item instead"));
00113 connect(d->bSkip, SIGNAL(clicked()), this, SLOT(skipPressed()));
00114
00115 d->bAutoSkip = new QPushButton( i18n( "&Auto Skip" ), this );
00116 d->bAutoSkip->setToolTip((_mode & M_ISDIR) ? i18n("Do not copy or move any folder that already exists in the destination folder.\nYou will be prompted again in case of a conflict with an existing file though.")
00117 : i18n("Do not copy or move any file that already exists in the destination folder.\nYou will be prompted again in case of a conflict with an existing directory though."));
00118 connect(d->bAutoSkip, SIGNAL(clicked()), this, SLOT(autoSkipPressed()));
00119 }
00120
00121 if ( _mode & M_OVERWRITE ) {
00122 const QString text = (_mode & M_ISDIR) ? i18nc("Write files into an existing folder", "&Write Into") : i18n("&Overwrite");
00123 d->bOverwrite = new QPushButton(text, this);
00124 d->bOverwrite->setToolTip(i18n("Files and folders will be copied into the existing directory, alongside its existing contents.\nYou will be prompted again in case of a conflict with an existing file in the directory."));
00125 connect(d->bOverwrite, SIGNAL(clicked()), this, SLOT(overwritePressed()));
00126
00127 if ( _mode & M_MULTI ) {
00128 const QString textAll = (_mode & M_ISDIR) ? i18nc("Write files into any existing directory", "&Write Into All") : i18n("&Overwrite All");
00129 d->bOverwriteAll = new QPushButton( textAll, this );
00130 d->bOverwriteAll->setToolTip(i18n("Files and folders will be copied into any existing directory, alongside its existing contents.\nYou will be prompted again in case of a conflict with an existing file in a directory, but not in case of another existing directory."));
00131 connect(d->bOverwriteAll, SIGNAL(clicked()), this, SLOT(overwriteAllPressed()));
00132 }
00133 }
00134
00135 if ( _mode & M_RESUME ) {
00136 d->bResume = new QPushButton( i18n( "&Resume" ), this );
00137 connect(d->bResume, SIGNAL(clicked()), this, SLOT(resumePressed()));
00138
00139 if ( _mode & M_MULTI )
00140 {
00141 d->bResumeAll = new QPushButton( i18n( "R&esume All" ), this );
00142 connect(d->bResumeAll, SIGNAL(clicked()), this, SLOT(resumeAllPressed()));
00143 }
00144 }
00145
00146 QVBoxLayout* pLayout = new QVBoxLayout( this );
00147 pLayout->setMargin( KDialog::marginHint() );
00148 pLayout->setSpacing( KDialog::spacingHint() );
00149 pLayout->addStrut( 360 );
00150
00151
00152 if ( _mode & M_OVERWRITE_ITSELF ) {
00153 QLabel *lb = new QLabel( i18n( "This action would overwrite '%1' with itself.\n"
00154 "Please enter a new file name:" , KStringHandler::csqueeze( d->src.pathOrUrl(),100 ) ), this );
00155 d->bRename->setText(i18n("C&ontinue"));
00156 pLayout->addWidget( lb );
00157 }
00158 else if ( _mode & M_OVERWRITE ) {
00159
00160
00161
00162 pluginHandling();
00163 KService::List plugin_offers;
00164 if( d->mimeSrc != KMimeType::defaultMimeType() ){
00165 plugin_offers = KMimeTypeTrader::self()->query(d->mimeSrc, "RenameDialog/Plugin");
00166
00167 }else if(d->mimeDest != KMimeType::defaultMimeType() ) {
00168 plugin_offers = KMimeTypeTrader::self()->query(d->mimeDest, "RenameDialog/Plugin");
00169 }
00170 if(!plugin_offers.isEmpty() ){
00171 RenameDialogPlugin::FileItem src( _src, d->mimeSrc, sizeSrc, ctimeSrc, mtimeSrc );
00172 RenameDialogPlugin::FileItem dst( _dest,d->mimeDest, sizeDest, ctimeDest, mtimeDest );
00173 foreach (const KService::Ptr &ptr, plugin_offers) {
00174 RenameDialogPlugin *plugin = ptr->createInstance<RenameDialogPlugin>(this);
00175 if( !plugin )
00176 continue;
00177
00178 plugin->setObjectName( ptr->name() );
00179 if( plugin->wantToHandle( _mode, src, dst ) ) {
00180 d->plugin = true;
00181 plugin->handle( _mode, src, dst );
00182 pLayout->addWidget(plugin );
00183 break;
00184 } else {
00185 delete plugin;
00186 }
00187 }
00188
00189 }
00190
00191 if( !d->plugin ){
00192
00193 QGridLayout * gridLayout = new QGridLayout();
00194 gridLayout->setMargin( KDialog::marginHint() );
00195 gridLayout->setSpacing( KDialog::spacingHint() );
00196 pLayout->addLayout(gridLayout);
00197 gridLayout->setColumnStretch(0,0);
00198 gridLayout->setColumnStretch(1,10);
00199
00200 QString sentence1;
00201 if (mtimeDest < mtimeSrc)
00202 sentence1 = i18n("An older item named '%1' already exists.", d->dest.pathOrUrl());
00203 else if (mtimeDest == mtimeSrc)
00204 sentence1 = i18n("A similar file named '%1' already exists.", d->dest.pathOrUrl());
00205 else
00206 sentence1 = i18n("A newer item named '%1' already exists.", d->dest.pathOrUrl());
00207
00208 QLabel * lb1 = new KSqueezedTextLabel( sentence1, this );
00209 gridLayout->addWidget( lb1, 0, 0, 1, 2 );
00210
00211 lb1 = new QLabel( this );
00212 lb1->setPixmap( KIO::pixmapForUrl( d->dest ) );
00213 gridLayout->addWidget( lb1, 1, 0, 3, 1 );
00214
00215 int row = 1;
00216 if ( sizeDest != (KIO::filesize_t)-1 )
00217 {
00218 QLabel * lb = new QLabel( i18n("size %1", KIO::convertSize(sizeDest) ), this );
00219 gridLayout->addWidget( lb, row, 1 );
00220 row++;
00221
00222 }
00223 if ( ctimeDest != (time_t)-1 )
00224 {
00225 QDateTime dctime; dctime.setTime_t( ctimeDest );
00226 QLabel * lb = new QLabel( i18n("created on %1", KGlobal::locale()->formatDateTime(dctime) ), this );
00227 gridLayout->addWidget( lb, row, 1 );
00228 row++;
00229 }
00230 if ( mtimeDest != (time_t)-1 )
00231 {
00232 QDateTime dmtime; dmtime.setTime_t( mtimeDest );
00233 QLabel * lb = new QLabel( i18n("modified on %1", KGlobal::locale()->formatDateTime(dmtime) ), this );
00234 gridLayout->addWidget( lb, row, 1 );
00235 row++;
00236 }
00237
00238 if ( !d->src.isEmpty() )
00239 {
00240
00241
00242 QLabel * lb2 = new KSqueezedTextLabel( i18n("The source file is '%1'", d->src.pathOrUrl()), this );
00243 gridLayout->addWidget( lb2, 5, 0, 1, 2 );
00244
00245 lb2 = new QLabel( this );
00246 lb2->setPixmap( KIO::pixmapForUrl( d->src ) );
00247 gridLayout->addWidget( lb2, 6, 0, 3, 1 );
00248
00249 row = 6;
00250
00251 if ( sizeSrc != (KIO::filesize_t)-1 )
00252 {
00253 QLabel * lb = new QLabel( i18n("size %1", KIO::convertSize(sizeSrc) ), this );
00254 gridLayout->addWidget( lb, row, 1 );
00255 row++;
00256 }
00257 if ( ctimeSrc != (time_t)-1 )
00258 {
00259 QDateTime dctime; dctime.setTime_t( ctimeSrc );
00260 QLabel * lb = new QLabel( i18n("created on %1", KGlobal::locale()->formatDateTime(dctime) ), this );
00261 gridLayout->addWidget( lb, row, 1 );
00262 row++;
00263 }
00264 if ( mtimeSrc != (time_t)-1 )
00265 {
00266 QDateTime dmtime; dmtime.setTime_t( mtimeSrc );
00267 QLabel * lb = new QLabel( i18n("modified on %1", KGlobal::locale()->formatDateTime(dmtime) ), this );
00268 gridLayout->addWidget( lb, row, 1 );
00269 row++;
00270 }
00271 }
00272 }
00273 }
00274 else
00275 {
00276
00277
00278 QString sentence1;
00279 if (mtimeDest < mtimeSrc)
00280 sentence1 = i18n("An older item named '%1' already exists.", d->dest.pathOrUrl());
00281 else if (mtimeDest == mtimeSrc)
00282 sentence1 = i18n("A similar file named '%1' already exists.", d->dest.pathOrUrl());
00283 else
00284 sentence1 = i18n("A newer item named '%1' already exists.", d->dest.pathOrUrl());
00285
00286 QLabel *lb = new KSqueezedTextLabel( sentence1, this );
00287 pLayout->addWidget(lb);
00288 }
00289 QHBoxLayout* layout2 = new QHBoxLayout();
00290 pLayout->addLayout( layout2 );
00291
00292 d->m_pLineEdit = new KLineEdit( this );
00293 layout2->addWidget( d->m_pLineEdit );
00294 if ( d->bRename ) {
00295 const QString fileName = d->dest.fileName();
00296 d->m_pLineEdit->setText( KIO::decodeFileName( fileName ) );
00297 connect(d->m_pLineEdit, SIGNAL(textChanged(const QString &)),
00298 SLOT(enableRenameButton(const QString &)));
00299 } else {
00300 d->m_pLineEdit->hide();
00301 }
00302 if ( d->bSuggestNewName )
00303 {
00304 layout2->addWidget( d->bSuggestNewName );
00305 setTabOrder( d->m_pLineEdit, d->bSuggestNewName );
00306 }
00307
00308 KSeparator* separator = new KSeparator( this );
00309 pLayout->addWidget( separator );
00310
00311 QHBoxLayout* layout = new QHBoxLayout();
00312 pLayout->addLayout( layout );
00313
00314 layout->addStretch(1);
00315
00316 if ( d->bRename )
00317 {
00318 layout->addWidget( d->bRename );
00319 setTabOrder( d->bRename, d->bCancel );
00320 }
00321 if ( d->bSkip )
00322 {
00323 layout->addWidget( d->bSkip );
00324 setTabOrder( d->bSkip, d->bCancel );
00325 }
00326 if ( d->bAutoSkip )
00327 {
00328 layout->addWidget( d->bAutoSkip );
00329 setTabOrder( d->bAutoSkip, d->bCancel );
00330 }
00331 if ( d->bOverwrite )
00332 {
00333 layout->addWidget( d->bOverwrite );
00334 setTabOrder( d->bOverwrite, d->bCancel );
00335 }
00336 if ( d->bOverwriteAll )
00337 {
00338 layout->addWidget( d->bOverwriteAll );
00339 setTabOrder( d->bOverwriteAll, d->bCancel );
00340 }
00341 if ( d->bResume )
00342 {
00343 layout->addWidget( d->bResume );
00344 setTabOrder( d->bResume, d->bCancel );
00345 }
00346 if ( d->bResumeAll )
00347 {
00348 layout->addWidget( d->bResumeAll );
00349 setTabOrder( d->bResumeAll, d->bCancel );
00350 }
00351
00352 d->bCancel->setDefault( true );
00353 layout->addWidget( d->bCancel );
00354
00355 resize( sizeHint() );
00356 }
00357
00358 RenameDialog::~RenameDialog()
00359 {
00360 delete d;
00361
00362 }
00363
00364 void RenameDialog::enableRenameButton(const QString &newDest)
00365 {
00366 if ( newDest != KIO::decodeFileName( d->dest.fileName() ) && !newDest.isEmpty() )
00367 {
00368 d->bRename->setEnabled( true );
00369 d->bRename->setDefault( true );
00370 if ( d->bOverwrite )
00371 d->bOverwrite->setEnabled( false );
00372 }
00373 else
00374 {
00375 d->bRename->setEnabled( false );
00376 if ( d->bOverwrite )
00377 d->bOverwrite->setEnabled( true );
00378 }
00379 }
00380
00381 KUrl RenameDialog::newDestUrl()
00382 {
00383 KUrl newDest( d->dest );
00384 QString fileName = d->m_pLineEdit->text();
00385 newDest.setFileName( KIO::encodeFileName( fileName ) );
00386 return newDest;
00387 }
00388
00389 void RenameDialog::cancelPressed()
00390 {
00391 done( R_CANCEL );
00392 }
00393
00394
00395 void RenameDialog::renamePressed()
00396 {
00397 if ( d->m_pLineEdit->text().isEmpty() )
00398 return;
00399
00400 KUrl u = newDestUrl();
00401 if ( !u.isValid() )
00402 {
00403 KMessageBox::error( this, i18n( "Malformed URL\n%1" , u.url() ) );
00404 return;
00405 }
00406
00407 done( R_RENAME );
00408 }
00409
00410 QString RenameDialog::suggestName(const KUrl& baseURL, const QString& oldName)
00411 {
00412 QString dotSuffix, suggestedName;
00413 QString basename = oldName;
00414
00415 int index = basename.indexOf( '.' );
00416 if ( index != -1 ) {
00417 dotSuffix = basename.mid( index );
00418 basename.truncate( index );
00419 }
00420
00421 int pos = basename.lastIndexOf( '_' );
00422 if(pos != -1 ){
00423 QString tmp = basename.mid( pos+1 );
00424 bool ok;
00425 int number = tmp.toInt( &ok );
00426 if ( !ok ) {
00427 suggestedName = basename + '1' + dotSuffix;
00428 }
00429 else {
00430
00431 basename.replace( pos+1, tmp.length(), QString::number(number+1) );
00432 suggestedName = basename + dotSuffix;
00433 }
00434 }
00435 else
00436 suggestedName = basename + "_1" + dotSuffix ;
00437
00438
00439 bool exists = false;
00440
00441
00442 if ( baseURL.isLocalFile() )
00443 exists = QFileInfo( baseURL.path(KUrl::AddTrailingSlash) + suggestedName ).exists();
00444
00445 if ( !exists )
00446 return suggestedName;
00447 else
00448 return suggestName( baseURL, suggestedName );
00449 }
00450
00451
00452 void RenameDialog::suggestNewNamePressed()
00453 {
00454
00455 if ( d->m_pLineEdit->text().isEmpty() )
00456 return;
00457
00458 KUrl destDirectory( d->dest );
00459 destDirectory.setPath( destDirectory.directory() );
00460 d->m_pLineEdit->setText( suggestName( destDirectory, d->m_pLineEdit->text() ) );
00461 return;
00462 }
00463
00464 void RenameDialog::skipPressed()
00465 {
00466 done( R_SKIP );
00467 }
00468
00469 void RenameDialog::autoSkipPressed()
00470 {
00471 done( R_AUTO_SKIP );
00472 }
00473
00474 void RenameDialog::overwritePressed()
00475 {
00476 done( R_OVERWRITE );
00477 }
00478
00479 void RenameDialog::overwriteAllPressed()
00480 {
00481 done( R_OVERWRITE_ALL );
00482 }
00483
00484 void RenameDialog::resumePressed()
00485 {
00486 done( R_RESUME );
00487 }
00488
00489 void RenameDialog::resumeAllPressed()
00490 {
00491 done( R_RESUME_ALL );
00492 }
00493
00494 static QString mime( const KUrl& src )
00495 {
00496 KMimeType::Ptr type = KMimeType::findByUrl( src );
00497
00498
00499
00500 return type->name();
00501 }
00502
00509 void RenameDialog::pluginHandling()
00510 {
00511 d->mimeSrc = mime( d->src );
00512 d->mimeDest = mime(d->dest );
00513
00514 kDebug(7024) << "Source Mimetype: "<< d->mimeSrc;
00515 kDebug(7024) << "Dest Mimetype: "<< d->mimeDest;
00516 }
00517
00518 #include "renamedialog.moc"