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 of the GNU General Public License Version 2 or later (the "GPL"),
00028  * or 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     canvas = NULL;
00067     notebook = NULL;
00068 
00069     backID = 0;
00070     nextID = 0;
00071     cancelID = 0;
00072     bMoving = FALSE;
00073     bDone = FALSE;
00074 
00075     reslist = NULL;
00076 }
00077 
00078 nsXIContext::~nsXIContext()
00079 {
00080     // NOTE: don't try to delete "me" cause I control thee
00081 
00082     ReleaseResources();
00083 
00084     XI_IF_DELETE(ldlg);
00085     XI_IF_DELETE(wdlg);
00086     XI_IF_DELETE(sdlg);
00087     XI_IF_DELETE(cdlg);
00088     XI_IF_DELETE(idlg);
00089 
00090     XI_IF_DELETE(opt);
00091 }
00092 
00093 #define MAX_KEY_SIZE 64
00094 #define FIRST_ERR -601
00095 #define LAST_ERR  -631
00096 int
00097 nsXIContext::LoadResources()
00098 {
00099     int err = OK;
00100     nsINIParser *parser = NULL;
00101     char *resfile = NULL;
00102     kvpair *currkv = NULL;
00103     char currkey[MAX_KEY_SIZE];
00104     int len, i;
00105 
00106     resfile = nsINIParser::ResolveName(RES_FILE);
00107     if (!resfile)
00108         return E_INVALID_PTR;
00109 
00110     parser = new nsINIParser(resfile);
00111     if (!parser)
00112     {
00113         XI_IF_FREE(resfile);
00114         return E_MEM;
00115     }
00116 
00117     char *strkeys[] = 
00118     {
00119         "NEXT",
00120         "BACK",
00121         "CANCEL",
00122         "ACCEPT",
00123         "DECLINE",
00124         "INSTALL",
00125         "PAUSE",
00126         "RESUME",
00127         "DEFAULT_TITLE",
00128         "DEST_DIR",
00129         "BROWSE",
00130         "SELECT_DIR",
00131         "DOESNT_EXIST",
00132         "YES_LABEL",
00133         "NO_LABEL",
00134         "OK_LABEL",
00135         "DELETE_LABEL",
00136         "CANCEL_LABEL",
00137         "ERROR",
00138         "FATAL_ERROR",
00139         "DESCRIPTION",
00140         "PREPARING",
00141         "EXTRACTING",
00142         "INSTALLING_XPI",
00143         "COMPLETING_INSTALL",
00144         "PROCESSING_FILE",
00145         "NO_PERMS",
00146         "DL_SETTINGS",
00147         "SAVE_MODULES",
00148         "PROXY_SETTINGS",
00149         "PS_LABEL0",
00150         "PS_LABEL1",
00151         "PS_LABEL2",
00152         "PS_LABEL3",
00153         "ERROR_TITLE",
00154         "DS_AVAIL",
00155         "DS_REQD",
00156         "NO_DISK_SPACE",
00157         "CXN_DROPPED",
00158         "DOWNLOADING",
00159         "FROM",
00160         "TO",
00161         "STATUS",
00162         "DL_STATUS_STR",
00163         "CRC_CHECK",
00164         "USAGE_MSG",
00165         "UNKNOWN",
00166 
00167         __EOT__
00168     };
00169 
00170     /* read in UI strings */
00171     currkv = (kvpair *) malloc(sizeof(kvpair));
00172     reslist = currkv;
00173     for (i = 0; strcmp(strkeys[i], __EOT__) != 0; i++)
00174     {
00175         err = parser->GetStringAlloc(RES_SECT, strkeys[i], 
00176                                     &(currkv->val), &len);
00177         if (err != OK)
00178             goto BAIL;
00179 
00180         currkv->key = strdup(strkeys[i]);
00181         currkv->next = (kvpair *) malloc(sizeof(kvpair));
00182         currkv = currkv->next;
00183 
00184         if (i > 1024) /* inf loop prevention paranoia */
00185             break;
00186     }
00187     currkv->next = NULL; /* seal off list */
00188 
00189     /* read in err strings */
00190     for (i = FIRST_ERR; i >= LAST_ERR; i--)
00191     {
00192         sprintf(currkey, "%d", i);
00193         err = parser->GetStringAlloc(RES_SECT, currkey, &(currkv->val), &len);
00194         if (err != OK)
00195             goto BAIL;
00196         
00197         currkv->key = strdup(currkey);
00198         if (i == LAST_ERR)
00199             break;
00200         currkv->next = (kvpair *) malloc(sizeof(kvpair));
00201         currkv = currkv->next;
00202     }
00203     currkv->next = NULL; /* seal off list */
00204 
00205 BAIL:
00206     if (err != OK)
00207     {
00208         fprintf(stderr, "FATAL ERROR: Failed to load resources!\n");
00209     }
00210     XI_IF_FREE(resfile);
00211     XI_IF_DELETE(parser);
00212     return err;
00213 }
00214 
00215 int 
00216 nsXIContext::ReleaseResources()
00217 {
00218     int err = OK;
00219     kvpair *currkv = NULL, *delkv = NULL;
00220     
00221     /* empty list -- should never really happen */
00222     if (!reslist) 
00223         return E_PARAM;
00224     
00225     currkv = reslist;
00226     while (currkv)
00227     {
00228         XI_IF_FREE(currkv->key);
00229         XI_IF_FREE(currkv->val);
00230         delkv = currkv;
00231         currkv = currkv->next;
00232         XI_IF_FREE(delkv);
00233     }
00234 
00235     return err;
00236 }
00237 
00238 char *
00239 nsXIContext::Res(char *aKey)
00240 {
00241     char *val = NULL;
00242     kvpair *currkv = NULL;
00243 
00244     /* param check */
00245     if (!aKey || !reslist)
00246         return NULL;
00247 
00248     /* search through linked list */
00249     currkv = reslist;
00250     while (currkv)
00251     {
00252         if (strcmp(aKey, currkv->key) == 0)
00253         {
00254             val = currkv->val;
00255             break;
00256         }
00257         currkv = currkv->next;
00258     } 
00259 
00260     return val;
00261 }
00262