00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "StdAfx.h"
00025 #include "atltime.h"
00026 #include "Shareaza.h"
00027 #include "Settings.h"
00028 #include "DlgHelp.h"
00029 #include "Scheduler.h"
00030 #include "Network.h"
00031
00032 CScheduler Schedule;
00033
00035
00036
00037 CScheduler::CScheduler()
00038 {
00039 FillMemory( m_pSchedule , 7 * 24, SCHEDULE_FULL_SPEED );
00040 m_nCurrentHour = 0xFF;
00041 m_tLastCheck = 0;
00042 }
00043
00044 CScheduler::~CScheduler()
00045 {
00046 }
00047
00049
00050
00051 BOOL CScheduler::Load()
00052 {
00053 CFile pFile;
00054 CString strFile = Settings.General.UserPath + _T("\\Data\\Schedule.dat");
00055
00056 if ( ! pFile.Open( strFile, CFile::modeRead ) )
00057 {
00058 theApp.Message( MSG_ERROR, _T("Failed to open Schedule.dat") );
00059 return FALSE;
00060 }
00061
00062 try
00063 {
00064 CArchive ar( &pFile, CArchive::load );
00065 Serialize( ar );
00066 ar.Close();
00067 }
00068 catch ( CException* pException )
00069 {
00070 pException->Delete();
00071 }
00072
00073 pFile.Close();
00074
00075 m_nCurrentHour = 0xFF;
00076 return TRUE;
00077 }
00078
00079 void CScheduler::Save()
00080 {
00081 CFile pFile;
00082 CString strFile = Settings.General.UserPath + _T("\\Data\\Schedule.dat");
00083
00084 if ( pFile.Open( strFile, CFile::modeWrite|CFile::modeCreate ) )
00085 {
00086 CArchive ar( &pFile, CArchive::store );
00087 Serialize( ar );
00088 ar.Close();
00089 }
00090
00091 m_nCurrentHour = 0xFF;
00092 }
00093
00095
00096
00097 void CScheduler::Serialize(CArchive& ar)
00098 {
00099 int nDay, nHour;
00100
00101 if ( ar.IsStoring() )
00102 {
00103 for ( nDay = 0 ; nDay < 7 ; nDay++ )
00104 {
00105 for ( nHour = 0 ; nHour < 24 ; nHour++ )
00106 {
00107 ar << m_pSchedule[nDay][nHour];
00108 }
00109 }
00110 }
00111 else
00112 {
00113 for ( nDay = 0 ; nDay < 7 ; nDay++ )
00114 {
00115 for ( nHour = 0 ; nHour < 24 ; nHour++ )
00116 {
00117 ar >> m_pSchedule[nDay][nHour];
00118 }
00119 }
00120 }
00121 }
00122
00124
00125 void CScheduler::Update()
00126 {
00127 int nDay, nHour;
00128
00129 if ( Settings.Scheduler.Enable )
00130 {
00131 DWORD tTicks = GetTickCount();
00132
00133 if ( tTicks - m_tLastCheck < 20000 ) return;
00134 m_tLastCheck = tTicks;
00135
00136
00137 CTime tTime = CTime::GetCurrentTime();
00138
00139
00140 nDay = tTime.GetDayOfWeek() - 1;
00141 nHour = tTime.GetHour();
00142 if ( ( nDay >= 7 ) || ( nDay < 0 ) || ( nHour >= 24 ) || ( nHour < 0 ) )
00143 {
00144
00145 theApp.Message( MSG_ERROR, _T("Scheduler received invalid time") );
00146 return;
00147 }
00148
00149
00150 if ( m_nCurrentHour != nHour )
00151 {
00152 SetVariables( m_pSchedule[nDay][nHour] );
00153 m_nCurrentHour = nHour;
00154 }
00155 }
00156 }
00157
00159
00160 void CScheduler::SetVariables(BYTE nCurrentSettings)
00161 {
00162 switch ( nCurrentSettings )
00163 {
00164 case SCHEDULE_OFF:
00165 theApp.Message( MSG_DEBUG, _T("Scheduler: Shutting down") );
00166 Settings.Live.BandwidthScale = 0;
00167 Settings.Gnutella2.EnableToday = FALSE;
00168 Settings.Gnutella1.EnableToday = FALSE;
00169 Settings.eDonkey.EnableToday = FALSE;
00170 if ( Network.IsConnected() ) Network.Disconnect();
00171 break;
00172 case SCHEDULE_LIMITED_SPEED:
00173 theApp.Message( MSG_DEBUG, _T("Scheduler: Limited speed") );
00174 Settings.Live.BandwidthScale = Settings.Scheduler.LimitedBandwidth;
00175 Settings.Gnutella2.EnableToday = TRUE;
00176 Settings.Gnutella1.EnableToday = Settings.Scheduler.LimitedNetworks ? FALSE :Settings.Gnutella1.EnableAlways;
00177 Settings.eDonkey.EnableToday = Settings.Scheduler.LimitedNetworks ? FALSE :Settings.eDonkey.EnableAlways;
00178 if ( ! Network.IsConnected() ) Network.Connect( TRUE );
00179 break;
00180 case SCHEDULE_FULL_SPEED:
00181 theApp.Message( MSG_DEBUG, _T("Scheduler: Full Speed") );
00182 Settings.Live.BandwidthScale = 100;
00183 Settings.Gnutella2.EnableToday = TRUE;
00184 Settings.Gnutella1.EnableToday = Settings.Gnutella1.EnableAlways;
00185 Settings.eDonkey.EnableToday = Settings.eDonkey.EnableAlways;
00186 if ( ! Network.IsConnected() ) Network.Connect( TRUE );
00187 break;
00188 default:
00189 theApp.Message( MSG_ERROR, _T("Invalid value in schedule") );
00190 break;
00191 }
00192 }