Annotation of truecrypt/main/userpreferences.cpp, revision 1.1.1.1

1.1       root        1: /*
                      2:  Copyright (c) 2008 TrueCrypt Foundation. All rights reserved.
                      3: 
                      4:  Governed by the TrueCrypt License 2.4 the full text of which is contained
                      5:  in the file License.txt included in TrueCrypt binary and source code
                      6:  distribution packages.
                      7: */
                      8: 
                      9: #include "System.h"
                     10: #include "Main/Application.h"
                     11: #include "UserPreferences.h"
                     12: #include "Xml.h"
                     13: 
                     14: namespace TrueCrypt
                     15: {
                     16:        void UserPreferences::SetValue (const wxString &cfgText, bool &cfgVar)
                     17:        {
                     18:                if (cfgText == L"0")
                     19:                        cfgVar = false;
                     20:                else if (cfgText == L"1")
                     21:                        cfgVar = true;
                     22:        }
                     23: 
                     24:        void UserPreferences::SetValue (const wxString &cfgText, int &cfgVar)
                     25:        {
                     26:                if (cfgText.empty())
                     27:                        cfgVar = 0;
                     28:                else
                     29:                        cfgVar = StringConverter::ToUInt32 (wstring (cfgText));
                     30:        }
                     31:        
                     32:        void UserPreferences::SetValue (const wxString &cfgText, uint64 &cfgVar)
                     33:        {
                     34:                if (cfgText.empty())
                     35:                        cfgVar = 0;
                     36:                else
                     37:                        cfgVar = StringConverter::ToUInt64 (wstring (cfgText));
                     38:        }
                     39: 
                     40:        void UserPreferences::SetValue (const wxString &cfgText, wstring &cfgVar)
                     41:        {
                     42:                cfgVar = cfgText;
                     43:        }
                     44: 
                     45:        void UserPreferences::SetValue (const wxString &cfgText, wxString &cfgVar)
                     46:        {
                     47:                cfgVar = cfgText;
                     48:        }
                     49: 
                     50:        void UserPreferences::Load()
                     51:        {
                     52:                // Preferences
                     53:                FilePath cfgPath = Application::GetConfigFilePath (GetPreferencesFileName());
                     54:                if (cfgPath.IsFile())
                     55:                {
                     56:                        map <wxString, wxString> configMap;
                     57:                        foreach (XmlNode node, XmlParser (cfgPath).GetNodes (L"config"))
                     58:                        {
                     59:                                configMap[node.Attributes[L"key"]] = node.InnerText;
                     60:                        }
                     61: 
                     62: #define TC_CONFIG_SET(NAME) SetValue (configMap[L###NAME], NAME)
                     63: 
                     64:                        TC_CONFIG_SET (BackgroundTaskEnabled);
                     65:                        TC_CONFIG_SET (BackgroundTaskMenuDismountItemsEnabled);
                     66:                        TC_CONFIG_SET (BackgroundTaskMenuMountItemsEnabled);
                     67:                        TC_CONFIG_SET (BackgroundTaskMenuOpenItemsEnabled);
                     68:                        TC_CONFIG_SET (BeepAfterHotkeyMountDismount);
                     69:                        SetValue (configMap[L"CachePasswords"], DefaultMountOptions.CachePassword);
                     70:                        TC_CONFIG_SET (CloseBackgroundTaskOnNoVolumes);
                     71:                        TC_CONFIG_SET (CloseExplorerWindowsOnDismount);
                     72:                        TC_CONFIG_SET (DismountOnInactivity);
                     73:                        TC_CONFIG_SET (DismountOnLogOff);
                     74:                        TC_CONFIG_SET (DismountOnPowerSaving);
                     75:                        TC_CONFIG_SET (DismountOnScreenSaver);
                     76:                        TC_CONFIG_SET (DisplayMessageAfterHotkeyDismount);
                     77:                        TC_CONFIG_SET (BackgroundTaskEnabled);
                     78:                        SetValue (configMap[L"FilesystemOptions"], DefaultMountOptions.FilesystemOptions);
                     79:                        TC_CONFIG_SET (ForceAutoDismount);
                     80:                        TC_CONFIG_SET (LastSelectedSlotNumber);
                     81:                        TC_CONFIG_SET (MaxVolumeIdleTime);
                     82:                        TC_CONFIG_SET (MountDevicesOnLogon);
                     83:                        TC_CONFIG_SET (MountFavoritesOnLogon);
                     84: 
                     85:                        bool readOnly;
                     86:                        SetValue (configMap[L"MountVolumesReadOnly"], readOnly);
                     87:                        DefaultMountOptions.Protection = readOnly ? VolumeProtection::ReadOnly : VolumeProtection::None;
                     88: 
                     89:                        SetValue (configMap[L"MountVolumesRemovable"], DefaultMountOptions.Removable);
                     90:                        TC_CONFIG_SET (OpenExplorerWindowAfterMount);
                     91:                        SetValue (configMap[L"PreserveTimestamps"], DefaultMountOptions.PreserveTimestamps);
                     92:                        TC_CONFIG_SET (SaveHistory);
                     93:                        TC_CONFIG_SET (StartOnLogon);
                     94:                        TC_CONFIG_SET (UseKeyfiles);
                     95:                        TC_CONFIG_SET (WipeCacheOnAutoDismount);
                     96:                        TC_CONFIG_SET (WipeCacheOnClose);
                     97:                }
                     98: 
                     99:                // Default keyfiles
                    100:                cfgPath = Application::GetConfigFilePath (GetDefaultKeyfilesFileName());
                    101:                if (cfgPath.IsFile())
                    102:                {
                    103:                        foreach (const XmlNode &node, XmlParser (cfgPath).GetNodes (L"keyfile"))
                    104:                        {
                    105:                                DefaultKeyfiles.push_back (make_shared <Keyfile> ((wstring) node.InnerText));
                    106:                        }
                    107:                }
                    108:                
                    109: #ifdef TC_WINDOWS
                    110:                // Hotkeys
                    111:                Hotkeys = Hotkey::LoadList();
                    112: #endif
                    113:        }
                    114: 
                    115:        void UserPreferences::Save() const
                    116:        {
                    117:                // Preferences
                    118:                class ConfigXmlFormatter
                    119:                {
                    120:                public:
                    121:                        void AddEntry (const wchar_t *key, const wxString &value)
                    122:                        {
                    123:                                if (!value.empty())
                    124:                                {
                    125:                                        XmlNode config (L"config");
                    126:                                        config.Attributes[L"key"] = key;
                    127:                                        config.InnerText = value;
                    128:                                        XmlConfig.InnerNodes.push_back (config);
                    129:                                }
                    130:                        }
                    131: 
                    132:                        void AddEntry (const wchar_t *key, bool value)
                    133:                        {
                    134:                                AddEntry (key, wxString (value ? L"1" : L"0"));
                    135:                        }
                    136: 
                    137:                        void AddEntry (const wchar_t *key, int value)
                    138:                        {
                    139:                                wstringstream s;
                    140:                                s << value;
                    141:                                AddEntry (key, s.str());
                    142:                        }
                    143: 
                    144:                        void AddEntry (const wchar_t *key, uint64 value)
                    145:                        {
                    146:                                wstringstream s;
                    147:                                s << value;
                    148:                                AddEntry (key, s.str());
                    149:                        }
                    150: 
                    151:                        XmlNode XmlConfig;
                    152:                };
                    153: 
                    154:                ConfigXmlFormatter formatter;
                    155:                formatter.XmlConfig.Name = L"configuration";
                    156: 
                    157: #define TC_CONFIG_ADD(NAME) formatter.AddEntry (L###NAME, NAME)
                    158: 
                    159:                TC_CONFIG_ADD (BackgroundTaskEnabled);
                    160:                TC_CONFIG_ADD (BackgroundTaskMenuDismountItemsEnabled);
                    161:                TC_CONFIG_ADD (BackgroundTaskMenuMountItemsEnabled);
                    162:                TC_CONFIG_ADD (BackgroundTaskMenuOpenItemsEnabled);
                    163:                TC_CONFIG_ADD (BeepAfterHotkeyMountDismount);
                    164:                formatter.AddEntry (L"CachePasswords", DefaultMountOptions.CachePassword);
                    165:                TC_CONFIG_ADD (CloseBackgroundTaskOnNoVolumes);
                    166:                TC_CONFIG_ADD (CloseExplorerWindowsOnDismount);
                    167:                TC_CONFIG_ADD (DismountOnInactivity);
                    168:                TC_CONFIG_ADD (DismountOnLogOff);
                    169:                TC_CONFIG_ADD (DismountOnPowerSaving);
                    170:                TC_CONFIG_ADD (DismountOnScreenSaver);
                    171:                TC_CONFIG_ADD (DisplayMessageAfterHotkeyDismount);
                    172:                TC_CONFIG_ADD (BackgroundTaskEnabled);
                    173:                formatter.AddEntry (L"FilesystemOptions", DefaultMountOptions.FilesystemOptions);
                    174:                TC_CONFIG_ADD (ForceAutoDismount);
                    175:                TC_CONFIG_ADD (LastSelectedSlotNumber);
                    176:                TC_CONFIG_ADD (MaxVolumeIdleTime);
                    177:                TC_CONFIG_ADD (MountDevicesOnLogon);
                    178:                TC_CONFIG_ADD (MountFavoritesOnLogon);
                    179:                formatter.AddEntry (L"MountVolumesReadOnly", DefaultMountOptions.Protection == VolumeProtection::ReadOnly);
                    180:                formatter.AddEntry (L"MountVolumesRemovable", DefaultMountOptions.Removable);
                    181:                TC_CONFIG_ADD (OpenExplorerWindowAfterMount);
                    182:                formatter.AddEntry (L"PreserveTimestamps", DefaultMountOptions.PreserveTimestamps);
                    183:                TC_CONFIG_ADD (SaveHistory);
                    184:                TC_CONFIG_ADD (StartOnLogon);
                    185:                TC_CONFIG_ADD (UseKeyfiles);
                    186:                TC_CONFIG_ADD (WipeCacheOnAutoDismount);
                    187:                TC_CONFIG_ADD (WipeCacheOnClose);
                    188: 
                    189:                XmlWriter writer (Application::GetConfigFilePath (GetPreferencesFileName(), true));
                    190:                writer.WriteNode (formatter.XmlConfig);
                    191:                writer.Close();
                    192: 
                    193:                // Default keyfiles
                    194:                FilePath keyfilesCfgPath = Application::GetConfigFilePath (GetDefaultKeyfilesFileName(), true);
                    195:                
                    196:                if (DefaultKeyfiles.empty())
                    197:                {
                    198:                        if (keyfilesCfgPath.IsFile())
                    199:                                keyfilesCfgPath.Delete();
                    200:                }
                    201:                else
                    202:                {
                    203:                        XmlNode keyfilesXml (L"defaultkeyfiles");
                    204: 
                    205:                        foreach_ref (const Keyfile &keyfile, DefaultKeyfiles)
                    206:                        {
                    207:                                keyfilesXml.InnerNodes.push_back (XmlNode (L"keyfile", wxString (FilesystemPath (keyfile))));
                    208:                        }
                    209: 
                    210:                        XmlWriter keyfileWriter (keyfilesCfgPath);
                    211:                        keyfileWriter.WriteNode (keyfilesXml);
                    212:                        keyfileWriter.Close();
                    213:                }
                    214: 
                    215: #ifdef TC_WINDOWS
                    216:                // Hotkeys
                    217:                Hotkey::SaveList (Hotkeys);
                    218: #endif
                    219:        }
                    220: }

unix.superglobalmegacorp.com

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