konq_operations.cc

00001 /*  This file is part of the KDE project
00002     Copyright (C) 2000  David Faure <faure@kde.org>
00003 
00004     This program is free software; you can redistribute it and/or modify
00005     it under the terms of the GNU General Public License as published by
00006     the Free Software Foundation; either version 2 of the License, or
00007     (at your option) any later version.
00008 
00009     This program is distributed in the hope that it will be useful,
00010     but WITHOUT ANY WARRANTY; without even the implied warranty of
00011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012     GNU General Public License for more details.
00013 
00014     You should have received a copy of the GNU General Public License
00015     along with this program; if not, write to the Free Software
00016     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00017 */
00018 
00019 #include <qclipboard.h>
00020 #include "konq_operations.h"
00021 
00022 #include <kautomount.h>
00023 #include <kinputdialog.h>
00024 #include <klocale.h>
00025 #include <kmessagebox.h>
00026 #include <knotifyclient.h>
00027 #include <krun.h>
00028 #include <kshell.h>
00029 #include <kshortcut.h>
00030 
00031 #include <kdirnotify_stub.h>
00032 
00033 #include <dcopclient.h>
00034 #include "konq_undo.h"
00035 #include "konq_defaults.h"
00036 #include "konqbookmarkmanager.h"
00037 
00038 // For doDrop
00039 #include <qdir.h>//first
00040 #include <assert.h>
00041 #include <kapplication.h>
00042 #include <kipc.h>
00043 #include <kdebug.h>
00044 #include <kfileitem.h>
00045 #include <kdesktopfile.h>
00046 #include <kurldrag.h>
00047 #include <kglobalsettings.h>
00048 #include <kimageio.h>
00049 #include <kio/job.h>
00050 #include <kio/jobclasses.h>
00051 #include <kio/paste.h>
00052 #include <kio/netaccess.h>
00053 #include <kio/renamedlg.h>
00054 #include <konq_drag.h>
00055 #include <konq_iconviewwidget.h>
00056 #include <kprotocolinfo.h>
00057 #include <kprocess.h>
00058 #include <kstringhandler.h>
00059 #include <qpopupmenu.h>
00060 #include <unistd.h>
00061 #include <X11/Xlib.h>
00062 
00063 KBookmarkManager * KonqBookmarkManager::s_bookmarkManager;
00064 
00065 KonqOperations::KonqOperations( QWidget *parent )
00066     : QObject( parent, "KonqOperations" ),
00067       m_method( UNKNOWN ), m_info(0L), m_pasteInfo(0L)
00068 {
00069 }
00070 
00071 KonqOperations::~KonqOperations()
00072 {
00073     delete m_info;
00074     delete m_pasteInfo;
00075 }
00076 
00077 void KonqOperations::editMimeType( const QString & mimeType )
00078 {
00079   QString keditfiletype = QString::fromLatin1("keditfiletype");
00080   KRun::runCommand( keditfiletype + " " + KProcess::quote(mimeType),
00081                     keditfiletype, keditfiletype /*unused*/);
00082 }
00083 
00084 void KonqOperations::del( QWidget * parent, int method, const KURL::List & selectedURLs )
00085 {
00086   kdDebug(1203) << "KonqOperations::del " << parent->className() << endl;
00087   if ( selectedURLs.isEmpty() )
00088   {
00089     kdWarning(1203) << "Empty URL list !" << endl;
00090     return;
00091   }
00092 
00093   KonqOperations * op = new KonqOperations( parent );
00094   int confirmation = DEFAULT_CONFIRMATION;
00095   op->_del( method, selectedURLs, confirmation );
00096 }
00097 
00098 void KonqOperations::emptyTrash()
00099 {
00100   KonqOperations *op = new KonqOperations( 0L );
00101   op->_del( EMPTYTRASH, KURL("trash:/"), SKIP_CONFIRMATION );
00102 }
00103 
00104 void KonqOperations::restoreTrashedItems( const KURL::List& urls )
00105 {
00106   KonqOperations *op = new KonqOperations( 0L );
00107   op->_restoreTrashedItems( urls );
00108 }
00109 
00110 void KonqOperations::mkdir( QWidget *parent, const KURL & url )
00111 {
00112     KIO::Job * job = KIO::mkdir( url );
00113     KonqOperations * op = new KonqOperations( parent );
00114     op->setOperation( job, MKDIR, KURL::List(), url );
00115     (void) new KonqCommandRecorder( KonqCommand::MKDIR, KURL(), url, job ); // no support yet, apparently
00116 }
00117 
00118 void KonqOperations::doPaste( QWidget * parent, const KURL & destURL )
00119 {
00120    doPaste(parent, destURL, QPoint());
00121 }
00122 
00123 void KonqOperations::doPaste( QWidget * parent, const KURL & destURL, const QPoint &pos )
00124 {
00125     // move or not move ?
00126     bool move = false;
00127     QMimeSource *data = QApplication::clipboard()->data();
00128     if ( data->provides( "application/x-kde-cutselection" ) ) {
00129       move = KonqDrag::decodeIsCutSelection( data );
00130       kdDebug(1203) << "move (from clipboard data) = " << move << endl;
00131     }
00132 
00133     KIO::Job *job = KIO::pasteClipboard( destURL, move );
00134     if ( job )
00135     {
00136         KonqOperations * op = new KonqOperations( parent );
00137         KIO::CopyJob * copyJob = static_cast<KIO::CopyJob *>(job);
00138         KIOPasteInfo * pi = new KIOPasteInfo;
00139         pi->mousePos = pos;
00140         op->setPasteInfo( pi );
00141         op->setOperation( job, move ? MOVE : COPY, copyJob->srcURLs(), copyJob->destURL() );
00142         (void) new KonqCommandRecorder( move ? KonqCommand::MOVE : KonqCommand::COPY, KURL::List(), destURL, job );
00143     }
00144 }
00145 
00146 void KonqOperations::copy( QWidget * parent, int method, const KURL::List & selectedURLs, const KURL& destUrl )
00147 {
00148   kdDebug(1203) << "KonqOperations::copy() " << parent->className() << endl;
00149   if ((method!=COPY) && (method!=MOVE) && (method!=LINK))
00150   {
00151     kdWarning(1203) << "Illegal copy method !" << endl;
00152     return;
00153   }
00154   if ( selectedURLs.isEmpty() )
00155   {
00156     kdWarning(1203) << "Empty URL list !" << endl;
00157     return;
00158   }
00159 
00160   KonqOperations * op = new KonqOperations( parent );
00161   KIO::Job* job(0);
00162   if (method==LINK)
00163      job= KIO::link( selectedURLs, destUrl);
00164   else if (method==MOVE)
00165      job= KIO::move( selectedURLs, destUrl);
00166   else
00167      job= KIO::copy( selectedURLs, destUrl);
00168 
00169   op->setOperation( job, method, selectedURLs, destUrl );
00170 
00171   if (method==COPY)
00172      (void) new KonqCommandRecorder( KonqCommand::COPY, selectedURLs, destUrl, job );
00173   else
00174      (void) new KonqCommandRecorder( method==MOVE?KonqCommand::MOVE:KonqCommand::LINK, selectedURLs, destUrl, job );
00175 }
00176 
00177 void KonqOperations::_del( int method, const KURL::List & _selectedURLs, int confirmation )
00178 {
00179     KURL::List selectedURLs;
00180     for (KURL::List::ConstIterator it = _selectedURLs.begin(); it != _selectedURLs.end(); ++it)
00181         if (KProtocolInfo::supportsDeleting(*it))
00182             selectedURLs.append(*it);
00183     if (selectedURLs.isEmpty()) {
00184         delete this;
00185         return;
00186     }
00187 
00188   m_method = method;
00189   if ( confirmation == SKIP_CONFIRMATION || askDeleteConfirmation( selectedURLs, confirmation ) )
00190   {
00191     //m_srcURLs = selectedURLs;
00192     KIO::Job *job;
00193     switch( method )
00194     {
00195       case TRASH:
00196       {
00197         job = KIO::trash( selectedURLs );
00198         (void) new KonqCommandRecorder( KonqCommand::TRASH, selectedURLs, "trash:/", job );
00199          break;
00200       }
00201       case EMPTYTRASH:
00202       {
00203         // Same as in ktrash --empty
00204         QByteArray packedArgs;
00205         QDataStream stream( packedArgs, IO_WriteOnly );
00206         stream << (int)1;
00207         job = KIO::special( "trash:/", packedArgs );
00208         KNotifyClient::event(0, "Trash: emptied");
00209         break;
00210       }
00211       case DEL:
00212         job = KIO::del( selectedURLs );
00213         break;
00214       case SHRED:
00215         job = KIO::del( selectedURLs, true );
00216         break;
00217       default:
00218         kdWarning() << "Unknown operation: " << method << endl;
00219         delete this;
00220         return;
00221     }
00222     connect( job, SIGNAL( result( KIO::Job * ) ),
00223              SLOT( slotResult( KIO::Job * ) ) );
00224   } else
00225     delete this;
00226 }
00227 
00228 void KonqOperations::_restoreTrashedItems( const KURL::List& urls )
00229 {
00230     m_method = RESTORE;
00231     KonqMultiRestoreJob* job = new KonqMultiRestoreJob( urls, true );
00232     connect( job, SIGNAL( result( KIO::Job * ) ),
00233              SLOT( slotResult( KIO::Job * ) ) );
00234 }
00235 
00236 bool KonqOperations::askDeleteConfirmation( const KURL::List & selectedURLs, int confirmation )
00237 {
00238     QString keyName;
00239     bool ask = ( confirmation == FORCE_CONFIRMATION );
00240     if ( !ask )
00241     {
00242         KConfig config("konquerorrc", true, false);
00243         config.setGroup( "Trash" );
00244         keyName = ( m_method == DEL ? "ConfirmDelete" : m_method == SHRED ? "ConfirmShred" : "ConfirmTrash" );
00245         bool defaultValue = ( m_method == DEL ? DEFAULT_CONFIRMDELETE : m_method == SHRED ? DEFAULT_CONFIRMSHRED : DEFAULT_CONFIRMTRASH );
00246         ask = config.readBoolEntry( keyName, defaultValue );
00247     }
00248     if ( ask )
00249     {
00250       KURL::List::ConstIterator it = selectedURLs.begin();
00251       QStringList prettyList;
00252       for ( ; it != selectedURLs.end(); ++it ) {
00253         if ( (*it).protocol() == "trash" ) {
00254           QString path = (*it).path();
00255           // HACK (#98983): remove "0-foo". Note that it works better than
00256       // displaying KFileItem::name(), for files under a subdir.
00257           prettyList.append( path.remove(QRegExp("^/[0-9]*-")) );
00258         } else
00259           prettyList.append( (*it).pathOrURL() );
00260       }
00261 
00262       int result;
00263       switch(m_method)
00264       {
00265       case DEL:
00266           result = KMessageBox::warningContinueCancelList( 0,
00267                 i18n( "Do you really want to delete this item?", "Do you really want to delete these %n items?", prettyList.count()),
00268                 prettyList,
00269         i18n( "Delete Files" ),
00270         KStdGuiItem::del(),
00271         keyName, KMessageBox::Dangerous);
00272      break;
00273 
00274       case SHRED:
00275           result = KMessageBox::warningContinueCancelList( 0,
00276                 i18n( "Do you really want to shred this item?", "Do you really want to shred these %n items?", prettyList.count()),
00277                 prettyList,
00278                 i18n( "Shred Files" ),
00279         KGuiItem( i18n( "Shred" ), "editshred" ),
00280         keyName, KMessageBox::Dangerous);
00281         break;
00282 
00283       case MOVE:
00284       default:
00285           result = KMessageBox::warningContinueCancelList( 0,
00286                 i18n( "Do you really want to move this item to the trash?", "Do you really want to move these %n items to the trash?", prettyList.count()),
00287                 prettyList,
00288         i18n( "Move to Trash" ),
00289         KGuiItem( i18n( "Verb", "&Trash" ), "edittrash"),
00290         keyName, KMessageBox::Dangerous);
00291       }
00292       if (!keyName.isEmpty())
00293       {
00294          // Check kmessagebox setting... erase & copy to konquerorrc.
00295          KConfig *config = kapp->config();
00296          KConfigGroupSaver saver(config, "Notification Messages");
00297          if (!config->readBoolEntry(keyName, true))
00298          {
00299             config->writeEntry(keyName, true);
00300             config->sync();
00301             KConfig konq_config("konquerorrc", false);
00302             konq_config.setGroup( "Trash" );
00303             konq_config.writeEntry( keyName, false );
00304          }
00305       }
00306       return (result == KMessageBox::Continue);
00307     }
00308     return true;
00309 }
00310 
00311 void KonqOperations::doDrop( const KFileItem * destItem, const KURL & dest, QDropEvent * ev, QWidget * parent )
00312 {
00313     kdDebug(1203) << "doDrop: dest : " << dest.url() << endl;
00314     KURL::List lst;
00315     QMap<QString, QString> metaData;
00316     if ( KURLDrag::decode( ev, lst, metaData ) ) // Are they urls ?
00317     {
00318         if( lst.count() == 0 )
00319         {
00320             kdWarning(1203) << "Oooops, no data ...." << endl;
00321             ev->accept(false);
00322             return;
00323         }
00324         kdDebug(1203) << "KonqOperations::doDrop metaData: " << metaData.count() << " entries." << endl;
00325         QMap<QString,QString>::ConstIterator mit;
00326         for( mit = metaData.begin(); mit != metaData.end(); ++mit )
00327         {
00328             kdDebug(1203) << "metaData: key=" << mit.key() << " value=" << mit.data() << endl;
00329         }
00330         // Check if we dropped something on itself
00331         KURL::List::Iterator it = lst.begin();
00332         for ( ; it != lst.end() ; it++ )
00333         {
00334             kdDebug(1203) << "URL : " << (*it).url() << endl;
00335             if ( dest.equals( *it, true /*ignore trailing slashes*/ ) )
00336             {
00337                 // The event source may be the view or an item (icon)
00338                 // Note: ev->source() can be 0L! (in case of kdesktop) (Simon)
00339                 if ( !ev->source() || ev->source() != parent && ev->source()->parent() != parent )
00340                     KMessageBox::sorry( parent, i18n("You cannot drop a folder on to itself") );
00341                 kdDebug(1203) << "Dropped on itself" << endl;
00342                 ev->accept(false);
00343                 return; // do nothing instead of displaying kfm's annoying error box
00344             }
00345         }
00346 
00347         // Check the state of the modifiers key at the time of the drop
00348         Window root;
00349         Window child;
00350         int root_x, root_y, win_x, win_y;
00351         uint keybstate;
00352         XQueryPointer( qt_xdisplay(), qt_xrootwin(), &root, &child,
00353                        &root_x, &root_y, &win_x, &win_y, &keybstate );
00354 
00355         QDropEvent::Action action = ev->action();
00356         // Check for the drop of a bookmark -> we want a Link action
00357         if ( ev->provides("application/x-xbel") )
00358         {
00359             keybstate |= ControlMask | ShiftMask;
00360             action = QDropEvent::Link;
00361             kdDebug(1203) << "KonqOperations::doDrop Bookmark -> emulating Link" << endl;
00362         }
00363 
00364         KonqOperations * op = new KonqOperations(parent);
00365         op->setDropInfo( new DropInfo( keybstate, lst, metaData, win_x, win_y, action ) );
00366 
00367         // Ok, now we need destItem.
00368         if ( destItem )
00369         {
00370             op->asyncDrop( destItem ); // we have it already
00371         }
00372         else
00373         {
00374             // we need to stat to get it.
00375             op->_statURL( dest, op, SLOT( asyncDrop( const KFileItem * ) ) );
00376         }
00377         // In both cases asyncDrop will delete op when done
00378 
00379         ev->acceptAction();
00380     }
00381     else
00382     {
00383         //kdDebug(1203) << "Pasting to " << dest.url() << endl;
00384         KonqOperations * op = new KonqOperations(parent);
00385         KIO::CopyJob* job = KIO::pasteMimeSource( ev, dest,
00386                                                   i18n( "File name for dropped contents:" ),
00387                                                   parent );
00388         if ( job ) // 0 if canceled by user
00389         {
00390             op->setOperation( job, COPY, KURL::List(), job->destURL() );
00391             (void) new KonqCommandRecorder( KonqCommand::COPY, KURL::List(), dest, job );
00392         }
00393         ev->acceptAction();
00394     }
00395 }
00396 
00397 void KonqOperations::asyncDrop( const KFileItem * destItem )
00398 {
00399     assert(m_info); // setDropInfo should have been called before asyncDrop
00400     m_destURL = destItem->url();
00401 
00402     //kdDebug(1203) << "KonqOperations::asyncDrop destItem->mode=" << destItem->mode() << " url=" << m_destURL << endl;
00403     // Check what the destination is
00404     if ( destItem->isDir() )
00405     {
00406         doFileCopy();
00407         return;
00408     }
00409     if ( !m_destURL.isLocalFile() )
00410     {
00411         // We dropped onto a remote URL that is not a directory!
00412         // (e.g. an HTTP link in the sidebar).
00413         // Can't do that, but we can't prevent it before stating the dest....
00414         kdWarning(1203) << "Cannot drop onto " << m_destURL << endl;
00415         delete this;
00416         return;
00417     }
00418     if ( destItem->mimetype() == "application/x-desktop")
00419     {
00420         // Local .desktop file. What type ?
00421         KDesktopFile desktopFile( m_destURL.path() );
00422         if ( desktopFile.hasApplicationType() )
00423         {
00424             QString error;
00425             QStringList stringList;
00426             KURL::List lst = m_info->lst;
00427             KURL::List::Iterator it = lst.begin();
00428             for ( ; it != lst.end() ; it++ )
00429             {
00430                 stringList.append((*it).url());
00431             }
00432             if ( KApplication::startServiceByDesktopPath( m_destURL.path(), stringList, &error ) > 0 )
00433                 KMessageBox::error( 0L, error );
00434         }
00435         else
00436         {
00437             // Device or Link -> adjust dest
00438             if ( desktopFile.hasDeviceType() && desktopFile.hasKey("MountPoint") ) {
00439                 QString point = desktopFile.readEntry( "MountPoint" );
00440                 m_destURL.setPath( point );
00441                 QString dev = desktopFile.readDevice();
00442                 QString mp = KIO::findDeviceMountPoint( dev );
00443                 // Is the device already mounted ?
00444                 if ( !mp.isNull() )
00445                     doFileCopy();
00446                 else
00447                 {
00448                     bool ro = desktopFile.readBoolEntry( "ReadOnly", false );
00449                     QString fstype = desktopFile.readEntry( "FSType" );
00450                     KAutoMount* am = new KAutoMount( ro, fstype, dev, point, m_destURL.path(), false );
00451                     connect( am, SIGNAL( finished() ), this, SLOT( doFileCopy() ) );
00452                 }
00453                 return;
00454             }
00455             else if ( desktopFile.hasLinkType() && desktopFile.hasKey("URL") ) {
00456                 m_destURL = desktopFile.readPathEntry("URL");
00457                 doFileCopy();
00458                 return;
00459             }
00460             // else, well: mimetype, service, servicetype or .directory. Can't really drop anything on those.
00461         }
00462     }
00463     else
00464     {
00465         // Should be a local executable
00466         // (If this fails, there is a bug in KFileItem::acceptsDrops)
00467         kdDebug(1203) << "KonqOperations::doDrop " << m_destURL.path() << "should be an executable" << endl;
00468         Q_ASSERT ( access( QFile::encodeName(m_destURL.path()), X_OK ) == 0 );
00469         KProcess proc;
00470         proc << m_destURL.path() ;
00471         // Launch executable for each of the files
00472         KURL::List lst = m_info->lst;
00473         KURL::List::Iterator it = lst.begin();
00474         for ( ; it != lst.end() ; it++ )
00475             proc << (*it).path(); // assume local files
00476         kdDebug(1203) << "starting " << m_destURL.path() << " with " << lst.count() << " arguments" << endl;
00477         proc.start( KProcess::DontCare );
00478     }
00479     delete this;
00480 }
00481 
00482 void KonqOperations::doFileCopy()
00483 {
00484     assert(m_info); // setDropInfo - and asyncDrop - should have been called before asyncDrop
00485     KURL::List lst = m_info->lst;
00486     QDropEvent::Action action = m_info->action;
00487     bool isDesktopFile = false;
00488     bool itemIsOnDesktop = false;
00489     bool allItemsAreFromTrash = true;
00490     KURL::List mlst; // list of items that can be moved
00491     for (KURL::List::ConstIterator it = lst.begin(); it != lst.end(); ++it)
00492     {
00493         bool local = (*it).isLocalFile();
00494         if ( KProtocolInfo::supportsDeleting( *it ) && (!local || QFileInfo((*it).directory()).isWritable() ))
00495             mlst.append(*it);
00496         if ( local && KDesktopFile::isDesktopFile((*it).path()))
00497             isDesktopFile = true;
00498         if ( local && (*it).path().startsWith(KGlobalSettings::desktopPath()))
00499             itemIsOnDesktop = true;
00500         if ( local || (*it).protocol() != "trash" )
00501             allItemsAreFromTrash = false;
00502     }
00503 
00504     bool linkOnly = false;
00505     if (isDesktopFile && !kapp->authorize("run_desktop_files") &&
00506         (m_destURL.path(1) == KGlobalSettings::desktopPath()) )
00507     {
00508        linkOnly = true;
00509     }
00510 
00511     if ( !mlst.isEmpty() && m_destURL.protocol() == "trash" )
00512     {
00513         if ( itemIsOnDesktop && !kapp->authorize("editable_desktop_icons") )
00514         {
00515             delete this;
00516             return;
00517         }
00518 
00519         m_method = TRASH;
00520         if ( askDeleteConfirmation( mlst, DEFAULT_CONFIRMATION ) )
00521             action = QDropEvent::Move;
00522         else
00523         {
00524             delete this;
00525             return;
00526         }
00527     }
00528     else if ( allItemsAreFromTrash || m_destURL.protocol() == "trash" ) {
00529         // No point in asking copy/move/link when using dnd from or to the trash.
00530         action = QDropEvent::Move;
00531     }
00532     else if ( (((m_info->keybstate & ControlMask) == 0) && ((m_info->keybstate & ShiftMask) == 0)) ||
00533               linkOnly )
00534     {
00535         // Neither control nor shift are pressed => show popup menu
00536         KonqIconViewWidget *iconView = dynamic_cast<KonqIconViewWidget*>(parent());
00537         bool bSetWallpaper = false;
00538         if ( iconView && iconView->maySetWallpaper() && lst.count() == 1 )
00539     {
00540             KURL url = lst.first();
00541             KMimeType::Ptr mime = KMimeType::findByURL( url );
00542             if ( ( !KImageIO::type(url.path()).isEmpty() ) ||
00543                  ( KImageIO::isSupported(mime->name(), KImageIO::Reading) ) ||
00544                  mime->is( "image/svg+xml" ) )
00545             {
00546                 bSetWallpaper = true;
00547             }
00548         }
00549 
00550         // Check what the source can do
00551         KURL url = lst.first(); // we'll assume it's the same for all URLs (hack)
00552         bool sReading = KProtocolInfo::supportsReading( url );
00553         bool sDeleting = KProtocolInfo::supportsDeleting( url );
00554         bool sMoving = KProtocolInfo::supportsMoving( url );
00555         // Check what the destination can do
00556         bool dWriting = KProtocolInfo::supportsWriting( m_destURL );
00557         if ( !dWriting )
00558         {
00559             delete this;
00560             return;
00561         }
00562 
00563         QPopupMenu popup;
00564         if (!mlst.isEmpty() && (sMoving || (sReading && sDeleting)) && !linkOnly )
00565             popup.insertItem(SmallIconSet("goto"), i18n( "&Move Here" ) + "\t" + KKey::modFlagLabel( KKey::SHIFT ), 2 );
00566         if ( sReading && !linkOnly)
00567             popup.insertItem(SmallIconSet("editcopy"), i18n( "&Copy Here" ) + "\t" + KKey::modFlagLabel( KKey::CTRL ), 1 );
00568         popup.insertItem(SmallIconSet("www"), i18n( "&Link Here" ) + "\t" + KKey::modFlagLabel( (KKey::ModFlag)( KKey::CTRL|KKey::SHIFT ) ), 3 );
00569         if (bSetWallpaper)
00570             popup.insertItem(SmallIconSet("background"), i18n( "Set as &Wallpaper" ), 4 );
00571         popup.insertSeparator();
00572         popup.insertItem(SmallIconSet("cancel"), i18n( "C&ancel" ) + "\t" + KKey( Qt::Key_Escape ).toString(), 5);
00573 
00574         int result = popup.exec( m_info->mousePos );
00575 
00576         switch (result) {
00577         case 1 : action = QDropEvent::Copy; break;
00578         case 2 : action = QDropEvent::Move; break;
00579         case 3 : action = QDropEvent::Link; break;
00580         case 4 :
00581         {
00582             kdDebug(1203) << "setWallpaper iconView=" << iconView << " url=" << lst.first().url() << endl;
00583             if (iconView && iconView->isDesktop() ) iconView->setWallpaper(lst.first());
00584             delete this;
00585             return;
00586         }
00587         case 5 :
00588         default : delete this; return;
00589         }
00590     }
00591 
00592     KIO::Job * job = 0;
00593     switch ( action ) {
00594     case QDropEvent::Move :
00595         job = KIO::move( lst, m_destURL );
00596         job->setMetaData( m_info->metaData );
00597         setOperation( job, m_method == TRASH ? TRASH : MOVE, lst, m_destURL );
00598         (void) new KonqCommandRecorder(
00599             m_method == TRASH ? KonqCommand::TRASH : KonqCommand::MOVE,
00600             lst, m_destURL, job );
00601         return; // we still have stuff to do -> don't delete ourselves
00602     case QDropEvent::Copy :
00603         job = KIO::copy( lst, m_destURL );
00604         job->setMetaData( m_info->metaData );
00605         setOperation( job, COPY, lst, m_destURL );
00606         (void) new KonqCommandRecorder( KonqCommand::COPY, lst, m_destURL, job );
00607         return;
00608     case QDropEvent::Link :
00609         kdDebug(1203) << "KonqOperations::asyncDrop lst.count=" << lst.count() << endl;
00610         job = KIO::link( lst, m_destURL );
00611         job->setMetaData( m_info->metaData );
00612         setOperation( job, LINK, lst, m_destURL );
00613         (void) new KonqCommandRecorder( KonqCommand::LINK, lst, m_destURL, job );
00614         return;
00615     default : kdError(1203) << "Unknown action " << (int)action << endl;
00616     }
00617     delete this;
00618 }
00619 
00620 void KonqOperations::rename( QWidget * parent, const KURL & oldurl, const KURL& newurl )
00621 {
00622     kdDebug(1203) << "KonqOperations::rename oldurl=" << oldurl << " newurl=" << newurl << endl;
00623     if ( oldurl == newurl )
00624         return;
00625 
00626     KURL::List lst;
00627     lst.append(oldurl);
00628     KIO::Job * job = KIO::moveAs( oldurl, newurl, !oldurl.isLocalFile() );
00629     KonqOperations * op = new KonqOperations( parent );
00630     op->setOperation( job, MOVE, lst, newurl );
00631     (void) new KonqCommandRecorder( KonqCommand::MOVE, lst, newurl, job );
00632     // if moving the desktop then update config file and emit
00633     if ( oldurl.isLocalFile() && oldurl.path(1) == KGlobalSettings::desktopPath() )
00634     {
00635         kdDebug(1203) << "That rename was the Desktop path, updating config files" << endl;
00636         KConfig *globalConfig = KGlobal::config();
00637         KConfigGroupSaver cgs( globalConfig, "Paths" );
00638         globalConfig->writePathEntry("Desktop" , newurl.path(), true, true );
00639         globalConfig->sync();
00640         KIPC::sendMessageAll(KIPC::SettingsChanged, KApplication::SETTINGS_PATHS);
00641     }
00642 }
00643 
00644 void KonqOperations::setOperation( KIO::Job * job, int method, const KURL::List & /*src*/, const KURL & dest )
00645 {
00646     m_method = method;
00647     //m_srcURLs = src;
00648     m_destURL = dest;
00649     if ( job )
00650     {
00651         connect( job, SIGNAL( result( KIO::Job * ) ),
00652                  SLOT( slotResult( KIO::Job * ) ) );
00653         KIO::CopyJob *copyJob = dynamic_cast<KIO::CopyJob*>(job);
00654         KonqIconViewWidget *iconView = dynamic_cast<KonqIconViewWidget*>(parent());
00655         if (copyJob && iconView)
00656         {
00657             connect(copyJob, SIGNAL(aboutToCreate(KIO::Job *,const QValueList<KIO::CopyInfo> &)),
00658                  this, SLOT(slotAboutToCreate(KIO::Job *,const QValueList<KIO::CopyInfo> &)));
00659             connect(this, SIGNAL(aboutToCreate(const QPoint &, const QValueList<KIO::CopyInfo> &)),
00660                  iconView, SLOT(slotAboutToCreate(const QPoint &, const QValueList<KIO::CopyInfo> &)));
00661         }
00662     }
00663     else // for link
00664         slotResult( 0L );
00665 }
00666 
00667 void KonqOperations::slotAboutToCreate(KIO::Job *, const QValueList<KIO::CopyInfo> &files)
00668 {
00669     emit aboutToCreate( m_info ? m_info->mousePos : m_pasteInfo ? m_pasteInfo->mousePos : QPoint(), files);
00670 }
00671 
00672 void KonqOperations::statURL( const KURL & url, const QObject *receiver, const char *member )
00673 {
00674     KonqOperations * op = new KonqOperations( 0L );
00675     op->_statURL( url, receiver, member );
00676     op->m_method = STAT;
00677 }
00678 
00679 void KonqOperations::_statURL( const KURL & url, const QObject *receiver, const char *member )
00680 {
00681     connect( this, SIGNAL( statFinished( const KFileItem * ) ), receiver, member );
00682     KIO::StatJob * job = KIO::stat( url /*, false?*/ );
00683     connect( job, SIGNAL( result( KIO::Job * ) ),
00684              SLOT( slotStatResult( KIO::Job * ) ) );
00685 }
00686 
00687 void KonqOperations::slotStatResult( KIO::Job * job )
00688 {
00689     if ( job->error())
00690         job->showErrorDialog( (QWidget*)parent() );
00691     else
00692     {
00693         KIO::StatJob * statJob = static_cast<KIO::StatJob*>(job);
00694         KFileItem * item = new KFileItem( statJob->statResult(), statJob->url() );
00695         emit statFinished( item );
00696         delete item;
00697     }
00698     // If we're only here for a stat, we're done. But not if we used _statURL internally
00699     if ( m_method == STAT )
00700         delete this;
00701 }
00702 
00703 void KonqOperations::slotResult( KIO::Job * job )
00704 {
00705     if (job && job->error())
00706         job->showErrorDialog( (QWidget*)parent() );
00707     if ( m_method == EMPTYTRASH ) {
00708         // Update konq windows opened on trash:/
00709         KDirNotify_stub allDirNotify("*", "KDirNotify*");
00710         allDirNotify.FilesAdded( "trash:/" ); // yeah, files were removed, but we don't know which ones...
00711     }
00712     delete this;
00713 }
00714 
00715 void KonqOperations::rename( QWidget * parent, const KURL & oldurl, const QString & name )
00716 {
00717     KURL newurl( oldurl );
00718     newurl.setPath( oldurl.directory(false, true) + name );
00719     kdDebug(1203) << "KonqOperations::rename("<<name<<") called. newurl=" << newurl << endl;
00720     rename( parent, oldurl, newurl );
00721 }
00722 
00723 void KonqOperations::newDir( QWidget * parent, const KURL & baseURL )
00724 {
00725     bool ok;
00726     QString name = i18n( "New Folder" );
00727     if ( baseURL.isLocalFile() && QFileInfo( baseURL.path(+1) + name ).exists() )
00728         name = KIO::RenameDlg::suggestName( baseURL, i18n( "New Folder" ) );
00729 
00730     name = KInputDialog::getText ( i18n( "New Folder" ),
00731         i18n( "Enter folder name:" ), name, &ok, parent );
00732     if ( ok && !name.isEmpty() )
00733     {
00734         KURL url;
00735         if ((name[0] == '/') || (name[0] == '~'))
00736         {
00737            url.setPath(KShell::tildeExpand(name));
00738         }
00739         else
00740         {
00741            name = KIO::encodeFileName( name );
00742            url = baseURL;
00743            url.addPath( name );
00744         }
00745         KonqOperations::mkdir( 0L, url );
00746     }
00747 }
00748 
00750 
00751 KonqMultiRestoreJob::KonqMultiRestoreJob( const KURL::List& urls, bool showProgressInfo )
00752     : KIO::Job( showProgressInfo ),
00753       m_urls( urls ), m_urlsIterator( m_urls.begin() ),
00754       m_progress( 0 )
00755 {
00756   QTimer::singleShot(0, this, SLOT(slotStart()));
00757 }
00758 
00759 void KonqMultiRestoreJob::slotStart()
00760 {
00761     // Well, it's not a total in bytes, so this would look weird
00762     //if ( m_urlsIterator == m_urls.begin() ) // first time: emit total
00763     //    emit totalSize( m_urls.count() );
00764 
00765     if ( m_urlsIterator != m_urls.end() )
00766     {
00767         const KURL& url = *m_urlsIterator;
00768 
00769         KURL new_url = url;
00770         if ( new_url.protocol()=="system"
00771           && new_url.path().startsWith("/trash") )
00772         {
00773             QString path = new_url.path();
00774         path.remove(0, 6);
00775         new_url.setProtocol("trash");
00776         new_url.setPath(path);
00777         }
00778 
00779         Q_ASSERT( new_url.protocol() == "trash" );
00780         QByteArray packedArgs;
00781         QDataStream stream( packedArgs, IO_WriteOnly );
00782         stream << (int)3 << new_url;
00783         KIO::Job* job = KIO::special( new_url, packedArgs );
00784         addSubjob( job );
00785     }
00786     else // done!
00787     {
00788         KDirNotify_stub allDirNotify("*", "KDirNotify*");
00789         allDirNotify.FilesRemoved( m_urls );
00790         emitResult();
00791     }
00792 }
00793 
00794 void KonqMultiRestoreJob::slotResult( KIO::Job *job )
00795 {
00796     if ( job->error() )
00797     {
00798         KIO::Job::slotResult( job ); // will set the error and emit result(this)
00799         return;
00800     }
00801     subjobs.remove( job );
00802     // Move on to next one
00803     ++m_urlsIterator;
00804     ++m_progress;
00805     //emit processedSize( this, m_progress );
00806     emitPercent( m_progress, m_urls.count() );
00807     slotStart();
00808 }
00809 
00810 #include "konq_operations.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys