"Fossies" - the Fresh Open Source Software Archive

Member "src/Main/Application.cpp" (10 Oct 2018, 4260 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 "Application.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 <wx/stdpaths.h>
   15 #include "Main.h"
   16 #include "Application.h"
   17 #include "CommandLineInterface.h"
   18 #ifndef TC_NO_GUI
   19 #include "GraphicUserInterface.h"
   20 #endif
   21 #include "TextUserInterface.h"
   22 
   23 namespace VeraCrypt
   24 {
   25     namespace
   26     {
   27         void EnsureEndsWithPathSeparator( wxString &s )
   28         {
   29             const wxUniChar pathSeparator = wxFileName::GetPathSeparator();
   30             if (s[s.size() - 1] != pathSeparator)
   31                 s.append(pathSeparator);
   32         }
   33 
   34         wxString *GetXdgConfigPath ()
   35         {
   36             const wxChar *xdgConfig = wxGetenv(wxT("XDG_CONFIG_HOME"));
   37             wxString *configDir;
   38 
   39             if (!wxIsEmpty(xdgConfig))
   40             {
   41                 configDir = new wxString (xdgConfig);
   42                 //wcerr << L"XDG_CONFIG_HOME=" << *configDir << endl;
   43                 EnsureEndsWithPathSeparator(*configDir);
   44                 configDir->append(Application::GetName());
   45             }
   46             else
   47             {
   48                 #if !defined(TC_UNIX) || defined(TC_MACOSX) // Windows, OS X:
   49                     configDir =
   50                         new wxString (wxStandardPaths::Get().GetUserDataDir());
   51                 #else // Linux, FreeBSD, Solaris:
   52                     configDir = new wxString (wxFileName::GetHomeDir());
   53                     configDir->append(wxT("/.config/"));
   54                     configDir->append(Application::GetName());
   55 
   56                     if (!wxDirExists(*configDir))
   57                     {
   58                         wxString legacyConfigDir = wxStandardPaths::Get().GetUserDataDir();
   59                         //wcerr << L"Legacy config dir: " << legacyConfigDir << endl;
   60                         if (wxDirExists(legacyConfigDir))
   61                         {
   62                             configDir->swap(legacyConfigDir);
   63                         }
   64                     }
   65                 #endif
   66             }
   67 
   68             //wcerr << L"Config dir: " << *configDir << endl;
   69             return configDir;
   70         }
   71     }
   72 
   73     wxApp* Application::CreateConsoleApp ()
   74     {
   75         mUserInterface = new TextUserInterface;
   76         mUserInterfaceType = UserInterfaceType::Text;
   77         return mUserInterface;
   78     }
   79 
   80 #ifndef TC_NO_GUI
   81     wxApp* Application::CreateGuiApp ()
   82     {
   83         mUserInterface = new GraphicUserInterface;
   84         mUserInterfaceType = UserInterfaceType::Graphic;
   85         return mUserInterface;
   86     }
   87 #endif
   88 
   89     FilePath Application::GetConfigFilePath (const wxString &configFileName, bool createConfigDir)
   90     {
   91         static wxScopedPtr<const wxString> configDirC;
   92         static bool configDirExists = false;
   93 
   94         if (!configDirExists)
   95         {
   96             if (!configDirC)
   97             {
   98                 wxString *configDir;
   99 
  100                 if (Core->IsInPortableMode())
  101                 {
  102                     configDir = new wxString (
  103                         wxPathOnly(wxStandardPaths::Get().GetExecutablePath()));
  104                 }
  105                 else
  106                 {
  107                     configDir = GetXdgConfigPath();
  108                 }
  109 
  110                 EnsureEndsWithPathSeparator(*configDir);
  111                 configDirC.reset(configDir);
  112             }
  113 
  114             if (createConfigDir)
  115             {
  116                 if (!wxDirExists(*configDirC))
  117                 {
  118                     //wcerr << L"Creating config dir »" << *configDirC << L"« ..." << endl;
  119                     throw_sys_sub_if(
  120                         !wxMkdir(*configDirC, wxS_IRUSR | wxS_IWUSR | wxS_IXUSR),
  121                         configDirC->ToStdWstring());
  122                 }
  123                 configDirExists = true;
  124                 //wcerr << L"Config directory »" << *configDirC << L"« exists now" << endl;
  125             }
  126         }
  127 
  128         return FilePath((*configDirC + configFileName).ToStdWstring());
  129     }
  130 
  131     DirectoryPath Application::GetExecutableDirectory ()
  132     {
  133         return wstring (wxFileName (wxStandardPaths::Get().GetExecutablePath()).GetPath());
  134     }
  135 
  136     FilePath Application::GetExecutablePath ()
  137     {
  138         return wstring (wxStandardPaths::Get().GetExecutablePath());
  139     }
  140 
  141     void Application::Initialize (UserInterfaceType::Enum type)
  142     {
  143         switch (type)
  144         {
  145         case UserInterfaceType::Text:
  146             {
  147                 wxAppInitializer wxTheAppInitializer((wxAppInitializerFunction) CreateConsoleApp);
  148                 break;
  149             }
  150 
  151 #ifndef TC_NO_GUI
  152         case UserInterfaceType::Graphic:
  153             {
  154                 wxAppInitializer wxTheAppInitializer((wxAppInitializerFunction) CreateGuiApp);
  155                 break;
  156             }
  157 #endif
  158 
  159         default:
  160             throw ParameterIncorrect (SRC_POS);
  161         }
  162     }
  163 
  164     int Application::ExitCode = 0;
  165     UserInterface *Application::mUserInterface = nullptr;
  166     UserInterfaceType::Enum Application::mUserInterfaceType;
  167 }