Commit 56e52eaf authored by Joao Oliveira's avatar Joao Oliveira Committed by Albert Astals Cid

Support app.alert and util.printd

parent 25559542
......@@ -20,6 +20,8 @@
#include <QTimer>
#include <KLocalizedString>
#include <QMessageBox>
#include <QCheckBox>
#include "../document_p.h"
#include "../scripter.h"
......@@ -156,6 +158,113 @@ static KJSObject appGetViewerVersion( KJSContext *, void * )
return KJSNumber( fake_acroversion );
}
/*
Alert function defined in the reference, it shows a Dialog Box with options.
app.alert()
*/
static KJSObject appAlert( KJSContext *context, void *,
const KJSArguments &arguments )
{
if ( arguments.count() < 1 )
{
return context->throwException( i18n( "Missing alert type") );
}
QString cMsg = arguments.at( 0 ).toString( context );
int nIcon = 0;
int nType = 0;
QString cTitle = "Okular";
if( arguments.count() >= 2 )
nIcon = arguments.at( 1 ).toInt32( context );
if( arguments.count() >= 3 )
nType = arguments.at( 2 ).toInt32( context );
if( arguments.count() >= 4 )
cTitle = arguments.at( 3 ).toString( context );
QMessageBox::Icon icon;
switch( nIcon )
{
case 0:
icon = QMessageBox::Critical;
break;
case 1:
icon = QMessageBox::Warning;
break;
case 2:
icon = QMessageBox::Question;
break;
case 3:
icon = QMessageBox::Information;
break;
}
QMessageBox box( icon, cTitle, cMsg );
switch( nType )
{
case 0:
box.setStandardButtons( QMessageBox::Ok );
break;
case 1:
box.setStandardButtons( QMessageBox::Ok | QMessageBox::Cancel );
break;
case 2:
box.setStandardButtons( QMessageBox::Yes | QMessageBox::No );
break;
case 3:
box.setStandardButtons( QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel );
break;
}
QCheckBox *checkBox = nullptr;
KJSObject oCheckbox;
if( arguments.count() >= 6 )
{
oCheckbox = arguments.at( 5 );
KJSObject oMsg = oCheckbox.property( context, "cMsg" );
QString msg = i18n( "Do not show this message again" );
if( oMsg.isString() )
msg = oMsg.toString( context );
bool bInitialValue = false;
KJSObject value = oCheckbox.property( context, "bInitialValue" );
if( value.isBoolean() )
bInitialValue = value.toBoolean( context );
checkBox = new QCheckBox( msg );
checkBox->setChecked( bInitialValue );
box.setCheckBox( checkBox );
}
int button = box.exec();
int ret;
switch( button )
{
case QMessageBox::Ok:
ret = 1;
break;
case QMessageBox::Cancel:
ret = 2;
break;
case QMessageBox::No:
ret = 3;
break;
case QMessageBox::Yes:
ret = 4;
break;
}
if( arguments.count() >= 6 )
oCheckbox.setProperty( context, QStringLiteral( "bAfterValue" ), checkBox->isChecked() );
delete checkBox;
return KJSNumber( ret );
}
static KJSObject appBeep( KJSContext *context, void *,
const KJSArguments &arguments )
{
......@@ -296,6 +405,7 @@ void JSApp::initType( KJSContext *ctx )
g_appProto->defineProperty( ctx, QStringLiteral("viewerVariation"), appGetViewerVariation );
g_appProto->defineProperty( ctx, QStringLiteral("viewerVersion"), appGetViewerVersion );
g_appProto->defineFunction( ctx, QStringLiteral("alert"), appAlert );
g_appProto->defineFunction( ctx, QStringLiteral("beep"), appBeep );
g_appProto->defineFunction( ctx, QStringLiteral("getNthPlugInName"), appGetNthPlugInName );
g_appProto->defineFunction( ctx, QStringLiteral("goBack"), appGoBack );
......
......@@ -15,6 +15,10 @@
#include <kjs/kjsarguments.h>
#include <QUrl>
#include <QLocale>
#include <QDebug>
#include <QRegularExpression>
#include <QDateTime>
using namespace Okular;
......@@ -56,6 +60,34 @@ static KJSObject crackURL( KJSContext *context, void *,
return obj;
}
static KJSObject printd( KJSContext *context, void *,
const KJSArguments &arguments )
{
if ( arguments.count() < 2 )
{
return context->throwException( QStringLiteral("Invalid arguments") );
}
QLocale locale( "en_US" );
QString format = arguments.at( 0 ).toString( context ).replace( "tt", "ap" );
format.replace( "t", "a" );
QStringList str = arguments.at( 1 ).toString( context ).split( QRegularExpression( "\\W") );
QString myStr = QStringLiteral( "%1/%2/%3 %4:%5:%6" ).arg( str[1] ).
arg( str[2] ).arg( str[3] ).arg( str[4] ).arg( str[5] ).arg( str[6] );
QDateTime date = locale.toDateTime( myStr, QStringLiteral( "MMM/d/yyyy H:m:s" ) );
for( int i = 0 ; i < format.size() ; ++i )
{
if( format[i] == 'M' )
format[i] = 'm';
else if( format[i] == 'm' )
format[i] = 'M';
}
return KJSString( date.toString( format ) );
}
void JSUtil::initType( KJSContext *ctx )
{
static bool initialized = false;
......@@ -65,6 +97,7 @@ void JSUtil::initType( KJSContext *ctx )
g_utilProto = new KJSPrototype();
g_utilProto->defineFunction( ctx, QStringLiteral("crackURL"), crackURL );
g_utilProto->defineFunction( ctx, QStringLiteral("printd"), printd );
}
KJSObject JSUtil::object( KJSContext *ctx )
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment