Adapt Oxygen import script to the new realities
[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     #system "mkdir -p $output/$sizestr/$cat" and die "Could not create category dir\n";
76     system "mkdir -p $output/scalable/$cat" and die "Could not create category dir\n";
77     opendir (CATDIR, "$oxygen/$sizestr/$cat") or die "Could not open category dir\n";
78     foreach my $icon (readdir CATDIR) {
79       $icon =~ s/\.png$//;
80       next unless exists $req_icons{$icon};
81       $scalables{$cat}{$icon} = 1;
82       system "mkdir -p $output/$sizestr/$cat" and die "Could not create category dir\n";
83       system "cp -a $oxygen/$sizestr/$cat/$icon.png $output/$sizestr/$cat"
84         and die "Error while copying file $sizestr/$cat/$icon.png\n";
85       #print "Copy: $oxygen/$sizestr/$cat/$icon.png\n";
86       $found_icons{$icon} = 1;
87     }
88     closedir CATDIR;
89   }
90   closedir SIZEDIR;
91 }
92 closedir BASEDIR;
93
94 # Copy scalables
95 foreach my $cat (keys %scalables) {
96   system "mkdir -p $output/scalable/$cat" and die "Could not create category dir\n";
97   foreach my $scalable (keys %scalables{$cat}) {
98     system "cp -a $oxygen/scalable/$cat/$scalable.svgz $output/scalable/$cat/$scalable.svgz";
99   }
100 }
101
102 # Warn if we have still icons left
103 foreach my $icon (keys %req_icons) {
104   next if defined $found_icons{$icon};
105   print "Warning: Missing icon $icon\n";
106 }
107
108 # Generate .qrc
109 my @file_list;
110 generate_qrc($output, $qrcfile_kde);
111
112 # Copy license etc.
113 system "cp $oxygen/AUTHORS $oxygen/CONTRIBUTING $oxygen/COPYING $oxygen/index.theme $output/";
114
115 print "Done.\n";
116
117 ########################################################################################
118 sub generate_qrc {
119   my $dir = shift;
120   my $qrcfile = shift;
121
122   @file_list = ();
123   find(\&push_icon_path, $dir);
124   my $files = join "\n", @file_list;
125
126   my $qrc = "<RCC>\n"
127            ."  <qresource prefix=\"/icons\">\n"
128            ."$files\n"
129            ."  </qresource>\n"
130            ."</RCC>\n";
131
132   open QRC, ">$qrcfile" or die "Could not open $qrcfile for writing!\n";
133   print QRC $qrc;
134   close QRC;
135 }
136
137 sub push_icon_path {
138   return unless /\.png$/;
139
140   push @file_list, "    <file>$File::Find::name</file>";
141 }