Annotation of truecrypt/common/combo.c, revision 1.1.1.12

1.1.1.9   root        1: /*
1.1.1.11  root        2:  Legal Notice: Some portions of the source code contained in this file were
                      3:  derived from the source code of Encryption for the Masses 2.02a, which is
                      4:  Copyright (c) 1998-2000 Paul Le Roux and which is governed by the 'License
                      5:  Agreement for Encryption for the Masses'. Modifications and additions to
                      6:  the original source code (contained in this file) and all other portions of
                      7:  this file are Copyright (c) 2003-2008 TrueCrypt Foundation and are governed
1.1.1.12! root        8:  by the TrueCrypt License 2.5 the full text of which is contained in the
1.1.1.11  root        9:  file License.txt included in TrueCrypt binary and source code distribution
1.1.1.9   root       10:  packages. */
1.1.1.6   root       11: 
                     12: #include "Tcdefs.h"
                     13: #include "Combo.h"
                     14: #include "Dlgcode.h"
                     15: #include "Xml.h"
1.1       root       16: 
                     17: #include <time.h>
                     18: 
1.1.1.6   root       19: #define SIZEOF_MRU_LIST 20
1.1       root       20: 
                     21: void
1.1.1.8   root       22: AddComboItem (HWND hComboBox, char *lpszFileName, BOOL saveHistory)
1.1       root       23: {
                     24:        LPARAM nIndex;
                     25: 
1.1.1.8   root       26:        if (!saveHistory)
                     27:        {
                     28:                SendMessage (hComboBox, CB_RESETCONTENT, 0, 0);
                     29:                SetWindowText (hComboBox, lpszFileName);
                     30:                return;
                     31:        }
                     32: 
                     33:        nIndex = SendMessage (hComboBox, CB_FINDSTRINGEXACT, (WPARAM) - 1, (LPARAM) & lpszFileName[0]);
1.1       root       34: 
                     35:        if (nIndex == CB_ERR && *lpszFileName)
                     36:        {
1.1.1.6   root       37:                time_t lTime = time (NULL);
1.1       root       38:                nIndex = SendMessage (hComboBox, CB_ADDSTRING, 0, (LPARAM) & lpszFileName[0]);
                     39:                if (nIndex != CB_ERR)
                     40:                        SendMessage (hComboBox, CB_SETITEMDATA, nIndex, (LPARAM) lTime);
                     41:        }
                     42: 
                     43:        if (nIndex != CB_ERR && *lpszFileName)
                     44:                nIndex = SendMessage (hComboBox, CB_SETCURSEL, nIndex, 0);
                     45: 
                     46:        if (*lpszFileName == 0)
                     47:        {
                     48:                SendMessage (hComboBox, CB_SETCURSEL, (WPARAM) - 1, 0);
                     49:        }
                     50: }
                     51: 
                     52: 
                     53: LPARAM
1.1.1.8   root       54: MoveEditToCombo (HWND hComboBox, BOOL saveHistory)
1.1       root       55: {
1.1.1.8   root       56:        char szTmp[TC_MAX_PATH] = {0};
                     57: 
                     58:        if (!saveHistory)
                     59:        {
                     60:                GetWindowText (hComboBox, szTmp, sizeof (szTmp));
                     61:                SendMessage (hComboBox, CB_RESETCONTENT, 0, 0);
                     62:                SetWindowText (hComboBox, szTmp);
                     63:                return 0;
                     64:        }
1.1       root       65: 
                     66:        GetWindowText (hComboBox, szTmp, sizeof (szTmp));
                     67: 
                     68:        if (strlen (szTmp) > 0)
                     69:        {
                     70:                LPARAM nIndex = SendMessage (hComboBox, CB_FINDSTRINGEXACT, (WPARAM) - 1,
                     71:                                             (LPARAM) & szTmp[0]);
                     72:                if (nIndex == CB_ERR)
                     73:                {
1.1.1.6   root       74:                        time_t lTime = time (NULL);
1.1       root       75:                        nIndex = SendMessage (hComboBox, CB_ADDSTRING, 0, (LPARAM) & szTmp[0]);
                     76:                        if (nIndex != CB_ERR)
                     77:                                SendMessage (hComboBox, CB_SETITEMDATA, nIndex, (DWORD) lTime);
                     78:                }
                     79:                else
                     80:                {
1.1.1.6   root       81:                        time_t lTime = time (NULL);
1.1       root       82:                        SendMessage (hComboBox, CB_SETITEMDATA, nIndex, (DWORD) lTime);
                     83:                }
                     84: 
                     85:                return nIndex;
                     86:        }
                     87: 
                     88:        return SendMessage (hComboBox, CB_GETCURSEL, 0, 0);
                     89: }
                     90: 
                     91: int
                     92: GetOrderComboIdx (HWND hComboBox, int *nIdxList, int nElems)
                     93: {
                     94:        int x = (int) SendMessage (hComboBox, CB_GETCOUNT, 0, 0);
                     95:        if (x != CB_ERR)
                     96:        {
                     97:                int i, nHighIdx = CB_ERR;
1.1.1.6   root       98:                time_t lHighTime = -1;
1.1       root       99: 
                    100:                for (i = 0; i < x; i++)
                    101:                {
1.1.1.6   root      102:                        time_t lTime = SendMessage (hComboBox, CB_GETITEMDATA, (WPARAM) i, 0);
1.1       root      103:                        if (lTime > lHighTime)
                    104:                        {
                    105:                                int n;
                    106:                                for (n = 0; n < nElems; n++)
                    107:                                        if (nIdxList[n] == i)
                    108:                                                break;
                    109:                                if (n == nElems)
                    110:                                {
                    111:                                        lHighTime = lTime;
                    112:                                        nHighIdx = i;
                    113:                                }
                    114:                        }
                    115:                }
                    116: 
                    117:                return nHighIdx;
                    118:        }
                    119: 
                    120:        return CB_ERR;
                    121: }
                    122: 
                    123: LPARAM
                    124: UpdateComboOrder (HWND hComboBox)
                    125: {
                    126:        LPARAM nIndex;
                    127: 
                    128:        nIndex = SendMessage (hComboBox, CB_GETCURSEL, 0, 0);
                    129: 
                    130:        if (nIndex != CB_ERR)
                    131:        {
1.1.1.6   root      132:                time_t lTime = time (NULL);
1.1       root      133:                nIndex = SendMessage (hComboBox, CB_SETITEMDATA, (WPARAM) nIndex,
                    134:                                      (LPARAM) lTime);
                    135:        }
                    136: 
                    137:        return nIndex;
                    138: }
                    139: 
                    140: void
