Back to index

lightning-sunbird  0.9+nobinonly
scripter.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include "xp.h"
00039 
00040 #include "scripter.h"
00041 #include "scrpthlp.h"
00042 #include "profile.h"
00043 
00044 /********************************/
00045 /*       CScripter class        */
00046 /********************************/
00047 
00048 CScripter::CScripter() :
00049   m_pPlugin(NULL),
00050   m_pScript(NULL),
00051   m_bStopAutoExecFlag(FALSE),
00052   m_iCycleRepetitions(1),
00053   m_dwCycleDelay(0L)
00054 {
00055   m_pScript = new CScriptItemList();
00056 }
00057 
00058 CScripter::~CScripter()
00059 {
00060   if(m_pScript != NULL)
00061     delete m_pScript;
00062 }
00063   
00064 void CScripter::associate(CPluginBase * pPlugin)
00065 {
00066   m_pPlugin = pPlugin;
00067 }
00068   
00069 BOOL CScripter::getStopAutoExecFlag()
00070 {
00071   return m_bStopAutoExecFlag;
00072 }
00073 
00074 void CScripter::setStopAutoExecFlag(BOOL bFlag)
00075 {
00076   m_bStopAutoExecFlag = bFlag;
00077 }
00078 
00079 int CScripter::getCycleRepetitions()
00080 {
00081   return m_iCycleRepetitions;
00082 }
00083 
00084 DWORD CScripter::getCycleDelay()
00085 {
00086   return m_dwCycleDelay;
00087 }
00088 
00089 void CScripter::clearScript()
00090 {
00091   if(m_pScript != NULL)
00092     delete m_pScript;
00093 
00094   m_pScript = new CScriptItemList();
00095 }
00096 
00097 BOOL CScripter::executeScript()
00098 {
00099   if(m_pScript == NULL)
00100     return FALSE;
00101 
00102   for(ScriptItemListElement * psile = m_pScript->m_pFirst; psile != NULL; psile = psile->pNext)
00103   {
00104     executeScriptItem(psile->psis);
00105   }
00106 
00107   return TRUE;
00108 }
00109 
00110 BOOL CScripter::createScriptFromFile(LPSTR szFileName)
00111 {
00112   clearScript();
00113 
00114   if(!XP_IsFile(szFileName))
00115     return FALSE;
00116 
00117   char sz[16];
00118 
00119   XP_GetPrivateProfileString(SECTION_OPTIONS, KEY_LAST, PROFILE_DEFAULT_STRING, sz, sizeof(sz), szFileName);
00120   if(strcmp(sz, PROFILE_DEFAULT_STRING) == 0)
00121     return FALSE;
00122 
00123   int iLast = atoi(sz);
00124 
00125   int iFirst = 1;
00126 
00127   XP_GetPrivateProfileString(SECTION_OPTIONS, KEY_FIRST, PROFILE_DEFAULT_STRING, sz, sizeof(sz), szFileName);
00128   if(strcmp(sz, PROFILE_DEFAULT_STRING) != 0)
00129     iFirst = atoi(sz);
00130 
00131   XP_GetPrivateProfileString(SECTION_OPTIONS, KEY_DELAY, PROFILE_DEFAULT_STRING, sz, sizeof(sz), szFileName);
00132   if(strcmp(sz, PROFILE_DEFAULT_STRING) == 0)
00133     m_dwCycleDelay = 0L;
00134   else
00135     m_dwCycleDelay = atol(sz);
00136 
00137   XP_GetPrivateProfileString(SECTION_OPTIONS, KEY_REPETITIONS, PROFILE_DEFAULT_STRING, sz, sizeof(sz), szFileName);
00138   if(strcmp(sz, PROFILE_DEFAULT_STRING) == 0)
00139     m_iCycleRepetitions = 1;
00140   else
00141     m_iCycleRepetitions = atoi(sz);
00142 
00143   for(int i = iFirst; i <= iLast; i++)
00144   {
00145     char szSection[16];
00146     wsprintf(szSection, "%i", i);
00147     ScriptItemStruct * psis = readProfileSectionAndCreateScriptItemStruct(szFileName, szSection);
00148     if(psis == NULL)
00149       continue;
00150     m_pScript->add(psis);
00151   }
00152   return TRUE;
00153 }
00154 
00155 BOOL CScripter::executeScriptItem(ScriptItemStruct * psis)
00156 {
00157   if(psis == NULL)
00158     return FALSE;
00159   if(m_pPlugin == NULL)
00160     return FALSE;
00161 
00162   DWORD dw1 = psis->arg1.dwArg;
00163   DWORD dw2 = psis->arg2.dwArg;
00164   DWORD dw3 = psis->arg3.dwArg;
00165   DWORD dw4 = psis->arg4.dwArg;
00166   DWORD dw5 = psis->arg5.dwArg;
00167   DWORD dw6 = psis->arg6.dwArg;
00168   DWORD dw7 = psis->arg7.dwArg;
00169 
00170   if(psis->action == action_npn_get_value)
00171   {
00172     static DWORD dwValue = 0L;
00173     m_pPlugin->m_pValue = (void *)&dwValue;
00174   }
00175 
00176   m_pPlugin->makeNPNCall(psis->action, dw1, dw2, dw3, dw4, dw5, dw6, dw7);
00177 
00178   if(psis->dwDelay >=0)
00179     XP_Sleep(psis->dwDelay);
00180 
00181   return TRUE;
00182 }