summaryrefslogtreecommitdiffstats
path: root/perl-install/printer.pm
diff options
context:
space:
mode:
authorTill Kamppeter <tkamppeter@mandriva.com>2002-08-28 23:38:37 +0000
committerTill Kamppeter <tkamppeter@mandriva.com>2002-08-28 23:38:37 +0000
commit8b3d1554c55a3ef28442461c2f3fb6ec63f5f028 (patch)
tree54e8978e0388d24e631a99797ac872c66502678b /perl-install/printer.pm
parenta70ec0d372ee16efaa038ec0ce5b09331f3d8b3c (diff)
downloaddrakx-8b3d1554c55a3ef28442461c2f3fb6ec63f5f028.tar
drakx-8b3d1554c55a3ef28442461c2f3fb6ec63f5f028.tar.gz
drakx-8b3d1554c55a3ef28442461c2f3fb6ec63f5f028.tar.bz2
drakx-8b3d1554c55a3ef28442461c2f3fb6ec63f5f028.tar.xz
drakx-8b3d1554c55a3ef28442461c2f3fb6ec63f5f028.zip
Added automatical configuration of the printers in the GIMP.
Diffstat (limited to 'perl-install/printer.pm')
-rw-r--r--perl-install/printer.pm413
1 files changed, 410 insertions, 3 deletions
diff --git a/perl-install/printer.pm b/perl-install/printer.pm
index 145dac96a..3cde49185 100644
--- a/perl-install/printer.pm
+++ b/perl-install/printer.pm
@@ -2184,7 +2184,7 @@ RIGHTDRIVE=\" \"
# ------------------------------------------------------------------
#
-# Configuration of printers in Applications
+# Configuration of printers in applications
#
# ------------------------------------------------------------------
@@ -2192,26 +2192,34 @@ sub configureapplications {
my ($printer) = @_;
configurestaroffice($printer);
configureopenoffice($printer);
+ configuregimp($printer);
}
sub addcupsremotetoapplications {
my ($printer, $queue) = @_;
return (addcupsremotetostaroffice($printer, $queue) &&
- addcupsremotetoopenoffice($printer, $queue));
+ addcupsremotetoopenoffice($printer, $queue) &&
+ addcupsremotetogimp($printer, $queue));
}
sub removeprinterfromapplications {
my ($printer, $queue) = @_;
return (removeprinterfromstaroffice($printer, $queue) &&
- removeprinterfromopenoffice($printer, $queue));
+ removeprinterfromopenoffice($printer, $queue) &&
+ removeprinterfromgimp($printer, $queue));
}
sub removelocalprintersfromapplications {
my ($printer) = @_;
removelocalprintersfromstaroffice($printer);
removelocalprintersfromopenoffice($printer);
+ removelocalprintersfromgimp($printer);
}
+# ------------------------------------------------------------------
+# Star Offica/OpenOffice.org
+# ------------------------------------------------------------------
+
sub configurestaroffice {
my ($printer) = @_;
# Do we have Star Office installed?
@@ -2842,4 +2850,403 @@ sub removesection {
return join ("", @lines);
}
+# ------------------------------------------------------------------
+# GIMP
+# ------------------------------------------------------------------
+
+sub configuregimp {
+ my ($printer, $queue) = @_;
+ # Do we have files to treat?
+ my @configfilenames = findgimpconfigfiles();
+ return 1 if $#configfilenames < 0;
+ # There is no system-wide config file, treat every user's config file
+ for my $configfilename (@configfilenames) {
+ # Load GIMP's printer config file
+ my $configfilecontent = readgimpconfigfile($configfilename);
+ # Update local printer queues
+ for my $queue (keys(%{$printer->{configured}})) {
+ # Check if we have a PPD file
+ if (! -r "$prefix/etc/foomatic/$queue.ppd") {
+ if (-r "$prefix/etc/cups/ppd/$queue.ppd") {
+ # If we have a PPD file in the CUPS config dir, link to it
+ run_program::rooted($prefix,
+ "ln", "-sf",
+ "/etc/cups/ppd/$queue.ppd",
+ "/etc/foomatic/$queue.ppd");
+ } elsif (-r "$prefix/usr/share/postscript/ppd/$queue.ppd") {
+ # Check PPD directory of GPR, too
+ run_program::rooted
+ ($prefix,
+ "ln", "-sf",
+ "/usr/share/postscript/ppd/$queue.ppd",
+ "/etc/foomatic/$queue.ppd");
+ } else {
+ # No PPD file at all? We cannot set up this printer
+ next;
+ }
+ }
+ # Add the printer entry
+ if (!isgimpprinterconfigured ($queue, $configfilecontent)) {
+ # Remove the old printer entry
+ $configfilecontent =
+ removegimpprinter($queue, $configfilecontent);
+ # Add the new printer entry
+ $configfilecontent =
+ makegimpprinterentry($printer, $queue,
+ $configfilecontent);
+ }
+ }
+ # Default printer
+ if ($configfilecontent !~ /^\s*Current\-Printer\s*:/m) {
+ $configfilecontent =~
+ s/\n/\nCurrent-Printer: $printer->{DEFAULT}\n/s;
+ } else {
+ $configfilecontent =~ /^\s*Current\-Printer\s*:\s*(\S+)\s*$/m;
+ if (!isgimpprinterconfigured ($1, $configfilecontent)) {
+ $configfilecontent =~
+ s/(Current\-Printer\s*:\s*)\S+/$1$printer->{DEFAULT}/;
+ }
+ }
+ # Write back GIMP's printer configuration file
+ writegimpconfigfile($configfilename, $configfilecontent);
+ }
+ return 1;
+}
+
+sub addcupsremotetogimp {
+ my ($printer, $queue) = @_;
+ # Do we have files to treat?
+ my @configfilenames = findgimpconfigfiles();
+ return 1 if $#configfilenames < 0;
+ my @printerlist = getcupsremotequeues();
+ my $ppdfile = "";
+ if (($printer->{SPOOLER} eq "cups") &&
+ (-x "$prefix/usr/bin/curl")) {
+ for my $listentry (@printerlist) {
+ next if !($listentry =~ /^([^\|]+)\|([^\|]+)$/);
+ my $q = $1;
+ next if ($q ne $queue);
+ my $server = $2;
+ # Remove server name from queue name
+ $q =~ s/^([^@]*)@.*$/$1/;
+ eval(run_program::rooted
+ ($prefix, "/usr/bin/curl", "-o",
+ "/etc/foomatic/$queue.ppd",
+ "http://$server:631/printers/$q.ppd"));
+ # Does the file exist and is it not an error message?
+ if ((-r "$prefix/etc/foomatic/$queue.ppd") &&
+ (cat_("$prefix/etc/foomatic/$queue.ppd") =~
+ /^\*PPD-Adobe/)) {
+ $ppdfile = "/etc/foomatic/$queue.ppd";
+ } else {
+ unlink ("$prefix/etc/foomatic/$queue.ppd");
+ }
+ }
+ } else {
+ return 1;
+ }
+ # There is no system-wide config file, treat every user's config file
+ for my $configfilename (@configfilenames) {
+ # Load GIMP's printer config file
+ my $configfilecontent = readsofficeconfigfile($configfilename);
+ # Add the printer entry
+ if (!isgimpprinterconfigured ($queue, $configfilecontent)) {
+ # Remove the old printer entry
+ $configfilecontent =
+ removegimpprinter($queue, $configfilecontent);
+ # Add the new printer entry
+ $configfilecontent =
+ makegimpprinterentry($printer, $queue,
+ $configfilecontent);
+ }
+ # Write back GIMP's printer configuration file
+ writegimpconfigfile($configfilename, $configfilecontent);
+ }
+ return 1;
+}
+
+sub removeprinterfromgimp {
+ my ($printer, $queue) = @_;
+ # Do we have files to treat?
+ my @configfilenames = findgimpconfigfiles();
+ return 1 if $#configfilenames < 0;
+ # There is no system-wide config file, treat every user's config file
+ for my $configfilename (@configfilenames) {
+ # Load GIMP's printer config file
+ my $configfilecontent = readgimpconfigfile($configfilename);
+ # Remove the printer entry
+ $configfilecontent =
+ removegimpprinter($queue, $configfilecontent);
+ # Write back GIMP's printer configuration file
+ writegimpconfigfile($configfilename, $configfilecontent);
+ }
+ return 1;
+}
+
+sub removelocalprintersfromgimp {
+ my ($printer) = @_;
+ # Do we have files to treat?
+ my @configfilenames = findgimpconfigfiles();
+ return 1 if $#configfilenames < 0;
+ # There is no system-wide config file, treat every user's config file
+ for my $configfilename (@configfilenames) {
+ # Load GIMP's printer config file
+ my $configfilecontent = readgimpconfigfile($configfilename);
+ # Remove the printer entries
+ for my $queue (keys(%{$printer->{configured}})) {
+ $configfilecontent =
+ removegimpprinter($queue, $configfilecontent);
+ }
+ # Write back GIMP's printer configuration file
+ writegimpconfigfile($configfilename, $configfilecontent);
+ }
+ return 1;
+}
+
+sub makegimpprinterentry {
+ my ($printer, $queue, $configfile) = @_;
+ # Make printer's section
+ $configfile = addgimpprinter($queue, $configfile);
+ # Load PPD file
+ my $ppd = cat_("$prefix/etc/foomatic/$queue.ppd");
+ # Is the printer configured with GIMP-Print?
+ my $gimpprintqueue = 0;
+ my $gimpprintdriver = "ps2";
+ if ($ppd =~ /CUPS\s*\+\s*GIMP\s*\-\s*Print/im) {
+ # Native CUPS driver
+ $gimpprintqueue = 1;
+ $ppd =~ /\s*\*ModelName:\s*\"(\S+)\"\s*$/im;
+ $gimpprintdriver = $1;
+ } elsif ($ppd =~ /Foomatic\s*\+\s*gimp\s*\-\s*print/im) {
+ # GhostScript + Foomatic driver
+ $gimpprintqueue = 1;
+ $ppd =~
+ /'idx'\s*=>\s*'ev\/gimp-print-((escp2|pcl|bjc|lexmark)\-\S*)'/im;
+ $gimpprintdriver = $1;
+ }
+ if ($gimpprintqueue) {
+ # Get the paper size from the PPD file
+ if ($ppd =~ /^\s*\*DefaultPageSize:\s*(\S+)\s*$/m) {
+ my $papersize = $1;
+ $configfile = removegimpentry($queue,
+ "Media-Size", $configfile);
+ $configfile = addgimpentry($queue,
+ "Media-Size: $papersize", $configfile);
+ }
+ $configfile = removegimpentry($queue,
+ "PPD-File:", $configfile);
+ $configfile = addgimpentry($queue,
+ "PPD-File:", $configfile);
+ $configfile = removegimpentry($queue,
+ "Driver:", $configfile);
+ $configfile = addgimpentry($queue,
+ "Driver: $gimpprintdriver", $configfile);
+ $configfile = removegimpentry($queue,
+ "Destination:", $configfile);
+ $configfile = addgimpentry($queue,
+ "Destination: /usr/bin/$lprcommand{$printer->{SPOOLER}} -P $queue -o raw", $configfile);
+ } else {
+ $configfile = removegimpentry($queue,
+ "PPD-File:", $configfile);
+ $configfile = addgimpentry($queue,
+ "PPD-File: /etc/foomatic/$queue.ppd", $configfile);
+ $configfile = removegimpentry($queue,
+ "Driver:", $configfile);
+ $configfile = addgimpentry($queue,
+ "Driver: ps2", $configfile);
+ $configfile = removegimpentry($queue,
+ "Destination:", $configfile);
+ $configfile = addgimpentry($queue,
+ "Destination: /usr/bin/$lprcommand{$printer->{SPOOLER}} -P $queue", $configfile);
+ }
+ return $configfile;
+}
+
+sub findgimpconfigfiles {
+ my @configfilenames;
+ if (-d "$prefix/usr/lib/gimp/1.2") {
+ push (@configfilenames, ".gimp-1.2/printrc");
+ }
+ if (-d "$prefix/usr/lib/gimp/1.3") {
+ push (@configfilenames, ".gimp-1.3/printrc");
+ }
+ my @filestotreat;
+ local *PASSWD;
+ open PASSWD, "< $prefix/etc/passwd" || die "Cannot read /etc/passwd!\n";
+ while (<PASSWD>) {
+ chomp;
+ if ($_ =~ /^([^:]+):[^:]*:([^:]+):([^:]+):[^:]*:([^:]+):[^:]*$/) {
+ my ($username, $uid, $gid, $homedir) = ($1, $2, $3, $4);
+ if ((($uid == 0) || ($uid >= 500)) && ($username ne "nobody")) {
+ for my $file (@configfilenames) {
+ my $dir = "$homedir/$file";
+ $dir =~ s:/[^/]*$::;
+ next if ((-f $dir) && (! -d $dir));
+ if (! -d $dir) {
+ run_program::rooted($prefix,
+ "/bin/mkdir", $dir)
+ or next;
+ run_program::rooted($prefix,
+ "/bin/chown", "$uid.$gid", $dir)
+ or next;
+ }
+ if (! -f "$homedir/$file") {
+ local *F;
+ open F, "> $prefix$homedir/$file" or next;
+ print F "#PRINTRCv1 written by GIMP-PRINT 4.2.2 - 13 Sep 2002\n";
+ close F;
+ run_program::rooted($prefix,
+ "/bin/chown", "$uid.$gid",
+ "$homedir/$file")
+ or next;
+ }
+ push (@filestotreat, "$homedir/$file");
+ }
+ }
+ }
+ }
+ @filestotreat;
+}
+
+sub readgimpconfigfile {
+ my ($file) = @_;
+ local *F;
+ open F, "< $prefix$file" || return "";
+ my $filecontent = join("", <F>);
+ close F;
+ return $filecontent;
+}
+
+sub writegimpconfigfile {
+ my ($file, $filecontent) = @_;
+ local *F;
+ open F, "> $prefix$file" || return 0;
+ print F $filecontent;
+ close F;
+ return 1;
+}
+
+sub addgimpentry {
+ my ($section, $entry, $filecontent) = @_;
+ my $sectionfound = 0;
+ my $entryinserted = 0;
+ my @lines = split("\n", $filecontent);
+ local $_;
+ for (@lines) {
+ if (!$sectionfound) {
+ if (/^\s*Printer\s*:\s*($section)\s*$/) {
+ $sectionfound = 1;
+ }
+ } else {
+ if (!/^\s*$/ && !/^\s*;/) { #-#
+ $_ = "$entry\n$_";
+ $entryinserted = 1;
+ last;
+ }
+ }
+ }
+ if ($sectionfound && !$entryinserted) {
+ push(@lines, $entry);
+ }
+ return join ("\n", @lines);
+}
+
+sub addgimpprinter {
+ my ($section, $filecontent) = @_;
+ my $entryinserted = 0;
+ my @lines = split("\n", $filecontent);
+ local $_;
+ for (@lines) {
+ if (/^\s*Printer\s*:\s*($section)\s*$/) {
+ # section already there, nothing to be done
+ return $filecontent;
+ }
+ }
+ return $filecontent . "\nPrinter: $section";
+}
+
+sub removegimpentry {
+ my ($section, $entry, $filecontent) = @_;
+ my $sectionfound = 0;
+ my $done = 0;
+ my @lines = split("\n", $filecontent);
+ local $_;
+ for (@lines) {
+ $_ = "$_\n";
+ next if ($done);
+ if (!$sectionfound) {
+ if (/^\s*Printer\s*:\s*($section)\s*$/) {
+ $sectionfound = 1;
+ }
+ } else {
+ if (/^\s*Printer\s*:\s*.*\s*$/) { # Next section
+ $done = 1;
+ } elsif (/^\s*$entry/) {
+ $_ = "";
+ $done = 1;
+ }
+ }
+ }
+ return join ("", @lines);
+}
+
+sub removegimpprinter {
+ my ($section, $filecontent) = @_;
+ my $sectionfound = 0;
+ my $done = 0;
+ my @lines = split("\n", $filecontent);
+ local $_;
+ for (@lines) {
+ $_ = "$_\n";
+ next if ($done);
+ if (!$sectionfound) {
+ if (/^\s*Printer\s*:\s*($section)\s*$/) {
+ $_ = "";
+ $sectionfound = 1;
+ }
+ } else {
+ if (/^\s*Printer\s*:\s*.*\s*$/) { # Next section
+ $done = 1;
+ } else {
+ $_ = "";
+ }
+ }
+ }
+ return join ("", @lines);
+}
+
+sub isgimpprinterconfigured {
+ my ($queue, $filecontent) = @_;
+ my $sectionfound = 0;
+ my $done = 0;
+ my $drivernotps2 = 0;
+ my $ppdfileset = 0;
+ my $nonrawprinting = 0;
+ my @lines = split("\n", $filecontent);
+ local $_;
+ for (@lines) {
+ last if ($done);
+ if (!$sectionfound) {
+ if (/^\s*Printer\s*:\s*($queue)\s*$/) {
+ $sectionfound = 1;
+ }
+ } else {
+ if (/^\s*Printer\s*:\s*.*\s*$/) { # Next section
+ $done = 1;
+ } elsif (/^\s*Driver:\s*(\S+)\s*$/) {
+ $drivernotps2 = ($1 ne "ps2");
+ } elsif (/^\s*PPD\-File:\s*(\S+)\s*$/) {
+ $ppdfileset = 1;
+ } elsif (/^\s*Destination:\s*(\S+.*)$/) {
+ $nonrawprinting = ($1 !~ /\-o\s*raw/);
+ }
+ }
+ }
+ return 0 if ($done && !$sectionfound);
+ return 1 if ($ppdfileset || $drivernotps2 || $nonrawprinting);
+ return 0;
+}
+
+# ------------------------------------------------------------------
+
1;