~ubuntu-branches/ubuntu/precise/krusader/precise

« back to all changes in this revision

Viewing changes to krusader/VFS/normal_vfs.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Scott Kitterman
  • Date: 2010-05-05 22:26:37 UTC
  • mfrom: (3.1.4 squeeze)
  • Revision ID: james.westby@ubuntu.com-20100505222637-ydv3cwjwy365on2r
Tags: 1:2.1.0~beta1-1ubuntu1
* Merge from Debian Unstable.  Remaining changes:
  - Retain Kubuntu doc path

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/***************************************************************************
2
 
                       normal_vfs.cpp                             
 
2
                       normal_vfs.cpp
3
3
                   -------------------
4
4
    copyright            : (C) 2000 by Rafi Yanai
5
5
    e-mail               : krusader@users.sourceforge.net
8
8
 
9
9
 ***************************************************************************
10
10
 
11
 
  A 
 
11
  A
12
12
 
13
13
     db   dD d8888b. db    db .d8888.  .d8b.  d8888b. d88888b d8888b.
14
14
     88 ,8P' 88  `8D 88    88 88'  YP d8' `8b 88  `8D 88'     88  `8D
27
27
 *   (at your option) any later version.                                   *
28
28
 *                                                                         *
29
29
 ***************************************************************************/
 
30
 
 
31
#include "normal_vfs.h"
 
32
 
30
33
#include <string.h>
31
34
#include <sys/stat.h>
32
35
#include <sys/types.h>
33
36
#include <unistd.h>
34
37
#include <dirent.h>
35
 
// QT includes
36
 
#include <qtimer.h>
37
 
#include <QByteArray>
38
 
#include <QDir>
39
 
// KDE includes
40
 
#include <kmessagebox.h>
41
 
#include <kmimetype.h>
42
 
#include <kio/jobclasses.h>
43
 
#include <kio/deletejob.h>
44
 
#include <kio/jobuidelegate.h>
45
 
#include <klocale.h>
46
 
#include <kglobalsettings.h>
47
 
#include <kdebug.h>
 
38
 
 
39
#ifdef HAVE_POSIX_ACL
 
40
#include <sys/acl.h>
 
41
#ifdef HAVE_NON_POSIX_ACL_EXTENSIONS
 
42
#include <acl/libacl.h>
 
43
#endif
 
44
#endif
 
45
 
 
46
#include <QtCore/QTimer>
 
47
#include <QtCore/QByteArray>
 
48
#include <QtCore/QDir>
 
49
 
48
50
#include <kde_file.h>
49
 
#include <kfileitem.h>
50
 
// Krusader includes
51
 
#include "normal_vfs.h"
 
51
#include <KMessageBox>
 
52
#include <KMimeType>
 
53
#include <KIO/DeleteJob>
 
54
#include <KIO/JobUiDelegate>
 
55
#include <KLocale>
 
56
#include <KGlobalSettings>
 
57
#include <KDebug>
 
58
#include <KFileItem>
 
59
 
52
60
#include "../Dialogs/krdialogs.h"
53
61
#include "../MountMan/kmountman.h"
54
62
#include "krpermhandler.h"
58
66
#include "../krslots.h"
59
67
#include "../krservices.h"
60
68
 
61
 
// header files for ACL
62
 
#ifdef HAVE_POSIX_ACL
63
 
#include <sys/acl.h>
64
 
#ifdef HAVE_NON_POSIX_ACL_EXTENSIONS
65
 
#include <acl/libacl.h>
66
 
#endif
67
 
#endif
68
 
 
69
 
