File: annotations.tcl

package info (click to toggle)
tkabber 0.9.9-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 5,028 kB
  • ctags: 1,798
  • sloc: tcl: 36,852; xml: 3,704; sh: 1,386; makefile: 67
file content (310 lines) | stat: -rw-r--r-- 8,874 bytes parent folder | download
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# $Id: annotations.tcl,v 1.6 2006/01/28 20:45:07 aleksey Exp $

# JEP-0145 Annotations support

namespace eval annotations {
    # variable to store roster notes
    array set notes {}

    variable NS
    set NS(private) "jabber:iq:private"
    set NS(rosternotes) "storage:rosternotes"
}

proc annotations::free_notes {connid} {
    variable notes

    array unset notes $connid,*
}

hook::add disconnected_hook [namespace current]::annotations::free_notes

proc annotations::request_notes {connid} {
    variable NS
    variable notes

    jlib::send_iq get \
	[jlib::wrapper:createtag query \
	     -vars [list xmlns $NS(private)] \
	     -subtags [list [jlib::wrapper:createtag storage \
				 -vars [list xmlns $NS(rosternotes)]]]] \
	-command [list [namespace current]::process_notes $connid] \
	-connection $connid
}

hook::add connected_hook [namespace current]::annotations::request_notes

proc annotations::process_notes {connid res child} {
    variable NS
    variable notes

    if {$res != "OK"} return

    array set notes {}

    jlib::wrapper:splitxml $child tag vars isempty cdata children

    foreach ch $children {
	jlib::wrapper:splitxml $ch tag1 vars1 isempty1 cdata1 children1

	if {[jlib::wrapper:getattr $vars1 xmlns] == $NS(rosternotes)} {
	    foreach note $children1 {
		jlib::wrapper:splitxml $note ntag nvars nisempty ncdata nchildren

		set jid [jlib::wrapper:getattr $nvars jid]
		set cdate [jlib::wrapper:getattr $nvars cdate]
		set mdate [jlib::wrapper:getattr $nvars mdate]
		set notes($connid,jid,$jid) $jid
		if {![catch { scan_time $cdate } cdate]} {
		    set notes($connid,cdate,$jid) $cdate
		}
		if {![catch { scan_time $mdate } mdate]} {
		    set notes($connid,mdate,$jid) $mdate
		}
		set notes($connid,note,$jid) $ncdata
	    
	    }
	}
    }
}

proc annotations::scan_time {timestamp} {
    if {[regexp {(.*)T(.*)Z} $timestamp -> date time]} {
	return [clock scan "$date $time" -gmt true]
    } else {
	return [clock scan $timestamp -gmt true]
    }
}

proc annotations::cleanup_and_store_notes {connid} {
    variable notes

    set roster_jids {}
    foreach rjid [roster::get_jids $connid] {
	lappend roster_jids [node_and_server_from_jid $rjid]
    }

    foreach idx [array names notes $connid,jid,*] {
	set jid $notes($idx)
	if {[lsearch -exact $roster_jids $jid] < 0 || \
		![info exists notes($connid,note,$jid)] || \
		$notes($connid,note,$jid) == ""} {
	    catch { unset notes($connid,jid,$jid) }
	    catch { unset notes($connid,cdate,$jid) }
	    catch { unset notes($connid,mdate,$jid) }
	    catch { unset notes($connid,note,$jid) }
	}
    }

    store_notes $connid
}

proc annotations::store_notes {connid} {
    variable NS
    variable notes

    set notelist {}
    foreach idx [array names notes $connid,jid,*] {
	set jid $notes($idx)

	set vars [list jid $jid]
	if {[info exists notes($connid,cdate,$jid)]} {
	    set cdate [clock format $notes($connid,cdate,$jid) \
			     -format "%Y-%m-%dT%TZ" -gmt true]
	}
	lappend vars cdate $cdate
	if {[info exists notes($connid,mdate,$jid)]} {
	    set mdate [clock format $notes($connid,mdate,$jid) \
			     -format "%Y-%m-%dT%TZ" -gmt true]
	}
	lappend vars mdate $mdate
	if {[info exists notes($connid,note,$jid)] && \
		$notes($connid,note,$jid) != ""} {
	    lappend notelist \
		[jlib::wrapper:createtag note \
		     -vars $vars \
		     -chdata $notes($connid,note,$jid)]
	}
    }

    jlib::send_iq set \
	[jlib::wrapper:createtag query \
	     -vars [list xmlns $NS(private)] \
	     -subtags [list [jlib::wrapper:createtag storage \
				 -vars [list xmlns $NS(rosternotes)] \
				 -subtags $notelist]]] \
	-command [list [namespace current]::store_notes_result $connid] \
	-connection $connid
}

proc annotations::store_notes_result {connid res child} {

    if {$res == "OK"} return

    if {[winfo exists .store_notes_error]} {
	destroy .store_notes_error
    }
    MessageDlg .store_notes_error -aspect 50000 -icon error \
	-message [format [::msgcat::mc "Storing roster notes failed: %s"] \
			 [error_to_string $child]] \
	-type user -buttons ok -default 0 -cancel 0
}

proc annotations::add_user_popup_info {infovar connid jid} {
    variable notes
    upvar 0 $infovar info

    set jid [node_and_server_from_jid $jid]

    if {[info exists notes($connid,note,$jid)] && \
	    $notes($connid,note,$jid) != ""} {
	append info "\n\tNote:\t"
	append info [string map [list "\n" "\n\t\t"] "$notes($connid,note,$jid)"]

	if {0} {
	    if {[info exists notes($connid,cdate,$jid)]} {
		append info [format "\n\tNote created: %s" \
				 [clock format $notes($connid,cdate,$jid) \
					-format "%Y-%m-%d %T" -gmt false]]
	    }
	    if {[info exists notes($connid,mdate,$jid)]} {
		append info [format "\n\tNote modified: %s" \
				 [clock format $notes($connid,mdate,$jid) \
					-format "%Y-%m-%d %T" -gmt false]]
	    }
	}
    }
}

hook::add roster_user_popup_info_hook \
    [namespace current]::annotations::add_user_popup_info 80

proc annotations::show_dialog {connid jid} {
    variable notes
    global font

    set jid [node_and_server_from_jid $jid]

    set allowed_name [jid_to_tag $jid]
    set w .note_edit_${connid}_$allowed_name

    if {[winfo exists $w]} {
	destroy $w
    }

    Dialog $w -title [format [::msgcat::mc "Edit roster notes for %s"] $jid] \
	-modal none -separator 1 -anchor e \
	-default 0 -cancel 1

    $w add -text [::msgcat::mc "Store"] \
	-command [list [namespace current]::commit_changes $w $connid $jid]
    $w add -text [::msgcat::mc "Cancel"] -command [list destroy $w]

    set f [$w getframe]

    if {[info exists notes($connid,cdate,$jid)]} {
	label $f.cdate -text [format [::msgcat::mc "Created: %s"] \
				     [clock format $notes($connid,cdate,$jid) \
					    -format "%Y-%m-%d %T" -gmt false]]
	pack $f.cdate -side top -anchor w
    }

    if {[info exists notes($connid,mdate,$jid)]} {
	label $f.mdate -text [format [::msgcat::mc "Modified: %s"] \
				     [clock format $notes($connid,mdate,$jid) \
					    -format "%Y-%m-%d %T" -gmt false]]
	pack $f.mdate -side top -anchor w
    }

    ScrolledWindow $f.sw
    pack $f.sw -side top -expand yes -fill both
    textUndoable $f.note -width 50 -height 5 -wrap word -font $font
    if {[info exists notes($connid,note,$jid)]} {
	$f.note insert 0.0 $notes($connid,note,$jid)
    }
    $f.sw setwidget $f.note

    bind $f.note <Control-Key-Return> "$w invoke default
				       break"
    bind $w <Key-Return> { }
    bind $w <Control-Key-Return> "$w invoke default
				  break"

    $w draw $f.note
}

proc annotations::commit_changes {w connid jid} {
    variable notes

    set text [$w getframe].note

    set date [clock seconds]

    set notes($connid,jid,$jid) $jid
    if {![info exists notes($connid,cdate,$jid)]} {
	set notes($connid,cdate,$jid) $date
    }
    set notes($connid,mdate,$jid) $date
    set notes($connid,note,$jid) [$text get 0.0 "end -1 char"]

    cleanup_and_store_notes $connid

    destroy $w
}

proc annotations::prefs_user_menu {m connid jid} {
    set rjid [roster::find_jid $connid $jid]
    $m add command -label [::msgcat::mc "Edit item notes..."] \
	-command [list [namespace current]::show_dialog $connid $rjid]
}

hook::add chat_create_user_menu_hook \
    [namespace current]::annotations::prefs_user_menu 76
hook::add roster_conference_popup_menu_hook \
    [namespace current]::annotations::prefs_user_menu 76
hook::add roster_service_popup_menu_hook \
    [namespace current]::annotations::prefs_user_menu 76
hook::add roster_jid_popup_menu_hook \
    [namespace current]::annotations::prefs_user_menu 76

proc annotations::note_page {tab connid jid editable} {
    variable notes
    global font

    if {$editable} return

    set jid [node_and_server_from_jid $jid]

    if {![info exists notes($connid,note,$jid)] || \
	    $notes($connid,note,$jid) == ""} {
	return
    }

    set notestab [$tab insert end notes -text [::msgcat::mc "Notes"]]
    set n [userinfo::pack_frame $notestab.notes [::msgcat::mc "Roster Notes"]]

    if {[info exists notes($connid,cdate,$jid)]} {
	label $n.cdate -text [format [::msgcat::mc "Created: %s"] \
				     [clock format $notes($connid,cdate,$jid) \
					    -format "%Y-%m-%d %T" -gmt false]]
	pack $n.cdate -side top -anchor w
    }

    if {[info exists notes($connid,mdate,$jid)]} {
	label $n.mdate -text [format [::msgcat::mc "Modified: %s"] \
				     [clock format $notes($connid,mdate,$jid) \
					    -format "%Y-%m-%d %T" -gmt false]]
	pack $n.mdate -side top -anchor w
    }

    set sw [ScrolledWindow $n.sw -scrollbar vertical]
    text $n.text -font $font -height 12 -wrap word
    $sw setwidget $n.text
    $n.text insert 0.0 $notes($connid,note,$jid)
    $n.text configure -state disabled
    pack $sw -side top -fill both -expand yes
    pack $n -fill both -expand yes
}

hook::add userinfo_hook [namespace current]::annotations::note_page 40