"Fossies" - the Fresh Open Source Software Archive

Member "src/Main/Forms/VolumeSizeWizardPage.cpp" (10 Oct 2018, 4064 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 "VolumeSizeWizardPage.cpp" see the Fossies "Dox" file reference documentation.

    1 /*
    2  Derived from source code of TrueCrypt 7.1a, which is
    3  Copyright (c) 2008-2012 TrueCrypt Developers Association and which is governed
    4  by the TrueCrypt License 3.0.
    5 
    6  Modifications and additions to the original source code (contained in this file)
    7  and all other portions of this file are Copyright (c) 2013-2017 IDRIX
    8  and are governed by the Apache License 2.0 the full text of which is
    9  contained in the file License.txt included in VeraCrypt binary and source
   10  code distribution packages.
   11 */
   12 
   13 #include "System.h"
   14 #include "Main/GraphicUserInterface.h"
   15 #include "VolumeSizeWizardPage.h"
   16 
   17 namespace VeraCrypt
   18 {
   19     VolumeSizeWizardPage::VolumeSizeWizardPage (wxPanel* parent, const VolumePath &volumePath, uint32 sectorSize, const wxString &freeSpaceText)
   20         : VolumeSizeWizardPageBase (parent),
   21         MaxVolumeSize (0),
   22         MaxVolumeSizeValid (false),
   23         MinVolumeSize (1),
   24         SectorSize (sectorSize)
   25     {
   26         VolumeSizePrefixChoice->Append (LangString["KB"], reinterpret_cast <void *> (1024));
   27         VolumeSizePrefixChoice->Append (LangString["MB"], reinterpret_cast <void *> (1024 * 1024));
   28         VolumeSizePrefixChoice->Append (LangString["GB"], reinterpret_cast <void *> (1024 * 1024 * 1024));
   29         VolumeSizePrefixChoice->Select (Prefix::MB);
   30 
   31         wxLongLong diskSpace = 0;
   32         if (!wxGetDiskSpace (wxFileName (wstring (volumePath)).GetPath(), nullptr, &diskSpace))
   33         {
   34             VolumeSizeTextCtrl->Disable();
   35             VolumeSizeTextCtrl->SetValue (L"");
   36         }
   37 
   38         FreeSpaceStaticText->SetFont (Gui->GetDefaultBoldFont (this));
   39 
   40         if (!freeSpaceText.empty())
   41         {
   42             FreeSpaceStaticText->SetLabel (freeSpaceText);
   43         }
   44         else
   45         {
   46 #ifdef TC_WINDOWS
   47             wxString drive = wxFileName (wstring (volumePath)).GetVolume();
   48             if (!drive.empty())
   49             {
   50                 FreeSpaceStaticText->SetLabel (StringFormatter (_("Free space on drive {0}: is {1}."),
   51                     drive, Gui->SizeToString (diskSpace.GetValue())));
   52             }
   53             else
   54 #endif
   55             {
   56                 FreeSpaceStaticText->SetLabel (StringFormatter (_("Free space available: {0}"),
   57                     Gui->SizeToString (diskSpace.GetValue())));
   58             }
   59         }
   60 
   61         VolumeSizeTextCtrl->SetMinSize (wxSize (Gui->GetCharWidth (VolumeSizeTextCtrl) * 20, -1));
   62 
   63         wxTextValidator validator (wxFILTER_INCLUDE_CHAR_LIST);  // wxFILTER_NUMERIC does not exclude - . , etc.
   64         const wxChar *valArr[] = { L"0", L"1", L"2", L"3", L"4", L"5", L"6", L"7", L"8", L"9" };
   65         validator.SetIncludes (wxArrayString (array_capacity (valArr), (const wxChar **) &valArr));
   66         VolumeSizeTextCtrl->SetValidator (validator);
   67     }
   68 
   69     uint64 VolumeSizeWizardPage::GetVolumeSize () const
   70     {
   71         uint64 prefixMult = 1;
   72         int selection = VolumeSizePrefixChoice->GetSelection();
   73         if (selection == wxNOT_FOUND)
   74             return 0;
   75 
   76         prefixMult = reinterpret_cast <uint64> (VolumeSizePrefixChoice->GetClientData (selection));
   77 
   78         uint64 val = StringConverter::ToUInt64 (wstring (VolumeSizeTextCtrl->GetValue()));
   79         if (val <= 0x7fffFFFFffffFFFFull / prefixMult)
   80         {
   81             val *= prefixMult;
   82 
   83             uint32 sectorSizeRem = val % SectorSize;
   84 
   85             if (sectorSizeRem != 0)
   86                 val += SectorSize - sectorSizeRem;
   87 
   88             return val;
   89         }
   90         else
   91             return 0;
   92     }
   93 
   94     bool VolumeSizeWizardPage::IsValid ()
   95     {
   96         if (!VolumeSizeTextCtrl->GetValue().empty() && Validate())
   97         {
   98             try
   99             {
  100                 if (GetVolumeSize() >= MinVolumeSize && (!MaxVolumeSizeValid || GetVolumeSize() <= MaxVolumeSize))
  101                     return true;
  102             }
  103             catch (...) { }
  104         }
  105         return false;
  106     }
  107 
  108     void VolumeSizeWizardPage::SetMaxStaticTextWidth (int width)
  109     {
  110         FreeSpaceStaticText->Wrap (width);
  111         InfoStaticText->Wrap (width);
  112     }
  113 
  114     void VolumeSizeWizardPage::SetVolumeSize (uint64 size)
  115     {
  116         if (size == 0)
  117         {
  118             VolumeSizePrefixChoice->Select (Prefix::MB);
  119             VolumeSizeTextCtrl->SetValue (L"");
  120             return;
  121         }
  122 
  123         if (size % (1024 * 1024 * 1024) == 0)
  124         {
  125             size /= 1024 * 1024 * 1024;
  126             VolumeSizePrefixChoice->Select (Prefix::GB);
  127         }
  128         else if (size % (1024 * 1024) == 0)
  129         {
  130             size /= 1024 * 1024;
  131             VolumeSizePrefixChoice->Select (Prefix::MB);
  132         }
  133         else
  134         {
  135             size /= 1024;
  136             VolumeSizePrefixChoice->Select (Prefix::KB);
  137         }
  138 
  139         VolumeSizeTextCtrl->SetValue (StringConverter::FromNumber (size));
  140     }
  141 }