Return to templates.c CVS log | Up to [Public] / Amaya / amaya |
1.1 cvs 1: /* 2: * 1.27 vatton 3: * COPYRIGHT INRIA and W3C, 1996-2005 1.1 cvs 4: * Please first read the full copyright statement in file COPYRIGHT. 5: * 6: */ 1.14 cvs 7: 1.1 cvs 8: /* 9: * Amaya browser functions called form Thot and declared in HTML.A. 10: * These functions concern links and other HTML general features. 11: * 1.15 cvs 12: * Authors: I. Vatton, L. Bonameau, S. Bonhomme (INRIA) 1.1 cvs 13: * 14: */ 15: 16: /* Included headerfiles */ 1.29 tollenae 17: 18: 1.1 cvs 19: #define THOT_EXPORT extern 20: #include "amaya.h" 21: #include "css.h" 22: #include "document.h" 23: #include "view.h" 1.28 tollenae 24: #include "wxdialogapi_f.h" 25: #include "appdialogue_wx.h" 1.29 tollenae 26: #include "init_f.h" 1.32 tollenae 27: #include "parser.h" 1.29 tollenae 28: #include "tree.h" 29: #include "HTMLimage_f.h" 1.32 tollenae 30: #ifdef TEMPLATES 31: #include "Template.h" 32: #endif /* TEMPLATES */ 33: #include "HTMLsave_f.h" 1.1 cvs 34: 35: /* content of the attr name of the meta tag defining the doc's destination URL */ 1.18 cvs 36: #define META_TEMPLATE_NAME "AMAYA_TEMPLATE" 1.1 cvs 37: /* URL of the script providing templates (for reload) */ 1.18 cvs 38: static char *script_URL; 1.3 cvs 39: #include "init_f.h" 40: 41: 1.1 cvs 42: /*---------------------------------------------------------------------- 1.34 tollenae 43: NewTemplate: Create the "new document from template's" dialog 1.1 cvs 44: ----------------------------------------------------------------------*/ 1.18 cvs 45: void NewTemplate (Document doc, View view) 1.1 cvs 46: { 1.31 tollenae 47: #ifdef TEMPLATES 1.28 tollenae 48: char *templateDir ; 49: char *amaya_home ; 50: 51: templateDir = (char *) TtaGetMemory (MAX_LENGTH); 52: amaya_home = TtaGetEnvString ("THOTDIR"); 53: 54: // Amaya's templates directory (only french yet) 1.34 tollenae 55: sprintf ((char *)templateDir, "%s%ctemplates%cfr%c", amaya_home,DIR_SEP,DIR_SEP,DIR_SEP); 1.28 tollenae 56: 57: char s [MAX_LENGTH]; 58: 59: TtaExtractName (DocumentURLs[doc], s, DocumentName); 60: strcpy (DirectoryName, s); 61: 62: int window_id = TtaGetDocumentWindowId( doc, view ); 63: ThotWindow p_window = (ThotWindow) TtaGetWindowFromId(window_id); 64: 65: 66: ThotBool created = CreateNewTemplateDocDlgWX(BaseDialog + OpenTemplate, 67: p_window, 68: doc, 1.31 tollenae 69: TtaGetMessage (AMAYA, AM_NEW_TEMPLATE), 1.28 tollenae 70: templateDir, 71: s); 72: 73: if (created) 1.25 vatton 74: { 1.28 tollenae 75: TtaSetDialoguePosition (); 76: TtaShowDialogue (BaseDialog + OpenTemplate, TRUE); 1.25 vatton 77: } 1.28 tollenae 78: 1.31 tollenae 79: #endif // TEMPLATES 1.1 cvs 80: } 1.25 vatton 81: 1.28 tollenae 82: 83: 84: 1.1 cvs 85: /*---------------------------------------------------------------------- 86: OpenTemplateDocument: Process the meta of a template document, 87: changes the URL, try to save it and warn the user if it cannot be 88: saved. 89: ----------------------------------------------------------------------*/ 1.28 tollenae 90: 1.18 cvs 91: void OpenTemplateDocument (Document doc) 1.1 cvs 92: { 93: ElementType metaElType; 94: Element metaEl; 95: Attribute metaAttr; 96: AttributeType metaAttrType; 97: ThotBool found = FALSE; 98: int length; 1.24 vatton 99: char *utf8path, *path; 100: char *URLdir, *URLname; 1.1 cvs 101: 102: metaElType.ElSSchema = TtaGetDocumentSSchema (doc); 103: metaElType.ElTypeNum = HTML_EL_META; 104: metaAttrType.AttrSSchema = TtaGetDocumentSSchema (doc); 105: metaAttrType.AttrTypeNum = HTML_ATTR_meta_name; 106: /* search the meta element */ 107: metaEl = TtaGetMainRoot (doc); 1.24 vatton 108: path = NULL; 1.1 cvs 109: do 110: { 111: metaEl = TtaSearchTypedElement (metaElType, SearchForward, metaEl); 112: if (metaEl != NULL) 113: { 114: metaAttr = TtaGetAttribute (metaEl, metaAttrType); 115: if (metaAttr != NULL) 116: { 1.24 vatton 117: length = TtaGetTextAttributeLength (metaAttr) + 1; 118: utf8path = (char *) TtaGetMemory (length); 119: TtaGiveTextAttributeValue (metaAttr, utf8path, &length); 120: path = (char *)TtaConvertMbsToByte ((unsigned char *)utf8path, 121: TtaGetDefaultCharset ()); 122: TtaFreeMemory (utf8path); 123: found = !strcmp (path, META_TEMPLATE_NAME); 1.26 cvs 124: /* free previous path if necessary */ 125: TtaFreeMemory (path); 126: path = NULL; 1.1 cvs 127: } 128: } 129: } 1.24 vatton 130: while (metaEl && !found); 131: 1.1 cvs 132: if (found) 133: { 134: /* get the url of the document */ 135: metaAttrType.AttrTypeNum = HTML_ATTR_meta_content; 136: metaAttr = TtaGetAttribute (metaEl, metaAttrType); 137: if (metaAttr != NULL) 138: { 1.24 vatton 139: length = TtaGetTextAttributeLength (metaAttr) + 1; 140: utf8path = (char *) TtaGetMemory (length); 141: TtaGiveTextAttributeValue (metaAttr, utf8path, &length); 142: path = (char *)TtaConvertMbsToByte ((unsigned char *)utf8path, 143: TtaGetDefaultCharset ()); 144: TtaFreeMemory (utf8path); 1.1 cvs 145: } 1.24 vatton 146: 1.1 cvs 147: /* Delete the meta element */ 148: TtaDeleteTree (metaEl, doc); 1.24 vatton 149: if (script_URL == NULL) 150: script_URL = TtaStrdup (DocumentURLs[doc]); 151: TtaFreeMemory (DocumentURLs[doc]); 152: DocumentURLs[doc] = TtaStrdup (path); 153: DocumentMeta[doc]->method = CE_TEMPLATE; 154: AddURLInCombobox (DocumentURLs[doc], NULL, TRUE); 155: TtaSetTextZone (doc, 1, URL_list); 156: TtaSetDocumentUnmodified (doc); 157: 1.1 cvs 158: /* set the document name and dir */ 1.24 vatton 159: URLname = strrchr (path, URL_SEP); 1.5 cvs 160: if (URLname) 161: { 162: URLname[0] = EOS; 163: URLname++; 1.24 vatton 164: URLdir = path; 1.5 cvs 165: TtaSetDocumentDirectory (doc, URLdir); 1.9 cvs 166: TtaSetDocumentName (doc, URLname); 1.5 cvs 167: /* SetBrowserEditor(doc); */ 168: } 169: else 170: { 1.16 cvs 171: TtaSetDocumentDirectory (doc, ""); 1.24 vatton 172: TtaSetDocumentName (doc, path); 1.5 cvs 173: } 1.9 cvs 174: SetWindowTitle (doc, doc, 0); 1.1 cvs 175: } 1.24 vatton 176: TtaFreeMemory (path); 1.1 cvs 177: } 178: 179: /*---------------------------------------------------------------------- 180: ReloadTemplateParams : restores the script URL and method into meta 181: to reload a template 182: ----------------------------------------------------------------------*/ 1.18 cvs 183: void ReloadTemplateParams (char **docURL, ClickEvent *method) 1.1 cvs 184: { 185: *method = CE_FORM_GET; 1.5 cvs 186: TtaFreeMemory (*docURL); 1.17 cvs 187: *docURL = TtaStrdup (script_URL); 1.1 cvs 188: } 1.28 tollenae 189: 190: 191: /*------------------------------------------------------------------------ 192: InitTemplateList : fills template list with HTML files in 193: the templates directory 194: ----------------------------------------------------------------------*/ 195: void InitTemplateList () 196: { 197: int i, nb,len; 198: unsigned char *urlstring,c; 199: char *app_home; 200: FILE *file; 201: CHARSET encoding; 202: 203: TtaFreeMemory(Template_list); 204: 205: urlstring = (unsigned char *) TtaGetMemory (MAX_LENGTH); 206: 207: /* open the file list_url.dat into APP_HOME directory */ 208: app_home = TtaGetEnvString ("APP_HOME"); 209: 210: sprintf ((char *)urlstring, "%s%clist_url_utf8.dat", app_home, DIR_SEP); 211: encoding = UTF_8; 212: 213: file = TtaReadOpen ((char *)urlstring); 214: 215: *urlstring = EOS; 216: if (file) 217: { 218: /* get the size of the file */ 219: fseek (file, 0L, 2); 220: /* end of the file */ 1.30 gully 221: Template_list_len = (ftell (file) * 4) + GetMaxURLList() + 4; 1.28 tollenae 222: Template_list = (char *)TtaGetMemory (Template_list_len); 223: Template_list[0] = EOS; 224: fseek (file, 0L, 0); /* beginning of the file */ 225: /* initialize the list by reading the file */ 226: i = 0; 227: nb = 0; 228: while (TtaReadByte (file, &c)) 229: { 230: if (c == '"') 231: { 232: len = 0; 233: urlstring[len] = EOS; 234: while (len < MAX_LENGTH && TtaReadByte (file, &c) && c != EOL) 235: { 236: if (c == '"') 237: urlstring[len] = EOS; 238: else if (c == 13) /* Carriage return */ 239: urlstring[len] = EOS; 240: else 241: urlstring[len++] = (char)c; 242: } 243: urlstring[len] = EOS; 244: if (i > 0 && len) 245: /* add an EOS between two urls */ 246: URL_list[i++] = EOS; 247: if (len) 248: { 249: nb++; 250: strcpy ((char *)&Template_list[i], (char *)urlstring); 251: i += len; 252: } 253: } 254: } 255: Template_list[i + 1] = EOS; 256: TtaReadClose (file); 257: } 258: TtaFreeMemory (urlstring); 259: } 1.37 tollenae 260: 1.36 tollenae 261: /*------------------------------------------------- 262: Insert the meta element identifying the template's 263: instance 264: ----------------------------------------------------*/ 265: void insertTemplateMeta (Document newdoc) 266: { 267: Element el = TtaGetMainRoot (newdoc); 268: Element meta; 1.37 tollenae 269: Element head; 1.36 tollenae 270: ElementType elType; 271: AttributeType attrType; 272: Attribute attr; 273: 274: elType.ElSSchema = TtaGetSSchema ("HTML", newdoc); 275: elType.ElTypeNum = HTML_EL_HEAD; 276: head = TtaSearchTypedElement(elType, SearchInTree, el); 277: if (head) 278: { 279: el = TtaGetFirstChild(head); 280: elType.ElTypeNum = HTML_EL_META; 281: meta = TtaNewElement (newdoc, elType); 282: 283: /* Create the "name" attribute */ 284: attrType.AttrTypeNum = HTML_ATTR_meta_name; 285: attrType.AttrSSchema = elType.ElSSchema; 286: attr = TtaNewAttribute (attrType); 287: TtaAttachAttribute (meta, attr, newdoc); 288: TtaSetAttributeText (attr, "template", meta, newdoc); 289: 290: /* Create the "content" attribute */ 291: attrType.AttrTypeNum = HTML_ATTR_meta_content; 292: attr = TtaNewAttribute (attrType); 293: TtaAttachAttribute (meta, attr, newdoc); 294: TtaSetAttributeText (attr, "version-modele", meta, newdoc); 295: 296: TtaInsertFirstChild (&meta, head, newdoc); 297: } 298: 299: } 1.29 tollenae 300: 1.32 tollenae 301: /*----------------------------------------------- 302: void UnlockSubtree 303: Set read/write access to an element and all his 304: children 305: -----------------------------------------------*/ 1.29 tollenae 306: 1.32 tollenae 307: void UnlockSubtree (Document doc, Element el) 308: { 309: TtaSetAccessRight (el, ReadWrite, doc); 310: TtaNextSibling (&el); 311: if (el != NULL) 312: /* The element has at least one sibling */ 313: UnlockSubtree (doc, el); 314: } 315: 316: /*----------------------------------------------- 317: void LockFixedAreas 318: Parse the subtree from el, set read-only access to 319: the element and his child when it's not a free_struct 320: element. 321: ------------------------------------------------*/ 322: 323: void LockFixedAreas (Document doc, Element el) 324: { 325: #ifdef TEMPLATES 326: ElementType elType; 327: char *s; 328: 329: TtaSetAccessRight (el, ReadOnly, doc); 330: elType = TtaGetElementType(el); 331: s = TtaGetSSchemaName (elType.ElSSchema); 1.36 tollenae 332: if (TtaGetFirstChild(el)!=NULL) 1.32 tollenae 333: { 334: /* The element is not a leaf */ 335: if ((strcmp (s,"Template") != 0) || 1.35 tollenae 336: ((elType.ElTypeNum != Template_EL_free_struct) && 337: (elType.ElTypeNum != Template_EL_free_content))) 1.32 tollenae 338: 339: { 340: /* The element has a fixed structure */ 341: /* So we look for a free structure in 342: the subtree */ 343: LockFixedAreas (doc, TtaGetFirstChild (el)); 344: } 345: else 346: { 347: /* The element has a free structure or a free content */ 1.35 tollenae 348: UnlockSubtree (doc, el); 1.32 tollenae 349: } 350: } 351: TtaNextSibling (&el); 352: if (el != NULL) 353: /* The element has at least one sibling */ 354: LockFixedAreas (doc, el); 355: #endif /* TEMPLATES */ 356: } 1.29 tollenae 357: 358: /*--------------------------------------------------------------- 1.32 tollenae 359: Load a template and create the instance file - update images and 1.29 tollenae 360: stylesheets related to the template. 361: ---------------------------------------------------------------*/ 362: 363: int CreateInstanceOfTemplate (Document doc, char *templatename, char *docname, 1.32 tollenae 364: DocumentType docType) 1.29 tollenae 365: { 366: char templateFile[MAX_LENGTH]; 367: int newdoc, len; 368: ThotBool stopped_flag; 369: 370: W3Loading = doc; 371: BackupDocument = doc; 372: TtaExtractName (templatename, DirectoryName, DocumentName); 373: AddURLInCombobox (docname, NULL, TRUE); 374: newdoc = InitDocAndView (doc, 1.33 tollenae 375: !DontReplaceOldDoc /* replaceOldDoc */, 376: InNewWindow /* inNewWindow */, 1.29 tollenae 377: DocumentName, (DocumentType)docType, 0, FALSE, 1.33 tollenae 378: L_Other, (ClickEvent)CE_ABSOLUTE); 379: if (newdoc != 0) 1.29 tollenae 380: { 381: /* load the saved file */ 382: W3Loading = newdoc; 1.33 tollenae 383: 1.29 tollenae 384: templateFile[0] = EOS; 385: 386: LoadDocument (newdoc, templatename, NULL, NULL, CE_ABSOLUTE, 1.33 tollenae 387: "", DocumentName, NULL, FALSE, &DontReplaceOldDoc); 388: 1.32 tollenae 389: /* Update URLs of linked documents */ 390: SetRelativeURLs (newdoc, docname); 1.33 tollenae 391: 1.29 tollenae 392: /* change its URL */ 393: TtaFreeMemory (DocumentURLs[newdoc]); 394: len = strlen (docname) + 1; 395: DocumentURLs[newdoc] = TtaStrdup (docname); 396: DocumentSource[newdoc] = 0; 397: /* add the URI in the combobox string */ 398: AddURLInCombobox (docname, NULL, FALSE); 399: TtaSetTextZone (newdoc, 1, URL_list); 400: /* change its directory name */ 401: TtaSetDocumentDirectory (newdoc, DirectoryName); 402: 403: TtaSetDocumentModified (newdoc); 404: W3Loading = 0; /* loading is complete now */ 405: DocNetworkStatus[newdoc] = AMAYA_NET_ACTIVE; 406: stopped_flag = FetchAndDisplayImages (newdoc, AMAYA_LOAD_IMAGE, NULL); 407: if (!stopped_flag) 1.32 tollenae 408: { 409: DocNetworkStatus[newdoc] = AMAYA_NET_INACTIVE; 410: /* almost one file is restored */ 411: TtaSetStatus (newdoc, 1, TtaGetMessage (AMAYA, AM_DOCUMENT_LOADED), 412: NULL); 413: } 1.29 tollenae 414: /* check parsing errors */ 415: CheckParsingErrors (newdoc); 416: 1.38 ! tollenae 417: /* Set elements access rights ! 418: according to free_* elements */ ! 419: Element el = TtaGetMainRoot (newdoc); ! 420: LockFixedAreas (newdoc, el); ! 421: ! 422: insertTemplateMeta(newdoc); 1.29 tollenae 423: } 1.33 tollenae 424: BackupDocument = 0; 425: return (newdoc); 1.36 tollenae 426: 1.29 tollenae 427: } 428: 1.37 tollenae 429: void LoadInstanceOfTemplate (Document doc) 430: { 431: Element el = TtaGetMainRoot (doc); 432: LockFixedAreas (doc, el); 433: }