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

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 */
                     17: #define THOT_EXPORT extern
                     18: #include "amaya.h"
                     19: #include "css.h"
                     20: #include "document.h"
                     21: #include "view.h"
1.28    ! tollenae   22: #include "wxdialogapi_f.h"
        !            23: #include "appdialogue_wx.h"
1.1       cvs        24: 
                     25: /* content of the attr name of the meta tag defining the doc's destination URL */
1.18      cvs        26: #define META_TEMPLATE_NAME "AMAYA_TEMPLATE"
1.1       cvs        27: /* URL of the script providing templates (for reload) */
1.18      cvs        28: static char   *script_URL;
1.3       cvs        29: #include "init_f.h"
                     30: 
                     31: 
1.1       cvs        32: /*----------------------------------------------------------------------
1.28    ! tollenae   33:    NewTemplate: Create the new document from template's dialog
1.1       cvs        34:   ----------------------------------------------------------------------*/
1.18      cvs        35: void NewTemplate (Document doc, View view)
1.1       cvs        36: {
1.28    ! tollenae   37:   char *templateDir ;
        !            38:   char *amaya_home ;
        !            39: 
        !            40:   templateDir = (char *) TtaGetMemory (MAX_LENGTH);
        !            41:   amaya_home = TtaGetEnvString ("THOTDIR");
        !            42: 
        !            43:   // Amaya's templates directory (only french yet)
        !            44:   sprintf ((char *)templateDir, "%s%ctemplates%ctemplates%cfr%c", amaya_home,DIR_SEP,DIR_SEP,DIR_SEP,DIR_SEP);
        !            45:   
        !            46:   char s [MAX_LENGTH];
        !            47:   
        !            48:   TtaExtractName (DocumentURLs[doc], s, DocumentName);
        !            49:   strcpy (DirectoryName, s);
        !            50: 
        !            51:   int window_id  = TtaGetDocumentWindowId( doc, view );
        !            52:   ThotWindow p_window = (ThotWindow) TtaGetWindowFromId(window_id);
        !            53: 
        !            54:   
        !            55:   /* Definir un label pour AM_OPEN_TEMPLATE */
1.1       cvs        56: 
1.28    ! tollenae   57:   ThotBool created = CreateNewTemplateDocDlgWX(BaseDialog + OpenTemplate,
        !            58:                                               p_window,
        !            59:                                               doc,
        !            60:                                               TtaGetMessage (AMAYA, AM_OPEN_DOCUMENT),
        !            61:                                               templateDir,
        !            62:                                               s);
        !            63:   
        !            64:   if (created)
1.25      vatton     65:     {
1.28    ! tollenae   66:       TtaSetDialoguePosition ();
        !            67:       TtaShowDialogue (BaseDialog + OpenTemplate, TRUE);
1.25      vatton     68:     }
1.28    ! tollenae   69: 
        !            70: 
1.1       cvs        71: }
1.25      vatton     72: 
1.28    ! tollenae   73: 
        !            74: 
        !            75: 
1.1       cvs        76: /*----------------------------------------------------------------------
                     77:    OpenTemplateDocument: Process the meta of a template document,
                     78:      changes the URL, try to save it and warn the user if it cannot be
                     79:      saved.
                     80:   ----------------------------------------------------------------------*/
