~nandhp/yocto/trunk

« back to all changes in this revision

Viewing changes to yocto

  • Committer: nandhp
  • Date: 2010-05-08 22:07:31 UTC
  • Revision ID: nandhp@gmail.com-20100508220731-x6kk8b7yz995pirb
Switch to XDG data directory (backwards incompatible), default to XDG Music directory, use File::Spec, fix for Notification area in Ubuntu Lucid.

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
use Encode qw/encode decode/;
22
22
use Gtk2;
23
23
use Gtk2::TrayIcon;
24
 
use File::Basename;
 
24
use File::Basename;                     # Replace with File::Spec?
 
25
use File::Spec::Functions qw/:ALL/;
 
26
use File::Path;
25
27
use FindBin;
26
28
use Getopt::Long;
27
29
use IO::Socket::INET;
34
36
# strings from header comments (name, description, copyright, license)
35
37
my $datadir = <DATA>;
36
38
if ( $datadir && $datadir =~ m/^datadir=(.+?)\s+$/ ) { $datadir = $1 }
37
 
else { $datadir = '.' }
 
39
else { $datadir = File::Spec->curdir() }
38
40
seek(DATA,0,0);                         # Jump to the top of the file
39
41
my ($progname,$tagline,$copyright,$license) = (undef,undef,undef);
40
42
while (<DATA>) {
93
95
   Gtk2::Gdk::Pixbuf->new_from_file_at_size($iconfile,128,128));
94
96
my $tipobj = Gtk2::Tooltips->new;       # Tooltips widget
95
97
 
 
98
# Lookup XDG Music directory
 
99
my $xdghome = Glib::get_home_dir()||'';
 
100
my $xdgmusic = (eval {Glib::get_user_special_dir('music')})||$xdghome;
96
101
# Load the configuration file
97
 
my $configfile = "$ENV{HOME}/.yocto";
 
102
my $configdir = catdir(Glib::get_user_config_dir(),'yocto');
 
103
my $configfile = catfile($configdir,'config');
98
104
my %config;
99
105
my %configcomments = ();
100
106
LoadConfig();                      # Load default configuration
 
107
MigrateConfig(catfile($ENV{HOME},'.yocto')) if $ENV{HOME};
101
108
LoadConfig($configfile);           # Load user configuration
102
109
SaveConfig();                      # Save changes (e.g. from upgrades)
103
110
 
247
254
        return $file if (-e $file);
248
255
        return undef;
249
256
    }
250
 
    foreach my $dir ($datadir,$FindBin::Bin,@INC,split(':', $ENV{PATH}),'.','/usr/local/share','/usr/share','/usr/share/pixmaps','/usr/local/share/pixmaps','/usr/share/yocto','/usr/local/share/yocto')
 
257
    foreach my $dir ($datadir,$FindBin::Bin,@INC,path(),'.','/usr/local/share','/usr/share','/usr/share/pixmaps','/usr/local/share/pixmaps','/usr/share/yocto','/usr/local/share/yocto')
251
258
    {
252
259
        my $f = "$dir/$file";
253
260
        return $f if -e $f;
452
459
        }
453
460
        elsif ( !$song{file} || $status{state} eq 'stop' ) { SetCover('') }
454
461
        else {
455
 
            my $d = $config{External}{MusicDirectory}.'/'.dirname($song{file});
 
462
            my $d = catdir($config{External}{MusicDirectory},
 
463
                           dirname($song{file}));
456
464
            opendir DIR, $d;
457
465
            my @f = sort grep { PickCover($d,$_) } readdir DIR;
458
466
            closedir DIR;
459
 
            if ( @f == 1 || @f ) { SetCover("$d/$f[0]") } # Ambiguous too
 
467
            if ( @f == 1 || @f ) { SetCover(catfile($d,$f[0])) }# Ambiguous too
460
468
            elsif ( @f == 0 ) { SetCover('') }
461
469
        }
462
470
    }
842
850
    my ($wx,$alloc) = @_;
843
851
    my ($oldsize) = $icon->get_size_request();
844
852
    my ($w,$h) = ($alloc->width,$alloc->height);
845
 
    if ( $w != $h ) {
846
 
        my $size = $w > $h ? $w : $h;
847
 
        if ( $h == $oldsize ) { $size = $w }
848
 
        if ( $w == $oldsize ) { $size = $h }
849
 
        $icon->set_size_request($size,$size);
850
 
    }
 
853
    #print "A $w $h\n";
 
854
    #if ( 1 ) {
 
855
    my $size = $w > $h ? $w : $h;
 
856
    if ( $h == $oldsize ) { $size = $w }
 
857
    if ( $w == $oldsize ) { $size = $h }
 
858
    $icon->set_size_request($size,$size) if $size != $oldsize;
 
859
    #}
851
860
}
852
861
 
853
862
sub ButtonEvent {
1090
1099
 
1091
1100
[External]
1092
1101
# Set this to MPD's music_directory
1093
 
MusicDirectory=
 
1102
MusicDirectory=$xdgmusic
1094
1103
# Runs whenever the last track is selected
1095
1104
RefillCommand=
1096
1105
# Command to start MPD if AutoStart is on
1138
1147
    close F;
1139
1148
}
1140
1149
 
 
1150
sub MigrateConfig {
 
1151
    # Load old configuration file if new file does not exist. If the
 
1152
    # new file was created successfully, delete the old one.
 
1153
    my ($old) = @_; if ( -e $old && !-e $configfile )
 
1154
      { LoadConfig($old); SaveConfig(); unlink($old) if -e $configfile }
 
1155
}
 
1156
 
1141
1157
sub SaveConfig {
1142
1158
    # Write a new configuration file.
1143
1159
    # - Do not write a new file if there are no changes.
1200
1216
    }
1201
1217
    # Save the file if it changed. Chmod the file if it didn't exist before.
1202
1218
    if ( $changed ) {
 
1219
        # Create the config directory
 
1220
        mkpath(catpath((splitpath($configfile))[0..1]),{mode => 0700});
1203
1221
        open F, '>', $configfile or warn "Failed to open $configfile: $!\n";
1204
1222
        eval { chmod 0600, $configfile } if $chmod; # chmod newly created file
1205
1223
        print F @output;
1652
1670
       sub {
1653
1671
           my $dirselect = Gtk2::FileChooserDialog->new('Select Music Location',$dialog,'select-folder',('gtk-cancel','reject','gtk-open','accept'));
1654
1672
           $dirselect->set_local_only(1);
1655
 
           $dirselect->set_filename($direntry->get_text()||$ENV{HOME}||'');
 
1673
           $dirselect->set_filename($direntry->get_text()||$xdgmusic||'');
1656
1674
           my $r = $dirselect->run();
1657
1675
           $direntry->set_text($dirselect->get_filename()) if $r eq 'accept';
1658
1676
           $dirselect->destroy();