Fixed compatibility of output.
[AROS.git] / external / openurl / library / prefs.c
blob43ef7e84d59a0cf938354eb2d5025689bd9183eb
1 /***************************************************************************
3 openurl.library - universal URL display and browser launcher library
4 Copyright (C) 1998-2005 by Troels Walsted Hansen, et al.
5 Copyright (C) 2005-2013 by openurl.library Open Source Team
7 This library is free software; it has been placed in the public domain
8 and you can freely redistribute it and/or modify it. Please note, however,
9 that some components may be under the LGPL or GPL license.
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15 openurl.library project: http://sourceforge.net/projects/openurllib/
17 $Id$
19 ***************************************************************************/
21 #include "lib.h"
23 #include <prefs/prefhdr.h>
25 #include "debug.h"
27 #define __NOLIBBASE__
28 #include <proto/openurl.h>
30 /**************************************************************************/
32 #define ID_BRWS MAKE_ID('B','R','W','S')
33 #define ID_MLRS MAKE_ID('M','L','R','S')
34 #define ID_FTPS MAKE_ID('F','T','P','S')
35 #define ID_FLGS MAKE_ID('F','L','G','S')
36 #define ID_DEFS MAKE_ID('D','E','F','S')
38 #define BRWS_SIZE (sizeof(struct URL_BrowserNode)-sizeof(struct MinNode))
39 #define MLRS_SIZE (sizeof(struct URL_MailerNode)-sizeof(struct MinNode))
40 #define FTPS_SIZE (sizeof(struct URL_FTPNode)-sizeof(struct MinNode))
41 #define FLGS_SIZE (sizeof(ULONG))
42 #define DEFS_SIZE (4 * sizeof(ULONG))
44 /**************************************************************************/
46 struct URL_Prefs *copyPrefs(struct URL_Prefs *old)
48 struct URL_Prefs *new = NULL;
50 ENTER();
52 if(old->up_Version == PREFS_VERSION)
54 if((new = allocArbitrateVecPooled(sizeof(struct URL_Prefs))) != NULL)
56 ULONG res;
58 new->up_Version = PREFS_VERSION;
59 new->up_Flags = old->up_Flags;
61 NewList((struct List *)&new->up_BrowserList);
62 NewList((struct List *)&new->up_MailerList);
63 NewList((struct List *)&new->up_FTPList);
65 if((res = copyList((struct List *)&new->up_BrowserList,(struct List *)&old->up_BrowserList,sizeof(struct URL_BrowserNode))))
67 new->up_DefShow = old->up_DefShow;
68 new->up_DefBringToFront = old->up_DefBringToFront;
69 new->up_DefNewWindow = old->up_DefNewWindow;
70 new->up_DefLaunch = old->up_DefLaunch;
72 if((res = copyList((struct List *)&new->up_MailerList,(struct List *)&old->up_MailerList,sizeof(struct URL_MailerNode))))
73 res = copyList((struct List *)&new->up_FTPList,(struct List *)&old->up_FTPList,sizeof(struct URL_FTPNode));
76 if(res == FALSE)
78 URL_FreePrefsA(new, NULL);
79 new = NULL;
84 return(new);
85 return new;
88 /**************************************************************************/
90 void initPrefs(struct URL_Prefs *p)
92 ENTER();
94 memset(p,0,sizeof(*p));
96 p->up_Version = PREFS_VERSION;
98 NewList((struct List *)&p->up_BrowserList);
99 NewList((struct List *)&p->up_MailerList);
100 NewList((struct List *)&p->up_FTPList);
102 LEAVE();
105 /**************************************************************************/
107 static void addBrowser(struct URL_Prefs *p, const char *name, const char *path, const char *port, const char *show, const char *toFront, const char *open, const char *openWin)
109 struct URL_BrowserNode *bn;
111 if((bn = allocArbitrateVecPooled(sizeof(*bn))) != NULL)
113 bn->ubn_Flags = UNF_DISABLED;
114 strlcpy(bn->ubn_Name, name, sizeof(bn->ubn_Name));
115 strlcpy(bn->ubn_Path, path, sizeof(bn->ubn_Path));
116 strlcpy(bn->ubn_Port, port, sizeof(bn->ubn_Port));
117 strlcpy(bn->ubn_ShowCmd, show, sizeof(bn->ubn_ShowCmd));
118 strlcpy(bn->ubn_ToFrontCmd, toFront, sizeof(bn->ubn_ToFrontCmd));
119 strlcpy(bn->ubn_OpenURLCmd, open, sizeof(bn->ubn_OpenURLCmd));
120 strlcpy(bn->ubn_OpenURLWCmd, openWin, sizeof(bn->ubn_OpenURLWCmd));
121 AddTail((struct List *)&p->up_BrowserList, (struct Node *)bn);
125 static void addMailer(struct URL_Prefs *p, const char *name, const char *path, const char *port, const char *show, const char *toFront, const char *write)
127 struct URL_MailerNode *mn;
129 if((mn = allocArbitrateVecPooled(sizeof(*mn))) != NULL)
131 mn->umn_Flags = UNF_DISABLED;
132 strlcpy(mn->umn_Name, name, sizeof(mn->umn_Name));
133 strlcpy(mn->umn_Path, path, sizeof(mn->umn_Path));
134 strlcpy(mn->umn_Port, port, sizeof(mn->umn_Port));
135 strlcpy(mn->umn_ShowCmd, show, sizeof(mn->umn_ShowCmd));
136 strlcpy(mn->umn_ToFrontCmd, toFront, sizeof(mn->umn_ToFrontCmd));
137 strlcpy(mn->umn_WriteMailCmd, write, sizeof(mn->umn_WriteMailCmd));
138 AddTail((struct List *)&p->up_MailerList, (struct Node *)mn);
142 static void addFTP(struct URL_Prefs *p, const char *name, const char *path, const char *port, const char *show, const char *toFront, const char *open, const char *openWin)
144 struct URL_FTPNode *fn;
146 if((fn = allocArbitrateVecPooled(sizeof(*fn))) != NULL)
148 fn->ufn_Flags = UNF_DISABLED;
149 strlcpy(fn->ufn_Name, name, sizeof(fn->ufn_Name));
150 strlcpy(fn->ufn_Path, path, sizeof(fn->ufn_Path));
151 strlcpy(fn->ufn_Port, port, sizeof(fn->ufn_Port));
152 strlcpy(fn->ufn_ShowCmd, show, sizeof(fn->ufn_ShowCmd));
153 strlcpy(fn->ufn_ToFrontCmd, toFront, sizeof(fn->ufn_ToFrontCmd));
154 strlcpy(fn->ufn_OpenURLCmd, open, sizeof(fn->ufn_OpenURLCmd));
155 strlcpy(fn->ufn_OpenURLWCmd, openWin, sizeof(fn->ufn_OpenURLWCmd));
156 AddTail((struct List *)&p->up_FTPList, (struct Node *)fn);
160 void setDefaultPrefs(struct URL_Prefs *p)
162 ENTER();
164 initPrefs(p);
166 p->up_Flags = DEF_FLAGS;
168 p->up_DefShow = DEF_DefShow;
169 p->up_DefBringToFront = DEF_DefBringToFront;
170 p->up_DefNewWindow = DEF_DefNewWindow;
171 p->up_DefLaunch = DEF_DefLaunch;
173 // Browsers: OWB
174 addBrowser(p, "OWB", "OWB \"%u\"", "OWB", "", "SCREENTOFRONT", "OPENURL \"%u\"", "OPENURL \"%u\"");
176 // Browsers: MUIOWB
177 addBrowser(p, "MUIOWB", "MUIOWB \"%u\"", "MUIOWB", "SHOW", "SCREENTOFRONT", "OPEN NAME=\"%u\"", "OPEN NAME=\"%u\" NEWPAGE");
179 // Browsers: NetSurf
180 addBrowser(p, "NetSurf", "NetSurf \"%u\"", "NETSURF", "", "TOFRONT", "OPEN \"%u\"", "OPEN \"%u\" NEW");
182 // Browsers: IBrowse
183 addBrowser(p, "IBrowse", "IBrowse \"%u\"", "IBROWSE", "SHOW", "SCREENTOFRONT", "GOTOURL \"%u\"", "NEWWINDOW \"%u\"");
185 // Browsers: AWeb
186 addBrowser(p, "AWeb", "AWeb \"%u\"", "AWEB", "ICONIFY SHOW", "SCREENTOFRONT", "OPEN \"%u\"", "NEW \"%u\"");
188 // Browsers: Voyager
189 addBrowser(p, "Voyager", "V \"%u\"", "VOYAGER", "SHOW", "SCREENTOFRONT", "OPENURL \"%u\"", "OPENURL \"%u\" NEWWIN");
191 // Mailers: YAM
192 addMailer(p, "YAM", "YAM MAILTO=\"%a\" SUBJECT=\"%s\" LETTER=\"%f\"", "YAM", "SHOW", "SCREENTOFRONT", "MAILWRITE;WRITETO \"%a\";WRITESUBJECT \"%s\";WRITEEDITOR \"CLEAR\";WRITEEDITOR \"TEXT %b\"");
194 // Mailers: SimpleMail
195 addMailer(p, "SimpleMail", "SimpleMail MAILTO=\"%a\" SUBJECT=\"%s\"", "SIMPLEMAIL", "SHOW", "SCREENTOFRONT", "MAILWRITE MAILTO=\"%a\" SUBJECT=\"%s\"");
197 // Mailers: MicroDot II
198 addMailer(p, "MicroDot II", "MicroDot TO=\"%a\" SUBJECT=\"%s\" CONTENTS=\"%f\"", "MD", "SHOW", "SCREENTOFRONT", "NEWMSGWINDOW TO=\"%a\" SUBJECT=\"%s\" CONTENTS=\"%f\"");
200 // Mailers: lola
201 addMailer(p, "lola", "lola TO=\"%a\" SUBJECT=\"%s\" TEXT=\"%b\" CX_POPUP CX_POPKEY=\"control alt l\"", "LOLA", "SHOW", "", "FILL TO=\"%a\" SUBJECT=\"%s\" TEXT=\"%b\"");
203 // FTP: AmiFTP
204 addFTP(p, "AmiFTP", "AmiFTP \"%a\"", "AMIFTP", "", "", "", "");
206 // FTP: Pete's FTP
207 addFTP(p, "Pete's FTP", "pftp \"%a\"", "", "", "", "", "");
209 // FTP: AmiTradeCenter
210 addFTP(p, "AmiTradeCenter", "AmiTradeCenter \"%a\"", "ATC_MAIN", "", "", "", "");
212 LEAVE();
215 /**************************************************************************/
217 BOOL savePrefs(CONST_STRPTR filename, struct URL_Prefs *p)
219 struct IFFHandle *iffh;
220 BOOL res = FALSE;
222 ENTER();
224 if((iffh = AllocIFF()) != NULL)
226 if((iffh->iff_Stream = (IPTR)Open(filename, MODE_NEWFILE)) != 0)
228 InitIFFasDOS(iffh);
230 if(OpenIFF(iffh, IFFF_WRITE) == 0)
232 struct PrefHeader prhd;
233 struct URL_BrowserNode *bn;
234 struct URL_MailerNode *mn;
235 struct URL_FTPNode *fn;
237 D(DBF_ALWAYS, "saving prefs to '%s'", filename);
239 if(PushChunk(iffh, ID_PREF, ID_FORM, IFFSIZE_UNKNOWN) != 0)
240 goto fail;
241 if(PushChunk(iffh, ID_PREF, ID_PRHD, sizeof(struct PrefHeader)) != 0)
242 goto fail;
244 prhd.ph_Version = p->up_Version;
245 prhd.ph_Type = 0;
246 prhd.ph_Flags = 0;
248 if(WriteChunkBytes(iffh, &prhd, sizeof(struct PrefHeader)) != sizeof(struct PrefHeader))
249 goto fail;
251 if(PopChunk(iffh) != 0)
252 goto fail;
254 /* write browser nodes */
255 D(DBF_ALWAYS, "saving browsers");
256 for(bn = (struct URL_BrowserNode *)p->up_BrowserList.mlh_Head;
257 bn->ubn_Node.mln_Succ;
258 bn = (struct URL_BrowserNode *)bn->ubn_Node.mln_Succ)
260 // mask out possibly invalid flags
261 bn->ubn_Flags &= UNF_VALID_MASK;
263 if(PushChunk(iffh, ID_PREF, ID_BRWS, BRWS_SIZE) != 0)
264 goto fail;
265 if(WriteChunkBytes(iffh, &bn->ubn_Flags, BRWS_SIZE) != BRWS_SIZE)
266 goto fail;
267 if(PopChunk(iffh) != 0)
268 goto fail;
271 /* write mailer nodes */
272 D(DBF_ALWAYS, "saving mailers");
273 for(mn = (struct URL_MailerNode *)p->up_MailerList.mlh_Head;
274 mn->umn_Node.mln_Succ;
275 mn = (struct URL_MailerNode *)mn->umn_Node.mln_Succ)
277 // mask out possibly invalid flags
278 mn->umn_Flags &= UNF_VALID_MASK;
280 if(PushChunk(iffh, ID_PREF, ID_MLRS, MLRS_SIZE) != 0)
281 goto fail;
282 if(WriteChunkBytes(iffh, &mn->umn_Flags, MLRS_SIZE) != MLRS_SIZE)
283 goto fail;
284 if(PopChunk(iffh) != 0)
285 goto fail;
288 /* write ftp nodes */
289 D(DBF_ALWAYS, "saving ftps");
290 for(fn = (struct URL_FTPNode *)p->up_FTPList.mlh_Head;
291 fn->ufn_Node.mln_Succ;
292 fn = (struct URL_FTPNode *)fn->ufn_Node.mln_Succ)
294 // mask out possibly invalid flags
295 fn->ufn_Flags &= UNF_VALID_MASK;
297 if(PushChunk(iffh, ID_PREF, ID_FTPS, FTPS_SIZE) != 0)
298 goto fail;
299 if(WriteChunkBytes(iffh, &fn->ufn_Flags, FTPS_SIZE) != FTPS_SIZE)
300 goto fail;
301 if(PopChunk(iffh) != 0)
302 goto fail;
305 /* write flags */
306 D(DBF_ALWAYS, "saving flags");
307 // mask out possibly invalid flags
308 p->up_Flags &= UPF_VALID_MASK;
310 if(PushChunk(iffh, ID_PREF, ID_FLGS, FLGS_SIZE) != 0)
311 goto fail;
312 if(WriteChunkBytes(iffh, &p->up_Flags, FLGS_SIZE) != FLGS_SIZE)
313 goto fail;
314 if(PopChunk(iffh) != 0)
315 goto fail;
317 /* write defaults */
318 D(DBF_ALWAYS, "saving defaults");
319 if(PushChunk(iffh, ID_PREF, ID_DEFS, DEFS_SIZE) != 0)
320 goto fail;
321 if(WriteChunkBytes(iffh, &p->up_DefShow, DEFS_SIZE) != DEFS_SIZE)
322 goto fail;
323 if(PopChunk(iffh) != 0)
324 goto fail;
326 /* pop the IFF PREF FORM chunk */
327 if(PopChunk(iffh) != 0)
328 goto fail;
330 res = TRUE;
332 fail:
333 CloseIFF(iffh);
336 Close((BPTR)iffh->iff_Stream);
339 FreeIFF(iffh);
342 if(res == FALSE)
343 DeleteFile(filename);
345 RETURN(res);
346 return res;
349 /**************************************************************************/
351 BOOL loadPrefs(struct URL_Prefs *p,ULONG mode)
353 struct IFFHandle *iffh;
354 BOOL res = FALSE;
356 ENTER();
358 initPrefs(p);
360 if((iffh = AllocIFF()) != NULL)
362 CONST_STRPTR fileName;
363 BPTR file;
365 fileName = (mode==LOADPREFS_ENV) ? DEF_ENV : DEF_ENVARC;
367 if (!(file = Open(fileName,MODE_OLDFILE)))
368 if (mode==LOADPREFS_ENV) file = Open(DEF_ENVARC,MODE_OLDFILE);
370 if((iffh->iff_Stream = (IPTR)file) != 0)
372 InitIFFasDOS(iffh);
374 if (!OpenIFF(iffh,IFFF_READ))
376 struct PrefHeader prhd;
377 struct ContextNode *cn;
379 if (StopChunk(iffh,ID_PREF,ID_PRHD)) goto fail;
380 if (StopChunk(iffh,ID_PREF,ID_DEFS)) goto fail;
381 if (StopChunk(iffh,ID_PREF,ID_FLGS)) goto fail;
382 if (StopChunk(iffh,ID_PREF,ID_FTPS)) goto fail;
383 if (StopChunk(iffh,ID_PREF,ID_MLRS)) goto fail;
384 if (StopChunk(iffh,ID_PREF,ID_BRWS)) goto fail;
386 if (ParseIFF(iffh,IFFPARSE_SCAN)) goto fail;
388 if (!(cn = CurrentChunk(iffh))) goto fail;
390 if ((cn->cn_Type!=ID_PREF) || (cn->cn_ID!=ID_PRHD) ||
391 (cn->cn_Size!=sizeof(struct PrefHeader))) goto fail;
393 if (ReadChunkBytes(iffh,&prhd,cn->cn_Size)!=cn->cn_Size) goto fail;
394 if (prhd.ph_Version>PREFS_VERSION) goto fail;
396 for (;;)
398 LONG error;
400 error = ParseIFF(iffh,IFFPARSE_SCAN);
401 if (error==IFFERR_EOF) break;
402 else if (error) goto fail;
404 if (!(cn = CurrentChunk(iffh))) goto fail;
406 if (cn->cn_Type!=ID_PREF) continue;
408 if ((cn->cn_ID==ID_BRWS) && (cn->cn_Size==BRWS_SIZE))
410 struct URL_BrowserNode *bn;
412 if(!(bn = allocArbitrateVecPooled(sizeof(struct URL_BrowserNode))))
413 goto fail;
415 if (ReadChunkBytes(iffh,&bn->ubn_Flags,cn->cn_Size)!=cn->cn_Size)
417 freeArbitrateVecPooled(bn);
418 goto fail;
421 // mask out possibly invalid flags
422 bn->ubn_Flags &= UNF_VALID_MASK;
424 AddTail((struct List *)(&p->up_BrowserList),(struct Node *)(bn));
426 continue;
429 if ((cn->cn_ID==ID_MLRS) && (cn->cn_Size==MLRS_SIZE))
431 struct URL_MailerNode *mn;
433 if(!(mn = allocArbitrateVecPooled(sizeof(struct URL_MailerNode))))
434 goto fail;
436 if (ReadChunkBytes(iffh,&mn->umn_Flags,cn->cn_Size)!=cn->cn_Size)
438 freeArbitrateVecPooled(mn);
439 goto fail;
442 // mask out possibly invalid flags
443 mn->umn_Flags &= UNF_VALID_MASK;
445 AddTail((struct List *)(&p->up_MailerList),(struct Node *)(mn));
447 continue;
450 if ((cn->cn_ID==ID_FTPS) && (cn->cn_Size==FTPS_SIZE))
452 struct URL_FTPNode *fn;
454 if(!(fn = allocArbitrateVecPooled(sizeof(struct URL_FTPNode))))
455 goto fail;
457 if (ReadChunkBytes(iffh,&fn->ufn_Flags,cn->cn_Size)!=cn->cn_Size)
459 freeArbitrateVecPooled(fn);
460 goto fail;
463 // mask out possibly invalid flags
464 fn->ufn_Flags &= UNF_VALID_MASK;
466 AddTail((struct List *)(&p->up_FTPList),(struct Node *)(fn));
468 continue;
471 if ((cn->cn_ID==ID_FLGS) && (cn->cn_Size==FLGS_SIZE))
473 if (ReadChunkBytes(iffh,&p->up_Flags,cn->cn_Size)!=cn->cn_Size)
474 goto fail;
476 continue;
479 if ((cn->cn_ID==ID_DEFS) && (cn->cn_Size==DEFS_SIZE))
481 if (ReadChunkBytes(iffh,&p->up_DefShow,cn->cn_Size)!=cn->cn_Size)
482 goto fail;
484 continue;
488 // the loaded prefs are not the default ones
489 CLEAR_FLAG(p->up_Flags, UPF_ISDEFAULTS);
491 // mask out possibly invalid flags
492 p->up_Flags &= UPF_VALID_MASK;
494 res = TRUE;
496 fail:
497 CloseIFF(iffh);
500 Close(file);
503 FreeIFF(iffh);
506 RETURN(res);
507 return res;
510 /**************************************************************************/
512 struct URL_Prefs *loadPrefsNotFail(void)
514 struct URL_Prefs *p;
516 ENTER();
518 if((p = allocArbitrateVecPooled(sizeof(*p))) != NULL)
520 if(loadPrefs(p, LOADPREFS_ENV) == FALSE)
521 setDefaultPrefs(p);
524 RETURN(p);
525 return p;
528 /**************************************************************************/