#!/usr/bin/env ruby1.8

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

require 'getopts'
getopts('r:hvimp')

case File.basename $0
when "rbqtapi"
   require 'Qt'
when "rbkdeapi"
   require 'Korundum'
end

if $OPT_v
    # TODO add and use version number #{Qt::VERSION}
    print "qtruby using Qt-#{Qt::version}\n"
    exit 0 
elsif $OPT_h
    print $usage
    exit 0
end

if $OPT_m
    while 1
        line = STDIN.readline.chomp
        line.gsub!(/^Q(?=[A-Z])/,'Qt::')
        line.gsub!(/^K/,'KDE::') unless line =~ /^(KDE)|(KIO)|(KParts)|(KNS)/
        classid = Qt::Internal::find_pclassid($_)
        puts "__START__"
        if classid
            a = Qt::Internal::findAllMethods(classid)
            ids = (a.keys.sort.map{|k|a[k]}).flatten
            candidates = Qt::dumpCandidates(ids)
            sup = []
            Qt::Internal::getAllParents(classid, sup)
            sup.each {
                |sup_item|
                a = Qt::Internal::findAllMethods(sup_item)
                ids = (a.keys.sort.map{|k|a[k]}).flatten
                candidates << Qt::Internal::dumpCandidates(ids)
            }
            candidates.gsub("\t","") # erm. whats the "s" mean on s/\t//gs ?
            print candidates
        end
        puts "__END__"
    end
end

search_string = ARGV[0] ? ARGV[0].dup : nil
search_string.gsub!(/^Q(?=[A-Z])/,'Qt::') if search_string
# search_string.gsub!(/^K(?=[^D][^E])/,'KDE::') if search_string
search_string.gsub!(/^K/,'KDE::') unless search_string.nil? or search_string =~ /^(KDE)|(KIO)|(KParts)|(KNS)/
classid = search_string ? Qt::Internal::find_pclassid(search_string) : 1
if classid == 0
    puts "Class #{search_string} not found"
    exit 1
end
regexp = nil
regexp = ( $OPT_i ? Regexp.new($OPT_r, Regexp::IGNORECASE) : Regexp.new($OPT_r) ) if $OPT_r
candidates = ""
while true
    a = Qt::Internal::findAllMethods(classid)
    break if a.nil? 
    ids = (a.keys.sort.map{|k|a[k]}).flatten
    candidates = Qt::Internal::dumpCandidates(ids)
    if $OPT_p and !search_string.empty? and classid
	sup = []
        Qt::Internal::getAllParents(classid, sup)
	sup.each {
	    |sup_item|
            a = Qt::Internal::findAllMethods(sup_item)
            ids = (a.keys.sort.map{|k|a[k]}).flatten
            candidates << Qt::Internal::dumpCandidates(ids)
        }
    end
    if regexp
	candidates.split("\n").each {
	    |candidate|
	    puts candidate if candidate =~ regexp
	}
    else
	print candidates
    end
    break unless search_string.nil?
    classid += 1
end

BEGIN {
$usage = <<USAGE
rbqtapi - a qtruby introspection tool\t(c) Germain Garand 2003 <germain\@ebooksfrance.org>

usage: rbqtapi [-r <re>] [<class>]

options:
\t-r <re> : find all functions matching regular expression/keyword <re>
\t-i : together with -r, performs a case insensitive search
\t-p : display also inherited methods for <class>.
\t-v : print qtruby and Qt versions
\t-h : print this help message
USAGE
}
