Debian Codegraph @

demos/pkgInfo

raw
1 #!/usr/bin/perl -w
2 # $Id: pkgInfo,v 1.5 2013/07/17 19:34:07 tom Exp $
3
4 use strict;
5
6 #
7 # This script provides a user interface to packages on the system.
8 #
9 use Cdk;
10 Cdk::init();
11
12 # Create a dialog box with options.
13 my @dialogMessge = ( "<C></U>Unix Package Interface", "<C>Select an option." );
14
15 # Create the buutons for the dialog box.
16 my @buttons =
17 ( "<Add Package>", "<Remove Package>", "<Package Info>", "<Exit>" );
18
19 # Create the dialog widget.
20 my $dialog =
21 new Cdk::Dialog( 'Message' => \@dialogMessge, 'Buttons' => \@buttons );
22
23 # Do this forever.
24 for ( ; ; ) {
25
26 # Activate the dialog box.
27 my $choice = $dialog->activate();
28 next if !defined $choice;
29
30 # Check the answer.
31 if ( $choice == 0 ) {
32 addPackage();
33 }
34 elsif ( $choice == 1 ) {
35 removePackage();
36 }
37 elsif ( $choice == 2 ) {
38 packageInfo();
39 }
40 elsif ( $choice == 3 ) {
41
42 # Exit.
43 Cdk::end();
44 exit;
45 }
46 }
47
48 #
49 # This function adds a package.
50 #
51 sub addPackage {
52 my @packageList = ();
53 my $x;
54
55 # Set up the entry field.
56 my $mesg = "Enter the source package directory:";
57 my $entry = new Cdk::Entry(
58 'Label' => $mesg,
59 'Max' => 512,
60 'Width' => 30
61 );
62
63 # Get the package directory.
64 my $pkgDir = $entry->activate();
65 return if !defined $pkgDir;
66
67 # Open the given directory.
68 if ( !opendir( DIR, $pkgDir ) ) {
69
70 # Couldn't open the directory.
71 my @mesg = (
72 "<C></B/U>Error", "<C>Could not open the directory </B>$pkgDir",
73 "<C></U>$!", "", "<C>Press any key to continue."
74 );
75 my $label = new Cdk::Label( 'Message' => \@mesg );
76 $label->draw();
77 $label->wait();
78 return;
79 }
80
81 # Get the directory listing of the given directory.
82 my @contents = readdir(DIR);
83 closedir(DIR);
84
85 # Create a list of all of the directories under the given directory.
86 foreach my $file (@contents) {
87 next if $file =~ /^\./;
88 push( @packageList, $file ) if ( -d "$pkgDir/$file" );
89 }
90
91 # Create a selection list and display the known packages.
92 my $select = new Cdk::Selection(
93 'Title' => "Add Which Packages?",
94 'List' => \@packageList,
95 'Choices' => [ "Yes ", "No" ],
96 'Height' => 20,
97 'Width' => 50
98 );
99
100 # Activate the packages.
101 my @choiceList = $select->activate();
102 my @selectedPackages = ();
103 return if !@choiceList;
104
105 # Generate the names of the selected packages.
106 for ( $x = 0 ; $x <= $#choiceList ; $x++ ) {
107 if ( $choiceList[$x] == 1 ) {
108 push( @selectedPackages, $packageList[$x] );
109 }
110 }
111 my $label = new Cdk::Label( 'Message' => \@selectedPackages );
112 $label->draw();
113 $label->wait();
114 }
115
116 #
117 # This function removes a package.
118 #
119 sub removePackage {
120
121 # Get the packages to remove.
122 my @packageNames = selectPackages("<C></U>Select the packages to delete.");
123 my @buttons = ( "<No>", "<Yes>", "<Yes To ALL>" );
124 my $yesToAll = 0;
125
126 # Now that we have the package names, ask for each one.
127 foreach my $name (@packageNames) {
128
129 # If they asked for all to be deleted, no need to ask
130 if ( !$yesToAll ) {
131
132 # Create the dialog message.
133 my @mesg = (
134 "<C>Are you sure you want",
135 "to delete the package </U>$name<!U>?"
136 );
137
138 # Make sure they want to do it.
139 my $dialog = new Cdk::Dialog(
140 'Message' => \@mesg,
141 'Buttons' => \@buttons
142 );
143
144 # Activate it.
145 my $answer = $dialog->activate();
146
147 # If the user hit escape, let the user know the package
148 # was NOT deleted.
149 if ( !defined $answer ) {
150 popupLabel(
151 [
152 "<C>Escape Hit",
153 "<C>The package was </R>Not<!R> deleted."
154 ]
155 );
156 next;
157 }
158
159 # Check the answer
160 if ( $answer == 1 ) {
161
162 # Delete the package
163 popupLabel("pkgrm $name");
164 }
165 else {
166
167 # Set the yes to all flag.
168 $yesToAll = 1;
169
170 # Delete the package
171 popupLabel("pkgrm $name");
172 }
173 }
174 else {
175
176 # Delete the package
177 popupLabel("pkgrm $name");
178 }
179 }
180 }
181
182 #
183 # This function provides information about packages.
184 #
185 sub packageInfo {
186
187 # Get the package to view.
188 my $packageName = selectPackage("<C></U>Select a package to view.");
189
190 # Get the information about the selected package.
191 my @pinfo = qx (pkginfo -l $packageName);
192 chomp @pinfo;
193
194 # Create the information viewer.
195 my $viewer = new Cdk::Viewer(
196 'Buttons' => ["OK"],
197 'Height' => -2,
198 'Width' => -4
199 );
200
201 # Activate the viewer.
202 $viewer->set(
203 'Title' => "<C>Package Name: </U>$packageName<!U>",
204 'Info' => \@pinfo,
205 'Interp' => "FALSE"
206 );
207 $viewer->activate();
208 }
209
210 #
211 # This function creates a scrolling list of all the packages on the system
212 # and returns the name of the package selected.
213 #
214 sub selectPackage {
215 my $title = shift;
216
217 # Get a list of all the packages installed on the system.
218 my @packageList = qx (pkginfo);
219 my @packageNames = ();
220
221 # Strip out the names of the packages.
222 foreach my $PKG (@packageList) {
223 my $pkgName = ( split( /\s+/, $PKG ) )[1];
224 push( @packageNames, $pkgName );
225 }
226
227 # Create the scrolling list.
228 my $packageList = new Cdk::Scroll(
229 'Title' => $title,
230 'List' => \@packageNames,
231 'Numbers' => "TRUE",
232 'Height' => 20,
233 'Width' => 50
234 );
235
236 # Return the selected name.
237 my $choice = $packageList->activate();
238 return ( $packageNames[$choice] );
239 }
240
241 #
242 # This function creates a selection list of all the packages on the system
243 # and returns the name of the package selected.
244 #
245 sub selectPackages {
246 my $title = shift;
247 my @options = qw (Keep Delete);
248 my @selectedNames = ();
249
250 # Get a list of all the packages installed on the system.
251 my @packageList = qx (pkginfo);
252 my @packageNames = ();
253 my @selectedPackages = ();
254 my $x = 0;
255
256 # Strip out the names of the packages.
257 foreach my $PKG (@packageList) {
258 my $pkgName = ( split( /\s+/, $PKG ) )[1];
259 push( @packageNames, $pkgName );
260 }
261
262 # Create the selection list.
263 my $select = new Cdk::Selection(
264 'Title' => $title,
265 'List' => \@packageNames,
266 'Choices' => \@options,
267 'Height' => 20,
268 'Width' => 50
269 );
270
271 # Activate the selection list.
272 my @list = $select->activate();
273
274 # Generate the names of the selected packages.
275 for ( $x = 0 ; $x <= $#packageNames ; $x++ ) {
276 if ( $list[$x] == 1 ) {
277 push( @selectedPackages, $packageNames[$x] );
278 }
279 }
280 return (@selectedPackages);
281 }