1#!/usr/bin/python -Qwarnall
2
3# This is a simple script to check which icon files are not referenced
4# from either mark-up'ed icon names or desktop files.
5# Call it from the toplevel source dir of Calligra.
6
7import sys, subprocess, polib
8
9dotDesktopIconsFileName = "dotDesktopIconFiles.list"
10calligraIconsFileName = "calligra-icons.list"
11
12
13# TODO: make path a variable
14def createCalligraIconFile(calligraIconsFile):
15    r = subprocess.call( ("find . " +
16            "-type d \( -name 'doc' -o " +
17                       "-name 'docs' -o " +
18                       "-name 'tests' \) -prune -o " +
19            "! -path './braindump/data/states/states/*' " +
20            "! -path './gemini/themes/*' " +
21            "! -path './karbon/stencils/*' " +
22            "! -path './plugins/karbonplugins/tools/CalligraphyTool/tutorial/*' " +
23            "! -path './sheets/data/sheetstyles/*' " +
24            "! -path './stage/pics/animations/*' " + # names are calculated when used, needs more complex check
25            "! -path './words/templates/*' " + # find out where gemini style templates refer icon used
26            "\( -name '*.png' -o -name '*.svg' -o -name '*.svgz' \) -printf '%%f\n' | " +
27            "sed -e 's/\.png$//' -e 's/\.svg$//' -e 's/\.svgz$//' | " +
28            "sort -u > %s") % (calligraIconsFile), shell=True)
29    return r
30
31
32# TODO: make path a variable
33def createDotDesktopIconFile():
34    r = subprocess.call("grep 'Icon=' . -R --exclude-dir='karbon/stencils' --include=\*.desktop > " + dotDesktopIconsFileName, shell=True)
35    return r
36
37
38# TODO: make path a variable
39def createPotFile():
40    r = subprocess.call("find . -name '*.cpp' -o -name '*.cc' -o -name '*.h' -o -name '*.c' | sort > koIconFiles.list", shell=True)
41    if r:
42        return r
43    r = subprocess.call("xgettext --from-code=UTF-8 -k " +
44             "-kkoIcon:1 " +
45             "-kkoIconName:1 " +
46             "-kkoIconNameCStr:1 " +
47             "-kkoSmallIcon:1 " +
48             "-kkoDesktopIcon:1 " +
49
50             "-kkoIconNeeded:1c,2 " +
51             "-kkoIconNeededWithSubs:1c,2 " +
52             "-kkoIconNameNeeded:1c,2 " +
53             "-kkoIconNameNeededWithSubs:1c,2 " +
54             "-kkoIconNameCStrNeeded:1c,2 " +
55             "-kkoIconNameCStrNeededWithSubs:1c,2 " +
56
57             "-kkoIconWanted:1c,2 " +
58             "-kkoIconNameWanted:1c,2 " +
59
60             "-D . --files-from=koIconFiles.list -o koIconNames.po -n", shell=True)
61    return r
62    #TODO: use Python pipes and/or clean-up helper files
63
64sizePrefixes = (
65    "16-", "22-", "24-", "32-", "48-", "64-", "128-", "256-", "512-", "1024-", "sc-"
66)
67groupPrefixes = [
68    # KDE 3 compatibility
69    "mime-", "filesys-", "device-", "app-", "action-",
70    # KDE 4 / icon naming specification compatibility
71    "mimetypes-", "places-", "devices-", "apps-", "actions-", "categories-",
72    "status-", "emblems-", "emotes-", "animations-", "intl-"
73]
74
75elsewhereUsedIcons = (
76    "application-x-sqlite2",
77    "application-x-sqlite3",
78    "calligra-logo-black-glow",
79    "calligra-logo-white-glow",
80    "layout-elements", # stage UI element
81    "questionmark", # for unknown shapes
82    "cursor_shear", "cursor_rotate", "cursor_connect", "zoom_out_cursor", "zoom_in_cursor" # cursor images
83)
84
85
86# returns map of iconnames with used filenames
87def readIcons(fileName):
88    iconNames = {}
89    with open(fileName, "r") as f:
90        for line in f:
91            iconFileName = line.strip().lower()
92            if iconFileName:
93                iconName = iconFileName;
94                if iconName.startswith(sizePrefixes):
95                    iconName = iconName.split('-',2)[2]
96
97                if iconName in iconNames:
98                    iconNames[iconName].append(iconFileName)
99                else:
100                    iconNames[iconName] = [iconFileName];
101    return iconNames
102
103
104def readDotDesktopIcons():
105    iconNames = set()
106    with open(dotDesktopIconsFileName, "r") as f:
107        for line in f:
108            line = line.strip().lower()
109            if line:
110                fileName, entry = line.split(':', 1)
111                iconName = entry.split('=', 1)[1]
112                if iconName:
113                    iconNames.add(iconName)
114    return iconNames
115
116
117def main():
118
119    r = createCalligraIconFile(calligraIconsFileName)
120    if r:
121        return r
122
123    calligraIcons = readIcons(calligraIconsFileName)
124
125    r = createPotFile()
126    if r:
127        return r
128
129    r = createDotDesktopIconFile()
130    if r:
131        return r
132
133    po = polib.pofile('koIconNames.po')
134
135    # collect icons and their occurrences
136    codeUsedIcons = set()
137
138    for entry in po:
139        codeUsedIcons.add(entry.msgid)
140
141    desktopFileUsedIcons = readDotDesktopIcons()
142
143    # output unused icons
144    for iconName, iconFileNames in sorted(calligraIcons.iteritems()):
145        if not iconName in codeUsedIcons and not iconName in desktopFileUsedIcons and not iconName in elsewhereUsedIcons:
146            print iconName
147            for filename in iconFileNames:
148                print '    %s' % (filename)
149
150    return 0
151
152if __name__ == '__main__':
153    main()
154