mirror of
https://github.com/seiichiro0185/sailotp.git
synced 2024-11-22 07:39:42 +00:00
Added FileIO Class as preparation for DB Export/Import
This commit is contained in:
parent
aa945ff787
commit
42f686846d
4 changed files with 104 additions and 3 deletions
|
@ -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
|
||||
|
||||
|
|
51
src/fileio.cpp
Normal file
51
src/fileio.cpp
Normal file
|
@ -0,0 +1,51 @@
|
|||
#include "fileio.h"
|
||||
#include <QFile>
|
||||
#include <QTextStream>
|
||||
|
||||
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;
|
||||
}
|
33
src/fileio.h
Normal file
33
src/fileio.h
Normal file
|
@ -0,0 +1,33 @@
|
|||
#ifndef FILEIO_H
|
||||
#define FILEIO_H
|
||||
|
||||
#include <QObject>
|
||||
|
||||
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
|
|
@ -33,10 +33,23 @@
|
|||
|
||||
#include <sailfishapp.h>
|
||||
#include <QGuiApplication>
|
||||
#include "fileio.h"
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
QCoreApplication::setApplicationVersion(APP_VERSION);
|
||||
QScopedPointer<QGuiApplication> app(SailfishApp::application(argc, argv));
|
||||
QScopedPointer<QQuickView> 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>("FileIO", 1, 0, "FileIO");
|
||||
|
||||
view->setSource(SailfishApp::pathTo("qml/harbour-sailotp.qml"));
|
||||
view->show();
|
||||
|
||||
return app->exec();
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue