File: search.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 (466 lines) | stat: -rw-r--r-- 12,071 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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
# $Id: search.tcl,v 1.43 2006/01/20 03:08:00 aleksey Exp $

namespace eval search {
    set winid 0
    set show_all 0
}

proc search::open {jid args} {
    variable winid

    foreach {opt val} $args {	
	switch -- $opt {
	    -connection { set connid $val }
	}
    }
    if {![info exists connid]} {
	set connid [jlib::route $jid]
    }

    set sw .search$winid

    toplevel $sw -cursor watch
    wm group $sw .
    set title [format [::msgcat::mc "Search in %s"] $jid]
    wm title $sw $title
    wm iconname $sw $title
    wm transient $sw .
    if {$::tcl_platform(platform) == "macintosh"} {
        catch { unsupported1 style $sw floating sideTitlebar }
    } elseif {$::aquaP} {
        ::tk::unsupported::MacWindowStyle style $sw dBoxProc
    }
    wm resizable $sw 0 0

    wm withdraw $sw

    ButtonBox $sw.bbox -spacing 0 -padx 10 -default 0
    $sw.bbox add -text [::msgcat::mc "OK"] \
	-command [list search::search $sw $jid -connection $connid] \
        -state disabled
    $sw.bbox add -text [::msgcat::mc "Cancel"] -command [list destroy $sw]
    pack $sw.bbox -padx 2m -pady 2m -anchor e -side bottom

    bind $sw <Return> [list ButtonBox::invoke $sw.bbox default]
    bind $sw <Escape> [list ButtonBox::invoke $sw.bbox 1]

    Separator::create $sw.sep -orient horizontal
    pack $sw.sep -side bottom -fill x -pady 1m
    
    frame $sw.fields -class Search

    pack $sw.fields -expand yes -fill both -anchor nw -padx 2m -pady 2m

    jlib::send_iq get \
	[jlib::wrapper:createtag query \
	     -vars [list xmlns jabber:iq:search]] \
	-to $jid -command [list search::recv_fields $sw $jid] \
	-connection $connid
    
    incr winid
}

proc search::recv_fields {sw jid res child} {
    debugmsg search "$res $child"

    if {![cequal $res OK]} {
        destroy $sw
	MessageDlg ${sw}_err -aspect 50000 -icon error \
	    -message [format [::msgcat::mc "Search: %s"] \
			  [error_to_string $child]] \
	    -type user -buttons ok -default 0 -cancel 0
	return
    }

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

    if {[cequal [jlib::wrapper:getattr $vars xmlns] jabber:iq:search]} {
	data::fill_fields $sw.fields $children
    }

    $sw configure -cursor {}
    $sw.bbox itemconfigure 0 -state normal

    foreach child [winfo children $sw.fields] {
        if {[cequal [winfo class $child] Entry] && \
		[cequal [$child cget -state] normal]} {
	    focus $child
	    break
	}
    }
    update idletasks
    wm deiconify $sw
}

proc search::search {sw jid args} {
    variable data

    foreach {opt val} $args {	
	switch -- $opt {
	    -connection { set connid $val }
	}
    }
    if {![info exists connid]} {
	set connid [jlib::route $jid]
    }

    $sw configure -cursor watch
    $sw.bbox itemconfigure 0 -state disabled

    set restags [data::get_tags $sw.fields]

    jlib::send_iq set \
	[jlib::wrapper:createtag query \
	     -vars [list xmlns jabber:iq:search] \
	     -subtags $restags] \
	-to $jid -command [list search::recv_items $sw $jid $connid] \
	-connection $connid
}

proc search::recv_items {sw jid connid res child} {
    global font
    variable lastsort

    debugmsg search "$res $child"

    if {![winfo exists $sw]} {
	return
    }

    if {![cequal $res OK]} {
	$sw configure -cursor {}
	$sw.bbox itemconfigure 0 -text [::msgcat::mc "Try again"] \
	    -command [list search::search_again $sw $jid $connid errormsg] \
	    -state normal
	$sw.bbox itemconfigure 1 -text [::msgcat::mc "Close"]

	if {[winfo exists $sw.errormsg]} {
	    destroy $sw.errormsg
	}

	message $sw.errormsg -aspect 50000 -font $font \
	    -text [format [::msgcat::mc "An error occured when searching in %s\n\n%s"] \
		       $jid [error_to_string $child]]

	pack $sw.errormsg -expand yes -fill both -after $sw.fields -anchor nw -padx 1c -pady 1c
	pack forget $sw.fields

	return
    }

    wm withdraw $sw

    set rw [toplevel ${sw}results]
    wm group $rw .
    set title [format [::msgcat::mc "Search in %s"] $jid]
    wm title $rw $title
    wm iconname $rw $title
    wm withdraw $rw

    ButtonBox $rw.bbox -spacing 0 -padx 10 -default 0
    $rw.bbox add -text [::msgcat::mc "Search again"] \
	-command "destroy [list $rw]
		  search::search_again [list $sw] [list $jid] [list $connid]"
    $rw.bbox add -text [::msgcat::mc "Close"] -command "destroy [list $rw]
							destroy [list $sw]"
    pack $rw.bbox -padx 2m -pady 2m -anchor e -side bottom

    bind $rw <Return> [list ButtonBox::invoke $rw.bbox default]
    bind $rw <Escape> [list ButtonBox::invoke $rw.bbox 1]

    Separator::create $rw.sep -orient horizontal
    pack $rw.sep -side bottom -fill x -pady 1m

    set sww [ScrolledWindow $rw.items]

    ::mclistbox::mclistbox $sww.listbox \
	-resizeonecolumn 1 \
	-font $font \
	-labelfont $font \
	-labelanchor w \
	-width 90 \
	-height 16

    pack $sww -expand yes -fill both -anchor nw -padx 2m -pady 2m
    $sww setwidget $sww.listbox

    set lastsort($sww.listbox) ""
    bind $sww.listbox <Destroy> +[list [namespace current]::delete_lastsort $sww.listbox]
    
    bind $sww.listbox <3> \
	"[namespace current]::select_and_popup_menu [list $sww.listbox] \
	     \[$sww.listbox nearest \[::mclistbox::convert %W -y %y\]\] \
	     $connid"

    bindscroll $sww $sww.listbox

    set reported_fields [data::get_reported_fields $sw.fields]

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

    if {[cequal [jlib::wrapper:getattr $vars xmlns] jabber:iq:search]} {
	set rows [fill_mclistbox $rw $jid $sww.listbox $reported_fields $children]
    }

    if {$rows <= 0} {
	pack forget $sww
	message $rw.errormsg -aspect 50000 -font $font \
	    -text [format [::msgcat::mc "Search in %s: No matching items found"] $jid]
	pack $rw.errormsg -expand yes -fill both -anchor nw -padx 1c -pady 1c
    } elseif {$rows <= 12} {
	$sww.listbox configure -height [expr {$rows - ($rows % 4) + 4}]
    }

    wm deiconify $rw
}

proc search::delete_lastsort {id} {
    variable lastsort

    if {[info exists lastsort($id)]} {
	unset lastsort($id)
    }
}

proc search::fill_mclistbox_x {sw jid w reported_fields items} {
    variable show_all

    set width(0) 3
    set name(0) N
    $w column add N -label " [::msgcat::mc #] "

    set row 0
    set col 1

    foreach item $items {
	jlib::wrapper:splitxml $item tag vars isempty chdata children

	switch -- $tag {
	    title {
		if {$chdata != ""} {
		    wm title $sw $chdata
		    wm iconname $sw $chdata
		}
	    }
	    reported {
		set reported_fields {}
		foreach field $children {
		    jlib::wrapper:splitxml $field tag vars isempty chdata children1

		    set lname [jlib::wrapper:getattr $vars "var"]
		    set label_name($lname) [jlib::wrapper:getattr $vars "label"]
		    lappend reported_fields $lname
		}
	    }
	    item {
		foreach field $children {
		    jlib::wrapper:splitxml $field tag vars isempty chdata children1

		    if {$tag == "field"} {
			set var [jlib::wrapper:getattr $vars "var"]

			foreach value $children1 {
			    
			    jlib::wrapper:splitxml $value tag vars isempty chdata children1

			    if {($tag == "value") && ($chdata != "")} {
				if {$show_all || ([lsearch -exact $reported_fields $var] != -1)} {
				    if {![info exists fieldcol($var)]} {
					set fieldcol($var) $col
					if {[info exists label_name($var)]} {
					    set l $label_name($var)
					} else {
					    set l $var
					}
					set width($col) [string length " $l "]
					set name($col) $var
					$w column add $var -label " $l "
					$w label bind $var <ButtonPress-1> "[namespace current]::sort %W $var"
					set lasttag $var

					incr col
				    }
				    set data($fieldcol($var),$row) $chdata

				    debugmsg search "$var $chdata"
				}
			    }
			}
		    }
		}
		set data(0,$row) [expr {$row + 1}]
		incr row
	    }
	    default {}
	}	
    }

    finalize_mclistbox $w $row $col name data width
}

proc search::finalize_mclistbox {w row col n d wi} {
    upvar $n name
    upvar $d data
    upvar $wi width

    $w column add lastcol -label "" -width 0
    $w configure -fillcolumn lastcol
    
    for {set j 0} {$j < $row} {incr j} {
	set datalist {}
	for {set i 0} {$i < $col} {incr i} {
	    if {[info exists data($i,$j)]} {
		set wd [string length " $data($i,$j) "]
		if {$wd > $width($i)} {
		    set width($i) $wd
		}
		lappend datalist " $data($i,$j) "
	    } else {
		lappend datalist ""
	    }
	}
	lappend datalist ""
	$w insert end $datalist
    }
    for {set i 0} {$i < $col} {incr i} {
	$w column configure $name($i) -width $width($i)
    }

    return $row
}

proc search::fill_mclistbox {sw jid w reported_fields items} {
    variable show_all

    foreach item $items {
	jlib::wrapper:splitxml $item tag vars isempty chdata children
	if {$tag == "x" &&
	    [jlib::wrapper:getattr $vars xmlns] == "jabber:x:data"} {
	    return [fill_mclistbox_x $sw $jid $w $reported_fields $children]
	}
    }

    set width(0) 3
    set name(0) N
    $w column add N -label " [::msgcat::mc #] "

    set fieldcol(jid) 1
    set width(1) 5
    set name(1) jid
    $w column add jid -label " jid "
    $w label bind jid <ButtonPress-1> "[namespace current]::sort %W jid"
 
    set row 0
    set col 2

    foreach item $items {
	jlib::wrapper:splitxml $item tag vars isempty chdata children

	switch -- $tag {
	    item {
		set itemjid [jlib::wrapper:getattr $vars jid]
		set data(1,$row) $itemjid

		foreach field $children {
		    jlib::wrapper:splitxml $field tag vars isempty \
			chdata children1

		    if {$chdata != ""} {
			if {$show_all || ([lsearch -exact $reported_fields $tag] != -1)} {
			    if {![info exists fieldcol($tag)]} {
				set fieldcol($tag) $col
				set width($col) [string length " $tag "]
				set name($col) $tag
				$w column add $tag -label " $tag "
				$w label bind $tag <ButtonPress-1> "[namespace current]::sort %W $tag"
				set lasttag $tag

				incr col
			    }
			    set data($fieldcol($tag),$row) $chdata

			    debugmsg search "$tag $chdata"
			}
		    }
		}
		set data(0,$row) [expr {$row + 1}]
		incr row
	    }
	    default {}
	}	
    }

    finalize_mclistbox $w $row $col name data width
}

proc search::sort {w tag} {
    variable lastsort

    set data [$w get 0 end]
    set index [lsearch -exact [$w column names] $tag]
    if {$lastsort($w) != $tag} {
	set result [lsort -dictionary -index $index $data]
	set lastsort($w) $tag
    } else {
	set result [lsort -decreasing -dictionary -index $index $data]
	set lastsort($w) ""
    }
    set result1 {}
    set i 0
    foreach row $result {
	lappend result1 [lreplace $row 0 0 " [incr i] "]
    }
    $w delete 0 end
    eval $w insert end $result1
}

proc search::search_again {sw jid connid {delwidget ""}} {
    $sw configure -cursor {}
    if {![cequal $delwidget ""]} {
	pack $sw.fields -expand yes -fill both -after $sw.$delwidget -anchor nw -padx 2m -pady 2m
	pack forget $sw.$delwidget

	$sw.bbox itemconfigure 0 -text [::msgcat::mc "OK"] \
	    -command [list search::search $sw $jid -connection $connid] \
	    -state normal
	$sw.bbox itemconfigure 1 -text [::msgcat::mc "Cancel"]
    } else {
	$sw.bbox itemconfigure 0 -state normal
	wm deiconify $sw
    }
}

proc search::select_and_popup_menu {w index connid} {

    $w selection clear 0 end
    $w selection set $index
    set jid [string trim [lindex [$w get $index] 1]]

    if {[winfo exists [set m .searchpopupmenu]]} {
	destroy $m
    }
    menu $m -tearoff 0

    hook::run search_popup_menu_hook $m $connid $jid

    tk_popup $m [winfo pointerx .] [winfo pointery .]
}

proc search::add_separator {m connid jid} {
    $m add separator
}

hook::add search_popup_menu_hook \
    [namespace current]::search::add_separator 40
hook::add search_popup_menu_hook \
    [namespace current]::search::add_separator 50

hook::add postload_hook \
    [list browser::register_ns_handler jabber:iq:search \
	[namespace current]::search::open \
	-desc [list * [::msgcat::mc "Search"]]]
hook::add postload_hook \
    [list disco::browser::register_feature_handler jabber:iq:search \
	[namespace current]::search::open \
	-desc [list * [::msgcat::mc "Search"]]]