~ubuntu-branches/ubuntu/intrepid/tcm/intrepid

« back to all changes in this revision

Viewing changes to src/ui/printerqueuedialog.c

  • Committer: Bazaar Package Importer
  • Author(s): Otavio Salvador
  • Date: 2003-07-03 20:08:21 UTC
  • Revision ID: james.westby@ubuntu.com-20030703200821-se4xtqx25e5miczi
Tags: upstream-2.20
ImportĀ upstreamĀ versionĀ 2.20

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
////////////////////////////////////////////////////////////////////////////////
 
2
//
 
3
// This file is part of Toolkit for Conceptual Modeling (TCM).
 
4
// (c) copyright 1997, Vrije Universiteit Amsterdam.
 
5
// Author: Frank Dehne (frank@cs.vu.nl).
 
6
//
 
7
// TCM is free software; you can redistribute it and/or modify
 
8
// it under the terms of the GNU General Public License as published by
 
9
// the Free Software Foundation; either version 2 of the License, or
 
10
// (at your option) any later version.
 
11
//
 
12
// TCM is distributed in the hope that it will be useful,
 
13
// but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
// GNU General Public License for more details.
 
16
//
 
17
// You should have received a copy of the GNU General Public License
 
18
// along with TCM; if not, write to the Free Software
 
19
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 
20
// 02111-1307, USA.
 
21
////////////////////////////////////////////////////////////////////////////////
 
22
#include "printerqueuedialog.h"
 
23
#include "printeroptions.h"
 
24
#include "mainwindow.h"
 
25
#include "promptdialog.h"
 
26
#include "messagedialog.h"
 
27
#include "link.h"
 
28
#include "mstring.h"
 
29
#include "system.h"
 
30
#include <Xm/XmAll.h>
 
31
#include <stdlib.h>
 
32
 
 
33
PrinterQueueDialog::PrinterQueueDialog(Widget p, MainWindow *w): 
 
34
                SelectionDialog(p) {
 
35
        SetAutoUnmanage(False);
 
36
        mainwindow = w;
 
37
        promptDialog = new PromptDialog(mainwindow->GetWidget());
 
38
        promptDialog->Initialize();
 
39
        printerOptions = mainwindow->GetPrinterOptions();
 
40
        queueCommand = "";
 
41
        defaultQueueCommand = "";
 
42
        removeCommand = "";
 
43
        defaultRemoveCommand = "";
 
44
}
 
45
 
 
46
void PrinterQueueDialog::CreateWidget() {
 
47
        // create the selection box dialog
 
48
        Arg al[10]; int ac = 0;
 
49
        XtSetArg(al[ac], XmNautoUnmanage, GetAutoUnmanage()); ac++;
 
50
        XtSetArg(al[ac], XmNdefaultButtonType, XmDIALOG_APPLY_BUTTON); ac++;
 
51
        XtSetArg(al[ac], XmNmustMatch, False); ac++;
 
52
        SetWidget(XmCreateSelectionDialog(GetParent(), (char *)GetClassName(), al, ac));
 
53
}
 
54
 
 
55
void PrinterQueueDialog::Configure() {
 
56
        SelectionDialog::Configure();
 
57
        const char *selection = 
 
58
                "Update refreshes, Remove cancels selected item and"
 
59
                " Dismiss deletes dialog.";
 
60
        SetSelectionLabel(selection);
 
61
        SetCancelButtonLabel("Dismiss");
 
62
        SetOKButtonLabel("Update");
 
63
        SetApplyButtonLabel("Remove");
 
64
        SetApplyCallback(RemoveCB, this);
 
65
        SetOKCallback(UpdateCB, this);
 
66
        SetCancelCallback(UnmanageCB, this);
 
67
        ManageHelpButton(False);
 
68
        SetEditable(False);
 
69
}
 
