File indexing completed on 2024-04-21 05:41:56

0001 #!/usr/bin/perl -w
0002 
0003 # David Faure <faure@kde.org>
0004 # KGenericFactory + K_EXPORT_COMPONENT_FACTORY -> K_PLUGIN_FACTORY + K_EXPORT_PLUGIN
0005 
0006 use strict;
0007 use File::Basename;
0008 use lib dirname($0);
0009 use functionUtilkde;
0010 
0011 my $plugin;
0012 my $factory;
0013 my $looking_for_plugin = 0;
0014 
0015 foreach my $file (@ARGV) {
0016     functionUtilkde::substInFile {
0017         s!kgenericfactory\.h!kpluginfactory.h!g;
0018         s!<KGenericFactory>!<KPluginFactory>!g;
0019         s!<KParts/GenericFactory>!<KPluginFactory>!g;
0020         s!kparts/factory.h!kpluginfactory.h!g;
0021         s!kparts/genericfactory.h!kpluginfactory.h!g;
0022         s!<KParts/Factory>!<KPluginFactory>!g;
0023         if (/typedef KGenericFactory<(.*)>\s*(.*);/ || /typedef KParts::GenericFactory<(.*)>\s*(.*);/) {
0024             $plugin = $1;
0025             $factory = $2;
0026             # Ignore ",BaseClassForParentArg" in $plugin
0027             if ($plugin =~ s/,(.*)//) {
0028                 print STDERR "Warning, parent argument for plugin was $1, needs to be changed to QObject, or you need a create function\n";
0029                 print STDERR "  (see koffice/libs/main/KoDocInfoPropsFactory.cpp for an example)\n";
0030             }
0031             $_ = "K_PLUGIN_FACTORY($factory, registerPlugin<$plugin>();)\n";
0032         }
0033         if (/([A-Za-z_]*)::createPartObject/) {
0034             $factory = $1;
0035             $looking_for_plugin = 1;  # the "new FooPart" must be somewhere nearby
0036         }
0037         if ($looking_for_plugin && /\s*new \s*([A-Za-z]+)/) {
0038             $plugin = $1;
0039             $looking_for_plugin = 0;
0040             $_ = "K_PLUGIN_FACTORY($factory, registerPlugin<$plugin>();)\n" .
0041                  "K_EXPORT_PLUGIN($factory(\"TODO_componentName\", \"TODO_catalogName\"))\n" .
0042                  "// TODO: remove createPartObject method, and the factory class\n" . $_;
0043         }
0044         if (/:\s*public\s*KParts::Factory/) {
0045             $_ = "// TODO: remove factory class\n" . $_;
0046         }
0047         # Factory without arguments
0048         if (/K_EXPORT_COMPONENT_FACTORY\(\s*(\w*),\s*([^\(\)]*?)(?:\(\))?\s*\)/) {
0049             my $libname_ignored = $1;
0050             my $factory2 = $2;
0051             die "Expected $factory, got $factory2" if (defined $factory && $factory ne $factory2);
0052             $_ = "K_EXPORT_PLUGIN($factory2)\n";
0053         }
0054         if (/K_EXPORT_COMPONENT_FACTORY\(\s*(\w*),\s*(\w*)\(\s*([^\)]*)\s*\)\s*\)/) {
0055             my $libname_ignored = $1;
0056             my $factory2 = $2;
0057             my $catalogname = $3;
0058             die "Expected $factory, got $factory2" if ($factory ne $factory2);
0059             $_ = "K_EXPORT_PLUGIN($factory($catalogname))\n";
0060         }
0061         # All in one call, like K_EXPORT_COMPONENT_FACTORY(spreadsheetshape, KGenericFactory<TableShapePlugin>("TableShape"))
0062         if (/K_EXPORT_COMPONENT_FACTORY\s*\(\s*(\w*),\s*KGenericFactory\s*<(\w*)>\s*\(\s*(\"[^\"]*\")\s*\)\s*\)/) {
0063             my $libname_ignored = $1;
0064             my $plugin = $2;
0065             my $factory = $plugin . "Factory";
0066             my $catalogname = $3;
0067             $_ = "K_PLUGIN_FACTORY($factory, registerPlugin<$plugin>();)\n";
0068             $_ .= "K_EXPORT_PLUGIN($factory($catalogname))\n";
0069         }
0070 
0071         # Incremental fixing... can be removed later
0072         $plugin = $1 if (/registerPlugin<(.*)>/);
0073         $factory = $1 if (/K_PLUGIN_FACTORY\((\w*),/);
0074     } $file;
0075     # Now that we know the plugin name, fix its constructor signature, if by chance it's in the same file
0076     if (defined $plugin) {
0077         functionUtilkde::substInFile {
0078             if (/${plugin}::$plugin/) {
0079                 if (s/QStringList/QVariantList/) {
0080                 } elsif (/(.*)\((QWidget\s*\*\s*[^ ,]*\s*,\s*QObject\s*\*\s*\w*)\)(.*)/) {
0081                     $_ = "$1($2, const QVariantList&)$3\n";
0082                 }
0083             }
0084         } $file;
0085         my $header = $file;
0086         $header =~ s/\.cpp$/.h/;
0087         $header =~ s/\.cc$/.h/;
0088         my $headerChanged = 0;
0089         functionUtilkde::substInFile {
0090             if (/${plugin}\(/) {
0091                 if (s/QStringList/QVariantList/) {
0092                     $headerChanged = 1;
0093                 } elsif (/(.*)\((QWidget\s*\*\s*[^ ,]*\s*,\s*QObject\s*\*\s*\w*[^,\)]*)\)(.*)/) {
0094                     $_ = "$1($2, const QVariantList&)$3\n";
0095                     $headerChanged = 1;
0096                 }
0097             }
0098         } $header;
0099         if ($headerChanged) {
0100             functionUtilkde::addIncludeInFile( $header, "QVariantList");
0101         }
0102     }
0103 }
0104 functionUtilkde::diffFile( @ARGV );