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: ----------------------------------------------------*/ 1.42 ! tollenae 265: void InsertInstanceMeta (Document newdoc) 1.36 tollenae 266: { 1.42 ! tollenae 267: Element head = TtaGetMainRoot (newdoc); 1.36 tollenae 268: Element meta; 269: ElementType elType; 270: AttributeType attrType; 271: Attribute attr; 1.42 ! tollenae 272: char *temp_id, *name; ! 273: int length; ! 274: ThotBool found = false; 1.36 tollenae 275: 276: elType.ElSSchema = TtaGetSSchema ("HTML", newdoc); 277: elType.ElTypeNum = HTML_EL_HEAD; 1.42 ! tollenae 278: head = TtaSearchTypedElement(elType, SearchInTree, head); 1.36 tollenae 279: if (head) 280: { 281: attrType.AttrTypeNum = HTML_ATTR_meta_name; 282: attrType.AttrSSchema = elType.ElSSchema; 1.42 ! tollenae 283: ! 284: elType.ElTypeNum = HTML_EL_META; ! 285: /* Search the template_id meta in the template document */ ! 286: meta = TtaSearchTypedElement(elType, SearchInTree, head); ! 287: while (meta && !found) ! 288: { ! 289: attr = TtaGetAttribute (meta, attrType); ! 290: if (attr != NULL) ! 291: { ! 292: length = TtaGetTextAttributeLength (attr) + 1; ! 293: name = (char *) TtaGetMemory (length); ! 294: TtaGiveTextAttributeValue (attr, name, &length); ! 295: if (!strcmp (name, "template_id")) ! 296: { ! 297: /* The element is the template ID meta */ ! 298: attrType.AttrTypeNum = HTML_ATTR_meta_content; ! 299: attr = TtaGetAttribute (meta, attrType); ! 300: ! 301: if (attr) ! 302: { ! 303: length = TtaGetTextAttributeLength (attr) + 1; ! 304: temp_id = (char *) TtaGetMemory (length); ! 305: TtaGiveTextAttributeValue (attr, temp_id, &length); ! 306: found = True; ! 307: } ! 308: TtaFreeMemory(name); ! 309: } ! 310: } ! 311: meta = TtaSearchTypedElement(elType, SearchForward, meta); ! 312: } ! 313: /* now "temp_id" should contain the content of template_id meta */ 1.36 tollenae 314: 1.42 ! tollenae 315: if (found) ! 316: { ! 317: meta = TtaNewElement (newdoc, elType); ! 318: ! 319: /* Create the "name" attribute */ ! 320: ! 321: attrType.AttrTypeNum = HTML_ATTR_meta_name; ! 322: attr = TtaNewAttribute (attrType); ! 323: TtaAttachAttribute (meta, attr, newdoc); ! 324: TtaSetAttributeText (attr, "template", meta, newdoc); ! 325: ! 326: /* Create the "content" attribute */ ! 327: attrType.AttrTypeNum = HTML_ATTR_meta_content; ! 328: attr = TtaNewAttribute (attrType); ! 329: TtaAttachAttribute (meta, attr, newdoc); ! 330: TtaSetAttributeText (attr, temp_id, meta, newdoc); ! 331: ! 332: TtaInsertFirstChild (&meta, head, newdoc); ! 333: } ! 334: } 1.36 tollenae 335: } 1.29 tollenae 336: 1.32 tollenae 337: /*----------------------------------------------- 338: void UnlockSubtree 339: Set read/write access to an element and all his 340: children 341: -----------------------------------------------*/ 1.29 tollenae 342: 1.32 tollenae 343: void UnlockSubtree (Document doc, Element el) 344: { 345: TtaSetAccessRight (el, ReadWrite, doc); 346: TtaNextSibling (&el); 347: if (el != NULL) 348: /* The element has at least one sibling */ 349: UnlockSubtree (doc, el); 350: } 351: 1.39 tollenae 352: void UnlockContentElements (Document doc, Element el) 353: { 354: ElementType elType; 355: 356: if (el != NULL) 357: { 358: elType = TtaGetElementType (el); 359: if (TtaIsLeaf (elType)) 360: { 361: /* It's a content element */ 362: TtaSetAccessRight(el, ReadWrite, doc); 363: } 364: else 365: { 366: TtaSetAccessRight(el, ReadOnly, doc); 367: UnlockContentElements(doc,TtaGetFirstChild(el)); 368: } 369: TtaNextSibling(&el); 370: if (el != NULL) 371: { 372: UnlockContentElements(doc,el); 373: } 374: } 375: } 376: 1.32 tollenae 377: /*----------------------------------------------- 378: void LockFixedAreas 379: Parse the subtree from el, set read-only access to 380: the element and his child when it's not a free_struct 381: element. 382: ------------------------------------------------*/ 383: 384: void LockFixedAreas (Document doc, Element el) 385: { 386: #ifdef TEMPLATES 387: ElementType elType; 388: char *s; 389: 390: TtaSetAccessRight (el, ReadOnly, doc); 391: elType = TtaGetElementType(el); 392: s = TtaGetSSchemaName (elType.ElSSchema); 1.36 tollenae 393: if (TtaGetFirstChild(el)!=NULL) 1.32 tollenae 394: { 395: /* The element is not a leaf */ 1.39 tollenae 396: if ((strcmp (s,"Template") == 0) && 1.40 cvs 397: (elType.ElTypeNum == Template_EL_FREE_STRUCT)) 1.39 tollenae 398: { 399: /* The element has a free structure */ 400: UnlockSubtree (doc, el); 401: } 402: else if ((strcmp (s,"Template") == 0) && 1.40 cvs 403: (elType.ElTypeNum == Template_EL_FREE_CONTENT)) 1.39 tollenae 404: { 405: /* The element has free content */ 406: UnlockContentElements (doc, el); 407: } 408: else 409: { 410: /* The element has a fixed structure */ 411: /* So we look for a free structure in 412: the subtree */ 413: LockFixedAreas (doc, TtaGetFirstChild (el)); 414: } 1.32 tollenae 415: } 416: TtaNextSibling (&el); 417: if (el != NULL) 418: /* The element has at least one sibling */ 419: LockFixedAreas (doc, el); 420: #endif /* TEMPLATES */ 421: } 1.29 tollenae 422: 423: /*--------------------------------------------------------------- 1.32 tollenae 424: Load a template and create the instance file - update images and 1.29 tollenae 425: stylesheets related to the template. 426: ---------------------------------------------------------------*/ 427: 428: int CreateInstanceOfTemplate (Document doc, char *templatename, char *docname, 1.32 tollenae 429: DocumentType docType) 1.29 tollenae 430: { 1.41 cvs 431: Element el; 1.29 tollenae 432: int newdoc, len; 433: ThotBool stopped_flag; 434: 435: W3Loading = doc; 436: BackupDocument = doc; 437: TtaExtractName (templatename, DirectoryName, DocumentName); 438: AddURLInCombobox (docname, NULL, TRUE); 1.42 ! tollenae 439: // doc = TtaInitDocument("HTML", templatename, 0); 1.29 tollenae 440: newdoc = InitDocAndView (doc, 1.33 tollenae 441: !DontReplaceOldDoc /* replaceOldDoc */, 1.42 ! tollenae 442: InNewWindow, /* inNewWindow */ 1.29 tollenae 443: DocumentName, (DocumentType)docType, 0, FALSE, 1.42 ! tollenae 444: L_Other, (ClickEvent)CE_ABSOLUTE); 1.33 tollenae 445: if (newdoc != 0) 1.29 tollenae 446: { 447: LoadDocument (newdoc, templatename, NULL, NULL, CE_ABSOLUTE, 1.33 tollenae 448: "", DocumentName, NULL, FALSE, &DontReplaceOldDoc); 1.42 ! tollenae 449: ! 450: InsertInstanceMeta(newdoc); 1.33 tollenae 451: 1.32 tollenae 452: /* Update URLs of linked documents */ 453: SetRelativeURLs (newdoc, docname); 1.33 tollenae 454: 1.29 tollenae 455: /* change its URL */ 456: TtaFreeMemory (DocumentURLs[newdoc]); 457: len = strlen (docname) + 1; 458: DocumentURLs[newdoc] = TtaStrdup (docname); 459: DocumentSource[newdoc] = 0; 460: /* add the URI in the combobox string */ 461: AddURLInCombobox (docname, NULL, FALSE); 462: TtaSetTextZone (newdoc, 1, URL_list); 463: /* change its directory name */ 464: TtaSetDocumentDirectory (newdoc, DirectoryName); 465: 466: TtaSetDocumentModified (newdoc); 467: W3Loading = 0; /* loading is complete now */ 468: DocNetworkStatus[newdoc] = AMAYA_NET_ACTIVE; 469: stopped_flag = FetchAndDisplayImages (newdoc, AMAYA_LOAD_IMAGE, NULL); 470: if (!stopped_flag) 1.32 tollenae 471: { 472: DocNetworkStatus[newdoc] = AMAYA_NET_INACTIVE; 473: /* almost one file is restored */ 474: TtaSetStatus (newdoc, 1, TtaGetMessage (AMAYA, AM_DOCUMENT_LOADED), 475: NULL); 476: } 1.29 tollenae 477: /* check parsing errors */ 478: CheckParsingErrors (newdoc); 479: 1.38 tollenae 480: /* Set elements access rights 481: according to free_* elements */ 1.41 cvs 482: el = TtaGetMainRoot (newdoc); 1.38 tollenae 483: LockFixedAreas (newdoc, el); 484: 1.42 ! tollenae 485: 1.29 tollenae 486: } 1.33 tollenae 487: BackupDocument = 0; 488: return (newdoc); 1.36 tollenae 489: 1.29 tollenae 490: } 491: 1.42 ! tollenae 492: /*--------------------------------------------------------------- ! 493: Here we put all the actions to performs when we know a document ! 494: is an instance ! 495: ---------------------------------------------------------------*/ ! 496: 1.37 tollenae 497: void LoadInstanceOfTemplate (Document doc) 498: { 499: Element el = TtaGetMainRoot (doc); 500: LockFixedAreas (doc, el); 501: } 1.42 ! tollenae 502: ! 503: ! 504: /*--------------------------------------------------------------- ! 505: ThotBool isTemplateInstance (Document doc) ! 506: Return true if the document is an instance ! 507: ---------------------------------------------------------------*/ ! 508: ! 509: ThotBool IsTemplateInstance (Document doc) ! 510: { ! 511: Element el = TtaGetMainRoot (doc); ! 512: Element meta; ! 513: ElementType metaType; ! 514: AttributeType attrType; ! 515: Attribute attr; ! 516: ThotBool found = false; ! 517: char* name,* version; ! 518: int length; ! 519: ! 520: /* metaType is first used to find a meta element */ ! 521: metaType.ElSSchema = TtaGetSSchema ("HTML", doc); ! 522: metaType.ElTypeNum = HTML_EL_META; ! 523: meta = TtaSearchTypedElement(metaType, SearchInTree, el); ! 524: ! 525: /* attrType will be used to find the name attribute of a meta */ ! 526: attrType.AttrSSchema = metaType.ElSSchema; ! 527: attrType.AttrTypeNum = HTML_ATTR_meta_name; ! 528: while (meta && !found) ! 529: { ! 530: attr = TtaGetAttribute (meta, attrType); ! 531: if (attr != NULL) ! 532: /* The element contains a name attribute */ ! 533: { ! 534: length = TtaGetTextAttributeLength (attr) + 1; ! 535: name = (char *) TtaGetMemory (length); ! 536: TtaGiveTextAttributeValue (attr, name, &length); ! 537: ! 538: if (!strcmp(name, "template")) ! 539: { ! 540: /* The element is the template meta, the document is a template instance */ ! 541: attrType.AttrTypeNum = HTML_ATTR_meta_content; ! 542: attr = TtaGetAttribute (meta, attrType); ! 543: ! 544: if (attr) ! 545: { ! 546: length = TtaGetTextAttributeLength (attr) + 1; ! 547: version = (char *) TtaGetMemory (length); ! 548: TtaGiveTextAttributeValue (attr, version, &length); ! 549: ! 550: DocumentMeta[doc]->template_version = version; ! 551: TtaFreeMemory (version); ! 552: found = True; ! 553: } ! 554: } ! 555: TtaFreeMemory(name); ! 556: } ! 557: meta = TtaSearchTypedElement(metaType, SearchForward, meta); ! 558: } ! 559: ! 560: return found; ! 561: }