package printerdrake;
# $Id$
use diagnostics;
use strict;
use common;
use detect_devices;
use commands;
use modules;
use network;
use log;
use printer;
1;
sub choose_printer_type {
my ($printer, $in) = @_;
$in->set_help('configurePrinterConnected') if $::isInstall;
my $queue = $printer->{OLD_QUEUE};
$printer->{str_type} = $printer::printer_type_inv{$printer->{TYPE}};
$printer->{str_type} =
$in->ask_from_list_(_("Select Printer Connection"),
_("How is the printer connected?") .
($printer->{SPOOLER} eq "cups" ?
_("
Printers on remote CUPS servers you do not have to configure
here; these printers will be automatically detected. Please
select \"Printer on remote CUPS server\" in this case.") : ()),
[ printer::printer_type($printer) ],
$printer->{str_type},
) or return 0;
$printer->{TYPE} = $printer::printer_type{$printer->{str_type}};
1;
}
sub setup_remote_cups_server {
my ($printer, $in) = @_;
$in->set_help('configureRemoteCUPSServer') if $::isInstall;
my $queue = $printer->{OLD_QUEUE};
#- hack to handle cups remote server printing,
#- first read /etc/cups/cupsd.conf for variable BrowsePoll address:port
my ($server, $port, $default, $autoconf);
# Return value: 0 when nothing was changed ("Apply" never pressed), 1
# when "Apply" was at least pressed once.
my $retvalue = 0;
while (1) {
# Read CUPS config file
my @cupsd_conf = printer::read_cupsd_conf();
foreach (@cupsd_conf) {
/^\s*BrowsePoll\s+(\S+)/ and $server = $1, last;
}
$server =~ /([^:]*):(.*)/ and ($server, $port) = ($1, $2);
# Read printer list
my @queuelist = printer::read_cups_printer_list();
if ($#queuelist >=0) {
if ($printer->{DEFAULT} eq '') {
$default = printer::get_default_printer($printer);
if ($default) {
# If a CUPS system has only remote printers and no default
# printer defined, it defines the first printer whose
# broadcast signal appeared after the start of the CUPS
# daemon, so on every start another printer gets the
# default printer. To avoid this, make sure that the
# default printer is defined.
$printer->{DEFAULT} = $default;
printer::set_default_printer($printer);
}
}
my $queue;
for $queue (@queuelist) {
if ($queue =~ /^\s*$default/) {
$default = $queue;
}
}
} else {
push(@queuelist, "None");
$default = "None";
}
#- Did we have automatic or manual configuration mode for CUPS
$autoconf = printer::get_cups_autoconf();
#- Remember the server/port/autoconf settings to check whether the user
#- has changed them.
my $oldserver = $server;
my $oldport = $port;
my $oldautoconf = $autoconf;
#- then ask user for this combination and rewrite /etc/cups/cupsd.conf
#- according to new settings. There are no other point where such
#- information is written in this file.
if ($in->ask_from_
({ title => _("Remote CUPS server"),
messages => _("With remote CUPS servers, you do not have to configure any
printer here; CUPS servers inform your machine automatically
about their printers. All printers known to your machine
currently are listed in the \"Default printer\" field. Choose
the default printer for your machine there and click the
\"Apply/Re-read printers\" button. Click the same button to
refresh the list (it can take up to 30 seconds after the start
of CUPS until all remote printers are visible).
When your CUPS server is in a different network, you have to
give the CUPS server IP address and optionally the port number
to get the printer information from the server, otherwise leave
these fields blank.") .
($::expert ? _("
Normally, CUPS is automatically configured according to your
network environment, so that you can access the printers on the
CUPS servers in your local network. If this does not correctly,
turn off \"Automatic CUPS configuration\" and edit your file
/etc/cups/cupsd.conf manually. Do not forget to restart CUPS
afterwards (command: \"service cups restart\").") : ()),
cancel => _("Close"),
ok => _("Apply/Re-read printers"),
callbacks => { complete => sub {
unless (!$server || network::is_ip($server)) {
$in->ask_warn('',
_("The IP address should look like 192.168.1.20"));
return (1,0);
}
if ($port !~ /^\d*$/) {
$in->ask_warn('',
_("The port number should be an integer!"));
return (1,1);
}
return 0;
} }
},
[
{ label => _("Default printer"), val => \$default,
not_edit => 0, list => \@queuelist},
#{ label => _("Default printer") },
#{ val => \$default,
# format => \&translate, not_edit => 0, list => \@queuelist},
{ label => _("CUPS server IP"), val => \$server },
{ label => _("Port"), val => \$port },
($::expert ?
{ text => _("Automatic CUPS configuration"), type => 'bool',
val => \$autoconf } : ()),
]
)) {
# We have clicked "Apply/Re-read"
$retvalue = 1;
# Set default printer
if ($default =~ /^\s*([^\s\(\)]+)\s*\(/) {
$default = $1;
}
if ($default ne "None") {
$printer->{DEFAULT} = $default;
printer::set_default_printer($printer);
}
# Set BrowsePoll line
if (($server ne $oldserver) || ($port ne $oldport)) {
$server && $port and $server = "$server:$port";
if ($server) {
@cupsd_conf =
map { $server and
s/^\s*BrowsePoll\s+(\S+)/BrowsePoll $server/ and
$server = '';
$_ } @cupsd_conf;
$server and push @cupsd_conf, "\nBrowsePoll $server\n";
} else {
@cupsd_conf =
map { s/^\s*BrowsePoll\s+(\S+)/\#BrowsePoll $1/;
$_ } @cupsd_conf;
}
printer::write_cupsd_conf(@cupsd_conf);
sleep 3;
}
# Set auto-configuration state
if ($autoconf != $oldautoconf) {
printer::set_cups_autoconf($autoconf);
}
} else {
last;
}
}
# Save user settings for auto-install
$printer->{BROWSEPOLLADDR} = $server;
$printer->{BROWSEPOLLPORT} = $port;
$printer->{MANUALCUPSCONFIG} = 1 - $autoconf;
return $retvalue;
}
sub setup_printer_connection {
my ($printer, $in) = @_;
# Choose the appropriate connection config dialog
my $done = 1;
for ($printer->{TYPE}) {
/LOCAL/ and setup_local ($printer, $in) and last;
/LPD/ and setup_lpd ($printer, $in) and last;
/SOCKET/ and setup_socket ($printer, $in) and last;
/SMB/ and setup_smb ($printer, $in) and last;
/NCP/ and setup_ncp ($printer, $in) and last;
/URI/ and setup_uri ($printer, $in) and last;
/POSTPIPE/ and setup_postpipe ($printer, $in) and last;
$done = 0; last;
}
return $done;
}
sub auto_detect {
my ($in) = @_;
{
my $w = $in->wait_message(_("Test ports"), _("Detecting devices ..."));
modules::get_alias("usb-interface") and eval { modules::load("printer"); sleep(2); };
foreach (qw(parport_pc lp parport_probe parport)) {
eval { modules::unload($_); }; #- on kernel 2.4 parport has to be unloaded to probe again
}
foreach (qw(parport_pc lp parport_probe)) {
eval { modules::load($_); }; #- take care as not available on 2.4 kernel (silent error).
}
}
my $b = before_leaving { eval { modules::unload("parport_probe") } };
detect_devices::whatPrinter();
}
sub setup_local {
my ($printer, $in) = @_;
my (@port, @str, $device);
my $queue = $printer->{OLD_QUEUE};
my @parport = auto_detect($in);
# $printer->{currentqueue}{queuedata}
foreach (@parport) {
$_->{val}{DESCRIPTION} and push @str, _("A printer, model \"%s\", has been detected on ",
$_->{val}{DESCRIPTION}) . $_->{port};
}
if ($::expert || !@str) {
@port = detect_devices::whatPrinterPort();
|