fileaccess.h 5.91 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"

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

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

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

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

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

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

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
   FileAccess(const FileAccess& other);
Michael Reeves's avatar
Michael Reeves committed
46
   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

   bool isLocal() const;

74 75
   bool readFile(void* pDestBuffer, qint64 maxLength );
   bool writeFile(const void* pSrcBuffer, qint64 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

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

104 105 106
   class FileAccessPrivateData;
   FileAccessPrivateData* d();
   const FileAccessPrivateData* d() const;
107
   void createData();
108

109
   FileAccessPrivateData* m_pData = nullptr;
110 111

   QFileInfo m_fileInfo;
112
   QString m_filePath; // might be absolute or relative if m_pParent!=0
113
   qint64 m_size;
Joachim Eibl's avatar
Joachim Eibl committed
114
   QDateTime m_modificationTime;
115 116 117 118 119
   bool m_bSymLink  : 1;
   bool m_bFile     : 1;
   bool m_bDir      : 1;
   bool m_bExists   : 1;
   bool m_bWritable : 1;
120 121
   bool m_bReadable : 1;
   bool m_bExecutable:1;
122
   bool m_bHidden   : 1;
Joachim Eibl's avatar
Joachim Eibl committed
123 124 125 126 127 128 129 130 131 132 133 134

   friend class FileAccessJobHandler;
};

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


class FileAccessJobHandler : public QObject
{
   Q_OBJECT
public:
Michael Reeves's avatar
Michael Reeves committed
135
   explicit FileAccessJobHandler( FileAccess* pFileAccess );
Joachim Eibl's avatar
Joachim Eibl committed
136 137

   bool get( void* pDestBuffer, long maxLength );
Joachim Eibl's avatar
Joachim Eibl committed
138
   bool put( const void* pSrcBuffer, long maxLength, bool bOverwrite, bool bResume=false, int permissions=-1 );
Joachim Eibl's avatar
Joachim Eibl committed
139 140 141 142 143 144 145 146
   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 );
147 148
   bool removeFile( const QUrl& dirName );
   bool symLink( const QUrl& linkTarget, const QUrl& linkLocation );
Joachim Eibl's avatar
Joachim Eibl committed
149 150 151 152 153 154

private:
   FileAccess* m_pFileAccess;
   bool m_bSuccess;

   // Data needed during Job
155
   qint64 m_transferredBytes;
Michael Reeves's avatar
Michael Reeves committed
156
   char* m_pTransferBuffer = nullptr;  // Needed during get or put
157
   qint64 m_maxLength;
Joachim Eibl's avatar
Joachim Eibl committed
158 159 160 161

   QString m_filePattern;
   QString m_fileAntiPattern;
   QString m_dirAntiPattern;
Michael Reeves's avatar
Michael Reeves committed
162
   t_DirectoryList* m_pDirList = nullptr;
Michael Reeves's avatar
Michael Reeves committed
163 164 165
   bool m_bFindHidden = false;
   bool m_bRecursive = false;
   bool m_bFollowDirLinks = false;
Joachim Eibl's avatar
Joachim Eibl committed
166 167 168

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

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

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

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


#endif