From: psc Date: Fri, 20 Feb 2009 12:44:34 +0000 (+0000) Subject: Refactor parts of the plugin: X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=302035383f9a033535aff1844493be611419f237;p=gosa.git Refactor parts of the plugin: - ppdManager::loadDescription now returns a well-formed associative array instead of a string which is parsed over and over again. This was neccessary to fix problems that occured after applying the double-manufacturer-fix, because large parts of the plugin were dependent on the string structure. - Fix display of printer names, it should print the manufacturer only once. - Adapt all usages of the loadDescription function and variables storing its return value. git-svn-id: https://oss.gonicus.de/repositories/gosa/branches/2.6-lhm@13433 594d385d-05f5-0310-b6e9-bd551577e9d8 --- diff --git a/gosa-plugins/goto/admin/systems/goto/class_printGeneric.inc b/gosa-plugins/goto/admin/systems/goto/class_printGeneric.inc index fdd8f3809..f199a7b39 100644 --- a/gosa-plugins/goto/admin/systems/goto/class_printGeneric.inc +++ b/gosa-plugins/goto/admin/systems/goto/class_printGeneric.inc @@ -512,7 +512,8 @@ class printgeneric extends plugin if((!file_exists($path.$this->gotoPrinterPPD))){ $smarty->assign("driverInfo", "".sprintf(_("Your currently selected PPD file '%s' doesn't exist."),$path.$this->gotoPrinterPPD).""); }else{ - $smarty->assign("driverInfo", $ppdManager->loadDescription($path.$this->gotoPrinterPPD)); + $ppdDesc = $ppdManager->loadDescription($path.$this->gotoPrinterPPD); + $smarty->assign("driverInfo", $ppdDesc['name']); } }else{ $smarty->assign("driverInfo", _("Not defined")); diff --git a/gosa-plugins/goto/admin/systems/ppd/class_ppdManager.inc b/gosa-plugins/goto/admin/systems/ppd/class_ppdManager.inc index fe72164d2..db3e50c60 100644 --- a/gosa-plugins/goto/admin/systems/ppd/class_ppdManager.inc +++ b/gosa-plugins/goto/admin/systems/ppd/class_ppdManager.inc @@ -291,10 +291,8 @@ class ppdManager function loadDescription($ppdFile) { - $model= ""; - $manufacturer= ""; - - + $ppdDesc = array(); + /* Only parse complete PPD file again, if it was changed */ $modified = filemtime($ppdFile); if(isset($this->cachedList[$ppdFile]) && isset($this->timestamps[$ppdFile]) && $modified == $this->timestamps[$ppdFile]){ @@ -315,26 +313,31 @@ class ppdManager /* Extract interesting informations */ if (preg_match('/^\*Manufacturer:/i', $line)){ - $manufacturer= preg_replace('/^\*Manufacturer:\s+"?([^"]+)"?.*$/i', '\1', $line); + $ppdDesc['manufacturer'] = trim(preg_replace('/^\*Manufacturer:\s+"?([^"]+)"?.*$/i', '\1', $line)); } if (preg_match('/^\*ModelName:/i', $line)){ - $model= preg_replace('/^\*ModelName:\s+"?([^"]+)"?.*$/i', '\1', $line); + $ppdDesc['model'] = trim(preg_replace('/^\*ModelName:\s+"?([^"]+)"?.*$/i', '\1', $line)); } /* Got everything we need? Skip rest for speed reasons... */ - if ($model != '' && $manufacturer != ''){ + if (isset($ppdDesc['manufacturer']) && isset($ppdDesc['model'])){ break; } } gzclose ($fh); + /* If model contains manufacturer strip it */ + $ppdDesc['model'] = str_replace($ppdDesc['manufacturer']." ", '', $ppdDesc['model']); + /* Write out a notice that the PPD file seems to be broken if we can't extract any usefull informations */ - if ($model == '' || $manufacturer == ''){ + if ($ppdDesc['manufacturer'] == '' || $ppdDesc['model'] == ''){ trigger_error(sprintf(_('Parsing PPD file %s failed - no information found.'), $ppdFile), E_USER_WARNING); } - return ($manufacturer.' - '.$model); + $ppdDesc['name'] = $ppdDesc['manufacturer'] . " - " . $ppdDesc['model']; + + return $ppdDesc; } diff --git a/gosa-plugins/goto/admin/systems/ppd/class_printerPPDDialog.inc b/gosa-plugins/goto/admin/systems/ppd/class_printerPPDDialog.inc index e2875f57a..e82e18597 100644 --- a/gosa-plugins/goto/admin/systems/ppd/class_printerPPDDialog.inc +++ b/gosa-plugins/goto/admin/systems/ppd/class_printerPPDDialog.inc @@ -62,15 +62,14 @@ class printerPPDDialog extends plugin if(!file_exists($this->pathToPPD.$this->pathToModified.$ppdfile)){ msg_dialog::display(_("PPD error"), sprintf(_("Cannot open PPD '%s'!"), $ppdfile), ERROR_DIALOG); }else{ - $res = $this->ppdManager->loadDescription($this->pathToPPD.$this->pathToModified.$ppdfile); - if($res){ - $tmp = split("\n",$res); - $tmp3 = array(); - $tmp3['name'] = trim(preg_replace("/^\-/","",trim($tmp[1]))); - $tmp3['link'] = $ppdfile; - $tmp3['ppd'] = $res; + $ppdDesc = $this->ppdManager->loadDescription($this->pathToPPD.$this->pathToModified.$ppdfile); + if($ppdDesc){ + $selectedPPD = array(); + $selectedPPD['name'] = $ppdDesc['name']; + $selectedPPD['link'] = $ppdfile; + $selectedPPD['ppd'] = $ppdDesc; } - $this->selectedPPD = $tmp3; + $this->selectedPPD = $selectedPPD; } } } @@ -197,12 +196,11 @@ class printerPPDDialog extends plugin msg_dialog::display(_("PPD error"), msgPool::cannotReadFile($AbsoluteSourceName), ERROR_DIALOG); return; } - $res = $this->ppdManager->loadDescription($AbsoluteSourceName); - if($res){ - $tmp = split("\n",$res); - $Name = trim(preg_replace("/^\-/","",trim($tmp[1]))); - $Vendor = trim($tmp[0]); - $Model = trim(preg_replace("/".$Vendor."/","",$Name)); + $ppdDesc = $this->ppdManager->loadDescription($AbsoluteSourceName); + if($ppdDesc){ + $Name = $ppdDesc['name']; + $Vendor = $ppdDesc['manufacturer']; + $Model = $ppdDesc['model']; } $PrinterName = $this->cn."-".preg_replace("/[^a-z0-9-_\.]/i","_",$Name); @@ -251,12 +249,11 @@ class printerPPDDialog extends plugin $this->getPrinterReload(); /* Get Description from ppd, & parse out some informations */ - $res = @$this->ppdManager->loadDescription($_PathOnHdd); - if($res){ - $tmp = split("\n",$res); - $name = trim(preg_replace("/^\-/","",trim($tmp[1]))); - $vendor = trim($tmp[0]); - $model = trim(preg_replace("/".$vendor."/","",$name)); + $ppdDesc = @$this->ppdManager->loadDescription($_PathOnHdd); + if($ppdDesc){ + $name = $ppdDesc['name']; + $vendor = $ppdDesc['manufacturer']; + $model = $ppdDesc['model']; } /* Check if parse was successfull */ @@ -342,18 +339,18 @@ class printerPPDDialog extends plugin /* Sort all available files, and create header (Vendor index) */ foreach($tmp as $file=>$ppd){ - + if(preg_match("#".$this->pathToModified."#",$file)) continue; - $tmp2 = split("\n",$ppd); - if(!isset($this->ppdListHeader[$tmp2[0]])){ - $this->ppdListHeader[$tmp2[0]]=0; + if(!isset($this->ppdListHeader[$ppd['manufacturer']])){ + $this->ppdListHeader[$ppd['manufacturer']]=0; } - $tmp3['name'] =preg_replace("/^ -/","",$tmp2[1]." - ".$tmp2[2]); + + $tmp3['name'] =$ppd['name']; $tmp3['link'] =$file; $tmp3['ppd'] =$ppd; - $this->ppdListHeader[$tmp2[0]]++; - $this->ppdList[$tmp2[0]][preg_replace("/^ -/","",$tmp2[1]." - ".$tmp2[2])]=$tmp3; + $this->ppdListHeader[$ppd['manufacturer']]++; + $this->ppdList[$ppd['manufacturer']][$ppd['name']]=$tmp3; } } } @@ -400,12 +397,12 @@ class printerPPDDialog extends plugin { $str = "none"; if(!empty($this->selectedPPD)){ - $str = $this->ppdManager->loadDescription($this->pathToPPD.$this->pathToModified.$this->selectedPPD['link']); + $ppdDesc = $this->ppdManager->loadDescription($this->pathToPPD.$this->pathToModified.$this->selectedPPD['link']); + $str = $ppdDesc['name']; } return($str) ; } - /* Display all options from the selected ppd file */ function generateProperties() { diff --git a/gosa-plugins/goto/admin/systems/ppd/class_printerPPDSelectionDialog.inc b/gosa-plugins/goto/admin/systems/ppd/class_printerPPDSelectionDialog.inc index 546ac7d8b..e0dcd1c93 100644 --- a/gosa-plugins/goto/admin/systems/ppd/class_printerPPDSelectionDialog.inc +++ b/gosa-plugins/goto/admin/systems/ppd/class_printerPPDSelectionDialog.inc @@ -106,7 +106,8 @@ class printerPPDSelectionDialog extends plugin $list = array(); foreach($this->list as $cat => $ppds){ foreach($ppds as $ppd){ - if(preg_match("/^".str_replace("*",".*",$regex)."/i",$ppd['ppd'])){ + $name = $ppd['ppd']['name']; + if(preg_match("/^".str_replace("*",".*",$regex)."/i",$name)){ if(is_readable($ppd['link'])){ $list[$ppd['link']] = $ppd; } @@ -142,6 +143,7 @@ class printerPPDSelectionDialog extends plugin if(empty($this->Vendor)){ foreach($this-> header as $key => $entry){ $div ->AddEntry (array( + array("string"=>sprintf($linkopen,base64_encode($key),$key),"attach"=>"style='border-right:0px;'") )); } @@ -150,7 +152,8 @@ class printerPPDSelectionDialog extends plugin array("string"=>sprintf($linkopen,"",".. ["._("back")."]"),"attach"=>"style='border-right:0px;'") )); foreach($list as $key => $ppd){ - if(preg_match("/^".$this->Vendor."/",$ppd['ppd'])){ + $name = $ppd['ppd']['name']; + if(preg_match("/^".$this->Vendor."/", $name)){ if(is_writeable($ppd['link'])){ $del_str = sprintf($dellink,base64_encode($key)); @@ -159,7 +162,7 @@ class printerPPDSelectionDialog extends plugin } $div ->AddEntry (array( - array("string"=>sprintf($uselink,base64_encode($key),$ppd['ppd'])), + array("string"=>sprintf($uselink,base64_encode($key),$name)), array("string"=>$del_str,"attach"=>"style='border-right:0px;'") ));