Back to index

lightning-sunbird  0.9+nobinonly
nsXIContext.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is Mozilla Communicator client code, released
00016  * March 31, 1998.
00017  *
00018  * The Initial Developer of the Original Code is
00019  * Netscape Communications Corporation.
00020  * Portions created by the Initial Developer are Copyright (C) 1998
00021  * the Initial Developer. All Rights Reserved.
00022  *
00023  * Contributor(s):
00024  *   Samir Gehani <sgehani@netscape.com>
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either the GNU General Public License Version 2 or later (the "GPL"), or
00028  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 
00040 #include "nsXIContext.h"
00041 
00042 #define __EOT__ "EndOfTable"
00043 
00044 nsXIContext::nsXIContext()
00045 {
00046     me = NULL;
00047 
00048     ldlg = NULL;
00049     wdlg = NULL;
00050     sdlg = NULL;
00051     cdlg = NULL;
00052     idlg = NULL;
00053 
00054     opt = new nsXIOptions();
00055 
00056     window = NULL;
00057     back = NULL;
00058     next = NULL;
00059     cancel = NULL;
00060     nextLabel = NULL;
00061     backLabel = NULL;
00062     acceptLabel = NULL;
00063     declineLabel = NULL;
00064     installLabel = NULL;
00065     logo = NULL; 
00066     notebook = NULL;
00067     header = NULL;
00068 
00069     backID = 0;
00070     nextID = 0;
00071     cancelID = 0;
00072     bDone = FALSE;
00073 
00074     reslist = NULL;
00075 }
00076 
00077 nsXIContext::~nsXIContext()
00078 {
00079     // NOTE: don't try to delete "me" cause I control thee
00080 
00081     ReleaseResources();
00082 
00083     XI_IF_DELETE(ldlg);
00084     XI_IF_DELETE(wdlg);
00085     XI_IF_DELETE(sdlg);
00086     XI_IF_DELETE(cdlg);
00087     XI_IF_DELETE(idlg);
00088 
00089     XI_IF_DELETE(opt);
00090 }
00091 
00092 char *
00093 nsXIContext::itoa(int n)
00094 {
00095        char *s;
00096        int i, j, sign, tmp;
00097        
00098        /* check sign and convert to positive to stringify numbers */
00099        if ( (sign = n) < 0)
00100               n = -n;
00101        i = 0;
00102        s = (char*) malloc(sizeof(char));
00103        
00104        /* grow string as needed to add numbers from powers of 10 
00105      * down till none left 
00106      */
00107        do
00108        {
00109               s = (char*) realloc(s, (i+1)*sizeof(char));
00110               s[i++] = n % 10 + '0';  /* '0' or 30 is where ASCII numbers start */
00111               s[i] = '\0';
00112        }
00113        while( (n /= 10) > 0);      
00114        
00115        /* tack on minus sign if we found earlier that this was negative */
00116        if (sign < 0)
00117        {
00118               s = (char*) realloc(s, (i+1)*sizeof(char));
00119               s[i++] = '-';
00120        }
00121        s[i] = '\0';
00122        
00123        /* pop numbers (and sign) off of string to push back into right direction */
00124        for (i = 0, j = strlen(s) - 1; i < j; i++, j--)
00125        {
00126               tmp = s[i];
00127               s[i] = s[j];
00128               s[j] = tmp;
00129        }
00130        
00131        return s;
00132 }
00133 
00134 #define MAX_KEY_SIZE 64
00135 #define FIRST_ERR -601
00136 #define LAST_ERR  -625
00137 int
00138 nsXIContext::LoadResources()
00139 {
00140     int err = OK;
00141     nsINIParser *parser = NULL;
00142     char *resfile = NULL;
00143     kvpair *currkv = NULL;
00144     char currkey[MAX_KEY_SIZE];
00145     int len, i;
00146 
00147     resfile = nsINIParser::ResolveName(RES_FILE);
00148     if (!resfile)
00149         return E_INVALID_PTR;
00150 
00151     parser = new nsINIParser(resfile);
00152     if (!parser)
00153     {
00154         XI_IF_FREE(resfile);
00155         return E_MEM;
00156     }
00157 
00158     char *strkeys[] = 
00159     {
00160         "NEXT",
00161         "BACK",
00162         "CANCEL",
00163         "ACCEPT",
00164         "DECLINE",
00165         "INSTALL",
00166         "PAUSE",
00167         "RESUME",
00168         "DEFAULT_TITLE",
00169         "DEST_DIR",
00170         "BROWSE",
00171         "SELECT_DIR",
00172         "DOESNT_EXIST",
00173         "YES_LABEL",
00174         "NO_LABEL",
00175         "OK_LABEL",
00176         "DELETE_LABEL",
00177         "CANCEL_LABEL",
00178         "ERROR",
00179         "FATAL_ERROR",
00180         "DESCRIPTION",
00181         "WILL_INSTALL",
00182         "TO_LOCATION",
00183         "PREPARING",
00184         "EXTRACTING",
00185         "INSTALLING_XPI",
00186         "PROCESSING_FILE",
00187         "NO_PERMS",
00188         "DL_SETTINGS",
00189         "SAVE_MODULES",
00190         "PROXY_SETTINGS",
00191         "PS_LABEL0",
00192         "PS_LABEL1",
00193         "PS_LABEL2",
00194         "PS_LABEL3",
00195         "ERROR_TITLE",
00196         "DS_AVAIL",
00197         "DS_REQD",
00198         "NO_DISK_SPACE",
00199         "CXN_DROPPED",
00200         "DOWNLOADING",
00201         "FROM",
00202         "TO",
00203         "STATUS",
00204         "DL_STATUS_STR",
00205         "CRC_FAILED",
00206         "CRC_CHECK",
00207         "USAGE_MSG",
00208         "UNKNOWN",
00209 
00210         __EOT__
00211     };
00212 
00213     /* read in UI strings */
00214     currkv = (kvpair *) malloc(sizeof(kvpair));
00215     reslist = currkv;
00216     for (i = 0; strcmp(strkeys[i], __EOT__) != 0; i++)
00217     {
00218         err = parser->GetStringAlloc(RES_SECT, strkeys[i], 
00219                                     &(currkv->val), &len);
00220         if (err != OK)
00221             goto BAIL;
00222 
00223         currkv->key = strdup(strkeys[i]);
00224         currkv->next = (kvpair *) malloc(sizeof(kvpair));
00225         currkv = currkv->next;
00226 
00227         if (i > 1024) /* inf loop prevention paranoia */
00228             break;
00229     }
00230     currkv->next = NULL; /* seal off list */
00231 
00232     /* read in err strings */
00233     for (i = FIRST_ERR; i >= LAST_ERR; i--)
00234     {
00235         sprintf(currkey, "%d", i);
00236         err = parser->GetStringAlloc(RES_SECT, currkey, &(currkv->val), &len);
00237         if (err != OK)
00238             goto BAIL;
00239         
00240         currkv->key = strdup(currkey);
00241         if (i == LAST_ERR)
00242             break;
00243         currkv->next = (kvpair *) malloc(sizeof(kvpair));
00244         currkv = currkv->next;
00245     }
00246     currkv->next = NULL; /* seal off list */
00247 
00248 BAIL:
00249     if (err != OK)
00250     {
00251         fprintf(stderr, "FATAL ERROR: Failed to load resources!\n");
00252     }
00253     XI_IF_FREE(resfile);
00254     XI_IF_DELETE(parser);
00255     return err;
00256 }
00257 
00258 int 
00259 nsXIContext::ReleaseResources()
00260 {
00261     int err = OK;
00262     kvpair *currkv = NULL, *delkv = NULL;
00263     
00264     /* empty list -- should never really happen */
00265     if (!reslist) 
00266         return E_PARAM;
00267     
00268     currkv = reslist;
00269     while (currkv)
00270     {
00271         XI_IF_FREE(currkv->key);
00272         XI_IF_FREE(currkv->val);
00273         delkv = currkv;
00274         currkv = currkv->next;
00275         XI_IF_FREE(delkv);
00276     }
00277 
00278     return err;
00279 }
00280 
00281 char *
00282 nsXIContext::Res(char *aKey)
00283 {
00284     char *val = NULL;
00285     kvpair *currkv = NULL;
00286 
00287     /* param check */
00288     if (!aKey || !reslist)
00289         return NULL;
00290 
00291     /* search through linked list */
00292     currkv = reslist;
00293     while (currkv)
00294     {
00295         if (strcmp(aKey, currkv->key) == 0)
00296         {
00297             val = currkv->val;
00298             break;
00299         }
00300         currkv = currkv->next;
00301     } 
00302 
00303     return val;
00304 }
00305