Code

Correctly display and save PPDs when an option is not translated. (TRAC #5063)
[gosa.git] / trunk / gosa-plugins / goto / admin / systems / ppd / class_ppdManager.inc
index fe72164d2ea787168b9528d62d5b275d3fec9439..4a6d16556e00330cab372ada37b23a165a77a666 100644 (file)
@@ -162,7 +162,12 @@ class ppdManager
        }
 
        function loadProperties($ppdFile)
-       {
+  {
+    if(!(filesize($ppdFile))) {
+      trigger_error(_('Parsing PPD file failed - File has 0 bytes'));
+      return;
+    }
+
                $group= "";
                $option= "";
                $properties= array();
@@ -274,8 +279,16 @@ class ppdManager
                                /* Possible value? */
                                if (preg_match("@^\*$eoption\s+@", $line)){
 #*PageSize Letter/US Letter: "<>setpagedevice"
-                                       $c= preg_replace("@^\*$eoption\s+([^/]+).*$@", "$1", $line);
+                                       /* Options can have one of 2 forms:
+                                          *OptionName OptionValue/TranslationOfValue: ...
+                                          *OptionName OptionValue: ...
+                                          i.e. there may be a translation or not.
+                                       */
+                                       $c= preg_replace("@^\*$eoption\s+([^/:]+).*$@", "$1", $line);
                                        $d= preg_replace("@^\*$eoption\s+[^/]+/([^:]+).*$@", "$1", $line);
+                                       if ($d == $line) { /* if there is no translation, use untranslated value*/
+                                               $d = "$c";
+                                       }
                                        if(empty($group)){
                                                $properties["NO_SECTION"][$option][trim($c)]= trim($d);
                                        }else{
@@ -290,11 +303,14 @@ class ppdManager
        }
 
        function loadDescription($ppdFile)
-       {
-               $model= "";
-               $manufacturer= "";
-
-               
+  {
+    if(!(filesize($ppdFile))) {
+      trigger_error(_('Parsing PPD file failed - File has 0 bytes'));
+      return;
+    }
+
+               $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 +331,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;
        }