• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 API Reference
  • KDE Home
  • Contact Us
 

kjsembed

  • kjsembed
  • kjsembed
fileio.cpp
Go to the documentation of this file.
1/* This file is part of the KDE libraries
2 Copyright (C) 2005, 2006 Ian Reinhart Geiser <geiseri@kde.org>
3 Copyright (C) 2005, 2006 Matt Broadstone <mbroadst@gmail.com>
4 Copyright (C) 2005, 2006 Richard J. Moore <rich@kde.org>
5 Copyright (C) 2005, 2006 Erik L. Bunce <kde@bunce.us>
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 <QtCore/QFile>
24#include "kjseglobal.h"
25#include <QtCore/QTemporaryFile>
26#include <QtCore/QDebug>
27
28using namespace KJSEmbed;
29
30FileIOBinding::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
38START_OBJECT_METHOD( callFileOpen, QFile )
39 result = KJS::jsBoolean( object->open( (QIODevice::OpenModeFlag) KJSEmbed::extractInt(exec, args, 0)));
40END_OBJECT_METHOD
41
42START_OBJECT_METHOD( callFileClose, QFile )
43 object->close();
44END_OBJECT_METHOD
45
46START_OBJECT_METHOD( callFileReadLine, QFile )
47 result = KJS::jsString( object->readLine().data() );
48END_OBJECT_METHOD
49
50START_OBJECT_METHOD( callFileReadAll, QFile )
51 result = KJS::jsString( object->readAll().data() );
52END_OBJECT_METHOD
53
54START_OBJECT_METHOD( callFileWriteLine, QFile )
55 result = KJS::jsNumber( (long int)object->write(KJSEmbed::extractQByteArray(exec, args, 0) + '\n') );
56END_OBJECT_METHOD
57
58START_OBJECT_METHOD( callFileAtEnd, QFile )
59 result = KJS::jsBoolean(object->atEnd());
60END_OBJECT_METHOD
61
62START_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();
71END_STATIC_OBJECT_METHOD
72
73START_STATIC_OBJECT_METHOD( callRemoveFile )
74 return KJS::jsBoolean( QFile::remove(KJSEmbed::extractQString( exec, args, 0) ));
75END_STATIC_OBJECT_METHOD
76
77START_STATIC_OBJECT_METHOD( callCopyFile )
78 return KJS::jsBoolean( QFile::copy(KJSEmbed::extractQString( exec, args, 0),KJSEmbed::extractQString( exec, args, 0) ));
79END_STATIC_OBJECT_METHOD
80
81START_STATIC_OBJECT_METHOD( callMoveFile )
82 if( QFile::copy(KJSEmbed::extractQString( exec, args, 0),KJSEmbed::extractQString( exec, args, 0) ) )
83 return KJS::jsBoolean( QFile::remove(KJSEmbed::extractQString( exec, args, 0) ) );
84 return KJS::jsBoolean(false);
85END_STATIC_OBJECT_METHOD
86
87START_STATIC_OBJECT_METHOD( callLinkFile )
88 return KJS::jsBoolean( QFile::link(KJSEmbed::extractQString( exec, args, 0),KJSEmbed::extractQString( exec, args, 0) ));
89END_STATIC_OBJECT_METHOD
90
91START_STATIC_OBJECT_METHOD( callExistsFile )
92 return KJS::jsBoolean( QFile::exists(KJSEmbed::extractQString( exec, args, 0) ));
93END_STATIC_OBJECT_METHOD
94
95START_STATIC_OBJECT_METHOD( callTempFile )
96 QTemporaryFile *file = new QTemporaryFile( KJSEmbed::extractQString( exec, args, 0) );
97 file->setAutoRemove( KJSEmbed::extractBool(exec, args, 1));
98
99 if( file->open() )
100 {
101 return new KJSEmbed::FileIOBinding( exec, file );
102 }
103 delete file;
104 KJS::throwError(exec, KJS::GeneralError, i18n("Could not create temporary file."));
105END_STATIC_OBJECT_METHOD
106
107START_STATIC_METHOD_LUT( FileIO )
108 {"openfile", 2, KJS::DontDelete|KJS::ReadOnly, &callOpenFile },
109 {"remove", 1, KJS::DontDelete|KJS::ReadOnly, &callRemoveFile },
110 {"copy", 2, KJS::DontDelete|KJS::ReadOnly, &callCopyFile },
111 {"move", 2, KJS::DontDelete|KJS::ReadOnly, &callMoveFile },
112 {"link", 2, KJS::DontDelete|KJS::ReadOnly, &callLinkFile },
113 {"exists", 2, KJS::DontDelete|KJS::ReadOnly, &callExistsFile },
114 {"tempfile", 2, KJS::DontDelete|KJS::ReadOnly, &callTempFile }
115END_METHOD_LUT
116
117START_ENUM_LUT( FileIO )
118 {"ReadOnly", QIODevice::ReadOnly },
119 {"WriteOnly", QIODevice::WriteOnly },
120 {"ReadWrite", QIODevice::ReadWrite }
121END_ENUM_LUT
122
123START_METHOD_LUT( FileIO )
124 {"open", 1, KJS::DontDelete|KJS::ReadOnly, &callFileOpen },
125 {"close", 0, KJS::DontDelete|KJS::ReadOnly, &callFileClose },
126 {"readln", 0, KJS::DontDelete|KJS::ReadOnly, &callFileReadLine },
127 {"readAll", 0, KJS::DontDelete|KJS::ReadOnly, &callFileReadAll },
128 {"writeln", 1, KJS::DontDelete|KJS::ReadOnly, &callFileWriteLine },
129 {"atEnd", 0, KJS::DontDelete|KJS::ReadOnly, &callFileAtEnd }
130END_METHOD_LUT
131
132START_CTOR( FileIO, File, 1 )
133 return new KJSEmbed::FileIOBinding( exec, new QFile( KJSEmbed::extractQString(exec,args,0 ) ) );
134END_CTOR
135
136//kate: indent-spaces on; indent-width 4; replace-tabs on; indent-mode cstyle;
START_METHOD_LUT
#define START_METHOD_LUT(TYPE)
Definition: binding_support.h:127
START_ENUM_LUT
#define START_ENUM_LUT(TYPE)
Definition: binding_support.h:139
END_CTOR
#define END_CTOR
Definition: binding_support.h:166
START_STATIC_METHOD_LUT
#define START_STATIC_METHOD_LUT(TYPE)
Definition: binding_support.h:131
START_CTOR
#define START_CTOR(TYPE, JSNAME, ARGS)
Definition: binding_support.h:157
END_METHOD_LUT
#define END_METHOD_LUT
Definition: binding_support.h:135
END_ENUM_LUT
#define END_ENUM_LUT
Definition: binding_support.h:143
KJSEmbed::FileIOBinding
Definition: fileio.h:36
KJSEmbed::FileIOBinding::FileIOBinding
FileIOBinding(KJS::ExecState *exec, QFile *value)
Definition: fileio.cpp:30
KJSEmbed::ObjectBinding
Definition: object_binding.h:89
KJSEmbed::ObjectFactory::methods
static const Method * methods()
Definition: object_binding.h:85
KJSEmbed::StaticBinding::publish
static void publish(KJS::ExecState *exec, KJS::JSObject *object, const Method *methods)
Publishes an array of Methods to an object.
Definition: static_binding.cpp:60
KJSEmbed::VariantFactory::methods
static const Method * methods()
Definition: variant_binding.h:251
result
END_VARIANT_METHOD result
Definition: color.cpp:85
if
if(file->open((QIODevice::OpenModeFlag) KJSEmbed::extractInt(exec, args, 0)))
Definition: fileio.cpp:64
close
END_OBJECT_METHOD object close()
file
END_OBJECT_METHOD QFile * file
Definition: fileio.cpp:63
fileio.h
kjseglobal.h
KJSEmbed
Definition: application.h:33
KJSEmbed::extractQString
QString KJSEMBED_EXPORT extractQString(KJS::ExecState *exec, const KJS::List &args, int idx, const QString defaultValue=QString())
Extracts a QString from an argument list.
Definition: binding_support.cpp:34
KJSEmbed::extractInt
int KJSEMBED_EXPORT extractInt(KJS::ExecState *exec, const KJS::List &args, int idx, int defaultValue=0)
Extracts an integer from an argument list.
Definition: binding_support.cpp:72
KJSEmbed::extractBool
bool KJSEMBED_EXPORT extractBool(KJS::ExecState *exec, const KJS::List &args, int idx, bool defaultValue=false)
Extracts a bool from an argument list.
Definition: binding_support.cpp:149
KJSEmbed::extractQByteArray
QByteArray KJSEMBED_EXPORT extractQByteArray(KJS::ExecState *exec, const KJS::List &args, int idx, const QByteArray &defaultValue=QByteArray())
Extracts a QByteArray from an argument list.
Definition: binding_support.cpp:50
KJS
Implement QString-KJS::UString conversion methods.
Definition: kjs_object_model.h:29
KJS::throwError
JSObject * throwError(ExecState *e, ErrorType t, const QString &m)
Definition: binding_support.h:241
START_OBJECT_METHOD
#define START_OBJECT_METHOD(METHODNAME, TYPE)
A simple pointer syle method.
Definition: object_binding.h:40
END_OBJECT_METHOD
#define END_OBJECT_METHOD
End a variant method started by START_OBJECT_METHOD.
Definition: object_binding.h:57
END_STATIC_OBJECT_METHOD
#define END_STATIC_OBJECT_METHOD
Definition: object_binding.h:75
START_STATIC_OBJECT_METHOD
#define START_STATIC_OBJECT_METHOD(METHODNAME)
Definition: object_binding.h:67
value
QVariant value
Definition: settings.cpp:35
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kjsembed

Skip menu "kjsembed"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdelibs-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal