home *** CD-ROM | disk | FTP | other *** search
- /* The Printer Configurator
- * Jan Holesovsky <kendy@suse.cz>, 2000
- *
- * $Id: printconf.ycp,v 1.28.4.2 2000/03/23 16:22:10 gs Exp $
- */
-
- {
- // Menuentry for the YaST2 menu
- if (Args() == [ "get_menuentry" ])
- return [
- "printconf", $[
- `menuentry : UI(_("Hardware/Printer")),
- `arguments : [ ],
- `widget : `RichText(
- UI(_("<P>Launch this module to configure your printer. Please, turn
- the printer on, YaST2 will try to autodetect it.</P>
- <P>You need to be logged in as <i>root</i> in order to
- do this.</P>"))),
- `codefragment : nil ]
- ];
-
-
- UI(``{
- // Debug dialog
- define DebugDialog(string str) ``{
- OpenDialog(//`opt(`decorated),
- `VBox(`RichText(str),
- `PushButton(_("&OK"))));
- any r = UserInput();
- CloseDialog();
- };
-
- // The main dialog. Copied from installation.ycp.
- OpenDialog(`opt(`defaultsize),
- `VBox(`Image(`suseheader, "SuSE"),
- `HBox(`HWeight(30, `RichText(`id(`help), "")),
- `HWeight(70, `VBox(
- `Left(`Heading(`id(`title), _("YaST2 Printconf\n
- Initializing ...
- "))),
- `HVCenter(`ReplacePoint(`id(`contents), `Empty())),
- `HBox(
- // back pushbutton: the user input is ignored and the last dialog is called
- `PushButton(`id(`back), `opt(`disabled), _("&Back")),
- `HStretch(),
- // next pushbutton: the user input is checked and the next dialog is called
- `PushButton(`id(`next), `opt(`disabled), _("&Next"))))))));
-
- // Create content of the dialog. See installation.ycp.
- define SetPrintconfContents(string title, term contents, string helptext,
- boolean back_is_return, string next_label) ``{
- /*if (back_is_return)
- // "Return" is on the first screen of the installation.
- // It means that the printconf will be left without changes.
- ChangeWidget(`id(`back), `Label, _("&Return"));
- else
- ChangeWidget(`id(`back), `Label, _("&Back"));*/
-
- ChangeWidget(`id(`next), `Label, next_label);
- ChangeWidget(`id(`back), `Enabled, true);
- ChangeWidget(`id(`next), `Enabled, true);
-
- ChangeWidget(`id(`help), `Value, helptext);
- ChangeWidget(`id(`title), `Value, title);
- ReplaceWidget(`id(`contents), contents);
- };
-
- /*
- * Displays a message to the user in a small dialog with an OK
- * button.
- */
-
- define DisplayMessage(string message) ``{
- // ok pushbutton: confirm the dialog
- OpenDialog(`opt(`decorated), `VBox(`Label(message),
- `PushButton(_("&OK"))));
- UserInput();
- CloseDialog();
- };
-
-
- // Define dialog to let the user choose the connection type.
- define SelectConnection(map dial_noncommon, map known, boolean is_first) ``{
- list connection_val = [];
- foreach(`type, `info, dial_noncommon, ``{
- connection_val = add(connection_val, `item(`id(type), lookup(info, "name")));
- });
- term connection_sel = `SelectionBox(`id(`connectionsel),
- // Do not do the translation longer than this text.
- // It is a label of a selection box.
- _("Select the printer type you want to add:"), connection_val);
-
- list alreadyconf_val = [];
- foreach(`key, `pr, known, ``{
- if (pr != nil) {
- boolean|void to_add = lookup(pr, "add");
- if ((to_add != nil) && to_add) {
- list names = lookup(pr, "names");
- string pr_con = lookup(pr, "connection");
- string con_name = lookup(lookup(dial_noncommon, pr_con), "name");
- string name = "";
-
- foreach(`n, names, ``{
- if (size(name) > 0)
- name = name+", ";
- name = name+n;
- });
- alreadyconf_val = add(alreadyconf_val,
- sformat(_("%1 (%2)"), name, con_name));
- }
- }
- });
-
- // Help, 1 of 3
- string help_text = _("<P>In this dialog, you can manually add printers to your spooling system.
- Use this in case, when your printer was not found by YaST2.</P>
- ");
- // Help, 2 of 3
- help_text = help_text + _("<P>The left column contains printers that will be added to
- your system after pressing the <B>Finish</B> button.</P>
- ");
- // Help, 3 of 3
- help_text = help_text + _("<P>In the right column you can choose the type of the printer you have.
- When you push the <B>Add</B> button, the procedure to add a printer will start.</P>
- ");
-
- term contents =
- `HBox(`SelectionBox(`id(`alreadyconf), `opt(`disable),
- // Do not do the translation longer than this text.
- _("Configured printers:"), alreadyconf_val),
- `VBox(connection_sel, `Right(`PushButton(`id(`new_one), _("&Add"))))
- );
- return SetPrintconfContents(_("Choose the printer you have"),
- contents, help_text,
- is_first, _("&Finish")); // TODO: help text.
-
- };
-
- });
-
- /* Chooses one message according to the language */
- define Trans(map|void msgs, string lang) ``{
- string|void message = "";
- if (msgs != nil) {
- message = lookup(msgs, lang);
- if (message == nil)
- message = lookup(msgs, substring(lang, 0, 2));
- if (message == nil)
- message = lookup(msgs, "default");
- if (message == nil)
- message = ""; // Everything has failed.
- }
- return message;
- };
-
- /* Reads and localises the printer database */
- define ReadLocalizedDatabase(string filename) ``{
- list|void dbase = ReadY2(filename);
- if (dbase == nil) {
- UI(`DisplayMessage(_("FATAL: I am unable to find the printer database!")));
- return nil;
- }
- string language = UI(`GetLanguage());
-
- return maplist(`manuf, dbase, ``{
- list|void printers = lookup(manuf, "printers");
- if (printers != nil) {
- manuf = add(manuf, "printers", maplist(`pr, printers, ``{
- map|void ps2printer = lookup(pr, "ps2printer");
- if (ps2printer != nil) {
- list|void options = lookup(ps2printer, "options");
- if (options != nil) {
- ps2printer = add(ps2printer, "options", maplist(`opt, options, ``{
- map|void values = lookup(opt, "values");
- if (values != nil) {
- map val = $[];
- foreach(`k, `v, values, ``{
- val = add(val, k, Trans(v, language));
- });
- opt = add(opt, "values", val); // tr. values
- }
- return add(opt, "name", Trans(lookup(opt, "name"), language));// tr. opts
- }));
- }
- pr = add(pr, "ps2printer", ps2printer);
- }
- return add(pr, "name", Trans(lookup(pr, "name"), language)); // tr. printers
- }));
- }
- return add(manuf, "name", Trans(lookup(manuf, "name"), language)); // tr. manufact
- });
- };
-
- /* Returns manufacturer according to 1284 id */
- define FindManufacturer1284(list database, string id) ``{
- id = tolower(id);
- list man = filter(`mnf, database, ``{
- string|void mf = lookup(mnf, "1284id");
- return (mf != nil && mf != "" && substring(id, 0, size(mf)) == tolower(mf));
- });
- if (size(man) == 0)
- return nil;
- else
- return select(man, 0);
- };
-
- /* Returns printer according to 1284 id */
- define FindPrinter1284(map manufacturer, string id) ``{
- id = tolower(id);
- list prntrs = lookup(manufacturer, "printers");
- list pr = filter(`prns, prntrs, ``{
- string|void p = lookup(prns, "1284id");
- return (p != nil && p != "" && substring(id, 0, size(p)) == tolower(p));
- });
- if (size(pr) == 0)
- return nil;
- else
- return select(pr, 0);
- };
-
- /* Move printer records from list "to_add" to indexed map "known" */
- define AddPrinters(map known, list to_add) ``{
- integer idx = size(known);
- foreach(`prn, to_add, ``{
- known = add(known, idx, prn);
- idx = idx+1;
- });
- return known;
- };
-
- /* Autoprobes parallel printers.
- Returns a list of autoprobe records */
- define AutoprobeDevice(list pr_database) ``{
- Shell("/sbin/rmmod parport_probe");
- Shell("/sbin/modprobe parport_probe"); // Must be restarted!
-
- list|void aprobe = SCR(`Read(.probe.byclass.printer));
- if (aprobe == nil) {
- UI(`DisplayMessage(_("I tried to autoprobe parallel and USB printers,\n
- but I haven't found any on your computer.
- ")));
- return [];
- }
-
- return maplist(`pr, aprobe, ``{
- map record = $[];
- string bus = lookup(pr, "bus");
- string|void man1284 = nil;
- string|void mod1284 = nil;
- if (bus == "Parallel") {
- record = $[
- "autodetected" : true, // This record was found by printconf
- "connection" : "parallel", // on parallel device
- "parallel": $[ "device": lookup(pr, "dev_name") ] // device <dev>
- ];
- man1284 = lookup(pr, "vendor");
- mod1284 = lookup(pr, "device");
- }
- if (bus == "USB") {
- record = $[
- "autodetected" : true, // This record was found by printconf
- "connection" : "usb", // on usb device
- "usb": $[ "device": lookup(pr, "dev_name") ] // device <dev>
- ];
- man1284 = lookup(pr, "sub_vendor");
- mod1284 = lookup(pr, "sub_device");
- }
-
- if (man1284 != nil) {
- map|void manuf = FindManufacturer1284(pr_database, man1284);
- if (manuf != nil) {
- record = add(record, "manufacturer", lookup(manuf, "name"));
- if (mod1284 != nil) {
- map|void model = FindPrinter1284(manuf, mod1284);
- if (model != nil) {
- record = add(add(record, "model", lookup(model, "name")),
- "ps2printer", lookup(model, "ps2printer"));
- }
- }
- }
- }
- return record;
- });
- };
-
- /* Tries to find the highest lp number */
- define GetHighestLpNumber(list printcap) ``{
- integer high = 0;
- foreach(`rec, printcap, ``{
- list|void names = lookup(rec, "names");
- if (names != nil)
- foreach(`nam, names, ``{
- if (substring(nam, 0, 2) == "lp") {
- string num = substring(nam, 2, size(nam)-2);
- if (num != "" && findfirstnotof(num, "0123456789") == nil) {
- integer in = tointeger(num);
- if (in > high)
- high = in;
- }
- }
- });
- });
- return high;
- };
-
- /* Get a list of already used names. For a single list. */
- define GetForbiddenNames(list printconf) ``{
- list forb = maplist(`rec, printconf, ``lookup(rec, "names"));
- forb = filter(`nam, forb, ``(nam != nil));
- return flatten(forb);
- };
-
- /* Get a list of already used names. For a single list. */
- define GetForbiddenNames(integer current, map known_printers) ``{
- list forb = maplist(`idx, `pr, known_printers, ``{
- if (idx != current)
- return lookup(pr, "names");
- else
- return nil;
- });
- forb = filter(`nam, forb, ``(nam != nil));
- return flatten(forb);
- };
-
- /* Show dialogs for some type of connection
- * first: Am I the first dialog? */
- define ShowDialogs(map printer, map dialogs, boolean first, boolean reverse) ``{
- integer max = size(dialogs);
- boolean|void add_it = lookup(printer, "add");
- integer index = 0;
- if (reverse && (add_it != false)) index = max-1;
-
- boolean reverse_call = reverse;
- while ((index >= 0) && (index < max)) {
- term dlg = lookup(dialogs, index);
-
- dlg = add(dlg, printer); // Add parameters.
- dlg = add(dlg, (index == 0) && first);
- dlg = add(dlg, reverse_call);
-
- map|void ret = CallFunction(dlg);
-
- if (ret != nil) {
- printer = ret;
- boolean|void add_it = lookup(printer, "add");
- if (add_it == false) break;
- index = index+1;
- reverse_call = false;
- } else {
- index = index-1;
- reverse_call = true;
- }
- }
-
- if (index < 0) return nil;
- return printer;
- };
-
- /* Initialization */
- list|void printer_database = ReadLocalizedDatabase("printers.ycp");
- if (printer_database == nil) {
- UI(`CloseDialog());
- return;
- }
-
- Shell("/sbin/modprobe lp");
- Shell("/sbin/lsmod | grep usbcore || /sbin/init.d/usb start");
- Shell("/sbin/lsmod | grep printer || /sbin/modprobe printer");
-
- integer lpdaemon = Shell("/sbin/init.d/lpd restart");
- if (lpdaemon != 0) {
- UI(`DisplayMessage(_("FATAL: I am unable to restart the lpd daemon!\n
- Maybe, you haven't installed lpd (one of\n
- the lprold, plp or lprng packages) or\n
- you are not the user 'root'.
- ")));
- UI(`CloseDialog());
- return;
- }
-
- if (Shell("which gs") != 0)
- UI(`DisplayMessage(_("It seems that you don't have GhostScript\n
- installed on your computer. It is necessary \n
- in order to print.
- ")));
-
- boolean y2_save = false;
- integer y2_save_call =
- Shell("grep apsfilter /etc/printcap || ( mv /etc/printcap /etc/printcap.YaST2save && exit 1)");
- if (y2_save_call == 1)
- y2_save = true;
-
- list|void printcap = SCR(`Read(.etc.printcap));
- /* apsfilter.setup generates lp? names. This is the highest one. */
- integer highest_lp_no = 0;
- list printcap_forb_names = [];
- if (printcap != nil) {
- highest_lp_no = GetHighestLpNumber(printcap);
- printcap_forb_names = GetForbiddenNames(printcap);
- }
-
- map dial_noncommon = $[
- "parallel": $[ "name" : _("Parallel printer"),
- "default" : `printconf_ask_device("parallel"),
- "autodetected" : `printconf_device_confirm() ],
- "usb": $[ "name" : _("USB printer"),
- "default" : `printconf_ask_device("usb"),
- "autodetected" : `printconf_device_confirm() ]
- ];
-
- map known_printers = $[];
- /* The records should be:
- <num>:$[ "autodetected":true/false, -- added by user or by printconf?
- "add":true/false, -- add this to the spooling system?
- "manufacturer":<man>, "model":<mod>,
- "names": [ <name1>, <name2>, ...],
- "connection":<con>, <con>:$[<connection related>]]
- The unknown information should not be filled in. The user will be asked.*/
-
- known_printers = AddPrinters(known_printers, AutoprobeDevice(printer_database));
- /* TODO: more detection here (NCP, Samba, ...)*/
-
- integer index = 0;
- boolean reverse = false;
- /* Go through the known_printers map and ask for missing information. */
- do {
- any ret_value = nil;
- map|void printer = lookup(known_printers, index);
-
- if (printer == nil) { // The user must be asked...
- any dlg = UI(`SelectConnection(dial_noncommon, known_printers, index <= 0));
- string|void new_conn = nil; // User wants to add this type.
- do {
- ret_value = UI(`UserInput());
- new_conn = UI(`QueryWidget(`id(`connectionsel), `CurrentItem));
- if ((ret_value == `new_one) && (new_conn == nil))
- UI(`DisplayMessage(_("Please select the type of printer you want to add.")));
- } while ((ret_value == `new_one) && (new_conn == nil));
-
- if (ret_value == `new_one) // The user wants one more printer!
- known_printers = add(known_printers, index, // Add empty one!
- $[ "autodetected":false, "add":true, "connection":new_conn ]);
- else
- if (ret_value == `back) index = index-1;
- reverse = (ret_value == `back);
- } else { // printer != nil, configure it.
- string type = lookup(printer, "connection");
- boolean|void autodet = lookup(printer, "autodetected");
-
- map|void configured = nil;
- term|void non_common = nil;
- if (autodet == true)
- non_common = lookup(lookup(dial_noncommon, type), "autodetected");
- else
- non_common = lookup(lookup(dial_noncommon, type), "default");
-
- list forbidden_names = union(printcap_forb_names,
- GetForbiddenNames(index, known_printers));
-
- if (non_common != nil) {
- map dialogs = $[ // Dialogs for configuring...
- 1: `printconf_manuf_model(printer_database),
- 2: `printconf_spool_info(forbidden_names),
- 3: `printconf_filter()
- ];
- dialogs = add(dialogs, 0, non_common);
-
- configured =
- ShowDialogs(printer, dialogs, (index == 0) && (autodet == true), reverse);
- } else
- UI(`DisplayMessage(
- sformat("I am sorry, module for configuring %1 printers is not implemented yet.", type)));
-
- if (configured == nil) {
- boolean|void auto = lookup(printer, "autodetected");
- if (auto != true) // If the printer was added by user
- known_printers = add(known_printers, index, nil); // remove it,
- else
- index = index-1; // else go back.
- } else {
- known_printers = add(known_printers, index, configured); // New configuration
- index = index+1;
- }
- reverse = (configured == nil);
- }
- } while ((ret_value != `next) && (index >= 0));
-
- if (index < 0) {
- UI(`DisplayMessage(_("There were no changes in \n
- the configuration of printers.
- ")));
- if (y2_save == true)
- Shell("mv /etc/printcap.YaST2save /etc/printcap");
- } else {
- integer i = 0;
- boolean something_added = false;
- while (i < size(known_printers)) {
- map|void printer = lookup(known_printers, i);
- if ((printer != nil) && (lookup(printer, "add") == true)) {
- something_added = true;
- if (lookup(printer, "connection") == "usb") {
- Shell("cd /etc/rc.config.d/ ; \
- . usb.rc.config ; \
- echo $USB_DRIVERS | \
- grep printer || \
- ( sed 's/^USB_DRIVERS=/#USB_DRIVERS=/' < usb.rc.config \
- > usb.rc.config.new ; \
- echo \"USB_DRIVERS=\\\"$USB_DRIVERS printer\\\"\" >> usb.rc.config.new ; \
- mv usb.rc.config.new usb.rc.config )");
- Shell("cat /etc/rc.config | \
- sed 's/START_USB=\"[^\"]*\"/START_USB=\"yes\"/' > /etc/rc.config.new ; \
- mv /etc/rc.config.new /etc/rc.config");
- Shell("SuSEconfig -quick --nonewpackage");
- }
-
- /* TODO: Do better dialogs! */
- if (CallFunction(`printconf_write_printer(printer, "add")))
- // %1 - name of the printer
- UI(`DisplayMessage(sformat(UI(_("Printer '%1' was\n
- successfully configured.
- ")),
- select(lookup(printer, "names"), 0))));
- else
- // %1 - name of the printer
- UI(`DisplayMessage(sformat(UI(_("I was unable to configure\n
- printer '%1'.
-
- ")),
- select(lookup(printer, "names"), 0))));
- }
- i = i+1;
- }
- if (y2_save == true && !something_added)
- Shell("mv /etc/printcap.YaST2save /etc/printcap");
- }
- UI(`CloseDialog());
-
- }
-