1.28    ! tollenae   81: 
1.18      cvs        82: void OpenTemplateDocument (Document doc)
1.1       cvs        83: {
                     84:   ElementType         metaElType;
                     85:   Element             metaEl;
                     86:   Attribute           metaAttr;
                     87:   AttributeType       metaAttrType;
                     88:   ThotBool            found = FALSE;
                     89:   int                 length;
1.24      vatton     90:   char               *utf8path, *path;
                     91:   char               *URLdir, *URLname;
1.1       cvs        92: 
                     93:   metaElType.ElSSchema = TtaGetDocumentSSchema (doc);
                     94:   metaElType.ElTypeNum = HTML_EL_META;
                     95:   metaAttrType.AttrSSchema = TtaGetDocumentSSchema (doc);
                     96:   metaAttrType.AttrTypeNum = HTML_ATTR_meta_name;
                     97:   /* search the meta element */
                     98:   metaEl = TtaGetMainRoot (doc);
1.24      vatton     99:   path = NULL;
1.1       cvs       100:   do
                    101:     {
                    102:       metaEl = TtaSearchTypedElement (metaElType, SearchForward, metaEl);
                    103:       if (metaEl != NULL)
                    104:         {
                    105:           metaAttr = TtaGetAttribute (metaEl, metaAttrType); 
                    106:           if (metaAttr != NULL)
                    107:             {
1.24      vatton    108:              length = TtaGetTextAttributeLength (metaAttr) + 1;
                    109:              utf8path = (char *) TtaGetMemory (length);
                    110:              TtaGiveTextAttributeValue (metaAttr, utf8path, &length);
                    111:              path = (char *)TtaConvertMbsToByte ((unsigned char *)utf8path,
                    112:                                                  TtaGetDefaultCharset ());
                    113:              TtaFreeMemory (utf8path);
                    114:               found = !strcmp (path, META_TEMPLATE_NAME);
1.26      cvs       115:              /* free previous path if necessary */
                    116:           TtaFreeMemory (path);
                    117:                  path = NULL;
1.1       cvs       118:             }
                    119:         }
                    120:     }
1.24      vatton    121:   while (metaEl && !found);
                    122: 
1.1       cvs       123:   if (found)
                    124:     {
                    125:       /* get the url of the document */
                    126:       metaAttrType.AttrTypeNum = HTML_ATTR_meta_content; 
                    127:       metaAttr = TtaGetAttribute (metaEl, metaAttrType);
                    128:       if (metaAttr != NULL)
                    129:        {
1.24      vatton    130:          length = TtaGetTextAttributeLength (metaAttr) + 1;
                    131:          utf8path = (char *) TtaGetMemory (length);
                    132:          TtaGiveTextAttributeValue (metaAttr, utf8path, &length);
                    133:          path = (char *)TtaConvertMbsToByte ((unsigned char *)utf8path,
                    134:                                              TtaGetDefaultCharset ());
                    135:          TtaFreeMemory (utf8path);
1.1       cvs       136:        }
1.24      vatton    137: 
1.1       cvs       138:       /* Delete the meta element */
                    139:       TtaDeleteTree (metaEl, doc);  
1.24      vatton    140:       if (script_URL == NULL)
                    141:        script_URL = TtaStrdup (DocumentURLs[doc]);
                    142:       TtaFreeMemory (DocumentURLs[doc]);
                    143:       DocumentURLs[doc] = TtaStrdup (path);
                    144:       DocumentMeta[doc]->method = CE_TEMPLATE;
                    145:       AddURLInCombobox (DocumentURLs[doc], NULL, TRUE);
                    146:       TtaSetTextZone (doc, 1, URL_list);
                    147:       TtaSetDocumentUnmodified (doc);
                    148:       
1.1       cvs       149:       /* set the document name and dir */
1.24      vatton    150:       URLname = strrchr (path, URL_SEP);
1.5       cvs       151:       if (URLname)
                    152:        {
                    153:          URLname[0] = EOS;
                    154:          URLname++;
1.24      vatton    155:          URLdir = path;
1.5       cvs       156:          TtaSetDocumentDirectory (doc, URLdir);
1.9       cvs       157:          TtaSetDocumentName (doc, URLname);
1.5       cvs       158:          /* SetBrowserEditor(doc); */ 
                    159:        }
                    160:       else
                    161:        {
1.16      cvs       162:          TtaSetDocumentDirectory (doc, "");
1.24      vatton    163:          TtaSetDocumentName (doc, path);
1.5       cvs       164:        }
1.9       cvs       165:       SetWindowTitle (doc, doc, 0);
1.1       cvs       166:     }
1.24      vatton    167:   TtaFreeMemory (path);
1.1       cvs       168: }
                    169:   
                    170: /*----------------------------------------------------------------------
                    171:   ReloadTemplateParams : restores the script URL and method into meta
                    172:   to reload a template
                    173:   ----------------------------------------------------------------------*/
