[Kde-perl] listview and filehandles
Edward
edward at thecookies.org
Sun Apr 25 03:50:19 CEST 2004
ok well. heres something really odd. when using the listView in perlqt i
can no longer close or write to filehandles. they supposedly open fine
but when i print to them nothing prints and when i close them using
close FHA or die "cant close $pathname: $!";
i get cant close /tmp/Adema.Adema: Bad file descriptor at
/root/ui/form1.pl line 234
im really new to all this perlqt stuff and limited experience with perl
but here is the code that i have so far. its a media library for mp3's i
couldnt find any one that i liked so i have to make one. any help with
this would be appreciated. just need help with not being able to print
after listView2 gets created. i have tried with/without the extra use's
including strict. i am using perqt 3.0.0.8 and perl 5.8.2 on FreeBSD 5.2.
#!/usr/local/bin/perl
# Form implementation generated from reading ui file 'form1.ui'
#
# Created: Fri Apr 23 23:39:55 2004
# by: The PerlQt User Interface Compiler (puic)
#
# WARNING! All changes made in this file will be lost!
use strict;
use utf8;
package Form1;
use Qt;
use Qt::isa qw(Qt::Widget);
use Qt::slots
mine => [],
endme => [];
use Qt::attributes qw(
listView2
btnpopulate
btnexit
);
use File::Basename;
use List::Compare;
use MP3::Tag;
use Data::Dumper;
use constant SEARCH_ALL => 'all';
use IO::Handle;
use Fcntl qw(:DEFAULT :flock); #might not be needed
my $basedir = "/tmp/"; #base dir for the files
my %supported_frames = (
TIT1 => 1,
TIT2 => 1,
TRCK => 1,
TALB => 1,
TPE1 => 1,
COMM => 1,
WXXX => 1,
TYER => 1,
);
my @supported_frames = keys %supported_frames;
my $artist = "";
my $trackname = "";
my $tracknumber = "";
my $tracklength = "";
my $genre = "";
my $album = "";
my $combochars = "----------";
my $file = "";
my @rawdata=[];
sub TRUE { 1 }
sub FALSE { -1 }
my $DEBUG = FALSE;
# simple check for the base dir to make sure it has the trailing /.
if (!(substr($basedir,-1,1) eq "/")) {
$basedir = $basedir."/";
}
my @song = ["", "", "", ""];
my @album = [@song];
my @artist = [@album];
my @artists = (["Artist", at artist]);
my @newsong = [];
sub NEW {
shift->SUPER::NEW(@_[0..2]);
if ( name() eq "unnamed" ) {
setName("Form1" );
}
#*******
#*******
#******* THIS IS THE LAST POINT WHERE THIS WORKS CORRECTLY.
# $pathname = "/tmp/Adema.Adema";
# print "Opening file and printing to it\n";
# open(FHA, "> $pathname") or die "can't open $pathname: $!";
# #flock(FH, LOCK_EX) or die "can't flock $pathname: $!";
# print FHA "hello there\n" or die "cant print $pathname: $!";
# close(FHA) or die "cant close $pathname: $!";
#
#
#
listView2 = Qt::ListView(this, trUtf8("listView2"));
listView2->addColumn(trUtf8("Artist"));
listView2->addColumn(trUtf8("Album"));
listView2->addColumn(trUtf8("Song"));
listView2->addColumn(trUtf8("Time"));
listView2->addColumn(trUtf8("Track Number"));
listView2->setGeometry( Qt::Rect(0, 0, 520, 500) );
listView2->setSizePolicy( Qt::SizePolicy(4, 4, 100, 100,
listView2->sizePolicy()->hasHeightForWidth()) );
btnpopulate = Qt::PushButton(this, "btnpopulate");
btnpopulate->setGeometry( Qt::Rect(0, 500, 107, 27) );
btnexit = Qt::PushButton(this, "btnexit");
btnexit->setGeometry( Qt::Rect(410, 500, 107, 27) );
btnexit->setFlat( 0 );
languageChange();
my $resize = Qt::Size(520, 529);
$resize = $resize->expandedTo(minimumSizeHint());
resize( $resize );
clearWState( &Qt::WState_Polished );
Qt::Object::connect(btnexit, SIGNAL "released()", this, SLOT "endme()");
Qt::Object::connect(btnpopulate, SIGNAL "released()", this, SLOT
"mine()");
}
sub languageChange {
setCaption(trUtf8("Form1") );
listView2->header()->setLabel( 0, trUtf8("Artist") );
listView2->header()->setLabel( 1, trUtf8("Album") );
listView2->header()->setLabel( 2, trUtf8("Song") );
listView2->header()->setLabel( 3, trUtf8("Time") );
listView2->header()->setLabel( 4, trUtf8("Track Number") );
listView2->clear();
my $item = Qt::ListViewItem(listView2, undef);
$item->setText(0, trUtf8("New Item"));
btnpopulate->setText( trUtf8("Populate") );
btnexit->setText( trUtf8("Exit") );
}
sub mine {
print "Form1->mine(): Not implemented yet.\n";
print "Creating a listitem\n";
puttrackinfo_tofile();
print "artist=$artist\n" if ($DEBUG eq TRUE);
print "trackname=$trackname\n" if ($DEBUG eq TRUE);
print "tracknumber=$tracknumber\n" if ($DEBUG eq TRUE);
print "tracklength=$tracklength\n" if ($DEBUG eq TRUE);
print "genre=$genre\n" if ($DEBUG eq TRUE);
print "album=$album\n" if ($DEBUG eq TRUE);
my $artistitem = Qt::ListViewItem(listView2, undef);
my $songitem = Qt::ListViewItem($artistitem, undef);
$artistitem->setText(0, trUtf8($artist));
$songitem->setText(1, trUtf8(" Maybe"));
my $songitem = Qt::ListViewItem($artistitem, undef);
$songitem->setText(1, trUtf8(" Hopefully"));
}
sub endme {
Qt::Application::exit();
}
sub generateinfofromfilename {
print "Not Implemented Yet generateinfofromfilename\n";
}
sub gettrackinfo_frommp3 {
#$tag->{COMM} = $id3v1->comment();
#$tag->{TIT2} = $id3v1->song();
#$tag->{TPE1} = $id3v1->artist();
#$tag->{TALB} = $id3v1->album();
#$tag->{TYER} = $id3v1->year();
#$tag->{TRCK} = $id3v1->track();
#$tag->{TIT1} = $id3v1->genre();
print "WORK IN PROGRESS: GETTRACKINFO_FROMMP3\n";
$artist = "adema";
$trackname = "close friends";
$tracknumber = "1";
$tracklength = "60";
$genre = "rock";
$album = "black";
$file = "/files/mp3/Adema/Adema/Adema - Blow It Away.mp3";
my $tag = get_tag($file, 1);
unless (defined $tag) {
if (-r $file && -f $file) {
generateinfofromfilename();
}
else {
print "Nonexistent file $file, skipping";
}
next;
}
print "Got tag ", Dumper $tag if ($DEBUG eq TRUE);
$artist = $tag->{TPE1};
$trackname = $tag->{TIT2};
$tracknumber = $tag->{TRCK};
$tracklength = "NOT IMPLEMENT TRACK LENGTH GETTRACKINFO_FROMMP3";
$genre = $tag->{TIT1};
if (!($genre)) { $genre="Rock"; }
$album = $tag->{TALB};
}
sub gettrackinfo_fromfile {
print "Not Implemented Yet PUTTRACKINFO_FROMFILE\n";
}
sub puttrackinfo_tofile {
my $pathname = $basedir.$artist.".$album";
my $line;
my $dontadd = FALSE;
my $lookforme;
my $counter;
open(ALBUMINFO, "$pathname");
my @rawdata = <ALBUMINFO>;
close ALBUMINFO;
$lookforme = join($combochars, $artist, $album, $tracknumber,
$trackname, $tracklength, $genre);
print $pathname,"\n";
print $#rawdata,"\n";
if (!$#rawdata == -1) {
foreach $line (@rawdata) {
if ("$line" eq "$lookforme") {
$dontadd=TRUE;
}
print $line,"\n";
}
}
#if ($dontadd == FALSE) {
$pathname = "/tmp/Adema.Adema";
print "Opening file and printing to it\n";
open(FHA, "> $pathname") or die "can't open $pathname: $!";
#flock(FH, LOCK_EX) or die "can't flock $pathname: $!";
#we probably dont need this.
print FHA "$lookforme\n" or die "cant print $pathname: $!";
#printing to the filehandle wont work but if we move these few lines up
before were listview gets initialized it works fine.
close(FHA) or die "cant close $pathname: $!";
#close(FHA) close FHA and FHA->close report the same thing :(
#}
}
sub makeuppercase {
$artist = uc $artist;
$trackname = uc $trackname;
$tracknumber = uc $tracknumber;
$tracklength = uc $tracklength;
$genre = uc $genre;
$album = uc $album;
}
sub is_valid_track_number {
my $text = shift @_;
return $text && length $text && $text =~ m/\d/;
}
sub set_tag {
my $file = shift @_;
my $tag = shift @_;
my $mp3 = MP3::Tag->new($file);
print Dumper $tag;
my $tags = $mp3->get_tags();
my $id3v2;
if (ref $tags eq 'HASH' && exists $tags->{ID3v2}) {
$id3v2 = $tags->{ID3v2};
}
else {
$id3v2 = $mp3->new_tag("ID3v2");
}
my %old_frames = %{$id3v2->get_frame_ids()};
foreach my $fname (keys %$tag) {
$id3v2->remove_frame($fname) if exists $old_frames{$fname};
if ($fname eq 'WXXX') {
$id3v2->add_frame('WXXX', 'ENG', 'FreeDB URL', $tag->{WXXX}) ;
}
elsif ($fname eq 'COMM') {
$id3v2->add_frame('COMM', 'ENG', 'Comment', $tag->{COMM}) ;
}
else {
$id3v2->add_frame($fname, $tag->{$fname});
}
}
$id3v2->write_tag();
return 0;
}
sub get_tag {
my $file = shift @_;
my $upgrade = shift @_;
my $mp3 = MP3::Tag->new($file);
return undef unless defined $mp3;
$mp3->get_tags();
my $tag = {};
if (exists $mp3->{ID3v2}){
my $id3v2 = $mp3->{ID3v2};
my $frames = $id3v2->supported_frames();
while (my ($fname, $longname) = each %$frames) {
# only grab the frames we know
next unless exists $supported_frames{$fname};
$tag->{$fname} = $id3v2->get_frame($fname);
delete $tag->{$fname} unless defined $tag->{$fname};
$tag->{$fname} = $tag->{$fname}->{Text} if $fname eq 'COMM';
$tag->{$fname} = $tag->{$fname}->{URL} if $fname eq 'WXXX';
$tag->{$fname} = '' unless defined $tag->{$fname};
}
}
elsif (exists $mp3->{ID3v1}) {
warn "No ID3 v2 TAG info in $file, using the v1 tag";
my $id3v1 = $mp3->{ID3v1};
$tag->{COMM} = $id3v1->comment();
$tag->{TIT2} = $id3v1->song();
$tag->{TPE1} = $id3v1->artist();
$tag->{TALB} = $id3v1->album();
$tag->{TYER} = $id3v1->year();
$tag->{TRCK} = $id3v1->track();
$tag->{TIT1} = $id3v1->genre();
set_tag($file, $tag);
}
else {
warn "No ID3 TAG info in $file, creating it";
$tag = { TIT2 => '', TPE1 => '', TALB => '', TYER => 9999, COMM
=> '' };
}
print "Got tag ", Dumper $tag if ($DEBUG eq TRUE);
return $tag;
}
sub print_tag_info {
my $filename = shift @_;
my $tag = shift @_;
my $extra = shift @_ || 'Track info';
# argument checking
return unless ref $tag eq 'HASH';
print "$extra for '$filename':\n";
foreach (keys %$tag)
{
printf "%10s : %s\n", $_, $tag->{$_};
}
}
1;
package main;
use Qt;
use Form1;
my $a = Qt::Application(\@ARGV);
my $w = Form1;
$a->setMainWidget($w);
$w->show;
exit $a->exec;
More information about the Kde-perl
mailing list