-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathfileio.cpp
105 lines (88 loc) · 1.86 KB
/
fileio.cpp
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include "fileio.h"
#include <QFile>
FileIO::FileIO(QObject *parent)
: QObject(parent)
, m_error(false)
{
}
bool FileIO::read()
{
QString filePath;
if (m_filePath.isLocalFile()) {
filePath = m_filePath.toLocalFile();
}
else {
filePath = m_filePath.toString();
}
QFile file(filePath);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
m_error = true;
m_errorString = tr("Could not open file for reading");
emit errorStringChanged();
return false;
}
else {
m_error = false;
m_errorString = QString();
emit errorStringChanged();
}
m_text = QString::fromUtf8(file.readAll());
file.close();
emit textChanged();
return true;
}
bool FileIO::write()
{
QString filePath;
if (m_filePath.isLocalFile()) {
filePath = m_filePath.toLocalFile();
}
else {
filePath = m_filePath.toString();
}
QFile file(filePath);
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
m_error = true;
m_errorString = tr("Could not open file for writing");
emit errorStringChanged();
return false;
}
else {
m_error = false;
m_errorString = QString();
emit errorStringChanged();
}
file.write(m_text.toUtf8());
file.close();
return true;
}
QUrl FileIO::filePath() const
{
return m_filePath;
}
void FileIO::setFilePath(const QUrl &filePath)
{
if (m_filePath == filePath)
return;
m_filePath = filePath;
emit filePathChanged();
}
QString FileIO::text() const
{
return m_text;
}
void FileIO::setText(const QString &text)
{
if (m_text == text)
return;
m_text = text;
emit textChanged();
}
QString FileIO::errorString() const
{
return m_errorString;
}
bool FileIO::hasError() const
{
return m_error;
}