-
Notifications
You must be signed in to change notification settings - Fork 0
/
qml-file.cc
69 lines (58 loc) · 1.55 KB
/
qml-file.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
* qml-file.cc
* Copyright (C) 2012 Adrian Perez <aperez@igalia.com>
*
* Distributed under terms of the GPLv3 license.
*/
#include "qml-file.h"
#include <QTextStream>
#include <QFile>
static const QString noPathErrorString("No 'path' was set before attempting to read");
static const QString notExistsErrorString("File '%1' does not exist");
static const QString openForReadErrorString("File '%1' could not be opened for reading");
static const QString openForWriteErrorString("File '%1' could not be opened for writing");
QmlFile::QmlFile(QObject* parent)
: QObject(parent)
, _file()
{
}
QmlFile::~QmlFile()
{
}
QString QmlFile::read()
{
const QString path(_file.fileName());
if (path.isEmpty()) {
emit error(noPathErrorString);
return QString();
}
if (!_file.exists()) {
emit error(notExistsErrorString.arg(path));
return QString();
}
if (!_file.open(QFile::ReadOnly)) {
emit error(openForReadErrorString.arg(path));
return QString();
}
QTextStream stream(&_file);
QString data = stream.readAll();
_file.close();
return data;
}
bool QmlFile::write(const QString& data)
{
const QString path(_file.fileName());
if (path.isEmpty()) {
emit error(noPathErrorString);
return false;
}
if (!_file.open(QFile::WriteOnly | QFile::Truncate)) {
emit error(openForWriteErrorString.arg(path));
return false;
}
QTextStream stream(&_file);
stream << data;
_file.close();
emit contentChanged(data);
return true;
}