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