xref: /haiku/src/add-ons/print/transports/lpr/LprTransport.cpp (revision 58481f0f6ef1a61ba07283f012cafbc2ed874ead)
1 // Sun, 18 Jun 2000
2 // Y.Takagi
3 
4 #include <Alert.h>
5 #include <DataIO.h>
6 #include <Message.h>
7 #include <Directory.h>
8 
9 #include <netdb.h>
10 #include <pwd.h>
11 #include <stdio.h>
12 #include <string.h>
13 #include <unistd.h>
14 #include <iomanip>
15 #include <sstream>
16 
17 #include "LpsClient.h"
18 #include "LprSetupDlg.h"
19 #include "LprTransport.h"
20 #include "LprDefs.h"
21 #include "DbgMsg.h"
22 
23 #if (!__MWERKS__)
24 using namespace std;
25 #else
26 #define std
27 #endif
28 
29 LprTransport::LprTransport(BMessage *msg)
30 	: BDataIO()
31 {
32 	__server[0] = '\0';
33 	__queue[0]  = '\0';
34 	__file[0]   = '\0';
35 	__user[0]   = '\0';
36 	__jobid     = 0;
37 	__error     = false;
38 
39 	struct passwd *pwd = getpwuid(geteuid());
40 	if (pwd != NULL && pwd->pw_name != NULL && pwd->pw_name[0])
41 		strcpy(__user, pwd->pw_name);
42 	else
43 		strcpy(__user, "baron");
44 
45 	DUMP_BMESSAGE(msg);
46 
47 	const char *spool_path = msg->FindString(SPOOL_PATH);
48 	if (spool_path && *spool_path) {
49 		BDirectory dir(spool_path);
50 		DUMP_BDIRECTORY(&dir);
51 
52 		dir.ReadAttr(LPR_SERVER_NAME, B_STRING_TYPE, 0, __server, sizeof(__server));
53 		if (__server[0] == '\0') {
54 			LprSetupDlg *dlg = new LprSetupDlg(&dir);
55 			if (dlg->Go() == B_ERROR) {
56 				__error = true;
57 				return;
58 			}
59 		}
60 
61 		dir.ReadAttr(LPR_SERVER_NAME, B_STRING_TYPE, 0, __server, sizeof(__server));
62 		dir.ReadAttr(LPR_QUEUE_NAME,  B_STRING_TYPE, 0, __queue,  sizeof(__queue));
63 		dir.ReadAttr(LPR_JOB_ID,      B_INT32_TYPE,  0, &__jobid, sizeof(__jobid));
64 		__jobid++;
65 		if (__jobid > 255) {
66 			__jobid = 1;
67 		}
68 		dir.WriteAttr(LPR_JOB_ID, B_INT32_TYPE, 0, &__jobid, sizeof(__jobid));
69 
70 		sprintf(__file, "%s/%s@ipp.%ld", spool_path, __user, __jobid);
71 
72 		__fs.open(__file, ios::in | ios::out | ios::binary | ios::trunc);
73 		if (__fs.good()) {
74 			DBGMSG(("spool_file: %s\n", __file));
75 			return;
76 		}
77 	}
78 	__error = true;
79 }
80 
81 LprTransport::~LprTransport()
82 {
83 	char hostname[128];
84 	gethostname(hostname, sizeof(hostname));
85 
86 	ostringstream cfname;
87 	cfname << "cfA" << setw(3) << setfill('0') << __jobid << hostname;
88 
89 	ostringstream dfname;
90 	dfname << "dfA" << setw(3) << setfill('0') << __jobid << hostname;
91 
92 	ostringstream cf;
93 	cf << 'H' << hostname     << '\n';
94 	cf << 'P' << __user << '\n';
95 	cf << 'l' << dfname.str() << '\n';
96 	cf << 'U' << dfname.str() << '\n';
97 
98 	long cfsize = cf.str().length();
99 	long dfsize = __fs.tellg();
100 	__fs.seekg(0, ios::beg);
101 
102 	try {
103 		LpsClient lpr(__server);
104 
105 		lpr.connect();
106 		lpr.receiveJob(__queue);
107 
108 		lpr.receiveControlFile(cfsize, cfname.str().c_str());
109 		lpr.transferData(cf.str().c_str(), cfsize);
110 		lpr.endTransfer();
111 
112 		lpr.receiveDataFile(dfsize, dfname.str().c_str());
113 		lpr.transferData(__fs, dfsize);
114 		lpr.endTransfer();
115 	}
116 
117 	catch (LPSException &err) {
118 		DBGMSG(("error: %s\n", err.what()));
119 		BAlert *alert = new BAlert("", err.what(), "OK");
120 		alert->Go();
121 	}
122 
123 	unlink(__file);
124 }
125 
126 ssize_t LprTransport::Read(void *, size_t)
127 {
128 	return 0;
129 }
130 
131 ssize_t LprTransport::Write(const void *buffer, size_t size)
132 {
133 //	DBGMSG(("write: %d\n", size));
134 	if (!__fs.write((char *)buffer, size)) {
135 		__error = true;
136 		return 0;
137 	}
138 //	return __fs.pcount();
139 	return size;
140 }
141