diff --git a/FileIO/FileIO.pri b/FileIO/FileIO.pri new file mode 100644 index 0000000..816d5bc --- /dev/null +++ b/FileIO/FileIO.pri @@ -0,0 +1,7 @@ +QT += core declarative + +INCLUDEPATH += $$PWD/src + +HEADERS += $$PWD/src/fileio.h + +SOURCES += $$PWD/src/fileio.cpp diff --git a/FileIO/docs/FileIO.md b/FileIO/docs/FileIO.md new file mode 100644 index 0000000..edf24ea --- /dev/null +++ b/FileIO/docs/FileIO.md @@ -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 + #include + #include + #include "fileio.h" + + int main(int argc, char *argv[]) + { + // Get App and QML-View objects + QScopedPointer app(SailfishApp::application(argc, argv)); + QScopedPointer view(SailfishApp::createView()); + + // Register FileIO Class + qmlRegisterType("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** \ No newline at end of file diff --git a/FileIO/src/fileio.cpp b/FileIO/src/fileio.cpp new file mode 100644 index 0000000..146bc74 --- /dev/null +++ b/FileIO/src/fileio.cpp @@ -0,0 +1,113 @@ +/* + * Copyright (c) 2014, Stefan Brand + * 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 +#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; +} + +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); +} diff --git a/FileIO/src/fileio.h b/FileIO/src/fileio.h new file mode 100644 index 0000000..878811a --- /dev/null +++ b/FileIO/src/fileio.h @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2014, Stefan Brand + * 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 +#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); + 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 diff --git a/README.md b/README.md index 9ea3181..d38d758 100644 --- a/README.md +++ b/README.md @@ -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. \ No newline at end of file +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. \ No newline at end of file