#!/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-2010 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.63";

&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($tAcnt) = 0;
my($tAlstr) = "";
my($wcnt) = 0;
my($wlstr) = "";
my($wKcnt) = 0;
my($wKlstr) = "";
my($wAcnt) = 0;
my($wAlstr) = "";
my($sAcnt) = 0;
my($sAlstr) = "";
my($hAcnt) = 0;
my($hAlstr) = "";

my(%Widgets);
tie %Widgets, "Tie::IxHash";
my($line,$lline,$sline);
my($type,$name,$key);
my($linecnt) = 0;
my($slinecnt);
my($hasToolTip,$hasWhatsThis,$hasStatusTip,$hasHelpText,$isSep);
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\s(KPIM::)*K(UrlRequester|ComboBox|LineEdit|TabBar|TextEdit|TimeEdit|DateEdit|Action)/) {
    next if ($line =~ m/(Application|Buffer|Date|DBus|Hash|Map|Process|Widget|Label|List|Layout|Frame|Menu|HBox|VBox|Dir|Url|Action|ActionCollection|Dialog|Drag|Time|Timer|TimeLine|Editor|Adaptor|Event|Pixmap|Globals|Config|Splitter|FontMetrics|Interface|GroupBox|MimeData|ScriptEngine|Painter|GraphicsScene|Shortcut|SpacerItem|QPersistentModelIndex|QGraphicsItemAnimation|QGraphicsSimpleTextItem|SignalMapper|ScrollArea|ItemSelection|StandardItem|SortFilterProxyModel|MessageBox)/ && $line !~ m/(UrlRequester|LineEdit|ComboBox|TimeEdit|DateEdit|KAction)/);
    next if ($line =~ m/KActionCollection/);
    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_/);

  # skip KAction, as we do a different check for them separately
  next if ($Widgets{$w}{'type'} =~ m/KAction/);

  $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());
  }
}

# for each KAction, skip to its starting line and search for stuff.
# dfaure says:
#   "Ignore setWhatsThis - if it's called explicitely then it has a longer text,
#   that's fine. A krazy check could look for a variable of type KAction* that
#   is used like action->setToolTip(). If there is no action->setStatusTip(),
#   then this would be better off with setHelpText in order to provide both
#   in one go. ... it doesn't hurt to port setToolTip to setHelpText in all
#   cases where setStatusTip isn't called explicitly."
foreach my $w (keys %Widgets) {
  $hasToolTip=0;
  $hasWhatsThis=0;
  $hasStatusTip=0;
  $hasHelpText=0;
  $isSep=0;

  next if ($Widgets{$w}{'type'} ne "KAction");

  $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/);
    $hasStatusTip = 1 if ($line =~ m/$Widgets{$w}{'name'}->setStatusTip/);
    $hasHelpText = 1 if ($line =~ m/$Widgets{$w}{'name'}->setHelpText/);
    $isSep = 1 if ($line =~ m/$Widgets{$w}{'name'}->setSeparator/);
  }

  next if( $isSep );

  if (!$hasWhatsThis) {
    $wAcnt++;
    if ($wAcnt == 1) {
      $wAlstr = "KAction missing whatsThis line\#" . $Widgets{$w}{'linecnt'};
    } else {
      $wAlstr = $wAlstr . "," . $Widgets{$w}{'linecnt'};
    }
    $wAlstr .= " [$Widgets{$w}{'type'} $Widgets{$w}{'name'}]";
    print "=> $Widgets{$w}{'line'}" if (&verboseArg());
  }

  next if ($hasHelpText);

  if ($hasToolTip) {
    if (!$hasStatusTip) {
      #port setToolTip to setHelpText
      $sAcnt++;
      if ($sAcnt == 1) {
        $sAlstr = "KAction toolTip should be ported to helpText line\#" . $Widgets{$w}{'linecnt'};
      } else {
        $sAlstr = $sAlstr . "," . $Widgets{$w}{'linecnt'};
      }
      $sAlstr .= " [$Widgets{$w}{'type'} $Widgets{$w}{'name'}]";
      print "=> $Widgets{$w}{'line'}" if (&verboseArg());
    } else {
      #aok
    }
  } else {
    if (!$hasStatusTip){
      #missing setHelpText
      $hAcnt++;
      if ($hAcnt == 1) {
        $hAlstr = "KAction missing helpText line\#" . $Widgets{$w}{'linecnt'};
      } else {
        $hAlstr = $hAlstr . "," . $Widgets{$w}{'linecnt'};
      }
      $hAlstr .= " [$Widgets{$w}{'type'} $Widgets{$w}{'name'}]";
      print "=> $Widgets{$w}{'line'}" if (&verboseArg());
    } else {
      #missing setToolTip
      $tAcnt++;
      if ($tAcnt == 1) {
        $tAlstr = "KAction missing toolTip (or convert statusTip to helpText) line\#" . $Widgets{$w}{'linecnt'};
      } else {
        $tAlstr = $tAlstr . "," . $Widgets{$w}{'linecnt'};
      }
      $tAlstr .= " [$Widgets{$w}{'type'} $Widgets{$w}{'name'}]";
      print "=> $Widgets{$w}{'line'}" if (&verboseArg());
    }
  }
}

my ($total_count) =
  $tcnt + $wcnt +
  $tKcnt + $wKcnt +
  $tAcnt + $sAcnt + $hAcnt + $wAcnt;

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);

    print "$tAlstr ($tAcnt)\n" if ($tAcnt);
    print "$sAlstr ($sAcnt)\n" if ($sAcnt);
    print "$hAlstr ($hAcnt)\n" if ($hAcnt);
    print "$wAlstr ($wAcnt)\n" if ($wAcnt);
  }
  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.  KAction's should use setStatusTip instead of setToolTip.\n";
  Exit 0 if &explainArg();
}
