Icon-Theme-Helper

 view release on metacpan or  search on metacpan

lib/Icon/Theme/Helper.pm  view on Meta::CPAN

	my %args;
	if (defined( $_[1] )) {
		%args=%{$_[1]};
	}
	my $method='new';

	my $self={error=>undef, perror=>undef, errorString=>'', module=>'Icon-Theme-Helper'};
	bless $self;

	#try to automatically detect the directory to use if none is specified
	if (!defined( $args{dir} )) {
		if (-d '/usr/local/share/icons/') {
			$self->{dir}='/usr/local/share/icons/';
		}else {
			if (-d '/usr/share/icons/') {
				$self->{dir}='/usr/share/icons/';
			}
		}

		#error if no directory can be found
		if (!defined( $self->{dir} )) {
			$self->{error}=1;
			$self->{perror}=1;
			$self->{errorString}='No directory specified and one could not be automatically located';
			warn($self->{module}.' '.$method.':'.$self->{error}.': '.$self->{errorString});
			return $self;
		}

	}else {
		$self->{dir}=$args{dir};

		#error if the specified directory can not be found
		if (!defined( $self->{dir} )) {
			$self->{error}=2;
			$self->{perror}=1;
			$self->{errorString}='The specified directory, "'.$self->{dir}.'", does not exist';
			warn($self->{module}.' '.$method.':'.$self->{error}.': '.$self->{errorString});
			return $self;
		}
	}


	#get the theme to use
	if (!defined( $args{theme} )) {
		$self->{theme}='hicolor';
	}else {
		$self->{theme}=$args{theme};
	}

	if (! -d $self->{dir}.'/'.$self->{theme} ) {
			$self->{error}=3;
			$self->{perror}=1;
			$self->{errorString}='The specified directory, "'.$self->{dir}.'", does not exist';
			warn($self->{module}.' '.$method.':'.$self->{error}.': '.$self->{errorString});
			return $self;
	}

	if (! -f $self->{dir}.'/'.$self->{theme}.'/index.theme' ) {
			$self->{error}=4;
			$self->{perror}=1;
			$self->{errorString}='The specified theme, "'.$self->{theme}.'", is not a Freedesktop.org compliant icon theme';
			warn($self->{module}.' '.$method.':'.$self->{error}.': '.$self->{errorString});
			return $self;
	}

	#holds the required themes
	$self->{themes}={};

	#read the default theme
	$self->{themes}{$self->{theme}}=Icon::Theme::Index::Parse->new_from_file($self->{dir}.'/'.$self->{theme}.'/index.theme');
	if ($self->{themes}{$self->{theme}}->{error}) {
			$self->{error}=5;
			$self->{perror}=1;
			$self->{errorString}='Failed to load the index for the theme "'.$self->{theme}.'"';
			warn($self->{module}.' '.$method.':'.$self->{error}.': '.$self->{errorString});
			return $self;
	}

	#gets the themes it inherits
	my @inherits=$self->{themes}{$self->{theme}}->inherits;

	#skips over any thing it is told to be skipped

	#builds a order array of themes to check
	my @themeOrder=( $self->{theme} );
	push(@themeOrder, @inherits);

	#this holds a list of items that have been processed
	my %processed;
	$processed{$self->{theme}}=1; #adds the starting theme to the list to avoid it

	#processes each one...
	my $int=1; #we start at 1 as the first item in the themeOrder list is the theme that was used
	while (defined( $themeOrder[$int] )) {
		my $theme=$themeOrder[$int];

		#only process it if we have now already
		if (!defined( $processed{$theme} )) {
			#only parse it if the index file exists
			my $index=$self->{dir}.'/'.$theme.'/index.theme';
			if (-f $index) {
				my $parsedTheme=Icon::Theme::Index::Parse->new_from_file($index);
				#process this theme if it did not error
				if (! $parsedTheme->{error} ) {
					$self->{themes}{$theme}=$parsedTheme;
					#
					push(@themeOrder, $theme);
					#push the inherited stuff onto this stack
					@inherits=$self->{themes}{$theme}->inherits;
					push(@themeOrder, @inherits);
					
					#mark as processed so it is not done again
					$processed{$theme}=1
				}
			}
		}

		$int++;
	}

	#include hicolor if needed

lib/Icon/Theme/Helper.pm  view on Meta::CPAN

				if (! $parsedTheme->{error} ) {
					$self->{themes}{'gnome'}=$parsedTheme;
					#push this theme into the order
					push(@themeOrder, 'gnome');
					#push the inherited stuff onto this stack
					@inherits=$self->{themes}{'gnome'}->inherits;
					push(@themeOrder, @inherits);
					
					#mark as processed so it is not done again
					$processed{'gnome'}=1
				}
			}
		}
	}
	
	#cleans the order list of redundant items
	my @cleanThemeOrder;
	my %cleanedThemes; #used for check if a theme has been cleaned or not
	$int=0;
	while ( defined( $themeOrder[$int] ) ) {
		my $theme=$themeOrder[$int];

		if (!defined( $cleanedThemes{ $theme } )) {
			push(@cleanThemeOrder, $theme);
			$cleanedThemes{$theme}=1;
		}

		$int++;
	}

	#saves the theme order
	$self->{order}=\@cleanThemeOrder;

	#saves the min, max, preferred size, and scalable boolean
	if (defined( $args{minsize} )) {
		$self->{minSize}=$args{minsize};
	}
	if (defined( $args{maxSize} )) {
		$self->{maxSize}=$args{maxSize};
	}
	if (defined( $args{size} )) {
		$self->{size}=$args{size};
	}else {
		$self->{size}='32';
	}
	if (defined($args{scalable} )) {
		$self->{scalable}=$args{scalable};
	}else {
		$self->{scalable}='1';
	}

	return $self;
}

=head2 getIcon

This returns the icon file.

The first arguement is the context and the second is the icon name.

    my $iconFile=$iconhelper->getIcon('Places', 'desktop');

=cut

sub getIcon{
	my $self=$_[0];
	my $context=$_[1];
	my $name=$_[2];
	my $method='getIcon';

	#blanks any previous errors
	$self->errorblank;
	if ($self->{error}) {
		return undef;
	}

	#error if there is no context specified
	if (!defined( $context )) {
			$self->{error}=6;
			$self->{errorString}='No context defined';
			warn($self->{module}.' '.$method.':'.$self->{error}.': '.$self->{errorString});
			return $self;
	}

	#error if there is no name specified
	if (!defined( $name )) {
			$self->{error}=7;
			$self->{errorString}='No icon name defined';
			warn($self->{module}.' '.$method.':'.$self->{error}.': '.$self->{errorString});
			return $self;
	}

	#process each theme till we find a int
	my $themeInt=0;
	while (defined( $self->{order}[$themeInt] )) {
		my $theme=$self->{order}[$themeInt];

		#check each directory
		my $dirInt=0;
		if (defined( $self->{themes}{$theme} )) {
			my @dirs=$self->{themes}{$theme}->directories;
			if (!$self->{themes}{$theme}->{error}) {
				while (defined( $dirs[$dirInt] )) {
					my $dircontext=$self->{themes}{$theme}->dirContext( $dirs[$dirInt] );
					if (defined($dircontext) && ($context eq $dircontext)) {
						my $check=1; #default if true;
						
						my $size=$self->{themes}{$theme}->dirSize( $dirs[$dirInt] );
						
						#checks if it needs to check minSize
						if (defined($self->{minSize}) && defined($size)) {
							#don't check this directory if the size is to small
							if ( $self->{minSize} > $size ) {
								$check=0;
							}
						}
						
						#checks if it needs to check maxSize
						if (defined($self->{maxSize}) && defined($size) && $check ) {
							#don't check this directory if the size is to large
							if ( $self->{maxSize} < $size ) {

lib/Icon/Theme/Helper.pm  view on Meta::CPAN

	#if we get here we did not find one
	return undef;
}

=head1 ERROR METHODS

=head2 error

This fetches any current errors.

A return of undef, false, indicates no error is present.

    if($iconhelper->error){
        warn("Error!");
    }

=cut

sub error{
	return $_[0]->{error};
}

=head2 errorblank

This blanks any previous error.

This is a internal function.

=cut

sub errorblank{
	my $self=$_[0];

	if ($self->{perror}) {
		warn($self->{module}.' errorblank: A permanent error is set');
	}

	$self->{error}=undef;
	$self->{errorString}=undef;

	return 1;
}


=head1 ERROR CODES

=head2 1

No directory specified and one could not be automatically located.

=head2 2

The specified directory does not exist.

=head2 3

The specified theme does not exist.

=head2 4

The theme is not a Freedesktop.org compliant theme.

=head2 5

Parsing the index for the specified config failed.

=head2 6

No context specified.

=head2 7

No icon name specified.

=head2 8

No MIME type specified.

=head1 AUTHOR

Zane C. Bowers, C<< <vvelox at vvelox.net> >>

=head1 BUGS

Please report any bugs or feature requests to C<bug-icon-theme-helper at rt.cpan.org>, or through
the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Icon-Theme-Helper>.  I will be notified, and then you'll
automatically be notified of progress on your bug as I make changes.




=head1 SUPPORT

You can find documentation for this module with the perldoc command.

    perldoc Icon::Theme::Helper


You can also look for information at:

=over 4

=item * RT: CPAN's request tracker

L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Icon-Theme-Helper>

=item * AnnoCPAN: Annotated CPAN documentation

L<http://annocpan.org/dist/Icon-Theme-Helper>

=item * CPAN Ratings

L<http://cpanratings.perl.org/d/Icon-Theme-Helper>

=item * Search CPAN

L<http://search.cpan.org/dist/Icon-Theme-Helper/>

=back




( run in 1.705 second using v1.01-cache-2.11-cpan-5837b0d9d2c )