home *** CD-ROM | disk | FTP | other *** search
/ Media Share 13 / mediashare_13.zip / mediashare_13 / ZIPPED / PROGRAM / WTJ9403.ZIP / WILDASS / SOURCE / APPINFO.CPP < prev    next >
C/C++ Source or Header  |  1993-08-12  |  10KB  |  399 lines

  1. // appinfo.cpp
  2. //
  3. // update: 1.00 08-Aug-93 tw
  4.  
  5. #include <afxwin.h>
  6. #include <afxext.h> 
  7.  
  8. #include <stdlib.h> 
  9. #include <direct.h>
  10. #include "appinfo.h"
  11. #include "app.h"     
  12. #include "resource.h"
  13.  
  14. #define new DEBUG_NEW
  15.    
  16. CApplication::CApplication()
  17. {
  18.    m_strAppPath = "";
  19.    m_strAppTitle = "";
  20.    m_strAppArgs = "";
  21.    m_strAppInitialDir = "";
  22.    m_nRunState = 2;
  23. }                   
  24.  
  25. CApplication::CApplication( const CApplication& rApp )
  26. {
  27.    m_strAppPath = rApp.m_strAppPath;
  28.    m_strAppTitle = rApp.m_strAppTitle;
  29.    m_strAppArgs = rApp.m_strAppArgs;
  30.    m_strAppInitialDir = rApp.m_strAppInitialDir;
  31.    m_nRunState = rApp.m_nRunState;
  32. }
  33.    
  34. CApplication::CApplication(
  35.    const char * pszAppPath,
  36.    const char * pszAppTitle,
  37.    const char * pszAppArgs,
  38.    const char * pszAppInitialDir,
  39.    int nRunState
  40.    )
  41. {
  42.    m_strAppPath = pszAppPath;
  43.    
  44.    // if no title give, buidl one from the
  45.    // path
  46.    if ( ! pszAppTitle )
  47.    {  
  48.       char drive[_MAX_DRIVE];
  49.       char dir[_MAX_DIR];
  50.       char fname[_MAX_FNAME];
  51.       char ext[_MAX_EXT];
  52.       _splitpath( m_strAppPath, drive, dir, fname, ext );
  53.       m_strAppTitle = fname;
  54.    }
  55.    else
  56.    {
  57.       m_strAppTitle = pszAppTitle;
  58.    }      
  59.    m_strAppArgs = pszAppArgs;
  60.    m_strAppInitialDir = pszAppInitialDir;
  61.    m_nRunState = nRunState;
  62. }  
  63.  
  64. void
  65. CApplication::Description(
  66.    const char * pszAppPath,
  67.    const char * pszAppTitle,
  68.    const char * pszAppArgs,
  69.    const char * pszAppInitialDir,
  70.    int nRunState
  71.    )
  72. {
  73.    m_strAppPath = pszAppPath;
  74.    m_strAppTitle = pszAppTitle;
  75.    m_strAppArgs = pszAppArgs;
  76.    m_strAppInitialDir = pszAppInitialDir;
  77.    m_nRunState = nRunState;
  78. }  
  79.  
  80. const char *
  81. CApplication::Title() const
  82. {
  83.    return m_strAppTitle;
  84. }  
  85.  
  86. const char * 
  87. CApplication::Path() const
  88. {
  89.    return m_strAppPath;
  90. }
  91.  
  92. const char * 
  93. CApplication::Args() const
  94. {
  95.    return m_strAppArgs;
  96. }
  97.  
  98. const char * 
  99. CApplication::InitialDir() const
  100. {
  101.    return m_strAppInitialDir;
  102. }
  103.  
  104. int 
  105. CApplication::RunState() const
  106. {
  107.    return m_nRunState;
  108. }
  109.  
  110. BOOL 
  111. CApplication::ReadProfile( const char * pszTag, int nProfileIndex )
  112. {
  113.    CMyApp * pApp = (CMyApp*)AfxGetApp();
  114.    CString strPath("path"), strArgs("args"), strDir("dir"), strTitle("title");
  115.    CString strState("state");
  116.    char szTmp[10];
  117.    strPath += itoa( nProfileIndex, szTmp, 10 );
  118.    strArgs += itoa( nProfileIndex, szTmp, 10 );
  119.    strDir += itoa( nProfileIndex, szTmp, 10 );
  120.    strTitle += itoa( nProfileIndex, szTmp, 10 );
  121.    strState += itoa( nProfileIndex, szTmp, 10 );
  122.  
  123.    m_strAppPath = pApp->GetProfileString( pszTag, strPath );
  124.    if ( m_strAppPath.IsEmpty() )
  125.       return FALSE;
  126.       
  127.    m_strAppTitle = pApp->GetProfileString( pszTag, strTitle );
  128.    m_strAppArgs = pApp->GetProfileString( pszTag, strArgs );
  129.    m_strAppInitialDir = pApp->GetProfileString( pszTag, strDir );
  130.    m_nRunState = pApp->GetProfileInt( pszTag, strState, 2 );
  131.    return TRUE;
  132. }
  133.  
  134. void 
  135. CApplication::DumpProfile( const char * pszTag, int nProfileIndex )
  136. {
  137.    CMyApp * pApp = (CMyApp*)AfxGetApp();          
  138.    CString strPath("path"), strArgs("args"), strDir("dir"), strTitle("title");
  139.    CString strState("state");
  140.    char szTmp[10];
  141.    strPath += itoa( nProfileIndex, szTmp, 10 );
  142.    strArgs += itoa( nProfileIndex, szTmp, 10 );
  143.    strDir += itoa( nProfileIndex, szTmp, 10 );
  144.    strTitle += itoa( nProfileIndex, szTmp, 10 );
  145.    strState += itoa( nProfileIndex, szTmp, 10 );
  146.    
  147.    VERIFY( pApp->WriteProfileString( pszTag, strPath, Path() ));
  148.    VERIFY( pApp->WriteProfileString( pszTag, strArgs, Args() ));
  149.    VERIFY( pApp->WriteProfileString( pszTag, strDir, InitialDir() ));
  150.    VERIFY( pApp->WriteProfileString( pszTag, strTitle, Title() ));
  151.    VERIFY( pApp->WriteProfileInt( pszTag, strState, RunState() ));
  152. }
  153.  
  154. void 
  155. CApplication::Run() const
  156. {                        
  157.    // get the current directory, switch
  158.    // to the applications default directory
  159.    char szCWD[_MAX_PATH];
  160.    int nDrive;
  161.    
  162.    if ( ! m_strAppInitialDir.IsEmpty() )
  163.    {
  164.       _getcwd( szCWD, _MAX_PATH );
  165.       nDrive = _getdrive(); 
  166.  
  167.       char drive[_MAX_DRIVE];
  168.       char dir[_MAX_DIR];
  169.       char fname[_MAX_FNAME];
  170.       char ext[_MAX_EXT];
  171.       _splitpath( m_strAppPath, drive, dir, fname, ext );
  172.       
  173.       _chdrive( drive[0]-65 );
  174.       _chdir( dir );
  175.    }
  176.  
  177.    // try to run this application
  178.    // and notify user if anything isn`t
  179.    // working as expected
  180.    UINT nReturn = WinExec( Path(), SW_SHOW ); 
  181.    
  182.    // if this was a failure..
  183.    if ( nReturn < 32 )
  184.       AfxMessageBox( CString("Problem running:") + Path() );
  185.       
  186.    // return to our path
  187.    if ( ! m_strAppInitialDir.IsEmpty() )
  188.    {
  189.       _chdrive( nDrive );
  190.       _chdir( szCWD );      
  191.    }      
  192. }
  193.  
  194.  
  195. // ******************* application list
  196.  
  197. CApplicationList::CApplicationList()
  198. {
  199.    m_nIcon = 0;
  200.    m_strIconFile = "";
  201.    // m_hIcon = 0;
  202. }
  203.  
  204. void 
  205. CApplicationList::Icon( const char * pszIconFile, UINT nIcon )
  206. {
  207.    m_strIconFile = pszIconFile;
  208.    m_nIcon = nIcon;
  209. }
  210.  
  211. HICON
  212. CApplicationList::Icon() const
  213. {
  214.    // if we have an icon file, try to use this one
  215.    if ( ! m_strIconFile.IsEmpty() )
  216.    {
  217.       HICON hIcon = ExtractIcon( AfxGetInstanceHandle(), m_strIconFile, m_nIcon );
  218.       if ( hIcon!=NULL && hIcon!=(HICON)1 )
  219.          return hIcon;
  220.    }
  221.    
  222.    // if this didnt work, check if this is an empty,
  223.    // a one-app or a mutiple app group
  224.    if ( GetCount() == 0 )
  225.    {
  226.       return AfxGetApp()->LoadIcon( IDI_GROUPEMPTY );
  227.    }   
  228.  
  229.    if ( GetCount()==1 )
  230.    {            
  231.       CApplication *pApp = (CApplication*)GetHead();
  232.       HICON hIcon = ExtractIcon( AfxGetInstanceHandle(), pApp->Path(), 0 );
  233.       if ( hIcon!=NULL && hIcon!=(HICON)1 )
  234.          return hIcon;
  235.    }
  236.    
  237.    // in all other cases, retur the group icon
  238.    return AfxGetApp()->LoadIcon( IDI_GROUP );
  239.    
  240. }           
  241.  
  242. const CString&
  243. CApplicationList::IconFile() const
  244. {
  245.    return m_strIconFile;
  246. }  
  247.  
  248. UINT
  249. CApplicationList::IconID() const
  250. {
  251.    return m_nIcon;
  252. }
  253.  
  254. CApplicationList::~CApplicationList()
  255. {
  256.    RemoveAll();
  257. }    
  258.  
  259. //POSITION 
  260. //CApplicationList::AddTail( CApplication* newElement )
  261. //{
  262. //   return CObList::AddTail( newElement );
  263. //}
  264. void 
  265. CApplicationList::ReadProfile( int nProfileIndex )
  266. {
  267.    CString strTag("AppList");
  268.    char szTmp[10];
  269.    strTag += itoa( nProfileIndex, szTmp, 10 );
  270.    
  271.    // read the icon to use    
  272.    CMyApp * pApp =(CMyApp*)AfxGetApp();
  273.    m_strIconFile = pApp->GetProfileString( strTag, "IconFile" );
  274. //   if ( ! m_strIconFile.IsEmpty() )
  275. //   {
  276.       m_nIcon = pApp->GetProfileInt( strTag, "IconID", 0 );
  277.       // m_hIcon = ExtractIcon( AfxGetInstanceHandle(), m_strIconFile, m_nIcon );
  278. //   }      
  279.    
  280.    int iEntry = 0;
  281.    
  282.    while( TRUE )
  283.    {
  284.       CApplication * pApp = new CApplication();
  285.       if ( ! pApp->ReadProfile( strTag, iEntry ))
  286.       {
  287.          delete pApp;
  288.          return;
  289.       }  
  290.       else
  291.       {
  292.          AddTail( pApp );
  293.       }
  294.       iEntry++;
  295.    }      
  296. }
  297.  
  298. void 
  299. CApplicationList::DumpProfile( int nProfileIndex )
  300. {
  301.    CString strTag("AppList");
  302.    char szTmp[10];
  303.    strTag += itoa( nProfileIndex, szTmp, 10 );
  304.    POSITION pos;
  305.    
  306.    // empty this profile section, so that
  307.    // no old entries stay in there
  308.    // the CWinApp fxn will assert in this case,
  309.    // so we are using the SDK fxn for that
  310.    CString strFile = AfxGetAppName() + CString(".ini");
  311.    WritePrivateProfileString( strTag, NULL, NULL, strFile );
  312.    
  313.    // dump the icon info   
  314.    CMyApp * pApp =(CMyApp*)AfxGetApp();
  315.    pApp->WriteProfileString( strTag, "IconFile", m_strIconFile );
  316.    pApp->WriteProfileInt( strTag, "IconID", m_nIcon );
  317.    
  318.    int iEntry;
  319.    for ( iEntry=0,pos=GetHeadPosition(); pos!=NULL; iEntry++ )
  320.    {
  321.       CApplication * pApp = GetNext( pos );
  322.       pApp->DumpProfile( strTag, iEntry );
  323.    }
  324. }
  325.  
  326. void 
  327. CApplicationList::Add( CApplicationList* pNewList )
  328. {
  329.    for ( POSITION pos = pNewList->GetHeadPosition(); pos!=NULL; )
  330.    {
  331.       AddTail( new CApplication( *pNewList->GetNext(pos) ) );
  332.    }
  333.     
  334.    // transfer icon inco
  335.    m_strIconFile = pNewList->IconFile();
  336.    m_nIcon = pNewList->IconID(); 
  337. }
  338.  
  339. // Add a single application (this is a drag n drop message)
  340. void 
  341. CApplicationList::Add( const char * pszPath )
  342. {
  343.    AddTail( new CApplication( pszPath ) );
  344.    
  345.    // in case we didn`t have a group icon yet, create one
  346.    if ( m_strIconFile.IsEmpty() )
  347.    {
  348.       // does this app have an icon ?
  349.       HICON hIcon = ExtractIcon( AfxGetInstanceHandle(), pszPath, 0 );
  350.       if ( hIcon!=0 && hIcon!=(HICON)1 )
  351.       {
  352.          m_strIconFile = pszPath;
  353.          m_nIcon = 0;
  354.       }  
  355.    }
  356. }   
  357.    
  358. void 
  359. CApplicationList::RemoveAll()
  360. {
  361.    for ( POSITION pos=GetHeadPosition(); pos!=NULL; )
  362.    {        
  363.       CApplication * pApp = GetNext( pos );
  364.       delete pApp;
  365.    }
  366.    CObList::RemoveAll();
  367.    
  368.    // remove the icon info
  369.    m_strIconFile="";
  370.    m_nIcon = 0;
  371. }
  372.  
  373.  
  374. // avoid casts in usage
  375. CApplication*& 
  376. CApplicationList::GetNext( POSITION& rPos )
  377. {
  378.    return (CApplication*&)CObList::GetNext( rPos );
  379. }
  380.  
  381. CApplication* 
  382. CApplicationList::GetNext( POSITION& rPos ) const
  383. {
  384.    return (CApplication*)CObList::GetNext( rPos );
  385. }
  386.  
  387. CApplication*& 
  388. CApplicationList::GetAt( POSITION pos )
  389. {                       
  390.    return (CApplication*&)CObList::GetAt( pos );
  391. }
  392.  
  393. CApplication* 
  394. CApplicationList::GetAt( POSITION pos ) const
  395. {
  396.    return (CApplication*)CObList::GetAt( pos );
  397. }
  398.  
  399.