1.1.1.6   root      141: LoadCombo (HWND hComboBox)
1.1       root      142: {
1.1.1.6   root      143:        DWORD size;
                    144:        char *history = LoadFile (GetConfigPath (FILE_HISTORY), &size);
                    145:        char *xml = history;
                    146:        char volume[MAX_PATH];
1.1       root      147: 
1.1.1.6   root      148:        if (xml == NULL) return;
1.1       root      149: 
1.1.1.6   root      150:        while (xml = XmlFindElement (xml, "volume"))
                    151:        {
1.1.1.9   root      152:                XmlGetNodeText (xml, volume, sizeof (volume));
1.1.1.8   root      153:                AddComboItem (hComboBox, volume, TRUE);
1.1.1.6   root      154:                xml++;
1.1       root      155:        }
                    156: 
                    157:        SendMessage (hComboBox, CB_SETCURSEL, 0, 0);
                    158: 
1.1.1.6   root      159:        free (history);
1.1       root      160: }
                    161: 
                    162: void
1.1.1.6   root      163: DumpCombo (HWND hComboBox, int bClear)
1.1       root      164: {
1.1.1.6   root      165:        FILE *f;
1.1       root      166:        int i, nComboIdx[SIZEOF_MRU_LIST];
                    167: 
1.1.1.6   root      168:        if (bClear)
                    169:        {
                    170:                DeleteFile (GetConfigPath (FILE_HISTORY));
                    171:                return;
                    172:        }
                    173: 
                    174:        f = fopen (GetConfigPath (FILE_HISTORY), "w");
                    175:        if (f == NULL) return;
                    176: 
                    177:        XmlWriteHeader (f);
                    178:        fputs ("\n\t<history>", f);
                    179: 
1.1       root      180:        /* combo list part:- get mru items */
                    181:        for (i = 0; i < SIZEOF_MRU_LIST; i++)
1.1.1.6   root      182:                nComboIdx[i] = GetOrderComboIdx (hComboBox, &nComboIdx[0], i);
1.1       root      183: 
                    184:        /* combo list part:- write out mru items */
                    185:        for (i = 0; i < SIZEOF_MRU_LIST; i++)
                    186:        {
1.1.1.6   root      187:                char szTmp[MAX_PATH] = { 0 };
                    188:                 
                    189:                if (SendMessage (hComboBox, CB_GETLBTEXTLEN, nComboIdx[i], 0) < sizeof (szTmp))
1.1       root      190:                        SendMessage (hComboBox, CB_GETLBTEXT, nComboIdx[i], (LPARAM) & szTmp[0]);
                    191: 
1.1.1.6   root      192:                if (szTmp[0] != 0)
1.1.1.9   root      193:                {
                    194:                        char q[MAX_PATH * 2] = { 0 };
                    195:                        XmlQuoteText (szTmp, q, sizeof (q));
                    196: 
                    197:                        fprintf (f, "\n\t\t<volume>%s</volume>", q);
                    198:                }
1.1       root      199:        }
1.1.1.6   root      200: 
                    201:        fputs ("\n\t</history>", f);
                    202:        XmlWriteFooter (f);
                    203:        fclose (f);
1.1       root      204: }
                    205: 
                    206: void
                    207: ClearCombo (HWND hComboBox)
                    208: {
                    209:        int i;
                    210:        for (i = 0; i < SIZEOF_MRU_LIST; i++)
                    211:        {
                    212:                SendMessage (hComboBox, CB_DELETESTRING, 0, 0);
                    213:        }
                    214: }
                    215: 
                    216: int
                    217: IsComboEmpty (HWND hComboBox)
                    218: {
                    219:        return SendMessage (hComboBox, CB_GETCOUNT, 0, 0) < 1;
                    220: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.