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/
19 ***************************************************************************/
23 #include <prefs/prefhdr.h>
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
;
52 if(old
->up_Version
== PREFS_VERSION
)
54 if((new = allocArbitrateVecPooled(sizeof(struct URL_Prefs
))) != NULL
)
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
));
78 URL_FreePrefsA(new, NULL
);
88 /**************************************************************************/
90 void initPrefs(struct URL_Prefs
*p
)
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
);
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
)
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
;
174 addBrowser(p
, "OWB", "OWB \"%u\"", "OWB", "", "SCREENTOFRONT", "OPENURL \"%u\"", "OPENURL \"%u\"");
177 addBrowser(p
, "MUIOWB", "MUIOWB \"%u\"", "MUIOWB", "SHOW", "SCREENTOFRONT", "OPEN NAME=\"%u\"", "OPEN NAME=\"%u\" NEWPAGE");
180 addBrowser(p
, "NetSurf", "NetSurf \"%u\"", "NETSURF", "", "TOFRONT", "OPEN \"%u\"", "OPEN \"%u\" NEW");
183 addBrowser(p
, "IBrowse", "IBrowse \"%u\"", "IBROWSE", "SHOW", "SCREENTOFRONT", "GOTOURL \"%u\"", "NEWWINDOW \"%u\"");
186 addBrowser(p
, "AWeb", "AWeb \"%u\"", "AWEB", "ICONIFY SHOW", "SCREENTOFRONT", "OPEN \"%u\"", "NEW \"%u\"");
189 addBrowser(p
, "Voyager", "V \"%u\"", "VOYAGER", "SHOW", "SCREENTOFRONT", "OPENURL \"%u\"", "OPENURL \"%u\" NEWWIN");
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\"");
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\"");
204 addFTP(p
, "AmiFTP", "AmiFTP \"%a\"", "AMIFTP", "", "", "", "");
207 addFTP(p
, "Pete's FTP", "pftp \"%a\"", "", "", "", "", "");
209 // FTP: AmiTradeCenter
210 addFTP(p
, "AmiTradeCenter", "AmiTradeCenter \"%a\"", "ATC_MAIN", "", "", "", "");
215 /**************************************************************************/
217 BOOL
savePrefs(CONST_STRPTR filename
, struct URL_Prefs
*p
)
219 struct IFFHandle
*iffh
;
224 if((iffh
= AllocIFF()) != NULL
)
226 if((iffh
->iff_Stream
= (IPTR
)Open(filename
, MODE_NEWFILE
)) != 0)
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)
241 if(PushChunk(iffh
, ID_PREF
, ID_PRHD
, sizeof(struct PrefHeader
)) != 0)
244 prhd
.ph_Version
= p
->up_Version
;
248 if(WriteChunkBytes(iffh
, &prhd
, sizeof(struct PrefHeader
)) != sizeof(struct PrefHeader
))
251 if(PopChunk(iffh
) != 0)
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)
265 if(WriteChunkBytes(iffh
, &bn
->ubn_Flags
, BRWS_SIZE
) != BRWS_SIZE
)
267 if(PopChunk(iffh
) != 0)
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)
282 if(WriteChunkBytes(iffh
, &mn
->umn_Flags
, MLRS_SIZE
) != MLRS_SIZE
)
284 if(PopChunk(iffh
) != 0)
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)
299 if(WriteChunkBytes(iffh
, &fn
->ufn_Flags
, FTPS_SIZE
) != FTPS_SIZE
)
301 if(PopChunk(iffh
) != 0)
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)
312 if(WriteChunkBytes(iffh
, &p
->up_Flags
, FLGS_SIZE
) != FLGS_SIZE
)
314 if(PopChunk(iffh
) != 0)
318 D(DBF_ALWAYS
, "saving defaults");
319 if(PushChunk(iffh
, ID_PREF
, ID_DEFS
, DEFS_SIZE
) != 0)
321 if(WriteChunkBytes(iffh
, &p
->up_DefShow
, DEFS_SIZE
) != DEFS_SIZE
)
323 if(PopChunk(iffh
) != 0)
326 /* pop the IFF PREF FORM chunk */
327 if(PopChunk(iffh
) != 0)
336 Close((BPTR
)iffh
->iff_Stream
);
343 DeleteFile(filename
);
349 /**************************************************************************/
351 BOOL
loadPrefs(struct URL_Prefs
*p
,ULONG mode
)
353 struct IFFHandle
*iffh
;
360 if((iffh
= AllocIFF()) != NULL
)
362 CONST_STRPTR fileName
;
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)
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
;
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
))))
415 if (ReadChunkBytes(iffh
,&bn
->ubn_Flags
,cn
->cn_Size
)!=cn
->cn_Size
)
417 freeArbitrateVecPooled(bn
);
421 // mask out possibly invalid flags
422 bn
->ubn_Flags
&= UNF_VALID_MASK
;
424 AddTail((struct List
*)(&p
->up_BrowserList
),(struct Node
*)(bn
));
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
))))
436 if (ReadChunkBytes(iffh
,&mn
->umn_Flags
,cn
->cn_Size
)!=cn
->cn_Size
)
438 freeArbitrateVecPooled(mn
);
442 // mask out possibly invalid flags
443 mn
->umn_Flags
&= UNF_VALID_MASK
;
445 AddTail((struct List
*)(&p
->up_MailerList
),(struct Node
*)(mn
));
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
))))
457 if (ReadChunkBytes(iffh
,&fn
->ufn_Flags
,cn
->cn_Size
)!=cn
->cn_Size
)
459 freeArbitrateVecPooled(fn
);
463 // mask out possibly invalid flags
464 fn
->ufn_Flags
&= UNF_VALID_MASK
;
466 AddTail((struct List
*)(&p
->up_FTPList
),(struct Node
*)(fn
));
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
)
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
)
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
;
510 /**************************************************************************/
512 struct URL_Prefs
*loadPrefsNotFail(void)
518 if((p
= allocArbitrateVecPooled(sizeof(*p
))) != NULL
)
520 if(loadPrefs(p
, LOADPREFS_ENV
) == FALSE
)
528 /**************************************************************************/