Annotation of Amaya/amaya/templates.c, revision 1.41

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: 
1.39      tollenae  316: void UnlockContentElements (Document doc, Element el)
                    317: {
                    318:   ElementType elType;
                    319: 
                    320:   if (el != NULL) 
                    321:     {
                    322:       elType = TtaGetElementType (el);
                    323:       if (TtaIsLeaf (elType))
                    324:         {
                    325:           /* It's a content element */
                    326:           TtaSetAccessRight(el, ReadWrite, doc);
                    327:         }
                    328:       else
                    329:         {
                    330:           TtaSetAccessRight(el, ReadOnly, doc);
                    331:           UnlockContentElements(doc,TtaGetFirstChild(el));
                    332:         }
                    333:       TtaNextSibling(&el);
                    334:       if (el != NULL)
                    335:         {
                    336:           UnlockContentElements(doc,el);
                    337:         }
                    338:     }
                    339: }
                    340: 
1.32      tollenae  341: /*-----------------------------------------------
                    342: void LockFixedAreas
                    343: Parse the subtree from el, set read-only access to
                    344: the element and his child when it's not a free_struct
                    345: element.
                    346: ------------------------------------------------*/
                    347: 
                    348: void LockFixedAreas (Document doc, Element el)
                    349: {
                    350: #ifdef TEMPLATES
                    351:   ElementType elType;
                    352:   char *s;
                    353:   
                    354:   TtaSetAccessRight (el, ReadOnly, doc);
                    355:   elType = TtaGetElementType(el);
                    356:   s = TtaGetSSchemaName (elType.ElSSchema);
1.36      tollenae  357:   if (TtaGetFirstChild(el)!=NULL)
1.32      tollenae  358:     {
                    359:       /* The element is not a leaf */
1.39      tollenae  360:       if ((strcmp (s,"Template") == 0) &&
1.40      cvs       361:           (elType.ElTypeNum == Template_EL_FREE_STRUCT))
1.39      tollenae  362:         {
                    363:           /* The element has a free structure */
                    364:           UnlockSubtree (doc, el);
                    365:         }
                    366:       else if ((strcmp (s,"Template") == 0) &&
1.40      cvs       367:                (elType.ElTypeNum == Template_EL_FREE_CONTENT))
1.39      tollenae  368:         {
                    369:           /* The element has free content */
                    370:           UnlockContentElements (doc, el);
                    371:         }
                    372:       else       
                    373:         {
                    374:           /* The element has a fixed structure */
                    375:           /* So we look for a free structure in
                    376:              the subtree */
                    377:           LockFixedAreas (doc, TtaGetFirstChild (el));
                    378:         }
1.32      tollenae  379:     }
                    380:   TtaNextSibling (&el);
                    381:   if (el != NULL)
                    382:     /* The element has at least one sibling */
                    383:     LockFixedAreas (doc, el);
                    384: #endif /* TEMPLATES */
                    385: }
1.29      tollenae  386: 
                    387: /*---------------------------------------------------------------
1.32      tollenae  388:   Load a template and create the instance file - update images and 
1.29      tollenae  389:   stylesheets related to the template.
                    390:   ---------------------------------------------------------------*/
                    391: 
                    392: int CreateInstanceOfTemplate (Document doc, char *templatename, char *docname,
1.32      tollenae  393:                              DocumentType docType)
1.29      tollenae  394: {
1.41    ! cvs       395:   Element       el;
1.29      tollenae  396:   char          templateFile[MAX_LENGTH];
                    397:   int           newdoc, len;
                    398:   ThotBool      stopped_flag;
                    399: 
                    400:   W3Loading = doc;
                    401:   BackupDocument = doc;
                    402:   TtaExtractName (templatename, DirectoryName, DocumentName);
                    403:   AddURLInCombobox (docname, NULL, TRUE);
                    404:   newdoc = InitDocAndView (doc,
1.33      tollenae  405:                            !DontReplaceOldDoc /* replaceOldDoc */,
                    406:                            InNewWindow /* inNewWindow */,
1.29      tollenae  407:                            DocumentName, (DocumentType)docType, 0, FALSE,
1.33      tollenae  408:                            L_Other, (ClickEvent)CE_ABSOLUTE);
                    409:   if (newdoc != 0)
1.29      tollenae  410:     {
                    411:       /* load the saved file */
                    412:       W3Loading = newdoc;
1.33      tollenae  413:       
1.29      tollenae  414:       templateFile[0] = EOS;
                    415:       
                    416:       LoadDocument (newdoc, templatename, NULL, NULL, CE_ABSOLUTE,
1.33      tollenae  417:                     "", DocumentName, NULL, FALSE, &DontReplaceOldDoc);
                    418:       
1.32      tollenae  419:       /* Update URLs of linked documents */
                    420:       SetRelativeURLs (newdoc, docname);
1.33      tollenae  421:       
1.29      tollenae  422:       /* change its URL */
                    423:       TtaFreeMemory (DocumentURLs[newdoc]);
                    424:       len = strlen (docname) + 1;
                    425:       DocumentURLs[newdoc] = TtaStrdup (docname);
                    426:       DocumentSource[newdoc] = 0;
                    427:       /* add the URI in the combobox string */
                    428:       AddURLInCombobox (docname, NULL, FALSE);
                    429:       TtaSetTextZone (newdoc, 1, URL_list);
                    430:       /* change its directory name */
                    431:       TtaSetDocumentDirectory (newdoc, DirectoryName);
                    432: 
                    433:       TtaSetDocumentModified (newdoc);
                    434:       W3Loading = 0;           /* loading is complete now */
                    435:       DocNetworkStatus[newdoc] = AMAYA_NET_ACTIVE;
                    436:       stopped_flag = FetchAndDisplayImages (newdoc, AMAYA_LOAD_IMAGE, NULL);
                    437:       if (!stopped_flag)
1.32      tollenae  438:         {
                    439:           DocNetworkStatus[newdoc] = AMAYA_NET_INACTIVE;
                    440:           /* almost one file is restored */
                    441:           TtaSetStatus (newdoc, 1, TtaGetMessage (AMAYA, AM_DOCUMENT_LOADED),
                    442:                           NULL);
                    443:         }
1.29      tollenae  444:       /* check parsing errors */
                    445:       CheckParsingErrors (newdoc);
                    446:       
1.38      tollenae  447:       /* Set elements access rights
                    448:          according to free_* elements */
1.41    ! cvs       449:       el = TtaGetMainRoot (newdoc);
1.38      tollenae  450:       LockFixedAreas (newdoc, el);
                    451: 
                    452:       insertTemplateMeta(newdoc);
1.29      tollenae  453:     }
1.33      tollenae  454:    BackupDocument = 0;
                    455:    return (newdoc);
1.36      tollenae  456: 
1.29      tollenae  457: }
                    458: 
1.37      tollenae  459: void LoadInstanceOfTemplate (Document doc)
                    460: {
                    461:   Element el = TtaGetMainRoot (doc);
                    462:   LockFixedAreas (doc, el);
                    463: }

Webmaster