Skip to content

Commit

Permalink
QmlFile: Add class to access local files from QML
Browse files Browse the repository at this point in the history
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
aperezdc committed Jul 9, 2012
1 parent 52f5061 commit 42408b2
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 2 deletions.
4 changes: 4 additions & 0 deletions cfm-main.cc
Expand Up @@ -6,12 +6,14 @@
*/

#include "cfm-controller.h"
#include "qml-file.h"
#include <QLocale>
#include <QTextCodec>
#include <QTranslator>
#include <QApplication>
#include <QDeclarativeView>
#include <MDeclarativeCache>
#include <QtDeclarative>
#include <gst/gst.h>


Expand All @@ -21,6 +23,8 @@ static const QUrl INITIAL_VIEW("qrc:/qml/main.qml");
Q_DECL_EXPORT int
main (int argc, char *argv[])
{
qmlRegisterType<QmlFile, 1>("com.igalia.people.aperez", 1, 0, "File");

gst_init(&argc, &argv);

QApplication *application(MDeclarativeCache::qApplication(argc, argv));
Expand Down
6 changes: 4 additions & 2 deletions cuacfmeego.pro
Expand Up @@ -27,8 +27,10 @@ QMAKE_LFLAGS += -pie -rdynamic -Wl,--as-needed
# Application sources
#
SOURCES += cfm-main.cc \
cfm-controller.cc
HEADERS += cfm-controller.h
cfm-controller.cc \
qml-file.cc
HEADERS += cfm-controller.h \
qml-file.h
RESOURCES += cuacfmeego.qrc

# l10n stuff
Expand Down
69 changes: 69 additions & 0 deletions qml-file.cc
@@ -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;
}
60 changes: 60 additions & 0 deletions qml-file.h
@@ -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__ */

0 comments on commit 42408b2

Please sign in to comment.