Handle hostnames with upper-case letters
[webmin.git] / lpadmin / solaris-driver.pl
1 # solaris-driver.pl
2 # Functions for Solaris 10 printer drivers
3
4 $webmin_windows_driver = 1;
5 $cups_ppd_dir = "/etc/lp/ppd";
6 $lpoptions = &has_command("lpoptions.cups") ? "lpoptions.cups" : "lpoptions";
7
8 # is_windows_driver(path, &printer)
9 # Returns the server, share, username, password, workgroup, program
10 # if path is a webmin windows driver
11 sub is_windows_driver
12 {
13 return &is_webmin_windows_driver(@_);
14 }
15
16 # is_driver(path, &printer)
17 # Returns the driver name if some path is a Solaris driver, or undef
18 sub is_driver
19 {
20 local $ppd = $_[1]->{'ppd'} ? &parse_cups_ppd($_[1]->{'ppd'}) : undef;
21 if ($ppd && $ppd->{'NickName'}) {
22         # Printer has a PPD file
23         return { 'mode' => 1,
24                  'manuf' => $ppd->{'Manufacturer'},
25                  'model' => $ppd->{'ModelName'},
26                  'nick' => $ppd->{'NickName'},
27                  'desc' => $ppd->{'NickName'} };
28         }
29 elsif ($_[1]->{'iface'}) {
30         # Some other kind of interface file
31         return { 'mode' => 2,
32                  'file' => $_[1]->{'iface'},
33                  'desc' => $_[1]->{'iface'} };
34         }
35 else {
36         # No driver
37         return { 'mode' => 0,
38                  'desc' => $text{'cups_none'} };
39         }
40 }
41
42 # create_windows_driver(&printer, &driver)
43 sub create_windows_driver
44 {
45 return &create_webmin_windows_driver(@_);
46 }
47
48 # create_driver(&printer, &driver)
49 sub create_driver
50 {
51 local $drv = "$cups_ppd_dir/$_[0]->{'name'}.ppd";
52 if ($_[1]->{'mode'} == 0) {
53         # No driver
54         &system_logged("rm -f \"$drv\"");
55         $_[0]->{'ppd'} = undef;
56         return undef;
57         }
58 elsif ($_[1]->{'mode'} == 2) {
59         # A separate interface program
60         &system_logged("rm -f \"$drv\"");
61         $_[0]->{'ppd'} = undef;
62         return $_[1]->{'file'};
63         }
64 else {
65         # A PPD driver, which replaces any interface program
66         $_[0]->{'ppd'} = $_[1]->{'ppd'};
67         return undef;
68         }
69 }
70
71 # delete_driver(name)
72 sub delete_driver
73 {
74 &system_logged("rm -f \"$cups_ppd_dir/$_[0].ppd\"");
75 }
76
77 # driver_input(&printer, &driver)
78 sub driver_input
79 {
80 printf "<tr> <td><input type=radio name=mode value=0 %s> %s</td>\n",
81         $_[1]->{'mode'} == 0 ? 'checked' : '', $text{'cups_none'};
82 print "<td>($text{'cups_nonemsg'})</td> </tr>\n";
83 printf "<tr> <td><input type=radio name=mode value=2 %s> %s</td>",
84         $_[1]->{'mode'} == 2 ? 'checked' : '', $text{'cups_prog'};
85 printf "<td><input name=program size=40 value='%s'></td> </tr>\n",
86         $_[1]->{'mode'} == 2 ? $_[0]->{'iface'} : '';
87
88 # Display all the CUPS drivers
89 printf "<tr> <td valign=top><input type=radio name=mode value=1 %s> %s</td>\n",
90         $_[1]->{'mode'} == 1 ? 'checked' : '', $text{'cups_driver'};
91 print "<td><select name=ppd size=10>\n";
92 local (@ppds, $d, $f, $ppd, %cache, $outofdate, @files, %donefile);
93 open(FIND, "find '$config{'model_path'}' -type f -print |");
94 while(<FIND>) {
95         chop;
96         /([^\/]+)$/;
97         next if ($donefile{$1}++);
98         push(@files, $_);
99         }
100 close(FIND);
101 &read_file("$module_config_directory/ppd-cache", \%cache);
102 foreach $f (@files) {
103         if (!defined($cache{$f})) {
104                 $outofdate = $f;
105                 last;
106                 }
107         }
108 if ($outofdate || scalar(keys %cache) != scalar(@files)) {
109         # Cache is out of date
110         undef(%cache);
111         local %donecache;
112         foreach $f (@files) {
113                 local $ppd = &parse_cups_ppd($f);
114                 $cache{$f} = $ppd->{'NickName'}
115                         if (!$donecache{$ppd->{'NickName'}}++);
116                 }
117         &write_file("$module_config_directory/ppd-cache", \%cache);
118         }
119 local %done;
120 foreach $f (sort { $cache{$a} cmp $cache{$b} } keys %cache) {
121         if ($cache{$f} && !$done{$cache{$f}}++) {
122                 printf "<option value=%s %s>%s\n",
123                         $f, $_[1]->{'nick'} eq $cache{$f} ? 'selected' : '',
124                         $cache{$f};
125                 $currppd = $f if ($_[1]->{'nick'} eq $cache{$f});
126                 }
127         }
128 print "</select>\n";
129 print "</td> </tr>\n";
130 return undef;
131 }
132
133 # parse_driver()
134 # Parse driver selection from %in and return a driver structure
135 sub parse_driver
136 {
137 if ($in{'mode'} == 0) {
138         return { 'mode' => 0 };
139         }
140 elsif ($in{'mode'} == 2) {
141         $in{'program'} =~ /^(\S+)/ && -x $1 ||
142                 &error(&text('cups_eprog', $in{'program'}));
143         return { 'mode' => 2,
144                  'file' => $in{'program'} };
145         }
146 elsif ($in{'mode'} == 1) {
147         # CUPS printer driver
148         local $ppd = &parse_cups_ppd($in{'ppd'});
149         local $rv = { 'mode' => 1,
150                       'ppd' => $in{'ppd'},
151                       'nick' => $ppd->{'NickName'},
152                       'manuf' => $ppd->{'Manufacturer'},
153                       'model' => $ppd->{'ModelName'} };
154         return $rv;
155         }
156 }
157
158 1;
159