diff --git a/cfm-main.cc b/cfm-main.cc index 5275348..72bfabb 100644 --- a/cfm-main.cc +++ b/cfm-main.cc @@ -6,12 +6,14 @@ */ #include "cfm-controller.h" +#include "qml-file.h" #include #include #include #include #include #include +#include #include @@ -21,6 +23,8 @@ static const QUrl INITIAL_VIEW("qrc:/qml/main.qml"); Q_DECL_EXPORT int main (int argc, char *argv[]) { + qmlRegisterType("com.igalia.people.aperez", 1, 0, "File"); + gst_init(&argc, &argv); QApplication *application(MDeclarativeCache::qApplication(argc, argv)); diff --git a/cuacfmeego.pro b/cuacfmeego.pro index e0ef236..096a487 100644 --- a/cuacfmeego.pro +++ b/cuacfmeego.pro @@ -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 diff --git a/qml-file.cc b/qml-file.cc new file mode 100644 index 0000000..791621c --- /dev/null +++ b/qml-file.cc @@ -0,0 +1,69 @@ +/* + * qml-file.cc + * Copyright (C) 2012 Adrian Perez + * + * Distributed under terms of the GPLv3 license. + */ + +#include "qml-file.h" +#include +#include + +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; +} diff --git a/qml-file.h b/qml-file.h new file mode 100644 index 0000000..9ee60bc --- /dev/null +++ b/qml-file.h @@ -0,0 +1,60 @@ + +/* + * qml-file.h + * Copyright (C) 2012 Adrian Perez + * + * Distributed under terms of the GPLv3 license. + */ + +#ifndef __qml_file_h__ +#define __qml_file_h__ + +#include +#include + +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__ */ +