diff --git a/harbour-sailotp.pro b/harbour-sailotp.pro index 6bfb246..fd985d8 100644 --- a/harbour-sailotp.pro +++ b/harbour-sailotp.pro @@ -13,7 +13,8 @@ DEFINES += APP_BUILDNUM=\\\"$$RELEASE\\\" CONFIG += sailfishapp -SOURCES += src/harbour-sailotp.cpp +SOURCES += src/harbour-sailotp.cpp \ + src/fileio.cpp OTHER_FILES += qml/harbour-sailotp.qml \ qml/cover/CoverPage.qml \ @@ -28,3 +29,6 @@ OTHER_FILES += qml/harbour-sailotp.qml \ qml/lib/sha.js \ qml/sailotp.png +HEADERS += \ + src/fileio.h + diff --git a/src/fileio.cpp b/src/fileio.cpp new file mode 100644 index 0000000..eceea0b --- /dev/null +++ b/src/fileio.cpp @@ -0,0 +1,51 @@ +#include "fileio.h" +#include +#include + +FileIO::FileIO(QObject *parent) : + QObject(parent) +{ + +} + +QString FileIO::read() +{ + if (mSource.isEmpty()){ + emit error("source is empty"); + return QString(); + } + + QFile file(mSource); + QString fileContent; + if ( file.open(QIODevice::ReadOnly) ) { + QString line; + QTextStream t( &file ); + do { + line = t.readLine(); + fileContent += line; + } while (!line.isNull()); + + file.close(); + } else { + emit error("Unable to open the file"); + return QString(); + } + return fileContent; +} + +bool FileIO::write(const QString& data) +{ + if (mSource.isEmpty()) + return false; + + QFile file(mSource); + if (!file.open(QFile::WriteOnly | QFile::Truncate)) + return false; + + QTextStream out(&file); + out << data; + + file.close(); + + return true; +} diff --git a/src/fileio.h b/src/fileio.h new file mode 100644 index 0000000..011b99b --- /dev/null +++ b/src/fileio.h @@ -0,0 +1,33 @@ +#ifndef FILEIO_H +#define FILEIO_H + +#include + +class FileIO : public QObject +{ + Q_OBJECT + +public: + Q_PROPERTY(QString source + READ source + WRITE setSource + NOTIFY sourceChanged) + explicit FileIO(QObject *parent = 0); + + Q_INVOKABLE QString read(); + Q_INVOKABLE bool write(const QString& data); + + QString source() { return mSource; }; + +public slots: + void setSource(const QString& source) { mSource = source; }; + +signals: + void sourceChanged(const QString& source); + void error(const QString& msg); + +private: + QString mSource; +}; + +#endif // FILEIO_H diff --git a/src/harbour-sailotp.cpp b/src/harbour-sailotp.cpp index b967dae..1b68ccc 100644 --- a/src/harbour-sailotp.cpp +++ b/src/harbour-sailotp.cpp @@ -33,10 +33,23 @@ #include #include +#include "fileio.h" int main(int argc, char *argv[]) { - QCoreApplication::setApplicationVersion(APP_VERSION); + QScopedPointer app(SailfishApp::application(argc, argv)); + QScopedPointer view(SailfishApp::createView()); - return SailfishApp::main(argc, argv); + app->setOrganizationName("harbour-sailotp"); + app->setOrganizationDomain("harbour-sailotp"); + app->setApplicationName("harbour-sailotp"); + app->setApplicationVersion(APP_VERSION); + + qmlRegisterType("FileIO", 1, 0, "FileIO"); + + view->setSource(SailfishApp::pathTo("qml/harbour-sailotp.qml")); + view->show(); + + return app->exec(); } +