"Fossies" - the Fresh Open Source Software Archive

Member "src/Common/Combo.c" (10 Oct 2018, 6195 Bytes) of package /windows/misc/VeraCrypt_1.23-Hotfix-2_Source.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "Combo.c" see the Fossies "Dox" file reference documentation.

    1 /*
    2  Legal Notice: Some portions of the source code contained in this file were
    3  derived from the source code of TrueCrypt 7.1a, which is
    4  Copyright (c) 2003-2012 TrueCrypt Developers Association and which is
    5  governed by the TrueCrypt License 3.0, also from the source code of
    6  Encryption for the Masses 2.02a, which is Copyright (c) 1998-2000 Paul Le Roux
    7  and which is governed by the 'License Agreement for Encryption for the Masses'
    8  Modifications and additions to the original source code (contained in this file)
    9  and all other portions of this file are Copyright (c) 2013-2017 IDRIX
   10  and are governed by the Apache License 2.0 the full text of which is
   11  contained in the file License.txt included in VeraCrypt binary and source
   12  code distribution packages. */
   13 
   14 #include "Tcdefs.h"
   15 #include "Combo.h"
   16 #include "Dlgcode.h"
   17 #include "Xml.h"
   18 
   19 #include <time.h>
   20 
   21 #define SIZEOF_MRU_LIST 20
   22 
   23 void AddComboItem (HWND hComboBox, const wchar_t *lpszFileName, BOOL saveHistory)
   24 {
   25     LPARAM nIndex;
   26 
   27     if (!saveHistory)
   28     {
   29         SendMessage (hComboBox, CB_RESETCONTENT, 0, 0);
   30         SetWindowText (hComboBox, lpszFileName);
   31         return;
   32     }
   33 
   34     nIndex = SendMessage (hComboBox, CB_FINDSTRINGEXACT, (WPARAM) - 1, (LPARAM) & lpszFileName[0]);
   35 
   36     if (nIndex == CB_ERR && *lpszFileName)
   37     {
   38         time_t lTime = time (NULL);
   39         nIndex = SendMessage (hComboBox, CB_ADDSTRING, 0, (LPARAM) & lpszFileName[0]);
   40         if (nIndex != CB_ERR)
   41             SendMessage (hComboBox, CB_SETITEMDATA, nIndex, (LPARAM) lTime);
   42     }
   43 
   44     if (nIndex != CB_ERR && *lpszFileName)
   45         nIndex = SendMessage (hComboBox, CB_SETCURSEL, nIndex, 0);
   46 
   47     if (*lpszFileName == 0)
   48     {
   49         SendMessage (hComboBox, CB_SETCURSEL, (WPARAM) - 1, 0);
   50     }
   51 }
   52 
   53 
   54 LPARAM MoveEditToCombo (HWND hComboBox, BOOL saveHistory)
   55 {
   56     wchar_t szTmp[TC_MAX_PATH] = {0};
   57 
   58     if (!saveHistory)
   59     {
   60         GetWindowText (hComboBox, szTmp, ARRAYSIZE (szTmp));
   61         SendMessage (hComboBox, CB_RESETCONTENT, 0, 0);
   62         SetWindowText (hComboBox, szTmp);
   63         return 0;
   64     }
   65 
   66     GetWindowText (hComboBox, szTmp, ARRAYSIZE (szTmp));
   67 
   68     if (wcslen (szTmp) > 0)
   69     {
   70         LPARAM nIndex = SendMessage (hComboBox, CB_FINDSTRINGEXACT, (WPARAM) - 1,
   71                          (LPARAM) & szTmp[0]);
   72         if (nIndex == CB_ERR)
   73         {
   74             time_t lTime = time (NULL);
   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         {
   81             time_t lTime = time (NULL);
   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 GetOrderComboIdx (HWND hComboBox, int *nIdxList, int nElems)
   92 {
   93     int x = (int) SendMessage (hComboBox, CB_GETCOUNT, 0, 0);
   94     if (x != CB_ERR)
   95     {
   96         int i, nHighIdx = CB_ERR;
   97         time_t lHighTime = -1;
   98 
   99         for (i = 0; i < x; i++)
  100         {
  101             time_t lTime = SendMessage (hComboBox, CB_GETITEMDATA, (WPARAM) i, 0);
  102             if (lTime > lHighTime)
  103             {
  104                 int n;
  105                 for (n = 0; n < nElems; n++)
  106                     if (nIdxList[n] == i)
  107                         break;
  108                 if (n == nElems)
  109                 {
  110                     lHighTime = lTime;
  111                     nHighIdx = i;
  112                 }
  113             }
  114         }
  115 
  116         return nHighIdx;
  117     }
  118 
  119     return CB_ERR;
  120 }
  121 
  122 LPARAM UpdateComboOrder (HWND hComboBox)
  123 {
  124     LPARAM nIndex;
  125 
  126     nIndex = SendMessage (hComboBox, CB_GETCURSEL, 0, 0);
  127 
  128     if (nIndex != CB_ERR)
  129     {
  130         time_t lTime = time (NULL);
  131         nIndex = SendMessage (hComboBox, CB_SETITEMDATA, (WPARAM) nIndex,
  132                       (LPARAM) lTime);
  133     }
  134 
  135     return nIndex;
  136 }
  137 
  138 void LoadCombo (HWND hComboBox, BOOL bEnabled, BOOL bOnlyCheckModified, BOOL *pbModified)
  139 {
  140     DWORD size;
  141     char *history = LoadFile (GetConfigPath (TC_APPD_FILENAME_HISTORY), &size);
  142     char *xml = history;
  143     char volume[MAX_PATH];
  144     int i, nComboIdx[SIZEOF_MRU_LIST] = {0};
  145     int count = (int) SendMessage (hComboBox, CB_GETCOUNT, 0, 0);
  146 
  147     if (xml == NULL)
  148     {
  149         // No history XML file but history is enabled
  150         if (bEnabled && pbModified)
  151         *pbModified = TRUE;
  152         return;
  153     }
  154 
  155     if (!bEnabled && bOnlyCheckModified)
  156     {
  157         // History is disable but there is a history XML file
  158         if (pbModified)
  159             *pbModified = TRUE;
  160         free (history);
  161         return;
  162     }
  163 
  164 
  165     /* combo list part:- get mru items */
  166     for (i = 0; i < SIZEOF_MRU_LIST; i++)
  167         nComboIdx[i] = GetOrderComboIdx (hComboBox, &nComboIdx[0], i);
  168 
  169     i = 0;
  170     while (xml = XmlFindElement (xml, "volume"))
  171     {
  172         wchar_t szTmp[MAX_PATH] = { 0 };
  173         wchar_t wszVolume[MAX_PATH] = {0};
  174 
  175         if (i < count)
  176         {
  177             if (SendMessage (hComboBox, CB_GETLBTEXTLEN, nComboIdx[i], 0) < ARRAYSIZE (szTmp))
  178                 SendMessage (hComboBox, CB_GETLBTEXT, nComboIdx[i], (LPARAM) & szTmp[0]);
  179         }
  180 
  181         XmlGetNodeText (xml, volume, sizeof (volume));
  182         if (0 == MultiByteToWideChar (CP_UTF8, 0, volume, -1, wszVolume, MAX_PATH))
  183             wszVolume [0] = 0;
  184         if (!bOnlyCheckModified)
  185             AddComboItem (hComboBox, wszVolume, TRUE);
  186 
  187         if (pbModified && wcscmp (wszVolume, szTmp))
  188             *pbModified = TRUE;
  189 
  190         xml++;
  191         i++;
  192     }
  193 
  194     if (pbModified && (i != count))
  195         *pbModified = TRUE;
  196 
  197     if (!bOnlyCheckModified)
  198         SendMessage (hComboBox, CB_SETCURSEL, 0, 0);
  199 
  200     free (history);
  201 }
  202 
  203 void DumpCombo (HWND hComboBox, int bClear)
  204 {
  205     FILE *f;
  206     int i, nComboIdx[SIZEOF_MRU_LIST] = {0};
  207 
  208     if (bClear)
  209     {
  210         DeleteFile (GetConfigPath (TC_APPD_FILENAME_HISTORY));
  211         return;
  212     }
  213 
  214     f = _wfopen (GetConfigPath (TC_APPD_FILENAME_HISTORY), L"w,ccs=UTF-8");
  215     if (f == NULL) return;
  216 
  217     XmlWriteHeader (f);
  218     fputws (L"\n\t<history>", f);
  219 
  220     /* combo list part:- get mru items */
  221     for (i = 0; i < SIZEOF_MRU_LIST; i++)
  222         nComboIdx[i] = GetOrderComboIdx (hComboBox, &nComboIdx[0], i);
  223 
  224     /* combo list part:- write out mru items */
  225     for (i = 0; i < SIZEOF_MRU_LIST; i++)
  226     {
  227         wchar_t szTmp[MAX_PATH] = { 0 };
  228 
  229         if (SendMessage (hComboBox, CB_GETLBTEXTLEN, nComboIdx[i], 0) < ARRAYSIZE (szTmp))
  230             SendMessage (hComboBox, CB_GETLBTEXT, nComboIdx[i], (LPARAM) & szTmp[0]);
  231 
  232         if (szTmp[0] != 0)
  233         {
  234             wchar_t q[MAX_PATH * 2] = { 0 };
  235             XmlQuoteTextW (szTmp, q, ARRAYSIZE (q));
  236 
  237             fwprintf (f, L"\n\t\t<volume>%s</volume>", q);
  238         }
  239     }
  240 
  241     fputws (L"\n\t</history>", f);
  242     XmlWriteFooter (f);
  243     fclose (f);
  244 }
  245 
  246 void ClearCombo (HWND hComboBox)
  247 {
  248     int i;
  249     for (i = 0; i < SIZEOF_MRU_LIST; i++)
  250     {
  251         SendMessage (hComboBox, CB_DELETESTRING, 0, 0);
  252     }
  253 }
  254 
  255 int IsComboEmpty (HWND hComboBox)
  256 {
  257     return SendMessage (hComboBox, CB_GETCOUNT, 0, 0) < 1;
  258 }