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

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.29      tollenae  260: 
1.32      tollenae  261: /*-----------------------------------------------
                    262: void UnlockSubtree
                    263: Set read/write access to an element and all his
                    264: children
                    265: -----------------------------------------------*/
1.29      tollenae  266: 
1.32      tollenae  267: void UnlockSubtree (Document doc, Element el)
                    268: {
                    269:   TtaSetAccessRight (el, ReadWrite, doc);
                    270:   TtaNextSibling (&el);
                    271:   if (el != NULL)
                    272:     /* The element has at least one sibling */
                    273:     UnlockSubtree (doc, el);
                    274: }
                    275: 
                    276: /*-----------------------------------------------
                    277: void LockFixedAreas
                    278: Parse the subtree from el, set read-only access to
                    279: the element and his child when it's not a free_struct
                    280: element.
                    281: ------------------------------------------------*/
                    282: 
                    283: void LockFixedAreas (Document doc, Element el)
                    284: {
                    285: #ifdef TEMPLATES
                    286:   ElementType elType;
                    287:   char *s;
                    288:   
                    289:   TtaSetAccessRight (el, ReadOnly, doc);
                    290:   elType = TtaGetElementType(el);
                    291:   s = TtaGetSSchemaName (elType.ElSSchema);
                    292:   if (!TtaIsLeaf(elType))
                    293:     {
                    294:       /* The element is not a leaf */
                    295:       if ((strcmp (s,"Template") != 0) ||
1.35    ! tollenae  296:           ((elType.ElTypeNum != Template_EL_free_struct) &&
        !           297:            (elType.ElTypeNum != Template_EL_free_content)))
1.32      tollenae  298:          
                    299:        {
                    300:          /* The element has a fixed structure */
                    301:          /* So we look for a free structure in
                    302:             the subtree */
                    303:          LockFixedAreas (doc, TtaGetFirstChild (el));
                    304:        }
                    305:       else 
                    306:        {
                    307:          /* The element has a free structure or a free content */
1.35    ! tollenae  308:          UnlockSubtree (doc, el);
1.32      tollenae  309:        }
                    310:     }
                    311:   TtaNextSibling (&el);
                    312:   if (el != NULL)
                    313:     /* The element has at least one sibling */
                    314:     LockFixedAreas (doc, el);
                    315: #endif /* TEMPLATES */
                    316: }
1.29      tollenae  317: 
                    318: /*---------------------------------------------------------------
1.32      tollenae  319:   Load a template and create the instance file - update images and 
1.29      tollenae  320:   stylesheets related to the template.
                    321:   ---------------------------------------------------------------*/
                    322: 
                    323: int CreateInstanceOfTemplate (Document doc, char *templatename, char *docname,
1.32      tollenae  324:                              DocumentType docType)
1.29      tollenae  325: {
                    326:   char          templateFile[MAX_LENGTH];
                    327:   int           newdoc, len;
                    328:   ThotBool      stopped_flag;
                    329: 
                    330:   W3Loading = doc;
                    331:   BackupDocument = doc;
                    332:   TtaExtractName (templatename, DirectoryName, DocumentName);
                    333:   AddURLInCombobox (docname, NULL, TRUE);
                    334:   newdoc = InitDocAndView (doc,
1.33      tollenae  335:                            !DontReplaceOldDoc /* replaceOldDoc */,
                    336:                            InNewWindow /* inNewWindow */,
1.29      tollenae  337:                            DocumentName, (DocumentType)docType, 0, FALSE,
1.33      tollenae  338:                            L_Other, (ClickEvent)CE_ABSOLUTE);
                    339:   if (newdoc != 0)
1.29      tollenae  340:     {
                    341:       /* load the saved file */
                    342:       W3Loading = newdoc;
1.33      tollenae  343:       
1.29      tollenae  344:       templateFile[0] = EOS;
                    345:       
                    346:       LoadDocument (newdoc, templatename, NULL, NULL, CE_ABSOLUTE,
1.33      tollenae  347:                     "", DocumentName, NULL, FALSE, &DontReplaceOldDoc);
                    348:       
1.32      tollenae  349:       /* Update URLs of linked documents */
                    350:       SetRelativeURLs (newdoc, docname);
1.33      tollenae  351:       
1.29      tollenae  352:       /* change its URL */
                    353:       TtaFreeMemory (DocumentURLs[newdoc]);
                    354:       len = strlen (docname) + 1;
                    355:       DocumentURLs[newdoc] = TtaStrdup (docname);
                    356:       DocumentSource[newdoc] = 0;
                    357:       /* add the URI in the combobox string */
                    358:       AddURLInCombobox (docname, NULL, FALSE);
                    359:       TtaSetTextZone (newdoc, 1, URL_list);
                    360:       /* change its directory name */
                    361:       TtaSetDocumentDirectory (newdoc, DirectoryName);
                    362: 
                    363:       TtaSetDocumentModified (newdoc);
                    364:       W3Loading = 0;           /* loading is complete now */
                    365:       DocNetworkStatus[newdoc] = AMAYA_NET_ACTIVE;
                    366:       stopped_flag = FetchAndDisplayImages (newdoc, AMAYA_LOAD_IMAGE, NULL);
                    367:       if (!stopped_flag)
1.32      tollenae  368:         {
                    369:           DocNetworkStatus[newdoc] = AMAYA_NET_INACTIVE;
                    370:           /* almost one file is restored */
                    371:           TtaSetStatus (newdoc, 1, TtaGetMessage (AMAYA, AM_DOCUMENT_LOADED),
                    372:                           NULL);
                    373:         }
1.29      tollenae  374:       /* check parsing errors */
                    375:       CheckParsingErrors (newdoc);
                    376:       
1.32      tollenae  377: 
                    378: 
                    379:       
                    380:       /* Set elements access rights
                    381:          according to free_* elements */
                    382:       Element el = TtaGetMainRoot (newdoc);
                    383:       LockFixedAreas (newdoc, el);
1.29      tollenae  384:     }
1.33      tollenae  385:    BackupDocument = 0;
                    386:    return (newdoc);
1.29      tollenae  387: }
                    388: 
                    389: 
1.31      tollenae  390: 

Webmaster