22#include "tdeio/renamedlg.h"
23#include "tdeio/renamedlgplugin.h"
27#include <tqfileinfo.h>
30#include <tqlineedit.h>
33#include <tdemessagebox.h>
34#include <kpushbutton.h>
35#include <tdeapplication.h>
36#include <tdeio/global.h>
38#include <klibloader.h>
45#include <kseparator.h>
46#include <kstringhandler.h>
47#include <kstdguiitem.h>
49#include <ksqueezedtextlabel.h>
57class RenameDlg::RenameDlgPrivate
62 bRename = bSkip = bAutoSkip = bOverwrite = bOverwriteAll = 0;
63 bResume = bResumeAll = bSuggestNewName = 0;
67 TQPushButton *bRename;
69 TQPushButton *bAutoSkip;
70 TQPushButton *bOverwrite;
71 TQPushButton *bOverwriteAll;
72 TQPushButton *bResume;
73 TQPushButton *bResumeAll;
74 TQPushButton *bSuggestNewName;
75 TQLineEdit* m_pLineEdit;
84RenameDlg::RenameDlg(TQWidget *parent,
const TQString & _caption,
85 const TQString &_src,
const TQString &_dest,
94 : TQDialog ( parent,
"TDEIO::RenameDialog" , _modal )
96 d =
new RenameDlgPrivate( );
102#if !defined(TQ_WS_QWS) && !defined(TQ_WS_WIN)
104 KWin::setState( winId(), NET::StaysOnTop );
113 setCaption( _caption );
115 d->bCancel =
new KPushButton( KStdGuiItem::cancel(),
this );
116 connect(d->bCancel, TQ_SIGNAL(clicked()),
this, TQ_SLOT(
b0Pressed()));
118 if ( ! (_mode & M_NORENAME ) ) {
119 d->bRename =
new TQPushButton( i18n(
"&Rename" ),
this );
120 d->bRename->setEnabled(
false);
121 d->bSuggestNewName =
new TQPushButton( i18n(
"Suggest New &Name" ),
this );
122 connect(d->bSuggestNewName, TQ_SIGNAL(clicked()),
this, TQ_SLOT(b8Pressed()));
123 connect(d->bRename, TQ_SIGNAL(clicked()),
this, TQ_SLOT(b1Pressed()));
126 if ( ( _mode & M_MULTI ) && ( _mode & M_SKIP ) ) {
127 d->bSkip =
new TQPushButton( i18n(
"&Skip" ),
this );
128 connect(d->bSkip, TQ_SIGNAL(clicked()),
this, TQ_SLOT(b2Pressed()));
130 d->bAutoSkip =
new TQPushButton( i18n(
"&Auto Skip" ),
this );
131 connect(d->bAutoSkip, TQ_SIGNAL(clicked()),
this, TQ_SLOT(b3Pressed()));
134 if ( _mode & M_OVERWRITE ) {
135 d->bOverwrite =
new TQPushButton( i18n(
"&Overwrite" ),
this );
136 connect(d->bOverwrite, TQ_SIGNAL(clicked()),
this, TQ_SLOT(b4Pressed()));
138 if ( _mode & M_MULTI ) {
139 d->bOverwriteAll =
new TQPushButton( i18n(
"O&verwrite All" ),
this );
140 connect(d->bOverwriteAll, TQ_SIGNAL(clicked()),
this, TQ_SLOT(b5Pressed()));
144 if ( _mode & M_RESUME ) {
145 d->bResume =
new TQPushButton( i18n(
"&Resume" ),
this );
146 connect(d->bResume, TQ_SIGNAL(clicked()),
this, TQ_SLOT(b6Pressed()));
148 if ( _mode & M_MULTI )
150 d->bResumeAll =
new TQPushButton( i18n(
"R&esume All" ),
this );
151 connect(d->bResumeAll, TQ_SIGNAL(clicked()),
this, TQ_SLOT(b7Pressed()));
155 TQVBoxLayout* pLayout =
new TQVBoxLayout(
this, KDialog::marginHint(),
156 KDialog::spacingHint() );
157 pLayout->addStrut( 360 );
160 if ( _mode & M_OVERWRITE_ITSELF ) {
161 TQLabel *lb =
new TQLabel( i18n(
"This action would overwrite '%1' with itself.\n"
162 "Please enter a new file name:" ).arg( KStringHandler::csqueeze( d->src.pathOrURL(),100 ) ),
this );
163 d->bRename->setText(i18n(
"C&ontinue"));
164 pLayout->addWidget( lb );
166 else if ( _mode & M_OVERWRITE ) {
178 if(!plugin_offers.isEmpty() ){
179 kdDebug(7024) <<
"Offers" << endl;
180 TDETrader::OfferList::ConstIterator it = plugin_offers.begin();
181 TDETrader::OfferList::ConstIterator end = plugin_offers.end();
182 for( ; it != end; ++it ){
183 TQString libName = (*it)->library();
184 if( libName.isEmpty() ){
185 kdDebug(7024) <<
"lib is empty" << endl;
188 KLibrary *lib = KLibLoader::self()->library(libName.local8Bit() );
192 KLibFactory *factory = lib->factory();
197 TQObject *obj = factory->create(
this, (*it)->name().latin1() );
207 if( plugin->
initialize( _mode, _src, _dest, d->mimeSrc,
208 d->mimeDest, sizeSrc, sizeDest,
210 mtimeSrc, mtimeDest ) ) {
212 pLayout->addWidget(plugin );
213 kdDebug(7024) <<
"RenameDlgPlugin" << endl;
224 TQGridLayout * gridLayout =
new TQGridLayout( 0L, 9, 2, KDialog::marginHint(),
225 KDialog::spacingHint() );
226 pLayout->addLayout(gridLayout);
227 gridLayout->setColStretch(0,0);
228 gridLayout->setColStretch(1,10);
231 if (mtimeDest < mtimeSrc)
232 sentence1 = i18n(
"An older item named '%1' already exists.");
233 else if (mtimeDest == mtimeSrc)
234 sentence1 = i18n(
"A similar file named '%1' already exists.");
236 sentence1 = i18n(
"A newer item named '%1' already exists.");
238 TQLabel * lb1 =
new KSqueezedTextLabel( sentence1.arg(d->dest.pathOrURL() ),
this );
239 gridLayout->addMultiCellWidget( lb1, 0, 0, 0, 1 );
241 lb1 =
new TQLabel(
this );
243 gridLayout->addMultiCellWidget( lb1, 1, 3, 0, 0 );
248 TQLabel * lb =
new TQLabel( i18n(
"size %1").arg(
TDEIO::convertSize(sizeDest) ),
this );
249 gridLayout->addWidget( lb, row, 1 );
253 if ( ctimeDest != (time_t)-1 )
255 TQDateTime dctime; dctime.setTime_t( ctimeDest );
256 TQLabel * lb =
new TQLabel( i18n(
"created on %1").arg( TDEGlobal::locale()->formatDateTime(dctime) ),
this );
257 gridLayout->addWidget( lb, row, 1 );
260 if ( mtimeDest != (time_t)-1 )
262 TQDateTime dmtime; dmtime.setTime_t( mtimeDest );
263 TQLabel * lb =
new TQLabel( i18n(
"modified on %1").arg( TDEGlobal::locale()->formatDateTime(dmtime) ),
this );
264 gridLayout->addWidget( lb, row, 1 );
268 if ( !d->src.isEmpty() )
271 gridLayout->addRowSpacing( 4, 20 );
273 TQLabel * lb2 =
new KSqueezedTextLabel( i18n(
"The source file is '%1'").arg(d->src.pathOrURL()),
this );
274 gridLayout->addMultiCellWidget( lb2, 5, 5, 0, 1 );
276 lb2 =
new TQLabel(
this );
278 gridLayout->addMultiCellWidget( lb2, 6, 8, 0, 0 );
284 TQLabel * lb =
new TQLabel( i18n(
"size %1").arg(
TDEIO::convertSize(sizeSrc) ),
this );
285 gridLayout->addWidget( lb, row, 1 );
288 if ( ctimeSrc != (time_t)-1 )
290 TQDateTime dctime; dctime.setTime_t( ctimeSrc );
291 TQLabel * lb =
new TQLabel( i18n(
"created on %1").arg( TDEGlobal::locale()->formatDateTime(dctime) ),
this );
292 gridLayout->addWidget( lb, row, 1 );
295 if ( mtimeSrc != (time_t)-1 )
297 TQDateTime dmtime; dmtime.setTime_t( mtimeSrc );
298 TQLabel * lb =
new TQLabel( i18n(
"modified on %1").arg( TDEGlobal::locale()->formatDateTime(dmtime) ),
this );
299 gridLayout->addWidget( lb, row, 1 );
310 if (mtimeDest < mtimeSrc)
311 sentence1 = i18n(
"An older item named '%1' already exists.");
312 else if (mtimeDest == mtimeSrc)
313 sentence1 = i18n(
"A similar file named '%1' already exists.");
315 sentence1 = i18n(
"A newer item named '%1' already exists.");
317 TQLabel *lb =
new KSqueezedTextLabel( sentence1.arg(d->dest.pathOrURL()),
this );
318 pLayout->addWidget(lb);
320 TQHBoxLayout* layout2 =
new TQHBoxLayout();
321 pLayout->addLayout( layout2 );
323 d->m_pLineEdit =
new TQLineEdit(
this );
324 layout2->addWidget( d->m_pLineEdit );
325 TQString fileName = d->dest.fileName();
327 if ( d->bRename || d->bOverwrite )
328 connect(d->m_pLineEdit, TQ_SIGNAL(textChanged(
const TQString &)),
329 TQ_SLOT(enableRenameButton(
const TQString &)));
330 if ( d->bSuggestNewName )
332 layout2->addWidget( d->bSuggestNewName );
333 setTabOrder( d->m_pLineEdit, d->bSuggestNewName );
336 KSeparator* separator =
new KSeparator(
this );
337 pLayout->addWidget( separator );
339 TQHBoxLayout* layout =
new TQHBoxLayout();
340 pLayout->addLayout( layout );
342 layout->addStretch(1);
346 layout->addWidget( d->bRename );
347 setTabOrder( d->bRename, d->bCancel );
351 layout->addWidget( d->bSkip );
352 setTabOrder( d->bSkip, d->bCancel );
356 layout->addWidget( d->bAutoSkip );
357 setTabOrder( d->bAutoSkip, d->bCancel );
361 layout->addWidget( d->bOverwrite );
362 setTabOrder( d->bOverwrite, d->bCancel );
364 if ( d->bOverwriteAll )
366 layout->addWidget( d->bOverwriteAll );
367 setTabOrder( d->bOverwriteAll, d->bCancel );
371 layout->addWidget( d->bResume );
372 setTabOrder( d->bResume, d->bCancel );
376 layout->addWidget( d->bResumeAll );
377 setTabOrder( d->bResumeAll, d->bCancel );
380 d->bCancel->setDefault(
true );
381 layout->addWidget( d->bCancel );
383 resize( sizeHint() );
385 d->m_pLineEdit->setFocus();
388RenameDlg::~RenameDlg()
394void RenameDlg::enableRenameButton(
const TQString &newDest)
398 d->bRename->setEnabled(
true );
399 d->bRename->setDefault(
true );
401 d->bOverwrite->setEnabled(
false );
405 d->bRename->setEnabled(
false );
407 d->bOverwrite->setEnabled(
true );
413 KURL newDest( d->dest );
414 TQString fileName = d->m_pLineEdit->text();
425void RenameDlg::b1Pressed()
427 if ( d->m_pLineEdit->text().isEmpty() )
433 KMessageBox::error(
this, i18n(
"Malformed URL\n%1" ).arg( u.url() ) );
442 TQString dotSuffix, suggestedName;
443 TQString basename = oldName;
445 int index = basename.find(
'.' );
447 dotSuffix = basename.mid( index );
448 basename.truncate( index );
451 int pos = basename.findRev(
'_' );
453 TQString tmp = basename.mid( pos+1 );
455 int number = tmp.toInt( &ok );
457 suggestedName = basename +
"1" + dotSuffix;
461 basename.replace( pos+1, tmp.length(), TQString::number(
number+1) );
462 suggestedName = basename + dotSuffix;
466 suggestedName = basename +
"_1" + dotSuffix ;
472 if ( baseURL.isLocalFile() )
473 exists = TQFileInfo( baseURL.path(+1) + suggestedName ).exists();
476 return suggestedName;
482void RenameDlg::b8Pressed()
485 if ( d->m_pLineEdit->text().isEmpty() )
488 KURL destDirectory( d->dest );
489 destDirectory.setPath( destDirectory.directory() );
490 d->m_pLineEdit->setText(
suggestName( destDirectory, d->m_pLineEdit->text() ) );
494void RenameDlg::b2Pressed()
499void RenameDlg::b3Pressed()
504void RenameDlg::b4Pressed()
509void RenameDlg::b5Pressed()
514void RenameDlg::b6Pressed()
519void RenameDlg::b7Pressed()
524static TQString mime(
const KURL& src )
539void RenameDlg::pluginHandling()
541 d->mimeSrc = mime( d->src );
542 d->mimeDest = mime(d->dest );
544 kdDebug(7024) <<
"Source Mimetype: "<< d->mimeSrc << endl;
545 kdDebug(7024) <<
"Dest Mimetype: "<< d->mimeDest << endl;
550 const TQString & _src,
const TQString & _dest,
551 RenameDlg_Mode _mode,
562 RenameDlg dlg( 0L, _caption, _src, _dest, _mode,
563 sizeSrc, sizeDest, ctimeSrc, ctimeDest, mtimeSrc, mtimeDest,
566 _new = dlg.newDestURL().path();
571#include "renamedlg.moc"
static Ptr findByURL(const KURL &_url, mode_t _mode=0, bool _is_local_file=false, bool _fast_mode=false)
Finds a KMimeType with the given _url.
static const TQString & defaultMimeType()
Returns the name of the default mimetype.
static TQPixmap pixmapForURL(const KURL &_url, mode_t _mode=0, TDEIcon::Group _group=TDEIcon::Desktop, int _force_size=0, int _state=0, TQString *_path=0L)
Convenience method to find the pixmap for a URL.
This is the base class for all RenameDlg plugins.
virtual bool initialize(TDEIO::RenameDlg_Mode, const TQString &, const TQString &, const TQString &, const TQString &, TDEIO::filesize_t, TDEIO::filesize_t, time_t, time_t, time_t, time_t)
This function will be called by RenameDlg.
A dialog for the options to rename two files.
static TQString suggestName(const KURL &baseURL, const TQString &oldName)
Given a directory path and a filename (which usually exists already), this function returns a suggest...
void b0Pressed()
KDE4: rename to cancelPressed(), renamePressed() etc.
virtual OfferList query(const TQString &servicetype, const TQString &constraint=TQString::null, const TQString &preferences=TQString::null) const
The main function in the TDETrader class.
static TDETrader * self()
This is a static pointer to a TDETrader instance.
TQValueList< KService::Ptr > OfferList
A list of services.
A namespace for TDEIO globals.
TDEIO_EXPORT TQString number(TDEIO::filesize_t size)
Converts a size to a string representation Not unlike TQString::number(...)
TDEIO_EXPORT TQString convertSize(TDEIO::filesize_t size)
Converts size from bytes to the string representation.
TDEIO_EXPORT TQString encodeFileName(const TQString &str)
Encodes (from the text displayed to the real filename) This translates % into %% and / into ∕ (U+2215...
TDEIO_EXPORT TQString decodeFileName(const TQString &str)
Decodes (from the filename to the text displayed) This translates %2[fF] into /, %% into %,...
RenameDlg_Result
The result of open_RenameDlg().
TQ_ULLONG filesize_t
64-bit file size