File indexing completed on 2024-05-19 11:43:15

0001 /* This file is part of the KDE libraries
0002     Copyright (C) 2005, 2006 Ian Reinhart Geiser <geiseri@kde.org>
0003     Copyright (C) 2005, 2006 Matt Broadstone <mbroadst@gmail.com>
0004     Copyright (C) 2005, 2006 Richard J. Moore <rich@kde.org>
0005     Copyright (C) 2005, 2006 Erik L. Bunce <kde@bunce.us>
0006 
0007     This library is free software; you can redistribute it and/or
0008     modify it under the terms of the GNU Library General Public
0009     License as published by the Free Software Foundation; either
0010     version 2 of the License, or (at your option) any later version.
0011 
0012     This library is distributed in the hope that it will be useful,
0013     but WITHOUT ANY WARRANTY; without even the implied warranty of
0014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0015     Library General Public License for more details.
0016 
0017     You should have received a copy of the GNU Library General Public License
0018     along with this library; see the file COPYING.LIB.  If not, write to
0019     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0020     Boston, MA 02110-1301, USA.
0021 */
0022 #include "fileio.h"
0023 #include <QFile>
0024 #include "kjseglobal.h"
0025 #include <QTemporaryFile>
0026 #include <QDebug>
0027 
0028 using namespace KJSEmbed;
0029 
0030 FileIOBinding::FileIOBinding(KJS::ExecState *exec,  QFile *value)
0031     : ObjectBinding(exec, "File", value)
0032 {
0033     StaticBinding::publish(exec, this, FileIO::methods());
0034     StaticBinding::publish(exec, this, ObjectFactory::methods());
0035     StaticBinding::publish(exec, this, VariantFactory::methods());
0036 }
0037 
0038 START_OBJECT_METHOD(callFileOpen, QFile)
0039 result = KJS::jsBoolean(object->open((QIODevice::OpenModeFlag) KJSEmbed::extractInt(exec, args, 0)));
0040 END_OBJECT_METHOD
0041 
0042 START_OBJECT_METHOD(callFileClose, QFile)
0043 object->close();
0044 END_OBJECT_METHOD
0045 
0046 START_OBJECT_METHOD(callFileReadLine, QFile)
0047 result = KJS::jsString(object->readLine().data());
0048 END_OBJECT_METHOD
0049 
0050 START_OBJECT_METHOD(callFileReadAll, QFile)
0051 result = KJS::jsString(object->readAll().data());
0052 END_OBJECT_METHOD
0053 
0054 START_OBJECT_METHOD(callFileWriteLine, QFile)
0055 result = KJS::jsNumber((long int)object->write(KJSEmbed::extractQByteArray(exec, args, 0) + '\n'));
0056 END_OBJECT_METHOD
0057 
0058 START_OBJECT_METHOD(callFileAtEnd, QFile)
0059 result = KJS::jsBoolean(object->atEnd());
0060 END_OBJECT_METHOD
0061 
0062 START_STATIC_OBJECT_METHOD(callOpenFile)
0063 QFile *file = new QFile(KJSEmbed::extractQString(exec, args, 0));
0064 if (file->open((QIODevice::OpenModeFlag) KJSEmbed::extractInt(exec, args, 0)))
0065 {
0066     return new KJSEmbed::FileIOBinding(exec, file);
0067 }
0068 delete file;
0069 KJS::throwError(exec, KJS::TypeError, i18n("Could not open file '%1'",  KJSEmbed::extractQString(exec, args, 0)));
0070 return KJS::jsNull();
0071 END_STATIC_OBJECT_METHOD
0072 
0073 START_STATIC_OBJECT_METHOD(callRemoveFile)
0074 return KJS::jsBoolean(QFile::remove(KJSEmbed::extractQString(exec, args, 0)));
0075 END_STATIC_OBJECT_METHOD
0076 
0077 START_STATIC_OBJECT_METHOD(callCopyFile)
0078 return KJS::jsBoolean(QFile::copy(KJSEmbed::extractQString(exec, args, 0), KJSEmbed::extractQString(exec, args, 0)));
0079 END_STATIC_OBJECT_METHOD
0080 
0081 START_STATIC_OBJECT_METHOD(callMoveFile)
0082 if (QFile::copy(KJSEmbed::extractQString(exec, args, 0), KJSEmbed::extractQString(exec, args, 0)))
0083 {
0084     return KJS::jsBoolean(QFile::remove(KJSEmbed::extractQString(exec, args, 0)));
0085 }
0086 return KJS::jsBoolean(false);
0087 END_STATIC_OBJECT_METHOD
0088 
0089 START_STATIC_OBJECT_METHOD(callLinkFile)
0090 return KJS::jsBoolean(QFile::link(KJSEmbed::extractQString(exec, args, 0), KJSEmbed::extractQString(exec, args, 0)));
0091 END_STATIC_OBJECT_METHOD
0092 
0093 START_STATIC_OBJECT_METHOD(callExistsFile)
0094 return KJS::jsBoolean(QFile::exists(KJSEmbed::extractQString(exec, args, 0)));
0095 END_STATIC_OBJECT_METHOD
0096 
0097 START_STATIC_OBJECT_METHOD(callTempFile)
0098 QTemporaryFile *file = new QTemporaryFile(KJSEmbed::extractQString(exec, args, 0));
0099 file->setAutoRemove(KJSEmbed::extractBool(exec, args, 1));
0100 
0101 if (file->open())
0102 {
0103     return new KJSEmbed::FileIOBinding(exec, file);
0104 }
0105 delete file;
0106 KJS::throwError(exec, KJS::GeneralError, i18n("Could not create temporary file."));
0107 END_STATIC_OBJECT_METHOD
0108 
0109 START_STATIC_METHOD_LUT(FileIO)
0110 {"openfile", 2, KJS::DontDelete | KJS::ReadOnly, &callOpenFile },
0111 {"remove", 1, KJS::DontDelete | KJS::ReadOnly, &callRemoveFile },
0112 {"copy", 2, KJS::DontDelete | KJS::ReadOnly, &callCopyFile },
0113 {"move", 2, KJS::DontDelete | KJS::ReadOnly, &callMoveFile },
0114 {"link", 2, KJS::DontDelete | KJS::ReadOnly, &callLinkFile },
0115 {"exists", 2, KJS::DontDelete | KJS::ReadOnly, &callExistsFile },
0116 {"tempfile", 2, KJS::DontDelete | KJS::ReadOnly, &callTempFile }
0117 END_METHOD_LUT
0118 
0119 START_ENUM_LUT(FileIO)
0120 {"ReadOnly", QIODevice::ReadOnly },
0121 {"WriteOnly", QIODevice::WriteOnly },
0122 {"ReadWrite", QIODevice::ReadWrite }
0123 END_ENUM_LUT
0124 
0125 START_METHOD_LUT(FileIO)
0126 {"open", 1, KJS::DontDelete | KJS::ReadOnly, &callFileOpen },
0127 {"close", 0, KJS::DontDelete | KJS::ReadOnly, &callFileClose },
0128 {"readln", 0, KJS::DontDelete | KJS::ReadOnly, &callFileReadLine },
0129 {"readAll", 0, KJS::DontDelete | KJS::ReadOnly, &callFileReadAll },
0130 {"writeln", 1, KJS::DontDelete | KJS::ReadOnly, &callFileWriteLine },
0131 {"atEnd", 0, KJS::DontDelete | KJS::ReadOnly, &callFileAtEnd }
0132 END_METHOD_LUT
0133 
0134 START_CTOR(FileIO, File, 1)
0135 return new KJSEmbed::FileIOBinding(exec, new QFile(KJSEmbed::extractQString(exec, args, 0)));
0136 END_CTOR
0137