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
| #!/usr/bin/perl
use warnings;
use strict;
use utf8;
use Tk;
use Tk::LabFrame;
use Tk::ROText;
use File::Spec; # Permet une meilleure gestion des noms des chemins sous tous les OS
use File::Path; # Gestion des répertoires
my $RepBas = 'E:/';
my $Directory = $RepBas;
my $LabRepertoire = $RepBas;
my $LabRecursif = "Récursivité désactivée";
my $LabFic = "";
my $LabMessage = "";
my $Trouve = 0;
my $Stop = "";
my $IN = ();
my $IN_Fic = "";
ActiverAccentsDOS();
my $fen = MainWindow->new;
my $width = $fen->screenwidth();
my $height = $fen->screenheight();
$fen->destroy();
# Creation du widget principal
my $Wm = MainWindow->new(
-width => $width,
-height => $height,
-title => "Spinoza",
-background => 'white',
);
$width = $Wm->screenwidth*(4/5);
$height = $Wm->screenheight*(4/5);
$Wm->configure(-height=>$height, -width=>$width, -title=>"Recherche dans un fichier");
$Wm->geometry( $width ."x". $height );
$Wm->MoveToplevelWindow (100,100);
#$Wm->overrideredirect(1);
#$Wm->packPropagate(0);
#$Wm->raise();
###############################
# Scrolled qui affiche le texte
my $widget_texte = $Wm->Scrolled(
'ROText',
-scrollbars => 'e',
-takefocus => 0,
-wrap => 'word',
-relief => 'flat',
-background => '#ABFAA3',
);
configurer_tags($widget_texte);
# Cadre des configurations
my $couleur_commune = '#E0F0FF';
my $cadre_configuration_playlist = $Wm->Frame( -background => $couleur_commune, );
# Cadre Gestion
my $cadre_gestion = $Wm->LabFrame(
-label => 'Gestion',
-background => $couleur_commune,
-font => '{Simplified Arabic} 10 {bold}',
);
# Choix du répertoire
my $Repertoire = $cadre_gestion->Button(
-text => "Choix du répertoire",
-command => sub {
# Selectionner un répertoire
$Directory = $Wm->chooseDirectory(
-title => "Selection d'un répertoire",
-initialdir => $Directory,
);
if ( $Directory) {
$LabRepertoire = $Directory;
$Wm->update();
}
},
);
# Cases à cocher pour récursivité
my $Recursif = 0;
my $CasRecurv = $cadre_gestion->Checkbutton (
-text => "Récusivité",
-variable => \$Recursif,
-command => sub {
if ( $Recursif == 0 ) {
$LabRecursif = "Récursivité désactivée";
} else {
$LabRecursif = "Récursivité activée";
}
},
);
# Pattern Nom fichier
my $PatternFic = ".*";
my $LabelPatternFic = $cadre_gestion->Label(
-text => "Fichier",
-font => '{Simplified Arabic} 10 {bold}',
);
my $PatternFicSaisie = $cadre_gestion->Entry(
-width => 30,
-justify => 'left',
-textvariable => \$PatternFic,
-font => '{Simplified Arabic} 10 {bold}',
);
# Pattern Extension
my $PatternExt = ".*txt";
my $LabelPatternExt = $cadre_gestion->Label(
-text => "Extension",
-font => '{Simplified Arabic} 10 {bold}',
);
my $PatternExtSaisie = $cadre_gestion->Entry(
-width => 5,
-justify => 'left',
-textvariable => \$PatternExt,
-font => '{Simplified Arabic} 10 {bold}',
);
# Pattern Recherche Avec
my $PatternRechAvec = "";
my $LabelPatternRechAvec = $cadre_gestion->Label(
-text => "Avec",
-font => '{Simplified Arabic} 10 {bold}',
);
my $PatternRechercheAvec = $cadre_gestion->Entry(
-width => 40,
-justify => 'left',
-textvariable => \$PatternRechAvec,
-font => '{Simplified Arabic} 10 {bold}',
);
# Pattern Recherche Sans
my $PatternRechSans = "";
my $LabelPatternRechSans = $cadre_gestion->Label(
-text => "Sans",
-font => '{Simplified Arabic} 10 {bold}',
);
my $PatternRechercheSans = $cadre_gestion->Entry(
-width => 40,
-justify => 'left',
-textvariable => \$PatternRechSans,
-font => '{Simplified Arabic} 10 {bold}',
);
# Start
my $bouton_start = $cadre_gestion->Button(
-text => "Start",
-command => sub {
$Trouve = 0;
$LabRepertoire = $Directory;
$LabFic = "";
$LabMessage = "Recherche en cours...";
$widget_texte->delete( '1.0', 'end' );
$Wm->update;
ListerFichiers($Directory, $Recursif);
$LabMessage = "Recherche terminée : TROUVÉ = $Trouve";
},
);
# Stop
my $bouton_stop = $cadre_gestion->Button(
-text => "Stop",
-command => [ \&stop, ],
);
# Exit
my $bouton_exit = $cadre_gestion->Button(
-text => "Exit",
-command => [ \&fermer, ],
);
####################################
# Cadre pour les messages de service
my $cadre_messages = $Wm->Frame( -background => $couleur_commune, );
# Label qui affichera le répertoire courant
my $label_repertoire = $cadre_messages->Label(
-textvariable => \$LabRepertoire,
-width => 15,
-background => '#FFE0D0',
-relief => 'groove',
);
# Label qui affichera l'info récursivité
my $label_recursif = $cadre_messages->Label(
-textvariable => \$LabRecursif,
-width => 20,
-background => '#FFE0D0',
-relief => 'groove',
);
# Label qui affichera le fichier en cours
my $label_fic = $cadre_messages->Label(
-textvariable => \$LabFic,
-width => 5,
-background => '#FFE0D0',
-relief => 'groove',
);
# Label qui affichera les messages, en bas
my $label_message = $cadre_messages->Label(
-textvariable => \$LabMessage,
-background => '#FFE0D0',
-relief => 'groove',
);
####################################
# Gestion de l'affichage des widgets
#
$cadre_messages->pack(qw / -side bottom -fill both -expand 0 /);
$label_repertoire->pack(qw / -side left -fill both -expand 1 /);
$label_recursif->pack(qw / -side left -fill both -expand 0 /);
$label_fic->pack(qw / -side left -fill both -expand 1 /);
$label_message->pack(qw / -side left -fill both -expand 1 /);
$widget_texte->pack(qw / -side bottom -fill both -expand 1 /);
$cadre_gestion->pack(qw / -side right -fill both -expand 1 /);
$Repertoire->grid( $CasRecurv, $LabelPatternFic, $PatternFicSaisie, $PatternExtSaisie, $LabelPatternExt, $LabelPatternRechAvec, $PatternRechercheAvec,
$PatternRechercheSans, $LabelPatternRechSans, $bouton_start, $bouton_stop, $bouton_exit,
qw / -padx 2 -pady 5 -sticky nsew /
);
MainLoop;
#############################################################################
#============================================================================
# Nombre d'arguments : 1 ou 2
# Argument(s) : un répertoire et valeur 0 ou 1 (récursivité)
# Retourne : Tableau de fichier (@fichiers)
# Origine = http://perl.developpez.com/faq/perl/?page=section1#ListerFichiers
#============================================================================
sub ListerFichiers {
my ( $repertoire, $recursivite ) = @_;
if ( ( not defined $recursivite ) || ( $recursivite != 1 ) ) { $recursivite = 0; }
opendir (my $FhRep, $repertoire)
or warn "impossible d'ouvrir le répertoire $repertoire\n" and $LabMessage = "Ouverture impossible $repertoire - $! - $@" and return;
# Liste fichiers et répertoire sauf (. et ..)
my @Contenu = grep { !/^\.\.?$/ } readdir($FhRep);
closedir ($FhRep);
# On récupère tous les fichiers
my @fichiers;
foreach my $nom ( sort @Contenu ) {
last if ($Stop ne "");
# Fichiers
if ( -f "$repertoire/$nom") {
my ($Fic, $Ext) = split (/\./,$nom);
# print "FIC=$Fic == EXT=$Ext PATTERNFIC=$PatternFic PATTERNEXT=$PatternExt\n";
next if ($Fic !~ /$PatternFic/ );
next if (!$Ext);
next if ($Ext eq "mp3" );
next if ($Ext eq "mp4" );
next if ($Ext eq "wav" );
next if ($Ext eq "jpg" );
next if ($Ext eq "bmp" );
next if ($Ext !~ /$PatternExt/ );
push ( @fichiers, "$repertoire/$nom" );
$LabRepertoire = "$repertoire/$nom";
$IN_Fic = "$repertoire/$nom";
$LabFic = "$repertoire/$nom";
my $Encodage = `file "$IN_Fic"`;
my $Codage = '';
$Codage = 'utf8' if ( $Encodage =~ 'UTF-8' ); # $Codage = '' if ( $Encodage =~ 'ISO-8859' );
unless (open ($IN, "<:".$Codage, $IN_Fic)) {
$LabMessage = "ERREUR : Impossible d'ouvrir $IN_Fic - ENCODAGE=$Codage $! - $@\n";
$Wm->Label->update();
return;
}
my $NumLigne = 0;
my $Flag = 0;
while ( my $Ligne = <$IN> ) {
$NumLigne++;
next if ($Ligne !~ /$PatternRechAvec/ );
next if ($PatternRechSans && $Ligne =~ /$PatternRechSans/ );
$widget_texte->insert( 'end', "\n\n$repertoire/$nom\n", "g14" ) if ($Flag < 1);
$Flag++; $Trouve++;
$widget_texte->insert( 'end', $NumLigne .' - '. $Ligne, "g12" );
$widget_texte->update;
}
close $IN;
}
# Répertoires
elsif ( -d "$repertoire/$nom" and $recursivite == 1 ) {
# récursivité
push ( @fichiers, ListerFichiers("$repertoire/$nom", $recursivite) );
}
}
return @fichiers;
}
####################################################################################
sub stop {
$Stop = 'Lecture stoppée';
$LabMessage = 'Lecture stoppée';
$Wm->update;
return;
}
sub fermer {
exit (0);
}
#########################################
# Configuration des polices de caractères
#
sub configurer_tags {
my $widget_text = shift;
for my $police ( 12 .. 72 ) {
if ( $police % 2 == 0 ) {
$widget_text->tagConfigure(
"g$police",
-background => '#ABFAA3',
-font => "{Simplified Arabic} $police {bold}",
-justify => 'left',
);
$widget_text->tagConfigure(
"c$police",
-background => '#ABFAA3',
-font => "{Simplified Arabic} $police {bold}",
-justify => 'center',
);
$widget_text->tagConfigure(
"d$police",
-background => '#ABFAA3',
-font => "{Simplified Arabic} $police {bold}",
-justify => 'right',
);
}
}
return;
}
#==============================================================
# Pour avoir les accents sur la console DOS
# http://perl.developpez.com/faq/perl/?page=Terminal#AccentsDOS
#==============================================================
sub ActiverAccentsDOS {
my $encodage;
# Windows
if ( lc($^O ) eq 'mswin32') {
eval {
my ($codepage) = ( `chcp` =~ m/:\s+(\d+)/ );
$encodage = "cp$codepage";
foreach my $h ( \*STDOUT, \*STDERR, \*STDIN, ) {
binmode $h, ":encoding($encodage)";
}
};
}
else {
$encodage = `locale charmap`;
eval {
foreach my $h ( \*STDOUT, \*STDERR, \*STDIN, ) {
binmode $h, ":encoding($encodage)";
}
};
}
return $encodage;
}
#Sinon :
#ü \x81 à \x85 è \x8A
#é \x82 ç \x87 ï \x8B
#â \x83 ê \x88 î \x8C
#ä \x84 ë \x89
=pod
##############################
# Retourne encodage du fichier
#
# Jedaï :
# my $Codage = guess_file_encoding("$IN_Fic");
#open my $fh, "<:encoding($name_enc)", $filename
# or die "Can't open $filename : $!\n";
#unless (open (IN, "<:encoding($Codage)", $IN_Fic)) {
# $LabMessage = "ERREUR : Impossible d'ouvrir $IN_Fic - ENCODAGE=$Codage $! - $@\n";
# $Wm->Label->update();
# return;
#}
sub guess_file_encoding {
my $filename = shift;
open my $fh, '<:raw', $filename
or die "Can't open $filename to guess encoding : $!\n";
die "No data in $filename\n" unless read $fh, my $data, 256;
my $enc = Encode::Guess->guess($data);
ref($enc) or die "Couldn't guess encoding of $filename.\n";
return $enc->name();
} |
Partager