home *** CD-ROM | disk | FTP | other *** search
/ Chip 2000 May / Chip_2000-05_cd2.bin / suse / inst-sys / lib / YaST2 / clients / printconf_write_printer.ycp < prev    next >
Encoding:
Text File  |  2000-03-30  |  4.0 KB  |  141 lines

  1. /* The Printer Configurator
  2.  * Module for writing a printer record to the spooling system
  3.  * Jan Holesovsky <kendy@suse.cz>, 2000
  4.  *
  5.  * $Id: printconf_write_printer.ycp,v 1.3 2000/02/22 08:57:57 kendy Exp $
  6.  */
  7.  
  8. /* Arguments:
  9.      1st: (map)    printer to be written
  10.      2nd: (string) "add", "delete", "update"
  11.      
  12.    Returns:
  13.      true          everything was successful
  14.      false         otherwise
  15. */
  16.  
  17. {
  18.   map printer = Args(0);
  19.   string aps_operation = Args(1);
  20.  
  21.   string upp_dir = "/etc/gs.upp/";
  22.   string upp_file = "";                /* This will be written... */
  23.  
  24.   /* May I add this printer? */
  25.   boolean|void add_it = lookup(printer, "add");
  26.   if (add_it != true)
  27.     return false;
  28.  
  29.   if (!Mkdir(upp_dir)) {
  30.     UI(`DisplayMessage(sformat(UI(_("Can't create directory '%1'!")), upp_dir)));
  31.     return false;
  32.   }
  33.   
  34.   string not_properly = UI(_("ERROR: Record '%1' is
  35. not properly configured."));
  36.   
  37.   list|void names = lookup(printer, "names");
  38.   if (names == nil) {
  39.     UI(`DisplayMessage(sformat(not_properly, "names")));
  40.     return false;
  41.   }
  42.  
  43.   string filename = "y2prn";
  44.   foreach(`nam, names, ``{ filename = filename+"_"+nam; });
  45.   
  46.   string upp_file_name = filename+".upp";
  47.   string y2_file_name = filename+".y2";
  48.  
  49.   string|void manufacturer = lookup(printer, "manufacturer");
  50.   if (manufacturer == nil) {
  51.     UI(`DisplayMessage(sformat(not_properly, "manufacturer")));
  52.     return false;
  53.   }
  54.   string|void model = lookup(printer, "model");
  55.   if (model == nil) {
  56.     UI(`DisplayMessage(sformat(not_properly, "model")));
  57.     return false;
  58.   }
  59.   string|void connection = lookup(printer, "connection");
  60.   if (connection == nil) {
  61.     UI(`DisplayMessage(sformat(not_properly, "connection")));
  62.     return false;
  63.   }
  64.   
  65.   /* Model of the printer (and information for administration) */
  66.   upp_file = sformat("-supModel=\"%1 %2\"\n", manufacturer, model);
  67.  
  68.   map|void ps2printer = lookup(printer, "ps2printer");
  69.   if (ps2printer == nil) {
  70.     UI(`DisplayMessage(sformat(not_properly, "ps2printer")));
  71.     return false;
  72.   }
  73.   
  74.   list|void params = lookup(ps2printer, "params");
  75.   list|void options = lookup(ps2printer, "options");
  76.   if (params != nil)
  77.     foreach(`par, params, ``{ upp_file = upp_file+sformat("%1\n", par); });
  78.   if (options != nil) {
  79.     foreach(`opt, options, ``{
  80.       string|void def = lookup(opt, "default");
  81.       if (def != nil)
  82.         upp_file = upp_file+sformat("%1\n", def);
  83.     });
  84.   }
  85.   
  86.   /* Prepare the apsfilter */
  87.   string names_aps = "";
  88.   string sep = "";
  89.   foreach(`nam, names, ``{names_aps = names_aps+sep+nam; sep = "|"; });
  90.   
  91.   /* Switches that vary on the type of connection */
  92.   string aps_dest = "";
  93.     
  94.   /* In case of parallel/USB connection */
  95.   if (connection == "parallel" || connection == "usb" || connection == "serial") {
  96.     map|void conn_map = lookup(printer, connection);
  97.     if (conn_map == nil) {
  98.       UI(`DisplayMessage(sformat(not_properly, connection)));
  99.       return false;
  100.     }
  101.     string|void device_nam = lookup(conn_map, "device");
  102.     if (device_nam == nil) {
  103.       UI(`DisplayMessage(sformat(not_properly, "device")));
  104.       return false;
  105.     }
  106.     
  107.     aps_dest = aps_dest+sformat(" -d %1", device_nam);
  108.   }
  109.  
  110.   string aps_op = "-A";
  111.   if (aps_operation == "delete") aps_op = "-D";
  112.   if (aps_operation == "update") aps_op = "-O";
  113.  
  114.   if (aps_operation != "delete") {
  115.     /* Save the filter settings*/
  116.     WriteString(upp_dir+upp_file_name, upp_file);
  117.     Shell(sformat("chmod 644 %1", upp_dir+upp_file_name));
  118.   
  119.     /* Save the current configuration */
  120.     Write(upp_dir+y2_file_name, printer);
  121.   }
  122.  
  123.   /* Call apsfilter.setup */
  124.   string aps_command = sformat("/var/lib/apsfilter/apsfilter.setup %1 -upp %2 -N %3",
  125.       aps_op, upp_file_name, names_aps);
  126.   integer suc = Shell(aps_command + aps_dest);
  127.  
  128.   if (aps_operation == "delete") {
  129.     Shell(sformat("rm %1", upp_dir+upp_file_name));
  130.     Shell(sformat("rm %1", upp_dir+y2_file_name));
  131.   }
  132.  
  133.   if (suc != 0 || Shell("touch /etc/printcap") != 0) {
  134.     UI(`DisplayMessage(sformat(UI(_("Call of apsfilter.setup (%1%2) was not succesful.")),
  135.       aps_command, aps_dest)));
  136.     return false;
  137.   }
  138.   
  139.   return true;
  140. }
  141.