forked from league/mpack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
macmpack.c
2128 lines (1919 loc) · 53.1 KB
/
macmpack.c
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* macmpack.c -- Mac user interface to mpack routines
*
* (C) Copyright 1993-1995 by Carnegie Mellon University
* All Rights Reserved.
*
* Permission to use, copy, modify, distribute, and sell this software
* and its documentation for any purpose is hereby granted without fee,
* provided that the above copyright notice appear in all copies and
* that both that copyright notice and this permission notice appear in
* supporting documentation, and that the name of Carnegie Mellon University
* not be used in advertising or publicity pertaining to distribution of the
* software without specific, written prior permission. Carnegie
* Mellon University makes no representations about the suitability of
* this software for any purpose. It is provided "as is" without
* express or implied warranty.
*
* CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
* THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
* SOFTWARE.
*
* NOTE: a good GUI requires a lot of work... This needs more.
*/
#include <Folders.h>
#include <Script.h>
#include <GestaltEqu.h>
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <ctype.h>
#include "version.h"
#include "part.h"
#include "macnapp.h"
#include "macmpack.h"
#include "macICTypes.h"
#include "macICAPI.h"
#include "macICKeys.h"
/* ThinkC's internal stdio functions: */
#include <ansi_private.h>
/* window types: */
#define DECODELIST 1
#define PREFWIN 2
/* save watch cursor */
Cursor watch;
/* preferences */
struct pref_folder *pfolder = NULL;
struct mpack_preferences **mpack_prefs = NULL;
static ICInstance icinst = NULL;
/* flag for active help window */
static WindowPtr helpw = NULL;
/* active decode status window */
static na_win *curstatwin = NULL;
short didchat;
/* MacTCP started: -1 = error, 1 = active, 0 = unknown */
static short tcpstart = 0;
/* this is used for opening TEXT files */
SFTypeList textList = { 'TEXT', 0, 0, 0 };
/* next two types are used in the dialog used to select files to decode
*/
typedef struct filelist {
short vRefNum;
long dirID;
PCstr fname[65];
} filelist;
typedef struct listwin {
na_win win;
int count;
filelist **hflist;
ListHandle l;
} listwin;
/* this is the status window for decoding
*/
typedef struct statuswin {
na_win win;
RgnHandle urgn; /* user region */
Rect urect; /* user rectangle */
Rect frect; /* frame rectangle */
short row; /* row at top of scroll area */
short nrow; /* rows of status text */
long size, used; /* bytes of status text & amount used */
Handle text; /* status text */
ControlHandle sb; /* scroll bar control */
short height, ascent; /* font height and ascent */
} statuswin;
/* this is for the encode window
*/
typedef struct encodewin {
nate_win w;
Boolean nateon; /* cursor in Desc edit field */
Boolean useemail; /* sending email */
long partsize; /* max part size (0 = no limit) */
OSType ftype; /* type of file to encode */
FSSpec fspec; /* file to encode */
FSSpec ofile; /* output file */
} encodewin;
/* show progress
*/
typedef struct progresswin {
natcp_win w;
short percent;
} progresswin;
/* send mail
*/
typedef struct mailwin {
progresswin w;
Handle headers; /* email headers */
Handle envelope; /* envelope */
short state; /* state */
short remaining; /* messages remaining */
Boolean sending; /* flag for active SMTP task */
Boolean useemail; /* sending email */
Boolean gothost; /* got the hostname */
long partsize; /* max part size (0 = no limit) */
long dirID; /* ID for temp dir */
OSType ftype; /* type of file to encode */
FSSpec fspec; /* file to be encoded */
FSSpec ofile; /* output file */
FILE *dfile; /* desc file */
PCstr server[257]; /* SMTP server */
PCstr subj[257]; /* subject */
CInfoPBRec cpb;
} mailwin;
/* mailwin states */
#define MS_MACTCP 0 /* Starting MacTCP */
#define MS_GETHOST 1 /* Getting hostname */
#define MS_ENCODE 2 /* Do encoding */
#define MS_SENDING 3 /* Sending email */
/* some prototypes */
void warn(char *str);
void stattext(Str255, unsigned char);
static void do_decodefiles(na_win *);
static void addfile(listwin *, FSSpec *);
static void removefile(listwin *);
static short listclose(na_win *);
static short listmouse(na_win *, Point, short, short);
static short listctrl(na_win *, Point, short, short, ControlHandle);
static short listupdate(na_win *, Boolean);
static short listinit(na_win *,long *);
static short prefsctrl(na_win *, Point, short, short, ControlHandle);
static short prefsinit(na_win *, long *);
static void do_decode(FSSpec *);
static void do_encode(FSSpec *, OSType);
static short mainmenu(struct na_win*, WORD, WORD);
#define dwin ((listwin *) win)
#define swin ((statuswin *) win)
#define ewin ((encodewin *) win)
#define twin ((nate_win *) win)
#define prwin ((progresswin *) win)
#define mwin ((mailwin *) win)
/* Get a FILE* to a Macintosh file
******************************* ###############################
* KLUDGE ALERT! KLUDGE ALERT! * # KLUDGE ALERT! KLUDGE ALERT! #
******************************* ###############################
* Mac files are specified by name/vRefNum/dirID combo, but the portable
* portions of mpack use FILE* to do I/O. We need a way to get an open FILE*
* from a file specified by name/vRefNum/dirID. Here we use the proper Macintosh
* routines to open a file, then hack together a FILE* using ThinkC's internal
* routines. The major trouble is that we have no way to get at the FILE action
* procedure (fp->proc), so we need a sample FILE* to be passed in. Bleargh!
******************************* ###############################
* KLUDGE ALERT! KLUDGE ALERT! * # KLUDGE ALERT! KLUDGE ALERT! #
******************************* ###############################
*/
FILE *Macopen(FILE *sample, Str255 name, short vRefNum, long dirID,
short binary_flag, short res_fork, SignedByte permission)
{
FILE *fp = NULL;
short refnum;
long curEOF;
OSErr err;
if ((!res_fork && (err = HOpen(vRefNum, dirID, name, permission, &refnum)) == noErr)
|| (res_fork && (err = HOpenRF(vRefNum, dirID, name, permission, &refnum)) == noErr)) {
if ((fp = __getfile()) == NULL) {
FSClose(refnum);
} else {
if (permission == fsWrPerm) {
/* if we're writing to the file, truncate it */
SetEOF(refnum, curEOF = 0);
} else {
GetEOF(refnum, &curEOF);
}
fp->refnum = refnum;
fp->len = (fpos_t) curEOF;
fp->binary = binary_flag;
setvbuf(fp, NULL, _IOFBF, BUFSIZ);
fp->proc = sample->proc;
}
}
return (fp);
}
/* warn the user
*/
void warn(char *str)
{
PCstr wstr[257];
CtoPCstrncpy(wstr, str, 255);
ParamText(P(wstr), NULL, NULL, NULL);
NAalert(warnALRT);
}
/* yell at the user
*/
void yell(char *str)
{
PCstr wstr[257];
CtoPCstrncpy(wstr, str, 255);
ParamText(P(wstr), NULL, NULL, NULL);
NAalert(errorALRT);
}
/* chat with user
*/
chat(char *str)
{
PCstr tmpstr[257];
CtoPCstrcpy(tmpstr, str);
stattext(P(tmpstr), 0);
}
/* returns NA_ALLCLOSED if appropriate, else NA_CLOSED
*/
static short alldone(na_win *win)
{
if (win->next == NULL && win->afterp == NULL && (*mpack_prefs)->quit_finished
&& RecoverHandle((Ptr) win) == (Handle) NAhead) {
return (NA_ALLCLOSED);
}
return (NA_CLOSED);
}
/* update procedure for status dialog box
*/
static short statupdate(na_win *win, Boolean newsize)
{
RgnHandle savergn;
unsigned char *s;
short row, top;
Rect tmpr;
FrameRect(&swin->frect);
savergn = NewRgn();
if (savergn) {
GetClip(savergn);
SetClip(swin->urgn);
}
/* redraw text area */
HLock(swin->text);
s = * (unsigned char **) swin->text;
top = swin->urect.top;
for (row = 0; row < swin->row; ++row) {
s += s[1] + 2;
}
for (; row < swin->nrow && top + swin->height <= swin->urect.bottom; ++row) {
MoveTo(swin->urect.left, top + swin->ascent);
if (*s) TextFace(1);
DrawString(s + 1);
if (*s) TextFace(0);
/* advance to next string */
top += swin->height;
s += s[1] + 2;
}
HUnlock(swin->text);
if (savergn) {
SetClip(savergn);
DisposeRgn(savergn);
}
return (NA_NOTPROCESSED);
}
/* refresh status window
*/
void statrefresh()
{
na_win *win = curstatwin;
Draw1Control(swin->sb);
statupdate(win, false);
}
/* add text to the status window
*/
void stattext(Str255 str, unsigned char bold)
{
na_win *win = curstatwin;
short i, len;
unsigned char *s, *start;
RgnHandle rgn;
Rect tmpr;
if (!win) return;
didchat = 1;
/* advance to next row */
if (swin->height * (swin->nrow++ - swin->row)
>= swin->urect.bottom - swin->urect.top) {
SetCtlMax(swin->sb, ++swin->row);
SetCtlValue(swin->sb, swin->row);
if ((rgn = NewRgn()) != NULL) {
tmpr = swin->urect;
ScrollRect(&tmpr, 0, -swin->height, rgn);
DisposeRgn(rgn);
}
}
/* add the text */
len = * (unsigned char *) str;
if (swin->size - swin->used < len + 1) {
SetHandleSize(swin->text, swin->size * 2);
if (MemError() == 0) swin->size *= 2;
}
HLock(swin->text);
s = start = * (unsigned char **) swin->text;
for (i = 1; i < swin->nrow; ++i) {
s += s[1] + 2;
}
if (len + 2 + s < start + swin->size) {
*s = bold;
memcpy(s + 1, str, len + 1);
swin->used = s + len + 2 - start;
}
HUnlock(swin->text);
statupdate(win, false);
}
/* scroll the status dialog
*/
static void statscroll(na_win *win, short rows)
{
RgnHandle rgn;
if ((rgn = NewRgn()) != NULL) {
SetCtlValue(swin->sb, swin->row += rows);
ScrollRect(&swin->urect, 0, - swin->height * rows, rgn);
EraseRgn(rgn);
DisposeRgn(rgn);
}
statupdate(win, false);
}
/* scroll bar procedure
*/
static pascal void statscollbar(ControlHandle ctrlh, short part)
{
na_win *win = (na_win *) GetCRefCon(ctrlh);
short max, new, page;
max = GetCtlMax(ctrlh);
page = (swin->urect.bottom - swin->urect.top) / swin->height - 1;
switch (part) {
case inUpButton:
page = 1;
/* fall through */
case inPageUp:
if (swin->row > 0) {
statscroll(win, - (swin->row < page ? swin->row : page));
}
break;
case inDownButton:
page = 1;
/* fall through */
case inPageDown:
if (swin->row < max) {
statscroll(win, max - swin->row < page ? max - swin->row : page);
}
break;
case inThumb:
break;
}
}
/* control procedure for status dialog box
*/
static short statctrl(na_win *win, Point p, short item, short mods, ControlHandle ctrlh)
{
short value;
if (ctrlh == swin->sb) {
ctrlh = swin->sb;
if (item != inThumb) {
SetCRefCon(ctrlh, (long) win);
TrackControl(ctrlh, p, statscollbar);
} else {
TrackControl(ctrlh, p, nil);
value = GetCtlValue(ctrlh);
if (value != swin->row) statscroll(win, value - swin->row);
}
} else if (item == iOk) {
return (NA_REQCLOSE);
}
return (NA_NOTPROCESSED);
}
/* close procedure for status dialog box
*/
static short statclose(na_win *win)
{
DisposeRgn(swin->urgn);
DisposHandle(swin->text);
DisposeControl(swin->sb);
return (alldone(win));
}
/* init procedure for status dialog box
*/
static short statinit(na_win *win, long *data)
{
Rect tmpr;
FontInfo finfo;
/* disable OK button while working */
NAhiliteDItem(win->pwin, iOk, 255);
/* set up status text area & font */
if ((swin->urgn = NewRgn()) == NULL) return (NA_CLOSED);
TextFont(geneva);
TextSize(9);
GetFontInfo(&finfo);
swin->ascent = finfo.ascent;
swin->height = finfo.ascent + finfo.descent + finfo.leading;
NAgetDRect(win->pwin, iStatus, &swin->frect);
swin->urect = swin->frect;
InsetRect(&swin->urect, 2,
2 + ((swin->urect.bottom - swin->urect.top - 4) % swin->height) / 2);
RectRgn(swin->urgn, &swin->urect);
/* set up text storage */
if ((swin->text = NewHandle(swin->size = 1024)) == NULL) {
DisposeRgn(swin->urgn);
return (NA_CLOSED);
}
**(char **)swin->text = '\0';
/* set up scrollbar */
NAgetDRect(win->pwin, iStatScroll, &tmpr);
swin->sb = NewControl(win->pwin, &tmpr, "\p", true, 0, 0, 0, scrollBarProc, 0);
if (!swin->sb) {
DisposeRgn(swin->urgn);
DisposHandle(swin->text);
return (NA_CLOSED);
}
/* set up procedures */
win->closep = statclose;
win->ctrlp = statctrl;
win->updatep = statupdate;
/* keep window locked until decoding is done */
++win->locks;
curstatwin = win;
return (NA_NOTPROCESSED);
}
/* process the files in the file list
*/
static void do_decodefiles(na_win *win)
{
int count = dwin->count;
filelist *fl;
FILE *dfile, *tmpf;
extern long _ftype, _fcreator;
long ticks;
int result;
MapTypeCreator("text/plain", 0);
SetCursor(&watch);
if (NAwindow(0, NA_DIALOGWINDOW | NA_TITLEBAR | NA_DEFBUTTON | NA_USERESOURCE
| NA_CLOSEBOX | NA_HASCONTROLS,
0, dstatDLOG, 0, sizeof (statuswin), statinit) == NA_CLOSED) {
warn("Not enough memory to decode");
return;
}
MoveHHi((Handle) dwin->hflist);
HLock((Handle) dwin->hflist);
fl = *dwin->hflist;
tmpf = tmpfile();
while (count--) {
stattext(fl->fname, 1);
didchat = 0;
if (dfile = Macopen(tmpf, fl->fname, fl->vRefNum, fl->dirID, 0, 0, 1)) {
result = handleMessage(part_init(dfile), "text/plain",
0, (*mpack_prefs)->extract_text);
if (result != 0 || didchat <= 0) {
if (didchat < 0) {
chat("Decoding cancelled");
} else {
chat("Found nothing to decode");
}
}
fclose(dfile);
} else {
chat("Couldn't find source file");
}
++fl;
}
fclose(tmpf);
HUnlock((Handle) dwin->hflist);
NAhiliteDItem(curstatwin->pwin, iOk, 0);
NAunlockWindow(curstatwin);
curstatwin = NULL;
SetCursor(&arrow);
DisposHandle((Handle) dwin->hflist);
}
/* return non-zero if two filenames have the same prefix
*/
static int fprefixMatch(char *base, PCstr *match)
{
PCstr temp[257];
char *scan;
short prefixlen;
PtoPCstrcpy(temp, base);
scan = C(temp) + PCstrlen(temp) - 1;
while (isdigit(*scan) && scan > C(temp)) --scan;
prefixlen = scan - C(temp) + 1;
if (strncmp(C(temp), C(match), prefixlen)) return (0);
scan = C(match) + prefixlen;
while (isdigit(*scan)) ++scan;
return (!*scan);
}
/* do the add of a file to a list
*/
static void addit(listwin *dw, short vRefNum, long dirID, char *fname)
{
long size = GetHandleSize((Handle) dw->hflist) / sizeof (filelist);
filelist *fl;
char *bp;
Cell c;
int i;
PCstr fbuf[42];
if (size == dw->count) {
SetHandleSize((Handle) dw->hflist, (++size * sizeof (filelist)));
if (MemError() != noErr) return;
}
MoveHHi((Handle) dw->hflist);
HLock((Handle) dw->hflist);
fl = *dw->hflist;
for (i = dw->count; i; --i, ++fl) {
if (fl->vRefNum == vRefNum && fl->dirID == dirID &&
*fl->fname == *fname && !strncmp(C(fl->fname), C(fname), *fl->fname)) {
break;
}
}
if (!i) {
fl->vRefNum = vRefNum;
fl->dirID = dirID;
PtoPCstrcpy(fl->fname, fname);
SetPt(&c, 0, dw->count);
LAddRow(1, ++dw->count, dw->l);
LSetCell((Ptr) C(fname), (short) Pstrlen(fname), c, dw->l);
}
HUnlock((Handle) dw->hflist);
}
/* add file set to file list
*/
static void addfile(dw, fspec)
listwin *dw;
FSSpec *fspec;
{
CInfoPBRec cipbr;
HFileInfo *fpb = (HFileInfo *)&cipbr;
PCstr fbuf[42];
short idx, foundone = 0;
long procid;
/* remove working directory stuff */
if (fspec->parID == 0) {
GetWDInfo(fspec->vRefNum, &fspec->vRefNum, &fspec->parID, &procid);
}
/* loop through directory */
for (idx = 1; ; ++idx) {
fpb->ioVRefNum = fspec->vRefNum;
fpb->ioNamePtr = P(fbuf);
fpb->ioDirID = fspec->parID;
fpb->ioFDirIndex = idx;
if (PBGetCatInfoSync(&cipbr)) break;
SetClen(fbuf);
if (!(fpb->ioFlAttrib & 16) && fprefixMatch((char *)fspec->name, fbuf)) {
addit(dw, fspec->vRefNum, fspec->parID, (char *) P(fbuf));
foundone = 1;
}
}
if (!foundone) {
addit(dw, fspec->vRefNum, fspec->parID, (char *) fspec->name);
}
}
/* remove file from file list
*/
static void removefile(dw)
listwin *dw;
{
filelist *fl;
int count;
Cell c;
c.h = c.v = 0;
if (LGetSelect(TRUE, &c, dw->l)) {
MoveHHi((Handle) dw->hflist);
HLock((Handle) dw->hflist);
fl = *dw->hflist + c.v;
count = dw->count - c.v;
while (--count) {
fl[0] = fl[1];
++fl;
}
HUnlock((Handle) dw->hflist);
--dw->count;
LDelRow(1, c.v, dw->l);
}
}
/* close list window
*/
static short listclose(win)
na_win *win;
{
LDispose(dwin->l);
return (alldone(win));
}
/* mouse procedure
*/
static short listmouse(na_win *win, Point p, short type, short mods)
{
Cell c;
if (!(type & 1)) {
LClick(p, mods, dwin->l);
c.h = c.v = 0;
NAhiliteDItem((DialogPtr)win->pwin, iRemove, LGetSelect(TRUE, &c, dwin->l) ? 0 : 255);
}
return (NA_NOTPROCESSED);
}
/* control procedure
*/
static short listctrl(na_win *win, Point p, short item, short mods, ControlHandle ctrlh)
{
StandardFileReply reply;
switch (item) {
case iAdd:
NAgetFile(NULL, 1, textList, &reply);
if (reply.sfGood) {
if (!dwin->count) {
NAhiliteDItem((DialogPtr)win->pwin, iOk, 0);
}
addfile(dwin, &reply.sfFile);
}
return (NA_PROCESSED);
case iRemove:
removefile(dwin);
NAhiliteDItem((DialogPtr)win->pwin, iRemove, 255);
if (!dwin->count) {
NAhiliteDItem((DialogPtr)win->pwin, iOk, 255);
}
return (NA_PROCESSED);
case iOk:
win->afterp = do_decodefiles;
case iCancel:
return (NA_REQCLOSE);
}
return (NA_NOTPROCESSED);
}
/* update the list window
*/
static short listupdate(na_win *win, Boolean resize)
{
Rect r;
NAgetDRect(win->pwin, iFileList, &r);
FrameRect(&r);
LUpdate(win->pwin->visRgn, dwin->l);
return (NA_NOTPROCESSED);
}
/* initialize the list window
*/
static short listinit(win, data)
na_win *win;
long *data;
{
FSSpec *fspec = (FSSpec *) data;
Rect r, zrect;
Point p;
Handle hand;
short type;
GetDItem((DialogPtr)win->pwin, iFileList, &type, &hand, &r);
InsetRect(&r, 1, 1);
zrect.top = zrect.bottom = zrect.left = p.h = p.v = 0;\
zrect.right = 1;
dwin->l = LNew(&r, &zrect, p, 0, win->pwin, 0, 0, 0, 1);
if (!dwin->l) return (NA_CLOSED);
(*dwin->l)->selFlags = lOnlyOne;
dwin->hflist = (filelist **) NewHandle(sizeof (filelist));
if (!dwin->hflist) {
LDispose(dwin->l);
return (NA_CLOSED);
}
dwin->count = 0;
addfile(dwin, fspec);
win->closep = listclose;
win->updatep = listupdate;
win->ctrlp = listctrl;
win->mousep = listmouse;
win->type = DECODELIST;
NAhiliteDItem((DialogPtr)win->pwin, iRemove, 255);
ShowWindow(win->pwin);
LDoDraw(TRUE, dwin->l);
return (NA_NOTPROCESSED);
}
/* Decode procedure: first get a file, then open decode window
*/
static void do_decode(FSSpec *fspec)
{
StandardFileReply infile;
na_win **wh, *wp;
if (!fspec) {
NAgetFile(NULL, 1, textList, &infile);
if (!infile.sfGood) return;
fspec = &infile.sfFile;
} else {
/* file supplied by drag & drop, look for existing decode window: */
for (wh = NAhead; wh && (*wh)->type != DECODELIST; wh = (*wh)->next);
if (wh && (wp = NAlockWindow(wh)) != NULL) {
addfile((listwin *) wp, fspec);
NAunlockWindow(wp);
return;
}
}
NAwindow(0, NA_DIALOGWINDOW | NA_USERESOURCE | NA_DEFBUTTON |
NA_HASCONTROLS | NA_CLOSEBOX, NULL, decodeDLOG, (long *) fspec,
sizeof (listwin), listinit);
}
/* Map MIME type to/from Macintosh file types
*/
void MapTypeCreator(char *contenttype, OSType type)
{
extern long _ftype, _fcreator;
PCstr tstr[257];
Handle h;
ICAttr attr;
long size = 0;
Ptr map;
ICMapEntry *ment;
unsigned char *scan, *end, *pstr;
short mapcount, i, foundit = 0;
OSType temp;
if (!type) CtoPCstrncpy(tstr, contenttype, 255);
/* first try a lookup via Internet Config */
if (icinst && ICBegin(icinst, icReadOnlyPerm) == noErr) {
if (ICGetPref(icinst, kICMapping, &attr, nil, &size) == noErr
&& size > 0 && (map = NewPtr(size)) != nil) {
if (ICGetPref(icinst, kICMapping, &attr, map, &size) == noErr) {
scan = (unsigned char *) map;
end = scan + size;
while (scan < end) {
ment = (ICMapEntry *) scan;
pstr = scan + ment->fixed_length;
scan += ment->total_length;
if (type && ment->file_type != type) continue;
pstr += *pstr + 1; /* skip over extension */
pstr += *pstr + 1; /* skip over creator app name */
pstr += *pstr + 1; /* skip over post app name */
if (type) {
PtoPCstrcpy((PCstr *) contenttype, (char *) pstr);
foundit = 1;
break;
} else if (EqualString(P(tstr), pstr, false, true)) {
_ftype = ment->file_type;
_fcreator = ment->file_creator;
foundit = 1;
break;
}
}
}
DisposPtr(map);
}
ICEnd(icinst);
}
/* if we didn't find it, try our quick&dirty mappings */
if (!foundit) {
if (type) {
mapcount = CountResources('TyCr');
for (i = 1; i <= mapcount; ++i) {
h = GetIndResource('TyCr', i);
if (h && **(OSType **)h == type) {
GetResInfo(h, &i, &temp, P(contenttype));
if (ResError() == noErr) break;
}
}
SetClen((PCstr *) contenttype);
} else {
h = GetNamedResource('TyCr', P(tstr));
if (h) {
_ftype = (*(OSType **)h)[0];
_fcreator = (*(OSType **)h)[1];
} else {
_ftype = '????';
_fcreator = 'mPAK';
}
}
}
}
/* get internet config string prefs
*/
static short getICprefs(na_win *win, PCstr *eaddr, PCstr *smtphost)
{
char *scan, *end;
ICAttr attr;
long size;
ICError err = noErr;
*C(eaddr) = '\0';
SetPlen(eaddr);
*C(smtphost) = '\0';
SetPlen(smtphost);
if (icinst && ICBegin(icinst, icReadOnlyPerm) == noErr) {
size = 256;
if (ICGetPref(icinst, kICEmail, &attr, (Ptr) eaddr, &size) == noErr
&& win && (attr & ICattr_locked_mask)) {
NAenableDItem(win->pwin, iEmailAddr, 0);
}
SetClen(eaddr);
size = 256;
if (ICGetPref(icinst, kICSMTPHost, &attr, (Ptr) smtphost, &size) == noErr
&& win && (attr & ICattr_locked_mask)) {
NAenableDItem(win->pwin, iMailServer, 0);
}
SetClen(smtphost);
ICEnd(icinst);
} else {
HLock((Handle) mpack_prefs);
end = (char *) (*mpack_prefs) + GetHandleSize((Handle) mpack_prefs);
scan = (*mpack_prefs)->internet_host;
while (scan < end && *scan++);
if (scan < end) CtoPCstrcpy(eaddr, scan);
while (scan < end && *scan++);
if (scan < end) CtoPCstrcpy(smtphost, scan);
HUnlock((Handle) mpack_prefs);
}
}
/* copy desc file, with word-wrap
*/
static short copydesc(FILE *out, TEHandle hTE)
{
char c;
short i, count, word, col, reset;
char **htxt;
count = (*hTE)->teLength;
htxt = (char **) (*hTE)->hText;
for (i = word = col = 0; i < count; ++i) {
c = (*htxt)[i];
reset = i - word == 80 || c == '\r';
if (reset || c == ' ') {
while (word < i) putc((*htxt)[word], out), ++word;
}
if (reset || ++col == 80) {
putc('\r', out);
c = (*htxt)[word];
if (c == ' ' || c == '\r') ++word;
col = 0;
}
}
while (word < i) putc((*htxt)[word], out), ++word;
rewind(out);
}
/* start up MacTCP callback
*/
static void mytcpinit(short status)
{
static DialogPtr dialog = NULL;
GrafPtr save;
Rect box;
if (status < 0) {
tcpstart = -1;
} else if (status == 0) {
tcpstart = 1;
} else {
if (!dialog && status < 100) {
dialog = GetNewDialog(progDLOG, nil, (WindowPtr) -1);
NAsetIText(dialog, iWorkText, "\pWaiting for MacTCP to finish. Press \021. to stop.");
}
if (dialog) {
GetPort(&save);
SetPort(dialog);
NAgetDRect(dialog, iProgress, &box);
FrameRect(&box);
InsetRect(&box, 1, 1);
box.right = box.left + (box.right - box.left) * status / 100;
FillRect(&box, qd.dkGray);
SetPort(save);
if (status == 100) {
DisposDialog(dialog);
dialog = NULL;
}
}
}
}
/* update the progress bar
*/
static short progressupdate(na_win *win, Boolean newsize)
{
Rect box;
if (prwin->percent >= 0) {
NAgetDRect(win->pwin, iProgress, &box);
FrameRect(&box);
InsetRect(&box, 1, 1);
if (prwin->percent) {
box.right = box.left + (box.right - box.left) * prwin->percent / 100;
FillRect(&box, qd.dkGray);
} else {
EraseRect(&box);
}
}
return (NA_NOTPROCESSED);
}
/* handle the cancel button
*/
static short progressctrl(na_win *win, Point p, short item, short mods,
ControlHandle ctrlh)
{
return (item == iCancel ? NA_REQCLOSE : NA_NOTPROCESSED);
}
/* close progress window
*/
static short progressclose(na_win *win)
{
NAmodalMenus(0);
return (NA_CLOSED);