Add missing index.theme to the bundled Oxygen icon theme
[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 QIcon::fromTheme\\(\\" $source`;
46 foreach(@results) {
47   next unless my ($name) = /\W+QIcon::fromTheme\(\"([-\w]+)/;
48   $req_icons{$name} = 1
49     unless exists $blacklist{$name};
50 }
51
52 # Add extra icons
53 open EXTRA, "<$extrafile" or die "Could not open $extrafile\n";
54 while(<EXTRA>) {
55   s/#.*//;
56   next unless my ($name) = /([-\w]+)\s*/;
57   $req_icons{$name} = 1;
58 }
59 close EXTRA;
60
61 # Clean old output dir
62 print "Removing old $output...\n";
63 system("rm -rf $output");
64
65 # Now copy the icons
66 my %scalables;
67
68 print "Copying icons from $oxygen...\n";
69 opendir (BASEDIR, "$oxygen") or die "Could not open oxygen basedir\n";
70 foreach my $sizestr (readdir BASEDIR) {
71   next unless $sizestr =~ /\d+x\d+/;
72   opendir (SIZEDIR, "$oxygen/$sizestr") or die "Could not open dir $sizestr\n";
73   foreach my $cat (readdir SIZEDIR) {
74     next if $cat eq '.' or $cat eq '..';
75     opendir (CATDIR, "$oxygen/$sizestr/$cat") or die "Could not open category dir\n";
76     foreach my $icon (readdir CATDIR) {
77       $icon =~ s/\.png$//;
78       next unless exists $req_icons{$icon};
79       $scalables{$cat}{$icon} = 1;
80       system "mkdir -p $output/$sizestr/$cat" and die "Could not create category dir\n";
81       system "cp -a $oxygen/$sizestr/$cat/$icon.png $output/$sizestr/$cat"
82         and die "Error while copying file $sizestr/$cat/$icon.png\n";
83       #print "Copy: $oxygen/$sizestr/$cat/$icon.png\n";
84       $found_icons{$icon} = 1;
85     }
86     closedir CATDIR;
87   }
88   closedir SIZEDIR;
89 }
90 closedir BASEDIR;
91
92 # Copy scalables
93 foreach my $cat (keys %scalables) {
94   system "mkdir -p $output/scalable/$cat" and die "Could not create category dir\n";
95   foreach my $scalable (keys %scalables{$cat}) {
96     system "cp -a $oxygen/scalable/$cat/$scalable.svgz $output/scalable/$cat/$scalable.svgz";
97   }
98 }
99
100 # Warn if we have still icons left
101 foreach my $icon (keys %req_icons) {
102   next if defined $found_icons{$icon};
103   print "Warning: Missing icon $icon\n";
104 }
105
106 # Copy license etc.
107 system "cp $oxygen/AUTHORS $oxygen/CONTRIBUTING $oxygen/COPYING $oxygen/index.theme $output/";
108
109 # Generate .qrc
110 my @file_list;
111 generate_qrc($output, $qrcfile_kde);
112
113 print "Done.\n";
114
115 ########################################################################################
116 sub generate_qrc {
117   my $dir = shift;
118   my $qrcfile = shift;
119
120   @file_list = ();
121   find(\&push_icon_path, $dir);
122   my $files = join "\n", @file_list;
123
124   my $qrc = "<RCC>\n"
125            ."  <qresource prefix=\"/icons\">\n"
126            ."$files\n"
127            ."  </qresource>\n"
128            ."</RCC>\n";
129
130   open QRC, ">$qrcfile" or die "Could not open $qrcfile for writing!\n";
131   print QRC $qrc;
132   close QRC;
133 }
134
135 sub push_icon_path {
136   return unless /\.png$/ or /^index.theme$/;
137
138   push @file_list, "    <file>$File::Find::name</file>";
139 }