00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "tdeio/renamedlg.h"
00023 #include "tdeio/renamedlgplugin.h"
00024 #include <stdio.h>
00025 #include <assert.h>
00026
00027 #include <tqfileinfo.h>
00028 #include <tqlabel.h>
00029 #include <tqlayout.h>
00030 #include <tqlineedit.h>
00031 #include <tqdir.h>
00032
00033 #include <tdemessagebox.h>
00034 #include <kpushbutton.h>
00035 #include <tdeapplication.h>
00036 #include <tdeio/global.h>
00037 #include <ktrader.h>
00038 #include <klibloader.h>
00039 #include <kdialog.h>
00040 #include <tdelocale.h>
00041 #include <tdeglobal.h>
00042 #include <kdebug.h>
00043 #include <kurl.h>
00044 #include <kmimetype.h>
00045 #include <kseparator.h>
00046 #include <kstringhandler.h>
00047 #include <kstdguiitem.h>
00048 #include <kguiitem.h>
00049 #include <ksqueezedtextlabel.h>
00050
00051 #ifdef Q_WS_X11
00052 #include <twin.h>
00053 #endif
00054
00055 using namespace TDEIO;
00056
00057 class RenameDlg::RenameDlgPrivate
00058 {
00059 public:
00060 RenameDlgPrivate(){
00061 bCancel = 0;
00062 bRename = bSkip = bAutoSkip = bOverwrite = bOverwriteAll = 0;
00063 bResume = bResumeAll = bSuggestNewName = 0;
00064 m_pLineEdit = 0;
00065 }
00066 KPushButton *bCancel;
00067 TQPushButton *bRename;
00068 TQPushButton *bSkip;
00069 TQPushButton *bAutoSkip;
00070 TQPushButton *bOverwrite;
00071 TQPushButton *bOverwriteAll;
00072 TQPushButton *bResume;
00073 TQPushButton *bResumeAll;
00074 TQPushButton *bSuggestNewName;
00075 TQLineEdit* m_pLineEdit;
00076 KURL src;
00077 KURL dest;
00078 TQString mimeSrc;
00079 TQString mimeDest;
00080 bool modal;
00081 bool plugin;
00082 };
00083
00084 RenameDlg::RenameDlg(TQWidget *parent, const TQString & _caption,
00085 const TQString &_src, const TQString &_dest,
00086 RenameDlg_Mode _mode,
00087 TDEIO::filesize_t sizeSrc,
00088 TDEIO::filesize_t sizeDest,
00089 time_t ctimeSrc,
00090 time_t ctimeDest,
00091 time_t mtimeSrc,
00092 time_t mtimeDest,
00093 bool _modal)
00094 : TQDialog ( parent, "TDEIO::RenameDialog" , _modal )
00095 {
00096 d = new RenameDlgPrivate( );
00097 d->modal = _modal;
00098 #if 0
00099
00100
00101
00102 #if !defined(Q_WS_QWS) && !defined(Q_WS_WIN) //FIXME(E): Implement for QT Embedded & win32
00103 if (d->modal)
00104 KWin::setState( winId(), NET::StaysOnTop );
00105 #endif
00106 #endif
00107
00108 d->src = _src;
00109 d->dest = _dest;
00110 d->plugin = false;
00111
00112
00113 setCaption( _caption );
00114
00115 d->bCancel = new KPushButton( KStdGuiItem::cancel(), this );
00116 connect(d->bCancel, TQT_SIGNAL(clicked()), this, TQT_SLOT(b0Pressed()));
00117
00118 if ( ! (_mode & M_NORENAME ) ) {
00119 d->bRename = new TQPushButton( i18n( "&Rename" ), this );
00120 d->bRename->setEnabled(false);
00121 d->bSuggestNewName = new TQPushButton( i18n( "Suggest New &Name" ), this );
00122 connect(d->bSuggestNewName, TQT_SIGNAL(clicked()), this, TQT_SLOT(b8Pressed()));
00123 connect(d->bRename, TQT_SIGNAL(clicked()), this, TQT_SLOT(b1Pressed()));
00124 }
00125
00126 if ( ( _mode & M_MULTI ) && ( _mode & M_SKIP ) ) {
00127 d->bSkip = new TQPushButton( i18n( "&Skip" ), this );
00128 connect(d->bSkip, TQT_SIGNAL(clicked()), this, TQT_SLOT(b2Pressed()));
00129
00130 d->bAutoSkip = new TQPushButton( i18n( "&Auto Skip" ), this );
00131 connect(d->bAutoSkip, TQT_SIGNAL(clicked()), this, TQT_SLOT(b3Pressed()));
00132 }
00133
00134 if ( _mode & M_OVERWRITE ) {
00135 d->bOverwrite = new TQPushButton( i18n( "&Overwrite" ), this );
00136 connect(d->bOverwrite, TQT_SIGNAL(clicked()), this, TQT_SLOT(b4Pressed()));
00137
00138 if ( _mode & M_MULTI ) {
00139 d->bOverwriteAll = new TQPushButton( i18n( "O&verwrite All" ), this );
00140 connect(d->bOverwriteAll, TQT_SIGNAL(clicked()), this, TQT_SLOT(b5Pressed()));
00141 }
00142 }
00143
00144 if ( _mode & M_RESUME ) {
00145 d->bResume = new TQPushButton( i18n( "&Resume" ), this );
00146 connect(d->bResume, TQT_SIGNAL(clicked()), this, TQT_SLOT(b6Pressed()));
00147
00148 if ( _mode & M_MULTI )
00149 {
00150 d->bResumeAll = new TQPushButton( i18n( "R&esume All" ), this );
00151 connect(d->bResumeAll, TQT_SIGNAL(clicked()), this, TQT_SLOT(b7Pressed()));
00152 }
00153 }
00154
00155 TQVBoxLayout* pLayout = new TQVBoxLayout( this, KDialog::marginHint(),
00156 KDialog::spacingHint() );
00157 pLayout->addStrut( 360 );
00158
00159
00160 if ( _mode & M_OVERWRITE_ITSELF ) {
00161 TQLabel *lb = new TQLabel( i18n( "This action would overwrite '%1' with itself.\n"
00162 "Please enter a new file name:" ).arg( KStringHandler::csqueeze( d->src.pathOrURL(),100 ) ), this );
00163 d->bRename->setText(i18n("C&ontinue"));
00164 pLayout->addWidget( lb );
00165 }
00166 else if ( _mode & M_OVERWRITE ) {
00167
00168
00169
00170 pluginHandling();
00171 TDETrader::OfferList plugin_offers;
00172 if( d->mimeSrc != KMimeType::defaultMimeType() ){
00173 plugin_offers = TDETrader::self()->query(d->mimeSrc, "'RenameDlg/Plugin' in ServiceTypes");
00174
00175 }else if(d->mimeDest != KMimeType::defaultMimeType() ) {
00176 plugin_offers = TDETrader::self()->query(d->mimeDest, "'RenameDlg/Plugin' in ServiceTypes");
00177 }
00178 if(!plugin_offers.isEmpty() ){
00179 kdDebug(7024) << "Offers" << endl;
00180 TDETrader::OfferList::ConstIterator it = plugin_offers.begin();
00181 TDETrader::OfferList::ConstIterator end = plugin_offers.end();
00182 for( ; it != end; ++it ){
00183 TQString libName = (*it)->library();
00184 if( libName.isEmpty() ){
00185 kdDebug(7024) << "lib is empty" << endl;
00186 continue;
00187 }
00188 KLibrary *lib = KLibLoader::self()->library(libName.local8Bit() );
00189 if(!lib) {
00190 continue;
00191 }
00192 KLibFactory *factory = lib->factory();
00193 if(!factory){
00194 lib->unload();
00195 continue;
00196 }
00197 TQObject *obj = factory->create( TQT_TQOBJECT(this), (*it)->name().latin1() );
00198 if(!obj) {
00199 lib->unload();
00200 continue;
00201 }
00202 RenameDlgPlugin *plugin = static_cast<RenameDlgPlugin *>(TQT_TQWIDGET(obj));
00203 if(!plugin ){
00204 delete obj;
00205 continue;
00206 }
00207 if( plugin->initialize( _mode, _src, _dest, d->mimeSrc,
00208 d->mimeDest, sizeSrc, sizeDest,
00209 ctimeSrc, ctimeDest,
00210 mtimeSrc, mtimeDest ) ) {
00211 d->plugin = true;
00212 pLayout->addWidget(plugin );
00213 kdDebug(7024) << "RenameDlgPlugin" << endl;
00214 break;
00215 } else {
00216 delete obj;
00217 }
00218 }
00219
00220 }
00221
00222 if( !d->plugin ){
00223
00224 TQGridLayout * gridLayout = new TQGridLayout( 0L, 9, 2, KDialog::marginHint(),
00225 KDialog::spacingHint() );
00226 pLayout->addLayout(TQT_TQLAYOUT(gridLayout));
00227 gridLayout->setColStretch(0,0);
00228 gridLayout->setColStretch(1,10);
00229
00230 TQString sentence1;
00231 if (mtimeDest < mtimeSrc)
00232 sentence1 = i18n("An older item named '%1' already exists.");
00233 else if (mtimeDest == mtimeSrc)
00234 sentence1 = i18n("A similar file named '%1' already exists.");
00235 else
00236 sentence1 = i18n("A newer item named '%1' already exists.");
00237
00238 TQLabel * lb1 = new KSqueezedTextLabel( sentence1.arg(d->dest.pathOrURL() ), this );
00239 gridLayout->addMultiCellWidget( lb1, 0, 0, 0, 1 );
00240
00241 lb1 = new TQLabel( this );
00242 lb1->setPixmap( KMimeType::pixmapForURL( d->dest ) );
00243 gridLayout->addMultiCellWidget( lb1, 1, 3, 0, 0 );
00244
00245 int row = 1;
00246 if ( sizeDest != (TDEIO::filesize_t)-1 )
00247 {
00248 TQLabel * lb = new TQLabel( i18n("size %1").arg( TDEIO::convertSize(sizeDest) ), this );
00249 gridLayout->addWidget( lb, row, 1 );
00250 row++;
00251
00252 }
00253 if ( ctimeDest != (time_t)-1 )
00254 {
00255 TQDateTime dctime; dctime.setTime_t( ctimeDest );
00256 TQLabel * lb = new TQLabel( i18n("created on %1").arg( TDEGlobal::locale()->formatDateTime(dctime) ), this );
00257 gridLayout->addWidget( lb, row, 1 );
00258 row++;
00259 }
00260 if ( mtimeDest != (time_t)-1 )
00261 {
00262 TQDateTime dmtime; dmtime.setTime_t( mtimeDest );
00263 TQLabel * lb = new TQLabel( i18n("modified on %1").arg( TDEGlobal::locale()->formatDateTime(dmtime) ), this );
00264 gridLayout->addWidget( lb, row, 1 );
00265 row++;
00266 }
00267
00268 if ( !d->src.isEmpty() )
00269 {
00270
00271 gridLayout->addRowSpacing( 4, 20 );
00272
00273 TQLabel * lb2 = new KSqueezedTextLabel( i18n("The source file is '%1'").arg(d->src.pathOrURL()), this );
00274 gridLayout->addMultiCellWidget( lb2, 5, 5, 0, 1 );
00275
00276 lb2 = new TQLabel( this );
00277 lb2->setPixmap( KMimeType::pixmapForURL( d->src ) );
00278 gridLayout->addMultiCellWidget( lb2, 6, 8, 0, 0 );
00279
00280 row = 6;
00281
00282 if ( sizeSrc != (TDEIO::filesize_t)-1 )
00283 {
00284 TQLabel * lb = new TQLabel( i18n("size %1").arg( TDEIO::convertSize(sizeSrc) ), this );
00285 gridLayout->addWidget( lb, row, 1 );
00286 row++;
00287 }
00288 if ( ctimeSrc != (time_t)-1 )
00289 {
00290 TQDateTime dctime; dctime.setTime_t( ctimeSrc );
00291 TQLabel * lb = new TQLabel( i18n("created on %1").arg( TDEGlobal::locale()->formatDateTime(dctime) ), this );
00292 gridLayout->addWidget( lb, row, 1 );
00293 row++;
00294 }
00295 if ( mtimeSrc != (time_t)-1 )
00296 {
00297 TQDateTime dmtime; dmtime.setTime_t( mtimeSrc );
00298 TQLabel * lb = new TQLabel( i18n("modified on %1").arg( TDEGlobal::locale()->formatDateTime(dmtime) ), this );
00299 gridLayout->addWidget( lb, row, 1 );
00300 row++;
00301 }
00302 }
00303 }
00304 }
00305 else
00306 {
00307
00308
00309 TQString sentence1;
00310 if (mtimeDest < mtimeSrc)
00311 sentence1 = i18n("An older item named '%1' already exists.");
00312 else if (mtimeDest == mtimeSrc)
00313 sentence1 = i18n("A similar file named '%1' already exists.");
00314 else
00315 sentence1 = i18n("A newer item named '%1' already exists.");
00316
00317 TQLabel *lb = new KSqueezedTextLabel( sentence1.arg(d->dest.pathOrURL()), this );
00318 pLayout->addWidget(lb);
00319 }
00320 TQHBoxLayout* layout2 = new TQHBoxLayout();
00321 pLayout->addLayout( layout2 );
00322
00323 d->m_pLineEdit = new TQLineEdit( this );
00324 layout2->addWidget( d->m_pLineEdit );
00325 TQString fileName = d->dest.fileName();
00326 d->m_pLineEdit->setText( TDEIO::decodeFileName( fileName ) );
00327 if ( d->bRename || d->bOverwrite )
00328 connect(d->m_pLineEdit, TQT_SIGNAL(textChanged(const TQString &)),
00329 TQT_SLOT(enableRenameButton(const TQString &)));
00330 if ( d->bSuggestNewName )
00331 {
00332 layout2->addWidget( d->bSuggestNewName );
00333 setTabOrder( d->m_pLineEdit, d->bSuggestNewName );
00334 }
00335
00336 KSeparator* separator = new KSeparator( this );
00337 pLayout->addWidget( separator );
00338
00339 TQHBoxLayout* layout = new TQHBoxLayout();
00340 pLayout->addLayout( layout );
00341
00342 layout->addStretch(1);
00343
00344 if ( d->bRename )
00345 {
00346 layout->addWidget( d->bRename );
00347 setTabOrder( d->bRename, d->bCancel );
00348 }
00349 if ( d->bSkip )
00350 {
00351 layout->addWidget( d->bSkip );
00352 setTabOrder( d->bSkip, d->bCancel );
00353 }
00354 if ( d->bAutoSkip )
00355 {
00356 layout->addWidget( d->bAutoSkip );
00357 setTabOrder( d->bAutoSkip, d->bCancel );
00358 }
00359 if ( d->bOverwrite )
00360 {
00361 layout->addWidget( d->bOverwrite );
00362 setTabOrder( d->bOverwrite, d->bCancel );
00363 }
00364 if ( d->bOverwriteAll )
00365 {
00366 layout->addWidget( d->bOverwriteAll );
00367 setTabOrder( d->bOverwriteAll, d->bCancel );
00368 }
00369 if ( d->bResume )
00370 {
00371 layout->addWidget( d->bResume );
00372 setTabOrder( d->bResume, d->bCancel );
00373 }
00374 if ( d->bResumeAll )
00375 {
00376 layout->addWidget( d->bResumeAll );
00377 setTabOrder( d->bResumeAll, d->bCancel );
00378 }
00379
00380 d->bCancel->setDefault( true );
00381 layout->addWidget( d->bCancel );
00382
00383 resize( sizeHint() );
00384
00385 d->m_pLineEdit->setFocus();
00386 }
00387
00388 RenameDlg::~RenameDlg()
00389 {
00390 delete d;
00391
00392 }
00393
00394 void RenameDlg::enableRenameButton(const TQString &newDest)
00395 {
00396 if ( newDest != TDEIO::decodeFileName( d->dest.fileName() ) && !newDest.isEmpty() )
00397 {
00398 d->bRename->setEnabled( true );
00399 d->bRename->setDefault( true );
00400 if ( d->bOverwrite )
00401 d->bOverwrite->setEnabled( false );
00402 }
00403 else
00404 {
00405 d->bRename->setEnabled( false );
00406 if ( d->bOverwrite )
00407 d->bOverwrite->setEnabled( true );
00408 }
00409 }
00410
00411 KURL RenameDlg::newDestURL()
00412 {
00413 KURL newDest( d->dest );
00414 TQString fileName = d->m_pLineEdit->text();
00415 newDest.setFileName( TDEIO::encodeFileName( fileName ) );
00416 return newDest;
00417 }
00418
00419 void RenameDlg::b0Pressed()
00420 {
00421 done( 0 );
00422 }
00423
00424
00425 void RenameDlg::b1Pressed()
00426 {
00427 if ( d->m_pLineEdit->text().isEmpty() )
00428 return;
00429
00430 KURL u = newDestURL();
00431 if ( !u.isValid() )
00432 {
00433 KMessageBox::error( this, i18n( "Malformed URL\n%1" ).arg( u.url() ) );
00434 return;
00435 }
00436
00437 done( 1 );
00438 }
00439
00440 TQString RenameDlg::suggestName(const KURL& baseURL, const TQString& oldName)
00441 {
00442 TQString dotSuffix, suggestedName;
00443 TQString basename = oldName;
00444
00445 int index = basename.find( '.' );
00446 if ( index != -1 ) {
00447 dotSuffix = basename.mid( index );
00448 basename.truncate( index );
00449 }
00450
00451 int pos = basename.findRev( '_' );
00452 if(pos != -1 ){
00453 TQString tmp = basename.mid( pos+1 );
00454 bool ok;
00455 int number = tmp.toInt( &ok );
00456 if ( !ok ) {
00457 suggestedName = basename + "1" + dotSuffix;
00458 }
00459 else {
00460
00461 basename.replace( pos+1, tmp.length(), TQString::number(number+1) );
00462 suggestedName = basename + dotSuffix;
00463 }
00464 }
00465 else
00466 suggestedName = basename + "_1" + dotSuffix ;
00467
00468
00469 bool exists = false;
00470
00471
00472 if ( baseURL.isLocalFile() )
00473 exists = TQFileInfo( baseURL.path(+1) + suggestedName ).exists();
00474
00475 if ( !exists )
00476 return suggestedName;
00477 else
00478 return suggestName( baseURL, suggestedName );
00479 }
00480
00481
00482 void RenameDlg::b8Pressed()
00483 {
00484
00485 if ( d->m_pLineEdit->text().isEmpty() )
00486 return;
00487
00488 KURL destDirectory( d->dest );
00489 destDirectory.setPath( destDirectory.directory() );
00490 d->m_pLineEdit->setText( suggestName( destDirectory, d->m_pLineEdit->text() ) );
00491 return;
00492 }
00493
00494 void RenameDlg::b2Pressed()
00495 {
00496 done( 2 );
00497 }
00498
00499 void RenameDlg::b3Pressed()
00500 {
00501 done( 3 );
00502 }
00503
00504 void RenameDlg::b4Pressed()
00505 {
00506 done( 4 );
00507 }
00508
00509 void RenameDlg::b5Pressed()
00510 {
00511 done( 5 );
00512 }
00513
00514 void RenameDlg::b6Pressed()
00515 {
00516 done( 6 );
00517 }
00518
00519 void RenameDlg::b7Pressed()
00520 {
00521 done( 7 );
00522 }
00523
00524 static TQString mime( const KURL& src )
00525 {
00526 KMimeType::Ptr type = KMimeType::findByURL( src );
00527
00528
00529
00530 return type->name();
00531 }
00532
00539 void RenameDlg::pluginHandling()
00540 {
00541 d->mimeSrc = mime( d->src );
00542 d->mimeDest = mime(d->dest );
00543
00544 kdDebug(7024) << "Source Mimetype: "<< d->mimeSrc << endl;
00545 kdDebug(7024) << "Dest Mimetype: "<< d->mimeDest << endl;
00546 }
00547
00548
00549 RenameDlg_Result TDEIO::open_RenameDlg( const TQString & _caption,
00550 const TQString & _src, const TQString & _dest,
00551 RenameDlg_Mode _mode,
00552 TQString& _new,
00553 TDEIO::filesize_t sizeSrc,
00554 TDEIO::filesize_t sizeDest,
00555 time_t ctimeSrc,
00556 time_t ctimeDest,
00557 time_t mtimeSrc,
00558 time_t mtimeDest)
00559 {
00560 Q_ASSERT(kapp);
00561
00562 RenameDlg dlg( 0L, _caption, _src, _dest, _mode,
00563 sizeSrc, sizeDest, ctimeSrc, ctimeDest, mtimeSrc, mtimeDest,
00564 true );
00565 int i = dlg.exec();
00566 _new = dlg.newDestURL().path();
00567
00568 return (RenameDlg_Result)i;
00569 }
00570
00571 #include "renamedlg.moc"
00572
00573
00574
00575
00576