normal_vfs::normal_vfs(QObject* panel):vfs(panel), watcher(0) {
70
 
  vfs_type=VFS_NORMAL;
 
69
normal_vfs::normal_vfs(QObject* panel): vfs(panel), watcher(0)
 
70
{
 
71
    vfs_type = VFS_NORMAL;
71
72
}
72
73
 
73
 
bool normal_vfs::populateVfsList(const KUrl& origin, bool showHidden){
74
 
        QString path = KrServices::getPath( origin, KUrl::RemoveTrailingSlash);
 
74
bool normal_vfs::populateVfsList(const KUrl& origin, bool showHidden)
 
75
{
 
76
    QString path = KrServices::getPath(origin, KUrl::RemoveTrailingSlash);
75
77
#ifdef Q_WS_WIN
76
 
        if(! path.contains("/"))
77
 
        {   //change C: to C:/
78
 
                path = path + QString( "/" );
79
 
        }
 
78
    if (! path.contains("/")) {  //change C: to C:/
 
79
        path = path + QString("/");
 
80
    }
80
81
#endif
81
 
        
82
 
        // set the writable attribute to true, if that's not the case - the KIO job
83
 
        // will give the warnings and errors
84
 
        isWritable = true;
85
 
        
86
 
        if( watcher ) delete watcher; //stop watching the old dir
87
 
        watcher = 0;
88
 
 
89
 
        // set the origin...
90
 
        vfs_origin = origin;
91
 
        vfs_origin.setPath(path);
92
 
        //vfs_origin.adjustPath(KUrl::RemoveTrailingSlash);
93
 
        vfs_origin.setProtocol("file"); // do not remove !
94
 
        vfs_origin.cleanPath();
95
 
        
96
 
        // check that the new origin exists
97
 
        if ( !QDir(path).exists() )
98
 
        {
99
 
                if( !quietMode ) KMessageBox::error(krApp, i18n("Directory %1 does not exist!", path ), i18n("Error"));
100
 
                return false;
101
 
        }
102
 
    
103
 
        KConfigGroup group( krConfig, "Advanced");
104
 
        if (group.readEntry("AutoMount",_AutoMount)) krMtMan.autoMount(path);
105
 
        
106
 
        DIR* dir = opendir(path.toLocal8Bit());
107
 
        if(!dir) 
108
 
        {
109
 
                if( !quietMode ) KMessageBox::error(krApp, i18n("Can't open the %1 directory!", path ), i18n("Error"));
110
 
                return false;
111
 
        }
112
 
 
113
 
  // change directory to the new directory
114
 
        QString save = QDir::currentPath();
115
 
        if ( ! QDir::setCurrent( path ) ) {
116
 
                if( !quietMode ) KMessageBox::error(krApp, i18n("Access denied to")+path, i18n("Error"));
117
 
                closedir(dir);
118
 
                return false;
119
 
        }
120
 
 
121
 
        struct dirent* dirEnt;
122
 
  QString name;
123
 
 
124
 
        while( (dirEnt=readdir(dir)) != NULL ){
125
 
    name = QString::fromLocal8Bit(dirEnt->d_name);
126
 
 
127
 
                // show hidden files ?
128
 
                if ( !showHidden && name.left(1) == "." ) continue ;
129
 
                // we dont need the ".",".." enteries
130
 
                if (name=="." || name == "..") continue;
131
 
          
132
 
                vfile* temp = vfileFromName( name, dirEnt->d_name );
133
 
    foundVfile(temp);
134
 
  }
135
 
        // clean up
136
 
        closedir(dir);
137
 
        QDir::setCurrent( save );
138
 
        
139
 
        if( panelConnected )
140
 
        {
141
 
                watcher = new KDirWatch();
142
 
                // connect the watcher
143
 
                connect(watcher,SIGNAL(dirty(const QString&)),this,SLOT(vfs_slotDirty(const QString&)));
144
 
                connect(watcher,SIGNAL(created(const QString&)),this, SLOT(vfs_slotCreated(const QString&)));
145
 
                connect(watcher,SIGNAL(deleted(const QString&)),this, SLOT(vfs_slotDeleted(const QString&)));   
146
 
                watcher->addDir(vfs_getOrigin().path(KUrl::RemoveTrailingSlash), KDirWatch::WatchFiles); //start watching the new dir
147
 
                watcher->startScan(true);
148
 
        }
149
 
 
150
 
  return true;
151
 
}
152
 
 
153
 
// copy a file to the vfs (physical)    
154
 
void normal_vfs::vfs_addFiles(KUrl::List *fileUrls,KIO::CopyJob::CopyMode mode,QObject* toNotify,QString dir, PreserveMode pmode ){
155
 
  //if( watcher ) watcher->stopScan(); // we will refresh manually this time... 
156
 
  if( watcher ) {
157
 
    delete watcher;   // stopScan is buggy, leaves reference on the directory, that's why we delete the watcher
 
82
 
 
83
    // set the writable attribute to true, if that's not the case - the KIO job
 
84
    // will give the warnings and errors
 
85
    isWritable = true;
 
86
 
 
87
    if (watcher) delete watcher;  //stop watching the old dir
158
88
    watcher = 0;
159
 
  }
160
 
 
161
 
  KUrl dest;
162
 
  dest.setPath(vfs_workingDir()+"/"+dir);
163
 
 
164
 
  KIO::Job* job = PreservingCopyJob::createCopyJob( pmode, *fileUrls,dest,mode,false,true );
165
 
  connect(job,SIGNAL(result(KJob*)),this,SLOT(vfs_refresh(KJob *)) );
166
 
  if(mode == KIO::CopyJob::Move) // notify the other panel
167
 
    connect(job,SIGNAL(result(KJob*)),toNotify,SLOT(vfs_refresh(KJob*)) );
168
 
  else
169
 
    job->ui()->setAutoErrorHandlingEnabled( true );
 
89
 
 
90
    // set the origin...
 
91
    vfs_origin = origin;
 
92
    vfs_origin.setPath(path);
 
93
    //vfs_origin.adjustPath(KUrl::RemoveTrailingSlash);
 
94
    vfs_origin.setProtocol("file"); // do not remove !
 
95
    vfs_origin.cleanPath();
 
96
 
 
97
    // check that the new origin exists
 
98
    if (!QDir(path).exists()) {
 
99
        if (!quietMode) KMessageBox::error(krApp, i18n("Directory %1 does not exist!", path), i18n("Error"));
 
100
        return false;
 
101
    }
 
102
 
 
103
    KConfigGroup group(krConfig, "Advanced");
 
104
    if (group.readEntry("AutoMount", _AutoMount)) krMtMan.autoMount(path);
 
105
 
 
106
    DIR* dir = opendir(path.toLocal8Bit());
 
107
    if (!dir) {
 
108
        if (!quietMode) KMessageBox::error(krApp, i18n("Can't open the %1 directory!", path), i18n("Error"));
 
109
        return false;
 
110
    }
 
111
 
 
112
    // change directory to the new directory
 
113
    QString save = QDir::currentPath();
 
114
    if (! QDir::setCurrent(path)) {
 
115
        if (!quietMode) KMessageBox::error(krApp, i18n("Access denied to") + path, i18n("Error"));
 
116
        closedir(dir);
 
117
        return false;
 
118
    }
 
119
 
 
120
    KDE_struct_dirent* dirEnt;
 
121
    QString name;
 
122
 
 
123
    while ((dirEnt = KDE_readdir(dir)) != NULL) {
 
124
        name = QString::fromLocal8Bit(dirEnt->d_name);
 
125
 
 
126
        // show hidden files ?
 
127
        if (!showHidden && name.left(1) == ".") continue ;
 
128
        // we don't need the ".",".." entries
 
129
        if (name == "." || name == "..") continue;
 
130
 
 
131
        vfile* temp = vfileFromName(name, dirEnt->d_name);
 
132
        foundVfile(temp);
 
133
    }
 
134
    // clean up
 
135
    closedir(dir);
 
136
    QDir::setCurrent(save);
 
137
 
 
138
    if (panelConnected) {
 
139
        watcher = new KDirWatch();
 
140
        // connect the watcher
 
141
        connect(watcher, SIGNAL(dirty(const QString&)), this, SLOT(vfs_slotDirty(const QString&)));
 
142
        connect(watcher, SIGNAL(created(const QString&)), this, SLOT(vfs_slotCreated(const QString&)));
 
143
        connect(watcher, SIGNAL(deleted(const QString&)), this, SLOT(vfs_slotDeleted(const QString&)));
 
144
        watcher->addDir(vfs_getOrigin().path(KUrl::RemoveTrailingSlash), KDirWatch::WatchFiles); //start watching the new dir
 
145
        watcher->startScan(true);
 
146
    }
 
147
 
 
148
    return true;
 
149
}
 
150
 
 
151
// copy a file to the vfs (physical)
 
152
void normal_vfs::vfs_addFiles(KUrl::List *fileUrls, KIO::CopyJob::CopyMode mode, QObject* toNotify, QString dir, PreserveMode pmode)
 
153
{
 
154
    //if( watcher ) watcher->stopScan(); // we will refresh manually this time...
 
155
    if (watcher) {
 
156
        delete watcher;   // stopScan is buggy, leaves reference on the directory, that's why we delete the watcher
 
157
        watcher = 0;
 
158
    }
 
159
 
 
160
    KUrl dest;
 
161
    dest.setPath(vfs_workingDir() + '/' + dir);
 
162
 
 
163
    KIO::Job* job = PreservingCopyJob::createCopyJob(pmode, *fileUrls, dest, mode, false, true);
 
164
    connect(job, SIGNAL(result(KJob*)), this, SLOT(vfs_refresh(KJob *)));
 
165
    if (mode == KIO::CopyJob::Move) // notify the other panel
 
166
        connect(job, SIGNAL(result(KJob*)), toNotify, SLOT(vfs_refresh(KJob*)));
 
167
    else
 
168
        job->ui()->setAutoErrorHandlingEnabled(true);
170
169
}
171
170
 
172
171
// remove a file from the vfs (physical)
173
 
void normal_vfs::vfs_delFiles(QStringList *fileNames, bool reallyDelete){
174
 
        KUrl::List filesUrls;
175
 
        KUrl url;
176
 
        QDir local( vfs_workingDir() );
177
 
        vfile* vf;
178
 
 
179
 
//  if( watcher ) watcher->stopScan(); // we will refresh manually this time... 
180
 
        if( watcher ) {
181
 
                delete watcher;   // stopScan is buggy, leaves reference on the directory, that's why we delete the watcher
182
 
                watcher = 0;
183
 
        }
184
 
 
185
 
        // names -> urls
186
 
        for(int i=0 ; i<fileNames->count(); ++i){
187
 
                QString filename = (*fileNames)[i];
188
 
                vf = vfs_search(filename);
189
 
                url.setPath( vfs_workingDir()+"/"+filename);
190
 
                filesUrls.append(url);
191
 
        }
192
 
        KIO::Job *job;
193
 
        
194
 
        // delete of move to trash ?
195
 
        KConfigGroup group( krConfig, "General");
196
 
        if( !reallyDelete && group.readEntry("Move To Trash",_MoveToTrash) ){
197
 
          job = KIO::trash(filesUrls);
198
 
          connect(job,SIGNAL(result(KJob*)),SLOTS,SLOT(changeTrashIcon()));
199
 
        }
200
 
        else
201
 
          job = KIO::del(filesUrls);
202
 
        
203
 
        connect(job,SIGNAL(result(KJob*)),this,SLOT(vfs_refresh(KJob*)));
 
172
void normal_vfs::vfs_delFiles(QStringList *fileNames, bool reallyDelete)
 
173
{
 
174
    KUrl::List filesUrls;
 
175
    KUrl url;
 
176
    QDir local(vfs_workingDir());
 
177
    vfile* vf;
 
178
 
 
179
//  if( watcher ) watcher->stopScan(); // we will refresh manually this time...
 
180
    if (watcher) {
 
181
        delete watcher;   // stopScan is buggy, leaves reference on the directory, that's why we delete the watcher
 
182
        watcher = 0;
 
183
    }
 
184
 
 
185
    // names -> urls
 
186
    for (int i = 0 ; i < fileNames->count(); ++i) {
 
187
        QString filename = (*fileNames)[i];
 
188
        vf = vfs_search(filename);
 
189
        url.setPath(vfs_workingDir() + '/' + filename);
 
190
        filesUrls.append(url);
 
191
    }
 
192
    KIO::Job *job;
 
193
 
 
194
    // delete of move to trash ?
 
195
    KConfigGroup group(krConfig, "General");
 
196
    if (!reallyDelete && group.readEntry("Move To Trash", _MoveToTrash)) {
 
197
        job = KIO::trash(filesUrls);
 
198
        connect(job, SIGNAL(result(KJob*)), SLOTS, SLOT(changeTrashIcon()));
 
199
    } else
 
200
        job = KIO::del(filesUrls);
 
201
 
 
202
    connect(job, SIGNAL(result(KJob*)), this, SLOT(vfs_refresh(KJob*)));
204
203
}
205
204
 
206
205
// return a path to the file
207
 
KUrl normal_vfs::vfs_getFile(const QString& name){      
208
 
  QString url;
209
 
        if ( vfs_workingDir() == "/" ) url = "/"+name;
210
 
        else url = vfs_workingDir()+"/"+name;
211
 
 
212
 
        return KUrl(url);
213
 
}
214
 
 
215
 
KUrl::List* normal_vfs::vfs_getFiles(QStringList* names){
216
 
  KUrl::List* urls = new KUrl::List();
217
 
  for(QStringList::Iterator name = names->begin(); name != names->end(); ++name){
218
 
    urls->append( vfs_getFile(*name) );
219
 
  }
220
 
  return urls;
221
 
}
222
 
 
223
 
void normal_vfs::vfs_mkdir(const QString& name){
224
 
        if (!QDir(vfs_workingDir()).mkdir(name))
225
 
          if (!quietMode) KMessageBox::sorry(krApp,i18n("Can't create a directory. Check your permissions."));
226
 
  vfs::vfs_refresh();
227
 
}
228
 
 
229
 
void normal_vfs::vfs_rename(const QString& fileName,const QString& newName){
230
 
  KUrl::List fileUrls;
231
 
  KUrl url , dest;
232
 
 
233
 
  //if( watcher ) watcher->stopScan(); // we will refresh manually this time... 
234
 
  if( watcher ) {
235
 
    delete watcher;   // stopScan is buggy, leaves reference on the directory, that's why we delete the watcher
236
 
    watcher = 0;
237
 
  }
238
 
 
239
 
  url.setPath( vfs_workingDir()+"/"+fileName );
240
 
  fileUrls.append(url);
241
 
  dest.setPath(vfs_workingDir()+"/"+newName);
242
 
 
243
 
  KIO::Job *job = KIO::move(fileUrls,dest);
244
 
  connect(job,SIGNAL(result(KJob*)),this,SLOT(vfs_refresh(KJob*)));
245
 
}
246
 
 
247
 
vfile* normal_vfs::vfileFromName(const QString& name, char * rawName ){
248
 
        QString path = vfs_workingDir()+"/"+name;
249
 
        QByteArray fileName = rawName == 0 ? path.toLocal8Bit() : (vfs_workingDir()+"/").toLocal8Bit().append( rawName );
250
 
        
251
 
        KDE_struct_stat stat_p;
252
 
        stat_p.st_size = 0;
253
 
        stat_p.st_mode = 0;
254
 
        KDE_lstat(fileName.data(),&stat_p);
255
 
        KIO::filesize_t size = stat_p.st_size;
256
 
        QString perm = KRpermHandler::mode2QString(stat_p.st_mode);
257
 
        bool symLink= S_ISLNK(stat_p.st_mode);
258
 
        if( S_ISDIR(stat_p.st_mode) ) perm[0] = 'd';
259
 
        
260
 
        KUrl mimeUrl = KUrl(path);
261
 
        QString mime=QString();
262
 
 
263
 
        char symDest[256];
264
 
        memset(symDest,0,256); 
265
 
        if( S_ISLNK(stat_p.st_mode) ){  // who the link is pointing to ?
266
 
                int endOfName=0;
267
 
                endOfName=readlink(fileName.data(),symDest,256);
268
 
                if ( endOfName != -1 ){
269
 
                        if ( QDir(QString::fromLocal8Bit( symDest ) ).exists() ) perm[0] = 'd';
270
 
                        if ( !QDir(vfs_workingDir()).exists( QString::fromLocal8Bit ( symDest ) ) ) mime = "Broken Link !";
271
 
                }
272
 
                else krOut << "Failed to read link: "<< path<<endl;
273
 
        }
274
 
        
275
 
        int rwx = 0;
276
 
        if( ::access( fileName.data(), R_OK ) == 0 )
277
 
                rwx |= R_OK;
278
 
        if( ::access( fileName.data(), W_OK ) == 0 )
279
 
                rwx |= W_OK;
280
 
        if( ::access( fileName.data(), X_OK ) == 0 )
281
 
                rwx |= X_OK;
282
 
        
283
 
        // create a new virtual file object
284
 
        vfile* temp=new vfile(name,size,perm,stat_p.st_mtime,symLink,stat_p.st_uid,
285
 
                          stat_p.st_gid,mime,QString::fromLocal8Bit( symDest ),stat_p.st_mode, rwx);
286
 
        temp->vfile_setUrl( mimeUrl );
287
 
        return temp;
288
 
}
289
 
 
290
 
void normal_vfs::getACL( vfile *file, QString &acl, QString &defAcl )
291
 
{
292
 
        acl = defAcl = QString();
293
 
#if defined( HAVE_POSIX_ACL )
294
 
        QByteArray fileName = file->vfile_getUrl().path( KUrl::RemoveTrailingSlash ).toLocal8Bit();
295
 
#if HAVE_NON_POSIX_ACL_EXTENSIONS
296
 
        if ( acl_extended_file( fileName.data() ) )
297
 
        {
298
 
#endif
299
 
                acl = getACL( fileName.data(), ACL_TYPE_ACCESS );
300
 
                if( file->vfile_isDir() )
301
 
                        defAcl = getACL( fileName.data(), ACL_TYPE_DEFAULT );
302
 
#if HAVE_NON_POSIX_ACL_EXTENSIONS
303
 
        }
304
 
#endif
305
 
#endif
306
 
}
307
 
 
308
 
QString normal_vfs::getACL( const QString & path, int type )
309
 
{
310
 
#if defined( HAVE_POSIX_ACL )
311
 
        acl_t acl = 0;
312
 
        // do we have an acl for the file, and/or a default acl for the dir, if it is one?
313
 
        if ( ( acl = acl_get_file( path.data(), type ) ) != 0 )
314
 
        {
315
 
                bool aclExtended = false;
316
 
                
317
 
#if HAVE_NON_POSIX_ACL_EXTENSIONS
318
 
                aclExtended = acl_equiv_mode( acl, 0 );
319
 
#else
320
 
                acl_entry_t entry;
321
 
                int ret = acl_get_entry( acl, ACL_FIRST_ENTRY, &entry );
322
 
                while ( ret == 1 ) {
323
 
                        acl_tag_t currentTag;
324
 
                        acl_get_tag_type( entry, &currentTag );
325
 
                        if ( currentTag != ACL_USER_OBJ &&
326
 
                                currentTag != ACL_GROUP_OBJ &&
327
 
                                currentTag != ACL_OTHER )
328
 
                        {
329
 
                                aclExtended = true;
330
 
                                break;
331
 
                        }
332
 
                        ret = acl_get_entry( acl, ACL_NEXT_ENTRY, &entry );
333
 
                }
334
 
#endif
335
 
                
336
 
                if ( !aclExtended )
337
 
                {
338
 
                        acl_free( acl );
339
 
                        acl = 0;
340
 
                }
341
 
        }
342
 
        
343
 
        if( acl == 0 )
344
 
                return QString();
345
 
        
346
 
        char *aclString = acl_to_text( acl, 0 );
347
 
        QString ret = QString::fromLatin1( aclString );
348
 
        acl_free( (void*)aclString );
349
 
        acl_free( acl );
350
 
        
351
 
        return ret;
352
 
#else
353
 
        return QString();
 
206
KUrl normal_vfs::vfs_getFile(const QString& name)
 
207
{
 
208
    QString url;
 
209
    if (vfs_workingDir() == "/") url = "/" + name;
 
210
    else url = vfs_workingDir() + '/' + name;
 
211
 
 
212
    return KUrl(url);
 
213
}
 
214
 
 
215
KUrl::List* normal_vfs::vfs_getFiles(QStringList* names)
 
216
{
 
217
    KUrl::List* urls = new KUrl::List();
 
218
    for (QStringList::Iterator name = names->begin(); name != names->end(); ++name) {
 
219
        urls->append(vfs_getFile(*name));
 
220
    }
 
221
    return urls;
 
222
}
 
223
 
 
224
void normal_vfs::vfs_mkdir(const QString& name)
 
225
{
 
226
    if (!QDir(vfs_workingDir()).mkdir(name))
 
227
        if (!quietMode) KMessageBox::sorry(krApp, i18n("Can't create a directory. Check your permissions."));
 
228
    vfs::vfs_refresh();
 
229
}
 
230
 
 
231
void normal_vfs::vfs_rename(const QString& fileName, const QString& newName)
 
232
{
 
233
    KUrl::List fileUrls;
 
234
    KUrl url , dest;
 
235
 
 
236
    //if( watcher ) watcher->stopScan(); // we will refresh manually this time...
 
237
    if (watcher) {
 
238
        delete watcher;   // stopScan is buggy, leaves reference on the directory, that's why we delete the watcher
 
239
        watcher = 0;
 
240
    }
 
241
 
 
242
    url.setPath(vfs_workingDir() + '/' + fileName);
 
243
    fileUrls.append(url);
 
244
    dest.setPath(vfs_workingDir() + '/' + newName);
 
245
 
 
246
    KIO::Job *job = KIO::move(fileUrls, dest);
 
247
    connect(job, SIGNAL(result(KJob*)), this, SLOT(vfs_refresh(KJob*)));
 
248
}
 
249
 
 
250
vfile* normal_vfs::vfileFromName(const QString& name, char * rawName)
 
251
{
 
252
    QString path = vfs_workingDir() + '/' + name;
 
253
    QByteArray fileName = rawName == 0 ? path.toLocal8Bit() : (vfs_workingDir() + '/').toLocal8Bit().append(rawName);
 
254
 
 
255
    KDE_struct_stat stat_p;
 
256
    stat_p.st_size = 0;
 
257
    stat_p.st_mode = 0;
 
258
    KDE_lstat(fileName.data(), &stat_p);
 
259
    KIO::filesize_t size = stat_p.st_size;
 
260
    QString perm = KRpermHandler::mode2QString(stat_p.st_mode);
 
261
    bool symLink = S_ISLNK(stat_p.st_mode);
 
262
    if (S_ISDIR(stat_p.st_mode)) perm[0] = 'd';
 
263
 
 
264
    KUrl mimeUrl = KUrl(path);
 
265
    QString mime;
 
266
 
 
267
    char symDest[256];
 
268
    memset(symDest, 0, 256);
 
269
    if (S_ISLNK(stat_p.st_mode)) {  // who the link is pointing to ?
 
270
        int endOfName = 0;
 
271
        endOfName = readlink(fileName.data(), symDest, 256);
 
272
        if (endOfName != -1) {
 
273
            if (QDir(QString::fromLocal8Bit(symDest)).exists()) perm[0] = 'd';
 
274
            if (!QDir(vfs_workingDir()).exists(QString::fromLocal8Bit(symDest))) mime = "Broken Link !";
 
275
        } else krOut << "Failed to read link: " << path << endl;
 
276
    }
 
277
 
 
278
    int rwx = 0;
 
279
    if (::access(fileName.data(), R_OK) == 0)
 
280
        rwx |= R_OK;
 
281
    if (::access(fileName.data(), W_OK) == 0)
 
282
        rwx |= W_OK;
 
283
        
 
284
#ifndef Q_CC_MSVC
 
285
    if (::access(fileName.data(), X_OK) == 0)
 
286
        rwx |= X_OK;
 
287
#endif
 
288
 
 
289
    // create a new virtual file object
 
290
    vfile* temp = new vfile(name, size, perm, stat_p.st_mtime, symLink, stat_p.st_uid,
 
291
                            stat_p.st_gid, mime, QString::fromLocal8Bit(symDest), stat_p.st_mode, rwx);
 
292
    temp->vfile_setUrl(mimeUrl);
 
293
    return temp;
 
294
}
 
295
 
 
296
void normal_vfs::getACL(vfile *file, QString &acl, QString &defAcl)
 
297
{
 
298
    acl.clear();
 
299
    defAcl.clear();
 
300
#ifdef HAVE_POSIX_ACL
 
301
    QByteArray fileName = file->vfile_getUrl().path(KUrl::RemoveTrailingSlash).toLocal8Bit();
 
302
#ifdef HAVE_NON_POSIX_ACL_EXTENSIONS
 
303
    if (acl_extended_file(fileName.data())) {
 
304
#endif
 
305
        acl = getACL(fileName.data(), ACL_TYPE_ACCESS);
 
306
        if (file->vfile_isDir())
 
307
            defAcl = getACL(fileName.data(), ACL_TYPE_DEFAULT);
 
308
#ifdef HAVE_NON_POSIX_ACL_EXTENSIONS
 
309
    }
 
310
#endif
 
311
#endif
 
312
}
 
313
 
 
314
QString normal_vfs::getACL(const QString & path, int type)
 
315
{
 
316
#ifdef HAVE_POSIX_ACL
 
317
    acl_t acl = 0;
 
318
    // do we have an acl for the file, and/or a default acl for the dir, if it is one?
 
319
    if ((acl = acl_get_file(path.data(), type)) != 0) {
 
320
        bool aclExtended = false;
 
321
 
 
322
#ifdef HAVE_NON_POSIX_ACL_EXTENSIONS
 
323
        aclExtended = acl_equiv_mode(acl, 0);
 
324
#else
 
325
        acl_entry_t entry;
 
326
        int ret = acl_get_entry(acl, ACL_FIRST_ENTRY, &entry);
 
327
        while (ret == 1) {
 
328
            acl_tag_t currentTag;
 
329
            acl_get_tag_type(entry, &currentTag);
 
330
            if (currentTag != ACL_USER_OBJ &&
 
331
                    currentTag != ACL_GROUP_OBJ &&
 
332
                    currentTag != ACL_OTHER) {
 
333
                aclExtended = true;
 
334
                break;
 
335
            }
 
336
            ret = acl_get_entry(acl, ACL_NEXT_ENTRY, &entry);
 
337
        }
 
338
#endif
 
339
 
 
340
        if (!aclExtended) {
 
341
            acl_free(acl);
 
342
            acl = 0;
 
343
        }
 
344
    }
 
345
 
 
346
    if (acl == 0)
 
347
        return QString();
 
348
 
 
349
    char *aclString = acl_to_text(acl, 0);
 
350
    QString ret = QString::fromLatin1(aclString);
 
351
    acl_free((void*)aclString);
 
352
    acl_free(acl);
 
353
 
 
354
    return ret;
 
355
#else
 
356
    return QString();
354
357
#endif
355
358
}
356
359
 
357
360
void normal_vfs::vfs_slotRefresh()
358
361
{
359
 
        KConfigGroup group( krConfig, "Advanced");
360
 
        int maxRefreshFrequency = group.readEntry("Max Refresh Frequency", 1000);
361
 
        vfs_refresh();
362
 
        disconnect( &refreshTimer, SIGNAL( timeout() ), this, SLOT( vfs_slotRefresh() ) );
363
 
        refreshTimer.setSingleShot( true );
364
 
        refreshTimer.start( maxRefreshFrequency );
365
 
}
366
 
 
367
 
bool normal_vfs::burstRefresh(const QString& path ){
368
 
        if( path == vfs_getOrigin().path(KUrl::RemoveTrailingSlash) ) {
369
 
                if( !refreshTimer.isActive() ) {
370
 
                        // the directory itself is dirty - full refresh is needed
371
 
                        QTimer::singleShot(0, this, SLOT( vfs_slotRefresh() ) ); // safety: dirty signal comes from KDirWatch!
372
 
                        return true;
373
 
                }
374
 
                disconnect( &refreshTimer, SIGNAL( timeout() ), this, SLOT( vfs_slotRefresh() ) );
375
 
                connect( &refreshTimer, SIGNAL( timeout() ), this, SLOT( vfs_slotRefresh() ) );
376
 
                postponedRefreshURL = KUrl(path);
377
 
                return true;
378
 
        }
379
 
        return false;
380
 
}
381
 
 
382
 
void normal_vfs::vfs_slotDirty(const QString& path){ 
383
 
        if( disableRefresh ){
384
 
                if( postponedRefreshURL.isEmpty() )
385
 
                        postponedRefreshURL = vfs_getOrigin();
386
 
                return;
387
 
        }
388
 
        
389
 
        if( burstRefresh( path ) )
390
 
                return;        
391
 
        
392
 
        KUrl url = KUrl(path);
393
 
        QString name = url.fileName();
394
 
        
395
 
        if ( name.left(1) == "." && !vfs_showHidden() )
396
 
                return;
397
 
        
398
 
        // do we have it already ?
399
 
        if( !vfs_search(name ) ) return vfs_slotCreated(path);
400
 
        
401
 
        // we have an updated file..
402
 
        removeFromList(name);
403
 
        vfile* vf = vfileFromName(name, 0);
404
 
        addToList(vf);
405
 
        emit updatedVfile(vf);
406
 
}
407
 
 
408
 
void normal_vfs::vfs_slotCreated(const QString& path){  
409
 
        if( disableRefresh ){
410
 
                if( postponedRefreshURL.isEmpty() )
411
 
                        postponedRefreshURL = vfs_getOrigin();
412
 
                return;
413
 
        }       
414
 
        
415
 
        if( burstRefresh( path ) )
416
 
                return;        
417
 
        
418
 
        KUrl url = KUrl(path);
419
 
        QString name = url.fileName();  
420
 
        
421
 
        if ( name.left(1) == "." && !vfs_showHidden() )
422
 
                return;
423
 
        
424
 
        // if it's in the CVS - it's an update not new file
425
 
        if( vfs_search(name) )
426
 
                return vfs_slotDirty(path);
427
 
        
428
 
        vfile* vf = vfileFromName(name, 0);
429
 
        addToList(vf);
430
 
        emit addedVfile(vf);    
431
 
}
432
 
 
433
 
void normal_vfs::vfs_slotDeleted(const QString& path){ 
434
 
        if( disableRefresh ){
435
 
                if( postponedRefreshURL.isEmpty() )
436
 
                        postponedRefreshURL = vfs_getOrigin();
437
 
                return;
438
 
        }
439
 
        
440
 
        if( burstRefresh( path ) )
441
 
                return;        
442
 
        
443
 
        
444
 
        KUrl url = KUrl(path);
445
 
        QString name = url.fileName();
446
 
        
447
 
        // if it's not in the CVS - do nothing
448
 
        if( vfs_search(name) ){
449
 
                emit deletedVfile(name);
450
 
                removeFromList(name);   
451
 
        }       
 
362
    KConfigGroup group(krConfig, "Advanced");
 
363
    int maxRefreshFrequency = group.readEntry("Max Refresh Frequency", 1000);
 
364
    vfs_refresh();
 
365
    disconnect(&refreshTimer, SIGNAL(timeout()), this, SLOT(vfs_slotRefresh()));
 
366
    refreshTimer.setSingleShot(true);
 
367
    refreshTimer.start(maxRefreshFrequency);
 
368
}
 
369
 
 
370
bool normal_vfs::burstRefresh(const QString& path)
 
371
{
 
372
    QString parentPath = path;
 
373
    int ndx = path.lastIndexOf(DIR_SEPARATOR);
 
374
    if (ndx >= 0)
 
375
        parentPath = path.left(ndx == 0 ? 1 : ndx);
 
376
 
 
377
    if (path == vfs_getOrigin().path(KUrl::RemoveTrailingSlash) ||
 
378
            parentPath == vfs_getOrigin().path(KUrl::RemoveTrailingSlash)) {
 
379
        if (!refreshTimer.isActive()) {
 
380
            // the directory itself is dirty - full refresh is needed
 
381
            QTimer::singleShot(0, this, SLOT(vfs_slotRefresh()));    // safety: dirty signal comes from KDirWatch!
 
382
            return true;
 
383
        }
 
384
        disconnect(&refreshTimer, SIGNAL(timeout()), this, SLOT(vfs_slotRefresh()));
 
385
        connect(&refreshTimer, SIGNAL(timeout()), this, SLOT(vfs_slotRefresh()));
 
386
        postponedRefreshURL = KUrl(path);
 
387
        return true;
 
388
    }
 
389
    return false;
 
390
}
 
391
 
 
392
void normal_vfs::vfs_slotDirty(const QString& path)
 
393
{
 
394
    if (disableRefresh) {
 
395
        if (postponedRefreshURL.isEmpty())
 
396
            postponedRefreshURL = vfs_getOrigin();
 
397
        return;
 
398
    }
 
399
 
 
400
    if (burstRefresh(path))
 
401
        return;
 
402
 
 
403
    KUrl url = KUrl(path);
 
404
    QString name = url.fileName();
 
405
 
 
406
    if (name.left(1) == "." && !vfs_showHidden())
 
407
        return;
 
408
 
 
409
    // do we have it already ?
 
410
    vfile * vf = vfs_search(name);
 
411
    if (!vf) return vfs_slotCreated(path);
 
412
 
 
413
    // we have an updated file..
 
414
    vfile *newVf = vfileFromName(name, 0);
 
415
    *vf = *newVf;
 
416
    delete newVf;
 
417
    emit updatedVfile(vf);
 
418
}
 
419
 
 
420
void normal_vfs::vfs_slotCreated(const QString& path)
 
421
{
 
422
    if (disableRefresh) {
 
423
        if (postponedRefreshURL.isEmpty())
 
424
            postponedRefreshURL = vfs_getOrigin();
 
425
        return;
 
426
    }
 
427
 
 
428
    if (burstRefresh(path))
 
429
        return;
 
430
 
 
431
    KUrl url = KUrl(path);
 
432
    QString name = url.fileName();
 
433
 
 
434
    if (name.left(1) == "." && !vfs_showHidden())
 
435
        return;
 
436
 
 
437
    // if it's in the CVS - it's an update not new file
 
438
    if (vfs_search(name))
 
439
        return vfs_slotDirty(path);
 
440
 
 
441
    vfile* vf = vfileFromName(name, 0);
 
442
    addToList(vf);
 
443
    emit addedVfile(vf);
 
444
}
 
445
 
 
446
void normal_vfs::vfs_slotDeleted(const QString& path)
 
447
{
 
448
    if (disableRefresh) {
 
449
        if (postponedRefreshURL.isEmpty())
 
450
            postponedRefreshURL = vfs_getOrigin();
 
451
        return;
 
452
    }
 
453
 
 
454
    if (burstRefresh(path))
 
455
        return;
 
456
 
 
457
 
 
458
    KUrl url = KUrl(path);
 
459
    QString name = url.fileName();
 
460
 
 
461
    // if it's not in the CVS - do nothing
 
462
    vfile *vf = vfs_search(name);
 
463
    if (vf) {
 
464
        emit deletedVfile(name);
 
465
        removeFromList(name);
 
466
        delete vf;
 
467
    }
452
468
}
453
469
 
454
470
#include "normal_vfs.moc"