#!/usr/bin/perl -w
# the CPAN-provided DCOP is awful: it uses backticks to do the heavy
# lifting, so you need to preprocess any data you hand to it to remove
# dangerous things like, well, more backticks. So I'm fixing it
# here.
package DCOP;
use strict;

our $VERSION = '0.037';

sub new() {
    my $proto  = shift;
    my $class  = ref( $proto ) || $proto;
    my %params = @_;
    my $self   = {};
    $self->{ start }    = localtime;
    $self->{ user }     = $params{ user }    if ( $params{ user } );
    $self->{ target }   = $params{ target }  if ( $params{ target } );
    $self->{ control }  = $params{ control } if ( $params{ control } );
    chomp( my $basepath = `kde-config --expandvars --exec-prefix` );
    $self->{ dcop }  = "$basepath/bin/dcop ";
    $self->{ dcop } .= "--user $self->{user} " if ( $self->{ user } );
    $self->{ dcop } .= "$self->{target} "      if ( $self->{ target } );
    $self->{ dcop } .= "$self->{control} "     if ( $self->{ control } );
    bless( $self, $class );
    return $self;
}

sub run() {
    my $self = shift;

    my $ret = "";
    my $pid = open( DCOP, "-|" );
    if ( $pid ) {
        while ( <DCOP> ) {
            $ret .= $_;
        }
        close( DCOP ) or warn "dcop exited $?";
        chomp( $ret );
    } else {
        my ( $prog, @args ) = split( ' ', $self->{dcop} );

        # this isn't entirely compatible with the old code: you'll need to
        # pass individiual args in as, well, individual args.
        push @args, @_;
        exec( $prog, @args ) or die "dcop can't exec: $!";
    }

    return $ret;
}

1;
