File indexing completed on 2024-09-29 05:02:38
0001 #!/usr/bin/perl -w 0002 0003 # David Faure <faure@kde.org> 0004 # KCommandLineArgs -> QCommandLineParser 0005 0006 use strict; 0007 use File::Basename; 0008 use lib dirname($0); 0009 use functionUtilkde; 0010 my $use_tr; 0011 my $use_aboutdata; 0012 0013 # Set $use_tr to generate code that uses QCoreApplication::translate 0014 # If it's not set, i18n will be used. 0015 #$use_tr = 1; 0016 0017 # Set $use_aboutdata_tr to generate code that uses kaboutdata 0018 $use_aboutdata = 1; 0019 0020 my $port_kapplicationAndK4AboutData; 0021 # Set $port_kapplicationAndK4AboutData if you want to port k4aboutdata + kapplication 0022 $port_kapplicationAndK4AboutData = 1; 0023 0024 foreach my $file (@ARGV) { 0025 my $context = "\"main\""; 0026 my $opt; 0027 my $short = ""; 0028 my $args; 0029 my %negatedOptions = (); 0030 my $needRemoveKApplication; 0031 my %varname = (); 0032 my $QCommandLineParserAdded; 0033 my $needQCommandLineOption; 0034 functionUtilkde::substInFile { 0035 if (defined $port_kapplicationAndK4AboutData) { 0036 if (/KApplication a/) { 0037 $_ =""; 0038 $needRemoveKApplication = 1; 0039 } 0040 my $regexpK4AboutDataLocal = qr/ 0041 ^(\s*) # (1) Indentation 0042 K4AboutData\s+ 0043 (\w+) # (2) variable name 0044 /x; # /x Enables extended whitespace mode 0045 if (my ($left, $var) = $_ =~ $regexpK4AboutDataLocal) { 0046 $varname{$var} = 1; 0047 s/ki18n/i18n/g; 0048 } 0049 if (/(\w+)\.addAuthor\s*\(/) { 0050 my $var = $1; 0051 if ( defined $varname{$var} ) { 0052 s/ki18n/i18n/g; 0053 s/KLocalizedString\b/QString/g; 0054 } 0055 } 0056 if (/(\w+)\.addCredit\s*\(/) { 0057 my $var = $1; 0058 if ( defined $varname{$var} ) { 0059 s/ki18n/i18n/g; 0060 s/KLocalizedString\b/QString/g; 0061 } 0062 } 0063 if (/(\w+)\.setTranslator\s*\(/) { 0064 my $var = $1; 0065 if ( defined $varname{$var} ) { 0066 s/ki18n/i18n/g; 0067 s/KLocalizedString\b/QString/g; 0068 } 0069 } 0070 0071 0072 s/K4AboutData/KAboutData/g; 0073 s/KAboutData::License_/KAboutLicense::/; 0074 } 0075 if (/KCmdLineOptions (\w*)/) { 0076 $opt = $1; 0077 0078 s/KCmdLineOptions /QCommandLineParser /; 0079 s/$opt/parser/; 0080 if (defined $QCommandLineParserAdded) { 0081 $_ = ""; 0082 } else { 0083 if (defined $port_kapplicationAndK4AboutData) { 0084 $_ .= " QApplication app(argc, argv); // PORTING SCRIPT: move this to before the KAboutData initialization\n"; 0085 $_ .= " KAboutData::setApplicationData(aboutData);\n"; 0086 } 0087 if ( defined $use_aboutdata) { 0088 $_ .= " //PORTING SCRIPT: adapt aboutdata variable if necessary\n"; 0089 $_ .= " aboutData.setupCommandLine(&parser);\n"; 0090 } 0091 $_ .= " parser.process(app); // PORTING SCRIPT: move this to after any parser.addOption\n"; 0092 if ( defined $use_aboutdata) { 0093 $_ .= " aboutData.processCommandLine(&parser);\n"; 0094 } 0095 } 0096 $QCommandLineParserAdded = 1; 0097 0098 } elsif (defined $opt && /KCmdLineArgs::addCmdLineOptions\s*\(\s*$opt\s*\)/ || /KCmdLineArgs::init/) { 0099 my $addNewAboutData; 0100 my $regexp = qr/ 0101 ^(.*) # (1) Indentation 0102 KCmdLineArgs::init\s*\((.*)\) # (2) KCmdLineArgs::init(...,...,...,...); 0103 (.*)$ # (3) afterreg 0104 /x; # /x Enables extended whitespace mode 0105 if (my ($indent, $argument, $afterreg) = $_ =~ $regexp) { 0106 my ($argc, $argv, $appname, $catalog, $programname, $version, $description); 0107 my $constructor_regexp = qr/ 0108 ^([^,]*)\s* # argc 0109 ,\s*([^,]*)\s* # argv 0110 ,\s*([^,]*)\s* # appname 0111 ,\s*([^,]*) # catalog 0112 ,\s*([^,]*) # programname 0113 ,\s*([^,]*) # version 0114 (?:,\s*([^,]*))? # description 0115 (.*)$ # after 0116 /x; 0117 if ( ($argc, $argv, $appname, $catalog, $programname, $version, $description) = $argument =~ $constructor_regexp ) { 0118 $appname =~ s/ki18n/i18n/; 0119 $appname =~ s/ki18n/i18n/; 0120 $programname =~ s/ki18n/i18n/; 0121 if ( $appname =~ /^\"/ ) { 0122 if ( $appname =~ /QLatin1String/ ) { 0123 #nothing 0124 } else { 0125 $appname = "QLatin1String($appname)"; 0126 } 0127 } 0128 if ( $version =~ /QLatin1String/ ) { 0129 #nothing 0130 } else { 0131 $version = "QLatin1String($version)"; 0132 } 0133 $_ = $indent . "KAboutData aboutData( $appname, $programname, $version);\n"; 0134 if (defined $description) { 0135 warn "DESCRIPION : $description\n"; 0136 $description =~ s/^,//; 0137 $description =~ s/ki18n/i18n/; 0138 $_ .= $indent . "aboutData.setShortDescription($description);\n"; 0139 } 0140 $addNewAboutData = 1; 0141 } else { 0142 $_ = ""; 0143 } 0144 } 0145 0146 if ( defined $QCommandLineParserAdded) { 0147 if (defined $addNewAboutData) { 0148 #nothing 0149 } else { 0150 $_ = ""; 0151 } 0152 } else { 0153 if (defined $port_kapplicationAndK4AboutData) { 0154 $_ .= " QApplication app(argc, argv); // PORTING SCRIPT: move this to before the KAboutData initialization\n"; 0155 $_ .= " QCommandLineParser parser;\n"; 0156 $_ .= " KAboutData::setApplicationData(aboutData);\n"; 0157 } 0158 if ( defined $use_aboutdata) { 0159 $_ .= " //PORTING SCRIPT: adapt aboutdata variable if necessary\n"; 0160 $_ .= " aboutData.setupCommandLine(&parser);\n"; 0161 } 0162 $_ .= " parser.process(app); // PORTING SCRIPT: move this to after any parser.addOption\n"; 0163 if ( defined $use_aboutdata) { 0164 $_ .= " aboutData.processCommandLine(&parser);\n"; 0165 } 0166 } 0167 $QCommandLineParserAdded = 1; 0168 } elsif (defined $opt && s/(.*)$opt\.add\s*\(\s*"([^\"]*)"\s*\)/$1$opt/) { # short option 0169 0170 $short = "QLatin1String(\"$2\") << "; 0171 $_ = "" if (!/\.add/); 0172 warn "$file: Be sure that option is added \'$2\'\n"; 0173 } 0174 if (defined $opt && /(.*)$opt.add\s*\(\s*"([^\"]*)"\s*,\s*k(i18nc?)\((.*)\)\s*(?:,\s*([^\)]*))?\)/) { 0175 my $prefix = $1; # e.g. indent 0176 my $name = $2; 0177 my $i18n = $3; 0178 my $description = $4; 0179 my $defaultValue = $5; 0180 my $trail = ""; 0181 if ($name =~ /([\w\-_]*) <(.*)>/) { # "stylesheet <xsl>" 0182 $name = $1; 0183 $trail = ", QLatin1String(\"$2\")"; 0184 } 0185 if (defined $defaultValue) { 0186 if ( $defaultValue =~ /QLatin1String/ ) { 0187 $trail .= ", $defaultValue"; 0188 } else { 0189 $trail .= ", QLatin1String($defaultValue)"; 0190 } 0191 } elsif ($name =~ /^no/) { # negative option, e.g. --nosignal 0192 $negatedOptions{$name} = 1; 0193 } 0194 my $translate = defined $use_tr ? "QCoreApplication::translate($context, $description)" : "$i18n($description)"; 0195 if ($name =~ s/^\+//) { 0196 $_ = "${prefix}parser.addPositionalArgument(QLatin1String(\"$name\"), $translate$trail);\n"; 0197 } else { 0198 $_ = "${prefix}parser.addOption(QCommandLineOption(QStringList() << ${short}QLatin1String(\"$name\"), $translate$trail));\n"; 0199 } 0200 $needQCommandLineOption = 1; 0201 $short = ""; 0202 } elsif (/KCmdLineArgs\s*\*\s*(\w*)\s*=\s*KCmdLineArgs::parsedArgs\(\s*\)/) { 0203 $args = $1; 0204 $_ = ""; 0205 } else { 0206 s/KCmdLineArgs::qtArgc\(\)/argc/; 0207 s/KCmdLineArgs::qtArgv\(\)/argv/; 0208 if (defined $args) { 0209 s/${args}\->getOptionList/parser.values/g; 0210 s/${args}\->getOption/parser.value/g; 0211 s/${args}\->isSet/parser.isSet/g; 0212 s/${args}\->count/parser.positionalArguments().count/; 0213 s/${args}\->usage\s*\(\)/parser.showHelp()/; 0214 s/${args}\->clear\s*\(\);//; 0215 s/KCmdLineArgs::usage\s*\(\)/parser.showHelp()/; 0216 if (/arguments?\(\"([\w\-_]*)/ || /isSet\(\"([\w\-_]*)/) { 0217 my $optionName = $1; 0218 if (defined $negatedOptions{"no$optionName"}) { 0219 s/$/\/\/ TODO: negate check (and ensure nobody passes the no-op --$optionName argument)/; 0220 s/$optionName/no$optionName/g; 0221 } 0222 } 0223 } 0224 } 0225 $_; 0226 } $file; 0227 0228 if (defined $needRemoveKApplication) { 0229 functionUtilkde::removeIncludeInFile($file, "KApplication"); 0230 functionUtilkde::removeIncludeInFile($file, "kapplication.h"); 0231 functionUtilkde::addIncludeInFile($file, "QApplication"); 0232 functionUtilkde::removeIncludeInFile($file, "K4AboutData"); 0233 functionUtilkde::removeIncludeInFile($file, "k4aboutdata.h"); 0234 functionUtilkde::addIncludeInFile($file, "KAboutData"); 0235 functionUtilkde::addIncludeInFile($file, "KLocalizedString"); 0236 0237 } 0238 0239 if (`grep QCommand $file | grep -v '#include'`) { 0240 functionUtilkde::removeIncludeInFile($file, "kcmdlineargs.h"); 0241 functionUtilkde::removeIncludeInFile($file, "KCmdLineArgs"); 0242 functionUtilkde::removeIncludeInFile($file, "KCmdLineOptions"); 0243 functionUtilkde::addIncludeInFile($file, "QCommandLineParser"); 0244 if (defined $needQCommandLineOption) { 0245 functionUtilkde::addIncludeInFile($file, "QCommandLineOption"); 0246 } 0247 } 0248 } 0249 0250 functionUtilkde::diffFile( "@ARGV" );