#!/usr/bin/perl

# This converts kde1 code to kde2.
#
# It can only handle some of the more simple mechanical changes.
# It will convert _every_ file in the current directory and below,
# but only changes the C++ code and headers. It will not change the
# makefiles.
#
# To use this script just cd to the base directory where the
# code is located and run it. I've added a command line parameter
# of the directory to convert, but I haven't tested that.
#
# Use this script at you own risk. It would be advisable to backup
# all files before use. There is absolutely no license restrictions
# on this code, but I am not responsible for any error or loss of data
# that may be caused by it's use. :-)
#
# If you add any of the missing conversions, please send me a patch
# so that it can be including for others to use. Thanks.
#
# jbb@kdevelop.org 22-Nov-2000

use File::Find;

@ARGV = ('.')   unless @ARGV;
find (\&process, ("."));

sub process()
{
    my $count = 0;
    my $file = $_;
    my $all_text = "";

    return  if $file =~ /^\.\.?$/;
    return  if ($file =~ /\.moc\.cpp$|la_meta_unload\.cpp$/);
    return  unless ($file =~ /\.cc$|\.C$|\.cpp$|\.h$|\.hh$|\.hpp$/);

    open (FILE, "<$file") or die "Opening <$file>: $!";
    $all_text .= $_ while (<FILE>);
    close FILE;

# headers
    $count += ($all_text =~ s/kpopmenu\.h/kpopupmenu\.h/mg);
    $count += ($all_text =~ s/klined\.h/klineedit\.h/mg);
    $count += ($all_text =~ s/kintegerline\.h/knuminput\.h/mg);
    $count += ($all_text =~ s/khtmlw\.h/khtml\.h/mg);
    $count += ($all_text =~ s/kpanner\.h/qsplitter\.h/mg);
    $count += ($all_text =~ s/knewpanner\.h/qsplitter\.h/mg);
    $count += ($all_text =~ s/kclipboard\.h/qclipboard\.h/mg);
    $count += ($all_text =~ s/kmsgbox\.h/kmessagebox\.h/mg);
    $count += ($all_text =~ s/kquickhelp\.h/qwhatsthis\.h/mg);
    $count += ($all_text =~ s/kspinbox\.h/qspinbox\.h/mg);
    $count += ($all_text =~ s/ktreelist\.h/klistview\.h/mg);
    $count += ($all_text =~ s/kledlamp\.h/kled\.h/mg);
    $count += ($all_text =~ s/kcombo\.h/kcombobox\.h/mg);
    $count += ($all_text =~ s/kbutton\.h/qtoolbutton\.h/mg);
    $count += ($all_text =~ s/kiconloaderdialog\.h/kicondialog\.h/mg);

# remove some headers
    $count += ($all_text =~ s/\s*#\s*include\s*[<"]*kstring\.h[>"]*\s*/\n/mg);
    $count += ($all_text =~ s/\s*#\s*include\s*[<"]*ktopwidget\.h[>"]*\s*/\n/mg);
    $count += ($all_text =~ s/\s*#\s*include\s*[<"]*kfm\.h[>"]*\s*/\n/mg);
    $count += ($all_text =~ s/\s*#\s*include\s*[<"]*htmlview\.h[>"]*\s*/\n/mg);
    $count += ($all_text =~ s/\s*#\s*include\s*[<"]*htmltoken\.h[>"]*\s*/\n/mg);
    $count += ($all_text =~ s/\s*#\s*include\s*[<"]*kdir\.h[>"]*\s*/\n/mg);
    $count += ($all_text =~ s/\s*#\s*include\s*[<"]*kwm\.h[>"]*\s*/\n/mg);
    $count += ($all_text =~ s/\s*#\s*include\s*[<"]*kwmmapp\.h[>"]*\s*/\n/mg);

# classes
    $count += ($all_text =~ s/\bKIntegerLine\b/KIntNumInput/mg);
    $count += ($all_text =~ s/\bKLined\b/KLineEdit/mg);
    $count += ($all_text =~ s/\bKTMainWindow\b/KMainWindow/mg);
    $count += ($all_text =~ s/\bKHTMLW\b/KHTML/mg);
    $count += ($all_text =~ s/\bKNewPanner\b/QSplitter/mg);
    $count += ($all_text =~ s/\bKPanner\b/QSplitter/mg);
    $count += ($all_text =~ s/\bKClipboard\b/QClipboard/mg);
    $count += ($all_text =~ s/\bKMsgBox\b/KMessageBox/mg);
    $count += ($all_text =~ s/\bKTreeList\b/KListView/mg);
    $count += ($all_text =~ s/\bKTreeListItem\b/QListViewItem/mg);
    $count += ($all_text =~ s/\bKCombo\b/KComboBox/mg);
    $count += ($all_text =~ s/\bKQuickHelp\b/QWhatsThis/mg);
    $count += ($all_text =~ s/\bKSpinBox\b/QSpinBox/mg);
    $count += ($all_text =~ s/\bKNumericSpinBox\b/QSpinBox/mg);
    $count += ($all_text =~ s/\bKString\b/QString/mg);
    $count += ($all_text =~ s/\bKTopWidget\b/KMainWindow/mg);
    $count += ($all_text =~ s/\bKButton\b/QToolButton/mg);
    $count += ($all_text =~ s/\bKIconLoaderDialog\b/KIconDialog/mg);
    $count += ($all_text =~ s/\bKIconLoaderCanvas\b/KIconCanvas/mg);
    $count += ($all_text =~ s/\bKIconLoaderButton\b/KIconButton/mg);
    $count += ($all_text =~ s/\bKAccel::StdAccel\b/KStdAccel::StdAccel/mg);
    $count += ($all_text =~ s/\bKDirDialog\b/KFileDialog/mg);
    $count += ($all_text =~ s/\bKWMModuleApplication\b/KWinModule/mg);

# WARNING: these two conflict if you re-run this
#    $count += ($all_text =~ s/\bKHTMLView\b/KHTMLPart/mg);
#    $count += ($all_text =~ s/\bKHTMLWidget\b/KHTMLView/mg);

# functions
    $count += ($all_text =~ s/klocale-\>/KGlobal::locale\(\)-\>/mg);
    $count += ($all_text =~ s/kapp->fixedFont/KGlobalSettings::fixedFont\(\)/mg);
    $count += ($all_text =~ s/kapp->getIconLoader\(\)/KGlobal::iconLoader\(\)/mg);
    $count += ($all_text =~ s/kapp->getConfig\(\)/KGlobal::config\(\)/mg);
    $count += ($all_text =~ s/kapp->getKCharsets\(\)/KGlobal::charsets\(\)/mg);
    $count += ($all_text =~ s/KApplication::getKApplication\(\)->getConfig\(\)/KGlobal::config\(\)/mg);
    $count += ($all_text =~ s/(?!KAccel::)keyToString/KAccel::keyToString/mg);
    $count += ($all_text =~ s/(?!KAccel::)stringToKey/KAccel::stringToKey/mg);
    $count += ($all_text =~ s/KDirDialog::getDirectory/KFileDialog::getExistingDirectory/mg);
    $count += ($all_text =~ s/KFileDialog::getDirectory/KFileDialog::getExistingDirectory/mg);
    $count += ($all_text =~ s/KApplication::getKApplication\(\)->getIconLoader/KGlobal::iconLoader/mg);
    $count += ($all_text =~ s/KApplication::localkdedir\(\)/locate\(\"\",\"\"\);\n#warning FIXME Check usage of locate from localekdedir/mg);
    $count += ($all_text =~ s/setRulerStyle/setRulerMetricStyle/mg);
    $count += ($all_text =~ s/KFileDialog::getOpenFileURL/KFileDialog::getOpenURL/mg);
    $count += ($all_text =~ s/KFileDialog::getSaveFileURL/KFileDialog::getSaveURL/mg);

#enums
    $count += ($all_text =~ s/KRuler::pixel/KRuler::Pixel/mg);
    $count += ($all_text =~ s/KRuler::horizontal/Horizontal/mg);
    $count += ($all_text =~ s/KRuler::vertical/Vertical/mg);
    $count += ($all_text =~ s/Event_MouseButtonPress/QEvent::MouseButtonPress/mg);
    $count += ($all_text =~ s/Event_KeyPress/QEvent::KeyPress/mg);

# Needs some work done by the user
    $count += ($all_text =~
           s/kapp->kde_(.*?)dir\(\)/locate\(\"$1\", \"\"\);\n#warning TODO FIXME Check usage of locate from kde_$1dir\(\)/mg);

    $count += ($all_text =~
           s/KApplication::kde_(.*?)dir\(\)/locate\(\"$1\", \"\"\);\n#warning TODO FIXME Check usage of locate from kde_$1dir\(\)/mg);

# very suspect changes
#    $count += ($all_text =~ s/\s*const\s*char\s*\*\s*key/ QString key/mg);

# Finish
    if ($count)
    {
        rename ($file, "$file.orig");
        open (FILE, ">$file") or die "Opening <$file>: $!";
        print FILE $all_text;
        print "Updated $File::Find::name\n";
    }
}

