2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these librararies and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
23 /* $TOG: DtPrinterIcon.C /main/5 1998/07/24 16:13:14 mgreess $ */
24 #include "DtPrinterIcon.h"
28 #include "DtPrtProps.h"
29 #include "DtSetModList.h"
31 #include "DtDetailsLabel.h"
32 #include "Dt/Action.h"
37 #include <unistd.h> // This is for the getuid function
38 #include <stdlib.h> // This is for the getenv function
41 #include "dtprintinfomsg.h"
43 const char *PROPS_PRINTER_ID = "PrinterPropsDE";
44 const char *PRINTER_ID = "PrinterDE";
45 const char *STATUS_FLAG = "Flag";
46 const char *PRINTER_ICON_FILE = "Fpprnt";
47 const char *PRINTERS_PERSONAL_DIR = ".dt/.Printers";
49 char DtPrinterIcon::homeDir[300] = "";
51 DtPrinterIcon::DtPrinterIcon(DtMainW *mainW, AnyUI *parent, Queue *que,
52 PrinterApplicationMode _app_mode)
53 : IconObj((char *) que->ObjectClassName(), parent,
54 GetPrinterLabel(que->Name(), _app_mode),
55 GetPrinterIcon(que->Name(), _app_mode))
62 _print_device_up = NULL;
65 // Return if initializing printers
66 if (app_mode == INITIALIZE_PRINTERS)
69 char *buf = new char[300];
73 struct passwd * pwInfo;
74 char *home = getenv("HOME");
75 if (home == NULL || strlen(home) == 0)
77 pwInfo = getpwuid(getuid());
78 home = pwInfo->pw_dir;
80 strcpy(homeDir, home);
81 sprintf(buf, "%s/%s", homeDir, PRINTERS_PERSONAL_DIR);
82 if (stat(buf, &statbuff) < 0)
84 sprintf(buf, "mkdir -p %s/%s", homeDir, PRINTERS_PERSONAL_DIR);
87 sprintf(buf, "%s/.dt/types", homeDir);
88 if (stat(buf, &statbuff) < 0)
90 sprintf(buf, "mkdir -p %s/.dt/types", homeDir);
93 DtDbReloadNotify(&DtPrinterIcon::ReloadNotifyCB, this);
95 sprintf(buf, "%s/%s/%s_Print", homeDir, PRINTERS_PERSONAL_DIR,
97 if (stat(buf, &statbuff) < 0)
99 int fd = creat(buf, 0755);
102 else if (statbuff.st_mode != 0755)
107 waitForChildren = false;
108 if (app_mode == PRINT_MANAGER)
109 expand = new Button(this, queue->DisplayName(), PUSH_BUTTON, OpenCloseCB,
110 NULL, NULL, NULL, NULL, "Dtplus");
114 if (app_mode != CONFIG_PRINTERS)
117 n_devices = queue->NumberDevices();
118 _print_device_up = new boolean[n_devices];
120 for (i = 0; i < n_devices; i++)
121 _print_device_up[i] = true;
123 queue->ReadAttributes();
124 _print_device_up = true;
126 _print_queue_up = true;
127 flag = new IconObj((char *)STATUS_FLAG, this, NULL, "DtFlag");
128 flag->Visible(false);
129 details_label = new DtDetailsLabel(this);
130 _previous_show_only_my_jobs = mainw->setPrefD->ShowOnlyMyJobs() ?
132 ShowDetailsLabel(IconView(), false);
137 details_label = NULL;
139 dnd = new DtDND(this, DndCB);
140 mainw->RegisterPopup(this);
141 ApplicationData = NULL;
142 if (app_mode == SINGLE_PRINTER)
144 mainw->IconName((char *)Name());
145 char *iconFile = GetPrinterIcon(que->Name(), _app_mode);
146 char *s = new char [strlen(iconFile) + 6];
148 sprintf(s, "%s.l.bm", iconFile);
150 sprintf(s, "%s.l.pm", iconFile);
152 if (!mainw->IconFile())
154 *(s + strlen(iconFile) + 1) = 'm';
162 DtPrinterIcon::~DtPrinterIcon()
166 delete _print_device_up;
171 char *DtPrinterIcon::Description()
173 static char buf[200];
175 sprintf(buf, "%s_Print", queue->Name());
176 char *desc = DtActionDescription(buf);
177 return (desc ? desc : "");
180 void DtPrinterIcon::PrintQueueUp(boolean _flag)
182 if (app_mode == CONFIG_PRINTERS)
185 status[0]->Value(_flag ? MESSAGE(UpL) : MESSAGE(DownL));
186 _print_queue_up = _flag;
192 void DtPrinterIcon::PrintDeviceUp(boolean _flag, int index)
194 if (app_mode == CONFIG_PRINTERS)
196 if (index >= 0 && index < n_devices)
199 status[index + 1]->Value(_flag ? MESSAGE(UpL) : MESSAGE(DownL));
200 _print_device_up[index] = _flag;
205 boolean DtPrinterIcon::PrintDeviceUp(int index)
207 if (app_mode != CONFIG_PRINTERS)
209 if (index >= 0 && index < n_devices)
210 return _print_device_up[index];
217 void DtPrinterIcon::PrintDeviceUp(boolean _flag)
219 if (app_mode == CONFIG_PRINTERS)
222 status[1]->Value(_flag ? MESSAGE(UpL) : MESSAGE(DownL));
223 _print_device_up = _flag;
229 boolean DtPrinterIcon::SetName(char *_name)
231 IconObj::SetName(_name);
237 boolean DtPrinterIcon::SetOpen(boolean _flag)
239 if (app_mode != CONFIG_PRINTERS)
240 IconObj::SetOpen(_flag);
244 DtPrinterContainer *DtPrinterIcon::CreateContainer()
248 container = new DtPrinterContainer((char *)Category(), this,
249 queue->DisplayName());
250 mainw->RegisterPopup(container);
251 container->dnd = new DtDND(container, DndCB);
252 container->ApplicationData = queue;
253 ApplicationData = container;
258 char *DtPrinterIcon::CreateActionFile()
260 static char filename[300];
262 struct stat statbuff;
265 char *buf = new char[300];
266 char *lang = getenv("LANG");
267 if (!(lang && *lang))
270 if (app_mode == INITIALIZE_PRINTERS || app_mode == CONFIG_PRINTERS)
272 sprintf(filename, "/etc/dt/appconfig/types/%s", lang);
273 if (stat(filename, &statbuff) < 0)
275 sprintf(buf, "mkdir -p %s", filename);
278 sprintf(filename, "/etc/dt/appconfig/types/%s/%s.dt", lang,
280 if (stat(filename, &statbuff) < 0 || statbuff.st_size == 0)
287 sprintf(buf, "%s/.dt/types/%s.dt", homeDir, queue->Name());
288 if (stat(buf, &statbuff) < 0 || statbuff.st_size == 0)
290 sprintf(buf, "/etc/dt/appconfig/types/%s/%s.dt", lang, queue->Name());
291 if (stat(buf, &statbuff) >= 0 && statbuff.st_size > 0)
293 sprintf(buf, "cp /etc/dt/appconfig/types/%s/%s.dt %s/.dt/types",
294 lang, queue->Name(), homeDir);
303 sprintf(filename, "%s/.dt/types/%s.dt", homeDir, queue->Name());
307 if (fp = fopen(filename, "w"))
311 fprintf(fp, "################################################################\n\n");
312 fprintf(fp, "# Actions and Datatypes for Printer \"%s\"\n\n", queue->Name());
313 fprintf(fp, "# Common Desktop Environment 1.0\n\n");
314 fprintf(fp, "# This file was created by the \"dtprintinfo\" program.\n\n");
315 strftime(buf, 300, "%c", localtime(&secs));
316 fprintf(fp, "# Date of creation: %s\n\n", buf);
317 fprintf(fp, "################################################################\n\n");
318 fprintf(fp, "ACTION %s_Print\n", queue->Name());
320 fprintf(fp, " ARG_TYPE *\n");
321 fprintf(fp, " LABEL %s\n", queue->Name());
322 fprintf(fp, " ICON %s\n", PRINTER_ICON_FILE);
323 fprintf(fp, " TYPE COMMAND\n");
324 fprintf(fp, " WINDOW_TYPE NO_STDIO\n");
325 fprintf(fp, " EXEC_STRING env LPDEST=%s \\\n", queue->Name());
326 fprintf(fp, " /usr/dt/bin/dtaction Print %%(File)Arg_1%%\n");
327 sprintf(buf, MESSAGE(DefaultDescriptionL), queue->Name());
328 fprintf(fp, " DESCRIPTION %s\n", buf);
329 fprintf(fp, "}\n\n");
331 fprintf(fp, "ACTION %s_Print\n", queue->Name());
333 fprintf(fp, " ARG_COUNT 0\n");
334 fprintf(fp, " TYPE COMMAND\n");
335 fprintf(fp, " WINDOW_TYPE NO_STDIO\n");
336 fprintf(fp, " EXEC_STRING /usr/dt/bin/dtaction Dtprintinfo %s\n", queue->Name());
340 chmod(filename, 0644);
347 void DtPrinterIcon::DndCB(BaseUI *obj, char **value, int * /*len*/,
348 DNDProtocol dndProtocol)
350 DtPrinterIcon *printer;
351 if (obj->UIClass() == ICON)
352 printer = (DtPrinterIcon *)obj;
354 printer = (DtPrinterIcon *)obj->Parent();
356 char *old_LPDEST = NULL;
357 ap[0].argClass = DtACTION_FILE;
359 char *buf = new char[100];
362 case FILENAME_TRANSFER: // Dropping an Object on a printer
363 ap[0].u.file.name = *value;
364 if (printer->PrintActionExists())
365 sprintf(buf, "%s_Print", printer->queue->Name());
368 if (old_LPDEST = STRDUP(getenv("LPDEST")))
370 sprintf(buf, "LPDEST=%s", printer->queue->Name());
373 strcpy(buf, "Print");
376 DtActionInvoke(((AnyUI *)printer->mainw->Parent())->BaseWidget(), buf, ap,
377 1, NULL, NULL, NULL, True, NULL, NULL);
380 sprintf(buf, "LPDEST=%s", old_LPDEST);
385 case CONVERT_DATA: // Dragging a printer to an object
386 if (printer->PrintActionExists())
388 struct stat statbuff;
389 *value = new char[strlen(homeDir) + strlen(PRINTERS_PERSONAL_DIR) +
390 strlen(printer->queue->Name()) + 10];
391 sprintf(*value, "%s/%s/%s_Print", homeDir, PRINTERS_PERSONAL_DIR,
392 printer->queue->Name());
393 if (stat(*value, &statbuff) < 0)
395 int fd = creat(*value, 0755);
405 char *y = strchr(x, '\n');
407 char *filename = strchr(y, '\n');
409 char *work_space = strchr(filename, '\n');
411 *work_space++ = '\0';
416 case BUFFER_TRANSFER:
417 *value = new char[strlen(printer->queue->Name()) + 10];
418 sprintf(*value, "%s_Print", printer->queue->Name());
428 boolean DtPrinterIcon::SetIcon(IconStyle style)
430 IconObj::SetIcon(style);
433 ShowDetailsLabel(style, Visible());
438 boolean DtPrinterIcon::HandleHelpRequest()
440 mainw->DisplayHelp((char *)PRINTER_ID);
444 void DtPrinterIcon::UpdateExpand()
449 expand->IconFile("Dtminus");
451 expand->IconFile("Dtplus");
453 ShowDetailsLabel(IconView(), true);
456 void DtPrinterIcon::Update()
458 char *cmd = new char[200];
459 sprintf(cmd, GET_QUEUE_STATUS, queue->Name());
460 Invoke *_thread = new Invoke(cmd);
461 PrintQueueUp(_thread->status ? false : true);
465 for (i = 0; i < n_devices; i++)
467 sprintf(cmd, GET_DEVICE_STATUS, queue->Device(i));
468 _thread = new Invoke(cmd);
469 PrintDeviceUp(_thread->status ? false : true, i);
474 if (queue->IsRemote())
475 PrintDeviceUp(_print_queue_up);
478 sprintf(cmd, GET_DEVICE_STATUS, queue->Name());
479 _thread = new Invoke(cmd);
480 PrintDeviceUp(_thread->status ? false : true);
484 sprintf(cmd, GET_DEVICE_STATUS, queue->Name());
485 _thread = new Invoke(cmd);
486 PrintDeviceUp(_thread->status ? false : true);
493 void DtPrinterIcon::ShowFlag()
495 if (app_mode == CONFIG_PRINTERS)
498 if (Visible() == false || mainw->setPrefD->ShowStatusFlags() == false)
500 flag->Visible(false);
503 // Update problem flag's visiblity
510 for (i = 0; i < n_devices; i++)
511 if (_print_device_up[i] == false)
519 flag->Visible(show_it);
521 if (_print_queue_up && _print_device_up)
522 flag->Visible(false);
528 void DtPrinterIcon::NotifyVisiblity(BaseUI *obj)
530 if (app_mode == CONFIG_PRINTERS)
535 boolean show_it = Visible();
538 if (mainw->setPrefD->ShowStatusFlags() == false)
547 for (i = 0; i < n_devices; i++)
548 if (_print_device_up[i] == false)
557 if (_print_queue_up && _print_device_up)
564 if (flag->Visible() != show_it)
565 flag->Visible(show_it);
567 else if (obj == details_label)
569 boolean show_it = Visible();
572 if (container && IconView() == DETAILS && Open())
574 if (container->NumChildren() == 0 ||
575 (container->Children()[0])->UIClass() == LABEL)
577 else if (mainw->setPrefD->ShowDetailsLabel())
581 BaseUI **children = container->Children();
582 for (i = 0; i < container->NumChildren(); i++)
583 if (children[i]->Visible())
590 show_it = mainw->setPrefD->ShowDetailsLabel();
595 if (details_label->Visible() != show_it)
596 details_label->Visible(show_it);
600 void DtPrinterIcon::ShowDetailsLabel(IconStyle style, boolean is_visible)
602 if (app_mode == CONFIG_PRINTERS)
605 if (_previous_show_only_my_jobs != mainw->setPrefD->ShowOnlyMyJobs())
607 _previous_show_only_my_jobs = mainw->setPrefD->ShowOnlyMyJobs();
608 details_label->Update(_previous_show_only_my_jobs);
610 if (container && style == DETAILS && is_visible && Open())
612 if (container->NumChildren() == 0 ||
613 (container->Children()[0])->UIClass() == LABEL)
614 details_label->Visible(false);
615 else if (mainw->setPrefD->ShowDetailsLabel())
617 boolean show_it = false;
619 BaseUI **children = container->Children();
620 for (i = 0; i < container->NumChildren(); i++)
621 if (children[i]->Visible())
626 details_label->Visible(show_it);
629 details_label->Visible(false);
632 details_label->Visible(false);
635 boolean DtPrinterIcon::SetVisiblity(boolean _flag)
639 IconObj::SetVisiblity(_flag);
641 if (container && Open())
642 container->Visible(true);
643 ShowDetailsLabel(IconView(), _flag);
649 container->Visible(false);
653 BaseUI **children = container->Children();
654 for (i = 0; i < container->NumChildren(); i++)
655 mainw->findD->DeleteJobFromList(children[i]);
658 IconObj::SetVisiblity(_flag);
662 dnd->UpdateActivity(_flag);
666 void DtPrinterIcon::DisplayProps()
670 char *title = new char[200];
671 sprintf(title, MESSAGE(PrinterPropsTitleL), mainw->Name());
672 queue->ReadAttributes();
673 props = new DtPrtProps(mainw, title, this);
675 props->FindByName(MESSAGE(PrinterStatusL), 0, &n_matches,
679 props->Visible(true);
682 void DtPrinterIcon::ReloadNotifyCB(XtPointer data)
684 DtPrinterIcon *printer = (DtPrinterIcon *)data;
686 int i, n_siblings = printer->NumSiblings();
687 BaseUI **siblings = printer->Siblings();
689 for (i = 0; i < n_siblings; i++)
691 printer = (DtPrinterIcon *)siblings[i];
692 char *iconName = GetPrinterLabel(printer->queue->Name(),
694 char *iconFile = GetPrinterIcon(printer->queue->Name(),
697 if (strcmp(iconName, printer->Name()))
699 printer->Name(iconName);
700 if (printer->PrinterAppMode() == SINGLE_PRINTER)
701 printer->mainw->IconName(iconName);
702 if (printer->PrinterAppMode() == PRINT_MANAGER)
704 if (printer->mainw->findD && printer->mainw->findD->Visible())
705 printer->mainw->findD->UpdatePrinter(printer);
708 if (strcmp(iconFile, printer->IconFile()))
710 printer->IconFile(iconFile);
711 if (printer->PrinterAppMode() == SINGLE_PRINTER &&
712 iconFile && *iconFile)
714 char *s = new char [strlen(iconFile) + 6];
716 sprintf(s, "%s.l.bm", iconFile);
718 sprintf(s, "%s.l.pm", iconFile);
719 printer->mainw->IconFile(s);
723 if (printer->props && printer->props->Visible())
724 printer->props->Reset();
726 if (printer->mainw->setModList && printer->mainw->setModList->Visible())
727 printer->mainw->setModList->Reset();
730 void DtPrinterIcon::OpenCloseCB(void *data)
732 BaseUI *obj = ((BaseUI *)data)->Parent();
739 boolean DtPrinterIcon::PrintActionExists()
742 char *buf = new char[60];
743 sprintf(buf, "%s_Print", queue->Name());
744 b = (DtActionExists(buf) ? true : false);
749 char *DtPrinterIcon::GetPrinterIcon(const char *printer,
750 PrinterApplicationMode _app_mode)
752 static char buf[200];
754 sprintf(buf, "%s_Print", printer);
755 if (_app_mode != INITIALIZE_PRINTERS && DtActionExists(buf))
757 char *iconfile = DtActionIcon(buf);
759 strcpy(buf, iconfile);
761 strcpy(buf, PRINTER_ICON_FILE);
764 strcpy(buf, PRINTER_ICON_FILE);
768 char *DtPrinterIcon::GetPrinterLabel(const char *printer,
769 PrinterApplicationMode _app_mode)
771 static char buf[200];
773 sprintf(buf, "%s_Print", printer);
774 if (_app_mode != INITIALIZE_PRINTERS && DtActionExists(buf))
776 char *label = DtActionLabel(buf);
780 strcpy(buf, printer);
783 strcpy(buf, printer);