Code

Some layer menus
[gosa.git] / include / class_ppdManager.inc
index 86f6c538567661e1317366b6ce201bc244fed88c..a9dfd1a7f995e5bb292b7985864ebac8984039c3 100644 (file)
@@ -4,6 +4,7 @@ class ppdManager
 {
        var $path= "";
        var $cachedList= array();
+       var $timestamps = array();
 
        function ppdManager($path)
        {
@@ -54,8 +55,10 @@ class ppdManager
                $section= preg_replace('/^\*/', '', $section);
                $attribute= preg_replace('/^\*/', '', $attribute);
 
-               $rp= fopen($file, "r");
-               $wp= fopen("$file.tmp", "w");
+               $rp= @fopen($file, "r");
+               $wp= @fopen("$file.tmp", "w");
+
+               
 
                while (!feof($rp)){
                        $lines[]= fgets($rp, 1024);
@@ -71,6 +74,12 @@ class ppdManager
                 continue;
             }
 
+                       /* Change model name .. */
+                       if ((preg_match("/^\*".$attribute.":*\s+/",$line)) && ($attribute == "ModelName")){
+                               $line= "*$attribute: \"$value\"\n";
+                               $done =true;
+                       }
+
                        if (($fsection) && ($section != "NO_SECTION")){
                                if (preg_match("/^\*CloseGroup:*\s+\**$section\/*/", $line)){
                                        $fsection= false;
@@ -132,10 +141,16 @@ class ppdManager
 
        function saveProperties($ppdFile, $properties)
        {
-               foreach ($properties as $name => $section){
-                       foreach ($section as $attribute => $value){
-                               if (is_array($value)){
-                                       $this->updateAttribute($ppdFile, $name, $attribute, $value['_default']);
+               if(!is_readable($ppdFile)){
+                       print_red(sprintf(_("Specified ppd file '%s' can't be opened for reading."),$ppdFile));
+               }elseif(!is_writeable(preg_replace("#(^.*/).*$#","\\1",$ppdFile.".tmp"))){
+                       print_red(sprintf(_("The required tmp file file '%s' can't be opened for writing."),$ppdFile.".tmp"));
+               }else{
+                       foreach ($properties as $name => $section){
+                               foreach ($section as $attribute => $value){
+                                       if (is_array($value)){
+                                               $this->updateAttribute($ppdFile, $name, $attribute, $value['_default']);
+                                       }
                                }
                        }
                }
@@ -274,6 +289,16 @@ class ppdManager
                $model= "";
                $manufacturer= "";
 
+               
+               /* 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]){
+                       return($this->cachedList[$ppdFile]);
+               }
+
+               /* Remember modified timestamp, to speed up next request */
+               $this->timestamps[$ppdFile] = filemtime($ppdFile);
+
                $fh= fopen ($ppdFile, 'r');
                while ((!feof($fh))&&($fh)) {
 
@@ -315,10 +340,11 @@ class ppdManager
                        $list= $this->findPPD($this->path);
 
                        /* Load descriptive informations to build final printer list */
-                       $this->cachedList= array();
+                       $new = array();
                        foreach ($list as $ppdFile){
-                               $this->cachedList[$ppdFile]= $this->loadDescription($ppdFile);
+                               $new[$ppdFile] = $this->loadDescription($ppdFile); 
                        }
+                       $this->cachedList= $new ;
 
                }