ZConf-GUI
view release on metacpan or search on metacpan
lib/ZConf/GUI.pm view on Meta::CPAN
#if init works, it is now inited and thus we set it to one
$self->{init}=1;
}
#we don't set any error stuff here even if the above action failed...
#it will have been set any ways by init methode
return $self;
}
#reads it if it does not need to be initiated
if ($self->{init}) {
$self->{zconf}->read({set=>$self->{set}, config=>'gui'});
}else{
}
return $self;
}
=head2 getAppendOthers
This gets the value for 'appendOthers'.
my $appendOthers=$zg->getAppendOthers;
if($zg->{error}){
print "Error!\n";
}
=cut
sub getAppendOthers{
my $self=$_[0];
if ( ! $self->errorblank ){
return undef;
}
#fetch the preferences for the module
my %vars=$self->{zconf}->regexVarGet('gui', '^appendOthers$');
if( $self->{zconf}->error ){
$self->{error}=1;
$self->{errorString}='ZConf error getting value "appendOthers" in "gui".'.
' ZConf error="'.$self->{zconf}->error.'" '.
'ZConf error string="'.$self->{zconf}->errorString.'"';
$self->warn;
return undef;
}
#if we don't have it, return true
if (!defined($vars{appendOthers})) {
return 1;
}
#return it's value
return $vars{appendOthers}
}
=head2 getPreferred
This gets the preferred for a module.
my @prefs=$zg->getPreferred('ZConf::Runner');
if($zg->{error}){
print "Error!\n";
}
=cut
sub getPreferred{
my $self=$_[0];
my $module=$_[1];
if ( ! $self->errorblank ){
return undef;
}
if (!defined($module)) {
$self->{errorString}='No module specified';
$self->{error}=2;
$self->warn;
return undef;
}
#the change it for fetching the info
my $module2=$module;
$module2=~s/\:\:/\//g;
#fetch the preferences for the module
my %vars=$self->{zconf}->regexVarGet('gui', '^modules/'.quotemeta($module2).'$');
if($self->{zconf}->error){
$self->{error}=1;
$self->{errorString}='ZConf error listing sets for the config "gui".'.
' ZConf error="'.$self->{zconf}->error.'" '.
'ZConf error string="'.$self->{zconf}->errorString.'"';
$self->warn;
return undef;
}
#if we don't get it, try the default
if (!defined($vars{'modules/'.$module2})) {
%vars=$self->{zconf}->regexVarGet('gui', '^default$');
if($self->{zconf}->error){
$self->{error}=1;
$self->{errorString}='ZConf error listing sets for the config "gui".'.
' ZConf error="'.$self->{zconf}->error.'" '.
'ZConf error string="'.$self->{zconf}->errorString.'"';
$self->warn;
return undef;
}
#if the default does not exist
if (!defined($vars{default})) {
$self->{error}=5;
$self->{errorString}='No preferences for "'.$module.'" and there is no default';
$self->warn;
return undef;
}
return split(/:/, $vars{default});
}
return split(/:/, $vars{'modules/'.$module2});
lib/ZConf/GUI.pm view on Meta::CPAN
if($zg->{error}){
print "Error!\n";
}
=cut
sub setAppendOthers{
my $self=$_[0];
my $boolean=$_[1];
if ( ! $self->errorblank ){
return undef;
}
#make sure we were passed something
if (!defined($boolean)) {
$self->{error}=8;
$self->{errorString}='No value specified to set "appendOthers" to';
$self->warn;
return undef;
}
#make sure it is a 0 or 1
if ($boolean !~ /^[01]$/) {
$self->{error}=9;
$self->{errorString}='The value "'.$boolean.'" does not match /^[01]$/';
$self->warn;
return undef;
}
#set the value
$self->{zconf}->setVar('gui', 'appendOther');
if($self->{zconf}->error){
$self->{error}=1;
$self->{errorString}='ZConf error setting "appendOthers" for "gui".'.
' ZConf error="'.$self->{zconf}->error.'" '.
'ZConf error string="'.$self->{zconf}->errorString.'"';
$self->warn;
return undef;
}
#update it
$self->{zconf}->writeSetFromLoadedConfig({config=>'gui'});
if($self->{zconf}->error){
$self->{error}=1;
$self->{errorString}='ZConf error saving config "gui".'.
' ZConf error="'.$self->{zconf}->error.'" '.
'ZConf error string="'.$self->{zconf}->errorString.'"';
$self->warn;
return undef;
}
return 1;
}
=head2 setPreferred
This sets the preferred GUI back ends. The first arguement is the module.
The second is a array reference of the prefences.
my @prefs=('GUI', 'Curses');
#set it for ZConf::BGSet
my $zg->setPreferred('ZConf::BGSet', \@prefs);
=cut
sub setPreferred{
my $self=$_[0];
my $module=$_[1];
my $prefs;
if (defined($_[2])) {
$prefs=$_[2];
}
if ( ! $self->errorblank ){
return undef;
}
if (!defined($module)) {
$self->{error}=2;
$self->{errorString}='No module specified';
$self->warn;
return undef;
}
if (!defined(@{$prefs}[0])) {
$self->{error}=3;
$self->{errorString}='No prefs specified';
$self->warn;
return undef;
}
my $int=0;
while (defined(@{$prefs}[$int])){
if (@{$prefs}[$int] =~ /:/) {
$self->{error}=4;
$self->{errorString}='"'.@{$prefs}[$int].'" matched /:/';
$self->warn;
return undef;
}
$int++;
}
$module=~s/::/\//g;
my $joinedprefs=join(':', @{$prefs});
$self->{zconf}->setVar('gui', 'modules/'.$module, $joinedprefs);
if($self->{zconf}->error){
$self->{error}=1;
$self->{errorString}='ZConf error listing sets for the config "gui".'.
' ZConf error="'.$self->{zconf}->error.'" '.
'ZConf error string="'.$self->{zconf}->errorString.'"';
$self->warn;
return undef;
}
$self->{zconf}->writeSetFromLoadedConfig({config=>'gui'});
if($self->{zconf}->error){
$self->{error}=1;
$self->{errorString}='ZConf error saving config "gui".'.
' ZConf error="'.$self->{zconf}->error.'" '.
'ZConf error string="'.$self->{zconf}->errorString.'"';
$self->warn;
return undef;
}
return 1;
}
=head2 setUseX
This determines if X should be used or not. This only affects terminal
related modules that respect this.
$zcgui->setUseX('ZConf::Runner', '1');
if($zcgui->{error}){
print "Error!";
}
=cut
sub setUseX{
my $self=$_[0];
my $module=$_[1];
my $useX=$_[2];
if ( ! $self->errorblank ){
return undef;
}
if (!defined($module)) {
$self->{error}=2;
$self->{errorString}='No module specified';
$self->warn;
return undef;
}
if (!defined($useX)) {
$self->{error}=3;
$self->{errorString}='No prefs specified';
$self->warn;
return undef;
}
$module=~s/::/\//g;
$self->{zconf}->setVar('gui', 'useX/'.$module, $useX);
if($self->{zconf}->{error}){
$self->{error}=1;
$self->{errorString}='ZConf error listing sets for the config "gui".'.
' ZConf error="'.$self->{zconf}->error.'" '.
'ZConf error string="'.$self->{zconf}->errorString.'"';
$self->warn;
return undef;
}
$self->{zconf}->writeSetFromLoadedConfig({config=>'gui'});
return 1;
}
=head2 termAvailable
This checks to see if a terminal is available. It checks
if $ENV{TERM} is set or not. If this is not set, it was most
likely not ran from with a terminal.
if($zg->termAvailable){
print "a terminal is available";
}else{
print "no terminal is available";
}
=cut
sub termAvailable{
my $self=$_[0];
if ( ! $self->errorblank ){
return undef;
}
if (!defined($ENV{TERM})) {
return undef;
}
return 1;
}
=head2 Xavailable
This checks if X is available. This is checked for by trying to run
'/bin/sh -c \'xhost 2> /dev/null > /dev/null\'' and is assumed if a
non-zero exit code is returned then it failed and thus X is not
available.
There is no reason to ever check $zcr->{error} with
this as this function will not set it. It just returns
a boolean value.
lib/ZConf/GUI.pm view on Meta::CPAN
my $useX=$zcgui->useX('ZConf::Runner');
=cut
sub useX{
my $self=$_[0];
my $module=$_[1];
if ( ! $self->errorblank ){
return undef;
}
if (!defined($module)) {
$self->{error}=2;
$self->{errorString}='No module specified';
$self->warn;
return undef;
}
#get if X should be used
my $useX=$self->getUseX($module);
#if it should be used, make sure it is available
if ($useX) {
return $self->Xavailable;
}
return undef;
}
=head2 which
This chooses which should be used. This returns all available
backends in order of preference.
my @choosen=$zg->which('ZConf::BGSet');
if($zg->{error}){
print "Error!";
}
print 'The primary preferred module is "'.$choosen[0].'"';
=cut
sub which{
my $self=$_[0];
my $module=$_[1];
if ( ! $self->errorblank ){
return undef;
}
if (!defined($module)) {
$self->{error}=2;
$self->{errorString}='No module specified';
$self->warn;
return undef;
}
my @prefs=$self->getPreferred($module);
#checks if X and/or a terminal is available
my $Xavailable=$self->Xavailable();
my $termAvailable=$self->termAvailable();
#gets usable modules
my @available=$self->listAvailable($module);
#this will be returned
my @usable;
#get the preferred ones
my @preferred=$self->getPreferred($module);
#builds the list out of the prefered modules initially
my $int=0;
while (defined($preferred[$int])) {
my $aint=0;
my $matched=0;
while ((defined($available[$aint])) && (!$matched)) {
if ($preferred[$int] eq $available[$aint]) {
if ($Xavailable) {
push(@usable, $preferred[$int]);
$matched=1;
}else {
if ($preferred[$int]=~/^Term/) {
push(@usable, $preferred[$int]);
}
if ($preferred[$int]=~/^Curses/) {
push(@usable, $preferred[$int]);
}
$matched=1;
}
$aint++;
}
$aint++;
}
$int++;
}
#determine if we should append others or not
my $ao=$self->getAppendOthers;
if ($self->error) {
$self->warnString('getAppendOthers errored');
return undef;
}
#only process AO if we need to
if (!$ao) {
return @usable;
}
#append others if we need to
$int=0;
while (defined($available[$int])) {
#make sure it has not been added previously
( run in 0.549 second using v1.01-cache-2.11-cpan-8f98c5d2c55 )