⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 prpsheet.cpp

📁 mini http server,可以集成嵌入到程序中,实现简单的web功能
💻 CPP
📖 第 1 页 / 共 2 页
字号:
        else if ( !PIUtil_stricmp( pVariable, KEY_CONF_PASSWORD ) )
			{
            sPassword = pValue;
            }
		else
			{
			os << "Unknown directive '" << pVariable <<
				"'" << ends;
			CONFIG_ERR( Object(), os.str() );
			return 0;
			};

		return 1;
		};

public:
	PropSheet( PIObject *pObject, int iArgc, const char *ppArgv[] )
	:	IFaceBase( pObject ),
        pAdmin( 0 ),
        sTitle( "Pi3Web Admin" ),
        pConfig( CreateQConfig() ),
        iOptions( 0 ),
		iPort( 80 )
		{
        PIDB_add( PIObject_getDB( Object() ), PIDBTYPE_OPAQUE,
            QCONFIG_KEY, pConfig, 0 );

        ReadParameters( iArgc, ppArgv );

		if (sHost.Len()) {
			pAdmin = Admin_newRemote( Object(), sHost, iPort,
				sUserName, sPassword, &GetAuthenticationData, this );
			}

		if (sFileName.Len()) { pAdmin = Admin_newLocal( sFileName ); }
		}

	~PropSheet()
		{
		DestroyQConfig( pConfig );
		Admin_delete( pAdmin );
		for( DblListIterator i( lPages ); !i.BadIndex(); i++ )
			{
			PIObject_delete( (PIObject *)i.Current(), 0, 0 );
			};
		};
/*____________________________________________________________________________*\
 *
 Description:
\*____________________________________________________________________________*/
    virtual int Handle( Interface &tWindow )
        {
        switch( tWindow.iMessage )
            {
            case INTRFACE_MSG_INIT:
				pInterface = &tWindow;
				return Init();

            case INTRFACE_MSG_WIN32CREATE:
                return Win32Create();

            case INTRFACE_MSG_LOAD:
                return Load();

            case INTRFACE_MSG_DEFAULTS:
                return SetDefaults();

            default:
                assert( 0 );
                return PIAPI_ABORT;
            };

        return PIAPI_COMPLETED;
        };

/*____________________________________________________________________________*\
 *
 Description:
\*____________________________________________________________________________*/
	virtual int SaveConfig(Interface *pWindow, HWND hDlg)
		{
		if (IsChanged())
			{
			HourglassOn();
			int iRet = Admin_saveConfig( pAdmin, pConfig, pWindow->sError );
			HourglassOff();
			if (iRet)
				{
				if ( pWindow->sError.Len() )
					{
					::MessageBox( GetParent(hDlg),
						pWindow->sError, "Save Configuration Error",
						MB_OK | MB_ICONERROR );
					}
				else
					{
					::MessageBox( GetParent(hDlg),
						"Can't upload configuration changes to server",
						"Save Error", MB_OK | MB_ICONERROR );
					}
				SetWindowLong( hDlg, DWL_MSGRESULT, PSNRET_INVALID );
				}
			else
				{
				::MessageBox( GetParent(hDlg),
					"The configuration has been uploaded successfully",
					"Configuration Saved", MB_OK | MB_ICONINFORMATION );
				SetWindowLong( hDlg, DWL_MSGRESULT, PSNRET_NOERROR );
				Unchanged( hDlg );
				}
			}
		return (TRUE);
		}

/*____________________________________________________________________________*\
 *
 Description:
\*____________________________________________________________________________*/
	virtual int Cancel(Interface *pWindow, HWND hDlg)
		{
		if (IsChanged())
			{
			int mbRes = MessageBox( GetParent(hDlg), "Discard changes?",
				"Warning", MB_YESNOCANCEL | MB_ICONWARNING );
			switch (mbRes)
				{
				case IDYES:
					SetWindowLong( hDlg, DWL_MSGRESULT, FALSE );
					break;
				case IDNO:
					NMHDR nmhdr;
					memset((void *)&nmhdr, sizeof(nmhdr), 0);
					nmhdr.hwndFrom = hDlg;
					nmhdr.code = PSN_KILLACTIVE;
					/* ---
					Force to write back changes of property sheet page
					before safe configuration
					--- */
					SendMessage( hDlg, WM_NOTIFY, 0, (LPARAM)&nmhdr );
					SaveConfig( pWindow, hDlg );
 					SetWindowLong( hDlg, DWL_MSGRESULT, FALSE );
					break;
				case IDCANCEL:
				default:
					SetWindowLong( hDlg, DWL_MSGRESULT, TRUE );
				}
			}
		return (TRUE);
		}

/*____________________________________________________________________________*\
 *
 Description:
\*____________________________________________________________________________*/
   int Init()
		{
        /*
        ** Iterate through the property sheet pages
        */
        int iNumPages = lPages.Size();
        for( DblListIterator i( lPages ); !i.BadIndex(); i++)
            {
            PIObject *pPageObject = (PIObject *)i.Current();
			if ( PILogic_execute( pPageObject, 1, (const char **)&pInterface )
				!= PIAPI_COMPLETED ) 
				{
				pInterface->sError = "Failed to initialize property page '";
				pInterface->sError.Concatenate( PIObject_getName( pPageObject ));
				pInterface->sError.Concatenate( "'." );
				return PIAPI_ERROR;
				}
			}

        /*
        ** Done
        */
        return PIAPI_COMPLETED;
		}

/*____________________________________________________________________________*\
 *
 Description:
\*____________________________________________________________________________*/
   int SetDefaults()
		{
		if ( !(iOptions & FLG_CREATEDEFAULT) ) { return PIAPI_ABORT; };

        /*
        ** Iterate through the property sheet pages
        */
        int iNumPages = lPages.Size();
        for( DblListIterator i( lPages ); !i.BadIndex(); i++)
            {
            PIObject *pPageObject = (PIObject *)i.Current();
			if ( PILogic_execute( pPageObject, 1, (const char **)&pInterface )
				!= PIAPI_COMPLETED ) 
				{
				pInterface->sError = "Failed to setup defaults for property page '";
				pInterface->sError.Concatenate( PIObject_getName( pPageObject ));
				pInterface->sError.Concatenate( "'." );
				return PIAPI_ERROR;
				}
			}

        /*
        ** Done
        */
        return PIAPI_COMPLETED;
		};

/*____________________________________________________________________________*\
 *
 Description:
\*____________________________________________________________________________*/
    int Win32Create()
		{
        /*
        ** Create the property sheet
        */
        int iNumPages = lPages.Size();

        if ( !iNumPages )
            {
            pInterface->sError = "PropSheet: Cannot create property sheet";
			pInterface->sError.Concatenate( " with no property pages." );
            return PIAPI_ERROR;
            }

        HPROPSHEETPAGE *pSheets = PI_NEW( HPROPSHEETPAGE[iNumPages] );
        
        int j=0;
        for( DblListIterator i( lPages ); !i.BadIndex(); i++)
            {
            PIObject *pPageObject = (PIObject *)i.Current();
            PROPSHEETPAGE *pPsp = (PROPSHEETPAGE *)PIDB_lookup(
                PIObject_getDB( pPageObject ), PIDBTYPE_OPAQUE, 
                INTRFACE_PROPSHEETPAGE, 0 );
            HPROPSHEETPAGE hPropSheetPage = ::CreatePropertySheetPage( pPsp );
                    
            if ( !hPropSheetPage )
                {
				pInterface->sError = "PropSheet: Failed to create property page '";
				pInterface->sError.Concatenate( pPsp->pszTemplate );
				pInterface->sError.Concatenate( "'." );
                return PIAPI_ERROR;
                };

            pSheets[j++] = hPropSheetPage;
            };
        
        /*
        ** setup property structure
        */
        PROPSHEETHEADER tPsh;
        memset( &tPsh, 0, sizeof( PROPSHEETHEADER ) );
        tPsh.dwSize = sizeof( PROPSHEETHEADER );
        tPsh.dwFlags = PSH_MODELESS | PSH_DEFAULT | PSH_USEICONID;
        tPsh.hInstance = (HINSTANCE)PIDLL_getSystemHandle( PIClass_getLibrary(
            PIObject_getClass( Object() ) ) );
        tPsh.hwndParent = NULL;
        tPsh.pszIcon = MAKEINTRESOURCE(IDI_PI3ADMIN);
        tPsh.nPages = iNumPages;
        tPsh.phpage = pSheets;
        tPsh.pszCaption = sTitle;
    
        /*
        ** Create modeless property sheet
        */
		HourglassOn();
        pInterface->hWnd = (HWND)PropertySheet( &tPsh );
		HourglassOff();
        PI_DELETE( [] pSheets );

        if ( pInterface->hWnd == (HWND)-1 ) {
            pInterface->sError = "PropSheet: Failed to create property sheet.";
            return PIAPI_ERROR;
        }

		::SetWindowLong( pInterface->hWnd, GWL_USERDATA, (long)this );

        /*
        ** Done
        */
        return PIAPI_COMPLETED;
		};

/*____________________________________________________________________________*\
 *
 Description:
\*____________________________________________________________________________*/
   int Load()
		{
		if ( !(iOptions & FLG_LOADFILE)) { return PIAPI_ABORT; };

        /*
        ** Attempt to load configuration file
        */
		if ( Admin_loadConfig( pAdmin, pConfig, pInterface->sError )) {
			if ( !pInterface->sError.Len() ) {
				pInterface->sError = "PropSheet: Failed to load configuration file.";
			}
			return PIAPI_ERROR;
		}

        /*
        ** Done
        */
        return PIAPI_COMPLETED;
		}

/*____________________________________________________________________________*\
 *
 Description:
\*____________________________________________________________________________*/
	BOOL AuthDialogProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
		{
		enum { BUF_SIZE=1023 };
		char szBuf[BUF_SIZE+1];
	
		switch (uMsg)
			{
			case WM_INITDIALOG:
                hWndOK = GetDlgItem( hDlg, IDOK );
                hWndRealm = GetDlgItem( hDlg, IDC_REALM );
				hWndUser = GetDlgItem( hDlg, IDC_USER );
				hWndPassword = GetDlgItem( hDlg, IDC_PASSWORD );

				assert( hWndOK && hWndRealm && hWndUser	&& hWndPassword);

				SendMessage( hWndUser, EM_SETLIMITTEXT, 256, 0 );
				SendMessage( hWndPassword, EM_SETLIMITTEXT, 256, 0 );
				EnableWindow( hWndOK, FALSE );
				SendMessage( hWndUser, WM_SETTEXT, 0,
					(LPARAM)Admin_GetUsername( pAdmin ));
				SendMessage( hWndRealm, WM_SETTEXT, 0,
					(LPARAM)Admin_GetRealm( pAdmin ));

			case WM_COMMAND:
				switch (GET_WM_COMMAND_ID(wParam, lParam))
					{
					case IDOK:
						/*
						** Validate content of edit fields
						*/
						*szBuf = '\0';
						SendMessage( hWndUser, WM_GETTEXT, BUF_SIZE,(LPARAM)szBuf );
						Admin_SetUsername( pAdmin, szBuf );
						*szBuf = '\0';
						SendMessage( hWndPassword, WM_GETTEXT, BUF_SIZE,(LPARAM)szBuf );
						Admin_SetPassword( pAdmin, szBuf );
						EndDialog( hDlg, 1 );
						break;
	
					case IDCANCEL:
						EndDialog( hDlg, 0 );
						break;
	
					case IDC_USER:
						if ( GET_WM_COMMAND_CMD(wParam, lParam)==EN_CHANGE )
							{
							if ( SendMessage( GET_WM_COMMAND_HWND(wParam,
                                lParam ), EM_LINELENGTH, 0, 0 )>0 )
								{ EnableWindow( hWndOK, TRUE ); }
							else
								{ EnableWindow( hWndOK, FALSE ); }
							}
						else
							return (FALSE);
						break;
	
					default:
						return (FALSE);
					};
	
			default:
				return (FALSE);
			};
	
		return (TRUE);
		}
};

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
static BOOL CALLBACK fnAuthDialogProc(HWND hDlg, UINT uMsg, WPARAM wParam,
    LPARAM lParam)
{
    if ( uMsg==WM_INITDIALOG )
        {
        ::SetWindowLong( hDlg, DWL_USER, lParam );
        };

    PropSheet *pSheet = (PropSheet *)::GetWindowLong( hDlg, DWL_USER );
    return pSheet ? pSheet->AuthDialogProc( hDlg, uMsg, wParam, lParam ) :
        FALSE;
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
PUBLIC_PIAPI int PropSheet_constructor( PIObject *pObj,
	int iArgc, const char *ppArgv[] )
{
	return IFaceBase_constructor( pObj, PI_NEW( PropSheet( pObj,
		iArgc, ppArgv ) ) );
}

#if 0
/*___+++CNF_BEGIN+++___*/
	<Class>
		Name PropSheetClass
		Type LogicExtension
		Library IntrFace
		OnClassLoad IFaceBase_onClassLoad
		Constructor PropSheet_constructor
		CopyConstructor IFaceBase_copyConstructor
		Destructor IFaceBase_destructor
		Execute IFaceBase_execute
	</Class>

	<Object>
		Name PropSheet
		Class PropSheetClass
	</Object>

/*___+++CNF_END+++___*/
#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -