#!/usr/bin/perl -w

eval 'exec /usr/bin/perl -w -S $0 ${1+"$@"}'
    if 0; # not running under some shell
###############################################################################
# Sanity check plugin for the Krazy project.                                  #
# Copyright (C) 2008 by Allen Winter <winter@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.                                         #
#                                                                             #
# 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.,     #
# 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.               #
#                                                                             #
###############################################################################

# Tests KDE source for missing ToolTips or WhatsThis in C++ files.

# Program options:
#   --help:          print one-line help message and exit
#   --version:       print one-line version information and exit
#   --priority:      report issues of the specified priority only
#   --strict:        report issues with the specified strictness level only
#   --explain:       print an explanation with solving instructions
#   --installed      file is to be installed
#   --quiet:         suppress all output messages
#   --verbose:       print the offending content

# Exits with status=0 if test condition is not present in the source;
# else exits with the number of failures encountered.

use strict;
use Tie::IxHash;
use FindBin qw($Bin);
use lib "$Bin/../../../../lib";
use Krazy::PreProcess;
use Krazy::Utils;

my($Prog) = "tipsandthis";
my($Version) = "1.2";

&parseArgs();

&Help() if &helpArg();
&Version() if &versionArg();
&Explain() if &explainArg();
if ($#ARGV != 0){ &Help(); exit 0; }

my($f) = $ARGV[0];

# open file and slurp it in (C++)
if (&fileType($f) eq "c++" && $f =~ /\.(cpp|cxx|cc)$/) {
  open(F, "$f") || die "Couldn't open $f";
} else {
  print "okay\n" if (!&quietArg());
  exit 0;
}
my(@data_lines) = <F>;
close(F);

# Remove C-style comments and #if 0 blocks from the file input
my(@lines) = RemoveIfZeroBlockC( RemoveCommentsC( @data_lines ) );

my($tcnt) = 0;
my($tlstr) = "";
my($tKcnt) = 0;
my($tKlstr) = "";
my($wcnt) = 0;
my($wlstr) = "";
my($wKcnt) = 0;
my($wKlstr) = "";

my(%Widgets);
tie %Widgets, "Tie::IxHash";
my($line,$lline,$sline);
my($type,$name,$key);
my($linecnt) = 0;
my($slinecnt);
my($hasToolTip,$hasWhatsThis);
while ($linecnt < $#lines) {
  $lline = $line;
  $line = $lines[$linecnt++];
  if ($line =~ m+//.*[Kk]razy:excludeall=.*$Prog+ ||
      $line =~ m+//.*[Kk]razy:skip+) {
    print "okay\n" if (!&quietArg());
    exit 0;
  }
  next if ($line =~ m+//.*[Kk]razy:exclude=.*$Prog+);
  $line =~ s+//.*++;  #skip C++ comments

  if ($line =~ m/new\sQ\w+/ || $line =~ m/new\sK(ComboBox|LineEdit|TabBar|TextEdit)/) {
    next if ($line =~ m/(Application|Widget|Label|List|Layout|Frame|Menu|HBox|VBox|Dir|Url|Action|Dialog|Drag|Timer|TimeLine|Editor|Adaptor|Event|Pixmap|Globals|Config|Splitter|FontMetrics|Interface|GroupBox|MimeData|ScriptEngine|Painter|GraphicsScene|SpacerItem)/);
    next if ($line =~ m/\(\s*new\s/ || $line =~ m/^\s*return\snew/);

    $sline = $line;
    $slinecnt = $linecnt;

    $name = &widgetName($line);
    $name = &widgetName($lline) if ($name eq "");
    next if ($name eq "");
    $type = &widgetType($line);
    $key = $name . $slinecnt;
#    print "key=[$key], type=[$type], name=[$name]\n";
    $Widgets{$key}{'name'} = $name;
    $Widgets{$key}{'type'} = $type;
    $Widgets{$key}{'linecnt'} = $slinecnt;
    $Widgets{$key}{'line'} = $sline;
    next;
  }
}

# for each non-kcfg widget, skip to its starting line and search for stuff.
foreach my $w (keys %Widgets) {
  $hasToolTip=0;
  $hasWhatsThis=0;

  # skip kcfg stuff, as we do the inverse check for them separately
  next if ($Widgets{$w}{'name'} =~ m/^kcfg_/);

  $linecnt = $Widgets{$w}{'linecnt'};
  while ($linecnt < $#lines) {
    $line = $lines[$linecnt++];
    $line =~ s/\[\w+]/ARRAY/;

    $hasToolTip = 1 if ($line =~ m/$Widgets{$w}{'name'}->setToolTip/);
    $hasWhatsThis = 1 if ($line =~ m/$Widgets{$w}{'name'}->setWhatsThis/);
  }
  if ( !$hasToolTip) {
    $tcnt++;
    if ($tcnt == 1) {
      $tlstr = "toolTip missing line\#" . $Widgets{$w}{'linecnt'};
    } else {
      $tlstr = $tlstr . "," . $Widgets{$w}{'linecnt'};
    }
    $tlstr .= " [$Widgets{$w}{'type'} $Widgets{$w}{'name'}]";
    print "=> $Widgets{$w}{'line'}" if (&verboseArg());
  }
  if ( !$hasWhatsThis) {
    $wcnt++;
    if ($wcnt == 1) {
      $wlstr = "whatsThis missing line\#" . $Widgets{$w}{'linecnt'};
    } else {
      $wlstr = $wlstr . "," . $Widgets{$w}{'linecnt'};
    }
    $wlstr .= " [$Widgets{$w}{'type'} $Widgets{$w}{'name'}]";
    print "=> $Widgets{$w}{'line'}" if (&verboseArg());
  }
}

# for each kcfg widget, skip to its starting line and search for stuff.
foreach my $w (keys %Widgets) {
  $hasToolTip=0;
  $hasWhatsThis=0;

  next if ($Widgets{$w}{'name'} !~ m/^kcfg_/);

  $linecnt = $Widgets{$w}{'linecnt'};

  while ($linecnt < $#lines) {
    $line = $lines[$linecnt++];
    $line =~ s/\[\w+]/ARRAY/;

    $hasToolTip = 1 if ($line =~ m/$Widgets{$w}{'name'}->setToolTip/);
    $hasWhatsThis = 1 if ($line =~ m/$Widgets{$w}{'name'}->setWhatsThis/);
  }
  if ($hasToolTip) {
    $tKcnt++;
    if ($tKcnt == 1) {
      $tKlstr = "kcfg has toolTip line\#" . $Widgets{$w}{'linecnt'};
    } else {
      $tKlstr = $tKlstr . "," . $Widgets{$w}{'linecnt'};
    }
    $tKlstr .= " [$Widgets{$w}{'type'} $Widgets{$w}{'name'}]";
    print "=> $Widgets{$w}{'line'}" if (&verboseArg());
  }
  if ($hasWhatsThis) {
    $wKcnt++;
    if ($wKcnt == 1) {
      $wKlstr = "kcfg has whatsThis line\#" . $Widgets{$w}{'linecnt'};
    } else {
      $wKlstr = $wKlstr . "," . $Widgets{$w}{'linecnt'};
    }
    $wKlstr .= " [$Widgets{$w}{'type'} $Widgets{$w}{'name'}]";
    print "=> $Widgets{$w}{'line'}" if (&verboseArg());
  }
}

my ($total_count) = $tcnt + $tKcnt + $wcnt + $wKcnt;
if (!$total_count) {
  print "okay\n" if (!&quietArg());
  exit 0;
} else {
  if (!&quietArg()) {
    print "$tlstr ($tcnt)\n" if ($tcnt);
    print "$wlstr ($wcnt)\n" if ($wcnt);
    print "$tKlstr ($tKcnt)\n" if ($tKcnt);
    print "$wKlstr ($wKcnt)\n" if ($wKcnt);
  }
  exit $total_count;
}

sub widgetName {
  my($l) = @_;
  my($name);

  return "" if ($l =~ m/^\s*=/ || $l =~ m/^\s*new/);
  chomp($l);
  $name = $l;
  $name =~ s/^\s*[:\w]+\s//;
  $name =~ s/\s*=.*$//;
  $name =~ s/^\s*//;
  $name =~ s/^.*\*\s*//;
  $name =~ s/^\s*\w+\s*\(\s*//;
  if ($name eq "") {
    $name = $l;
    $name =~ s/\s*=.*//;
    $name =~ s/^\s*//;
    $name =~ s/^\*//;
  }
  if ($name =~ m/->/ || $name =~ m/^\s*[{}\(\)]/) {
    $name = "";
  }
  $name =~ s/\[[%\-\+\(\)\w\s]+\]/ARRAY/;
  return $name;
}

sub widgetType {
  my($l) = @_;
  my($type);

  chomp($l);
  $type = $l;
  $type =~ s/^.*=\s*new\s//;
  $type =~ s/\(.*$//;
  $type =~ s/\s$//;
  $type =~ s/;$//;
  if ($type =~ m/\s*new\s/) {
    $type = $l;
    $type =~ s/^.*new\s//;
    $type =~ s/\(.*$//;
  }
  return $type;
}

sub Help {
  print "Check for missing tooltips or whatsThis\n";
  exit 0 if &helpArg();
}

sub Version {
  print "$Prog, version $Version\n";
  exit 0 if &versionArg();
}

sub Explain {
  print "Please provide tooltips and whatsThis strings to help the user make informed choices.\n";
  exit 0 if &explainArg();
}
