File: fileprototype.cpp

package info (click to toggle)
copyq 3.7.3-1~bpo9+1
  • links: PTS, VCS
  • area: main
  • in suites: stretch-backports
  • size: 10,480 kB
  • sloc: cpp: 51,894; sh: 734; python: 211; xml: 57; makefile: 34
file content (199 lines) | stat: -rw-r--r-- 4,102 bytes parent folder | download | duplicates (2)
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
    Copyright (c) 2018, Lukas Holecek <hluk@email.cz>

    This file is part of CopyQ.

    CopyQ 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.

    CopyQ 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 CopyQ.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "fileprototype.h"

#include "../qt/bytearrayclass.h"

#include <QFile>
#include <QScriptEngine>
#include <QScriptValue>

Q_DECLARE_METATYPE(ByteArrayClass*)
Q_DECLARE_METATYPE(QByteArray*)
Q_DECLARE_METATYPE(QFile*)

namespace {

ByteArrayClass *getByteArrayClass(QScriptEngine *eng)
{
    QScriptValue ctor = eng->globalObject().property("ByteArray");
    return qscriptvalue_cast<ByteArrayClass*>(ctor.data());
}

} // namespace

FilePrototype::FilePrototype(QObject *parent)
    : QObject(parent)
{
}

bool FilePrototype::open()
{
    return thisFile()->open(QIODevice::ReadWrite);
}

bool FilePrototype::openReadOnly()
{
    return thisFile()->open(QIODevice::ReadOnly);
}

bool FilePrototype::openWriteOnly()
{
    return thisFile()->open(QIODevice::WriteOnly);
}

bool FilePrototype::openAppend()
{
    return thisFile()->open(QIODevice::Append);
}

void FilePrototype::close()
{
    thisFile()->close();
}

QScriptValue FilePrototype::read(qint64 maxSize)
{
    return newByteArray(thisFile()->read(maxSize));
}

QScriptValue FilePrototype::readLine(qint64 maxSize)
{
    return newByteArray(thisFile()->readLine(maxSize));
}

QScriptValue FilePrototype::readAll()
{
    return newByteArray(thisFile()->readAll());
}

qint64 FilePrototype::write(const QScriptValue &value)
{
    return thisFile()->write(toByteArray(value));
}

bool FilePrototype::atEnd() const
{
    return thisFile()->atEnd();
}

qint64 FilePrototype::bytesAvailable() const
{
    return thisFile()->bytesAvailable();
}

qint64 FilePrototype::bytesToWrite() const
{
    return thisFile()->bytesToWrite();
}

bool FilePrototype::canReadLine() const
{
    return thisFile()->canReadLine();
}

QScriptValue FilePrototype::errorString() const
{
    return thisFile()->errorString();
}

bool FilePrototype::isOpen() const
{
    return thisFile()->isOpen();
}

bool FilePrototype::isReadable() const
{
    return thisFile()->isReadable();
}

bool FilePrototype::isWritable() const
{
    return thisFile()->isWritable();
}

QScriptValue FilePrototype::peek(qint64 maxSize)
{
    return newByteArray(thisFile()->peek(maxSize));
}

qint64 FilePrototype::pos() const
{
    return thisFile()->pos();
}

bool FilePrototype::reset()
{
    return thisFile()->reset();
}

bool FilePrototype::seek(qint64 pos)
{
    return thisFile()->seek(pos);
}

void FilePrototype::setTextModeEnabled(bool enabled)
{
    thisFile()->setTextModeEnabled(enabled);
}

qint64 FilePrototype::size() const
{
    return thisFile()->size();
}

QScriptValue FilePrototype::fileName() const
{
    return thisFile()->fileName();
}

bool FilePrototype::exists() const
{
    return thisFile()->exists();
}

bool FilePrototype::flush()
{
    return thisFile()->flush();
}

bool FilePrototype::remove()
{
    return thisFile()->remove();
}

QFile *FilePrototype::thisFile() const
{
    return qscriptvalue_cast<QFile*>(thisObject().data());
}

QScriptValue FilePrototype::newByteArray(const QByteArray &bytes)
{
    ByteArrayClass *cls = getByteArrayClass(engine());
    return cls ? cls->newInstance(bytes) : engine()->newVariant(QVariant::fromValue(bytes));
}

QByteArray FilePrototype::toByteArray(const QScriptValue &value)
{
    ByteArrayClass *cls = getByteArrayClass(engine());
    return (cls && value.scriptClass() == cls)
            ? *qscriptvalue_cast<QByteArray*>(value.data())
            : value.toString().toUtf8();
}