fileaccess.h 5.88 KB
Newer Older
Joachim Eibl's avatar
Joachim Eibl committed
1
/***************************************************************************
Joachim Eibl's avatar
Joachim Eibl committed
2
 *   Copyright (C) 2003-2007 by Joachim Eibl                               *
Joachim Eibl's avatar
Joachim Eibl committed
3
 *   joachim.eibl at gmx.de                                                   *
Joachim Eibl's avatar
Joachim Eibl committed
4 5 6 7 8 9 10 11 12 13
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 ***************************************************************************/

#ifndef FILEACCESS_H
#define FILEACCESS_H

14 15
#include "progress.h"

Joachim Eibl's avatar
Joachim Eibl committed
16 17
#include <QDateTime>

18
#include <KIO/UDSEntry>
Joachim Eibl's avatar
Joachim Eibl committed
19

20
namespace KIO {
Michael Reeves's avatar
Michael Reeves committed
21
    class Job;
22
}
Joachim Eibl's avatar
Joachim Eibl committed
23

Joachim Eibl's avatar
Joachim Eibl committed
24 25
bool wildcardMultiMatch( const QString& wildcard, const QString& testString, bool bCaseSensitive );

Joachim Eibl's avatar
Joachim Eibl committed
26
class t_DirectoryList;
27
class QFileInfo;
Joachim Eibl's avatar
Joachim Eibl committed
28

29 30 31
class ProgressProxyExtender: public ProgressProxy
{
  Q_OBJECT
32 33
public:
   ProgressProxyExtender() { setMaxNofSteps(100); }
34
public Q_SLOTS:
35 36 37 38
  void slotListDirInfoMessage( KJob*, const QString& msg );
  void slotPercent( KJob*, unsigned long percent );
};

Joachim Eibl's avatar
Joachim Eibl committed
39 40 41 42 43 44
class FileAccess
{
public:
   FileAccess();
   ~FileAccess();
   FileAccess( const QString& name, bool bWantToWrite=false ); // name: local file or dirname or url (when supported)
45 46
   FileAccess(const FileAccess& other);
   const FileAccess& operator=(const FileAccess& other);
Joachim Eibl's avatar
Joachim Eibl committed
47 48 49 50 51 52 53
   void setFile( const QString& name, bool bWantToWrite=false );

   bool isValid() const;
   bool isFile() const;
   bool isDir() const;
   bool isSymLink() const;
   bool exists() const;
54 55
   qint64 size() const;            // Size as returned by stat().
   qint64 sizeForReading();  // If the size can't be determined by stat() then the file is copied to a local temp file.
Joachim Eibl's avatar
Joachim Eibl committed
56 57 58 59 60 61
   bool isReadable() const;
   bool isWritable() const;
   bool isExecutable() const;
   bool isHidden() const;
   QString readLink() const;

62
   //QDateTime   created()       const;
Joachim Eibl's avatar
Joachim Eibl committed
63
   QDateTime   lastModified()  const;
64
   //QDateTime   lastRead()      const;
Joachim Eibl's avatar
Joachim Eibl committed
65 66 67 68

   QString fileName() const; // Just the name-part of the path, without parent directories
   QString filePath() const; // The path-string that was used during construction
   QString prettyAbsPath() const;
69
   QUrl url() const;
Joachim Eibl's avatar
Joachim Eibl committed
70
   QString absoluteFilePath() const;
Joachim Eibl's avatar
Joachim Eibl committed
71 72 73 74

   bool isLocal() const;

   bool readFile(void* pDestBuffer, unsigned long maxLength );
Joachim Eibl's avatar
Joachim Eibl committed
75
   bool writeFile(const void* pSrcBuffer, unsigned long length );
Joachim Eibl's avatar
Joachim Eibl committed
76 77 78 79 80 81 82
   bool listDir( t_DirectoryList* pDirList, bool bRecursive, bool bFindHidden,
                 const QString& filePattern, const QString& fileAntiPattern,
                 const QString& dirAntiPattern, bool bFollowDirLinks, bool bUseCvsIgnore );
   bool copyFile( const QString& destUrl );
   bool createBackup( const QString& bakExtension );

   static QString tempFileName();
Joachim Eibl's avatar
Joachim Eibl committed
83
   static bool removeTempFile( const QString& );
Joachim Eibl's avatar
Joachim Eibl committed
84 85 86 87 88
   bool removeFile();
   static bool removeFile( const QString& );
   static bool makeDir( const QString& );
   static bool removeDir( const QString& );
   static bool exists( const QString& );
Joachim Eibl's avatar
Joachim Eibl committed
89
   static QString cleanPath( const QString& );
Joachim Eibl's avatar
Joachim Eibl committed
90 91 92 93 94 95 96

