Use a unicode string instead of the escaped version.
[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 $quassel_icons = "oxygen";
24 my $output = "oxygen_kde";
25 my $qrcfile_quassel = "oxygen.qrc";
26 my $qrcfile_kde = "oxygen_kde.qrc";
27
28 my $extrafile = "import/extra-icons";
29 my $blacklistfile = "import/blacklisted-icons";
30
31 my %sizes = (
32         Desktop => 48,
33         Bar => 22,
34         MainBar => 22,
35         Small => 16,
36         Panel => 32,
37         Dialog => 22
38 );
39
40 my %req_icons;
41 my %blacklist;
42 my %extra;
43
44 # First, load the icon blacklist
45 # Format: icon-name 16 22 32
46 open BLACKLIST, "<$blacklistfile" or die "Could not open $blacklistfile\n";
47 while(<BLACKLIST>) {
48   s/#.*//;
49   next unless my ($name, $sizes) = /([-\w]+)\s+(\d+(?:\s+\d+)*)?/;
50   $blacklist{$name} = $sizes;
51 }
52 close BLACKLIST;
53
54 # We now grep the source for things like SmallIcon("fubar") and generate size and name from that
55 print "Grepping $source for requested icons...\n";
56 my @results = `grep -r Icon\\(\\" $source`;
57 foreach(@results) {
58   next unless my ($type, $name) = /\W+(\s|Desktop|Bar|MainBar|Small|Panel|Dialog)Icon\("([-\w]+)/;
59   $type = "Desktop" if $type =~ /\s+/;
60   my $size = $sizes{$type};
61   $req_icons{$size}{$name} = 1
62     unless exists $blacklist{$name} and ($blacklist{$name} == undef or $blacklist{$name} =~ /$size/);
63 }
64
65 # Add extra icons
66 open EXTRA, "<$extrafile" or die "Could not open $extrafile\n";
67 while(<EXTRA>) {
68   s/#.*//;
69   next unless my ($name, $sizes) = /([-\w]+)\s+(\d+(?:\s+\d+)*)/;
70   foreach(split /\s+/, $sizes) {
71     $req_icons{$_}{$name} = 1;
72   }
73 }
74 close EXTRA;
75
76 # Clean old output dir
77 print "Removing old $output...\n";
78 system("rm -rf $output");
79
80 # Now copy the icons
81 my %scalables;
82
83 print "Copying icons from $oxygen...\n";
84 foreach my $size (keys %req_icons) {
85   my $sizestr = $size.'x'.$size;
86   opendir (BASEDIR, "$oxygen/$sizestr") or die "Could not open dir for size $size\n";
87   foreach my $cat (readdir BASEDIR) {
88     next if $cat eq '.' or $cat eq '..';
89     system "mkdir -p $output/$sizestr/$cat" and die "Could not create category dir\n";
90     system "mkdir -p $output/scalable/$cat" and die "Could not create category dir\n";
91     opendir (CATDIR, "$oxygen/$sizestr/$cat") or die "Could not open category dir\n";
92     foreach my $icon (readdir CATDIR) {
93       $icon =~ s/\.png$//;
94       next unless exists $req_icons{$size}{$icon};
95       $scalables{"$cat/$icon"} = 1;
96       system "cp -a $oxygen/$sizestr/$cat/$icon.png $output/$sizestr/$cat"
97         and die "Error while copying file $sizestr/$cat/$icon.png\n";
98       # print "Copy: $oxygen/$sizestr/$cat/$icon.png\n";
99       delete $req_icons{$size}{$icon};
100     }
101     closedir CATDIR;
102   }
103   closedir BASEDIR;
104 }
105
106 # Copy scalables
107 foreach my $scalable (keys %scalables) {
108   system "cp -a $oxygen/scalable/$scalable.svgz $output/scalable/$scalable.svgz";
109 }
110
111 # Warn if we have still icons left
112 foreach my $size (keys %req_icons) {
113   foreach my $missing (keys %{ $req_icons{$size} }) {
114     print "Warning: Missing icon $missing (size $size)\n";
115   }
116 }
117
118 # Generate .qrc
119 my @file_list;
120 generate_qrc($quassel_icons, $qrcfile_quassel);
121 generate_qrc($output, $qrcfile_kde);
122
123 print "Done.\n";
124
125 ########################################################################################
126 sub generate_qrc {
127   my $dir = shift;
128   my $qrcfile = shift;
129
130   @file_list = ();
131   find(\&push_icon_path, $dir);
132   my $files = join "\n", @file_list;
133
134   my $qrc = "<RCC>\n"
135            ."  <qresource prefix=\"/icons\">\n"
136            ."$files\n"
137            ."  </qresource>\n"
138            ."</RCC>\n";
139
140   open QRC, ">$qrcfile" or die "Could not open $qrcfile for writing!\n";
141   print QRC $qrc;
142   close QRC;
143 }
144
145 sub push_icon_path {
146   return unless /\.png$/;
147   my $alias = $File::Find::name;
148   $alias =~ s,^[^/]*(.*),$1,;
149
150   push @file_list, "    <file alias=\"oxygen$alias\">$File::Find::name</file>";
151 }