ArnLib  4.0.x
Active Registry Network
ArnQmlMSystem.cpp
Go to the documentation of this file.
1 // Copyright (C) 2010-2022 Michael Wiklund.
2 // All rights reserved.
3 // Contact: arnlib@wiklunden.se
4 //
5 // This file is part of the ArnLib - Active Registry Network.
6 // Parts of ArnLib depend on Qt and/or other libraries that have their own
7 // licenses. Usage of these other libraries is subject to their respective
8 // license agreements.
9 //
10 // GNU Lesser General Public License Usage
11 // This file may be used under the terms of the GNU Lesser General Public
12 // License version 2.1 as published by the Free Software Foundation and
13 // appearing in the file LICENSE_LGPL.txt included in the packaging of this
14 // file. In addition, as a special exception, you may use the rights described
15 // in the Nokia Qt LGPL Exception version 1.1, included in the file
16 // LGPL_EXCEPTION.txt in this package.
17 //
18 // GNU General Public License Usage
19 // Alternatively, this file may be used under the terms of the GNU General Public
20 // License version 3.0 as published by the Free Software Foundation and appearing
21 // in the file LICENSE_GPL.txt included in the packaging of this file.
22 //
23 // Other Usage
24 // Alternatively, this file may be used in accordance with the terms and conditions
25 // contained in a signed written agreement between you and Michael Wiklund.
26 //
27 // This program is distributed in the hope that it will be useful, but WITHOUT ANY
28 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
29 // PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
30 //
31 
32 #include "ArnInc/ArnQmlMSystem.hpp"
33 #include <QFile>
34 #include <QUrl>
35 #include <QTextStream>
36 #include <QDebug>
37 
38 
39 namespace Arn {
40 
41 QmlMFileIO::QmlMFileIO( QObject* parent)
42  : QObject( parent)
43 {
44 }
45 
46 
48 {
49  QUrl url = QUrl( _path);
50  if (_path.isEmpty() || !url.isLocalFile()){
51  emit error("Invalid fileName=" + _path);
52  return QString();
53  }
54  QString path = url.toLocalFile();
55 
56  QFile file( path);
57  QString fileContent;
58  if (file.open(QIODevice::ReadOnly)) {
59  QString line;
60  QTextStream ts( &file);
61  do {
62  line = ts.readLine();
63  fileContent += ((fileContent.size() > 0) && !line.isNull()) ? "\n" : "";
64  fileContent += line;
65  } while (!line.isNull());
66 
67  file.close();
68  }
69  else {
70  emit error("Can't' open file, path=" + path);
71  return QString();
72  }
73  return fileContent;
74 }
75 
76 
77 bool QmlMFileIO::write( const QString& data)
78 {
79  if (_path.isEmpty())
80  return false;
81 
82  QFile file( _path);
83  if (!file.open(QFile::WriteOnly | QFile::Truncate))
84  return false;
85 
86  QTextStream out( &file);
87  out << data;
88 
89  file.close();
90 
91  return true;
92 }
93 
94 
96 {
97  QUrl url = QUrl( _path);
98  if (_path.isEmpty() || !url.isLocalFile()){
99  emit error("Invalid fileName=" + _path);
100  return QByteArray();
101  }
102  QString path = url.path();
103 
104  QFile file( path);
105  QByteArray fileContent;
106  if (file.open( QIODevice::ReadOnly) ) {
107  fileContent = file.readAll();
108  file.close();
109  }
110  else {
111  emit error("Can't' open file, path=" + path);
112  return QByteArray();
113  }
114  return fileContent;
115 }
116 
117 
118 bool QmlMFileIO::writeBytes( const QByteArray& data)
119 {
120  if (_path.isEmpty())
121  return false;
122 
123  QFile file(_path);
124  if (!file.open(QFile::WriteOnly | QFile::Truncate))
125  return false;
126 
127  file.write( data);
128 
129  file.close();
130 
131  return true;
132 }
133 
134 
135 QString QmlMFileIO::path()
136 {
137  return _path;
138 }
139 
140 
141 void QmlMFileIO::setPath( const QString& path)
142 {
143  _path = path;
144  emit pathChanged( path);
145 }
146 
147 }
QString path()
QmlMFileIO(QObject *parent=arnNullptr)
void pathChanged(const QString &path)
void setPath(const QString &path)
Definition: Arn.cpp:43
Q_INVOKABLE bool write(const QString &data)
Q_INVOKABLE QByteArray readBytes()
Q_INVOKABLE QString read()
void error(const QString &msg)
Q_INVOKABLE bool writeBytes(const QByteArray &data)