diff options
-rw-r--r-- | doc/scripting-api/packagemanagercore.qdoc | 11 | ||||
-rw-r--r-- | src/libs/installer/packagemanagercore.cpp | 28 | ||||
-rw-r--r-- | src/libs/installer/packagemanagercore.h | 1 |
3 files changed, 40 insertions, 0 deletions
diff --git a/doc/scripting-api/packagemanagercore.qdoc b/doc/scripting-api/packagemanagercore.qdoc index aeb116329..65b72c9c4 100644 --- a/doc/scripting-api/packagemanagercore.qdoc +++ b/doc/scripting-api/packagemanagercore.qdoc @@ -334,6 +334,17 @@ */ /*! + \qmlmethod boolean installer::readFile(string filePath, string codecName) + + Returns the contents of the file \a filePath using the encoding specified + by \a codecName. The file is read in the text mode, that is, end-of-line + terminators are translated to the local encoding. + + \note If the file does not exist or an error occurs while reading the file, an + empty string is returned. +*/ + +/*! \qmlmethod boolean installer::fileExists(string filePath) Returns \c true if the \a filePath exists; otherwise returns \c false. diff --git a/src/libs/installer/packagemanagercore.cpp b/src/libs/installer/packagemanagercore.cpp index 8066ff308..a955bc8cc 100644 --- a/src/libs/installer/packagemanagercore.cpp +++ b/src/libs/installer/packagemanagercore.cpp @@ -61,6 +61,8 @@ #include <QtCore/QMutex> #include <QtCore/QSettings> #include <QtCore/QTemporaryFile> +#include <QtCore/QTextCodec> +#include <QtCore/QTextStream> #include <QDesktopServices> #include <QFileDialog> @@ -785,6 +787,32 @@ bool PackageManagerCore::fileExists(const QString &filePath) const return QFileInfo(filePath).exists(); } +/*! + Returns the contents of the file \a filePath using the encoding specified + by \a codecName. The file is read in the text mode, that is, end-of-line + terminators are translated to the local encoding. + + \note If the file does not exist or an error occurs while reading the file, an + empty string is returned. + + \sa {installer::readFile}{installer.readFile} + + */ +QString PackageManagerCore::readFile(const QString &filePath, const QString &codecName) const +{ + QFile f(filePath); + if (!f.open(QIODevice::ReadOnly | QIODevice::Text)) + return QString(); + + QTextCodec *codec = QTextCodec::codecForName(qPrintable(codecName)); + if (!codec) + return QString(); + + QTextStream stream(&f); + stream.setCodec(codec); + return stream.readAll(); +} + // -- QInstaller /*! diff --git a/src/libs/installer/packagemanagercore.h b/src/libs/installer/packagemanagercore.h index 5160b0588..9587de7b4 100644 --- a/src/libs/installer/packagemanagercore.h +++ b/src/libs/installer/packagemanagercore.h @@ -190,6 +190,7 @@ public: Q_INVOKABLE bool isFileExtensionRegistered(const QString &extension) const; Q_INVOKABLE bool fileExists(const QString &filePath) const; + Q_INVOKABLE QString readFile(const QString &filePath, const QString &codecName) const; public: ScriptEngine *componentScriptEngine() const; |