Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
QmlFile: Add class to access local files from QML
Conveniently, the QmlFile class can be also used to access app-resources included from .qrc files (in read-only mode, by the way.)
- Loading branch information
Showing
4 changed files
with
137 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* qml-file.cc | ||
* Copyright (C) 2012 Adrian Perez <aperez@igalia.com> | ||
* | ||
* Distributed under terms of the GPLv3 license. | ||
*/ | ||
|
||
#include "qml-file.h" | ||
#include <QTextStream> | ||
#include <QFile> | ||
|
||
static const QString noPathErrorString("No 'path' was set before attempting to read"); | ||
static const QString notExistsErrorString("File '%1' does not exist"); | ||
static const QString openForReadErrorString("File '%1' could not be opened for reading"); | ||
static const QString openForWriteErrorString("File '%1' could not be opened for writing"); | ||
|
||
|
||
QmlFile::QmlFile(QObject* parent) | ||
: QObject(parent) | ||
, _file() | ||
{ | ||
} | ||
|
||
QmlFile::~QmlFile() | ||
{ | ||
} | ||
|
||
QString QmlFile::read() | ||
{ | ||
const QString path(_file.fileName()); | ||
|
||
if (path.isEmpty()) { | ||
emit error(noPathErrorString); | ||
return QString(); | ||
} | ||
if (!_file.exists()) { | ||
emit error(notExistsErrorString.arg(path)); | ||
return QString(); | ||
} | ||
if (!_file.open(QFile::ReadOnly)) { | ||
emit error(openForReadErrorString.arg(path)); | ||
return QString(); | ||
} | ||
|
||
QTextStream stream(&_file); | ||
QString data = stream.readAll(); | ||
_file.close(); | ||
return data; | ||
} | ||
|
||
bool QmlFile::write(const QString& data) | ||
{ | ||
const QString path(_file.fileName()); | ||
|
||
if (path.isEmpty()) { | ||
emit error(noPathErrorString); | ||
return false; | ||
} | ||
if (!_file.open(QFile::WriteOnly | QFile::Truncate)) { | ||
emit error(openForWriteErrorString.arg(path)); | ||
return false; | ||
} | ||
|
||
QTextStream stream(&_file); | ||
stream << data; | ||
_file.close(); | ||
emit contentChanged(data); | ||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
|
||
/* | ||
* qml-file.h | ||
* Copyright (C) 2012 Adrian Perez <aperez@igalia.com> | ||
* | ||
* Distributed under terms of the GPLv3 license. | ||
*/ | ||
|
||
#ifndef __qml_file_h__ | ||
#define __qml_file_h__ | ||
|
||
#include <QObject> | ||
#include <QFile> | ||
|
||
class QmlFile: public QObject | ||
{ | ||
Q_OBJECT | ||
|
||
public: | ||
explicit QmlFile(QObject *parent = 0); | ||
virtual ~QmlFile(); | ||
|
||
Q_PROPERTY(QString path | ||
READ path | ||
WRITE setPath | ||
NOTIFY pathChanged) | ||
|
||
Q_PROPERTY(QString content | ||
READ read | ||
WRITE write | ||
NOTIFY contentChanged) | ||
|
||
Q_INVOKABLE bool exists() const { | ||
return _file.exists(); | ||
} | ||
|
||
Q_INVOKABLE QString read(); | ||
Q_INVOKABLE bool write(const QString& data); | ||
|
||
const QString path() const { | ||
return _file.fileName(); | ||
} | ||
|
||
public slots: | ||
void setPath(const QString& path) { | ||
_file.setFileName(path); | ||
emit pathChanged(path); | ||
} | ||
|
||
signals: | ||
void contentChanged(const QString& newContent) const; | ||
void pathChanged(const QString& newPath) const; | ||
void error(const QString& message) const; | ||
|
||
private: | ||
QFile _file; | ||
}; | ||
|
||
#endif /* !__qml_file_h__ */ | ||
|