70
 
 
71
void PrinterQueueDialog::Update() {
 
72
        mainwindow->SetCursor(MouseCursor::WATCH);
 
73
        mainwindow->SetStatus("");
 
74
        // Check if printer queue command exists.
 
75
        if  (!System::FileExists(queueCommand.getstr())) {
 
76
                string txt = "The Unix program " + queueCommand + 
 
77
                             " cannot be found."; 
 
78
                (new MessageDialog(mainwindow->GetWidget(), 
 
79
                        MessageDialog::ERROR))->
 
80
                        Show("Error", &txt);
 
81
                error("Error: could not open %s\n", queueCommand.getstr());
 
82
                return;
 
83
        }
 
84
        // establish a link to Unix printer queue with the link class.
 
85
        // and place the strings from output into the selection box.
 
86
        Link l;
 
87
        char s[MAXNAME], s2[MAXNAME];
 
88
        if (!check(GetWidget()))
 
89
                return;
 
90
        char command[MAXNAME];
 
91
        string printerName = *printerOptions->GetPrinterName();
 
92
        System::GiveFile(queueCommand.getstr(), command);
 
93
        if (equal(command, "lpq"))
 
94
                sprintf(s2, "-P%s", printerName.getstr());
 
95
        else if (equal(command, "lpstat"))
 
96
                sprintf(s2, "-o %s", printerName.getstr());
 
97
        l.Open(queueCommand.getstr(), s2);
 
98
        // l.Read(s);  lose header line
 
99
        // XmListDeleteAllItems(GetList());
 
100
        DeleteAllItems();
 
101
        while (!l.Read(s)) {
 
102
                AddItem(s);
 
103
                // xs = CreateXmString(s);
 
104
                // XmListAddItem(GetList(), xs, 0);
 
105
                // XmStringFree(xs);
 
106
        }
 
107
        l.Close();
 
108
        XtManageChild(GetWidget());
 
109
        mainwindow->SetStatus("Update printer queue");
 
110
        mainwindow->SetCursor(MouseCursor::LEFT_PTR);
 
111
}
 
112
 
 
113
void PrinterQueueDialog::Remove(const char *procstr) {
 
114
        char s[MAXNAME];
 
115
        char rank[20], owner[64], job[20];
 
116
        char removeProg[MAXNAME];
 
117
        char queueProg[MAXNAME];
 
118
        if  (!System::FileExists(removeCommand.getstr())) {
 
119
                string txt = "The Unix program " + removeCommand +
 
120
                             " can not be found"; 
 
121
                (new MessageDialog(mainwindow->GetWidget(), 
 
122
                        MessageDialog::ERROR))->
 
123
                        Show("Error", &txt);
 
124
                error("Error: could not open %s\n", removeCommand.getstr());
 
125
        }
 
126
        // remove item
 
127
        System::GiveFile(removeCommand.getstr(), removeProg);
 
128
        System::GiveFile(queueCommand.getstr(), queueProg);
 
129
        // extract the job number from the line and remove that job number.
 
130
        bool succes = True;
 
131
        if (equal(queueProg, "lpq")) {
 
132
                if (sscanf(procstr, "%s%s%s", rank, owner, job) != 3)
 
133
                        succes = False;
 
134
        }
 
135
        else if (equal(queueProg, "lpstat")) {
 
136
                if (sscanf(procstr, "%*[^-]-%s%s", job, owner) != 2)
 
137
                        succes = False;
 
138
        }
 
139
        else
 
140
                succes = False;
 
141
        if (succes) {
 
142
                string printerName = *printerOptions->GetPrinterName();
 
143
                if (equal(removeProg, "lprm")) {
 
144
                        sprintf(s, "%s -P%s %s 2>&1", 
 
145
                                removeProg, printerName.getstr(), job);
 
146
                }
 
147
                else if (equal(removeProg, "cancel")) {
 
148
                        sprintf(s, "%s %s-%s 2>&1", 
 
149
                                removeProg, printerName.getstr(), job);
 
150
                }
 
151
                system(s);
 
152
                string txt = "try to remove job '";
 
153
                txt += job;
 
154
                txt += "'";
 
155
                mainwindow->SetStatus(&txt);
 
156
        }
 
157
        else
 
158
                mainwindow->SetStatus("nothing to be removed");
 
159
        // make selection string empty.
 
160
        SetTextString("");
 
161
        Update();
 
162
}
 
