fileprop.cpp

 
//
// fileprop.cpp - Implementation of file properties functionality
// NoteXpad 2.0, Ryan Lederman, ryan@winprog.org
//
#include "fileprop.h"
#include "resource.h"
#include "files.h"
#include "macros.h"

HWND CreateFilePropertiesDlg( HWND hWndParent, TCHAR *szFileName )
{
//
// CreateFilePropertiesDlg
// creates the file properties dialog, duh!
//
if( !szFileName || !IsWindow( hWndParent ) )
return NULL;

return CreateDialogParam( GetModuleHandle( NULL ), (LPCTSTR)IDD_FILEPROP, hWndParent, FPDlgProc, (LPARAM)szFileName );
}

BOOL CALLBACK FPDlgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
static TCHAR *pFileName = 0;

switch( msg ) {
case WM_INITDIALOG:
pFileName = (TCHAR*)lParam;
OnInitHPDlg( hWnd, wParam, (LPARAM)pFileName );
break;
case WM_CLOSE: EndDialog( hWnd, 0 ); break;
case WM_COMMAND:
switch( LOWORD(wParam) ) {
case IDCANCEL: EndDialog( hWnd, 0 ); break;
case IDC_READONLY: OnChangeReadOnly( hWnd, wParam, (LPARAM)pFileName ); break;
case IDC_HIDDEN: OnChangeHidden( hWnd, wParam, (LPARAM)pFileName ); break;
}
}

return FALSE;
}

BOOL OnInitHPDlg( HWND hWnd, WPARAM wParam, LPARAM lParam )
{
//
// OnInitHPDlg
// called when dialog is being created. Initializes
// all fields
//
TCHAR *szFileName = (TCHAR*)lParam;
TCHAR szFileShort[MAX_PATH+1] = {0};
TCHAR szTemp[MAX_PATH+1] = {0};
TCHAR szAmPm[3]          = {0};
DWORD dwFileAttr  = 0;
int   n           = 0;
HANDLE hFile      = 0;
FILETIME ft_c     = {0};
FILETIME ft_m     = {0};
FILETIME ft_a     = {0};
FILETIME lft_c    = {0};
FILETIME lft_m    = {0};
FILETIME lft_a    = {0};
SYSTEMTIME st_c   = {0};
SYSTEMTIME st_m   = {0};
SYSTEMTIME st_a   = {0};

hFile = CreateFile( szFileName,
0,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0 );

if( hFile == INVALID_HANDLE_VALUE )
{
MSGBOX_ERROR( T("Failed to get file properties!") );
return FALSE;
}
//
// Get date information
// and translate to a more usable format
//

GetFileTime( hFile, &ft_c, &ft_a, &ft_m );
FileTimeToLocalFileTime( &ft_c, &lft_c );
FileTimeToLocalFileTime( &ft_m, &lft_m );
FileTimeToLocalFileTime( &ft_a, &lft_a );
FileTimeToSystemTime( &lft_c, &st_c );
FileTimeToSystemTime( &lft_m, &st_m );
FileTimeToSystemTime( &lft_a, &st_a );
//
// File name
//
FileNameFromPath( szFileName, szFileShort, MAX_PATH+1 );
SetDlgItemText( hWnd, IDC_FILENAME, szFileShort );

//
// Dates
//
PrintFileTime( &st_c, szTemp );
SetDlgItemText( hWnd, IDC_CREATED, szTemp );

memset( szTemp, 0, MAX_PATH+1 );
PrintFileTime( &st_m, szTemp );
SetDlgItemText( hWnd, IDC_MODIFIED, szTemp );

memset( szTemp, 0, MAX_PATH+1 );
PrintFileTime( &st_a, szTemp );
SetDlgItemText( hWnd, IDC_LASTACCESSED, szTemp );

//
// Directory
//
memset( szTemp, 0, MAX_PATH+1 );
strcpy( szTemp, szFileName );
for( n = _tcslen( szTemp ); n > 0; n-- ) {
if( szTemp[n] == '\\' ) {
szTemp[n] = 0; break; }
}
SetDlgItemText( hWnd, IDC_DIRECTORY, szTemp );

//
// File size
//
memset( szTemp, 0, MAX_PATH+1 );
wsprintf( szTemp, "%d bytes", GetFileSize( hFile, NULL ) );
SetDlgItemText( hWnd, IDC_FILESIZE, szTemp );

//
// Attributes
//
dwFileAttr = GetFileAttributes( szFileName );

if( dwFileAttr & FILE_ATTRIBUTE_READONLY )
CheckDlgButton( hWnd, IDC_READONLY, BST_CHECKED );
if( dwFileAttr & FILE_ATTRIBUTE_HIDDEN )
CheckDlgButton( hWnd, IDC_HIDDEN, BST_CHECKED );

CloseHandle( hFile );

ShowWindow( hWnd, SW_SHOW );

return FALSE;
}

BOOL OnChangeReadOnly( HWND hWnd, WPARAM wParam, LPARAM lParam )
{
//
// OnChangeReadOnly
// called when user changes read only attribute of file
//
DWORD dwFileAttributes = 0;
TCHAR *szFileName = (TCHAR*)lParam;

dwFileAttributes = GetFileAttributes( szFileName );

if( IsDlgButtonChecked( hWnd, IDC_READONLY ) == BST_CHECKED )
{
// Remove read only attribute
if( !SetFileAttributes( szFileName, dwFileAttributes | FILE_ATTRIBUTE_READONLY ) )
MSGBOX_ERROR( T("Failed to modify attributes of file!") );
}
else
{
// Set read only attribute
if( !SetFileAttributes( szFileName, dwFileAttributes & ~FILE_ATTRIBUTE_READONLY ) )
MSGBOX_ERROR( T("Failed to modify attributes of file!") );
}

return FALSE;
}

BOOL OnChangeHidden( HWND hWnd, WPARAM wParam, LPARAM lParam )
{
//
// OnChangeHidden
// called when user changes hidden attribute of file
//
DWORD dwFileAttributes = 0;
TCHAR *szFileName = (TCHAR*)lParam;

dwFileAttributes = GetFileAttributes( szFileName );

if( IsDlgButtonChecked( hWnd, IDC_HIDDEN ) == BST_CHECKED )
{
// Remove read only attribute
if( !SetFileAttributes( szFileName, dwFileAttributes | FILE_ATTRIBUTE_HIDDEN ) )
MSGBOX_ERROR( T("Failed to modify attributes of file!") );
}
else
{
// Set read only attribute
if( !SetFileAttributes( szFileName, dwFileAttributes & ~FILE_ATTRIBUTE_HIDDEN ) )
MSGBOX_ERROR( T("Failed to modify attributes of file!") );
}

return FALSE;
}

void PrintFileTime( SYSTEMTIME *st, TCHAR *szDest )
{
//
// PrintFileTime
// returns a buffer that represents the system time
// printed to human-readable format
//
TCHAR szAmPm[3] = {0};

if( !st || !szDest )
return;

strcpy( szAmPm, "AM" ); // AM by default

if( st->wHour == 0 )
st->wHour = 12;

if( st->wHour > 12 ) {
st->wHour -=12;
memset( szAmPm, 0, 3 );
strcpy( szAmPm, "PM" );
}

wsprintf( szDest,
  "%02d:%02d:%02d %s - %02d/%02d/%04d",
  st->wHour,
  st->wMinute,
  st->wSecond,
  szAmPm,
  st->wMonth,
  st->wDay,
  st->wYear );
return;
}

Project Homepage: