diff options
author | Till Kamppeter <tkamppeter@mandriva.com> | 2002-08-28 23:38:37 +0000 |
---|---|---|
committer | Till Kamppeter <tkamppeter@mandriva.com> | 2002-08-28 23:38:37 +0000 |
commit | 8b3d1554c55a3ef28442461c2f3fb6ec63f5f028 (patch) | |
tree | 54e8978e0388d24e631a99797ac872c66502678b | |
parent | a70ec0d372ee16efaa038ec0ce5b09331f3d8b3c (diff) | |
download | drakx-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.
-rw-r--r-- | perl-install/printer.pm | 413 | ||||
-rw-r--r-- | perl-install/printerdrake.pm | 28 |
2 files changed, 424 insertions, 17 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; diff --git a/perl-install/printerdrake.pm b/perl-install/printerdrake.pm index ebba01ed2..9e160b696 100644 --- a/perl-install/printerdrake.pm +++ b/perl-install/printerdrake.pm @@ -2856,7 +2856,7 @@ sub setup_default_spooler { return; } if ($printer->{SPOOLER} ne $oldspooler) { - # Remove the local printers from Star Office/OpenOffice.org + # Remove the local printers from Star Office/OpenOffice.org/GIMP printer::removelocalprintersfromapplications($printer); # Get the queues of this spooler { @@ -3077,7 +3077,7 @@ sub main { messages => ($noprinters ? "" : (($printer->{SPOOLER} eq "cups") ? - _("The following printers are configured. Double-click on a printer to change its settings; to make it the default printer; to view information about it; or to make a printer on a remote CUPS server available for Star Office/OpenOffice.org.") : + _("The following printers are configured. Double-click on a printer to change its settings; to make it the default printer; to view information about it; or to make a printer on a remote CUPS server available for Star Office/OpenOffice.org/GIMP.") : _("The following printers are configured. Double-click on a printer to change its settings; to make it the default printer; or to view information about it."))), cancel => (""), ok => (""), @@ -3398,8 +3398,8 @@ What do you want to modify on this printer?", (($queue ne $printer->{DEFAULT}) ? _("Set this printer as the default") : ()), ($printer->{configured}{$queue} ? () : - (_("Add this printer to Star Office/OpenOffice.org"), - _("Remove this printer from Star Office/OpenOffice.org"))), + (_("Add this printer to Star Office/OpenOffice.org/GIMP"), + _("Remove this printer from Star Office/OpenOffice.org/GIMP"))), _("Print test pages"), _("Know how to use this printer"), ($printer->{configured}{$queue} ? @@ -3476,23 +3476,23 @@ What do you want to modify on this printer?", printer::set_default_printer($printer); $in->ask_warn(_("Default printer"), _("The printer \"%s\" is set as the default printer now.", $queue)); - } elsif ($modify eq _("Add this printer to Star Office/OpenOffice.org")) { + } elsif ($modify eq _("Add this printer to Star Office/OpenOffice.org/GIMP")) { if (printer::addcupsremotetoapplications ($printer, $queue)) { - $in->ask_warn(_("Adding printer to Star Office/OpenOffice.org"), - _("The printer \"%s\" was successfully added to Star Office/OpenOffice.org.", $queue)); + $in->ask_warn(_("Adding printer to Star Office/OpenOffice.org/GIMP"), + _("The printer \"%s\" was successfully added to Star Office/OpenOffice.org/GIMP.", $queue)); } else { - $in->ask_warn(_("Adding printer to Star Office/OpenOffice.org"), - _("Failed to add the printer \"%s\" to Star Office/OpenOffice.org.", $queue)); + $in->ask_warn(_("Adding printer to Star Office/OpenOffice.org/GIMP"), + _("Failed to add the printer \"%s\" to Star Office/OpenOffice.org/GIMP.", $queue)); } - } elsif ($modify eq _("Remove this printer from Star Office/OpenOffice.org")) { + } elsif ($modify eq _("Remove this printer from Star Office/OpenOffice.org/GIMP")) { if (printer::removeprinterfromapplications ($printer, $queue)) { - $in->ask_warn(_("Removing printer from Star Office/OpenOffice.org"), - _("The printer \"%s\" was successfully removed from Star Office/OpenOffice.org.", $queue)); + $in->ask_warn(_("Removing printer from Star Office/OpenOffice.org/GIMP"), + _("The printer \"%s\" was successfully removed from Star Office/OpenOffice.org/GIMP.", $queue)); } else { - $in->ask_warn(_("Removing printer from Star Office/OpenOffice.org"), - _("Failed to remove the printer \"%s\" from Star Office/OpenOffice.org.", $queue)); + $in->ask_warn(_("Removing printer from Star Office/OpenOffice.org/GIMP"), + _("Failed to remove the printer \"%s\" from Star Office/OpenOffice.org/GIMP.", $queue)); } } elsif ($modify eq _("Print test pages")) { print_testpages($printer, $in, $upNetwork); |