File: Icondef.tcl

package info (click to toggle)
coccinella 0.96.20-7
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 13,108 kB
  • ctags: 5,908
  • sloc: tcl: 124,744; xml: 206; makefile: 66; sh: 62
file content (270 lines) | stat: -rw-r--r-- 7,208 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#  Icondef.tcl --
#  
#       This file is part of The Coccinella application. 
#       It implements parsing of iconsets as specified by the format in common
#       use within the jabber community. It can appear in three variants:
#       
#         1) a selfcontained icondef.xml file with all data base64 encoded
#         2) a folder with all data in separate files with an icondef.xml
#            describing their usage
#         3) as 2 but zipped up with a .jisp extension
#      
#       It is so far only written for roster style formats and not the
#       slightly different emoticon format.
#      
#       Format:
#      
#      	<icon>
#		<x xmlns='name'>status/away</x>
#		<object mime='image/png'>away.png</object>
#	</icon>
#	
#       is parsed into 'imageArr(status/away) [Image away.png]'
#
#  Copyright (c) 2005  Mats Bengtsson
#  
#   This program is free software: you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#   
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#   
#   You should have received a copy of the GNU General Public License
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.
#  
# $Id: Icondef.tcl,v 1.6 2008-05-22 13:00:26 matben Exp $

package provide Icondef 1.0

namespace eval ::Icondef {

    variable priv
    
    set priv(havezip) 0
    set priv(initted) 0
    
    # We must have support for these.
    set priv(formats) {gif png}
}

proc ::Icondef::Init {} {
    variable priv
    
    # We need the 'vfs::zip' package and if not using starkit we also need
    # the 'Memchan' package which is not automatically checked for.
    # 'rechan' is the tclkits built in version of 'Memchan'.
    if {[catch {package require vfs::zip}]} {
	set priv(havezip) 0
    } elseif {![catch {package require rechan}]} {
	set priv(havezip) 1
    } elseif {![catch {package require Memchan}]} {
	set priv(havezip) 1
    } else {
	set priv(havezip) 0
    }    
    set priv(initted) 1
}

# Icondef::Load --
# 
#       Loads an icon set and creates the necessary images.
#       
# Arguments:
#       path        file path to dir or jisp dir.
#       imageArr    name of array to store mapping key -> image
#       metaArr     name of array for meta data
#       
# Results:
#       name of iconset from file or dir name.

proc ::Icondef::Load {path imageArr metaArr} {    
    variable priv
    
    if {!$priv(initted)} {
	Init
    }
    
    # The dir variable points to the (virtual) directory containing it all.
    set dir $path
    set name [file rootname [file tail $path]]
    
    if {[string equal [file extension $path] ".jisp"]} {
	if {$priv(havezip)} {
	    set mountpath [file join [file dirname $path] $name]
	    if {[catch {
		set fdzip [vfs::zip::Mount $path $mountpath]
	    } err]} {
		return -code error $err
	    }
	    
	    # We cannot be sure of that the name of the archive is identical 
	    # with the name of the original directory.
	    set zipdir [lindex [glob -nocomplain -directory $mountpath -- *] 0]
	    set dir $zipdir
	} else {
	    return -code error "Cannot read jisp archive without vfs::zip"
	}
    }
    set icondefPath [file join $dir icondef.xml]
    if {![file isfile $icondefPath]} {
	return -code error "missing icondef.xml file in archive"
    }
    set fd [open $icondefPath]
    fconfigure $fd -encoding utf-8
    set xmldata [read $fd]
    close $fd
	
    # Parse data.
    ParseIconDef $imageArr $metaArr $dir $xmldata
    
    if {[info exists mountpath]} {
	vfs::zip::Unmount $fdzip $mountpath
    }
    return $name
}

proc ::Icondef::ParseIconDef {imageArr metaArr dir xmldata} {

    set token [tinydom::parse $xmldata]
    set xmllist [tinydom::documentElement $token]
    
    foreach elem [tinydom::children $xmllist] {
	
	switch -- [tinydom::tagname $elem] {
	    meta {
		ParseMeta $metaArr $dir $elem
	    }
	    icon {
		ParseIcon $imageArr $dir $elem
	    }
	}
    }
    tinydom::cleanup $token
}

proc ::Icondef::ParseMeta {metaArr dir xmllist} {
    upvar #0 $metaArr meta
    
    foreach elem [tinydom::children $xmllist] {
	set tag [tinydom::tagname $elem]
	lappend meta($tag) [tinydom::chdata $elem]
    }
}

proc ::Icondef::ParseIcon {imageArr dir xmllist} {    
    upvar #0 $imageArr imArr
    variable priv

    set mime ""
    
    foreach elem [tinydom::children $xmllist] {
	set tag [tinydom::tagname $elem]
	
	switch -- $tag {
	    x {
		set key [tinydom::chdata $elem]
	    }
	    data {
		# base64 coded image data
		set data [tinydom::chdata $elem]
		array set attr [tinydom::attrlist $elem]
		set mime $attr(mime)
	    }
	    object {
		set object [tinydom::chdata $elem]
		array set attr [tinydom::attrlist $elem]
		set mime $attr(mime)
	    }
	}
    }
    
    set format [lindex [split $mime /] 1]
    if {[lsearch $priv(formats) $format] < 0} {
	return
    }
    
    if {[info exists data]} {
	set im [image create photo -format $format -data $data]
    } else {
	set im [image create photo -format $format  \
	  -file [file join $dir $object]]
    }
    set imArr($key) $im
}

# set path /Users/mats/Tcl/Coccinella/coccinella/iconsets/service/default
# ::Icondef::DisplayWindow $path

proc ::Icondef::DisplayWindow {path} {
    variable idata
    
    unset -nocomplain idata
    set name [::Icondef::Load $path  \
      [namespace current]::idata     \
      [namespace current]::mdata]

    set i 0
    set wtop [ui::autoname]
    set w $wtop.f
    toplevel $wtop
    pack [ttk::frame $w -padding 20]
    foreach {type image} [array get idata] {
	ttk::label $w.t$i -text $type
	ttk::label $w.i$i -image $image
	grid  $w.t$i $w.i$i -sticky e -padx 10 -pady 2
	incr i
    }
}

#  ::Icondef::Decode /Users/mats/Tcl/Coccinella/coccinella/iconsets/roster/aim/default/icondef.xml

proc ::Icondef::Decode {fileName} {
  
    set decoded [file join [file dirname $fileName] decoded]
    file mkdir $decoded
    
    set fd [open $fileName]
    fconfigure $fd -encoding utf-8
    set xmldata [read $fd]
    close $fd
    
    set token [tinydom::parse $xmldata]
    set xmllist [tinydom::documentElement $token]
    
    foreach iconE [tinydom::children $xmllist] {
	if {[tinydom::tagname $iconE] eq "icon"} {
	    unset -nocomplain key data
	    foreach elem [tinydom::children $iconE] {
		set tag [tinydom::tagname $elem]
		switch -- $tag {
		    x {
			set key [tinydom::chdata $elem]
		    }
		    data {
			# base64 coded image data
			set data [tinydom::chdata $elem]
			array set attr [tinydom::attrlist $elem]
			set mime $attr(mime)
		    }
		}
	    }
	    set new [join [split $key /] -]
	    set ext [lindex [split $mime /] 1]
	    set dstName [file join $decoded $new.$ext]
	    
	    set fd [open $dstName w]
	    fconfigure $fd -translation binary
	    puts -nonewline $fd [::base64::decode [string trim $data]]
	    close $fd
	    
	    
	}
    }
    tinydom::cleanup $token
}