File: tkdict

package info (click to toggle)
libnet-dict-perl 2.21-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 272 kB
  • ctags: 67
  • sloc: perl: 2,257; makefile: 11
file content (795 lines) | stat: -rwxr-xr-x 21,762 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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
#!/usr/bin/env perl
#
# tkdict - a Perl/Tk DICT client, for accessing network dictionary servers
#
# Neil Bowers <neil@bowers.com>
# Copyright (C) 2001-2002, Neil Bowers
#

use strict;
use warnings;

use Tk;
use Tk::Dialog;
use Net::Dict;
use AppConfig::Std;

use vars qw($PROGRAM $VERSION);
$VERSION = sprintf("%d.%d", q$Revision: 1.1.1.1 $ =~ /(\d+)\.(\d+)/);

my $warn_dialog;
my $dict_server;
my $word;
my $text_window;
my $bgcolor;
my $mw;
my $config;
my $help;
my ($info_top, $info_text, $info_title);
my $ht;
my %helpString;
my $dict;
my ($lookup_mode, $modeDisplay);
my $mbDefine;
my ($sframe, $strat_menu, $strategy, $strategyDisplay);
my ($db_frame, $db_menu, $db, $dbDisplay);
my $bar3;

main();
exit 0;


#=======================================================================
#
# main()
#
# This is the main body of tkdict
#
#=======================================================================
sub main
{
    initialise();
    create_gui();
    if ($config->host)
    {
        $dict_server = $config->host;
        select_server();
    }
    $mw->protocol('WM_DELETE_WINDOW', \&tkdict_exit);
    MainLoop();
}

#=======================================================================
#
# initialise()
#
# check config file and command-line
#
#=======================================================================
sub initialise
{
    #-------------------------------------------------------------------
    # Initialise misc global variables
    #-------------------------------------------------------------------
    $PROGRAM = "TkDict";
    $lookup_mode = "define";

    #-------------------------------------------------------------------
    # Create AppConfig::Std, define parameters, and parse command-line
    #-------------------------------------------------------------------
    $config = AppConfig::Std->new()
        || die "failed to create AppConfig::Std: $!\n";

    $config->define('host',       { ARGCOUNT => 1, ALIAS => 'h' });
    $config->define('port',       { ARGCOUNT => 1, ALIAS => 'p',
                                    DEFAULT => 2628 });
    $config->define('client',     { ARGCOUNT => 1, ALIAS => 'c',
				    DEFAULT => "$PROGRAM $VERSION ".
				"[using Net::Dict $Net::Dict::VERSION]",
				  });

    $config->args(\@ARGV)
        || die "run \"$PROGRAM -help\" to see valid options\n";

    #-------------------------------------------------------------------
    # Consistency checking, ensure we have required options, etc.
    #-------------------------------------------------------------------
}

#=======================================================================
#
# select_server()
#
# connect to the server, and get information needed to
# configure the user interface.
#
#=======================================================================
sub select_server
{

    if (not defined $dict_server || $dict_server eq '')
    {
	configure_dict_gui();
	return;
    }

    $word = '';

    #-------------------------------------------------------------------
    # Create connection to DICT server
    #-------------------------------------------------------------------
    $dict = Net::Dict->new($dict_server,
                           Port   => $config->port,
                           Client => $config->client,
			   Debug  => $config->debug,
                          );
    if (not defined $dict)
    {
	tkd_warn("Failed to connect to DICT server $dict_server");
	configure_dict_gui();
	return;
    }

    configure_dict_gui();
}

#=======================================================================
#
# configure_dict_gui()
#
# Configure the relevant bits of the GUI according to
# the current DICT connection.
#
#=======================================================================
sub configure_dict_gui
{
    my @dbs;
    my %dbhash;
    my @strats;
    my %shash;

    $text_window->delete('0.0', 'end');
    if (not defined $dict)
    {
	$bar3->packForget();
	$db_frame->packForget();
    }
    else
    {
	$bar3->pack(-side => 'top', -fill => 'x');

	%dbhash = $dict->dbs();
	@dbs = map { [$dbhash{$_}, $_] } sort keys %dbhash;
	unshift(@dbs, ['search all databases', '*'],
			['search all, stop after 1st match', '!']);
	$db_menu->configure(-options => \@dbs);

	%shash = $dict->strategies();
	@strats = map { [$shash{$_}, $_] } sort keys %shash;
	$strat_menu->configure(-options => \@strats);

	$db_frame->pack(-side => 'left');
    }
}

#=======================================================================
#
# create_gui()
#
# This procedure creates the widgets for the tkdict GUI
#
#=======================================================================
sub create_gui
{
    my $bar2;
    my $menu_bar;
    my $mbFile;
    my $mbView;
    my $mbHelp;
    my $server_entry;
    my $word_entry;

    $mw = MainWindow->new(-title => "$PROGRAM $VERSION");

    $bgcolor = $mw->cget(-bg);

    #---------------------------------------------------------------------
    # menu bar
    #---------------------------------------------------------------------
    $menu_bar = $mw->Frame(-relief => 'raised', -bd => 2);
    $menu_bar->pack(-side => 'top', -fill => 'x');

    #---------------------------------------------------------------------
    # Menu: File
    #
    # Create the File menu and the entries on the menu
    #---------------------------------------------------------------------

    $mbFile = $menu_bar->Menubutton(
                       -text => 'File',
                       -underline => 0,
		       -tearoff => 0,
		       -menuitems => [
                                      '-',
                                      ['command' => 'Exit',
                                       -underline => 1,
                                       -command => \&tkdict_exit]
                                     ]);
    $mbFile->pack(-side => 'left');

    #---------------------------------------------------------------------
    # Menu: View
    #
    # Create the View menu and the entries on the menu
    #---------------------------------------------------------------------
    $mbView = $menu_bar->Menubutton(
			-text => 'View', -underline => 0,
			-tearoff => 0,
			-menuitems => [ ['command' => 'Server Information',
					-command => [\&show_info, 'server']],
					['command' => 'Database Information',
					-command => [\&show_info, 'db']],
					]);
    $mbView->pack(-side => 'left');


    #---------------------------------------------------------------------
    # Menu: Help
    #
    # Create the Help menu and the entries on the menu
    #---------------------------------------------------------------------
    $mbHelp = $menu_bar->Menubutton(
			-text      => 'Help',
			-underline => 0,
			-tearoff   => 0,
			-menuitems => [
				['command' => 'Overview',
					-command => [\&show_help, 'overview']],
				['command' => 'ToDo List',
					-command => [\&show_help, 'todo']],
				'-',
				['command' => 'About TkDict ...',
					-command => [\&show_help, 'about']],
					]);
    $mbHelp->pack(-side => 'right');

    #---------------------------------------------------------------------
    # bar which has the entries for specifying server and select a dict
    #---------------------------------------------------------------------
    $bar2 = $mw->Frame(-relief => 'raised', -bd => 2);
    $bar2->pack(-side => 'top', -fill => 'x');

    $bar2->Label(-text => 'Server: ')->pack(-side => 'left');
    $server_entry = $bar2->Entry(-relief => 'sunken',
			   -textvariable => \$dict_server,
			   -width => 16)->pack(-side => 'left', -fill => 'x');
    $server_entry->bind('<Return>', \&select_server);
    $server_entry->bind('<FocusIn>',
                        sub { $server_entry->configure(-bg => 'white'); });
    $server_entry->bind('<FocusOut>',
                        sub { $server_entry->configure(-bg => "$bgcolor"); });

    $db_frame = $bar2->Frame();

    $db_frame->Label(-text => 'Dictionary: ')->pack(-side => 'left');
    $db_menu = $db_frame->Optionmenu(-variable => \$db,
			-textvariable => \$dbDisplay,
			-options => [],
			    )->pack(-side => 'left');

    #-------------------------------------------------------------------
    # Bar which has the entry for entering the word to be defined
    #-------------------------------------------------------------------
    $bar3 = $mw->Frame(-relief => 'raised', -bd => 2);
    $bar3->pack(-side => 'top', -fill => 'x');
    # $bar3->Label(-text => 'Define word:')->pack(-side => 'left');
    $mbDefine = $bar3->Optionmenu(
			-textvariable => \$modeDisplay,
			-variable => \$lookup_mode,
			-command => \&set_mode,
			-options => [ ['Define word', 'define'],
					['Match pattern', 'match'],
					],
					);
    $mbDefine->pack(-side => 'left');

    $word_entry = $bar3->Entry(-relief => 'sunken',
			   -textvariable => \$word,
			   -width => 16)->pack(-side => 'left');
    $word_entry->bind('<Return>', \&lookup_word);
    $word_entry->bind('<FocusIn>',
                      sub { $word_entry->configure(-bg => 'white'); });
    $word_entry->bind('<FocusOut>',
                      sub { $word_entry->configure(-bg => "$bgcolor"); });

    $sframe = $bar3->Frame();
    $sframe->Label(-text => 'Strategy')->pack(-side => 'left');
    $strat_menu = $sframe->Optionmenu(-variable => \$strategy,
			-textvariable => \$strategyDisplay,
			-options => [],
			    )->pack(-side => 'left');
    $sframe->pack(-side => 'left');

    $bar3->packForget();

    #-------------------------------------------------------------------
    # Bar which has the entry for entering the word to be defined
    #-------------------------------------------------------------------
    $text_window = $mw->Scrolled('Text',
                                 -bg => 'white', -fg => 'black',
                                 -width => 72, -height => 16,
                                -scrollbars => 'osoe');
    $text_window->pack(-side => 'bottom', -fill => 'both', -expand => 1);


    #-- accelerators ---------------------------------------------
    $mw->bind('<Control-x><Control-c>', \&tkdict_exit);

    set_mode();

    $mw->update;
}

#=======================================================================
#
# set_mode()
#
# Configure the GUI according to the lookup mode selected.
# If 'match', then show the menu for selecting the match strategy.
# If 'define', then hide the strategy selection menu.
#
#=======================================================================
sub set_mode
{

    if ($lookup_mode eq 'match')
    {
	$sframe->pack();
    }
    else
    {
	$sframe->packForget();
    }
}

#=======================================================================
#
# lookup_word()
#
# Look up the word entered by the user.
# This will either be a match or a define operation.
#
#=======================================================================
sub lookup_word
{
    my $string = '';
    my $eref;

    if (!defined($word) || length($word) == 0)
    {
	tkd_warn("You need to type something first!");
	return;
    }

    #-------------------------------------------------------------------
    # clear out any help text which might be displayed
    #-------------------------------------------------------------------
    $text_window->delete('0.0', 'end');

    if ($lookup_mode eq 'define')
    {
        #---------------------------------------------------------------
        # Word definitions requested. We get back a list ref:
        #    [  [db,definition], [db,definition], ... ]
        #---------------------------------------------------------------
	$eref = $dict->define($word, $db);
	if (@$eref == 0)
	{
	    $string = "no definition found for \"$word\"\n";
	}
	else
	{
	    foreach my $entry (@$eref)
	    {
		$string .= "--- ".$dict->dbTitle($entry->[0])." ---\n";
                $string .= $entry->[1]."\n\n";
	    }
	}

    }
    else
    {
        #---------------------------------------------------------------
        # List of matching words requested.
        #---------------------------------------------------------------
        my %dbwords;
        my ($dbname, $match);

        $eref = $dict->match($word, $strategy);
        if (@$eref == 0)
        {
            $string = "no words matched :-(\n";
        }
        else
        {
            foreach my $entry (@$eref)
            {
                ($dbname, $match) = @$entry;
                $dbwords{$dbname} = [] if not exists $dbwords{$dbname};
                push(@{ $dbwords{$dbname }}, $match);
            }
            foreach $dbname (sort keys %dbwords)
            {
                my @words;
                $string .= $dict->dbTitle($dbname).":\n";
                $string .= join(', ', @{ $dbwords{$dbname}});
                $string .= "\n\n";
            }
        }
    }

    #-------------------------------------------------------------------
    # display the resulting string in the scrolling text window
    #-------------------------------------------------------------------
    $text_window->insert('end', $string);
}


#=======================================================================
#
# tkdict_exit()
#
# quit from TkDict. In the future there might be
# more to do here, hence the function.
#
#=======================================================================
sub tkdict_exit
{
    exit 0;
}

#=======================================================================
#
# show_info()
#
# Display information which is retrieved from the server.
# An argument is passed to identify which piece of info:
#
#    server: information about the server
#    db    : information about the selected DB (dictionary)
#
#=======================================================================
sub show_info
{
    my $topic = shift;


    if ($topic eq 'server' && !$dict_server)
    {
        tkd_warn("You have to connect to a server first!");
        return;
    }
    if ($topic eq 'db' && (!$db || $db eq '*' || $db eq '!'))
    {
        tkd_warn("You must select a specific database first");
        return;
    }

    if (not Exists($info_top))
    {
        $info_top = $mw->Toplevel(-class => 'TkDictInfo');
        $info_top->title("$PROGRAM Info");
        $info_title = $info_top->Label();
        $info_title->pack(-side => 'top', -fill => 'x');

        $info_text = $info_top->Scrolled('Text',
                                 -bg => 'white', -fg => 'black',
                              -width => 60, -height => 12,
                              -scrollbars => 'osoe',
                             )->pack(-side => 'top', -fill => 'both',
                                     -expand => 1);

        $info_top->Button(-text => "Close",
                -command => sub {$info_top->withdraw})->pack(-side => 'bottom');
    } else {
        $info_top->deiconify();
        $info_top->raise();
    }

    $info_text->delete('0.0', 'end');

    if ($topic eq 'server')
    {
        $info_title->configure(-text => "Server: $dict_server");
        $info_text->insert('end', $dict->serverInfo());
    }
    else
    {
        $info_title->configure(-text => "Database: ".$dict->dbTitle($db));
        foreach my $line ($dict->dbInfo($db))
        {
            $info_text->insert('end', $line);
        }
    }
}

#=======================================================================
# show_help() - display a selected help message
#       $topic - the identifier for the topic to display
#
# This procedure is used to display a help message. An identifying
# string is passed in, which is used to index the associative array
# holding the help text.
#=======================================================================
sub show_help
{
    my $topic = shift;


    #-- create the help display toplevel, if needed --------------
    if (not Exists($help))
    {
	$help = $mw->Toplevel(-class => 'TkDictHelp');
	$help->title("$PROGRAM Help");

	$ht = $help->Scrolled('Text',
                              -bg => 'white', -fg => 'black',
			      -width => 60, -height => 12,
			      -scrollbars => 'osoe',
			     )->pack(-side => 'top', -fill => 'both',
                                     -expand => 1);

	$help->Button(-text => "Close",
		-command => sub {$help->withdraw})->pack(-side => 'bottom');
	initialise_help();
    } else {
	$help->deiconify();
	$help->raise();
    }

    #-- clear out any help text which might be displayed ---------
    $ht->delete('0.0', 'end');

    #-- insert the selected help message in text widget ----------
    $ht->insert('end', $helpString{$topic});
}

#=======================================================================
#
# tkd_warn()
#
# Display a warning message in a dialog, then wait for the
# user to acknowledge it.
#
#=======================================================================
sub tkd_warn
{
    my $message = shift;

    my $choice;


    if (not Exists($warn_dialog))
    {
	$warn_dialog = $mw->Dialog(
				-title		=> "Warning",
				-text		=> $message,
				-bitmap		=> 'warning',
				-default_button => "OK",
			);
    }
    else
    {
	$warn_dialog->configure(-text => $message);
    }

    $choice = $warn_dialog->Show(-global);
}


#=======================================================================
# initialise_help() - initialize the help strings
#
# This procedure initializes the global array helpString, which holds
# the text for the different help messages. The array is indexed by
# single word identifiers.
#=======================================================================
sub initialise_help
{
    $helpString{about} = <<EOFABOUT;

                     $PROGRAM v$VERSION

$PROGRAM is a DICT client, used to access network dictionary
servers which support the protocol defined in RFC 2229.

This client is using Perl module Net::Dict $Net::Dict::VERSION.

Neil Bowers <neil\@bowers.com>
Copyright (C) 2001-2002, Neil Bowers
EOFABOUT

    $helpString{overview} = <<EOFENTRY;

              $PROGRAM $VERSION - Overview

$PROGRAM is a simple Tk tool for looking up entries
in dictionaries which are accessed using the DICT protocol.

First you must specify a Server (and press RETURN).
A good one to try is dict.org - it has a number of dictionaries.
You should get a menu for selecting dictionaries,
and a text box for entering a word.

Enter a word and press return. By default $PROGRAM will check
all dictionaries, so you might get a number of definitions.
 
EOFENTRY
 
    $helpString{todo} = <<EOFTODO;

            $PROGRAM v$VERSION - ToDo List
 
    *   better formatting of results
    *   more user-oriented user interface
    *   have the inline pod available on Help menu
    *   show one definition at a time
        with some sort of NEXT and PREV interface
    *   option to specify whether to stay connect or not
    *   haven't done anything to handle connnection timing out
    *   status line at the bottom of the main window

EOFTODO
}


#=======================================================================
#
# show_db_info()
#
# Query the server for information about the specified database,
# and display the results.
#
# The information is typically several pages of text,
# describing the contents of the dictionary, where it came from,
# credits, etc.
#
#=======================================================================
sub show_db_info
{
    my $db  = shift;
    my %dbs = $dict->dbs();


    if (not exists $dbs{$config->info})
    {
        print "  dictionary \"$db\" not known\n";
        return;
    }

    print $dict->dbInfo($config->info);
}

__END__

=head1 NAME

tkdict - a perl client for accessing network dictionary servers

=head1 SYNOPSIS

tkdict [OPTIONS]

=head1 DESCRIPTION

B<tkdict> is a Perl/Tk client for the Dictionary server protocol (DICT),
which is used to query natural dictionaries hosted on a remote machine.

At the moment it's not very user oriented, since I've just been
creating an interface to the protocol.

There is more information available in the B<Help> menu
when running B<tkdict>.

=head1 OPTIONS

=over 4

=item B<-h> I<server> or B<-host> I<server>

The hostname for the DICT server.

=item B<-p> I<port> or B<-port> I<port>

Specify the port for connections (default is 2628, from RFC 2229).

=item B<-c> I<string> or B<-client> I<string>

Specify the CLIENT identification string sent to the DICT server.

=item B<-help>

Display a short help message including command-line options.

=item B<-doc>

Display the full documentation for B<tkdict>.

=item B<-version>

Display the version of B<tkdict>

=item B<-verbose>

Display verbose information as B<tkdict> runs.

=item B<-debug>

Display debugging information as B<tkdict> runs.
Useful mainly for developers.

=back

=head1 KNOWN BUGS AND LIMITATIONS

=over 4

=item *

B<tkdict> doesn't know how to handle firewalls.

=item *

The authentication aspects of RFC 2229 aren't currently supported.

=item *

See the B<ToDo> page under the B<Help> menu.

=back

=head1 SEE ALSO

=over 4

=item www.dict.org

The DICT home page, with all sorts of useful information.
There are a number of other DICT clients available.

=item dict

The C dict client written by Rik Faith;
the options are pretty much lifted from Rik's client.

=item RFC 2229

The document which defines the DICT network protocol.

http://www.cis.ohio-state.edu/htbin/rfc/rfc2229.html

=item Net::Dict

The perl module which implements the client API for RFC 2229.
It includes a command-line perl client, B<dict>,
as well as B<tkdict>.

=back

=head1 VERSION

$Revision: 1.1.1.1 $

=head1 AUTHOR

Neil Bowers <neil@bowers.com>

=head1 COPYRIGHT

Copyright (C) 2001-2002 Neil Bowers. All rights reserved.

This script is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.

=cut