#include "precompiled.h" /* * wxJavaScript - protocol.cpp * * Copyright (c) 2002-2007 Franky Braem and the wxJavaScript project * * Project Info: http://www.wxjavascript.net or http://wxjs.sourceforge.net * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library 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 Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, * USA. * * $Id: protocol.cpp 598 2007-03-07 20:13:28Z fbraem $ */ #include #ifndef WX_PRECOMP #include #endif #include "../common/main.h" #include "jsstream.h" #include "sistream.h" #include "sockbase.h" #include "protocol.h" using namespace wxjs; using namespace wxjs::io; /*** * protocol * io * * Prototype for all protocol classes. * */ WXJS_INIT_CLASS(Protocol, "wxProtocol", 0) /*** * * * No error * A generic network error occurred. * An error occurred during negotiation. * The client failed to connect the server. * Invalid value. * * The remote file doesn't exist. * Last action aborted. * An error occurred during reconnection. * Someone tried to send a command during a transfer. * * wxProtocolError is ported to JavaScript as a separate class. Note that this * class doesn't exist in wxWidgets. * * * */ void Protocol::InitClass(JSContext *cx, JSObject *obj, JSObject *proto) { JSConstDoubleSpec wxProtocolErrorMap[] = { WXJS_CONSTANT(wxPROTO_, NOERR) WXJS_CONSTANT(wxPROTO_, NETERR) WXJS_CONSTANT(wxPROTO_, PROTERR) WXJS_CONSTANT(wxPROTO_, CONNERR) WXJS_CONSTANT(wxPROTO_, INVVAL) WXJS_CONSTANT(wxPROTO_, NOHNDLR) WXJS_CONSTANT(wxPROTO_, NOFILE) WXJS_CONSTANT(wxPROTO_, ABRT) WXJS_CONSTANT(wxPROTO_, RCNCT) WXJS_CONSTANT(wxPROTO_, STREAMING) { 0 } }; JSObject *constObj = JS_DefineObject(cx, obj, "wxProtocolError", NULL, NULL, JSPROP_READONLY | JSPROP_PERMANENT); JS_DefineConstDoubles(cx, constObj, wxProtocolErrorMap); } /*** * * * Returns the type of the content of the last opened stream. It is a mime-type * * * Returns the last occurred error. See @wxProtocol#wxProtocolError. * * */ WXJS_BEGIN_PROPERTY_MAP(Protocol) WXJS_READONLY_PROPERTY(P_CONTENT_TYPE, "contentType") WXJS_READONLY_PROPERTY(P_ERROR, "error") WXJS_END_PROPERTY_MAP() bool Protocol::GetProperty(SocketBasePrivate *p, JSContext *cx, JSObject *obj, int id, jsval *vp) { wxProtocol *protocol = dynamic_cast(p->GetBase()); switch(id) { case P_CONTENT_TYPE: *vp = ToJS(cx, protocol->GetContentType()); break; case P_ERROR: *vp = ToJS(cx, protocol->GetError()); break; } return true; } WXJS_BEGIN_METHOD_MAP(Protocol) WXJS_METHOD("abort", abort, 0) WXJS_METHOD("getInputStream", getInputStream, 1) WXJS_METHOD("reconnect", reconnect, 0) WXJS_METHOD("setPassword", setPassword, 1) WXJS_METHOD("setUser", setUser, 1) WXJS_END_METHOD_MAP() /*** * * * * Abort the current stream. * * */ JSBool Protocol::abort(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { SocketBasePrivate *p = GetPrivate(cx, obj); if ( p == NULL ) return JS_FALSE; wxProtocol *protocol = dynamic_cast(p->GetBase()); *rval = ToJS(cx, protocol->Abort()); return JS_TRUE; } /*** * * * * * * Creates a new input stream on the specified path. * You can use all but seek functionality of wxStream. * @wxInputStream#seekI isn't available on all stream. For example, http or * ftp streams doesn't deal with it. Other functions like * @wxStreamBase#size aren't available for the moment for * this sort of stream. You will be notified when the EOF * is reached by an error. * * */ JSBool Protocol::getInputStream(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { SocketBasePrivate *p = GetPrivate(cx, obj); if ( p == NULL ) return JS_FALSE; wxProtocol *protocol = dynamic_cast(p->GetBase()); wxString path; FromJS(cx, argv[0], path); wxInputStream *stream = protocol->GetInputStream(path); if ( stream != NULL ) { Stream *js_stream = new Stream(stream, false); p->AddStream(js_stream); *rval = SocketInputStream::CreateObject(cx, js_stream, NULL); js_stream->SetObject(JSVAL_TO_OBJECT(*rval)); } return JS_TRUE; } /*** * * * * Tries to reestablish a previous opened connection (close and renegotiate connection). * * */ JSBool Protocol::reconnect(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { SocketBasePrivate *p = GetPrivate(cx, obj); if ( p == NULL ) return JS_FALSE; wxProtocol *protocol = dynamic_cast(p->GetBase()); *rval = ToJS(cx, protocol->Reconnect()); return JS_TRUE; } /*** * * * * * * Sets the authentication password. It is mainly useful when FTP is used. * * */ JSBool Protocol::setPassword(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { SocketBasePrivate *p = GetPrivate(cx, obj); if ( p == NULL ) return JS_FALSE; wxProtocol *protocol = dynamic_cast(p->GetBase()); wxString pwd; FromJS(cx, argv[0], pwd); protocol->SetPassword(pwd); return JS_TRUE; } /*** * * * * * * Sets the authentication user. It is mainly useful when FTP is used. * * */ JSBool Protocol::setUser(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { SocketBasePrivate *p = GetPrivate(cx, obj); if ( p == NULL ) return JS_FALSE; wxProtocol *protocol = dynamic_cast(p->GetBase()); wxString user; FromJS(cx, argv[0], user); protocol->SetUser(user); return JS_TRUE; }