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

« back to all changes in this revision

Viewing changes to src/ui/fileutils.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 "fileutils.h"
 
23
#include "system.h"
 
24
#include "util.h"
 
25
#include <stdlib.h>
 
26
#include <stdio.h>
 
27
#include <unistd.h>
 
28
#include "questiondialog.h"
 
29
#include "printeroptions.h"
 
30
#include "application.h"
 
31
#include "config.h"
 
32
 
 
33
bool FileUtils::Load(Widget widget, const char *file, string *text) {
 
34
        FILE *fd;
 
35
        char buf[MAXNAME+1];
 
36
        if (!System::FileExists(file)) {
 
37
                sprintf(buf, "File %s does not exist", file);
 
38
                (new MessageDialog(widget, MessageDialog::ERROR))->
 
39
                        Show("Error", buf);
 
40
                return False;
 
41
        }
 
42
        else if (!System::FileRegular(file)) {
 
43
                sprintf(buf, "File %s is not a regular file", file);
 
44
                (new MessageDialog(widget, MessageDialog::ERROR))->
 
45
                        Show("Error", buf);
 
46
                return False;
 
47
        }
 
48
        else if ((fd = fopen(file, "r")) == 0) {
 
49
                sprintf(buf, "Cannot read file %s", file);
 
50
                (new MessageDialog(widget, MessageDialog::ERROR))->
 
51
                        Show("Error", buf);
 
52
                return False;
 
53
        }
 
54
        int cnt;
 
55
        while ((cnt = fread(buf, sizeof(char), MAXNAME, fd)) > 0) {
 
56
                buf[cnt] = '\0';
 
57
                *text += buf;
 
58
        }
 
59
        fclose(fd);
 
60
        return True;
 
61
}
 
62
        
 
63
bool FileUtils::Save(Widget widget, const char *file, const string *text) {
 
64
        FILE *fd;
 
65
        char buf[MAXNAME];
 
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))->
 
69
                        Show("Error", buf);
 
70
                return False;
 
71
        }
 
72
        if (System::FileExists(file)) {
 
73
                QuestionDialog q(widget, False);
 
74
                q.Initialize();
 
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)
 
80
                        return False;
 
81
        }
 
82
        if ((fd = fopen(file, "w")) == 0) {
 
83
                sprintf(buf, "Cannot write file %s", file);
 
84
                (new MessageDialog(widget, MessageDialog::ERROR))->
 
85
                        Show("Error", buf);
 
86
                return False;
 
87
        }
 
88
        fprintf(fd,"%s", text->getstr());
 
89
        fclose(fd);
 
90
        return True;
 
91
}
 
92
 
 
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 ";
 
97
        txt += jobName;
 
98
        txt += "\nto PostScript printer ";
 
99
        txt += name;
 
100
        txt += "?";
 
101
        QuestionDialog q(widget, False);
 
102
        q.Initialize();
 
103
        q.SetTitle("Print confirmation");
 
104
        q.SetMessageString(&txt);
 
105
        return (q.GetAnswer() == QuestionDialog::YES);
 
106
}
 
107
 
 
108
bool FileUtils::Print(Widget widget, const char *file, 
 
109
                const char *jobName, const char *filter) {
 
110
        bool result = True;
 
111
        char command[MAXNAME];
 
112
        char job[MAXNAME];
 
113
        if (equal(jobName, ""))
 
114
                strcpy(job, "Untitled");
 
115
        else
 
116
                strcpy(job, jobName);
 
117
 
 
118
        char buf[MAXNAME];
 
119
        command[0]= '\0';
 
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();
 
126
        // print it
 
127
        if (name != "") {
 
128
                if  (!System::FileExists(printCommand.getstr())) {
 
129
                        string txt = "The Unix command " + printCommand + 
 
130
                                     " is not installed"; 
 
131
                        (new MessageDialog(widget, MessageDialog::ERROR))->
 
132
                                Show("Error", &txt);
 
133
                        error("Error: could not open %s\n", 
 
134
                                printCommand.getstr());
 
135
                        result = False;
 
136
                }
 
137
                else {
 
138
                        System::GiveFile(printCommand.getstr(), buf);
 
139
                        // construct print command
 
140
                        if (equal(buf, "lpr")) {
 
141
                                if (bannerPage != Config::DEF_BANNER)
 
142
                                        printBanner = "-h";
 
143
                                if (equal(filter, ""))
 
144
                                        sprintf(command, 
 
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);
 
148
                                else
 
149
                                        sprintf(command, 
 
150
                                        "%s <%s | %s -P%s -#%d -J%s %s 2>&1 "
 
151
                                        "| cat >/dev/null",
 
152
                                        filter, file, printCommand.getstr(), 
 
153
                                        name.getstr(), copies, job,
 
154
                                        printBanner.getstr());
 
155
                        }
 
156
                        else if (equal(buf, "lp")) {
 
157
                                if (bannerPage != Config::DEF_BANNER)
 
158
                                        printBanner = "-o nobanner";
 
159
                                if (equal(filter, ""))
 
160
                                        sprintf(command, 
 
161
                                        "%s -d %s -n %d -t %s %s <%s 2>&1 "
 
162
                                        "| cat >/dev/null",
 
163
                                        printCommand.getstr(), name.getstr(), 
 
164
                                        copies, job, printBanner.getstr(), file);
 
165
                                else
 
166
                                        sprintf(command, 
 
167
                                        "%s < %s | %s -d %s -n %d -t %s %s 2>&1 "
 
168
                                        "| cat >/dev/null",
 
169
                                        filter, file, printCommand.getstr(), 
 
170
                                        name.getstr(), copies, job,
 
171
                                        printBanner.getstr());
 
172
                        }
 
173
                        else {
 
174
                                if (equal(filter, ""))
 
175
                                        sprintf(command, 
 
176
                                        "%s <%s 2>&1 | cat >/dev/null", 
 
177
                                        printCommand.getstr(), file);
 
178
                                else
 
179
                                        sprintf(command, 
 
180
                                        "%s < %s | %s 2>&1 | cat >/dev/null", 
 
181
                                        filter, file, printCommand.getstr());
 
182
                        }
 
183
                        int status = system(command);
 
184
                        // Check for errors
 
185
                        if (status) {
 
186
                                (new MessageDialog(widget, 
 
187
                                        MessageDialog::ERROR))->
 
188
                                        Show("Error", 
 
189
                                        "Printing did not succeed");
 
190
                                error("print error, print status = %d \n", 
 
191
                                        status);
 
192
                                result = False;
 
193
                        }
 
194
                }
 
195
        } 
 
196
        else {
 
197
                (new MessageDialog(widget, MessageDialog::ERROR))->
 
198
                        Show("Error", "The printer name is not set");
 
199
                result = False;
 
200
        }
 
201
        // remove it.
 
202
        unlink(file);
 
203
        return result;
 
204
}