home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / xfe / e_kit.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  10.3 KB  |  465 lines

  1. /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18. /**********************************************************************
  19.  e_kit.c
  20.  By Daniel Malmer
  21.  
  22.  Implementation for client customization.
  23. **********************************************************************/
  24.  
  25.  
  26. #include "mozilla.h"
  27. #include "xfe.h"
  28. #include "xpgetstr.h"
  29. #include "e_kit.h"
  30. #include "e_kit_patch.h"
  31. #include "prefapi.h"
  32. #include "AnimationType.h"
  33.  
  34. /* Messages declared in xfe_err.h */
  35. extern int XFE_EKIT_LOCK_FILE_NOT_FOUND;
  36. extern int XFE_EKIT_MODIFIED;
  37. extern int XFE_EKIT_CANT_OPEN;
  38. extern int XFE_EKIT_FILESIZE;
  39. extern int XFE_EKIT_CANT_READ;
  40. extern int XFE_ANIM_CANT_OPEN;
  41. extern int XFE_ANIM_READING_SIZES;
  42. extern int XFE_ANIM_READING_NUM_COLORS;
  43. extern int XFE_ANIM_READING_COLORS;
  44. extern int XFE_ANIM_READING_FRAMES;
  45. extern int XFE_ANIM_NOT_AT_EOF;
  46. extern int XFE_EKIT_ABOUT_MESSAGE;
  47.  
  48. static char* get_database_filename(Widget top_level);
  49. static XP_Bool read_lock_file(Widget top_level);
  50.  
  51. static int ekit_loaded = 0;
  52.  
  53. struct fe_icon_data* anim_custom_large = NULL;
  54. struct fe_icon_data* anim_custom_small = NULL;
  55.  
  56. static int ekit_errno = 0;
  57.  
  58. /*
  59.  * ekit_error
  60.  * Pops up an error dialog if an ekit error has occured.
  61.  * Error strings are found in 'xfe_err.h'.
  62.  */
  63. static void
  64. ekit_error(void)
  65. {
  66.   if ( ekit_errno ) {
  67.     FE_Alert(NULL, XP_GetString(ekit_errno));
  68.   }
  69. }
  70.  
  71.  
  72. /*
  73.  * ekit_Initialize
  74.  */
  75. void
  76. ekit_Initialize(Widget toplevel)
  77. {
  78.     /* If this is an e-kit version, modify resource database */
  79.     if ( (ekit_loaded = read_lock_file(toplevel)) == False ) {
  80.         if ( ekit_required() ) {
  81.             ekit_error();
  82.             exit(0);
  83.         }
  84.     }
  85.  
  86.     ekit_LoadCustomAnimation();
  87.  
  88.     if ( ekit_AutoconfigUrl() ) {
  89.       NET_SetNoProxyFailover();
  90.     }
  91. }
  92.  
  93.  
  94.  
  95.  
  96. #if 0
  97. /*
  98.  * ekit_LockPrefs
  99.  * First, set resources back to their default values.
  100.  * If this is an Enterprise Kit binary, try to read in
  101.  * the lock file.  If it doesn't exist, or has been changed,
  102.  * complain and die.
  103.  */
  104. void
  105. ekit_LockPrefs(void)
  106. {
  107.     if ( ekit_Enabled() == FALSE || ekit_loaded == FALSE ) return;
  108.  
  109.     update_preferences();
  110.  
  111.     if ( ekit_AnimationFile() ) {
  112.       if ( load_animation(ekit_AnimationFile()) == False ) {
  113.         ekit_error();
  114.       }
  115.     }
  116.  
  117.     if ( ekit_AutoconfigUrl() ) {
  118.       NET_SetNoProxyFailover();
  119.     }
  120. }
  121. #endif
  122.  
  123.  
  124. /*
  125.  * ekit_UserAgent
  126.  */
  127. char*
  128. ekit_UserAgent(void)
  129. {
  130.     char* user_agent = NULL;
  131.  
  132.     PREF_CopyCharPref("user_agent", &user_agent);
  133.     
  134.     return user_agent;
  135. }
  136.  
  137.  
  138. /*
  139.  * ekit_LogoUrl
  140.  */
  141. char*
  142. ekit_LogoUrl(void)
  143. {
  144.     char* logo_url = NULL;
  145.  
  146.     PREF_CopyCharPref("toolbar.logo.url", &logo_url);
  147.     
  148.     return logo_url;
  149. }
  150.  
  151.  
  152. /*
  153.  * ekit_AnimationFile
  154.  */
  155. char*
  156. ekit_AnimationFile(void)
  157. {
  158.     char* animation_file = NULL;
  159.  
  160.     PREF_CopyCharPref("x_animation_file", &animation_file);
  161.     
  162.     return animation_file;
  163. }
  164.  
  165.  
  166. /*
  167.  * ekit_Enabled
  168.  */
  169. XP_Bool
  170. ekit_Enabled(void)
  171. {
  172.     return TRUE;
  173. }
  174.  
  175.  
  176. /*
  177.  * ekit_AutoconfigUrl
  178.  */
  179. char*
  180. ekit_AutoconfigUrl(void)
  181. {
  182.     char* autoconfig_url = NULL;
  183.  
  184.     if ( fe_IsPolarisInstalled() ) {
  185.         PREF_CopyCharPref("autoadmin.global_config_url", &autoconfig_url);
  186.     }
  187.     
  188.     return autoconfig_url;
  189. }
  190.  
  191.  
  192. /*
  193.  * get_database_filename
  194.  * Looks for the Netscape.cfg file.
  195.  * "path" represents the search path for the config file.
  196.  * %E is replaced with the root directory that is specified
  197.  * at configure time by the e-kit locker, which is a binary
  198.  * patch program.
  199.  */
  200. static char*
  201. get_database_filename(Widget top_level)
  202. {
  203.     String filename;
  204.     SubstitutionRec sub = {'E', NULL};
  205.     Cardinal num_substitutions = 1;
  206.     String path = "%E/%L/%T/%N%C%S:"
  207.                   "%E/%l/%T/%N%C%S:"
  208.                   "%E/%T/%N%C%S:"
  209.                   "%E/%L/%T/%N%S:"
  210.                   "%E/%l/%T/%N%S:"
  211.                   "%E/%T/%N%S";
  212.  
  213.     sub.substitution = ekit_root();
  214.  
  215.     filename = XtResolvePathname(XtDisplay(top_level),
  216.                                  "app-defaults",
  217.                                  "netscape",
  218.                                  ".cfg",
  219.                                  path,
  220.                                  &sub,
  221.                                  num_substitutions,
  222.                                  NULL);
  223.  
  224.     return filename;
  225. }
  226.  
  227.  
  228. /*
  229.  * read_lock_file
  230.  * If there is a locked resource file 'netscape.cfg', read it in.
  231.  */
  232. static XP_Bool
  233. read_lock_file(Widget top_level)
  234. {
  235.     String filename;
  236.  
  237.     if ( (filename = get_database_filename(top_level)) == NULL ) {
  238.         return False;
  239.     }
  240.  
  241.     return ( PREF_ReadLockFile(filename) == PREF_NOERROR );
  242. }
  243.  
  244.  
  245. /*
  246.  * ekit_AboutData
  247.  */
  248. char*
  249. ekit_AboutData(void)
  250. {
  251.     return NULL;
  252. }
  253.  
  254.  
  255. /*
  256.  * ekit_LoadCustomUrl
  257.  */
  258. void
  259. ekit_LoadCustomUrl(char* prefix, MWContext* context)
  260. {
  261.     char* url;
  262.  
  263.     if ( PREF_GetUrl(prefix, &url) ) {
  264.         FE_GetURL(context, NET_CreateURLStruct(url, FALSE));
  265.     }
  266. }
  267.  
  268.  
  269. /*
  270.  * read_frames
  271.  * Reads animation frames from the animation input file.
  272.  */
  273. static int
  274. read_frames(FILE* file, int num_frames, int width, int height,
  275.                         struct fe_icon_data* anim)
  276. {
  277.     int i;
  278.     int j;
  279.     int n = width*height;
  280.     int bytes_per_line = (width+7)/8;
  281.     int total_bytes = bytes_per_line*height;
  282.  
  283.     for ( i = 0; i < num_frames; i++ ) {
  284.         anim[i].width = width;
  285.         anim[i].height = height;
  286.  
  287.         anim[i].mono_bits = (uint8*) malloc(total_bytes);
  288.         anim[i].mask_bits = (uint8*) malloc(total_bytes);
  289.         anim[i].color_bits = (uint8*) malloc(n);
  290.  
  291.         if ( anim[i].mono_bits == NULL ||
  292.              anim[i].mask_bits == NULL ||
  293.              anim[i].color_bits == NULL ) return -1;
  294.  
  295.         if ( fread(anim[i].mono_bits, sizeof(uint8), total_bytes, file) !=
  296.              total_bytes ) {
  297.             return -1;
  298.         }
  299.  
  300.         if ( fread(anim[i].mask_bits, sizeof(uint8), total_bytes, file) !=
  301.              total_bytes ) {
  302.             return -1;
  303.         }
  304.  
  305.         if ( fread(anim[i].color_bits, sizeof(uint8), n, file) != n ) {
  306.             return -1;
  307.         }
  308.  
  309.         for ( j = 0; j < width*height; j++ ) {
  310.             anim[i].color_bits[j]+= fe_n_icon_colors;
  311.         }
  312.     }
  313.  
  314.     return 0;
  315. }
  316.  
  317.  
  318. /*
  319.  * read_colors
  320.  * Reads color data from the animation data file.
  321.  */
  322. static int
  323. read_colors(FILE* file, int num_colors)
  324. {
  325.     int i;
  326.     uint16 r;
  327.     uint16 g;
  328.     uint16 b;
  329.  
  330.     for ( i = 0; i < num_colors; i++ ) {
  331.         if ( fscanf(file, " %hx %hx %hx", &r, &g, &b) != 3 ) {
  332.             return -1;
  333.         }
  334.         fe_icon_colors[fe_n_icon_colors+i][0] = r;
  335.         fe_icon_colors[fe_n_icon_colors+i][1] = g;
  336.         fe_icon_colors[fe_n_icon_colors+i][2] = b;
  337.     }
  338.  
  339.     if ( getc(file) != '\n' ) {
  340.       return -1;
  341.     }
  342.  
  343.     return 0;
  344. }
  345.  
  346.  
  347. /*
  348.  * load_animation
  349.  * Given the name of an animation file, reads in data from
  350.  * that file.
  351.  */
  352. static Boolean
  353. load_animation(char* filename)
  354. {
  355.     int num_frames_small;
  356.     int width_small;
  357.     int height_small;
  358.     int num_frames_large;
  359.     int width_large;
  360.     int height_large;
  361.     int num_colors;
  362.     FILE* file;
  363.  
  364.     if ( filename == NULL ) {
  365.     return False;
  366.     }
  367.  
  368.     if ( (file = fopen(filename, "r")) == NULL ) {
  369.         ekit_errno = XFE_ANIM_CANT_OPEN;
  370.         return False;
  371.     }
  372.  
  373.     if ( fscanf(file, " %d %d %d ",
  374.                       &num_frames_large,
  375.                       &width_large,
  376.                       &height_large) != 3 ||
  377.         fscanf(file, " %d %d %d ",
  378.                       &num_frames_small,
  379.                       &width_small,
  380.                       &height_small) != 3 || 
  381.         num_frames_large != num_frames_small || num_frames_large == 0 ) {
  382.         ekit_errno = XFE_ANIM_READING_SIZES;
  383.         return False;
  384.     }
  385.  
  386.     fe_anim_frames[XFE_ANIMATION_CUSTOM*2] = num_frames_large;
  387.     fe_anim_frames[XFE_ANIMATION_CUSTOM*2+1] = num_frames_small;
  388.  
  389.     if ( fscanf(file, " %d ", &num_colors) != 1 ) {
  390.         ekit_errno = XFE_ANIM_READING_NUM_COLORS;
  391.         return False;
  392.     }
  393.  
  394.     if ( read_colors(file, num_colors) == -1 ) {
  395.         ekit_errno = XFE_ANIM_READING_COLORS;
  396.         return False;
  397.     }
  398.  
  399.     if ( num_frames_large ) {
  400.       anim_custom_large = (struct fe_icon_data*)
  401.                           malloc(sizeof(struct fe_icon_data) * num_frames_large);
  402.       if ( anim_custom_large == NULL ) return False;
  403.     }
  404.  
  405.     if ( num_frames_small ) {
  406.       anim_custom_small = (struct fe_icon_data*)
  407.                           malloc(sizeof(struct fe_icon_data) * num_frames_small);
  408.       if ( anim_custom_small == NULL ) return False;
  409.     }
  410.  
  411.     if ( read_frames(file, num_frames_large, width_large, height_large,
  412.                      anim_custom_large) == -1 ||
  413.          read_frames(file, num_frames_small, width_small, height_small,
  414.                      anim_custom_small) == -1 ) {
  415.         ekit_errno = XFE_ANIM_READING_FRAMES;
  416.         return False;
  417.     }
  418.  
  419.     fe_n_icon_colors+= num_colors;
  420.  
  421.     fscanf(file, " ");
  422.  
  423.     if ( !feof(file) ) {
  424.       ekit_errno = XFE_ANIM_NOT_AT_EOF;
  425.       return False;
  426.     }
  427.  
  428.     return True;
  429. }
  430.  
  431.  
  432. static int custom_anim = 0;
  433.  
  434.  
  435. /*
  436.  * ekit_LoadCustomAnimation
  437.  */
  438. void
  439. ekit_LoadCustomAnimation(void)
  440. {
  441.     char* anim_file = NULL;
  442.  
  443.     PREF_CopyConfigString("x_animation_file", &anim_file);
  444.  
  445.     if ( anim_file == NULL || *anim_file == '\0' ) return;
  446.  
  447.     if ( load_animation(anim_file) == 0 ) {
  448.         ekit_error();
  449.     } else {
  450.         custom_anim = 1;
  451.     }
  452. }
  453.  
  454.  
  455. /*
  456.  * ekit_CustomAnimation
  457.  */
  458. Boolean
  459. ekit_CustomAnimation(void)
  460. {
  461.     return custom_anim;
  462. }
  463.  
  464.  
  465.