File indexing completed on 2024-04-14 05:35:06

0001 #!/usr/bin/perl -w
0002 
0003 # David Faure <faure@kde.org>
0004 # KStandardDirs -> QStandardPaths
0005 #
0006 # https://community.kde.org/Frameworks/Porting_Notes/KStandardDirs
0007 
0008 use strict;
0009 use File::Basename;
0010 use lib dirname($0);
0011 use functionUtilkde;
0012 
0013 my %easyResource = (
0014    "data" =>   "QStandardPaths::GenericDataLocation",
0015    "appdata" =>   "QStandardPaths::DataLocation",
0016    "config" =>   "QStandardPaths::ConfigLocation",
0017    "xdgdata-apps" =>   "QStandardPaths::ApplicationsLocation",
0018    "cache" =>   "QStandardPaths::CacheLocation",
0019    "socket" =>   "QStandardPaths::RuntimeLocation"
0020 );
0021 
0022 # subdirs of QStandardPaths::GenericDataLocation
0023 my %otherResource = (
0024    "services" => "kde5/services",
0025    "xdgdata-dirs" => "desktop-directories",
0026    "xdgdata-icon" => "icons",
0027    "icon" => "icons",
0028    "locale" => "locale",
0029    "wallpaper" => "wallpapers",
0030    "xdgdata-mime" => "mime",
0031    "sound" => "sounds"
0032 );
0033 
0034 # subdirs of QStandardPaths::ConfigLocation
0035 my %xdgconfResource = (
0036    "xdgconf-menu" => "menus",
0037    "xdgconf-autostart" => "autostart",
0038    "autostart" => "autostart"
0039 );
0040 
0041 sub locationAndSubdir
0042 {
0043     my ($resource, $fileName) = @_;
0044     my $loc;
0045     if (defined $easyResource{$resource}) {
0046         $loc = $easyResource{$resource};
0047     } elsif (defined $otherResource{$resource}) {
0048         $loc = "QStandardPaths::GenericDataLocation";
0049         $fileName = $fileName eq "QString()" ? "" : " + $fileName";
0050         $fileName = "QLatin1String(\"$otherResource{$resource}\/\")$fileName";
0051     } elsif (defined $xdgconfResource{$resource}) {
0052         $loc = "QStandardPaths::ConfigLocation";
0053         $fileName = $fileName eq "QString()" ? "" : " + $fileName";
0054         $fileName = "QLatin1String(\"$xdgconfResource{$resource}\/\")$fileName";
0055     } else {
0056         print STDERR "Unhandled resource $resource\n";
0057     }
0058     return ($loc, $fileName);
0059 }
0060 
0061 foreach my $file (@ARGV) {
0062     my $addconfigprefix = 0;
0063 
0064     functionUtilkde::substInFile {
0065         s/KGlobal::dirs\(\)->findResource\b/KStandardDirs::locate/;
0066         s/KGlobal::dirs\(\)->locate/KStandardDirs::locate/;
0067         s/KGlobal::dirs\(\)->findExe/KStandardDirs::findExe/;
0068         s/KGlobal::dirs\(\)->localxdgdatadir\s*\(\)/QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QLatin1Char('\/')/;
0069         s/KGlobal::dirs\(\)->localxdgconfdir\s*\(\)/QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation) + QLatin1Char('\/')/;
0070         s/KStandardDirs::locate\s*\(\s*"exe", /KStandardDirs::findExe\(/;
0071 
0072         if ( /KStandardDirs::Recursive/ ) {
0073            warn "$file : we can not convert directly see https://community.kde.org/Frameworks/Porting_Notes/KStandardDirs \n";
0074         }
0075 
0076         if (/KStandardDirs::locateLocal\s*\(\s*\"(.*)\",\s*(.*)\s*\)/) {
0077             my ($loc, $fileName) = locationAndSubdir($1, $2);
0078             if (defined $loc) {
0079                 # prepend a slash
0080                 if ($fileName =~ m/QLatin1String/) {
0081                     $fileName =~ s/QLatin1String\s*\(\s*\"/QLatin1String(\"\//;
0082                 } else {
0083                     $fileName = "QLatin1Char('\/') + " . $fileName;
0084                 }
0085                 s/KStandardDirs::locateLocal\s*\(.*\)/QStandardPaths::writableLocation($loc) + $fileName/;
0086             }
0087         }
0088         if (/KStandardDirs::locate\s*\(\s*\"(.*)\",\s*(.*)\s*\)/ ||
0089             /KGlobal::dirs\(\)->findAllResources\s*\(\s*\"(.*)\",\s*(.*)\s*\)/ ||
0090             /KGlobal::dirs\(\)->findDirs\s*\(\s*\"(.*)\",\s*(.*)\s*\)/  ) {
0091             my ($loc, $fileName) = locationAndSubdir($1, $2);
0092             if (defined $loc) {
0093                 # ends with a '/' (in a string literal) ?
0094                 #print STDERR "fileName=$fileName\n";
0095                 my ($search, $replace);
0096                 if (/KStandardDirs::locate/) {
0097                    $search = qr/KStandardDirs::locate\s*/;
0098                    $replace = "QStandardPaths::locate";
0099                 } elsif (/KGlobal::dirs\(\)->findAllResources/) {
0100                    $search = qr/KGlobal::dirs\(\)->findAllResources\s*/;
0101                    $replace = "QStandardPaths::locateAll";
0102                 } elsif (/KGlobal::dirs\(\)->findDirs/) {
0103                    $search = qr/KGlobal::dirs\(\)->findDirs\s*/;
0104                    $replace = "QStandardPaths::locateAll";
0105                 }
0106                 if ($fileName =~ s/\/\"$/\"/ || $fileName =~ s/\/\"\)$/\"\)/) {
0107                     s/$search\(.*\)/$replace($loc, $fileName, QStandardPaths::LocateDirectory)/;
0108                 } else {
0109                     s/$search\(.*\)/$replace($loc, $fileName)/;
0110                 }
0111             }
0112         }
0113         my $regexp = qr/
0114            ^(\s*)                                  # (1) Indentation
0115            (.*?)                                   # (2) Possibly "Classname " (the ? means non-greedy)
0116            (\w+)                                   # (3) variable name
0117            \s*=\s*                                 #   assignment
0118            KGlobal::dirs\(\)->findResourceDir\s*\(\s* #  KGlobal::dirs\(\)->findResourceDir\(
0119            \"(.*)\"\s*,                            # (4) location
0120            \s*(.*)\s*\)                            # (5) subdir
0121            (.*)$                                   # (6) afterreg
0122            /x; # /x Enables extended whitespace mode
0123         if (my ($indent, $left, $var, $location, $subdir, $after) = $_ =~ $regexp) {
0124            my ($loc, $fileName) = locationAndSubdir($location, $subdir);
0125            if (defined $loc) {
0126                warn "$loc, $fileName\n";
0127                $_ = $indent . $left . $var . "= QStandardPaths::locate($loc, $fileName)" . $after . "\n";
0128                $_ .= $indent . "$var = QFileInfo($var).absolutePath();\n";
0129            }
0130         }
0131        
0132 
0133  
0134         if (/KStandardDirs::findExe\s*\(\s*\"(.*)\"(,\s*[^\)]*)?\s*\)/ ||
0135             /KStandardDirs::findExe\s*\(\s*QLatin1String\s*\(\"(.*)\"\)(,\s*[^\)]*)?\s*\)/) {
0136             my $exe = $1;
0137             if (`which $exe 2>/dev/null`) {
0138                 print STDERR "found $exe\n";
0139                 s/KStandardDirs::findExe/QStandardPaths::findExecutable/;
0140             } else {
0141                 if (`locate libexec/$exe`) {
0142                     s/KStandardDirs::findExe\s*\([^\)]*\)/CMAKE_INSTALL_PREFIX \"\/\" LIBEXEC_INSTALL_DIR \"\/$exe\"/;
0143                     #print STDERR "$exe NOT found in PATH, use CMAKE_INSTALL_PREFIX \"/\" LIBEXEC_INSTALL_DIR \"/$exe\" instead\n";
0144                     $addconfigprefix = 1;
0145                 } else {
0146                     print STDERR "$exe NOT found in PATH, nor in libexec, using locate. Where is it?\n";
0147                 }
0148             }
0149         }
0150         if (/KGlobal::dirs\(\)->saveLocation\(\s*\"([^\"]*)\"(,\s*[^\)]*)?\s*\)/) {
0151             my $suffix = $2;
0152             my ($loc, $add) = locationAndSubdir($1, "");
0153             #print STDERR "resource=$resource suffix=$suffix\n";
0154             if ($add ne "") {
0155                 $add = " + $add";
0156             }
0157             if ($suffix) {
0158                 $suffix =~ s/,\s*//;
0159                 $add .= " + QLatin1Char('/') + $suffix)" unless $suffix eq "QString(" || $suffix eq "\"\"";
0160                 #print STDERR "loc=$loc suffix=$suffix add=$add\n";
0161             }
0162             s/KGlobal::dirs\(\)->saveLocation\(.*\)/QStandardPaths::writableLocation($loc)$add/ if ($loc);
0163         }
0164         if (/KGlobal::dirs\(\)->resourceDirs\(\s*\"([^\"]*)\"\s*\)/) {
0165             my ($loc, $add) = locationAndSubdir($1, "");
0166             if ($add) {
0167                 s/KGlobal::dirs\(\)->resourceDirs\(.*\)/QStandardPaths::locateAll($loc, $add, QStandardPaths::LocateDirectory)/;
0168             } elsif ($loc) {
0169                 s/KGlobal::dirs\(\)->resourceDirs\(.*\)/QStandardPaths::standardLocations($loc) \/* WARNING: no more trailing slashes *\//;
0170             }
0171         }
0172         # While we're here...
0173         s/KGlobal::config\(\)/KSharedConfig::openConfig()/g;
0174 
0175         # ex: KSharedConfig::openConfig("mimeapps.list", KConfig::NoGlobals, "xdgdata-apps");
0176         if (my ($file, $flags, $resource) = ($_ =~ m/KSharedConfig::openConfig\(\s*([^,]*), ([^,]*), \s*\"([^\"]*)\"\s*\)/)) {
0177             my ($loc, $fileName) = locationAndSubdir($resource, $file);
0178             if ($loc) {
0179               s/KSharedConfig::openConfig\(.*\)/KSharedConfig::openConfig($fileName, $flags, $loc)/;
0180             }
0181         }
0182         if (/KStandardDirs::locateLocal\s*\(\s*\"tmp\"/) {
0183             s/KStandardDirs::locateLocal\s*\(\s*\"tmp\"\s*,/QDir::tempPath\(\) \+ QLatin1Char\(\'\/\'\) \+ /;
0184         }
0185 
0186 
0187     } $file;
0188     functionUtilkde::addIncludeInFile($file, "config-prefix.h") if ($addconfigprefix);
0189     if (not `grep -i dirs $file | grep -v kstandarddirs\.h`) {
0190       functionUtilkde::removeIncludeInFile($file, "kstandarddirs.h");
0191     }
0192     if (not `grep -i dirs $file | grep -v KStandardDirs`) {
0193       functionUtilkde::removeIncludeInFile($file, "KStandardDirs");
0194     }
0195 
0196     if (`grep QStandardPaths $file | grep -v '#include'`) {
0197       functionUtilkde::addIncludeInFile($file, "QStandardPaths");
0198     }
0199     if (`grep KSharedConfig $file | grep -v '#include'` and not `grep ksharedconfig\.h $file`) {
0200       functionUtilkde::addIncludeInFile($file, "KSharedConfig");
0201     }
0202 }
0203 
0204 functionUtilkde::diffFile( "@ARGV" );