1.18      cvs       174: void ReloadTemplateParams (char **docURL, ClickEvent *method)
1.1       cvs       175: {
                    176:    *method = CE_FORM_GET;
1.5       cvs       177:    TtaFreeMemory (*docURL);
1.17      cvs       178:    *docURL = TtaStrdup (script_URL); 
1.1       cvs       179: }
1.28    ! tollenae  180: 
        !           181: 
        !           182: /*------------------------------------------------------------------------
        !           183:   InitTemplateList : fills template list with HTML files in 
        !           184:   the templates directory
        !           185:   ----------------------------------------------------------------------*/
        !           186: void InitTemplateList ()
        !           187: {
        !           188:   int i, nb,len;
        !           189:   unsigned char *urlstring,c;
        !           190:   char          *app_home;
        !           191:   FILE          *file;
        !           192:   CHARSET       encoding;
        !           193:   
        !           194:   TtaFreeMemory(Template_list);
        !           195:   
        !           196:   urlstring = (unsigned char *) TtaGetMemory (MAX_LENGTH);
        !           197:   
        !           198:   /* open the file list_url.dat into APP_HOME directory */
        !           199:   app_home = TtaGetEnvString ("APP_HOME");
        !           200: 
        !           201:   sprintf ((char *)urlstring, "%s%clist_url_utf8.dat", app_home, DIR_SEP);
        !           202:   encoding = UTF_8;
        !           203:   
        !           204:   file = TtaReadOpen ((char *)urlstring);
        !           205: 
        !           206:   *urlstring = EOS;
        !           207:   if (file)
        !           208:     {
        !           209:       /* get the size of the file */
        !           210:       fseek (file, 0L, 2);     
        !           211:       /* end of the file */
        !           212:       Template_list_len = (ftell (file) * 4) + MAX_URL_list + 4;
        !           213:       Template_list = (char *)TtaGetMemory (Template_list_len);
        !           214:       Template_list[0] = EOS;
        !           215:       fseek (file, 0L, 0);     /* beginning of the file */
        !           216:       /* initialize the list by reading the file */
        !           217:       i = 0;
        !           218:       nb = 0;
        !           219:       while (TtaReadByte (file, &c))
        !           220:        {
        !           221:          if (c == '"')
        !           222:            {
        !           223:              len = 0;
        !           224:              urlstring[len] = EOS;
        !           225:              while (len < MAX_LENGTH && TtaReadByte (file, &c) && c != EOL)
        !           226:                {
        !           227:                  if (c == '"')
        !           228:                    urlstring[len] = EOS;
        !           229:                  else if (c == 13) /* Carriage return */
        !           230:                    urlstring[len] = EOS;
        !           231:                  else
        !           232:                    urlstring[len++] = (char)c;
        !           233:                }
        !           234:              urlstring[len] = EOS;
        !           235:              if (i > 0 && len)
        !           236:                /* add an EOS between two urls */
        !           237:                URL_list[i++] = EOS;
        !           238:              if (len)
        !           239:                {
        !           240:                  nb++;
        !           241:                  strcpy ((char *)&Template_list[i], (char *)urlstring);
        !           242:                  i += len;
        !           243:                }
        !           244:            }
        !           245:        }
        !           246:       Template_list[i + 1] = EOS;
        !           247:       TtaReadClose (file);
        !           248:     }
        !           249:   TtaFreeMemory (urlstring);
        !           250: }

Webmaster