summaryrefslogtreecommitdiffstats
path: root/src/fileaccess.h
blob: b87c54ac2d12579be7bfb9439158eed425c30d41 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/***************************************************************************
 *   Copyright (C) 2003-2007 by Joachim Eibl                               *
 *   joachim.eibl at gmx.de                                                   *
 *                                                                         *
 *   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

#include <tqdialog.h>
#include <tqfileinfo.h>
#include <kprogress.h>
#include <tdeio/job.h>
#include <tdeio/jobclasses.h>
#include <kurldrag.h>
#include <list>
#include <tqstring.h>
#include <tqdatetime.h>

bool wildcardMultiMatch( const TQString& wildcard, const TQString& testString, bool bCaseSensitive );

class t_DirectoryList;

class FileAccess
{
public:
   FileAccess();
   ~FileAccess();
   FileAccess( const TQString& name, bool bWantToWrite=false ); // name: local file or dirname or url (when supported)
   FileAccess( const TQString& workingDir, const TQString& name, bool bWantToWrite=false ); // name: local file or dirname or url (when supported)
   void setFile( const TQString& name, bool bWantToWrite=false );

   bool isValid() const;
   bool isFile() const;
   bool isDir() const;
   bool isSymLink() const;
   bool exists() const;
   long size() const;            // Size as returned by stat().
   long sizeForReading();  // If the size can't be determined by stat() then the file is copied to a local temp file.
   bool isReadable() const;
   bool isWritable() const;
   bool isExecutable() const;
   bool isHidden() const;
   TQString readLink() const;

   TQDateTime   created()       const;
   TQDateTime   lastModified()  const;
   TQDateTime   lastRead()      const;

   TQString fileName() const; // Just the name-part of the path, without parent directories
   TQString filePath() const; // The path-string that was used during construction
   TQString prettyAbsPath() const;
   KURL url() const;
   TQString absFilePath() const;

   bool isLocal() const;

   bool readFile(void* pDestBuffer, unsigned long maxLength );
   bool writeFile(const void* pSrcBuffer, unsigned long length );
   bool listDir( t_DirectoryList* pDirList, bool bRecursive, bool bFindHidden,
                 const TQString& filePattern, const TQString& fileAntiPattern,
                 const TQString& dirAntiPattern, bool bFollowDirLinks, bool bUseCvsIgnore );
   bool copyFile( const TQString& destUrl );
   bool createBackup( const TQString& bakExtension );

   static TQString tempFileName();
   static bool removeTempFile( const TQString& );
   bool removeFile();
   static bool removeFile( const TQString& );
   static bool makeDir( const TQString& );
   static bool removeDir( const TQString& );
   static bool exists( const TQString& );
   static TQString cleanDirPath( const TQString& );

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

   void addPath( const TQString& txt );
   TQString getStatusText();
private:
   void setUdsEntry( const TDEIO::UDSEntry& e );
   KURL m_url;
   bool m_bLocal;
   bool m_bValidData;

   unsigned long m_size;
   TQDateTime m_modificationTime;
   TQDateTime m_accessTime;
   TQDateTime m_creationTime;
   bool m_bReadable;
   bool m_bWritable;
   bool m_bExecutable;
   bool m_bExists;
   bool m_bFile;
   bool m_bDir;
   bool m_bSymLink;
   bool m_bHidden;
   long m_fileType; // for testing only

   TQString m_workingDir;
   TQString m_linkTarget;
   TQString m_user;
   TQString m_group;
   TQString m_name;
   TQString m_path;
   TQString m_absFilePath;
   TQString m_localCopy;
   TQString m_statusText;  // Might contain an error string, when the last operation didn't succeed.

   friend class FileAccessJobHandler;
};

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


class FileAccessJobHandler : public TQObject
{
   TQ_OBJECT
  
public:
   FileAccessJobHandler( FileAccess* pFileAccess );

   bool get( void* pDestBuffer, long maxLength );
   bool put( const void* pSrcBuffer, long maxLength, bool bOverwrite, bool bResume=false, int permissions=-1 );
   bool stat(int detailLevel=2, bool bWantToWrite=false );
   bool copyFile( const TQString& dest );
   bool rename( const TQString& dest );
   bool listDir( t_DirectoryList* pDirList, bool bRecursive, bool bFindHidden,
                 const TQString& filePattern, const TQString& fileAntiPattern,
                 const TQString& dirAntiPattern, bool bFollowDirLinks, bool bUseCvsIgnore );
   bool mkDir( const TQString& dirName );
   bool rmDir( const TQString& dirName );
   bool removeFile( const TQString& dirName );
   bool symLink( const TQString& linkTarget, const TQString& linkLocation );

private:
   FileAccess* m_pFileAccess;
   bool m_bSuccess;

   // Data needed during Job
   long m_transferredBytes;
   char* m_pTransferBuffer;  // Needed during get or put
   long m_maxLength;

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

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

private slots:
   void slotStatResult( TDEIO::Job* );
   void slotSimpleJobResult( TDEIO::Job* pJob );
   void slotPutJobResult( TDEIO::Job* pJob );

   void slotGetData(TDEIO::Job*,const TQByteArray&);
   void slotPutData(TDEIO::Job*, TQByteArray&);

   void slotListDirInfoMessage( TDEIO::Job*, const TQString& msg );
   void slotListDirProcessNewEntries( TDEIO::Job *, const TDEIO::UDSEntryList& l );

   void slotPercent( TDEIO::Job* pJob, unsigned long percent );
};

class ProgressDialog : public TQDialog
{
   TQ_OBJECT
  
public:
   ProgressDialog( TQWidget* pParent );

   void setStayHidden( bool bStayHidden );
   void setInformation( const TQString& info, bool bRedrawUpdate=true );
   void setInformation( const TQString& info, double dCurrent, bool bRedrawUpdate=true );
   void setCurrent( double dCurrent, bool bRedrawUpdate=true  );
   void step( bool bRedrawUpdate=true );
   void setMaxNofSteps( int dMaxNofSteps );
   void push();
   void pop(bool bRedrawUpdate=true);

   // The progressbar goes from 0 to 1 usually.
   // By supplying a subrange transformation the subCurrent-values
   // 0 to 1 will be transformed to dMin to dMax instead.
   // Requirement: 0 < dMin < dMax < 1
   void setRangeTransformation( double dMin, double dMax );
   void setSubRangeTransformation( double dMin, double dMax );

   void exitEventLoop();
   void enterEventLoop( TDEIO::Job* pJob, const TQString& jobInfo );

   bool wasCancelled();
   void show();
   void hide();
   
   virtual void timerEvent(TQTimerEvent*);
private:

   struct ProgressLevelData
   {
      ProgressLevelData()
      {
         m_dCurrent=0; m_maxNofSteps=1; m_dRangeMin=0; m_dRangeMax=1; 
         m_dSubRangeMin = 0; m_dSubRangeMax = 1;
      }
      double m_dCurrent;
      int    m_maxNofSteps;     // when step() is used.
      double m_dRangeMax;
      double m_dRangeMin;
      double m_dSubRangeMax;
      double m_dSubRangeMin;
   };
   std::list<ProgressLevelData> m_progressStack;
   
   int m_progressDelayTimer;

   KProgress* m_pProgressBar;
   KProgress* m_pSubProgressBar;
   TQLabel* m_pInformation;
   TQLabel* m_pSubInformation;
   TQLabel* m_pSlowJobInfo;
   TQPushButton* m_pAbortButton;
   void recalc(bool bRedrawUpdate);
   TQTime m_t1;
   TQTime m_t2;
   bool m_bWasCancelled;
   TDEIO::Job* m_pJob;
   TQString m_currentJobInfo;  // Needed if the job doesn't stop after a reasonable time.
   bool m_bStayHidden;
protected:
   virtual void reject();
private slots:
   void delayedHide();
   void slotAbort();
};

// When using the ProgressProxy you need not take care of the push and pop, except when explicit.
class ProgressProxy
{
public:
   ProgressProxy();
   ~ProgressProxy();
   
   void setInformation( const TQString& info, bool bRedrawUpdate=true );
   void setInformation( const TQString& info, double dCurrent, bool bRedrawUpdate=true );
   void setCurrent( double dCurrent, bool bRedrawUpdate=true  );
   void step( bool bRedrawUpdate=true );
   void setMaxNofSteps( int dMaxNofSteps );
   bool wasCancelled();
   void setRangeTransformation( double dMin, double dMax );
   void setSubRangeTransformation( double dMin, double dMax );
private:
};

extern ProgressDialog* g_pProgressDialog;



#endif