File: convert-kprogressdialog.pl

package info (click to toggle)
kde-dev-scripts 4%3A18.08.0-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 3,496 kB
  • sloc: perl: 15,466; lisp: 5,627; sh: 4,157; python: 3,892; ruby: 2,158; makefile: 16; sed: 9
file content (200 lines) | stat: -rwxr-xr-x 7,862 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#!/usr/bin/perl -w

# Laurent Montel <montel@kde.org> (2014)
# KProgressDialog->QProgressDialog
# find -iname "*.cpp" -o -iname "*.h" |xargs kde-dev-scripts/kf5/convert-kprogressdialog.pl
# TODO need to improve it.
use strict;
use File::Basename;
use lib dirname($0);
use functionUtilkde;

foreach my $file (@ARGV) {

    my $modified;
    open(my $FILE, "<", $file) or warn "We can't open file $file:$!\n";
    my %varname = ();
    my @l = map {
        my $orig = $_;
        my $regexp = qr/
           ^(\s*)                             # (1) Indentation, possibly "Classname *" (the ? means non-greedy)
           (.*?)                              # (2) Possibly "Classname *" (the ? means non-greedy)
           (\w+)                              # (3) variable name
           \s*=\s*                            #   assignment
           new\s+KProgressDialog\s*\((.*)\)   # (4)  new KProgressDialog(...,...,...,...);
           (.*)$                              # (5) afterreg
           /x; # /x Enables extended whitespace mode
        if (my ($indent, $left, $var, $args, $afterreg) = $_ =~ $regexp) {
            warn "Found KProgressDialog $var \n";
            $varname{$var} = 1;
            my $constructor_regexp = qr/
                                 ^([^,]*)\s*        # parent
                                 (?:,\s([^,]*))?    # caption
                                 (?:,\s([^,]*))?    # text
                                 (.*)$              # after
                                 /x;
            if ( my ($parent, $caption, $text,  $after) = $args =~ $constructor_regexp ) {
                $_ = $indent . $left . "$var = new QProgressDialog($parent);\n";
                if (defined $caption) {
                   $_ .= $indent . "$var\-\>setWindowTitle($caption);\n";
                }
                if (defined $text) {
                   $_ .= $indent . "$var\-\>setLabelText($text);\n";
                }
            }
        }
        my $regexpLocal = qr/
          ^(\s*)           # (1) Indentation
          KProgressDialog\s+
          (\w+)            # (2) variable name
          (.*)/x; # /x Enables extended whitespace mode
        if (my ($left, $var, $args) = $_ =~ $regexpLocal) {
           $varname{$var} = 1;
            my $constructor_regexp = qr/
                                 ^([^,]*)\s*        # parent
                                 (?:,\s([^,]*))?    # caption
                                 (?:,\s([^,]*))?    # text
                                 (.*)$              # after
                                 /x;
            if ( my ($parent, $caption, $text,  $after) = $args =~ $constructor_regexp ) {
                $_ = $left . "QProgressDialog $var$parent);\n";
                if (defined $caption) {
                   $_ .= $left . "$var\.setWindowTitle($caption);\n";
                }
                if (defined $text) {
                   $_ .= $left . "$var\.setLabelText($text\n";
                }
            }
        }
        if (/(\w+)\.progressBar\(\)\-\>setMaximum\b/) {
            my $variable = $1;
            if (defined $varname{$variable}) {
                s/$variable\.progressBar\(\)\-\>setMaximum\b/$variable\.setMaximum/;
            }
        }
        if (/(\w+)\.progressBar\(\)\-\>setMinimum\b/) {
            my $variable = $1;
            if (defined $varname{$variable}) {
                s/$variable\-\>progressBar\(\)\.setMinimum\b/$variable\.setMinimum/;
            }
        }
        if (/(\w+)\.progressBar\(\)\-\>setValue\b/) {
            my $variable = $1;
            if (defined $varname{$variable}) {
                s/$variable\.progressBar\(\)\-\>setValue\b/$variable\.setValue/;
            }
        }
        if (/(\w+)\.progressBar\(\)\-\>value\b/) {
            my $variable = $1;
            if (defined $varname{$variable}) {
                s/$variable\.progressBar\(\)\-\>value\b/$variable\.value/;
            }
        }
        if (/(\w+)\.progressBar\(\)\-\>setRange\b/) {
            my $variable = $1;
            if (defined $varname{$variable}) {
                s/$variable\.progressBar\(\)\-\>setRange\b/$variable\.setRange/;
            }
        }
        if (/(\w+)\.wasCancelled\s*\(\)/) {
            my $variable = $1;
            if (defined $varname{$variable}) {
               s/$variable\.wasCancelled\s*\(\)/$variable\.wasCanceled()/;
            }
        }
        if (/(\w+)\.setAllowCancel\s*\(\s*false\s*\);/) {
            my $variable = $1;
            if (defined $varname{$variable}) {
               s/$variable\.setAllowCancel\s*\(\s*false\s*\);/$variable\.setCancelButton\(0\);/;
            }
        }
        if (/(\w+)\.setAllowCancel\s*\(\s*true\s*\);/) {
            my $variable = $1;
            if (defined $varname{$variable}) {
               $_ = "";
            }
        }
        if (/(\w+)\.setShowCancel\s*\(\s*false\s*\);/) {
            my $variable = $1;
            if (defined $varname{$variable}) {
               s/$variable\.setShowCancel\s*\(\s*false\s*\);/$variable\.setCancelButton\(0\);/;
            }
        }

        if (/(\w+)\-\>setShowCancel\s*\(\s*false\s*\);/) {
            my $variable = $1;
            if (defined $varname{$variable}) {
               s/$variable\-\>setShowCancel\s*\(\s*false\s*\);/$variable\-\>setCancelButton\(0\);/;
            }
        }


        if (/(\w+)\-\>progressBar\(\)\-\>setMaximum\b/) {
            my $variable = $1;
            if (defined $varname{$variable}) {
                s/$variable\-\>progressBar\(\)\-\>setMaximum\b/$variable\-\>setMaximum/;
            }
        }
        if (/(\w+)\-\>progressBar\(\)\-\>setMinimum\b/) {
            my $variable = $1;
            if (defined $varname{$variable}) {
                s/$variable\-\>progressBar\(\)\-\>setMinimum\b/$variable\-\>setMinimum/;
            }
        }
        if (/(\w+)\-\>progressBar\(\)\-\>setValue\b/) {
            my $variable = $1;
            if (defined $varname{$variable}) {
                s/$variable\-\>progressBar\(\)\-\>setValue\b/$variable\-\>setValue/;
            }
        }
        if (/(\w+)\-\>progressBar\(\)\-\>value\b/) {
            my $variable = $1;
            if (defined $varname{$variable}) {
                s/$variable\-\>progressBar\(\)\-\>value\b/$variable\-\>value/;
            }
        }
        if (/(\w+)\-\>progressBar\(\)\-\>setRange\b/) {
            my $variable = $1;
            if (defined $varname{$variable}) {
                s/$variable\-\>progressBar\(\)\-\>setRange\b/$variable\-\>setRange/;
            }
        }
        if (/(\w+)\-\>wasCancelled\s*\(\)/) {
            my $variable = $1;
            if (defined $varname{$variable}) {
               s/$variable\-\>wasCancelled\s*\(\)/$variable\-\>wasCanceled()/;
            }
        }
        if (/(\w+)\-\>setAllowCancel\s*\(\s*false\s*\);/) {
            my $variable = $1;
            if (defined $varname{$variable}) {
               s/$variable\-\>setAllowCancel\s*\(\s*false\s*\);/$variable\-\>setCancelButton\(0\);/;
            }
        }
        if (/(\w+)\-\>setAllowCancel\s*\(\s*true\s*\);/) {
            my $variable = $1;
            if (defined $varname{$variable}) {
               $_ = "";
            }
        }
        if ( /\s*connect\s*\(\s*(\w+),\s*SIGNAL\s*\(\s*cancelClicked\s*\(\s*\)/ ) {
           if (defined $varname{$1} ) {
              s/cancelClicked/canceled/;
           }
        }

        s/\bKProgressDialog\b/QProgressDialog/g;
        s/\<KProgressDialog\b\>/\<QProgressDialog>/ if (/#include/);
        s/\<kprogressdialog.h\>/\<QProgressDialog>/ if (/#include/);
        $modified ||= $orig ne $_;
        $_;
    } <$FILE>;

    if ($modified) {
        open (my $OUT, ">", $file);
        print $OUT @l;
        close ($OUT);
    }
}

functionUtilkde::diffFile( "@ARGV" );