Added FileIO QML Plugin

This commit is contained in:
seiichiro 2014-02-23 12:13:21 +01:00
parent 11fc75f3c1
commit d1a44054d7
5 changed files with 309 additions and 1 deletions

7
FileIO/FileIO.pri Normal file
View file

@ -0,0 +1,7 @@
QT += core declarative
INCLUDEPATH += $$PWD/src
HEADERS += $$PWD/src/fileio.h
SOURCES += $$PWD/src/fileio.cpp

120
FileIO/docs/FileIO.md Normal file
View file

@ -0,0 +1,120 @@
# FileIO
## General
A QML-Plugin which provides basic file operations.
## Including FileIO in Your Project
First copy the FileIO-directory into your project path somewhere:
my-project
|--my-project.pro
|--src
|--FileIO
| |--src
| |--FileIO.pri
|--my-project.cpp
Next include FileIO.pri into my-project.pro:
TARGET = my-project
CONFIG += sailfishapp
SOURCES += src/my-project.cpp
.
.
.
include (src/FileIO/FileIO.pri)
Now you can include the header in your main cpp and register the component:
#include <QtQuick>
#include <sailfishapp.h>
#include <QGuiApplication>
#include "fileio.h"
int main(int argc, char *argv[])
{
// Get App and QML-View objects
QScopedPointer<QGuiApplication> app(SailfishApp::application(argc, argv));
QScopedPointer<QQuickView> view(SailfishApp::createView());
// Register FileIO Class
qmlRegisterType<FileIO, 1>("harbour.my-project.FileIO", 1, 0, "FileIO");
// Prepare the QML and set Homedir
view->setSource(SailfishApp::pathTo("qml/my-project.qml"));
view->show();
// Run the app
return app->exec();
}
Now you can use the FileIO Object in your QML
## Using the QML Object
Here are some short examples:
### Reading Files
import harbour.my-project.FileIO 1.0
FileIO {
id: myFile
source: "/home/nemo/some-file.txt"
onError: { console.log(msg); }
}
var content = myFile.read()
First the FileIO-Plugin is imported in the QML file. Now a FileIO object with the ID myFile is created. The property **source** holds the filename of the file to read or write. The signal **onError** is called when something goes wrong, **msg** contains the error message.
After declaring the object the method **read()** is used, it retuns the contenst of the file as a string.
### Writing files
import harbour.my-project.FileIO 1.0
FileIO {
id: myFile
source: "/home/nemo/some-file.txt"
onError: { console.log(msg); }
}
myFile.write("Hello World")
The declaration stays the same as for reading files.
For writing the method **write("content")** is called. This mehtod will write the passed string to the file defined in **source**. The content of the file will be replaced with the content passed to the method.
### Checking if a File Exists
import harbour.my-project.FileIO 1.0
FileIO { id: fileIO }
if (fileIO.exists("/home/nemo/myfile.txt")) {
console.log("file exists");
} else {
console.log("file does not exists");
}
The method **exists(file)** gets passed the path of the file or directory to check. If the file or directory exists the method will return **true**, otherwise it will return **false**
The method **exists()** can also be called without a parameter, in this case it will check if the file defined in **source** exists.
### Creating Directories
import harbour.my-project.FileIO 1.0
FileIO { id: fileIO }
fileIO.mkpath("/tmp/directory");
The method **mkpath(directory)** will create the given directory, if it does not exist already. The method will return **true** if the directory exists or was successfully created, otherwise it will return **false**

113
FileIO/src/fileio.cpp Normal file
View file

@ -0,0 +1,113 @@
/*
* Copyright (c) 2014, Stefan Brand <seiichiro@seiichiro0185.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this
* list of conditions and the following disclaimer in the documentation and/or other
* materials provided with the distribution.
*
* 3. The names of the contributors may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "fileio.h"
#include <QFile>
#include <QDir>
#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;
}
bool FileIO::exists()
{
if (mSource.isEmpty()) {
emit error("Source is empty!");
return false;
}
QFile file(mSource);
return file.exists();
}
bool FileIO::exists(const QString& filename)
{
if (filename.isEmpty()) {
emit error("Source is empty!");
return false;
}
QFile file(filename);
return file.exists();
}
bool FileIO::mkpath(const QString& dirpath) {
if (dirpath.isEmpty()) {
emit error("Source is empty!");
return false;
}
QDir dir(dirpath);
return dir.mkpath(dirpath);
}

64
FileIO/src/fileio.h Normal file
View file

@ -0,0 +1,64 @@
/*
* Copyright (c) 2014, Stefan Brand <seiichiro@seiichiro0185.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this
* list of conditions and the following disclaimer in the documentation and/or other
* materials provided with the distribution.
*
* 3. The names of the contributors may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef FILEIO_H
#define FILEIO_H
#include <QObject>
#include <QString>
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);
Q_INVOKABLE bool exists();
Q_INVOKABLE bool exists(const QString& filename);
Q_INVOKABLE bool mkpath(const QString& dirpath);
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

View file

@ -6,4 +6,8 @@ In this repository I will collect general components for Sailfish Silica Apps th
### NotifyBanner
Show a notification banner at the top of your app.
Show a notification banner at the top of your app.
### FileIO
A QML-Plugin to do basic file operations like reading and writing text files.