Annotation of truecrypt/mount/favorites.cpp, revision 1.1.1.4

1.1       root        1: /*
                      2:  Copyright (c) 2010 TrueCrypt Developers Association. All rights reserved.
                      3: 
                      4:  Governed by the TrueCrypt License 3.0 the full text of which is contained in
                      5:  the file License.txt included in TrueCrypt binary and source code distribution
                      6:  packages.
                      7: */
                      8: 
                      9: #include "Tcdefs.h"
                     10: #include "Platform/Finally.h"
                     11: #include "Platform/ForEach.h"
                     12: #include "BootEncryption.h"
                     13: #include "Dlgcode.h"
                     14: #include "Language.h"
                     15: #include "Mount.h"
                     16: #include "Resource.h"
                     17: #include "Xml.h"
                     18: #include "Favorites.h"
                     19: 
                     20: using namespace std;
                     21: 
                     22: namespace TrueCrypt
                     23: {
                     24:        vector <FavoriteVolume> FavoriteVolumes;
                     25:        vector <FavoriteVolume> SystemFavoriteVolumes;
                     26:        list <FavoriteVolume> FavoritesOnArrivalMountRequired;
                     27:        list <FavoriteVolume> FavoritesMountedOnArrivalStillConnected;
                     28:        HMENU FavoriteVolumesMenu;
                     29: 
                     30: 
                     31:        BOOL AddMountedVolumeToFavorites (HWND hwndDlg, int driveNo, bool systemFavorites)
                     32:        {
                     33:                VOLUME_PROPERTIES_STRUCT prop;
                     34:                DWORD bytesReturned;
                     35: 
                     36:                memset (&prop, 0, sizeof (prop));
                     37:                prop.driveNo = driveNo;
                     38: 
                     39:                if (!DeviceIoControl (hDriver, TC_IOCTL_GET_VOLUME_PROPERTIES, &prop, sizeof (prop), &prop, sizeof (prop), &bytesReturned, NULL))
                     40:                {
                     41:                        handleWin32Error (hwndDlg);
                     42:                        return FALSE;
                     43:                }
                     44: 
                     45:                FavoriteVolume favorite;
                     46:                favorite.MountPoint = "X:\\";
                     47:                favorite.MountPoint[0] = (char) (prop.driveNo + 'A');
                     48: 
                     49:                favorite.Path = WideToSingleString ((wchar_t *) prop.wszVolume);
                     50:                if (favorite.Path.find ("\\??\\") == 0)
                     51:                        favorite.Path = favorite.Path.substr (4);
                     52: 
                     53:                if (IsVolumeDeviceHosted (favorite.Path.c_str()))
                     54:                {
                     55:                        // Get GUID path
                     56:                        string volumeDevPath = favorite.Path;
                     57: 
                     58:                        wchar_t resolvedVolumeDevPath[TC_MAX_PATH];
                     59:                        if (ResolveSymbolicLink (SingleStringToWide (volumeDevPath).c_str(), resolvedVolumeDevPath))
                     60:                                volumeDevPath = WideToSingleString (resolvedVolumeDevPath);
                     61: 
                     62:                        char volumeName[TC_MAX_PATH];
                     63:                        HANDLE find = FindFirstVolume (volumeName, sizeof (volumeName));
                     64: 
                     65:                        if (find != INVALID_HANDLE_VALUE)
                     66:                        {
                     67:                                do
                     68:                                {
                     69:                                        char findVolumeDevPath[TC_MAX_PATH];
                     70:                                        string vn = volumeName;
                     71: 
                     72:                                        if (QueryDosDevice (vn.substr (4, vn.size() - 5).c_str(), findVolumeDevPath, sizeof (findVolumeDevPath)) != 0
                     73:                                                && volumeDevPath == findVolumeDevPath)
                     74:                                        {
                     75:                                                favorite.VolumePathId = volumeName;
                     76:                                                break;
                     77:                                        }
                     78: 
                     79:                                } while (FindNextVolume (find, volumeName, sizeof (volumeName)));
                     80: 
                     81:                                FindVolumeClose (find);
                     82:                        }
                     83:                }
                     84: 
                     85:                favorite.ReadOnly = prop.readOnly ? true : false;
                     86:                favorite.Removable = prop.removable ? true : false;
                     87:                favorite.SystemEncryption = prop.partitionInInactiveSysEncScope ? true : false;
                     88:                favorite.OpenExplorerWindow = (bExplore == TRUE);
                     89: 
1.1.1.2   root       90:                if (favorite.VolumePathId.empty()
                     91:                        && IsVolumeDeviceHosted (favorite.Path.c_str())
                     92:                        && favorite.Path.find ("\\\\?\\Volume{") != 0)
                     93:                {
                     94:                        Warning (favorite.Path.find ("\\Partition0") == string::npos ? "FAVORITE_ADD_PARTITION_TYPE_WARNING" : "FAVORITE_ADD_DRIVE_DEV_WARNING");
                     95:                }
                     96: 
1.1       root       97:                return OrganizeFavoriteVolumes (hwndDlg, systemFavorites, favorite);
                     98:        }
                     99: 
                    100: 
                    101:        static BOOL CALLBACK FavoriteVolumesDlgProc (HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
                    102:        {
                    103:                /* This dialog is used both for System Favorites and non-system Favorites. 
                    104: 
                    105:                The following options have different meaning in System Favorites mode:
                    106: 
                    107:                IDC_FAVORITE_OPEN_EXPLORER_WIN_ON_MOUNT => MOUNT_SYSTEM_FAVORITES_ON_BOOT
                    108:                IDC_FAVORITE_DISABLE_HOTKEY             => DISABLE_NONADMIN_SYS_FAVORITES_ACCESS
                    109: 
                    110:                */
                    111: 
                    112:                WORD lw = LOWORD (wParam);
                    113:                static bool SystemFavoritesMode;
                    114:                static vector <FavoriteVolume> Favorites;
                    115:                static int SelectedItem;
                    116:                static HWND FavoriteListControl;
                    117: 
                    118:                switch (msg)
                    119:                {
                    120:                case WM_INITDIALOG:
                    121:                        {
                    122:                                try
                    123:                                {
                    124:                                        FavoriteListControl = GetDlgItem (hwndDlg, IDC_FAVORITE_VOLUMES_LIST);
                    125: 
                    126:                                        FavoriteVolumesDlgProcArguments *args = (FavoriteVolumesDlgProcArguments *) lParam;
                    127:                                        SystemFavoritesMode = args->SystemFavorites;
                    128: 
1.1.1.2   root      129:                                        LocalizeDialog (hwndDlg, SystemFavoritesMode ? "SYSTEM_FAVORITES_DLG_TITLE" : "IDD_FAVORITE_VOLUMES");
                    130: 
1.1       root      131:                                        if (SystemFavoritesMode)
                    132:                                        {
                    133:                                                RECT rec;
                    134: 
                    135:                                                BootEncryptionStatus bootEncStatus = BootEncryption (hwndDlg).GetStatus();
                    136: 
                    137:                                                if (!bootEncStatus.DriveMounted)
                    138:                                                        throw ErrorException ("SYS_FAVORITES_REQUIRE_PBA");
                    139: 
                    140:                                                ShowWindow (GetDlgItem(hwndDlg, IDC_FAVORITE_MOUNT_ON_LOGON), SW_HIDE);
                    141:                                                ShowWindow (GetDlgItem(hwndDlg, IDC_FAVORITE_MOUNT_ON_ARRIVAL), SW_HIDE);
                    142: 
                    143:                                                // MOUNT_SYSTEM_FAVORITES_ON_BOOT
                    144: 
                    145:                                                SetWindowTextW (GetDlgItem (hwndDlg, IDC_FAVORITE_OPEN_EXPLORER_WIN_ON_MOUNT), GetString ("MOUNT_SYSTEM_FAVORITES_ON_BOOT"));
                    146:                                                
                    147:                                                // DISABLE_NONADMIN_SYS_FAVORITES_ACCESS
                    148: 
                    149:                                                SetWindowTextW (GetDlgItem (hwndDlg, IDC_FAVORITE_DISABLE_HOTKEY), GetString ("DISABLE_NONADMIN_SYS_FAVORITES_ACCESS"));
                    150: 
                    151:                                                // Group box
                    152: 
                    153:                                                GetClientRect (GetDlgItem (hwndDlg, IDC_FAV_VOL_OPTIONS_GROUP_BOX), &rec);              
                    154: 
                    155:                                                SetWindowPos (GetDlgItem (hwndDlg, IDC_FAV_VOL_OPTIONS_GROUP_BOX), 0, 0, 0,
                    156:                                                        rec.right,
                    157:                                                        rec.bottom - CompensateYDPI (90),
                    158:                                                        SWP_NOMOVE | SWP_NOZORDER);
                    159: 
                    160:                                                InvalidateRect (GetDlgItem (hwndDlg, IDC_FAV_VOL_OPTIONS_GROUP_BOX), NULL, TRUE);
                    161:                                        }
                    162:                                        else
                    163:                                        {
                    164:                                                ShowWindow (GetDlgItem(hwndDlg, IDC_FAV_VOL_OPTIONS_GLOBAL_SETTINGS_BOX), SW_HIDE);
                    165:                                        }
                    166: 
                    167:                                        Favorites.clear();
                    168: 
                    169:                                        LVCOLUMNW column;
                    170:                                        SendMessageW (FavoriteListControl, LVM_SETEXTENDEDLISTVIEWSTYLE, 0, LVS_EX_FULLROWSELECT); 
                    171: 
                    172:                                        memset (&column, 0, sizeof (column));
                    173:                                        column.mask = LVCF_TEXT|LVCF_WIDTH|LVCF_SUBITEM|LVCF_FMT;
                    174:                                        column.pszText = GetString ("DRIVE");
                    175:                                        column.cx = CompensateXDPI (38);
                    176:                                        column.fmt = LVCFMT_CENTER;
                    177:                                        SendMessageW (FavoriteListControl, LVM_INSERTCOLUMNW, 1, (LPARAM) &column);
                    178: 
                    179:                                        ++column.iSubItem;
                    180:                                        column.fmt = LVCFMT_LEFT;
                    181:                                        column.pszText = GetString ("LABEL");
                    182:                                        column.cx = CompensateXDPI (160);
                    183:                                        SendMessageW (FavoriteListControl, LVM_INSERTCOLUMNW, 2, (LPARAM) &column);
                    184: 
                    185:                                        ++column.iSubItem;
                    186:                                        column.fmt = LVCFMT_LEFT;
                    187:                                        column.pszText = GetString ("VOLUME");
                    188:                                        column.cx = CompensateXDPI (330);
                    189:                                        SendMessageW (FavoriteListControl, LVM_INSERTCOLUMNW, 3, (LPARAM) &column);
                    190: 
                    191:                                        SetControls (hwndDlg, FavoriteVolume(), SystemFavoritesMode, false);
                    192: 
                    193:                                        if (SystemFavoritesMode)
                    194:                                                LoadFavoriteVolumes (Favorites, true);
                    195:                                        else
                    196:                                                Favorites = FavoriteVolumes;
                    197: 
                    198:                                        if (args->AddFavoriteVolume)
                    199:                                                Favorites.push_back (args->NewFavoriteVolume);
                    200: 
                    201:                                        FillListControl (FavoriteListControl, Favorites);
                    202: 
                    203:                                        SelectedItem = -1;
                    204: 
                    205:                                        if (args->AddFavoriteVolume)
                    206:                                        {
                    207:                                                ListView_SetItemState (FavoriteListControl, Favorites.size() - 1, LVIS_SELECTED, LVIS_SELECTED);
                    208:                                                ListView_EnsureVisible (FavoriteListControl, Favorites.size() - 1, FALSE);
                    209:                                        }
                    210: 
                    211:                                        if (SystemFavoritesMode)
                    212:                                                SetDlgItemTextW (hwndDlg, IDC_FAVORITES_HELP_LINK, GetString ("SYS_FAVORITES_HELP_LINK"));
                    213: 
                    214:                                        ToHyperlink (hwndDlg, IDC_FAVORITES_HELP_LINK);
                    215:                                }
                    216:                                catch (Exception &e)
                    217:                                {
                    218:                                        e.Show (hwndDlg);
                    219:                                        EndDialog (hwndDlg, IDCLOSE);
                    220:                                }
                    221:                        }
                    222:                        return 1;
                    223: 
                    224:                case WM_COMMAND:
                    225: 
                    226:                        switch (lw)
                    227:                        {
                    228:                        case IDOK:
                    229: 
                    230:                                /* Global System Favorites settings */
                    231: 
                    232:                                if (SystemFavoritesMode)
                    233:                                {
                    234:                                        BootEncryption BootEncObj (NULL);
                    235: 
                    236:                                        if (BootEncObj.GetStatus().DriveMounted)
                    237:                                        {
                    238:                                                try
                    239:                                                {
                    240:                                                        uint32 reqConfig = IsDlgButtonChecked (hwndDlg, IDC_FAVORITE_OPEN_EXPLORER_WIN_ON_MOUNT) ? TC_DRIVER_CONFIG_CACHE_BOOT_PASSWORD_FOR_SYS_FAVORITES : 0;
                    241:                                                        if (reqConfig != (ReadDriverConfigurationFlags() & TC_DRIVER_CONFIG_CACHE_BOOT_PASSWORD_FOR_SYS_FAVORITES))
                    242:                                                                BootEncObj.RegisterSystemFavoritesService (reqConfig ? TRUE : FALSE);
                    243: 
                    244:                                                        SetDriverConfigurationFlag (TC_DRIVER_CONFIG_DISABLE_NONADMIN_SYS_FAVORITES_ACCESS, IsDlgButtonChecked (hwndDlg, IDC_FAVORITE_DISABLE_HOTKEY));
                    245:                                                }
                    246:                                                catch (Exception &e)
                    247:                                                {
                    248:                                                        e.Show (hwndDlg);
                    249:                                                }
                    250:                                        }
                    251:                                }
                    252: 
                    253:                                /* (System) Favorites list */
                    254: 
                    255:                                if (SelectedItem != -1 && !Favorites.empty())
                    256:                                        SetFavoriteVolume (hwndDlg, Favorites[SelectedItem], SystemFavoritesMode);
                    257: 
                    258:                                if (SaveFavoriteVolumes (Favorites, SystemFavoritesMode))
                    259:                                {
                    260:                                        if (!SystemFavoritesMode)
                    261:                                        {
                    262:                                                bMountFavoritesOnLogon = FALSE;
                    263: 
                    264:                                                foreach (const FavoriteVolume &favorite, Favorites)
                    265:                                                {
                    266:                                                        if (favorite.MountOnLogOn)
                    267:                                                        {
                    268:                                                                bMountFavoritesOnLogon = TRUE;
                    269:                                                                break;
                    270:                                                        }
                    271:                                                }
                    272: 
                    273:                                                if (!bEnableBkgTask || bCloseBkgTaskWhenNoVolumes || IsNonInstallMode())
                    274:                                                {
                    275:                                                        foreach (const FavoriteVolume favorite, Favorites)
                    276:                                                        {
                    277:                                                                if (favorite.MountOnArrival)
                    278:                                                                {
                    279:                                                                        Warning ("FAVORITE_ARRIVAL_MOUNT_BACKGROUND_TASK_ERR");
                    280:                                                                        break;
                    281:                                                                }
                    282:                                                        }
                    283:                                                }
                    284: 
                    285:                                                FavoriteVolumes = Favorites;
                    286: 
                    287:                                                ManageStartupSeq();
                    288:                                                SaveSettings (hwndDlg);
                    289:                                        }
                    290:                                        else
                    291:                                                SystemFavoriteVolumes = Favorites;
                    292: 
                    293:                                        OnFavoriteVolumesUpdated();
                    294:                                        LoadDriveLetters (GetDlgItem (MainDlg, IDC_DRIVELIST), 0);
                    295: 
                    296:                                        EndDialog (hwndDlg, IDOK);
                    297:                                }
                    298: 
                    299:                                return 1;
                    300: 
                    301:                        case IDCANCEL:
                    302:                                EndDialog (hwndDlg, IDCLOSE);
                    303:                                return 1;
                    304: 
                    305:                        case IDC_FAVORITE_MOVE_DOWN:
                    306:                                if (SelectedItem != -1 && Favorites.size() > (size_t) SelectedItem + 1)
                    307:                                {
                    308:                                        swap (Favorites[SelectedItem], Favorites[SelectedItem + 1]);
                    309: 
                    310:                                        FillListControl (FavoriteListControl, Favorites);
                    311:                                        ++SelectedItem;
                    312:                                        ListView_SetItemState (FavoriteListControl, SelectedItem, LVIS_SELECTED, LVIS_SELECTED);
                    313:                                        ListView_EnsureVisible (FavoriteListControl, SelectedItem, FALSE);
                    314:                                }
                    315:                                return 1;
                    316: 
                    317:                        case IDC_FAVORITE_MOVE_UP:
                    318:                                if (SelectedItem > 0)
                    319:                                {
                    320:                                        swap (Favorites[SelectedItem], Favorites[SelectedItem - 1]);
                    321: 
                    322:                                        FillListControl (FavoriteListControl, Favorites);
                    323:                                        --SelectedItem;
                    324:                                        ListView_SetItemState (FavoriteListControl, SelectedItem, LVIS_SELECTED, LVIS_SELECTED);
                    325:                                        ListView_EnsureVisible (FavoriteListControl, SelectedItem, FALSE);
                    326:                                }
                    327:                                return 1;
                    328: 
                    329:                        case IDC_FAVORITE_REMOVE:
                    330:                                if (SelectedItem != -1)
                    331:                                {
                    332:                                        Favorites.erase (Favorites.begin() + SelectedItem);
                    333:                                        FillListControl (GetDlgItem (hwndDlg, IDC_FAVORITE_VOLUMES_LIST), Favorites);
                    334:                                        SetControls (hwndDlg, FavoriteVolume(), SystemFavoritesMode, false);
                    335:                                        SelectedItem = -1;
                    336:                                }
                    337:                                return 1;
                    338: 
                    339: 
                    340:                        case IDC_FAVORITE_OPEN_EXPLORER_WIN_ON_MOUNT:   // Note that this option means "MOUNT_SYSTEM_FAVORITES_ON_BOOT" when SystemFavoritesMode is true
                    341:                                if (SystemFavoritesMode)
                    342:                                {
                    343:                                        // MOUNT_SYSTEM_FAVORITES_ON_BOOT
                    344: 
                    345:                                        if (IsDlgButtonChecked (hwndDlg, IDC_FAVORITE_OPEN_EXPLORER_WIN_ON_MOUNT))
                    346:                                        {
                    347:                                                WarningDirect ((wstring (GetString ("SYS_FAVORITES_KEYBOARD_WARNING")) + L"\n\n" + GetString ("BOOT_PASSWORD_CACHE_KEYBOARD_WARNING")).c_str());
                    348: 
                    349:                                                if (!IsServerOS() && !IsDlgButtonChecked (hwndDlg, IDC_FAVORITE_DISABLE_HOTKEY))
                    350:                                                        Info ("SYS_FAVORITES_ADMIN_ONLY_INFO");
                    351:                                        }
                    352:                                }
                    353:                                return 1;
                    354: 
                    355:                        case IDC_FAVORITE_DISABLE_HOTKEY: // Note that this option means "DISABLE_NONADMIN_SYS_FAVORITES_ACCESS" when SystemFavoritesMode is true
                    356:                                if (SystemFavoritesMode)
                    357:                                {
                    358:                                        // DISABLE_NONADMIN_SYS_FAVORITES_ACCESS
                    359: 
                    360:                                        if (IsDlgButtonChecked (hwndDlg, IDC_FAVORITE_DISABLE_HOTKEY))
                    361:                                                WarningDirect ((wstring (GetString ("SYS_FAVORITES_ADMIN_ONLY_WARNING")) + L"\n\n" + GetString ("SETTING_REQUIRES_REBOOT")).c_str());
                    362:                                        else
                    363:                                                Warning ("SETTING_REQUIRES_REBOOT");
                    364:                                }
                    365:                                return 1;
                    366: 
                    367:                        case IDC_FAVORITES_HELP_LINK:
                    368:                                Applink (SystemFavoritesMode ? "sysfavorites" : "favorites", TRUE, "");
                    369:                                return 1;
                    370:                        }
                    371: 
                    372:                        return 0;
                    373: 
                    374:                case WM_NOTIFY:
                    375:                        if (((LPNMHDR) lParam)->code == LVN_ITEMCHANGED)
                    376:                        {
                    377:                                static bool reentry = false;
                    378:                                if (reentry)
                    379:                                        break;
                    380: 
                    381:                                reentry = true;
                    382: 
                    383:                                if (SelectedItem != -1)
                    384:                                {
                    385:                                        SetFavoriteVolume (hwndDlg, Favorites[SelectedItem], SystemFavoritesMode);
                    386:                                        FillListControlSubItems (FavoriteListControl, SelectedItem, Favorites[SelectedItem]);
                    387:                                }
                    388: 
                    389:                                SelectedItem = ListView_GetNextItem (GetDlgItem (hwndDlg, IDC_FAVORITE_VOLUMES_LIST), -1, LVIS_SELECTED);
                    390: 
                    391:                                if (SelectedItem != -1)
                    392:                                        SetControls (hwndDlg, Favorites[SelectedItem], SystemFavoritesMode);
                    393:                                else
                    394:                                        SetControls (hwndDlg, FavoriteVolume(), SystemFavoritesMode, false);
                    395: 
                    396:                                reentry = false;
                    397:                                return 1;
                    398:                        }
                    399:                        break;
                    400: 
                    401:                case WM_CLOSE:
                    402:                        EndDialog (hwndDlg, IDCLOSE);
                    403:                        return 1;
                    404:                }
                    405: 
                    406:                return 0;
                    407:        }
                    408: 
                    409: 
                    410:        static void FillFavoriteVolumesMenu ()
                    411:        {
                    412:                while (DeleteMenu (FavoriteVolumesMenu, 7, MF_BYPOSITION)) { }
                    413: 
                    414:                if (FavoriteVolumes.empty())
                    415:                        return;
                    416: 
                    417:                AppendMenu (FavoriteVolumesMenu, MF_SEPARATOR, 0, NULL);
                    418:                
                    419:                int i = 0;
                    420:                foreach (const FavoriteVolume &favorite, FavoriteVolumes)
                    421:                {
                    422:                        UINT flags = MF_STRING;
                    423: 
                    424:                        if (favorite.DisconnectedDevice)
                    425:                                flags |= MF_GRAYED;
                    426: 
                    427:                        wstring menuText = SingleStringToWide (favorite.Path);
                    428:                        if (favorite.DisconnectedDevice)
1.1.1.4 ! root      429:                                menuText = favorite.Label.empty() ? wstring (L"(") + GetString ("FAVORITE_DISCONNECTED_DEV") + L")" : L"";
1.1       root      430: 
                    431:                        if (!favorite.Label.empty())
                    432:                        {
                    433:                                if (favorite.DisconnectedDevice)
                    434:                                        menuText = favorite.Label + L" " + menuText;
                    435:                                else
                    436:                                        menuText = favorite.Label;
                    437:                        }
                    438: 
                    439:                        AppendMenuW (FavoriteVolumesMenu, flags, TC_FAVORITE_MENU_CMD_ID_OFFSET + i++,
                    440:                                (menuText + L"\t" + SingleStringToWide (favorite.MountPoint).substr (0, 2)).c_str());
                    441:                }
                    442:        }
                    443: 
                    444: 
                    445:        static void FillListControl (HWND favoriteListControl, vector <FavoriteVolume> &favorites)
                    446:        {
                    447:                SendMessage (favoriteListControl, LVM_DELETEALLITEMS, 0, 0);
                    448: 
                    449:                int line = 0;
                    450:                foreach (const FavoriteVolume favorite, favorites)
                    451:                {
                    452:                        ListItemAdd (favoriteListControl, line, (char *) favorite.MountPoint.substr (0, 2).c_str());
                    453:                        FillListControlSubItems (favoriteListControl, line++, favorite);
                    454:                }
                    455:        }
                    456: 
                    457: 
                    458:        static void FillListControlSubItems (HWND FavoriteListControl, int line, const FavoriteVolume &favorite)
                    459:        {
                    460:                ListSubItemSetW (FavoriteListControl, line, 1, (wchar_t *) favorite.Label.c_str());
                    461: 
                    462:                if (favorite.DisconnectedDevice)
                    463:                        ListSubItemSetW (FavoriteListControl, line, 2, (wchar_t *) (wstring (L"(") + GetString ("FAVORITE_DISCONNECTED_DEV") + L")").c_str());
                    464:                else
                    465:                        ListSubItemSet (FavoriteListControl, line, 2, (char *) favorite.Path.c_str());
                    466:        }
                    467: 
                    468: 
                    469:        wstring GetFavoriteVolumeLabel (const string &volumePath)
                    470:        {
                    471:                foreach (const FavoriteVolume &favorite, FavoriteVolumes)
                    472:                {
                    473:                        if (favorite.Path == volumePath)
                    474:                                return favorite.Label;
                    475:                }
                    476: 
                    477:                foreach (const FavoriteVolume &favorite, SystemFavoriteVolumes)
                    478:                {
                    479:                        if (favorite.Path == volumePath)
                    480:                                return favorite.Label;
                    481:                }
                    482: 
                    483:                return wstring();
                    484:        }
                    485: 
                    486: 
                    487:        void LoadFavoriteVolumes ()
                    488:        {
                    489:                LoadFavoriteVolumes (FavoriteVolumes, false);
                    490: 
                    491:                try
                    492:                {
                    493:                        LoadFavoriteVolumes (SystemFavoriteVolumes, true, true);
                    494:                }
                    495:                catch (...) { } // Ignore errors as SystemFavoriteVolumes list is used only for resolving volume paths to labels
                    496: 
                    497:                OnFavoriteVolumesUpdated();
                    498:        }
                    499: 
                    500: 
                    501:        void LoadFavoriteVolumes (vector <FavoriteVolume> &favorites, bool systemFavorites, bool noUacElevation)
                    502:        {
                    503:                favorites.clear();
                    504:                string favoritesFilePath = systemFavorites ? GetServiceConfigPath (TC_APPD_FILENAME_SYSTEM_FAVORITE_VOLUMES) : GetConfigPath (TC_APPD_FILENAME_FAVORITE_VOLUMES);
                    505: 
                    506:                if (systemFavorites && !IsAdmin() && !noUacElevation)
                    507:                {
                    508:                        favoritesFilePath = GetConfigPath (TC_APPD_FILENAME_SYSTEM_FAVORITE_VOLUMES);
                    509: 
                    510:                        try
                    511:                        {
                    512:                                BootEncryption bootEnc (MainDlg);
                    513:                                bootEnc.CopyFileAdmin (GetServiceConfigPath (TC_APPD_FILENAME_SYSTEM_FAVORITE_VOLUMES).c_str(), favoritesFilePath.c_str());
                    514:                        }
                    515:                        catch (SystemException &e)
                    516:                        {
                    517:                                if (e.ErrorCode == ERROR_FILE_NOT_FOUND)
                    518:                                        return;
                    519: 
                    520:                                throw;
                    521:                        }
                    522:                }
                    523: 
                    524:                DWORD size;
                    525:                char *favoritesXml = LoadFile (favoritesFilePath.c_str(), &size);
                    526: 
                    527:                if (systemFavorites && !IsAdmin() && !noUacElevation)
                    528:                        DeleteFile (GetConfigPath (TC_APPD_FILENAME_SYSTEM_FAVORITE_VOLUMES));
                    529: 
                    530:                char *xml = favoritesXml;
                    531:                char mountPoint[MAX_PATH], volume[MAX_PATH];
                    532: 
                    533:                if (xml == NULL)
                    534:                        return;
                    535: 
                    536:                while (xml = XmlFindElement (xml, "volume"))
                    537:                {
                    538:                        FavoriteVolume favorite;
                    539: 
                    540:                        XmlGetAttributeText (xml, "mountpoint", mountPoint, sizeof (mountPoint));
                    541:                        favorite.MountPoint = mountPoint;
                    542: 
                    543:                        XmlGetNodeText (xml, volume, sizeof (volume));
1.1.1.3   root      544:                        favorite.Path = WideToSingleString (Utf8StringToWide (volume));
1.1       root      545: 
                    546:                        char label[1024];
                    547:                        XmlGetAttributeText (xml, "label", label, sizeof (label));
                    548:                        favorite.Label = Utf8StringToWide (label);
                    549: 
                    550:                        char boolVal[2];
                    551:                        XmlGetAttributeText (xml, "readonly", boolVal, sizeof (boolVal));
                    552:                        if (boolVal[0])
                    553:                                favorite.ReadOnly = (boolVal[0] == '1');
                    554: 
                    555:                        XmlGetAttributeText (xml, "removable", boolVal, sizeof (boolVal));
                    556:                        if (boolVal[0])
                    557:                                favorite.Removable = (boolVal[0] == '1');
                    558: 
                    559:                        XmlGetAttributeText (xml, "system", boolVal, sizeof (boolVal));
                    560:                        if (boolVal[0])
                    561:                                favorite.SystemEncryption = (boolVal[0] == '1');
                    562: 
                    563:                        XmlGetAttributeText (xml, "noHotKeyMount", boolVal, sizeof (boolVal));
                    564:                        if (boolVal[0])
                    565:                                favorite.DisableHotkeyMount = (boolVal[0] == '1');
                    566: 
                    567:                        XmlGetAttributeText (xml, "openExplorerWindow", boolVal, sizeof (boolVal));
                    568:                        if (boolVal[0])
                    569:                                favorite.OpenExplorerWindow = (boolVal[0] == '1');
                    570: 
                    571:                        XmlGetAttributeText (xml, "mountOnArrival", boolVal, sizeof (boolVal));
                    572:                        if (boolVal[0])
                    573:                                favorite.MountOnArrival = (boolVal[0] == '1');
                    574: 
                    575:                        XmlGetAttributeText (xml, "mountOnLogOn", boolVal, sizeof (boolVal));
                    576:                        if (boolVal[0])
                    577:                                favorite.MountOnLogOn = (boolVal[0] == '1');
                    578: 
                    579:                        if (favorite.Path.find ("\\\\?\\Volume{") == 0 && favorite.Path.rfind ("}\\") == favorite.Path.size() - 2)
                    580:                        {
                    581:                                string resolvedPath = VolumeGuidPathToDevicePath (favorite.Path);
                    582:                                if (!resolvedPath.empty())
                    583:                                {
                    584:                                        favorite.DisconnectedDevice = false;
                    585:                                        favorite.VolumePathId = favorite.Path;
                    586:                                        favorite.Path = resolvedPath;
                    587:                                }
                    588:                                else
                    589:                                        favorite.DisconnectedDevice = true;
                    590:                        }
                    591: 
                    592:                        favorites.push_back (favorite);
                    593:                        xml++;
                    594:                }
                    595: 
                    596:                free (favoritesXml);
                    597:        }
                    598: 
                    599: 
                    600:        static void OnFavoriteVolumesUpdated ()
                    601:        {
                    602:                FillFavoriteVolumesMenu();
                    603: 
                    604:                FavoritesOnArrivalMountRequired.clear();
                    605: 
                    606:                foreach (const FavoriteVolume favorite, FavoriteVolumes)
                    607:                {
                    608:                        if (favorite.MountOnArrival)
                    609:                        {
                    610:                                FavoritesOnArrivalMountRequired.push_back (favorite);
                    611: 
                    612:                                if (IsMountedVolume (favorite.Path.c_str()))
                    613:                                {
                    614:                                        bool present = false;
                    615: 
                    616:                                        foreach (const FavoriteVolume favoriteConnected, FavoritesMountedOnArrivalStillConnected)
                    617:                                        {
                    618:                                                if (favorite.Path == favoriteConnected.Path)
                    619:                                                {
                    620:                                                        present = true;
                    621:                                                        break;
                    622:                                                }
                    623:                                        }
                    624: 
                    625:                                        if (!present)
                    626:                                                FavoritesMountedOnArrivalStillConnected.push_back (favorite);
                    627:                                }
                    628:                        }
                    629:                }
                    630:        }
                    631: 
                    632: 
                    633:        BOOL OrganizeFavoriteVolumes (HWND hwndDlg, bool systemFavorites, const FavoriteVolume &newFavorite)
                    634:        {
                    635:                FavoriteVolumesDlgProcArguments args;
                    636:                args.SystemFavorites = systemFavorites;
                    637: 
                    638:                if (!newFavorite.Path.empty())
                    639:                {
                    640:                        args.AddFavoriteVolume = true;
                    641:                        args.NewFavoriteVolume = newFavorite;
                    642:                }
                    643:                else
                    644:                        args.AddFavoriteVolume = false;
                    645: 
                    646:                return DialogBoxParamW (hInst, MAKEINTRESOURCEW (IDD_FAVORITE_VOLUMES), hwndDlg, (DLGPROC) FavoriteVolumesDlgProc, (LPARAM) &args) == IDOK;
                    647:        }
                    648: 
                    649: 
                    650:        static bool SaveFavoriteVolumes (const vector <FavoriteVolume> &favorites, bool systemFavorites)
                    651:        {
                    652:                FILE *f;
                    653:                int cnt = 0;
                    654: 
                    655:                f = fopen (GetConfigPath (systemFavorites ? TC_APPD_FILENAME_SYSTEM_FAVORITE_VOLUMES : TC_APPD_FILENAME_FAVORITE_VOLUMES), "w,ccs=UTF-8");
                    656:                if (f == NULL)
                    657:                {
                    658:                        handleWin32Error (MainDlg);
                    659:                        return false;
                    660:                }
                    661: 
                    662:                XmlWriteHeaderW (f);
                    663:                fputws (L"\n\t<favorites>", f);
                    664: 
                    665:                foreach (const FavoriteVolume &favorite, favorites)
                    666:                {
                    667:                        char tq[2048];
                    668: 
                    669:                        if (systemFavorites && favorite.Path.find ("\\\\") == 0 && favorite.Path.find ("Volume{") == string::npos)
                    670:                                Warning ("SYSTEM_FAVORITE_NETWORK_PATH_ERR");
                    671: 
                    672:                        XmlQuoteText (!favorite.VolumePathId.empty() ? favorite.VolumePathId.c_str() : favorite.Path.c_str(), tq, sizeof (tq));
                    673: 
                    674:                        wstring s = L"\n\t\t<volume mountpoint=\"" + SingleStringToWide (favorite.MountPoint) + L"\"";
                    675: 
                    676:                        if (!favorite.Label.empty())
                    677:                                s += L" label=\"" + favorite.Label + L"\"";
                    678: 
                    679:                        if (favorite.ReadOnly)
                    680:                                s += L" readonly=\"1\"";
                    681:                        
                    682:                        if (favorite.Removable)
                    683:                                s += L" removable=\"1\"";
                    684: 
                    685:                        if (favorite.SystemEncryption)
                    686:                                s += L" system=\"1\"";
                    687: 
                    688:                        if (favorite.MountOnArrival)
                    689:                                s += L" mountOnArrival=\"1\"";
                    690: 
                    691:                        if (favorite.MountOnLogOn)
                    692:                                s += L" mountOnLogOn=\"1\"";
                    693:                        
                    694:                        if (favorite.DisableHotkeyMount)
                    695:                                s += L" noHotKeyMount=\"1\"";
                    696: 
                    697:                        if (favorite.OpenExplorerWindow)
                    698:                                s += L" openExplorerWindow=\"1\"";
                    699: 
                    700:                        s += L">" + SingleStringToWide (tq) + L"</volume>";
                    701: 
                    702:                        fwprintf (f, L"%ws", s.c_str());
                    703:                        cnt++;
                    704:                }
                    705: 
                    706:                fputws (L"\n\t</favorites>", f);
                    707:                XmlWriteFooterW (f);
                    708: 
                    709:                if (!CheckFileStreamWriteErrors (f, systemFavorites ? TC_APPD_FILENAME_SYSTEM_FAVORITE_VOLUMES : TC_APPD_FILENAME_FAVORITE_VOLUMES))
                    710:                {
                    711:                        fclose (f);
                    712:                        return false;
                    713:                }
                    714: 
                    715:                fclose (f);
                    716: 
                    717:                BootEncryption bootEnc (MainDlg);
                    718: 
                    719:                if (systemFavorites)
                    720:                {
                    721:                        finally_do ({ remove (GetConfigPath (TC_APPD_FILENAME_SYSTEM_FAVORITE_VOLUMES)); });
                    722: 
                    723:                        try
                    724:                        {
                    725:                                bootEnc.DeleteFileAdmin (GetServiceConfigPath (TC_APPD_FILENAME_SYSTEM_FAVORITE_VOLUMES).c_str());
                    726:                        }
                    727:                        catch (UserAbort&) { return false; }
                    728:                        catch (...) { }
                    729: 
                    730:                        try
                    731:                        {
                    732:                                if (cnt != 0)
                    733:                                {
                    734:                                        bootEnc.CopyFileAdmin (GetConfigPath (TC_APPD_FILENAME_SYSTEM_FAVORITE_VOLUMES), GetServiceConfigPath (TC_APPD_FILENAME_SYSTEM_FAVORITE_VOLUMES).c_str());
                    735: 
                    736:                                        if (!(ReadDriverConfigurationFlags() & TC_DRIVER_CONFIG_CACHE_BOOT_PASSWORD_FOR_SYS_FAVORITES))
                    737:                                                Info ("SYS_FAVORITE_VOLUMES_SAVED");
                    738:                                }
                    739:                        }
                    740:                        catch (Exception &e)
                    741:                        {
                    742:                                e.Show (NULL);
                    743:                        }
                    744:                }
                    745: 
                    746:                if (cnt == 0)
                    747:                {
                    748:                        if (systemFavorites)
                    749:                        {
                    750:                                try
                    751:                                {
                    752:                                        bootEnc.DeleteFileAdmin (GetServiceConfigPath (TC_APPD_FILENAME_SYSTEM_FAVORITE_VOLUMES).c_str());
                    753:                                }
                    754:                                catch (...) { }
                    755:                        }
                    756:                        else
                    757:                                remove (GetConfigPath (TC_APPD_FILENAME_FAVORITE_VOLUMES));
                    758:                }
                    759: 
                    760:                return true;
                    761:        }
                    762: 
                    763: 
                    764:        static void SetControls (HWND hwndDlg, const FavoriteVolume &favorite, bool systemFavoritesMode, bool enable)
                    765:        {
                    766:                SetDlgItemTextW (hwndDlg, IDC_FAVORITE_LABEL, favorite.Label.c_str());
                    767:                SetCheckBox (hwndDlg, IDC_FAVORITE_MOUNT_ON_LOGON, favorite.MountOnLogOn);
                    768:                SetCheckBox (hwndDlg, IDC_FAVORITE_MOUNT_ON_ARRIVAL, favorite.MountOnArrival);
                    769:                SetCheckBox (hwndDlg, IDC_FAVORITE_MOUNT_READONLY, favorite.ReadOnly);
                    770:                SetCheckBox (hwndDlg, IDC_FAVORITE_MOUNT_REMOVABLE, favorite.Removable);
                    771: 
                    772:                if (systemFavoritesMode)
                    773:                {
                    774:                        uint32 driverConfig = ReadDriverConfigurationFlags();
                    775: 
                    776:                        // MOUNT_SYSTEM_FAVORITES_ON_BOOT
                    777:                        CheckDlgButton (hwndDlg, IDC_FAVORITE_OPEN_EXPLORER_WIN_ON_MOUNT, (driverConfig & TC_DRIVER_CONFIG_CACHE_BOOT_PASSWORD_FOR_SYS_FAVORITES) ? BST_CHECKED : BST_UNCHECKED);
                    778: 
                    779:                        // DISABLE_NONADMIN_SYS_FAVORITES_ACCESS
                    780:                        CheckDlgButton (hwndDlg, IDC_FAVORITE_DISABLE_HOTKEY, (driverConfig & TC_DRIVER_CONFIG_DISABLE_NONADMIN_SYS_FAVORITES_ACCESS) ? BST_CHECKED : BST_UNCHECKED);
                    781:                }
                    782:                else
                    783:                {
                    784:                        SetCheckBox (hwndDlg, IDC_FAVORITE_OPEN_EXPLORER_WIN_ON_MOUNT, favorite.OpenExplorerWindow);
                    785:                        SetCheckBox (hwndDlg, IDC_FAVORITE_DISABLE_HOTKEY, favorite.DisableHotkeyMount);
                    786:                }
                    787: 
                    788:                EnableWindow (GetDlgItem (hwndDlg, IDC_FAVORITE_MOVE_UP), enable);
                    789:                EnableWindow (GetDlgItem (hwndDlg, IDC_FAVORITE_MOVE_DOWN), enable);
                    790:                EnableWindow (GetDlgItem (hwndDlg, IDC_FAVORITE_REMOVE), enable);
                    791:                EnableWindow (GetDlgItem (hwndDlg, IDT_FAVORITE_LABEL), enable);
                    792:                EnableWindow (GetDlgItem (hwndDlg, IDC_FAVORITE_LABEL), enable);
                    793:                EnableWindow (GetDlgItem (hwndDlg, IDC_FAVORITE_MOUNT_ON_LOGON), enable && !systemFavoritesMode);
                    794:                EnableWindow (GetDlgItem (hwndDlg, IDC_FAVORITE_MOUNT_ON_ARRIVAL), enable && !systemFavoritesMode);
                    795:                EnableWindow (GetDlgItem (hwndDlg, IDC_FAVORITE_MOUNT_READONLY), enable);
                    796:                EnableWindow (GetDlgItem (hwndDlg, IDC_FAVORITE_MOUNT_REMOVABLE), enable);
                    797:                EnableWindow (GetDlgItem (hwndDlg, IDC_FAVORITE_OPEN_EXPLORER_WIN_ON_MOUNT), enable || systemFavoritesMode);
                    798:                EnableWindow (GetDlgItem (hwndDlg, IDC_FAVORITE_DISABLE_HOTKEY), enable || systemFavoritesMode);
                    799:        }
                    800: 
                    801: 
                    802:        static void SetFavoriteVolume (HWND hwndDlg, FavoriteVolume &favorite, bool systemFavoritesMode)
                    803:        {
                    804:                wchar_t label[1024];
                    805:                if (GetDlgItemTextW (hwndDlg, IDC_FAVORITE_LABEL, label, ARRAYSIZE (label)) != 0)
1.1.1.3   root      806:                {
1.1       root      807:                        favorite.Label = label;
1.1.1.3   root      808: 
                    809:                        for (size_t i = 0; i < favorite.Label.size(); ++i)
                    810:                        {
                    811:                                if (favorite.Label[i] == L'"')
                    812:                                        favorite.Label.at (i) = L'\'';
                    813:                        }
                    814:                }
1.1       root      815:                else
                    816:                        favorite.Label.clear();
                    817: 
                    818:                favorite.ReadOnly = (IsDlgButtonChecked (hwndDlg, IDC_FAVORITE_MOUNT_READONLY) != 0);
                    819:                favorite.Removable = (IsDlgButtonChecked (hwndDlg, IDC_FAVORITE_MOUNT_REMOVABLE) != 0);
                    820: 
                    821:                if (!systemFavoritesMode)
                    822:                {
                    823:                        favorite.MountOnLogOn = (IsDlgButtonChecked (hwndDlg, IDC_FAVORITE_MOUNT_ON_LOGON) != 0);
                    824:                        favorite.MountOnArrival = (IsDlgButtonChecked (hwndDlg, IDC_FAVORITE_MOUNT_ON_ARRIVAL) != 0);
                    825:                        favorite.DisableHotkeyMount = (IsDlgButtonChecked (hwndDlg, IDC_FAVORITE_DISABLE_HOTKEY) != 0);
                    826:                        favorite.OpenExplorerWindow = (IsDlgButtonChecked (hwndDlg, IDC_FAVORITE_OPEN_EXPLORER_WIN_ON_MOUNT) != 0);
                    827:                }
                    828: 
                    829:                if (favorite.VolumePathId.empty()
                    830:                        && IsVolumeDeviceHosted (favorite.Path.c_str())
                    831:                        && favorite.Path.find ("\\\\?\\Volume{") != 0)
                    832:                {
1.1.1.2   root      833:                        bool partition = (favorite.Path.find ("\\Partition0") == string::npos);
                    834: 
1.1       root      835:                        if (!favorite.Label.empty())
                    836:                        {
1.1.1.2   root      837:                                ErrorDirect ((GetString (partition ? "FAVORITE_LABEL_PARTITION_TYPE_ERR" : "FAVORITE_LABEL_DEVICE_PATH_ERR") + wstring (L"\n\n") + SingleStringToWide (favorite.Path)).c_str());
1.1       root      838:                                favorite.Label.clear();
                    839:                        }
                    840: 
                    841:                        if (favorite.MountOnArrival)
                    842:                        {
1.1.1.2   root      843:                                ErrorDirect ((GetString (partition ? "FAVORITE_ARRIVAL_MOUNT_PARTITION_TYPE_ERR" : "FAVORITE_ARRIVAL_MOUNT_DEVICE_PATH_ERR") + wstring (L"\n\n") + SingleStringToWide (favorite.Path)).c_str());
1.1       root      844:                                favorite.MountOnArrival = false;
                    845:                        }
                    846:                }
                    847: 
                    848:                if (favorite.MountOnArrival && favorite.Path.find ("\\\\") == 0 && favorite.Path.find ("Volume{") == string::npos)
                    849:                {
                    850:                        Error ("FAVORITE_ARRIVAL_MOUNT_NETWORK_PATH_ERR");
                    851:                        favorite.MountOnArrival = false;
                    852:                }
                    853:        }
                    854: 
                    855: 
                    856:        void UpdateDeviceHostedFavoriteVolumes ()
                    857:        {
                    858:                try
                    859:                {
                    860:                        LoadFavoriteVolumes();
                    861:                }
                    862:                catch (Exception &e)
                    863:                {
                    864:                        e.Show (MainDlg);
                    865:                }
                    866:        }
                    867: }

unix.superglobalmegacorp.com

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