extensions.cpp 3.62 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/***************************************************************************
 *   Copyright (C) 2002 by Wilco Greven <greven@kde.org>                   *
 *   Copyright (C) 2008 by Pino Toscano <pino@kde.org>                     *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 ***************************************************************************/

#include "extensions.h"

// local includes
#include "part.h"

16 17 18
namespace Okular
{

19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
/*
 * BrowserExtension class
 */
BrowserExtension::BrowserExtension(Part* parent)
    : KParts::BrowserExtension( parent ), m_part( parent )
{
    emit enableAction("print", true);
    setURLDropHandlingEnabled(true);
}


void BrowserExtension::print()
{
    m_part->slotPrint();
}


/*
 * OkularLiveConnectExtension class
 */
#define OKULAR_EVAL_RES_OBJ_NAME "__okular_object"
#define OKULAR_EVAL_RES_OBJ "this." OKULAR_EVAL_RES_OBJ_NAME

OkularLiveConnectExtension::OkularLiveConnectExtension( Part *parent )
    : KParts::LiveConnectExtension( parent ), m_inEval( false )
{
}


bool OkularLiveConnectExtension::get( const unsigned long objid, const QString &field,
                                      KParts::LiveConnectExtension::Type &type,
                                      unsigned long &retobjid, QString &value )
{
    Q_UNUSED( value )
    retobjid = objid;
    bool result = false;
    if ( field == QLatin1String( "postMessage" ) )
    {
         type = KParts::LiveConnectExtension::TypeFunction;
         result = true;
    }
    return result;
}


bool OkularLiveConnectExtension::put( const unsigned long objid, const QString &field, const QString &value )
{
    Q_UNUSED( objid )
    if ( m_inEval )
    {
        if ( field == QLatin1String( OKULAR_EVAL_RES_OBJ_NAME ) )
           m_evalRes = value;
        return true;
    }

    return false;
}


bool OkularLiveConnectExtension::call( const unsigned long objid, const QString &func, const QStringList &args,
                                       KParts::LiveConnectExtension::Type &type, unsigned long &retobjid, QString &value )
{
    retobjid = objid;
    bool result = false;
    if ( func == QLatin1String( "postMessage" ) )
    {
        type = KParts::LiveConnectExtension::TypeVoid;
        postMessage( args );
        value = QString();
        result = true;
    }
    return result;
}


QString OkularLiveConnectExtension::eval( const QString &script )
{
    KParts::LiveConnectExtension::ArgList args;
    args.append( qMakePair( KParts::LiveConnectExtension::TypeString, script ) );
    m_evalRes.clear();
    m_inEval = true;
Laurent Montel's avatar
Laurent Montel committed
100
    emit partEvent( 0, QStringLiteral("eval"), args );
101 102 103 104 105 106 107 108 109 110 111
    m_inEval = false;
    return m_evalRes;
}


void OkularLiveConnectExtension::postMessage( const QStringList &args )
{
    QStringList arrayargs;
    Q_FOREACH ( const QString &arg, args )
    {
        QString newarg = arg;
112 113
        newarg.replace( QLatin1Char('\''), QLatin1String("\\'") );
        arrayargs.append( QLatin1Char('"') + newarg + QLatin1Char('"') );
114
    }
115 116 117
    const QString arrayarg = QLatin1Char('[') + arrayargs.join( QStringLiteral(", ") ) + QLatin1Char(']');
    eval( QStringLiteral("if (this.messageHandler && typeof this.messageHandler.onMessage == 'function') "
          "{ this.messageHandler.onMessage(") + arrayarg + QStringLiteral(") }") );
118 119
}

120
}
121

122
#include "moc_extensions.cpp"
123 124

/* kate: replace-tabs on; indent-width 4; */