home *** CD-ROM | disk | FTP | other *** search
- /* The Printer Configurator
- * Module for writing a printer record to the spooling system
- * Jan Holesovsky <kendy@suse.cz>, 2000
- *
- * $Id: printconf_write_printer.ycp,v 1.3 2000/02/22 08:57:57 kendy Exp $
- */
-
- /* Arguments:
- 1st: (map) printer to be written
- 2nd: (string) "add", "delete", "update"
-
- Returns:
- true everything was successful
- false otherwise
- */
-
- {
- map printer = Args(0);
- string aps_operation = Args(1);
-
- string upp_dir = "/etc/gs.upp/";
- string upp_file = ""; /* This will be written... */
-
- /* May I add this printer? */
- boolean|void add_it = lookup(printer, "add");
- if (add_it != true)
- return false;
-
- if (!Mkdir(upp_dir)) {
- UI(`DisplayMessage(sformat(UI(_("Can't create directory '%1'!")), upp_dir)));
- return false;
- }
-
- string not_properly = UI(_("ERROR: Record '%1' is
- not properly configured."));
-
- list|void names = lookup(printer, "names");
- if (names == nil) {
- UI(`DisplayMessage(sformat(not_properly, "names")));
- return false;
- }
-
- string filename = "y2prn";
- foreach(`nam, names, ``{ filename = filename+"_"+nam; });
-
- string upp_file_name = filename+".upp";
- string y2_file_name = filename+".y2";
-
- string|void manufacturer = lookup(printer, "manufacturer");
- if (manufacturer == nil) {
- UI(`DisplayMessage(sformat(not_properly, "manufacturer")));
- return false;
- }
- string|void model = lookup(printer, "model");
- if (model == nil) {
- UI(`DisplayMessage(sformat(not_properly, "model")));
- return false;
- }
- string|void connection = lookup(printer, "connection");
- if (connection == nil) {
- UI(`DisplayMessage(sformat(not_properly, "connection")));
- return false;
- }
-
- /* Model of the printer (and information for administration) */
- upp_file = sformat("-supModel=\"%1 %2\"\n", manufacturer, model);
-
- map|void ps2printer = lookup(printer, "ps2printer");
- if (ps2printer == nil) {
- UI(`DisplayMessage(sformat(not_properly, "ps2printer")));
- return false;
- }
-
- list|void params = lookup(ps2printer, "params");
- list|void options = lookup(ps2printer, "options");
- if (params != nil)
- foreach(`par, params, ``{ upp_file = upp_file+sformat("%1\n", par); });
- if (options != nil) {
- foreach(`opt, options, ``{
- string|void def = lookup(opt, "default");
- if (def != nil)
- upp_file = upp_file+sformat("%1\n", def);
- });
- }
-
- /* Prepare the apsfilter */
- string names_aps = "";
- string sep = "";
- foreach(`nam, names, ``{names_aps = names_aps+sep+nam; sep = "|"; });
-
- /* Switches that vary on the type of connection */
- string aps_dest = "";
-
- /* In case of parallel/USB connection */
- if (connection == "parallel" || connection == "usb" || connection == "serial") {
- map|void conn_map = lookup(printer, connection);
- if (conn_map == nil) {
- UI(`DisplayMessage(sformat(not_properly, connection)));
- return false;
- }
- string|void device_nam = lookup(conn_map, "device");
- if (device_nam == nil) {
- UI(`DisplayMessage(sformat(not_properly, "device")));
- return false;
- }
-
- aps_dest = aps_dest+sformat(" -d %1", device_nam);
- }
-
- string aps_op = "-A";
- if (aps_operation == "delete") aps_op = "-D";
- if (aps_operation == "update") aps_op = "-O";
-
- if (aps_operation != "delete") {
- /* Save the filter settings*/
- WriteString(upp_dir+upp_file_name, upp_file);
- Shell(sformat("chmod 644 %1", upp_dir+upp_file_name));
-
- /* Save the current configuration */
- Write(upp_dir+y2_file_name, printer);
- }
-
- /* Call apsfilter.setup */
- string aps_command = sformat("/var/lib/apsfilter/apsfilter.setup %1 -upp %2 -N %3",
- aps_op, upp_file_name, names_aps);
- integer suc = Shell(aps_command + aps_dest);
-
- if (aps_operation == "delete") {
- Shell(sformat("rm %1", upp_dir+upp_file_name));
- Shell(sformat("rm %1", upp_dir+y2_file_name));
- }
-
- if (suc != 0 || Shell("touch /etc/printcap") != 0) {
- UI(`DisplayMessage(sformat(UI(_("Call of apsfilter.setup (%1%2) was not succesful.")),
- aps_command, aps_dest)));
- return false;
- }
-
- return true;
- }
-