• Skip to content
  • Skip to link menu
KDE 4.1 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

KIOSlave

file_win.cpp

Go to the documentation of this file.
00001 /*
00002    Copyright (C) 2000-2002 Stephan Kulow <coolo@kde.org>
00003    Copyright (C) 2000-2002 David Faure <faure@kde.org>
00004    Copyright (C) 2000-2002 Waldo Bastian <bastian@kde.org>
00005    Copyright (C) 2006 Allan Sandfeld Jensen <sandfeld@kde.org>
00006    Copyright (C) 2007 Thiago Macieira <thiago@kde.org>
00007    Copyright (C) 2007 Christian Ehrlicher <ch.ehrlicher@gmx.de>
00008 
00009    This library is free software; you can redistribute it and/or
00010    modify it under the terms of the GNU Library General Public
00011    License (LGPL) as published by the Free Software Foundation;
00012    either version 2 of the License, or (at your option) any later
00013    version.
00014 
00015    This library is distributed in the hope that it will be useful,
00016    but WITHOUT ANY WARRANTY; without even the implied warranty of
00017    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018    Library General Public License for more details.
00019 
00020    You should have received a copy of the GNU Library General Public License
00021    along with this library; see the file COPYING.LIB.  If not, write to
00022    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00023    Boston, MA 02110-1301, USA.
00024 */
00025 
00026 #define _WIN32_WINNT 0x0500
00027 
00028 #include "file.h"
00029 
00030 #include <windows.h>
00031 
00032 #include <QtCore/QDir>
00033 #include <QtCore/QDirIterator>
00034 #include <QtCore/QFileInfo>
00035 
00036 #include <config.h>
00037 
00038 #include <kconfiggroup.h>
00039 #include <kdebug.h>
00040 
00041 using namespace KIO;
00042 
00043 static DWORD CALLBACK CopyProgressRoutine(
00044     LARGE_INTEGER TotalFileSize,
00045     LARGE_INTEGER TotalBytesTransferred,
00046     LARGE_INTEGER StreamSize,
00047     LARGE_INTEGER StreamBytesTransferred,
00048     DWORD dwStreamNumber,
00049     DWORD dwCallbackReason,
00050     HANDLE hSourceFile,
00051     HANDLE hDestinationFile,
00052     LPVOID lpData
00053 ) {
00054     FileProtocol *f = reinterpret_cast<FileProtocol*>(lpData);
00055     f->processedSize( TotalBytesTransferred.QuadPart );
00056     return PROGRESS_CONTINUE;
00057 }
00058 
00059 void FileProtocol::copy( const KUrl &src, const KUrl &dest,
00060                          int _mode, JobFlags _flags )
00061 {
00062     kDebug(7101) << "copy(): " << src << " -> " << dest << ", mode=" << _mode;
00063 
00064     QFileInfo _src(src.toLocalFile());
00065     QFileInfo _dest(dest.toLocalFile());
00066     DWORD dwFlags = COPY_FILE_FAIL_IF_EXISTS;
00067 
00068     if( _src == _dest ) {
00069         error( KIO::ERR_IDENTICAL_FILES, _dest.filePath() );
00070         return;
00071     }
00072 
00073     if( !_src.exists() ) {
00074         error( KIO::ERR_DOES_NOT_EXIST, _src.filePath() );
00075         return;
00076     }
00077 
00078     if ( _src.isDir() ) {
00079         error( KIO::ERR_IS_DIRECTORY, _src.filePath() );
00080         return;
00081     }
00082 
00083     if( _dest.exists() ) {
00084         if( _dest.isDir() ) {
00085            error( KIO::ERR_DIR_ALREADY_EXIST, _dest.filePath() );
00086            return;
00087         }
00088 
00089         if (!(_flags & KIO::Overwrite))
00090         {
00091            error( KIO::ERR_FILE_ALREADY_EXIST, _dest.filePath() );
00092            return;
00093         }
00094 
00095         dwFlags = 0;
00096     }
00097 
00098     if ( CopyFileExW( ( LPCWSTR ) _src.filePath().utf16(),
00099                       ( LPCWSTR ) _dest.filePath().utf16(),
00100                       CopyProgressRoutine,
00101                       ( LPVOID ) this,
00102                       FALSE,
00103                       dwFlags) == 0 )
00104     {
00105         DWORD dwLastErr = GetLastError();
00106         if ( dwLastErr == ERROR_FILE_NOT_FOUND )
00107             error( KIO::ERR_DOES_NOT_EXIST, _src.filePath() );
00108         else if ( dwLastErr == ERROR_ACCESS_DENIED )
00109             error( KIO::ERR_ACCESS_DENIED, _dest.filePath() );
00110         else {
00111             error( KIO::ERR_CANNOT_RENAME, _src.filePath() );
00112             kDebug( 7101 ) <<  "Copying file "
00113                            << _src.filePath()
00114                            << " failed ("
00115                            << dwLastErr << ")";
00116         }
00117         return;
00118     }
00119 
00120     finished();
00121 }
00122 
00123 void FileProtocol::listDir( const KUrl& url )
00124 {
00125     kDebug(7101) << "========= LIST " << url.url() << " =========";
00126 
00127     if (!url.isLocalFile()) {
00128         KUrl redir(url);
00129         redir.setProtocol(config()->readEntry("DefaultRemoteProtocol", "smb"));
00130         redirection(redir);
00131         kDebug(7101) << "redirecting to " << redir.url();
00132         finished();
00133         return;
00134     }
00135 
00136     QDir dir( url.toLocalFile() );
00137 
00138     if ( !dir.exists() ) {
00139         kDebug(7101) << "========= ERR_DOES_NOT_EXIST  =========";
00140         error( KIO::ERR_DOES_NOT_EXIST, url.toLocalFile() );
00141         return;
00142     }
00143 
00144     if ( !dir.isReadable() ) {
00145         kDebug(7101) << "========= ERR_CANNOT_ENTER_DIRECTORY =========";
00146         error( KIO::ERR_CANNOT_ENTER_DIRECTORY, url.toLocalFile() );
00147         return;
00148     }
00149     QDirIterator it( dir );
00150     UDSEntry entry;
00151     while( it.hasNext() ) {
00152         it.next();
00153         QFileInfo fileInfo = it.fileInfo();
00154 
00155         entry.insert( KIO::UDSEntry::UDS_NAME, it.fileName() );
00156         if( fileInfo.isSymLink() ) {
00157             entry.insert( KIO::UDSEntry::UDS_LINK_DEST, fileInfo.symLinkTarget() );
00158 /* TODO - or not useful on windows?
00159             if ( details > 1 ) {
00160                 // It is a link pointing to nowhere
00161                 type = S_IFMT - 1;
00162                 access = S_IRWXU | S_IRWXG | S_IRWXO;
00163 
00164                 entry.insert( KIO::UDSEntry::UDS_FILE_TYPE, type );
00165                 entry.insert( KIO::UDSEntry::UDS_ACCESS, access );
00166                 entry.insert( KIO::UDSEntry::UDS_SIZE, 0LL );
00167                 goto notype;
00168 
00169             }
00170 */
00171         }
00172         int type = S_IFREG;
00173         int access = 0;
00174         if( fileInfo.isDir() )
00175             type = S_IFDIR;
00176         else if( fileInfo.isSymLink() )
00177             type = S_IFLNK;
00178         if( fileInfo.isReadable() )
00179             access |= S_IRUSR;
00180         if( fileInfo.isWritable() )
00181             access |= S_IWUSR;
00182         if( fileInfo.isExecutable() )
00183             access |= S_IXUSR;
00184 
00185         entry.insert( KIO::UDSEntry::UDS_FILE_TYPE, type );
00186         entry.insert( KIO::UDSEntry::UDS_ACCESS, access );
00187         entry.insert( KIO::UDSEntry::UDS_SIZE, fileInfo.size() );
00188 
00189         entry.insert( KIO::UDSEntry::UDS_MODIFICATION_TIME, fileInfo.lastModified().toTime_t() );
00190         entry.insert( KIO::UDSEntry::UDS_USER, fileInfo.owner() );
00191         entry.insert( KIO::UDSEntry::UDS_GROUP, fileInfo.group() );
00192         entry.insert( KIO::UDSEntry::UDS_ACCESS_TIME, fileInfo.lastRead().toTime_t() );
00193 
00194         listEntry( entry, false );
00195         entry.clear();
00196     }
00197 
00198     listEntry( entry, true ); // ready
00199 
00200     kDebug(7101) << "============= COMPLETED LIST ============";
00201 
00202     finished();
00203 }
00204 
00205 void FileProtocol::rename( const KUrl &src, const KUrl &dest,
00206                            KIO::JobFlags _flags )
00207 {
00208     kDebug(7101) << "rename(): " << src << " -> " << dest;
00209 
00210     QFileInfo _src(src.toLocalFile());
00211     QFileInfo _dest(dest.toLocalFile());
00212     DWORD dwFlags = 0;
00213 
00214     if( _src == _dest ) {
00215         error( KIO::ERR_IDENTICAL_FILES, _dest.filePath() );
00216         return;
00217     }
00218 
00219     if( !_src.exists() ) {
00220         error( KIO::ERR_DOES_NOT_EXIST, _src.filePath() );
00221         return;
00222     }
00223 
00224     if( _dest.exists() ) {
00225         if( _dest.isDir() ) {
00226            error( KIO::ERR_DIR_ALREADY_EXIST, _dest.filePath() );
00227            return;
00228         }
00229 
00230         if (!(_flags & KIO::Overwrite))
00231         {
00232            error( KIO::ERR_FILE_ALREADY_EXIST, _dest.filePath() );
00233            return;
00234         }
00235         
00236         dwFlags = MOVEFILE_REPLACE_EXISTING;
00237     }
00238 
00239     if ( MoveFileExW( ( LPCWSTR ) _src.filePath().utf16(),
00240                       ( LPCWSTR ) _dest.filePath().utf16(), dwFlags) == 0 )
00241     {
00242         DWORD dwLastErr = GetLastError();
00243         if ( dwLastErr == ERROR_FILE_NOT_FOUND )
00244             error( KIO::ERR_DOES_NOT_EXIST, _src.filePath() );
00245         else if ( dwLastErr == ERROR_ACCESS_DENIED )
00246             error( KIO::ERR_ACCESS_DENIED, _dest.filePath() );
00247         else {
00248             error( KIO::ERR_CANNOT_RENAME, _src.filePath() );
00249             kDebug( 7101 ) <<  "Renaming file "
00250                            << _src.filePath()
00251                            << " failed ("
00252                            << dwLastErr << ")";
00253         }
00254         return;
00255     }
00256 
00257     finished();
00258 }
00259 
00260 void FileProtocol::symlink( const QString &target, const KUrl &dest, KIO::JobFlags flags )
00261 {
00262     // no symlink on windows for now
00263     // vista provides a CreateSymbolicLink() function. for now use ::copy
00264     FileProtocol::copy( target, dest, 0, flags );
00265 }
00266 
00267 void FileProtocol::del( const KUrl& url, bool isfile )
00268 {
00269     QString _path( url.toLocalFile() );
00270     /*****
00271      * Delete files
00272      *****/
00273 
00274     if (isfile) {
00275         kDebug( 7101 ) << "Deleting file " << _path;
00276         
00277         if( DeleteFileW( ( LPCWSTR ) _path.utf16() ) == 0 ) {
00278             DWORD dwLastErr = GetLastError();
00279             if ( dwLastErr == ERROR_PATH_NOT_FOUND )
00280                 error( KIO::ERR_DOES_NOT_EXIST, _path );
00281             else if( dwLastErr == ERROR_ACCESS_DENIED )
00282                 error( KIO::ERR_ACCESS_DENIED, _path );
00283             else {
00284                 error( KIO::ERR_CANNOT_DELETE, _path );
00285                 kDebug( 7101 ) <<  "Deleting file "
00286                                << _path
00287                                << " failed ("
00288                                << dwLastErr << ")";
00289             }
00290         }
00291     } else {
00292         kDebug( 7101 ) << "Deleting directory " << url.url();
00293         if( RemoveDirectoryW( ( LPCWSTR ) _path.utf16() ) == 0 ) {
00294             DWORD dwLastErr = GetLastError();
00295             if ( dwLastErr == ERROR_FILE_NOT_FOUND )
00296                 error( KIO::ERR_DOES_NOT_EXIST, _path );
00297             else if( dwLastErr == ERROR_ACCESS_DENIED )
00298                 error( KIO::ERR_ACCESS_DENIED, _path );
00299             else {
00300                 error( KIO::ERR_CANNOT_DELETE, _path );
00301                 kDebug( 7101 ) <<  "Deleting directory "
00302                                << _path
00303                                << " failed ("
00304                                << dwLastErr << ")";
00305             }
00306         }
00307     }
00308     finished();
00309 }

KIOSlave

Skip menu "KIOSlave"
  • Main Page
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members
  • Related Pages

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • KIO
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • Kross
  • KUtils
  • Nepomuk
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal