| 1 |
#!/usr/bin/perl -w |
| 2 |
#----------------------------------------------------------------------------- |
| 3 |
# $Id : odfmetadoc 0.3 2008-05-04 JMG$ |
| 4 |
#----------------------------------------------------------------------------- |
| 5 |
|
| 6 |
=head1 NAME |
| 7 |
|
| 8 |
odfmetadoc - Document properties management interface |
| 9 |
|
| 10 |
=head1 SYNOPSIS |
| 11 |
|
| 12 |
odfmetadoc <directory> |
| 13 |
|
| 14 |
=head1 DESCRIPTION |
| 15 |
|
| 16 |
Simple graphical user interface allowing the user to get and set the metadata |
| 17 |
of the OpenDocument and OpenOffice.org files (ODT, ODP, ODS, SXW, SXI, SXC,...) |
| 18 |
in a given directory. |
| 19 |
|
| 20 |
The list of the files is shown in the main window. Each time the user selects |
| 21 |
a file (by double-clic or OK button), another window appears, with some |
| 22 |
property fields: title, author, subject, description, keywords. The flat text |
| 23 |
content is shown in a read-only field. The user can update any property, or |
| 24 |
insert/delete any keyword. A double-click on a word in the text content field |
| 25 |
appends the word to the keword list. 'OK' saves the changes in the file. |
| 26 |
|
| 27 |
=head1 LIMITS |
| 28 |
|
| 29 |
This script is provided as a demo and test tool, with a very basic user |
| 30 |
interface. There is no directory selection dialog box. Some file names with |
| 31 |
blank spaces and/or non-ASCII characters are not properly processed by the |
| 32 |
graphical user interface in some environments (even if the OODoc API does |
| 33 |
support the same character sets as the local Perl installation). |
| 34 |
|
| 35 |
=head1 REQUIREMENTS |
| 36 |
|
| 37 |
Requires Tk and some Tk extensions (TK::Dialog, Tk::LabFrame, Tk::LabEntry) |
| 38 |
|
| 39 |
=cut |
| 40 |
|
| 41 |
#------------------------------------------------------------------------------ |
| 42 |
|
| 43 |
use Tk; |
| 44 |
use Tk::Dialog; |
| 45 |
use OpenOffice::OODoc 2.101; |
| 46 |
|
| 47 |
#------------------------------------------------------------------------------ |
| 48 |
|
| 49 |
our $sv_chemin = $ARGV[0] || $ENV{'HOME'} || '.'; |
| 50 |
our $chemin = $sv_chemin; |
| 51 |
our $D = undef; |
| 52 |
our $f = undef; |
| 53 |
our $F = undef; |
| 54 |
our $boite_liste = undef; |
| 55 |
our @liste = (); |
| 56 |
our $couleur_saisie = 'ivory'; |
| 57 |
our $couleur_lecture = 'LightSkyBlue'; |
| 58 |
our %delimiteurs = |
| 59 |
( |
| 60 |
'text:p' => |
| 61 |
{ |
| 62 |
begin => undef, |
| 63 |
end => "\n" |
| 64 |
}, |
| 65 |
'text:h' => |
| 66 |
{ |
| 67 |
begin => '[Titre] ', |
| 68 |
end => "\n" |
| 69 |
}, |
| 70 |
'text:list-item' => |
| 71 |
{ |
| 72 |
begin => "\t* " |
| 73 |
}, |
| 74 |
%OpenOffice::OODoc::Text::DEFAULT_DELIMITERS |
| 75 |
); |
| 76 |
|
| 77 |
#------------------------------------------------------------------------------ |
| 78 |
|
| 79 |
sub place_fenetre |
| 80 |
{ |
| 81 |
my $fenetre = shift; |
| 82 |
my $x = int(($fenetre->screenwidth - $fenetre->reqwidth) / 2); |
| 83 |
my $y = int(($fenetre->screenheight - $fenetre->reqheight) / 2); |
| 84 |
$fenetre->geometry("+$x+$y"); |
| 85 |
$fenetre->resizable(0, 0); |
| 86 |
} |
| 87 |
|
| 88 |
#------------------------------------------------------------------------------ |
| 89 |
|
| 90 |
sub accesMeta |
| 91 |
{ |
| 92 |
require Tk::LabFrame; |
| 93 |
require Tk::LabEntry; |
| 94 |
|
| 95 |
my $fichier = shift; |
| 96 |
my $oo_archive = odfContainer($fichier); |
| 97 |
return undef unless $oo_archive; |
| 98 |
my $doc = odfMeta(container => $oo_archive); |
| 99 |
return undef unless $doc; |
| 100 |
my $txt = odfText |
| 101 |
(container => $oo_archive, delimiters => { %delimiteurs }); |
| 102 |
my ($titre, $auteur, $sujet, $description, @clefs, $mot); |
| 103 |
|
| 104 |
$f->idletasks; |
| 105 |
$D = $f->Toplevel |
| 106 |
( |
| 107 |
-title => $fichier, |
| 108 |
); |
| 109 |
$D->withdraw; |
| 110 |
my $fr2 = $D->Frame |
| 111 |
-> pack(-side => 'bottom', -expand => 1, -fill => 'x'); |
| 112 |
my $fr1 = $D->Frame |
| 113 |
->pack(-side => 'left', -anchor => 'n'); |
| 114 |
my $fr3 = $D->LabFrame |
| 115 |
( |
| 116 |
-label => 'Keywords', |
| 117 |
-labelside => 'acrosstop' |
| 118 |
) |
| 119 |
->pack(-side => 'right', -expand => 1, -fill => 'y'); |
| 120 |
$fr1->LabEntry |
| 121 |
( |
| 122 |
-label => 'Title', |
| 123 |
-textvariable => \$titre, |
| 124 |
-width => 40, |
| 125 |
-bg => $couleur_saisie |
| 126 |
) |
| 127 |
-> pack(-side => 'top', -fill => 'x'); |
| 128 |
$fr1->LabEntry |
| 129 |
( |
| 130 |
-label => 'Author', |
| 131 |
-textvariable => \$auteur, |
| 132 |
-width => 40, |
| 133 |
-bg => $couleur_saisie |
| 134 |
) |
| 135 |
-> pack(-side => 'top', -fill => 'x'); |
| 136 |
$fr1->LabEntry |
| 137 |
( |
| 138 |
-label => 'Subject', |
| 139 |
-textvariable => \$sujet, |
| 140 |
-width => 40, |
| 141 |
-bg => $couleur_saisie |
| 142 |
) |
| 143 |
-> pack(-side => 'top', -fill => 'x'); |
| 144 |
my $fr4 = $fr1->LabFrame |
| 145 |
( |
| 146 |
-label => 'Description', |
| 147 |
) |
| 148 |
-> pack(-side => 'top', -fill => 'x'); |
| 149 |
$description = $fr4->Scrolled |
| 150 |
( |
| 151 |
'Text', |
| 152 |
-scrollbars => 'ose', |
| 153 |
-height => 3, |
| 154 |
-width => 80, |
| 155 |
-bg => $couleur_saisie, |
| 156 |
-exportselection => 'yes' |
| 157 |
) |
| 158 |
-> pack(-side => 'top', -fill => 'both'); |
| 159 |
my $fr5 = $fr1->LabFrame |
| 160 |
( |
| 161 |
-label => 'Content', |
| 162 |
) |
| 163 |
-> pack(-side => 'top', -fill => 'x'); |
| 164 |
my $texte = $fr5->Scrolled |
| 165 |
( |
| 166 |
'Text', |
| 167 |
-scrollbars => 'ose', |
| 168 |
-height => 20, |
| 169 |
-width => 90, |
| 170 |
-bg => $couleur_lecture, |
| 171 |
-exportselection => 'yes' |
| 172 |
) |
| 173 |
-> pack(-side => 'top', -fill => 'both'); |
| 174 |
my $l = $fr3->Scrolled |
| 175 |
( |
| 176 |
'Listbox', |
| 177 |
-scrollbars => 'ose', |
| 178 |
-height => 20, |
| 179 |
-bg => $couleur_saisie, |
| 180 |
-exportselection => 'yes' |
| 181 |
) |
| 182 |
-> pack(-side => 'top', -expand => 1, -fill => 'x'); |
| 183 |
$texte->bind |
| 184 |
( |
| 185 |
'<Double-Button-1>' => sub |
| 186 |
{ |
| 187 |
push @clefs, $texte->get |
| 188 |
('insert wordstart', 'insert wordend'); |
| 189 |
$l->see('end'); |
| 190 |
} |
| 191 |
); |
| 192 |
my $nm = $fr3->Entry |
| 193 |
( |
| 194 |
-textvariable => \$mot, |
| 195 |
-width => 18, |
| 196 |
-bg => $couleur_saisie |
| 197 |
) |
| 198 |
-> pack(-side => 'bottom', -fill => 'x'); |
| 199 |
$nm->bind ( |
| 200 |
'<Return>' => sub |
| 201 |
{ |
| 202 |
if ($mot) |
| 203 |
{ |
| 204 |
push @clefs, $mot; |
| 205 |
$l->see('end'); |
| 206 |
$mot = ""; |
| 207 |
} |
| 208 |
} |
| 209 |
); |
| 210 |
$l->bind ( '<1>' => sub { $l->focus; }); |
| 211 |
$l->bind ( |
| 212 |
'<FocusOut>' => sub |
| 213 |
{ |
| 214 |
my $s = $l->curselection; |
| 215 |
$l->selectionClear($s) if defined $s; |
| 216 |
} |
| 217 |
); |
| 218 |
$l->bind ( |
| 219 |
'<Delete>' => sub |
| 220 |
{ |
| 221 |
my $s = $l->curselection; |
| 222 |
$l->delete($s) if defined $s; |
| 223 |
} |
| 224 |
); |
| 225 |
$fr2->Button |
| 226 |
( |
| 227 |
-text => 'OK', |
| 228 |
-command => sub |
| 229 |
{ |
| 230 |
$doc->title($titre); |
| 231 |
$doc->creator($auteur); |
| 232 |
$doc->subject($sujet); |
| 233 |
$doc->description |
| 234 |
( |
| 235 |
$description->get('1.0', 'end') |
| 236 |
); |
| 237 |
$doc->removeKeywords; |
| 238 |
$doc->keywords(@clefs); |
| 239 |
$doc->save; |
| 240 |
$doc->dispose; $txt->dispose; |
| 241 |
$D->destroy; |
| 242 |
} |
| 243 |
) |
| 244 |
-> pack(-side => 'left', -expand => 1, -fill => 'x'); |
| 245 |
$fr2->Button |
| 246 |
( |
| 247 |
-text => 'Cancel', |
| 248 |
-command => sub |
| 249 |
{ |
| 250 |
$doc->dispose; $txt->dispose; |
| 251 |
$D->destroy; |
| 252 |
} |
| 253 |
) |
| 254 |
-> pack(-side => 'left', -expand => 1, -fill => 'x'); |
| 255 |
$D->bind('<KeyPress-Escape>' => sub { $D->destroy }); |
| 256 |
$D->bind('<Destroy>' => sub { $f->deiconify; }); |
| 257 |
$D->update; |
| 258 |
place_fenetre($D); |
| 259 |
$f->withdraw; |
| 260 |
$D->deiconify; |
| 261 |
tie @clefs, "Tk::Listbox", $l; |
| 262 |
@clefs = $doc->keywords; shift @clefs unless $clefs[0]; |
| 263 |
$titre = $doc->title; |
| 264 |
$auteur = $doc->creator; |
| 265 |
$sujet = $doc->subject; |
| 266 |
$description->delete('1.0', 'end'); |
| 267 |
$description->insert('end', $doc->description); |
| 268 |
if ($txt) |
| 269 |
{ |
| 270 |
$texte->insert('end', "Loading the text..."); |
| 271 |
$D->idletasks; |
| 272 |
$texte->delete('1.0', 'end'); |
| 273 |
foreach my $contenu ($txt->selectTextContent) |
| 274 |
{ |
| 275 |
$texte->insert('end', ($contenu . "\n")); |
| 276 |
} |
| 277 |
} |
| 278 |
else |
| 279 |
{ |
| 280 |
$texte->insert('end', "**** CONTENT NOT AVAILABLE ****"); |
| 281 |
} |
| 282 |
$texte->configure(-state => 'disabled'); |
| 283 |
$D->grab; |
| 284 |
} |
| 285 |
|
| 286 |
#------------------------------------------------------------------------------ |
| 287 |
|
| 288 |
sub listeFichiers |
| 289 |
{ |
| 290 |
$chemin = $sv_chemin unless $chemin; |
| 291 |
my $r = chdir $chemin; |
| 292 |
unless ($r) |
| 293 |
{ |
| 294 |
warn "$chemin is not available ; back to $sv_chemin\n"; |
| 295 |
$chemin = $sv_chemin; |
| 296 |
chdir $chemin or warn "$chemin is not available\n"; |
| 297 |
} |
| 298 |
return $OpenOffice::OODoc::VERSION >= 2 ? <*.sx? *.od?> : <*.sx?>; |
| 299 |
} |
| 300 |
|
| 301 |
#------------------------------------------------------------------------------ |
| 302 |
|
| 303 |
$f = MainWindow->new |
| 304 |
( |
| 305 |
title => 'Documents', |
| 306 |
); |
| 307 |
$r1 = $f->Frame->pack(-side => 'bottom', -fill => 'x'); |
| 308 |
my $dir = $f->Entry(-width => 32, -textvariable => \$chemin) |
| 309 |
->pack(-side => 'bottom', -expand => 'yes'); |
| 310 |
$dir->bind('<Return>', sub { @liste = listeFichiers; }); |
| 311 |
$boite_liste = $f->Scrolled |
| 312 |
( |
| 313 |
'Listbox', |
| 314 |
-scrollbars => 'oe', |
| 315 |
-width => 32, |
| 316 |
-height => 16, |
| 317 |
-bg => $couleur_saisie |
| 318 |
) |
| 319 |
->pack(-side => 'top', -expand => 'yes'); |
| 320 |
my $OK = $r1->Button |
| 321 |
( |
| 322 |
-text => 'OK', |
| 323 |
) |
| 324 |
->pack(-side => 'left', -expand => 1, -fill => 'x'); |
| 325 |
$OK->configure |
| 326 |
( |
| 327 |
-command => sub |
| 328 |
{ |
| 329 |
my $f = $liste[($boite_liste->curselection)[0]]; |
| 330 |
$OK->configure |
| 331 |
( |
| 332 |
-text => 'Loading...' |
| 333 |
); |
| 334 |
accesMeta($f) if ($f); |
| 335 |
$OK->configure |
| 336 |
( |
| 337 |
-text => 'OK' |
| 338 |
); |
| 339 |
} |
| 340 |
); |
| 341 |
$boite_liste->bind |
| 342 |
( |
| 343 |
'<Double-Button-1>' => sub |
| 344 |
{ |
| 345 |
my $f = $liste[($boite_liste->curselection)[0]]; |
| 346 |
$OK->configure |
| 347 |
( |
| 348 |
-text => 'Loading...' |
| 349 |
); |
| 350 |
accesMeta($f) if ($f); |
| 351 |
$OK->configure |
| 352 |
( |
| 353 |
-text => 'OK' |
| 354 |
); |
| 355 |
} |
| 356 |
); |
| 357 |
my $Annuler = $r1->Button |
| 358 |
( |
| 359 |
-text => 'Cancel', |
| 360 |
-command => [ $f => 'destroy' ] |
| 361 |
) |
| 362 |
->pack(-side => 'left', -fill => 'x'); |
| 363 |
$f->bind('<KeyPress-Escape>' => sub { $f->destroy }); |
| 364 |
|
| 365 |
tie @liste, "Tk::Listbox", $boite_liste; |
| 366 |
@liste = listeFichiers; |
| 367 |
$boite_liste->selectionSet(0); |
| 368 |
|
| 369 |
place_fenetre($f); |
| 370 |
$OK->focus; |
| 371 |
|
| 372 |
#------------------------------------------------------------------------------ |
| 373 |
|
| 374 |
MainLoop; |
| 375 |
exit; |
| 376 |
|
| 377 |
#------------------------------------------------------------------------------ |