00001
00002
00003
00004
00005
00006 #include "stdafx.h"
00007 #include "FdmApp.h"
00008 #include "fsDownloadsHistoryMgr.h"
00009 #include "mfchelp.h"
00010 #include "WaitForConfirmationDlg.h"
00011
00012 #ifdef _DEBUG
00013 #undef THIS_FILE
00014 static char THIS_FILE[]=__FILE__;
00015 #define new DEBUG_NEW
00016 #endif
00017
00018 fsDownloadsHistoryMgr::fsDownloadsHistoryMgr()
00019 {
00020 m_pfnEventFunc = NULL;
00021 }
00022
00023 fsDownloadsHistoryMgr::~fsDownloadsHistoryMgr()
00024 {
00025
00026 }
00027
00028 void fsDownloadsHistoryMgr::ReadSettings()
00029 {
00030 m_bUse = _App.History_Downloads_Use ();
00031 m_cDaysMax = _App.History_Downloads_KeepDays ();
00032 m_bCompOnly = _App.History_Downloads_CompletedOnly ();
00033
00034 ApplySettings ();
00035 }
00036
00037 void fsDownloadsHistoryMgr::ApplySettings()
00038 {
00039 if (m_bUse == FALSE)
00040 {
00041 ClearHistory ();
00042 return;
00043 }
00044
00045 SYSTEMTIME stime;
00046 GetLocalTime (&stime);
00047 FILETIME time;
00048 SystemTimeToFileTime (&stime, &time);
00049
00050 for (int i = m_vRecords.size () - 1; i >= 0; i--)
00051 {
00052 fsDLHistoryRecord* r = &m_vRecords [i];
00053 int cDays = fsGetFTimeDaysDelta (time, r->dateRecordAdded);
00054
00055
00056 if (UINT (cDays) < m_cDaysMax)
00057 break;
00058
00059 Event (DHME_RECORDDELETED, int (r));
00060
00061 m_vRecords.del (i);
00062 }
00063 }
00064
00065 void fsDownloadsHistoryMgr::ClearHistory()
00066 {
00067 m_vRecords.clear ();
00068 Event (DHME_HISTORYCLEARED);
00069 }
00070
00071 void fsDownloadsHistoryMgr::SetEventFunc(fntDHMEEventFunc pfn, LPVOID lpParam)
00072 {
00073 m_pfnEventFunc = pfn;
00074 m_lpEvParam = lpParam;
00075 }
00076
00077 void fsDownloadsHistoryMgr::Event(fsDownloadsHistoryMgrEvent ev, int info)
00078 {
00079 if (m_pfnEventFunc)
00080 m_pfnEventFunc (ev, info, m_lpEvParam);
00081 }
00082
00083 void fsDownloadsHistoryMgr::AddToHistory(vmsDownloadSmartPtr dld)
00084 {
00085 fsDLHistoryRecord r;
00086
00087 if (m_bUse == FALSE)
00088 return;
00089
00090 if (m_bCompOnly && dld->pMgr->IsDone () == FALSE)
00091 return;
00092
00093 r.strURL = dld->pMgr->get_URL ();
00094 r.dateAdded = dld->dateAdded;
00095
00096 SYSTEMTIME t;
00097 FILETIME time;
00098 GetLocalTime (&t);
00099 SystemTimeToFileTime (&t, &time);
00100
00101 r.dateRecordAdded = time;
00102 if (dld->pMgr->IsDone ())
00103 r.dateDownloaded = time;
00104 else
00105 ZeroMemory (&r.dateDownloaded, sizeof (FILETIME));
00106
00107 char sz [MY_MAX_PATH];
00108 CDownloads_Tasks::GetFileName (dld, sz);
00109 r.strFileName = sz;
00110
00111 r.strSavedTo = dld->pMgr->get_OutputFilePathName ();
00112
00113 r.uFileSize = dld->pMgr->GetLDFileSize ();
00114 if (r.uFileSize == 0 && dld->pMgr->GetNumberOfSections () == 0)
00115 r.uFileSize = _UI64_MAX;
00116
00117 r.strComment = dld->strComment;
00118
00119 m_vRecords.insert (0, r);
00120 Event (DHME_RECORDADDED, (int) &m_vRecords [0]);
00121 }
00122
00123 int fsDownloadsHistoryMgr::GetRecordCount()
00124 {
00125 return m_vRecords.size ();
00126 }
00127
00128 BOOL fsDownloadsHistoryMgr::SaveHistory()
00129 {
00130 HANDLE hFile = CreateFile (fsGetDataFilePath ("downloads.his.sav"), GENERIC_WRITE, 0, NULL,
00131 CREATE_ALWAYS, FILE_ATTRIBUTE_HIDDEN, NULL);
00132
00133 if (hFile == INVALID_HANDLE_VALUE)
00134 return FALSE;
00135
00136 fsDownloadsHistMgrFileHdr hdr;
00137 DWORD dw;
00138
00139 if (FALSE == WriteFile (hFile, &hdr, sizeof (hdr), &dw, NULL))
00140 {
00141 CloseHandle (hFile);
00142 return FALSE;
00143 }
00144
00145 int cRecs = GetRecordCount ();
00146 if (FALSE == WriteFile (hFile, &cRecs, sizeof (int), &dw, NULL))
00147 {
00148 CloseHandle (hFile);
00149 return FALSE;
00150 }
00151
00152 for (int i = 0; i < cRecs; i++)
00153 {
00154 fsDLHistoryRecord* rec = GetRecord (i);
00155 if (FALSE == fsSaveStrToFile (rec->strFileName, hFile))
00156 break;
00157
00158 if (FALSE == fsSaveStrToFile (rec->strSavedTo, hFile))
00159 break;
00160
00161 if (FALSE == fsSaveStrToFile (rec->strURL, hFile))
00162 break;
00163
00164 if (FALSE == fsSaveStrToFile (rec->strComment, hFile))
00165 break;
00166
00167 if (FALSE == WriteFile (hFile, &rec->dateAdded, sizeof (FILETIME), &dw, NULL))
00168 break;
00169
00170 if (FALSE == WriteFile (hFile, &rec->dateDownloaded, sizeof (FILETIME), &dw, NULL))
00171 break;
00172
00173 if (FALSE == WriteFile (hFile, &rec->dateRecordAdded, sizeof (FILETIME), &dw, NULL))
00174 break;
00175
00176 if (FALSE == WriteFile (hFile, &rec->uFileSize, sizeof (UINT64), &dw, NULL))
00177 break;
00178 }
00179
00180 CloseHandle (hFile);
00181
00182 if (i != cRecs)
00183 return FALSE;
00184
00185 return TRUE;
00186 }
00187
00188 BOOL fsDownloadsHistoryMgr::LoadHistory()
00189 {
00190 if (GetFileAttributes (fsGetDataFilePath ("downloads.his.sav")) == UINT (-1))
00191 return TRUE;
00192
00193 HANDLE hFile = CreateFile (fsGetDataFilePath ("downloads.his.sav"), GENERIC_READ, FILE_SHARE_READ, NULL,
00194 OPEN_EXISTING, 0, NULL);
00195
00196 if (hFile == INVALID_HANDLE_VALUE)
00197 return FALSE;
00198
00199 if (_App.CheckHistorySize () && GetFileSize (hFile, NULL) > 1*1024*1024)
00200 {
00201 CWaitForConfirmationDlg dlg;
00202 dlg.Init (LS (L_HISTTOOLARGE), UINT_MAX, TRUE, FALSE, LS (L_DONTCHECKAGAIN));
00203 dlg.m_pszIcon = IDI_WARNING;
00204 UINT nRet = dlg.DoModal ();
00205 if (dlg.m_bDontAsk)
00206 _App.CheckHistorySize (FALSE);
00207 if (nRet == IDOK)
00208 {
00209 CloseHandle (hFile);
00210 return TRUE;
00211 }
00212 }
00213
00214 fsDownloadsHistMgrFileHdr hdr;
00215 DWORD dw;
00216
00217 if (FALSE == ReadFile (hFile, &hdr, sizeof (hdr), &dw, NULL))
00218 {
00219 CloseHandle (hFile);
00220 return FALSE;
00221 }
00222
00223 if (hdr.wVer != DLHISTFILE_CURRENT_VERSION ||
00224 strnicmp (hdr.szSig, DLHISTFILE_SIG, strlen (DLHISTFILE_SIG)))
00225 {
00226 CloseHandle (hFile);
00227 return FALSE;
00228 }
00229
00230 int cRecs;
00231 if (FALSE == ReadFile (hFile, &cRecs, sizeof (int), &dw, NULL))
00232 {
00233 CloseHandle (hFile);
00234 return FALSE;
00235 }
00236
00237 for (int i = 0; i < cRecs; i++)
00238 {
00239 fsDLHistoryRecord rec;
00240 if (FALSE == fsReadStrFromFile (&rec.strFileName.pszString, hFile))
00241 break;
00242
00243 if (FALSE == fsReadStrFromFile (&rec.strSavedTo.pszString, hFile))
00244 break;
00245
00246 if (FALSE == fsReadStrFromFile (&rec.strURL.pszString, hFile))
00247 break;
00248
00249 if (FALSE == fsReadStrFromFile (&rec.strComment.pszString, hFile))
00250 break;
00251
00252 if (FALSE == ReadFile (hFile, &rec.dateAdded, sizeof (FILETIME), &dw, NULL))
00253 break;
00254
00255 if (FALSE == ReadFile (hFile, &rec.dateDownloaded, sizeof (FILETIME), &dw, NULL))
00256 break;
00257
00258 if (FALSE == ReadFile (hFile, &rec.dateRecordAdded, sizeof (FILETIME), &dw, NULL))
00259 break;
00260
00261 if (FALSE == ReadFile (hFile, &rec.uFileSize, sizeof (UINT64), &dw, NULL))
00262 break;
00263
00264 m_vRecords.add (rec);
00265 }
00266
00267 CloseHandle (hFile);
00268
00269 if (i != cRecs)
00270 return FALSE;
00271
00272 return TRUE;
00273 }
00274
00275 fsDLHistoryRecord* fsDownloadsHistoryMgr::GetRecord(int iIndex)
00276 {
00277 return &m_vRecords [iIndex];
00278 }
00279
00280 void fsDownloadsHistoryMgr::DeleteRecord(fsDLHistoryRecord *rec)
00281 {
00282 int i = FindIndex (rec);
00283 if (i != -1)
00284 {
00285 Event (DHME_RECORDDELETED, int (rec));
00286 m_vRecords.del (i);
00287 }
00288 }
00289
00290 int fsDownloadsHistoryMgr::FindIndex(fsDLHistoryRecord *rec)
00291 {
00292 for (int i = 0; i < m_vRecords.size (); i++)
00293 {
00294 if (&m_vRecords [i] == rec)
00295 return i;
00296 }
00297
00298 return -1;
00299 }