Rework how icons are handled in the build system
[quassel.git] / icons / import / import_oxygen.pl
1 #!/usr/bin/perl
2
3 # This script scans the Quassel source for requested icons and imports the needed
4 # icons (and only them) from KDE's Oxygen theme.
5 # This relies on all icons being requested using one of the convenience constructors in
6 # (K)IconLoader, like this:
7 #   widget->setIcon(SmallIcon("fubar"));
8 # Additional icons can be specified in extra-icons; you can also blacklist icons.
9 #
10 # NOTE: Unless you are a Quassel developer and need to bump the icons we ship, you shouldn'y
11 #       need to use this script!
12
13 # USAGE: ./import/import_oxygen.pl $systhemefolder
14 # Run from the icon/ directory.
15
16 use strict;
17 use Data::Dumper;
18 use File::Find;
19
20 my $oxygen = shift;
21
22 my $source = "../src";
23 my $output = "oxygen";
24 my $qrcfile_kde = "oxygen.qrc";
25
26 my $extrafile = "import/extra-icons";
27 my $blacklistfile = "import/blacklisted-icons";
28
29 my %req_icons;
30 my %found_icons;
31 my %blacklist;
32 my %extra;
33
34 # First, load the icon blacklist
35 open BLACKLIST, "<$blacklistfile" or die "Could not open $blacklistfile\n";
36 while(<BLACKLIST>) {
37   s/#.*//;
38   next unless my ($name) = /([-\w]+)\s*/;
39   $blacklist{$name} = 1;
40 }
41 close BLACKLIST;
42
43 # We now grep the source for things like SmallIcon("fubar") and generate size and name from that
44 print "Grepping $source for requested icons...\n";
45 my @results = `grep -r Icon\\(\\" $source`;
46 foreach(@results) {
47   next unless my ($type, $name) = /\W+(\s|Desktop|Bar|MainBar|Small|Panel|Dialog)Icon\("([-\w]+)/;
48   $type = "Desktop" if $type =~ /\s+/;
49   $req_icons{$name} = 1
50     unless exists $blacklist{$name};
51 }
52
53 # Add extra icons
54 open EXTRA, "<$extrafile" or die "Could not open $extrafile\n";
55 while(<EXTRA>) {
56   s/#.*//;
57   next unless my ($name) = /([-\w]+)\s*/;
58   $req_icons{$name} = 1;
59 }
60 close EXTRA;
61
62 # Clean old output dir
63 print "Removing old $output...\n";
64 system("rm -rf $output");
65
66 # Now copy the icons
67 my %scalables;
68
69 print "Copying icons from $oxygen...\n";
70 opendir (BASEDIR, "$oxygen") or die "Could not open oxygen basedir\n";
71 foreach my $sizestr (readdir BASEDIR) {
72   next unless $sizestr =~ /\d+x\d+/;
73   opendir (SIZEDIR, "$oxygen/$sizestr") or die "Could not open dir $sizestr\n";
74   foreach my $cat (readdir SIZEDIR) {
75     next if $cat eq '.' or $cat eq '..';
76     #system "mkdir -p $output/$sizestr/$cat" and die "Could not create category dir\n";
77     system "mkdir -p $output/scalable/$cat" and die "Could not create category dir\n";
78     opendir (CATDIR, "$oxygen/$sizestr/$cat") or die "Could not open category dir\n";
79     foreach my $icon (readdir CATDIR) {
80       $icon =~ s/\.png$//;
81       next unless exists $req_icons{$icon};
82       $scalables{$cat}{$icon} = 1;
83       system "mkdir -p $output/$sizestr/$cat" and die "Could not create category dir\n";
84       system "cp -a $oxygen/$sizestr/$cat/$icon.png $output/$sizestr/$cat"
85         and die "Error while copying file $sizestr/$cat/$icon.png\n";
86       #print "Copy: $oxygen/$sizestr/$cat/$icon.png\n";
87       $found_icons{$icon} = 1;
88     }
89     closedir CATDIR;
90   }
91   closedir SIZEDIR;
92 }
93 closedir BASEDIR;
94
95 # Copy scalables
96 foreach my $cat (keys %scalables) {
97   system "mkdir -p $output/scalable/$cat" and die "Could not create category dir\n";
98   foreach my $scalable (keys %scalables{$cat}) {
99     system "cp -a $oxygen/scalable/$cat/$scalable.svgz $output/scalable/$cat/$scalable.svgz";
100   }
101 }
102
103 # Warn if we have still icons left
104 foreach my $icon (keys %req_icons) {
105   next if defined $found_icons{$icon};
106   print "Warning: Missing icon $icon\n";
107 }
108
109 # Generate .qrc
110 my @file_list;
111 generate_qrc($output, $qrcfile_kde);
112
113 # Copy license etc.
114 system "cp $oxygen/AUTHORS $oxygen/CONTRIBUTING $oxygen/COPYING $oxygen/index.theme $output/";
115
116 print "Done.\n";
117
118 ########################################################################################
119 sub generate_qrc {
120   my $dir = shift;
121   my $qrcfile = shift;
122
123   @file_list = ();
124   find(\&push_icon_path, $dir);
125   my $files = join "\n", @file_list;
126
127   my $qrc = "<RCC>\n"
128            ."  <qresource prefix=\"/icons\">\n"
129            ."$files\n"
130            ."  </qresource>\n"
131            ."</RCC>\n";
132
133   open QRC, ">$qrcfile" or die "Could not open $qrcfile for writing!\n";
134   print QRC $qrc;
135   close QRC;
136 }
137
138 sub push_icon_path {
139   return unless /\.png$/;
140
141   push @file_list, "    <file>$File::Find::name</file>";
142 }