   //bool chmod( const QString& );
   bool rename( const QString& );
   static bool symLink( const QString& linkTarget, const QString& linkLocation );

   void addPath( const QString& txt );
   QString getStatusText();
97 98 99

   FileAccess* parent() const; // !=0 for listDir-results, but only valid if the parent was not yet destroyed.
   void setSharedName( const QString& name ); // might reduce memory consumption
Joachim Eibl's avatar
Joachim Eibl committed
100 101
private:
   void setUdsEntry( const KIO::UDSEntry& e );
102
   void setFile( const QFileInfo& fi, FileAccess* pParent );
103
   void setStatusText( const QString& s );
104 105 106 107

   class Data;
   Data* d();
   const Data* d() const;
108
   void createData();
109 110 111 112 113 114 115

   union
   {
      Data* m_pData;
      FileAccess* m_pParent;
   };
   QString m_filePath; // might be absolute or relative if m_pParent!=0
116
   qint64 m_size;
Joachim Eibl's avatar
Joachim Eibl committed
117
   QDateTime m_modificationTime;
118 119 120 121 122 123 124
   bool m_bSymLink  : 1;
   bool m_bFile     : 1;
   bool m_bDir      : 1;
   bool m_bExists   : 1;
   bool m_bWritable : 1;
   bool m_bHidden   : 1;
   bool m_bUseData  : 1;
Joachim Eibl's avatar
Joachim Eibl committed
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139

   friend class FileAccessJobHandler;
};

class t_DirectoryList : public std::list<FileAccess>
{};


class FileAccessJobHandler : public QObject
{
   Q_OBJECT
public:
   FileAccessJobHandler( FileAccess* pFileAccess );

   bool get( void* pDestBuffer, long maxLength );
Joachim Eibl's avatar
Joachim Eibl committed
140
   bool put( const void* pSrcBuffer, long maxLength, bool bOverwrite, bool bResume=false, int permissions=-1 );
Joachim Eibl's avatar
Joachim Eibl committed
141 142 143 144 145 146 147 148
   bool stat(int detailLevel=2, bool bWantToWrite=false );
   bool copyFile( const QString& dest );
   bool rename( const QString& dest );
   bool listDir( t_DirectoryList* pDirList, bool bRecursive, bool bFindHidden,
                 const QString& filePattern, const QString& fileAntiPattern,
                 const QString& dirAntiPattern, bool bFollowDirLinks, bool bUseCvsIgnore );
   bool mkDir( const QString& dirName );
   bool rmDir( const QString& dirName );
149 150
   bool removeFile( const QUrl& dirName );
   bool symLink( const QUrl& linkTarget, const QUrl& linkLocation );
Joachim Eibl's avatar
Joachim Eibl committed
151 152 153 154 155 156

private:
   FileAccess* m_pFileAccess;
   bool m_bSuccess;

   // Data needed during Job
157
   qint64 m_transferredBytes;
Joachim Eibl's avatar
Joachim Eibl committed
158
   char* m_pTransferBuffer;  // Needed during get or put
159
   qint64 m_maxLength;
Joachim Eibl's avatar
Joachim Eibl committed
160 161 162 163 164 165 166 167 168 169 170

   QString m_filePattern;
   QString m_fileAntiPattern;
   QString m_dirAntiPattern;
   t_DirectoryList* m_pDirList;
   bool m_bFindHidden;
   bool m_bRecursive;
   bool m_bFollowDirLinks;

   bool scanLocalDirectory( const QString& dirName, t_DirectoryList* dirList );

171
private Q_SLOTS:
Joachim Eibl's avatar
Joachim Eibl committed
172 173 174
   void slotStatResult( KJob* );
   void slotSimpleJobResult( KJob* pJob );
   void slotPutJobResult( KJob* pJob );
Joachim Eibl's avatar
Joachim Eibl committed
175

Joachim Eibl's avatar
Joachim Eibl committed
176
   void slotGetData(KJob*,const QByteArray&);
Joachim Eibl's avatar
Joachim Eibl committed
177
   void slotPutData(KIO::Job*, QByteArray&);
Joachim Eibl's avatar
Joachim Eibl committed
178 179 180 181 182 183 184

   void slotListDirProcessNewEntries( KIO::Job *, const KIO::UDSEntryList& l );
};


#endif