-
Notifications
You must be signed in to change notification settings - Fork 1
/
project.cpp
143 lines (110 loc) · 3.43 KB
/
project.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
* This file is part of QssEditor.
*
* QssEditor is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* QssEditor is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with QssEditor. If not, see <http://www.gnu.org/licenses/>.
*/
#include <QDataStream>
#include <QByteArray>
#include <QRegExp>
#include <QDebug>
#include <QFile>
#include "project.h"
Project::Project()
: m_version(-1)
{}
bool Project::setFilePath(const QString &filePath)
{
static const QRegExp rx("/\\*\\s+QssEditor:\\s+([a-zA-Z0-9+\\/=]+)\\s+\\*/\\s+");
m_error.clear();
if(filePath.isEmpty())
{
m_error = QObject::tr("File name is empty");
return false;
}
QFile file(filePath);
if(!file.open(QIODevice::ReadOnly))
{
m_error = file.errorString();
return false;
}
QString qss = file.readAll();
int version = -1;
file.close();
if(rx.indexIn(qss) == 0)
{
QByteArray ba = QByteArray::fromBase64(rx.cap(1).toLatin1());
QDataStream ds(&ba, QIODevice::ReadOnly);
ds.setVersion(QDataStream::Qt_4_0);
ds >> version;
if(ds.status() != QDataStream::Ok)
{
m_error = dataStreamErrorToString(ds.status());
return false;
}
qss = qss.mid(rx.matchedLength());
qDebug("Loaded project version %d", version);
}
else
qDebug("QssEditor project header is not found");
m_qss = qss;
m_version = version;
return true;
}
bool Project::saveAs(const QString &filePath)
{
QFile file(filePath);
if(!file.open(QIODevice::WriteOnly | QIODevice::Truncate))
{
m_error = file.errorString();
return false;
}
QByteArray ba;
if(m_version > 0)
{
QDataStream ds(&ba, QIODevice::ReadWrite);
ds.setVersion(QDataStream::Qt_4_0);
qDebug("Saving project with version %d", m_version);
ds << m_version;
if(ds.status() != QDataStream::Ok)
{
m_error = dataStreamErrorToString(ds.status());
return false;
}
ba = "/* QssEditor: " + ba.toBase64() + " */\n" + m_qss.toLatin1();
}
else
ba = m_qss.toLatin1();
if(file.write(ba) != ba.length())
{
m_error = file.errorString();
return false;
}
return true;
}
QString Project::dataStreamErrorToString(int status)
{
switch(status)
{
case QDataStream::ReadPastEnd:
return QObject::tr("The data stream has read past the end of the data");
case QDataStream::ReadCorruptData:
return QObject::tr("The data stream has read corrupt data");
#if QT_VERSION >= QT_VERSION_CHECK(4,8,0)
case QDataStream::WriteFailed:
return QObject::tr("The data stream cannot write to the underlying device");
#endif
default:
return QObject::tr("Unknown error");
}
}