#!/usr/bin/perl -w

eval 'exec /usr/bin/perl -w -S $0 ${1+"$@"}'
    if 0; # not running under some shell

eval 'exec /usr/bin/perl -w -S $0 ${1+"$@"}'
    if 0; # not running under some shell

# Note: this program is part of PerlQt and makes use of its internal functions.
#       You should not rely on those in your own programs.

#
# Copyright (C) Germain Garand 2003,2004 (germain@ebooksfrance.org)
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2
# as published by the Free Software Foundation.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#

BEGIN {
    join(" ", @ARGV) =~ /\s--\s|$/;
    (my $r = $`) =~ /(?:^|\s*)-[hvimplL]*k[hvimplL]*(?:\s|$)/ || $0 =~ /pqkapi$/ and do { require KDE }
}

use Getopt::Std;
use strict 'vars';

our(%o, @x, $h);
getopts('r:hvkimplL', \%o) or die $h;

package Qt::_internal;
use Qt;

exists $o{'v'} and do{ print "PerlQt-$Qt::VERSION using Qt-".Qt::GlobalSpace::qVersion()."\n" and exit 0 };
exists $o{'h'} and do{ print $h and exit 0 };
exists $o{'m'} and do{      # interactive mode for driving the Qt Designer Plugin
    select(STDOUT); $| = 1; # unbuffered
    while(<STDIN>)
    {
        chomp;
        s/^Q(?=[A-Z])/Qt::/ or s/^(?:K(?:DE::)?)?(?=[A-Z])/KDE::/ unless $_ eq "Qt";
        my $i = find_pclassid( $_ );
        print "__START__\n";
        if ($i)
        {
            my $a = findAllMethods( $i );
            my $t = dumpCandidates( [map {@{ $$a{$_} }} sort keys %$a] );
            getAllParents($i, \my @sup);
            for my $s(@sup)
            {
                $a = findAllMethods( $s );
                $t.= dumpCandidates( [map {@{ $$a{$_} }} sort keys %$a] );
            }
            $t =~ s/\t//gs;
            print $t;
        }
        print "__END__\n";
    }
};
my $c = shift;
$c =~ s/^Q(?=[A-Z])/Qt::/ or $c =~ s/^(?:K(?:DE::)?)?(?=[A-Z])/KDE::/ unless !defined $c or $c eq "Qt";
my $r = exists $o{'r'} ? (exists $o{'i'} ? qr|$o{'r'}|i : qr|$o{'r'}|) : 0;
exists $o{'l'} || exists $o{'L'} and do {
    my @c = keys %Qt::_internal::CppName;
    my $s = ! exists $o{'L'} ?
        sub {print "\t$_\n"} : 
        sub {print "\t$Qt::_internal::CppName{$_}\n"};
    $c and @c = grep {$c eq $_} @c;
    $r and @c = grep {$_ =~ $r} @c;
    map { &$s } @c;
    exit 0;
};
my $i = $c ? find_pclassid( $c ) : 1;
my $d = "";

while ($i)
{
   my $a=findAllMethods($i);
   last unless keys %$a;
   @x=map {@{ $$a{$_} }} sort keys %$a;
   $d = dumpCandidates(\@x);
   if($c and $i and exists $o{'p'})
   {
        getAllParents($i, \my @sup);
        for my $s(@sup)
        {
            $a = findAllMethods( $s );
            $d.= dumpCandidates( [map {@{ $$a{$_} }} sort keys %$a] );
        }
   }
   if($r)
   {
       map { print "$_\n" if $_=~$r } split("\n", $d);
   }
   else
   {
       print $d
   }
   $c and last;
   $i++
}

BEGIN {
    $h = "pqtapi - a PerlQt introspection tool\t(c) Germain Garand 2004 <germain\@ebooksfrance.org>\n\n".
         "usage: pqtapi [options] [<class>]\n\n".
         "options:\n". 
         "\t-r <re>	find all functions matching regular expression/keyword <re>\n".
         "\t-i	together with -r, performs a case insensitive search\n".
         "\t-p	display also inherited methods for <class>.\n".
         "\t-k	add the KDE api\n".
         "\t-l	only display matching Perl class names\n".
         "\t-L	only display matching C++ class names\n".
         "\t-v	print PerlQt and Qt versions\n".
         "\t-h	print this help message\n";
}
