QGIS API Documentation 4.1.0-Master (60fea48833c)
Loading...
Searching...
No Matches
qgsfileutils.h
Go to the documentation of this file.
1/***************************************************************************
2 qgsfileutils.h
3 ---------------------------
4 begin : November 2017
5 copyright : (C) 2017 by Etienne Trimaille
6 email : etienne dot trimaille at gmail dot com
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
18#ifndef QGSFILEUTILS_H
19#define QGSFILEUTILS_H
20
21#include "qgis.h"
22#include "qgis_core.h"
23#include "qgis_sip.h"
24
25#include <QString>
26
32class CORE_EXPORT QgsFileUtils
33{
34 Q_GADGET
35
36 public:
40 static QString representFileSize( qint64 bytes );
41
42
50 static QStringList extensionsFromFilter( const QString &filter );
51
58 static QString wildcardsFromFilter( const QString &filter );
59
68 static bool fileMatchesFilter( const QString &fileName, const QString &filter );
69
81 static QString ensureFileNameHasExtension( const QString &fileName, const QStringList &extensions );
82
94 static QString addExtensionFromFilter( const QString &fileName, const QString &filter );
95
102 static QString stringToSafeFilename( const QString &string );
103
110 static QString findClosestExistingPath( const QString &path );
111
122 static QStringList findFile( const QString &file, const QString &basepath = QString(), int maxClimbs = 4, int searchCeiling = 4, const QString &currentDir = QString() );
123
131 static Qgis::DriveType driveType( const QString &path ) SIP_THROW( QgsNotSupportedException );
132
139 static bool pathIsSlowDevice( const QString &path );
140
157 static QSet< QString > sidecarFilesForPath( const QString &path );
158
182 static bool renameDataset(
183 const QString &oldPath, const QString &newPath, QString &error SIP_OUT, Qgis::FileOperationFlags flags = Qgis::FileOperationFlag::IncludeMetadataFile | Qgis::FileOperationFlag::IncludeStyleFile
184 );
185
195 static int openedFileLimit() SIP_SKIP;
196
206 static int openedFileCount() SIP_SKIP;
207
219 static bool isCloseToLimitOfOpenedFiles( int filesToBeOpened = 1 ) SIP_SKIP;
220
229 static QStringList splitPathToComponents( const QString &path );
230
241 static QString uniquePath( const QString &path );
242
248 enum class CopyFlag : int SIP_ENUM_BASETYPE( IntFlag )
249 {
250 NoSymLinks = 1 << 0,
251 };
252 Q_ENUM( CopyFlag )
253
254
259 Q_DECLARE_FLAGS( CopyFlags, CopyFlag )
260 Q_FLAG( CopyFlags )
261
274 static bool copyDirectory( const QString &source, const QString &destination, QgsFileUtils::CopyFlags flags = QgsFileUtils::CopyFlags() );
275
285 static bool replaceTextInFile( const QString &path, const QString &searchString, const QString &replacement );
286};
287
289
290#endif // QGSFILEUTILS_H
DriveType
Drive types.
Definition qgis.h:1152
@ IncludeMetadataFile
Indicates that any associated .qmd metadata file should be included with the operation.
Definition qgis.h:2346
@ IncludeStyleFile
Indicates that any associated .qml styling file should be included with the operation.
Definition qgis.h:2347
QFlags< FileOperationFlag > FileOperationFlags
File operation flags.
Definition qgis.h:2350
Contains utilities for working with files.
static QString stringToSafeFilename(const QString &string)
Converts a string to a safe filename, replacing characters which are not safe for filenames with an '...
static bool replaceTextInFile(const QString &path, const QString &searchString, const QString &replacement)
Replaces all occurrences of a given string in a file.
static QStringList findFile(const QString &file, const QString &basepath=QString(), int maxClimbs=4, int searchCeiling=4, const QString &currentDir=QString())
Will check basepath in an outward spiral up to maxClimbs levels to check if file exists.
static int openedFileCount()
Returns the number of currently opened files by the process.
CopyFlag
Creates a unique file path name from a desired path by appending _<n> (where <n> is an integer number...
static QString wildcardsFromFilter(const QString &filter)
Given a filter string like GeoTIFF Files (*.tiff *.tif), extracts the wildcard portion of this filter...
static bool renameDataset(const QString &oldPath, const QString &newPath, QString &error, Qgis::FileOperationFlags flags=Qgis::FileOperationFlag::IncludeMetadataFile|Qgis::FileOperationFlag::IncludeStyleFile)
Renames the dataset at oldPath to newPath, renaming both the file at oldPath and all associated sidec...
static QSet< QString > sidecarFilesForPath(const QString &path)
Returns a list of the sidecar files which exist for the dataset a the specified path.
static bool pathIsSlowDevice(const QString &path)
Returns true if the specified path is assumed to reside on a slow device, e.g.
static bool isCloseToLimitOfOpenedFiles(int filesToBeOpened=1)
Returns whether when opening new file(s) will reach, or nearly reach, the limit of simultaneously ope...
static Qgis::DriveType driveType(const QString &path)
Returns the drive type for the given path.
static bool fileMatchesFilter(const QString &fileName, const QString &filter)
Returns true if the given fileName matches a file filter string.
static QString ensureFileNameHasExtension(const QString &fileName, const QStringList &extensions)
Ensures that a fileName ends with an extension from the provided list of extensions.
static QString representFileSize(qint64 bytes)
Returns the human size from bytes.
static QString addExtensionFromFilter(const QString &fileName, const QString &filter)
Ensures that a fileName ends with an extension from the specified filter string.
static int openedFileLimit()
Returns the limit of simultaneously opened files by the process.
static QStringList splitPathToComponents(const QString &path)
Given a file path, returns a list of all the components leading to that path.
static QString findClosestExistingPath(const QString &path)
Returns the top-most existing folder from path.
QFlags< CopyFlag > CopyFlags
Flags controlling behavior of file copy operations.
static bool copyDirectory(const QString &source, const QString &destination, QgsFileUtils::CopyFlags flags=QgsFileUtils::CopyFlags())
Recursively copies a whole directory.
static QStringList extensionsFromFilter(const QString &filter)
Returns a list of the extensions contained within a file filter string.
Custom exception class which is raised when an operation is not supported.
#define SIP_ENUM_BASETYPE(type)
Definition qgis_sip.h:274
#define SIP_SKIP
Definition qgis_sip.h:133
#define SIP_OUT
Definition qgis_sip.h:57
#define SIP_THROW(name,...)
Definition qgis_sip.h:210
Q_DECLARE_OPERATORS_FOR_FLAGS(QgsProjectionSelectionWidget::CrsOptions)