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

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

unix.superglobalmegacorp.com

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