//\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
//
// CommonHelpers satellite library | Author: Sergei Y.Kitáev
//
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include "Precompiled.h"
#include "SharedIncludes.h"

namespace CommonHelpers {
//==================================================================================================

void SysHelpers_ShellLinkProperties::Arguments::set(String^ value)
{
   HArgChecking::VerifyNotNull( value );
   HDisposableSZ dszValue( value );
   HRESULT hResult = PSLInterface -> SetArguments( %dszValue );
   if ( FAILED( hResult ) )
      throw CreateOpFailedException_CantSetSLProperty( "Arguments", hResult );
}

//--------------------------------------------------------------------------------------------------

void SysHelpers_ShellLinkProperties::Description::set(String^ value)
{
   HArgChecking::VerifyNotNull( value );
   HDisposableSZ dszValue( value );
   HRESULT hResult = PSLInterface -> SetDescription( %dszValue );
   if ( FAILED( hResult ) )
      throw CreateOpFailedException_CantSetSLProperty( "Description", hResult );
}

//--------------------------------------------------------------------------------------------------

void SysHelpers_ShellLinkProperties::Hotkey::set(UInt16 value)
{
   HRESULT hResult = PSLInterface -> SetHotkey( value );
   if ( FAILED( hResult ) )
      throw CreateOpFailedException_CantSetSLProperty( "Hotkey", hResult );
}

//--------------------------------------------------------------------------------------------------

void SysHelpers_ShellLinkProperties::SetIconLocation(String^ path,int index)
{
   HArgChecking::VerifyNotNull( path );
   HDisposableSZ dszPath( path );
   HRESULT hResult = PSLInterface -> SetIconLocation( %dszPath, index );
   if ( FAILED( hResult ) )
      throw CreateOpFailedException_CantSetSLProperty( "IconLocation", hResult );
}

//--------------------------------------------------------------------------------------------------

void SysHelpers_ShellLinkProperties::Path::set(String^ value)
{
   HArgChecking::VerifyNotNull( value );
   HDisposableSZ dszValue( value );
   HRESULT hResult = PSLInterface -> SetPath( %dszValue );
   if ( FAILED( hResult ) )
      throw CreateOpFailedException_CantSetSLProperty( "Path", hResult );
   Char szValue[ MAX_PATH ];
   if ( FAILED( hResult = PSLInterface -> GetPath(
        $ARRAY_N_COUNT_OF(szValue), NULL, SLGP_RAWPATH ) ) )
      throw CreateOpFailedException_CantSetSLProperty( "Path",
         HShellLinkProperties::Verbose::VERIFICATIONAL_EXTRACTION_OF_SETTED_PATH_HAS_FAILED,
         HCOMError::HRToMessage( hResult ) );
   if ( !HString::Equals( gcnew String(szValue), value, true ) )
      throw CreateOpFailedException_CantSetSLProperty( "Path",
         HShellLinkProperties::Verbose::
         VERIFICATIONALLY_EXTRACTED_VALUE_OF_SETTED_PATH_VARIES_FROM_THE_SETTED_VALUE +
         ( value -> Contains( "%" ) ?
         String::Format( "\n({0})", HShellLinkProperties::Verbose::
         ENVIRONMENT_VARIABLES_ARE_NOT_EXPANDED_DURING_CONTROL_COMPARISON ) :
         "" ));
}

//--------------------------------------------------------------------------------------------------

void SysHelpers_ShellLinkProperties::DisplayName::set(String^ value)
{
   HArgChecking::VerifyNotNull( value );
   PIDLIST_RELATIVE
      pIDList = NULL;
   IBindCtx
      *pIBindCtx = NULL;
   try
   {
      HRESULT hResult;

      if ( FAILED( hResult = ::CreateBindCtx( 0, &pIBindCtx ) ) )
         throw HCOMError::CreateOpFailedException( hResult );

      HTCOMDummyObject dummyInterface;
      if ( FAILED( hResult = pIBindCtx -> RegisterObjectParam(
           STR_PARSE_DONT_REQUIRE_VALIDATED_URLS, &dummyInterface ) ) )
         throw HCOMError::CreateOpFailedException( hResult );

      ::BIND_OPTS bindOpts = {};
      bindOpts.cbStruct = sizeof( bindOpts );
      bindOpts.grfMode = STGM_CREATE;
      if ( FAILED( pIBindCtx -> SetBindOptions( &bindOpts ) ))
         throw HCOMError::CreateOpFailedException( hResult );

      HDisposableSZ dszValue( value );
      if ( FAILED( hResult = ::SHParseDisplayName( %dszValue, pIBindCtx, &pIDList, 0, NULL ) ) )
         throw HOpFailedExceptionCreator::Create(
            HShellLinkProperties::Verbose::CAN_NOT_PARSE_DISPLAYNAME_VALUE_0__1,
            %dszValue, HCOMError::HRToMessage( hResult ) );

      if ( FAILED( hResult = PSLInterface -> SetIDList( pIDList ) ) )
         throw CreateOpFailedException_CantSetSLProperty( "SetIDList", hResult );
   }
   finally
   {
      HTCOMInterface::Release( pIBindCtx );
      ::CoTaskMemFree( pIDList );
   }
}

//--------------------------------------------------------------------------------------------------

void SysHelpers_ShellLinkProperties::ShowCmd::set(Int32 value)
{
   HRESULT hResult = PSLInterface -> SetShowCmd( value );
   if ( FAILED( hResult ) )
      throw CreateOpFailedException_CantSetSLProperty( "ShowCmd", hResult );
}

//--------------------------------------------------------------------------------------------------

void SysHelpers_ShellLinkProperties::WorkingDirectory::set(String^ value)
{
   HArgChecking::VerifyNotNull( value );
   HDisposableSZ dszValue( value );
   HRESULT hResult = PSLInterface -> SetWorkingDirectory( %dszValue );
   if ( FAILED( hResult ) )
      throw CreateOpFailedException_CantSetSLProperty( "WorkingDirectory", hResult );
}

//==================================================================================================
} // CommonHelpers
