fix tricky regression noticed by Vyacheslav Tokarev on Google Reader.
[kdelibs.git] / kio / kio / renamedialog.cpp
blobbcc36b2640566eb4213d44f02c573268b5f05c35
1 /* This file is part of the KDE libraries
2 Copyright (C) 2000 Stephan Kulow <coolo@kde.org>
3 1999 - 2008 David Faure <faure@kde.org>
4 2001, 2006 Holger Freyther <freyther@kde.org>
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version.
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public License
17 along with this library; see the file COPYING.LIB. If not, write to
18 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA.
22 #include "kio/renamedialog.h"
23 #include "kio/renamedialogplugin.h"
24 #include <stdio.h>
25 #include <assert.h>
27 #include <QtCore/QDate>
28 #include <QtCore/QFileInfo>
29 #include <QtGui/QLabel>
30 #include <QtGui/QLayout>
31 #include <QtCore/QDir>
33 #include <klineedit.h>
34 #include <kmessagebox.h>
35 #include <kpushbutton.h>
36 #include <kio/global.h>
37 #include <kmimetypetrader.h>
38 #include <kdialog.h>
39 #include <klocale.h>
40 #include <kglobal.h>
41 #include <kdebug.h>
42 #include <kurl.h>
43 #include <kmimetype.h>
44 #include <kseparator.h>
45 #include <kstringhandler.h>
46 #include <kstandardguiitem.h>
47 #include <kguiitem.h>
48 #include <ksqueezedtextlabel.h>
50 using namespace KIO;
52 /** @internal */
53 class RenameDialog::RenameDialogPrivate
55 public:
56 RenameDialogPrivate(){
57 bCancel = 0;
58 bRename = bSkip = bAutoSkip = bOverwrite = bOverwriteAll = 0;
59 bResume = bResumeAll = bSuggestNewName = 0;
60 m_pLineEdit = 0;
62 KPushButton *bCancel;
63 QPushButton *bRename;
64 QPushButton *bSkip;
65 QPushButton *bAutoSkip;
66 QPushButton *bOverwrite;
67 QPushButton *bOverwriteAll;
68 QPushButton *bResume;
69 QPushButton *bResumeAll;
70 QPushButton *bSuggestNewName;
71 KLineEdit* m_pLineEdit;
72 KUrl src;
73 KUrl dest;
74 QString mimeSrc;
75 QString mimeDest;
76 bool plugin;
79 RenameDialog::RenameDialog(QWidget *parent, const QString & _caption,
80 const KUrl &_src, const KUrl &_dest,
81 RenameDialog_Mode _mode,
82 KIO::filesize_t sizeSrc,
83 KIO::filesize_t sizeDest,
84 time_t ctimeSrc,
85 time_t ctimeDest,
86 time_t mtimeSrc,
87 time_t mtimeDest)
88 : QDialog ( parent ), d(new RenameDialogPrivate)
90 setObjectName( "KIO::RenameDialog" );
92 d->src = _src;
93 d->dest = _dest;
94 d->plugin = false;
96 setWindowTitle( _caption );
98 d->bCancel = new KPushButton( KStandardGuiItem::cancel(), this );
99 connect(d->bCancel, SIGNAL(clicked()), this, SLOT(cancelPressed()));
101 if ( ! (_mode & M_NORENAME ) ) {
102 d->bRename = new QPushButton( i18n( "&Rename" ), this );
103 d->bRename->setEnabled(false);
104 d->bSuggestNewName = new QPushButton( i18n( "Suggest New &Name" ), this );
105 connect(d->bSuggestNewName, SIGNAL(clicked()), this, SLOT(suggestNewNamePressed()));
106 connect(d->bRename, SIGNAL(clicked()), this, SLOT(renamePressed()));
109 if ( ( _mode & M_MULTI ) && ( _mode & M_SKIP ) ) {
110 d->bSkip = new QPushButton( i18n( "&Skip" ), this );
111 d->bSkip->setToolTip((_mode & M_ISDIR) ? i18n("Do not copy or move this folder, skip to the next item instead")
112 : i18n("Do not copy or move this file, skip to the next item instead"));
113 connect(d->bSkip, SIGNAL(clicked()), this, SLOT(skipPressed()));
115 d->bAutoSkip = new QPushButton( i18n( "&Auto Skip" ), this );
116 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.")
117 : 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."));
118 connect(d->bAutoSkip, SIGNAL(clicked()), this, SLOT(autoSkipPressed()));
121 if ( _mode & M_OVERWRITE ) {
122 const QString text = (_mode & M_ISDIR) ? i18nc("Write files into an existing folder", "&Write Into") : i18n("&Overwrite");
123 d->bOverwrite = new QPushButton(text, this);
124 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."));
125 connect(d->bOverwrite, SIGNAL(clicked()), this, SLOT(overwritePressed()));
127 if ( _mode & M_MULTI ) {
128 const QString textAll = (_mode & M_ISDIR) ? i18nc("Write files into any existing directory", "&Write Into All") : i18n("&Overwrite All");
129 d->bOverwriteAll = new QPushButton( textAll, this );
130 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."));
131 connect(d->bOverwriteAll, SIGNAL(clicked()), this, SLOT(overwriteAllPressed()));
135 if ( _mode & M_RESUME ) {
136 d->bResume = new QPushButton( i18n( "&Resume" ), this );
137 connect(d->bResume, SIGNAL(clicked()), this, SLOT(resumePressed()));
139 if ( _mode & M_MULTI )
141 d->bResumeAll = new QPushButton( i18n( "R&esume All" ), this );
142 connect(d->bResumeAll, SIGNAL(clicked()), this, SLOT(resumeAllPressed()));
146 QVBoxLayout* pLayout = new QVBoxLayout( this );
147 pLayout->setMargin( KDialog::marginHint() );
148 pLayout->setSpacing( KDialog::spacingHint() );
149 pLayout->addStrut( 360 ); // makes dlg at least that wide
151 // User tries to overwrite a file with itself ?
152 if ( _mode & M_OVERWRITE_ITSELF ) {
153 QLabel *lb = new QLabel( i18n( "This action would overwrite '%1' with itself.\n"
154 "Please enter a new file name:" , KStringHandler::csqueeze( d->src.pathOrUrl(),100 ) ), this );
155 d->bRename->setText(i18n("C&ontinue"));
156 pLayout->addWidget( lb );
158 else if ( _mode & M_OVERWRITE ) {
160 // Figure out the mimetype and load one plugin
161 // (This is the only mode that is handled by plugins)
162 pluginHandling();
163 KService::List plugin_offers;
164 if( d->mimeSrc != KMimeType::defaultMimeType() ){
165 plugin_offers = KMimeTypeTrader::self()->query(d->mimeSrc, "RenameDialog/Plugin");
167 }else if(d->mimeDest != KMimeType::defaultMimeType() ) {
168 plugin_offers = KMimeTypeTrader::self()->query(d->mimeDest, "RenameDialog/Plugin");
170 if(!plugin_offers.isEmpty() ){
171 RenameDialogPlugin::FileItem src( _src, d->mimeSrc, sizeSrc, ctimeSrc, mtimeSrc );
172 RenameDialogPlugin::FileItem dst( _dest,d->mimeDest, sizeDest, ctimeDest, mtimeDest );
173 foreach (const KService::Ptr &ptr, plugin_offers) {
174 RenameDialogPlugin *plugin = ptr->createInstance<RenameDialogPlugin>(this);
175 if( !plugin )
176 continue;
178 plugin->setObjectName( ptr->name() );
179 if( plugin->wantToHandle( _mode, src, dst ) ) {
180 d->plugin = true;
181 plugin->handle( _mode, src, dst );
182 pLayout->addWidget(plugin );
183 break;
184 } else {
185 delete plugin;
191 if( !d->plugin ){
192 // No plugin found, build default dialog
193 QGridLayout * gridLayout = new QGridLayout();
194 gridLayout->setMargin( KDialog::marginHint() );
195 gridLayout->setSpacing( KDialog::spacingHint() );
196 pLayout->addLayout(gridLayout);
197 gridLayout->setColumnStretch(0,0);
198 gridLayout->setColumnStretch(1,10);
200 QString sentence1;
201 if (mtimeDest < mtimeSrc)
202 sentence1 = i18n("An older item named '%1' already exists.", d->dest.pathOrUrl());
203 else if (mtimeDest == mtimeSrc)
204 sentence1 = i18n("A similar file named '%1' already exists.", d->dest.pathOrUrl());
205 else
206 sentence1 = i18n("A newer item named '%1' already exists.", d->dest.pathOrUrl());
208 QLabel * lb1 = new KSqueezedTextLabel( sentence1, this );
209 gridLayout->addWidget( lb1, 0, 0, 1, 2 ); // takes the complete first line
211 lb1 = new QLabel( this );
212 lb1->setPixmap( KIO::pixmapForUrl( d->dest ) );
213 gridLayout->addWidget( lb1, 1, 0, 3, 1 ); // takes the first column on rows 1-3
215 int row = 1;
216 if ( sizeDest != (KIO::filesize_t)-1 )
218 QLabel * lb = new QLabel( i18n("size %1", KIO::convertSize(sizeDest) ), this );
219 gridLayout->addWidget( lb, row, 1 );
220 row++;
223 if ( ctimeDest != (time_t)-1 )
225 QDateTime dctime; dctime.setTime_t( ctimeDest );
226 QLabel * lb = new QLabel( i18n("created on %1", KGlobal::locale()->formatDateTime(dctime) ), this );
227 gridLayout->addWidget( lb, row, 1 );
228 row++;
230 if ( mtimeDest != (time_t)-1 )
232 QDateTime dmtime; dmtime.setTime_t( mtimeDest );
233 QLabel * lb = new QLabel( i18n("modified on %1", KGlobal::locale()->formatDateTime(dmtime) ), this );
234 gridLayout->addWidget( lb, row, 1 );
235 row++;
238 if ( !d->src.isEmpty() )
240 // rows 1 to 3 are the details (size/ctime/mtime), row 4 is empty
242 QLabel * lb2 = new KSqueezedTextLabel( i18n("The source file is '%1'", d->src.pathOrUrl()), this );
243 gridLayout->addWidget( lb2, 5, 0, 1, 2 ); // takes the complete first line
245 lb2 = new QLabel( this );
246 lb2->setPixmap( KIO::pixmapForUrl( d->src ) );
247 gridLayout->addWidget( lb2, 6, 0, 3, 1 ); // takes the first column on rows 6-8
249 row = 6;
251 if ( sizeSrc != (KIO::filesize_t)-1 )
253 QLabel * lb = new QLabel( i18n("size %1", KIO::convertSize(sizeSrc) ), this );
254 gridLayout->addWidget( lb, row, 1 );
255 row++;
257 if ( ctimeSrc != (time_t)-1 )
259 QDateTime dctime; dctime.setTime_t( ctimeSrc );
260 QLabel * lb = new QLabel( i18n("created on %1", KGlobal::locale()->formatDateTime(dctime) ), this );
261 gridLayout->addWidget( lb, row, 1 );
262 row++;
264 if ( mtimeSrc != (time_t)-1 )
266 QDateTime dmtime; dmtime.setTime_t( mtimeSrc );
267 QLabel * lb = new QLabel( i18n("modified on %1", KGlobal::locale()->formatDateTime(dmtime) ), this );
268 gridLayout->addWidget( lb, row, 1 );
269 row++;
274 else
276 // This is the case where we don't want to allow overwriting, the existing
277 // file must be preserved (e.g. when renaming).
278 QString sentence1;
279 if (mtimeDest < mtimeSrc)
280 sentence1 = i18n("An older item named '%1' already exists.", d->dest.pathOrUrl());
281 else if (mtimeDest == mtimeSrc)
282 sentence1 = i18n("A similar file named '%1' already exists.", d->dest.pathOrUrl());
283 else
284 sentence1 = i18n("A newer item named '%1' already exists.", d->dest.pathOrUrl());
286 QLabel *lb = new KSqueezedTextLabel( sentence1, this );
287 pLayout->addWidget(lb);
289 QHBoxLayout* layout2 = new QHBoxLayout();
290 pLayout->addLayout( layout2 );
292 d->m_pLineEdit = new KLineEdit( this );
293 layout2->addWidget( d->m_pLineEdit );
294 if ( d->bRename ) {
295 const QString fileName = d->dest.fileName();
296 d->m_pLineEdit->setText( KIO::decodeFileName( fileName ) );
297 connect(d->m_pLineEdit, SIGNAL(textChanged(const QString &)),
298 SLOT(enableRenameButton(const QString &)));
299 d->m_pLineEdit->setFocus();
300 } else {
301 d->m_pLineEdit->hide();
303 if ( d->bSuggestNewName )
305 layout2->addWidget( d->bSuggestNewName );
306 setTabOrder( d->m_pLineEdit, d->bSuggestNewName );
309 KSeparator* separator = new KSeparator( this );
310 pLayout->addWidget( separator );
312 QHBoxLayout* layout = new QHBoxLayout();
313 pLayout->addLayout( layout );
315 layout->addStretch(1);
317 if ( d->bRename )
319 layout->addWidget( d->bRename );
320 setTabOrder( d->bRename, d->bCancel );
322 if ( d->bSkip )
324 layout->addWidget( d->bSkip );
325 setTabOrder( d->bSkip, d->bCancel );
327 if ( d->bAutoSkip )
329 layout->addWidget( d->bAutoSkip );
330 setTabOrder( d->bAutoSkip, d->bCancel );
332 if ( d->bOverwrite )
334 layout->addWidget( d->bOverwrite );
335 setTabOrder( d->bOverwrite, d->bCancel );
337 if ( d->bOverwriteAll )
339 layout->addWidget( d->bOverwriteAll );
340 setTabOrder( d->bOverwriteAll, d->bCancel );
342 if ( d->bResume )
344 layout->addWidget( d->bResume );
345 setTabOrder( d->bResume, d->bCancel );
347 if ( d->bResumeAll )
349 layout->addWidget( d->bResumeAll );
350 setTabOrder( d->bResumeAll, d->bCancel );
353 d->bCancel->setDefault( true );
354 layout->addWidget( d->bCancel );
356 resize( sizeHint() );
359 RenameDialog::~RenameDialog()
361 delete d;
362 // no need to delete Pushbuttons,... qt will do this
365 void RenameDialog::enableRenameButton(const QString &newDest)
367 if ( newDest != KIO::decodeFileName( d->dest.fileName() ) && !newDest.isEmpty() )
369 d->bRename->setEnabled( true );
370 d->bRename->setDefault( true );
371 if ( d->bOverwrite )
372 d->bOverwrite->setEnabled( false ); // prevent confusion (#83114)
374 else
376 d->bRename->setEnabled( false );
377 if ( d->bOverwrite )
378 d->bOverwrite->setEnabled( true );
382 KUrl RenameDialog::newDestUrl()
384 KUrl newDest( d->dest );
385 QString fileName = d->m_pLineEdit->text();
386 newDest.setFileName( KIO::encodeFileName( fileName ) );
387 return newDest;
390 void RenameDialog::cancelPressed()
392 done( R_CANCEL );
395 // Rename
396 void RenameDialog::renamePressed()
398 if ( d->m_pLineEdit->text().isEmpty() )
399 return;
401 KUrl u = newDestUrl();
402 if ( !u.isValid() )
404 KMessageBox::error( this, i18n( "Malformed URL\n%1" , u.url() ) );
405 return;
408 done( R_RENAME );
411 QString RenameDialog::suggestName(const KUrl& baseURL, const QString& oldName)
413 QString dotSuffix, suggestedName;
414 QString basename = oldName;
416 int index = basename.indexOf( '.' );
417 if ( index != -1 ) {
418 dotSuffix = basename.mid( index );
419 basename.truncate( index );
422 int pos = basename.lastIndexOf( '_' );
423 if(pos != -1 ){
424 QString tmp = basename.mid( pos+1 );
425 bool ok;
426 int number = tmp.toInt( &ok );
427 if ( !ok ) {// ok there is no number
428 suggestedName = basename + '1' + dotSuffix;
430 else {
431 // yes there's already a number behind the _ so increment it by one
432 basename.replace( pos+1, tmp.length(), QString::number(number+1) );
433 suggestedName = basename + dotSuffix;
436 else // no underscore yet
437 suggestedName = basename + "_1" + dotSuffix ;
439 // Check if suggested name already exists
440 bool exists = false;
441 // TODO: network transparency. However, using NetAccess from a modal dialog
442 // could be a problem, no? (given that it uses a modal widget itself....)
443 if ( baseURL.isLocalFile() )
444 exists = QFileInfo( baseURL.toLocalFile(KUrl::AddTrailingSlash) + suggestedName ).exists();
446 if ( !exists )
447 return suggestedName;
448 else // already exists -> recurse
449 return suggestName( baseURL, suggestedName );
452 // Propose button clicked
453 void RenameDialog::suggestNewNamePressed()
455 /* no name to play with */
456 if ( d->m_pLineEdit->text().isEmpty() )
457 return;
459 KUrl destDirectory( d->dest );
460 destDirectory.setPath( destDirectory.directory() );
461 d->m_pLineEdit->setText( suggestName( destDirectory, d->m_pLineEdit->text() ) );
462 return;
465 void RenameDialog::skipPressed()
467 done( R_SKIP );
470 void RenameDialog::autoSkipPressed()
472 done( R_AUTO_SKIP );
475 void RenameDialog::overwritePressed()
477 done( R_OVERWRITE );
480 void RenameDialog::overwriteAllPressed()
482 done( R_OVERWRITE_ALL );
485 void RenameDialog::resumePressed()
487 done( R_RESUME );
490 void RenameDialog::resumeAllPressed()
492 done( R_RESUME_ALL );
495 static QString mime( const KUrl& src )
497 KMimeType::Ptr type = KMimeType::findByUrl( src );
498 //if( type->name() == KMimeType::defaultMimeType() ){ // ok no mimetype
499 // QString ty = KIO::NetAccess::mimetype(d->src );
500 // return ty;
501 return type->name();
504 /** This will figure out the mimetypes and query for a plugin
505 * Loads it then and asks the plugin if it wants to do the job
506 * We'll take the first available mimetype
507 * The scanning for a mimetype will be done in 2 ways
510 void RenameDialog::pluginHandling()
512 d->mimeSrc = mime( d->src );
513 d->mimeDest = mime(d->dest );
515 kDebug(7024) << "Source Mimetype: "<< d->mimeSrc;
516 kDebug(7024) << "Dest Mimetype: "<< d->mimeDest;
519 #include "renamedialog.moc"