-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.c
209 lines (166 loc) · 4.45 KB
/
config.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
#pragma noroot
#pragma lint -1
#pragma optimize -1
#include <control.h>
#include <gsos.h>
#include <memory.h>
#include <quickdraw.h>
#include <resources.h>
#include <stdfile.h>
#include <window.h>
#include <string.h>
#include "qserver.h"
extern Handle rPath;
static Word fd;
/*
* load default values.
* return 0 on failure, anything else on success.
*/
//
Word LoadConfig(Word MyID) {
Word t;
Word oFile;
Word oDepth;
static GSString32 filePath = {28, "*:System:Preferences:QServer"};
static GSString32 folderPath = {21, "*:System:Preferences:"};
static FileInfoRecGS InfoDCB = {12, (GSString255Ptr)&filePath};
static CreateRecGS CreateDCB = {4, (GSString255Ptr)&folderPath, 0xe3, 0x0f,
0};
// 1 - check if file exists
// 2 - if no, create the folder and file
// 3 - load up the data
// 4 - if data doesn't exist, store a default value.
fd = 0;
GetFileInfoGS(&InfoDCB);
t = _toolErr;
if (_toolErr == pathNotFound) {
CreateGS(&CreateDCB);
if (!_toolErr)
t = fileNotFound;
}
if (t == fileNotFound) // file doesn't exist, create
{
CreateResourceFile(0, 0x5A, 0xe3, (Pointer)&filePath);
if (_toolErr)
return 0;
} else if (t)
return 0;
fd = OpenResourceFile(readWriteEnable, NULL, (Pointer)&filePath);
if (_toolErr)
return 0;
// make sure we're the only resource file...
oFile = GetCurResourceFile();
SetCurResourceFile(fd);
oDepth = SetResourceFileDepth(1);
// load the quote path.
rPath = LoadResource(rC1InputString, 1);
if (_toolErr) {
static GSString255 defPath = {34, "*/system/CDEVs/Twilight/QuotesFile"};
rPath = NewHandle(36, MyID, 0, NULL);
if (rPath) {
PtrToHand((Pointer)defPath, rPath, 36);
AddResource(rPath, 0, rC1InputString, 1);
}
}
// restore old resource file...
SetCurResourceFile(oFile);
SetResourceFileDepth(oDepth);
return 1;
}
void UnloadConfig(void) {
if (fd)
CloseResourceFile(fd);
fd = 0;
}
/*
* callback from SFGetFile2. only allows if resource fork found.
*/
#pragma toolparms 1
Word SFFilter(DirEntryRecGS *file) {
if (file->flags & 0x8000)
return displaySelect;
return noSelect;
}
#pragma toolparms 0
static void SetText(WindowPtr win, Word id, void *data) {
GSString255 *gstr = (GSString255 *)data;
CtlRecHndl CtrlHand;
Rect r;
CtrlHand = GetCtlHandleFromID(win, id);
SetCtlMoreFlags(0x1000, CtrlHand);
r = (**CtrlHand).ctlRect;
EraseRect(&r);
if (data) {
SetCtlValue(gstr->length, CtrlHand);
SetCtlTitle(gstr->text, (Handle)CtrlHand);
} else {
SetCtlValue(0, CtrlHand);
SetCtlTitle("", (Handle)CtrlHand);
}
}
void DoConfig(Word MyID) {
static EventRecord event;
WindowPtr win;
Word control;
Word ok;
GrafPortPtr oldPort;
Handle newPath = NULL;
oldPort = GetPort();
memset(&event, 0, sizeof(event));
event.wmTaskMask = 0x001f0004;
win = NewWindow2(NULL, NULL, NULL, NULL, refIsResource, (long)rConfigWindow,
rWindParam1);
SetPort(win);
// set the current path text...
if (rPath) {
HLock(rPath);
SetText(win, CtrlPath, *rPath);
} else {
SetText(win, CtrlPath, NULL);
}
for (ok = true; ok;) {
control = (Word)DoModalWindow(&event, NULL, NULL, NULL, 0x0008);
switch (control) {
case CtrlCancel:
case CtrlOk:
ok = false;
break;
case CtrlBrowse: {
SFReplyRec2 myReply;
myReply.nameRefDesc = refIsNewHandle;
myReply.pathRefDesc = refIsNewHandle;
SFGetFile2(10, 35, refIsPointer, (Ref) "\pSelect quotation file.",
(WordProcPtr)SFFilter, /* filter proc */
NULL, /* type list */
&myReply);
if (myReply.good) {
Handle h;
Word size;
h = (Handle)myReply.pathRef;
HLock(h);
size = GetHandleSize(h) - 2;
BlockMove(*h + 2, *h, size);
ReAllocHandle(size, MyID, 0x8000, NULL, h);
SetText(win, CtrlPath, *h);
if (newPath)
DisposeHandle(newPath);
newPath = h;
DisposeHandle((Handle)myReply.nameRef);
}
} break;
}
}
if (control == CtrlOk) // update the config file.
{
if (newPath) {
Word size = (Word)GetHandleSize(newPath);
ReAllocHandle(size, MyID, 0, NULL, rPath);
HandToHand(newPath, rPath, size);
MarkResourceChange(true, rC1InputString, 1);
}
UpdateResourceFile(fd);
}
CloseWindow(win);
SetPort(oldPort);
HUnlock(rPath);
}