163
 
 
164
void PrinterQueueDialog::PrinterQueueCommand() {
 
165
        promptDialog->SetTitle("Printer queue command");
 
166
        promptDialog->SetSelectionLabel("Printer queue command:");
 
167
        promptDialog->SetTextString(&queueCommand);
 
168
        promptDialog->SetOKCallback(SetPrinterQueueCommandCB, this);
 
169
        promptDialog->SetDefaultValue(&defaultQueueCommand);
 
170
        promptDialog->Popup();
 
171
}
 
172
 
 
173
void PrinterQueueDialog::PrinterRemoveCommand() {
 
174
        promptDialog->SetTitle("Printer remove command");
 
175
        promptDialog->SetSelectionLabel("Printer remove command:");
 
176
        promptDialog->SetTextString(&removeCommand);
 
177
        promptDialog->SetOKCallback(SetPrinterRemoveCommandCB, this);
 
178
        promptDialog->SetDefaultValue(&defaultRemoveCommand);
 
179
        promptDialog->Popup();
 
180
}
 
181
 
 
182
void PrinterQueueDialog::SetPrinterQueueCommand(const string *s) {
 
183
        queueCommand = *s;
 
184
        string txt = "printer queue command is " + queueCommand;
 
185
        mainwindow->SetStatus(&txt);
 
186
}
 
187
 
 
188
void PrinterQueueDialog::SetPrinterRemoveCommand(const string *s) {
 
189
        removeCommand = *s;
 
190
        string txt = "printer remove command is " + removeCommand;
 
191
        mainwindow->SetStatus(&txt);
 
192
}
 
193
 
 
194
void PrinterQueueDialog::RemoveCB(Widget, XtPointer client_data, XtPointer cbs){
 
195
        XmSelectionBoxCallbackStruct *call_data = 
 
196
                (XmSelectionBoxCallbackStruct *)cbs;
 
197
        PrinterQueueDialog *p = (PrinterQueueDialog *)client_data;
 
198
        char *procstr;
 
199
        if (XmStringGetLtoR(call_data->value,XmFONTLIST_DEFAULT_TAG,&procstr)) {
 
200
                p->Remove(procstr);
 
201
                XtFree(procstr);
 
202
        }
 
203
}
 
204
 
 
205
void PrinterQueueDialog::UpdateCB(Widget, XtPointer client_data, XtPointer) {
 
206
        PrinterQueueDialog *p = (PrinterQueueDialog *)client_data;
 
207
        p->Update();
 
208
}
 
209
 
 
210
void PrinterQueueDialog::SetPrinterQueueCommandCB(Widget, XtPointer clientData,
 
211
                XtPointer) {
 
212
        PrinterQueueDialog *p = (PrinterQueueDialog *)clientData;
 
213
        PromptDialog *dialog = p->GetPromptDialog();
 
214
        string t;
 
215
        dialog->GetTextString(&t);
 
216
        p->SetPrinterQueueCommand(&t);
 
217
}
 
218
 
 
219
void PrinterQueueDialog::SetPrinterRemoveCommandCB(Widget, 
 
220
                XtPointer clientData, XtPointer) {
 
221
        PrinterQueueDialog *p = (PrinterQueueDialog *)clientData;
 
222
        PromptDialog *dialog = p->GetPromptDialog();
 
223
        string t;
 
224
        dialog->GetTextString(&t);
 
225
        p->SetPrinterRemoveCommand(&t);
 
226
}