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->addStrut( 360 );
00148
00149
00150 if ( _mode & M_OVERWRITE_ITSELF ) {
00151 QLabel *lb = new QLabel( i18n( "This action would overwrite '%1' with itself.\n"
00152 "Please enter a new file name:" , KStringHandler::csqueeze( d->src.pathOrUrl(),100 ) ), this );
00153 d->bRename->setText(i18n("C&ontinue"));
00154 pLayout->addWidget( lb );
00155 }
00156 else if ( _mode & M_OVERWRITE ) {
00157
00158
00159
00160 pluginHandling();
00161 KService::List plugin_offers;
00162 if( d->mimeSrc != KMimeType::defaultMimeType() ){
00163 plugin_offers = KMimeTypeTrader::self()->query(d->mimeSrc, "RenameDialog/Plugin");
00164
00165 }else if(d->mimeDest != KMimeType::defaultMimeType() ) {
00166 plugin_offers = KMimeTypeTrader::self()->query(d->mimeDest, "RenameDialog/Plugin");
00167 }
00168 if(!plugin_offers.isEmpty() ){
00169 RenameDialogPlugin::FileItem src( _src, d->mimeSrc, sizeSrc, ctimeSrc, mtimeSrc );
00170 RenameDialogPlugin::FileItem dst( _dest,d->mimeDest, sizeDest, ctimeDest, mtimeDest );
00171 foreach (const KService::Ptr &ptr, plugin_offers) {
00172 RenameDialogPlugin *plugin = ptr->createInstance<RenameDialogPlugin>(this);
00173 if( !plugin )
00174 continue;
00175
00176 plugin->setObjectName( ptr->name() );
00177 if( plugin->wantToHandle( _mode, src, dst ) ) {
00178 d->plugin = true;
00179 plugin->handle( _mode, src, dst );
00180 pLayout->addWidget(plugin );
00181 break;
00182 } else {
00183 delete plugin;
00184 }
00185 }
00186
00187 }
00188
00189 if( !d->plugin ){
00190
00191 QGridLayout * gridLayout = new QGridLayout();
00192 pLayout->addLayout(gridLayout);
00193 gridLayout->setColumnStretch(0,0);
00194 gridLayout->setColumnStretch(1,10);
00195
00196 QString sentence1;
00197 if (mtimeDest < mtimeSrc)
00198 sentence1 = i18n("An older item named '%1' already exists.", d->dest.pathOrUrl());
00199 else if (mtimeDest == mtimeSrc)
00200 sentence1 = i18n("A similar file named '%1' already exists.", d->dest.pathOrUrl());
00201 else
00202 sentence1 = i18n("A newer item named '%1' already exists.", d->dest.pathOrUrl());
00203
00204 QLabel * lb1 = new KSqueezedTextLabel( sentence1, this );
00205 gridLayout->addWidget( lb1, 0, 0, 1, 2 );
00206
00207 lb1 = new QLabel( this );
00208 lb1->setPixmap( KIO::pixmapForUrl( d->dest ) );
00209 gridLayout->addWidget( lb1, 1, 0, 3, 1 );
00210
00211 int row = 1;
00212 if ( sizeDest != (KIO::filesize_t)-1 )
00213 {
00214 QLabel * lb = new QLabel(this);
00215 lb->setText(i18n("size %1 (%2)", KIO::convertSize(sizeDest),
00216 KGlobal::locale()->formatNumber(sizeDest, 0)));
00217 gridLayout->addWidget( lb, row, 1 );
00218 row++;
00219
00220 }
00221 if ( ctimeDest != (time_t)-1 )
00222 {
00223 QDateTime dctime; dctime.setTime_t( ctimeDest );
00224 QLabel * lb = new QLabel( i18n("created on %1", KGlobal::locale()->formatDateTime(dctime) ), this );
00225 gridLayout->addWidget( lb, row, 1 );
00226 row++;
00227 }
00228 if ( mtimeDest != (time_t)-1 )
00229 {
00230 QDateTime dmtime; dmtime.setTime_t( mtimeDest );
00231 QLabel * lb = new QLabel( i18n("modified on %1", KGlobal::locale()->formatDateTime(dmtime) ), this );
00232 gridLayout->addWidget( lb, row, 1 );
00233 row++;
00234 }
00235
00236 if ( !d->src.isEmpty() )
00237 {
00238
00239 gridLayout->addItem( new QSpacerItem(2, fontMetrics().height() ), 4, 0, 1, 2 );
00240 QLabel * lb2 = new KSqueezedTextLabel( i18n("The source file is '%1'", d->src.pathOrUrl()), this );
00241 gridLayout->addWidget( lb2, 5, 0, 1, 2 );
00242
00243 lb2 = new QLabel( this );
00244 lb2->setPixmap( KIO::pixmapForUrl( d->src ) );
00245 gridLayout->addWidget( lb2, 6, 0, 3, 1 );
00246
00247 row = 6;
00248
00249 if ( sizeSrc != (KIO::filesize_t)-1 )
00250 {
00251 QLabel * lb = new QLabel(this);
00252 lb->setText(i18n("size %1 (%2)", KIO::convertSize(sizeSrc),
00253 KGlobal::locale()->formatNumber(sizeSrc, 0)));
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 d->m_pLineEdit->setFocus();
00300 } else {
00301 d->m_pLineEdit->hide();
00302 }
00303 if ( d->bSuggestNewName )
00304 {
00305 layout2->addWidget( d->bSuggestNewName );
00306 setTabOrder( d->m_pLineEdit, d->bSuggestNewName );
00307 }
00308
00309 KSeparator* separator = new KSeparator( this );
00310 pLayout->addWidget( separator );
00311
00312 QHBoxLayout* layout = new QHBoxLayout();
00313 pLayout->addLayout( layout );
00314
00315 layout->addStretch(1);
00316
00317 if ( d->bRename )
00318 {
00319 layout->addWidget( d->bRename );
00320 setTabOrder( d->bRename, d->bCancel );
00321 }
00322 if ( d->bSkip )
00323 {
00324 layout->addWidget( d->bSkip );
00325 setTabOrder( d->bSkip, d->bCancel );
00326 }
00327 if ( d->bAutoSkip )
00328 {
00329 layout->addWidget( d->bAutoSkip );
00330 setTabOrder( d->bAutoSkip, d->bCancel );
00331 }
00332 if ( d->bOverwrite )
00333 {
00334 layout->addWidget( d->bOverwrite );
00335 setTabOrder( d->bOverwrite, d->bCancel );
00336 }
00337 if ( d->bOverwriteAll )
00338 {
00339 layout->addWidget( d->bOverwriteAll );
00340 setTabOrder( d->bOverwriteAll, d->bCancel );
00341 }
00342 if ( d->bResume )
00343 {
00344 layout->addWidget( d->bResume );
00345 setTabOrder( d->bResume, d->bCancel );
00346 }
00347 if ( d->bResumeAll )
00348 {
00349 layout->addWidget( d->bResumeAll );
00350 setTabOrder( d->bResumeAll, d->bCancel );
00351 }
00352
00353 d->bCancel->setDefault( true );
00354 layout->addWidget( d->bCancel );
00355
00356 resize( sizeHint() );
00357 }
00358
00359 RenameDialog::~RenameDialog()
00360 {
00361 delete d;
00362
00363 }
00364
00365 void RenameDialog::enableRenameButton(const QString &newDest)
00366 {
00367 if ( newDest != KIO::decodeFileName( d->dest.fileName() ) && !newDest.isEmpty() )
00368 {
00369 d->bRename->setEnabled( true );
00370 d->bRename->setDefault( true );
00371 if ( d->bOverwrite )
00372 d->bOverwrite->setEnabled( false );
00373 }
00374 else
00375 {
00376 d->bRename->setEnabled( false );
00377 if ( d->bOverwrite )
00378 d->bOverwrite->setEnabled( true );
00379 }
00380 }
00381
00382 KUrl RenameDialog::newDestUrl()
00383 {
00384 KUrl newDest( d->dest );
00385 QString fileName = d->m_pLineEdit->text();
00386 newDest.setFileName( KIO::encodeFileName( fileName ) );
00387 return newDest;
00388 }
00389
00390 void RenameDialog::cancelPressed()
00391 {
00392 done( R_CANCEL );
00393 }
00394
00395
00396 void RenameDialog::renamePressed()
00397 {
00398 if ( d->m_pLineEdit->text().isEmpty() )
00399 return;
00400
00401 KUrl u = newDestUrl();
00402 if ( !u.isValid() )
00403 {
00404 KMessageBox::error( this, i18n( "Malformed URL\n%1" , u.url() ) );
00405 return;
00406 }
00407
00408 done( R_RENAME );
00409 }
00410
00411 QString RenameDialog::suggestName(const KUrl& baseURL, const QString& oldName)
00412 {
00413 QString dotSuffix, suggestedName;
00414 QString basename = oldName;
00415
00416 int index = basename.indexOf( '.' );
00417 if ( index != -1 ) {
00418 dotSuffix = basename.mid( index );
00419 basename.truncate( index );
00420 }
00421
00422 int pos = basename.lastIndexOf( '_' );
00423 if(pos != -1 ){
00424 QString tmp = basename.mid( pos+1 );
00425 bool ok;
00426 int number = tmp.toInt( &ok );
00427 if ( !ok ) {
00428 suggestedName = basename + '1' + dotSuffix;
00429 }
00430 else {
00431
00432 basename.replace( pos+1, tmp.length(), QString::number(number+1) );
00433 suggestedName = basename + dotSuffix;
00434 }
00435 }
00436 else
00437 suggestedName = basename + "_1" + dotSuffix ;
00438
00439
00440 bool exists = false;
00441
00442
00443 if ( baseURL.isLocalFile() )
00444 exists = QFileInfo( baseURL.toLocalFile(KUrl::AddTrailingSlash) + suggestedName ).exists();
00445
00446 if ( !exists )
00447 return suggestedName;
00448 else
00449 return suggestName( baseURL, suggestedName );
00450 }
00451
00452
00453 void RenameDialog::suggestNewNamePressed()
00454 {
00455
00456 if ( d->m_pLineEdit->text().isEmpty() )
00457 return;
00458
00459 KUrl destDirectory( d->dest );
00460 destDirectory.setPath( destDirectory.directory() );
00461 d->m_pLineEdit->setText( suggestName( destDirectory, d->m_pLineEdit->text() ) );
00462 return;
00463 }
00464
00465 void RenameDialog::skipPressed()
00466 {
00467 done( R_SKIP );
00468 }
00469
00470 void RenameDialog::autoSkipPressed()
00471 {
00472 done( R_AUTO_SKIP );
00473 }
00474
00475 void RenameDialog::overwritePressed()
00476 {
00477 done( R_OVERWRITE );
00478 }
00479
00480 void RenameDialog::overwriteAllPressed()
00481 {
00482 done( R_OVERWRITE_ALL );
00483 }
00484
00485 void RenameDialog::resumePressed()
00486 {
00487 done( R_RESUME );
00488 }
00489
00490 void RenameDialog::resumeAllPressed()
00491 {
00492 done( R_RESUME_ALL );
00493 }
00494
00495 static QString mime( const KUrl& src )
00496 {
00497 KMimeType::Ptr type = KMimeType::findByUrl( src );
00498
00499
00500
00501 return type->name();
00502 }
00503
00510 void RenameDialog::pluginHandling()
00511 {
00512 d->mimeSrc = mime( d->src );
00513 d->mimeDest = mime(d->dest );
00514
00515 kDebug(7024) << "Source Mimetype: "<< d->mimeSrc;
00516 kDebug(7024) << "Dest Mimetype: "<< d->mimeDest;
00517 }
00518
00519 #include "renamedialog.moc"