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

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

unix.superglobalmegacorp.com

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