6ac923b93e8dea03d3162f71f755ce2e6dac3f4b
[quassel.git] / icons / import / import_theme.pl
1 #!/usr/bin/perl
2
3 # This script scans the Quassel source for required icons and imports the needed
4 # icons (and only them) from a full icon theme.
5 # Additional icons can be specified in whitelisted-icons; you can also blacklist icons.
6 #
7 # NOTE: Unless you are a Quassel developer and need to bump the icons we ship, you shouldn't
8 #       need to use this script!
9
10 # USAGE: ./import/import_theme.pl $srcthemedir $themename
11 #
12 # Examples:
13 #   import_theme.pl ~/oxygen-icons oxygen
14 #   import_theme.pl ~/breeze-icons/icons breeze
15 #   import_theme.pl ~/breeze-icons/icons-dark breeze-dark
16
17 use strict;
18 use warnings;
19
20 use File::Basename;
21 use File::Find;
22 use File::Spec;
23
24 my $scriptroot = File::Basename::dirname(File::Spec->rel2abs($0));
25 my $srcroot = File::Spec->catdir($scriptroot, "..", "..");
26 my $srcdir = File::Spec->catdir($srcroot, "src");
27
28 my $srcthemedir = shift;
29 my $themename = shift;
30
31 # Sanity checks
32 die "Theme directory must be given\n" unless length $srcthemedir;
33 die "Theme directory \"$srcthemedir\" not found\n" unless -e $srcthemedir and -d $srcthemedir;
34 die "Theme name must not be empty\n" unless length $themename;
35
36 my $destbasedir  = File::Spec->catdir($srcroot, "3rdparty", "icons");
37 my $destthemedir = File::Spec->catdir($destbasedir, $themename);
38
39 my $whitelistfile = "$scriptroot/whitelisted-icons";
40 my $blacklistfile = "$scriptroot/blacklisted-icons";
41
42 my %req_icons;
43 my %found_icons;
44 my %whitelist;
45 my %blacklist;
46
47 # Add whitelisted icons
48 open WHITELIST, "<$whitelistfile" or die "Could not open $whitelistfile\n";
49 while(<WHITELIST>) {
50   s/#.*//;
51   next unless my ($name) = /([-\w]+)\s*/;
52   $req_icons{$name} = 1;
53 }
54 close WHITELIST;
55
56 # Load the icon blacklist
57 open BLACKLIST, "<$blacklistfile" or die "Could not open $blacklistfile\n";
58 while(<BLACKLIST>) {
59   s/#.*//;
60   next unless my ($name) = /([-\w]+)\s*/;
61   $blacklist{$name} = 1;
62 }
63 close BLACKLIST;
64
65 # We now grep the source for QIcon::fromTheme("fubar") to find required icons
66 print "Grepping $srcdir for required icons...\n";
67 my @results = `grep -r QIcon::fromTheme\\(\\" $srcdir`;
68 foreach(@results) {
69   next unless my ($name) = /\W+QIcon::fromTheme\(\"([-\w]+)/;
70   $req_icons{$name} = 1
71     unless exists $blacklist{$name};
72 }
73
74 # Clean old output dir
75 print "Removing old $destthemedir...\n";
76 system("rm -rf $destthemedir");
77
78 # Now copy the icons
79 my %scalables;
80
81 print "Copying icons from $srcthemedir...\n";
82 opendir (BASEDIR, "$srcthemedir") or die "Could not open theme basedir\n";
83 my $scalableFound = 0;
84 foreach my $parent (readdir BASEDIR) {
85   next unless (-d "$srcthemedir/$parent");
86   $scalableFound = $scalableFound ? 1 : $parent eq 'scalable';
87   next if $parent eq '.' or $parent eq '..' or $parent eq 'scalable' or $parent =~ /\..*/;
88   my $ischildcat = $parent =~ /\d+x\d+/ ? 1 : 0;
89   opendir (SIZEDIR, "$srcthemedir/$parent") or die "Could not open dir $parent\n";
90   foreach my $child (readdir SIZEDIR) {
91     next if $child eq '.' or $child eq '..';
92     my $cat = $ischildcat ? $child : $parent;
93     opendir (CATDIR, "$srcthemedir/$parent/$child") or die "Could not open category dir\n";
94     foreach my $icon (readdir CATDIR) {
95       my $iconname = $icon;
96       $iconname =~ s/\.png$//;
97       $iconname =~ s/\.svg$//;
98       next unless exists $req_icons{$iconname};
99       $scalables{$cat}{$iconname} = 1;
100       system "mkdir -p $destthemedir/$parent/$child" and die "Could not create category dir\n";
101       system "cp -aL $srcthemedir/$parent/$child/$icon $destthemedir/$parent/$child"
102         and die "Error while copying file $parent/$child/$icon\n";
103       print "Copy: $srcthemedir/$parent/$child/$icon\n";
104       $found_icons{$iconname} = 1;
105     }
106     closedir CATDIR;
107   }
108   closedir SIZEDIR;
109 }
110 closedir BASEDIR;
111
112 # Copy scalables
113 if ($scalableFound) {
114   foreach my $cat (keys %scalables) {
115     system "mkdir -p $destthemedir/scalable/$cat" and die "Could not create category dir\n";
116     foreach my $scalable (keys %{$scalables{$cat}}) {
117       system "cp -aL $srcthemedir/scalable/$cat/$scalable.svgz $destthemedir/scalable/$cat/$scalable.svgz";
118     }
119   }
120 }
121
122 # Warn if we have still icons left
123 foreach my $icon (keys %req_icons) {
124   next if defined $found_icons{$icon};
125   print "Warning: Missing icon $icon\n";
126 }
127
128 # Copy license etc.
129 system "cp -aL $srcthemedir/AUTHORS* $srcthemedir/CONTRIBUTING* $srcthemedir/COPYING* $srcthemedir/index.theme $destthemedir/ | true";
130
131 # Generate .qrc
132 #my $qrcfile = $themename."_icon_theme.qrc";
133 #$qrcfile =~ s/-/_/g;
134 my $qrcfile = File::Spec->catdir($destbasedir, ($themename."_icon_theme.qrc") =~ s/-/_/gr);
135 print "Generating $qrcfile...\n";
136
137 my @file_list = ();
138
139 sub push_icon_path {
140   return unless /\.png$/ or /\.svg$/ or /^index.theme$/;
141   push @file_list, "    <file>".$File::Find::name =~ s|^$destbasedir/||gr."</file>";
142 }
143
144 find(\&push_icon_path, $destthemedir);
145 @file_list = sort(@file_list);
146 my $files = join "\n", @file_list;
147
148 my $qrc = "<RCC>\n"
149          ."  <qresource prefix=\"/icons\">\n"
150          ."$files\n"
151          ."  </qresource>\n"
152          ."</RCC>\n";
153
154 open QRC, ">$qrcfile" or die "Could not open $qrcfile for writing!\n";
155 print QRC $qrc;
156 close QRC;
157
158 print "Done.\n";