forked from phoudoin/sanity
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Sanity.cpp
385 lines (312 loc) · 8.81 KB
/
Sanity.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <Application.h>
#include <Window.h>
#include <File.h>
#include <NodeInfo.h>
#include <Alert.h>
#include <TextView.h>
#include <Catalog.h>
#include "Sanity.h"
const char * APPLICATION_SIGNATURE = "application/x-vnd." SOFTWARE_EDITOR "-" SOFTWARE_NAME;
const char * STR_ABOUT_TITLE = "About " SOFTWARE_NAME;
const char * STR_ABOUT_BUTTON = "Hum?";
const char * STR_ABOUT_TEXT = \
SOFTWARE_NAME " " SOFTWARE_VERSION_LABEL "\n" \
"A little SANE front-end for BeOS.\n" \
B_UTF8_COPYRIGHT " 2000-2004 Philippe Houdoin\n" \
"\n" \
"Thanks to:\n" \
"\tChrystelle\n" \
"\tSANE project\n" \
"\tCarlos Hasan's flotsam library\n" \
"\tBe Inc.\n" \
"\tInternet\n" \
"\tLife" B_UTF8_ELLIPSIS "\n";
static void authenticate(
SANE_String_Const resource,
SANE_Char username[SANE_MAX_USERNAME_LEN],
SANE_Char password[SANE_MAX_PASSWORD_LEN]);
// --------------------------------------------------------------
int main()
{
// Create an application instance
Application * app;
app = new Application();
app->Run();
delete app;
return 0;
}
// --------------------------------------------------------------
Application::Application()
: BApplication(APPLICATION_SIGNATURE)
{
sane_init(0, authenticate);
BRect window_rect(50, 50, 780, 580);
m_window = new ScannerWindow(window_rect);
// TODO: add settings for opening last scanner by default
m_device.SetTo("");
}
// -------------------------------------------------
Application::~Application()
{
sane_exit();
}
// -------------------------------------------------
void Application::ReadyToRun( void )
{
OpenSaneWrapper(m_device);
m_window->Show();
}
// -------------------------------------------------
void Application::OpenSaneWrapper(BString dev)
{
if (dev.Length() > 0) {
status_t ret;
wait_for_thread(m_window->DevicesRosterThreadID(), &ret);
BMessenger msgr(m_window);
BMessage setDevMsg(ScannerWindow::SET_DEVICE_BY_NAME_MSG);
setDevMsg.AddString("device", dev.String());
msgr.SendMessage(&setDevMsg);
}
}
// --------------------------------------------------------------
void Application::MessageReceived( BMessage * pMsg )
{
inherited::MessageReceived( pMsg );
}
// --------------------------------------------------------------
void Application::RefsReceived( BMessage * msg )
{
uint32 type;
int32 count;
status_t ret = msg->GetInfo("refs", &type, &count);
if (ret != B_OK || type != B_REF_TYPE)
return;
entry_ref ref;
for (int32 i = 0; i < count; i++) {
if (msg->FindRef("refs", i, &ref) == B_OK) {
BFile file(&ref, B_READ_ONLY);
if (file.InitCheck() == B_OK) {
BNodeInfo finfo(&file);
if (finfo.InitCheck() != B_OK)
return;
char fmime[256];
if (finfo.GetType(fmime) < B_OK)
return;
if (strcmp(fmime, SANE_MIMETYPE))
return;
file.ReadAttrString("sane-device", &m_device);
if (!m_window->IsHidden())
OpenSaneWrapper(m_device);
}
}
return;
}
inherited::MessageReceived(msg);
}
// -------------------------------------------------
void Application::AboutRequested( void )
{
BAlert * about_box;
about_box = new BAlert(STR_ABOUT_TITLE, STR_ABOUT_TEXT, STR_ABOUT_BUTTON);
BTextView *tv = about_box->TextView();
if (tv) {
// Colors grab with Magnify on BeOS R5 deskbar logo
rgb_color red = {203, 0, 51, 255};
rgb_color blue = {0, 51, 152, 255};
tv->SetStylable(true);
char *text = (char*) tv->Text();
char *s = text;
// set first line 20pt bold
s = strchr(text, '\n');
BFont font(be_bold_font);
font.SetSize(16);
tv->SetFontAndColor(0, s-text, &font);
// set all Be in blue and red
s = text;
while ((s = strstr(s, "BeOS")) != NULL) {
int32 i = s - text;
//tv->SetFontAndColor(i, i+2, NULL, 0, &blue);
//tv->SetFontAndColor(i+2, i+4, NULL, 0, &red);
tv->SetFontAndColor(i, i+1, NULL, 0, &blue);
tv->SetFontAndColor(i+1, i+2, NULL, 0, &red);
s += 2;
}
}
about_box->Go();
}
// #pragma mark -
static void authenticate(SANE_String_Const resource,
SANE_Char username[SANE_MAX_USERNAME_LEN],
SANE_Char password[SANE_MAX_PASSWORD_LEN])
{
BString text;
text = "Authentification request:\n\n";
text << "Resource: " << resource << "\n\n";
text << "Feature not yet supported, sorry" B_UTF8_ELLIPSIS;
BAlert *alert = new BAlert("Authentification request",
text.String(), "Bummer!", NULL, NULL, B_WIDTH_AS_USUAL, B_STOP_ALERT);
alert->Go();
/*
char tmp[3 + 128 + SANE_MAX_USERNAME_LEN + SANE_MAX_PASSWORD_LEN], *wipe;
unsigned char md5digest[16];
int md5mode = 0, len, query_user = 1;
FILE *pass_file;
struct stat stat_buf;
*tmp = 0;
if (getenv ("HOME") != NULL)
{
if (strlen (getenv ("HOME")) < 500)
{
sprintf (tmp, "%s/.sane/pass", getenv ("HOME"));
}
}
if ((strlen (tmp) > 0) && (stat (tmp, &stat_buf) == 0))
{
if ((stat_buf.st_mode & 63) != 0)
{
fprintf (stderr, "%s has wrong permissions (use at least 0600)\n",
tmp);
}
else
{
if ((pass_file = fopen (tmp, "r")) != NULL)
{
if (strstr (resource, "$MD5$") != NULL)
len = (strstr (resource, "$MD5$") - resource);
else
len = strlen (resource);
while (fgets (tmp, 512, pass_file))
{
if ((strlen (tmp) > 0) && (tmp[strlen (tmp) - 1] == '\n'))
tmp[strlen (tmp) - 1] = 0;
if ((strlen (tmp) > 0) && (tmp[strlen (tmp) - 1] == '\r'))
tmp[strlen (tmp) - 1] = 0;
if (strchr (tmp, ':') != NULL)
{
if (strchr (strchr (tmp, ':') + 1, ':') != NULL)
{
if (
(strncmp
(strchr (strchr (tmp, ':') + 1, ':') + 1,
resource, len) == 0)
&&
((int) strlen
(strchr (strchr (tmp, ':') + 1, ':') + 1) ==
len))
{
if ((strchr (tmp, ':') - tmp) <
SANE_MAX_USERNAME_LEN)
{
if (
(strchr (strchr (tmp, ':') + 1, ':') -
(strchr (tmp, ':') + 1)) <
SANE_MAX_PASSWORD_LEN)
{
strncpy (username, tmp,
strchr (tmp, ':') - tmp);
username[strchr (tmp, ':') - tmp] = 0;
strncpy (password,
strchr (tmp, ':') + 1,
strchr (strchr (tmp, ':') + 1,
':') -
(strchr (tmp, ':') + 1));
password[strchr
(strchr (tmp, ':') + 1,
':') - (strchr (tmp,
':') + 1)] =
0;
query_user = 0;
break;
}
}
}
}
}
}
fclose (pass_file);
}
}
}
if (strstr (resource, "$MD5$") != NULL)
{
md5mode = 1;
len = (strstr (resource, "$MD5$") - resource);
if (query_user == 1)
fprintf (stderr, "Authentification required for resource %*.*s. "
"Enter username: ", len, len, resource);
}
else
{
if (accept_only_md5_auth != 0)
{
fprintf (stderr, "ERROR: backend requested plain-text password\n");
return;
}
else
{
fprintf (stderr,
"WARNING: backend requested plain-text password\n");
query_user = 1;
}
if (query_user == 1)
fprintf (stderr,
"Authentification required for resource %s. Enter username: ",
resource);
}
if (query_user == 1)
fgets (username, SANE_MAX_USERNAME_LEN, stdin);
if ((strlen (username)) && (username[strlen (username) - 1] == '\n'))
username[strlen (username) - 1] = 0;
if (query_user == 1)
{
#ifndef __BEOS__
strcpy (password, (wipe = getpass ("Enter password: ")));
#endif
memset (wipe, 0, strlen (password));
}
if (md5mode)
{
sprintf (tmp, "%.128s%.*s", (strstr (resource, "$MD5$")) + 5,
SANE_MAX_PASSWORD_LEN - 1, password);
md5_buffer (tmp, strlen (tmp), md5digest);
memset (password, 0, SANE_MAX_PASSWORD_LEN);
sprintf (password, "$MD5$%02x%02x%02x%02x%02x%02x%02x%02x"
"%02x%02x%02x%02x%02x%02x%02x%02x",
md5digest[0], md5digest[1],
md5digest[2], md5digest[3],
md5digest[4], md5digest[5],
md5digest[6], md5digest[7],
md5digest[8], md5digest[9],
md5digest[10], md5digest[11],
md5digest[12], md5digest[13], md5digest[14], md5digest[15]);
}
*/
}
// get the path to the binary ptr points in to
// (can't use GetAppInfo from addons)
status_t GetBinaryPath(char *path, void *ptr)
{
status_t status = B_ERROR;
// we must find the path to the binary this code is from...
// iterate through the the image list to find ourselves
// asking the roster for the app mime might get the wrong path
// if we are used as an addon
image_info ai;
int32 cookie = 0;
while ( B_OK == get_next_image_info(0, &cookie, &ai) )
{
if (((ptr >= (char *)ai.text) && (ptr < (char *)ai.text + ai.text_size))
|| ((ptr >= (char *)ai.data) && (ptr < (char *)ai.data + ai.data_size)))
{
strncpy(path, ai.name, B_PATH_NAME_LENGTH);
path[B_PATH_NAME_LENGTH] = '\0';
return B_OK;
}
}
return B_ERROR;
}