compile
[kdegraphics.git] / okular / core / script / kjs_util.cpp
blob99d329bb8cad4891e70c941027e1051f6f971d3c
1 /***************************************************************************
2 * Copyright (C) 2008 by Pino Toscano <pino@kde.org> *
3 * Copyright (C) 2008 by Harri Porten <porten@kde.org> *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 ***************************************************************************/
11 #include "kjs_util_p.h"
13 #include <kjs/kjsobject.h>
14 #include <kjs/kjsprototype.h>
15 #include <kjs/kjsarguments.h>
17 #include <kurl.h>
19 using namespace Okular;
21 static KJSPrototype *g_utilProto;
23 static KJSObject crackURL( KJSContext *context, void *,
24 const KJSArguments &arguments )
26 if ( arguments.count() < 1 )
28 return context->throwException( "Missing URL argument" );
30 QString cURL = arguments.at( 0 ).toString( context );
31 KUrl url( cURL );
32 if ( !url.isValid() )
34 return context->throwException( "Invalid URL" );
36 if ( url.protocol() != QLatin1String( "file" )
37 || url.protocol() != QLatin1String( "http" )
38 || url.protocol() != QLatin1String( "https" ) )
40 return context->throwException( "Protocol not valid: '" + url.protocol() + '\'' );
43 KJSObject obj;
44 obj.setProperty( context, "cScheme", url.protocol() );
45 if ( url.hasUser() )
46 obj.setProperty( context, "cUser", url.user() );
47 if ( url.hasPass() )
48 obj.setProperty( context, "cPassword", url.password() );
49 obj.setProperty( context, "cHost", url.host() );
50 obj.setProperty( context, "nPort", url.port( 80 ) );
51 // TODO cPath (Optional) The path portion of the URL.
52 // TODO cParameters (Optional) The parameter string portion of the URL.
53 if ( url.hasRef() )
54 obj.setProperty( context, "cFragments", url.ref() );
56 return obj;
59 void JSUtil::initType( KJSContext *ctx )
61 static bool initialized = false;
62 if ( initialized )
63 return;
64 initialized = true;
66 g_utilProto = new KJSPrototype();
67 g_utilProto->defineFunction( ctx, "crackURL", crackURL );
70 KJSObject JSUtil::object( KJSContext *ctx )
72 return g_utilProto->constructObject( ctx, 0 );