KJsEmbed

fileio.cpp
1 /* This file is part of the KDE libraries
2  Copyright (C) 2005, 2006 Ian Reinhart Geiser <[email protected]>
3  Copyright (C) 2005, 2006 Matt Broadstone <[email protected]>
4  Copyright (C) 2005, 2006 Richard J. Moore <[email protected]>
5  Copyright (C) 2005, 2006 Erik L. Bunce <[email protected]>
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Library General Public
9  License as published by the Free Software Foundation; either
10  version 2 of the License, or (at your option) any later version.
11 
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Library General Public License for more details.
16 
17  You should have received a copy of the GNU Library General Public License
18  along with this library; see the file COPYING.LIB. If not, write to
19  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20  Boston, MA 02110-1301, USA.
21 */
22 #include "fileio.h"
23 #include <QFile>
24 #include "kjseglobal.h"
25 #include <QTemporaryFile>
26 #include <QDebug>
27 
28 using namespace KJSEmbed;
29 
30 FileIOBinding::FileIOBinding(KJS::ExecState *exec, QFile *value)
31  : ObjectBinding(exec, "File", value)
32 {
33  StaticBinding::publish(exec, this, FileIO::methods());
34  StaticBinding::publish(exec, this, ObjectFactory::methods());
35  StaticBinding::publish(exec, this, VariantFactory::methods());
36 }
37 
38 START_OBJECT_METHOD(callFileOpen, QFile)
39 result = KJS::jsBoolean(object->open((QIODevice::OpenModeFlag) KJSEmbed::extractInt(exec, args, 0)));
40 END_OBJECT_METHOD
41 
42 START_OBJECT_METHOD(callFileClose, QFile)
43 object->close();
44 END_OBJECT_METHOD
45 
46 START_OBJECT_METHOD(callFileReadLine, QFile)
47 result = KJS::jsString(object->readLine().data());
48 END_OBJECT_METHOD
49 
50 START_OBJECT_METHOD(callFileReadAll, QFile)
51 result = KJS::jsString(object->readAll().data());
52 END_OBJECT_METHOD
53 
54 START_OBJECT_METHOD(callFileWriteLine, QFile)
55 result = KJS::jsNumber((long int)object->write(KJSEmbed::extractQByteArray(exec, args, 0) + '\n'));
56 END_OBJECT_METHOD
57 
58 START_OBJECT_METHOD(callFileAtEnd, QFile)
59 result = KJS::jsBoolean(object->atEnd());
60 END_OBJECT_METHOD
61 
62 START_STATIC_OBJECT_METHOD(callOpenFile)
63 QFile *file = new QFile(KJSEmbed::extractQString(exec, args, 0));
64 if (file->open((QIODevice::OpenModeFlag) KJSEmbed::extractInt(exec, args, 0)))
65 {
66  return new KJSEmbed::FileIOBinding(exec, file);
67 }
68 delete file;
69 KJS::throwError(exec, KJS::TypeError, i18n("Could not open file '%1'", KJSEmbed::extractQString(exec, args, 0)));
70 return KJS::jsNull();
71 END_STATIC_OBJECT_METHOD
72 
73 START_STATIC_OBJECT_METHOD(callRemoveFile)
74 return KJS::jsBoolean(QFile::remove(KJSEmbed::extractQString(exec, args, 0)));
75 END_STATIC_OBJECT_METHOD
76 
77 START_STATIC_OBJECT_METHOD(callCopyFile)
78 return KJS::jsBoolean(QFile::copy(KJSEmbed::extractQString(exec, args, 0), KJSEmbed::extractQString(exec, args, 0)));
79 END_STATIC_OBJECT_METHOD
80 
81 START_STATIC_OBJECT_METHOD(callMoveFile)
82 if (QFile::copy(KJSEmbed::extractQString(exec, args, 0), KJSEmbed::extractQString(exec, args, 0)))
83 {
84  return KJS::jsBoolean(QFile::remove(KJSEmbed::extractQString(exec, args, 0)));
85 }
86 return KJS::jsBoolean(false);
87 END_STATIC_OBJECT_METHOD
88 
89 START_STATIC_OBJECT_METHOD(callLinkFile)
90 return KJS::jsBoolean(QFile::link(KJSEmbed::extractQString(exec, args, 0), KJSEmbed::extractQString(exec, args, 0)));
91 END_STATIC_OBJECT_METHOD
92 
93 START_STATIC_OBJECT_METHOD(callExistsFile)
94 return KJS::jsBoolean(QFile::exists(KJSEmbed::extractQString(exec, args, 0)));
95 END_STATIC_OBJECT_METHOD
96 
97 START_STATIC_OBJECT_METHOD(callTempFile)
98 QTemporaryFile *file = new QTemporaryFile(KJSEmbed::extractQString(exec, args, 0));
99 file->setAutoRemove(KJSEmbed::extractBool(exec, args, 1));
100 
101 if (file->open())
102 {
103  return new KJSEmbed::FileIOBinding(exec, file);
104 }
105 delete file;
106 KJS::throwError(exec, KJS::GeneralError, i18n("Could not create temporary file."));
107 END_STATIC_OBJECT_METHOD
108 
109 START_STATIC_METHOD_LUT(FileIO)
110 {"openfile", 2, KJS::DontDelete | KJS::ReadOnly, &callOpenFile },
111 {"remove", 1, KJS::DontDelete | KJS::ReadOnly, &callRemoveFile },
112 {"copy", 2, KJS::DontDelete | KJS::ReadOnly, &callCopyFile },
113 {"move", 2, KJS::DontDelete | KJS::ReadOnly, &callMoveFile },
114 {"link", 2, KJS::DontDelete | KJS::ReadOnly, &callLinkFile },
115 {"exists", 2, KJS::DontDelete | KJS::ReadOnly, &callExistsFile },
116 {"tempfile", 2, KJS::DontDelete | KJS::ReadOnly, &callTempFile }
117 END_METHOD_LUT
118 
119 START_ENUM_LUT(FileIO)
120 {"ReadOnly", QIODevice::ReadOnly },
121 {"WriteOnly", QIODevice::WriteOnly },
122 {"ReadWrite", QIODevice::ReadWrite }
123 END_ENUM_LUT
124 
125 START_METHOD_LUT(FileIO)
126 {"open", 1, KJS::DontDelete | KJS::ReadOnly, &callFileOpen },
127 {"close", 0, KJS::DontDelete | KJS::ReadOnly, &callFileClose },
128 {"readln", 0, KJS::DontDelete | KJS::ReadOnly, &callFileReadLine },
129 {"readAll", 0, KJS::DontDelete | KJS::ReadOnly, &callFileReadAll },
130 {"writeln", 1, KJS::DontDelete | KJS::ReadOnly, &callFileWriteLine },
131 {"atEnd", 0, KJS::DontDelete | KJS::ReadOnly, &callFileAtEnd }
132 END_METHOD_LUT
133 
134 START_CTOR(FileIO, File, 1)
135 return new KJSEmbed::FileIOBinding(exec, new QFile(KJSEmbed::extractQString(exec, args, 0)));
136 END_CTOR
137 
bool remove()
virtual bool open(QIODevice::OpenMode mode) override
bool copy(const QString &newName)
bool exists() const const
QString i18n(const char *text, const TYPE &arg...)
static void publish(KJS::ExecState *exec, KJS::JSObject *object, const Method *methods)
Publishes an array of Methods to an object.
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Sun Dec 10 2023 03:59:19 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.