1
////////////////////////////////////////////////////////////////////////////////
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).
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.
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.
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
21
////////////////////////////////////////////////////////////////////////////////
22
#include "fileutils.h"
28
#include "questiondialog.h"
29
#include "printeroptions.h"
30
#include "application.h"
33
bool FileUtils::Load(Widget widget, const char *file, string *text) {
36
if (!System::FileExists(file)) {
37
sprintf(buf, "File %s does not exist", file);
38
(new MessageDialog(widget, MessageDialog::ERROR))->
42
else if (!System::FileRegular(file)) {
43
sprintf(buf, "File %s is not a regular file", file);
44
(new MessageDialog(widget, MessageDialog::ERROR))->
48
else if ((fd = fopen(file, "r")) == 0) {
49
sprintf(buf, "Cannot read file %s", file);
50
(new MessageDialog(widget, MessageDialog::ERROR))->
55
while ((cnt = fread(buf, sizeof(char), MAXNAME, fd)) > 0) {
63
bool FileUtils::Save(Widget widget, const char *file, const string *text) {
66
if (System::FileExists(file) && !System::FileRegular(file)) {
67
sprintf(buf, "File %s is not a regular file", file);
68
(new MessageDialog(widget, MessageDialog::ERROR))->
72
if (System::FileExists(file)) {
73
QuestionDialog q(widget, False);
75
q.SetTitle("Question");
76
sprintf(buf, "File %s exists\n do you want to overwrite?",file);
77
q.SetMessageString(buf);
78
int answer = q.GetAnswer();
79
if (answer == QuestionDialog::NO)
82
if ((fd = fopen(file, "w")) == 0) {
83
sprintf(buf, "Cannot write file %s", file);
84
(new MessageDialog(widget, MessageDialog::ERROR))->
88
fprintf(fd,"%s", text->getstr());
93
bool FileUtils::PrintingOK(Widget widget, const char *jobName) {
94
PrinterOptions *printerOptions = theApplication->GetPrinterOptions();
95
string name = *printerOptions->GetPrinterName();
96
string txt = "Do you want to print ";
98
txt += "\nto PostScript printer ";
101
QuestionDialog q(widget, False);
103
q.SetTitle("Print confirmation");
104
q.SetMessageString(&txt);
105
return (q.GetAnswer() == QuestionDialog::YES);
108
bool FileUtils::Print(Widget widget, const char *file,
109
const char *jobName, const char *filter) {
111
char command[MAXNAME];
113
if (equal(jobName, ""))
114
strcpy(job, "Untitled");
116
strcpy(job, jobName);
120
PrinterOptions *printerOptions = theApplication->GetPrinterOptions();
121
string name = *printerOptions->GetPrinterName();
122
int copies = printerOptions->GetPrintCopies();
123
int bannerPage = printerOptions->GetPrintBannerPage();
124
string printBanner = "";
125
string printCommand = *printerOptions->GetPrintCommand();
128
if (!System::FileExists(printCommand.getstr())) {
129
string txt = "The Unix command " + printCommand +
131
(new MessageDialog(widget, MessageDialog::ERROR))->
133
error("Error: could not open %s\n",
134
printCommand.getstr());
138
System::GiveFile(printCommand.getstr(), buf);
139
// construct print command
140
if (equal(buf, "lpr")) {
141
if (bannerPage != Config::DEF_BANNER)
143
if (equal(filter, ""))
145
"%s -P%s -#%d -J%s %s <%s 2>&1 | cat >/dev/null",
146
printCommand.getstr(), name.getstr(),
147
copies, job, printBanner.getstr(), file);
150
"%s <%s | %s -P%s -#%d -J%s %s 2>&1 "
152
filter, file, printCommand.getstr(),
153
name.getstr(), copies, job,
154
printBanner.getstr());
156
else if (equal(buf, "lp")) {
157
if (bannerPage != Config::DEF_BANNER)
158
printBanner = "-o nobanner";
159
if (equal(filter, ""))
161
"%s -d %s -n %d -t %s %s <%s 2>&1 "
163
printCommand.getstr(), name.getstr(),
164
copies, job, printBanner.getstr(), file);
167
"%s < %s | %s -d %s -n %d -t %s %s 2>&1 "
169
filter, file, printCommand.getstr(),
170
name.getstr(), copies, job,
171
printBanner.getstr());
174
if (equal(filter, ""))
176
"%s <%s 2>&1 | cat >/dev/null",
177
printCommand.getstr(), file);
180
"%s < %s | %s 2>&1 | cat >/dev/null",
181
filter, file, printCommand.getstr());
183
int status = system(command);
186
(new MessageDialog(widget,
187
MessageDialog::ERROR))->
189
"Printing did not succeed");
190
error("print error, print status = %d \n",
197
(new MessageDialog(widget, MessageDialog::ERROR))->
198
Show("Error", "The printer name is not set");