00001
00002
00003
00004
00005
00006 #include "stdafx.h"
00007 #include "FdmApp.h"
00008 #include "DlgExportHistory.h"
00009
00010 #ifdef _DEBUG
00011 #define new DEBUG_NEW
00012 #undef THIS_FILE
00013 static char THIS_FILE[] = __FILE__;
00014 #endif
00015
00016 extern CDownloadsWnd* _pwndDownloads;
00017
00018 CDlgExportHistory::CDlgExportHistory(CWnd* pParent )
00019 : CDialog(CDlgExportHistory::IDD, pParent)
00020 {
00021
00022
00023
00024 }
00025
00026 void CDlgExportHistory::DoDataExchange(CDataExchange* pDX)
00027 {
00028 CDialog::DoDataExchange(pDX);
00029
00030
00031
00032 }
00033
00034 BEGIN_MESSAGE_MAP(CDlgExportHistory, CDialog)
00035
00036
00037 END_MESSAGE_MAP()
00038
00039 BOOL CDlgExportHistory::OnInitDialog()
00040 {
00041 CDialog::OnInitDialog();
00042
00043 CheckRadioButton (IDC_ALL, IDC_SELECTED, IDC_ALL + _App.Export_WhichHist ());
00044 CheckDlgButton (IDC_COMPLETEDONLY, _App.Export_Hist_CompletedOnly () ? BST_CHECKED : BST_UNCHECKED);
00045 CheckDlgButton (IDC_APPEND, _App.Export_Hist_Append () ? BST_CHECKED : BST_UNCHECKED);
00046
00047 ApplyLanguage ();
00048
00049 return TRUE;
00050 }
00051
00052 void CDlgExportHistory::ApplyLanguage()
00053 {
00054 fsDlgLngInfo lnginfo [] = {
00055 fsDlgLngInfo (IDC_ALL, L_EXPORTALLENTRIES),
00056 fsDlgLngInfo (IDC_ALLINLIST, L_EXPORTALLENTRIESINLIST),
00057 fsDlgLngInfo (IDC_SELECTED, L_EXPORTSELECTEDENTRIES),
00058 fsDlgLngInfo (IDC_COMPLETEDONLY, L_EXPORTCOMPENTRIESONLY),
00059 fsDlgLngInfo (IDC_APPEND, L_APPENDIFFILEEXISTS),
00060 fsDlgLngInfo (IDC__EXPORTOPTS, L_EXPORTOPTIONS),
00061 fsDlgLngInfo (IDCANCEL, L_CANCEL),
00062 };
00063
00064 CString str = _LngMgr.GetStringNP (L_EXPORT);
00065 str += "...";
00066 SetDlgItemText (IDC__EXPORT, str);
00067
00068 _LngMgr.ApplyLanguage (this, lnginfo, sizeof (lnginfo) / sizeof (fsDlgLngInfo), L_EXPORTDLHIST);
00069 }
00070
00071 void CDlgExportHistory::OnOK()
00072 {
00073 int iWhich = 0;
00074 if (IsDlgButtonChecked (IDC_ALLINLIST) == BST_CHECKED)
00075 iWhich = 1;
00076 else if (IsDlgButtonChecked (IDC_SELECTED) == BST_CHECKED)
00077 iWhich = 2;
00078
00079 BOOL bCompletedOnly = IsDlgButtonChecked (IDC_COMPLETEDONLY) == BST_CHECKED;
00080 BOOL bAppend = IsDlgButtonChecked (IDC_APPEND) == BST_CHECKED;
00081
00082 _App.Export_WhichHist (iWhich);
00083 _App.Export_Hist_CompletedOnly (bCompletedOnly);
00084 _App.Export_Hist_Append (bAppend);
00085
00086 ExportHistory (iWhich, bCompletedOnly, bAppend);
00087
00088 CDialog::OnOK();
00089 }
00090
00091 void CDlgExportHistory::ExportHistory(int iWhich, BOOL bCompletedOnly, BOOL bAppend)
00092 {
00093 fs::list <fsDLHistoryRecord*> vpHist;
00094
00095 switch (iWhich)
00096 {
00097 case 0:
00098 {
00099 for (int i = 0; i < _DldsMgr.m_histmgr.GetRecordCount (); i++)
00100 {
00101 fsDLHistoryRecord* rec = _DldsMgr.m_histmgr.GetRecord (i);
00102 if (bCompletedOnly && rec->dateDownloaded.dwHighDateTime == 0)
00103 continue;
00104 vpHist.add (rec);
00105 }
00106 }
00107 break;
00108
00109 case 1:
00110 {
00111 CDownloads_History* list = &_pwndDownloads->m_wndHistory;
00112 int cItems = list->GetItemCount ();
00113 for (int i = cItems-1; i >= 0; i--)
00114 {
00115 fsDLHistoryRecord* rec = (fsDLHistoryRecord*) list->GetItemData (i);
00116 if (bCompletedOnly && rec->dateDownloaded.dwHighDateTime == 0)
00117 continue;
00118 vpHist.add (rec);
00119 }
00120 }
00121 break;
00122
00123 case 2:
00124 CDownloads_History* list = &_pwndDownloads->m_wndHistory;
00125 POSITION pos = list->GetFirstSelectedItemPosition ();
00126 while (pos)
00127 {
00128 int nItem = list->GetNextSelectedItem (pos);
00129 fsDLHistoryRecord* rec = (fsDLHistoryRecord*) list->GetItemData (nItem);
00130 if (bCompletedOnly && rec->dateDownloaded.dwHighDateTime == 0)
00131 continue;
00132 vpHist.add (rec);
00133 }
00134 }
00135
00136 if (vpHist.size () == 0)
00137 return;
00138
00139 CString strFilter;
00140 strFilter.Format ("%s (*.html)|*.html||", LS (L_HTMLFILES));
00141 UINT flags = OFN_NOCHANGEDIR;
00142 if (bAppend == FALSE)
00143 flags |= OFN_OVERWRITEPROMPT;
00144 CFileDialog dlg (FALSE, "html", NULL, flags, strFilter, NULL);
00145
00146 if (_DlgMgr.DoModal (&dlg) == IDCANCEL)
00147 return;
00148
00149 ExportHistory (dlg.GetPathName (), vpHist, bAppend);
00150 }
00151
00152 BOOL CDlgExportHistory::ExportHistory(LPCSTR pszFile, fs::list <fsDLHistoryRecord*> &vpHist, BOOL bAppend)
00153 {
00154 HANDLE hFile = CreateFile (pszFile, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_ALWAYS,
00155 FILE_ATTRIBUTE_NORMAL, NULL);
00156
00157 if (hFile == INVALID_HANDLE_VALUE)
00158 return FALSE;
00159
00160 CString str;
00161
00162 if (bAppend && GetLastError () == ERROR_ALREADY_EXISTS)
00163 {
00164 DWORD dwSize = GetFileSize (hFile, NULL);
00165 if (dwSize > 10000)
00166 {
00167 SetFilePointer (hFile, dwSize - 10000, NULL, FILE_BEGIN);
00168 dwSize = 10000;
00169 }
00170
00171 DWORD dw;
00172 LPSTR psz = new char [dwSize+1];
00173 ReadFile (hFile, psz, dwSize, &dw, NULL);
00174 psz [dwSize] = 0;
00175
00176 int nLen = lstrlen (psz);
00177
00178 if (nLen != (int) dwSize)
00179 {
00180
00181 CloseHandle (hFile);
00182 return FALSE;
00183 }
00184
00185 LPSTR psz2 = psz + nLen - 7;
00186 while (psz2 != psz)
00187 {
00188 if (_memicmp (psz2, "</body>", 7) == 0)
00189 {
00190 *psz2 = 0;
00191 break;
00192 }
00193
00194 *psz2--;
00195 }
00196
00197 if (psz2 == psz)
00198 {
00199
00200 CloseHandle (hFile);
00201 return FALSE;
00202 }
00203
00204 SetFilePointer (hFile, (psz2 - psz) - nLen, NULL, FILE_END);
00205 SetEndOfFile (hFile);
00206
00207 delete [] psz;
00208 }
00209 else
00210 {
00211
00212 SetEndOfFile (hFile);
00213 str = "<html>\n";
00214 str += "<style type=\"text/css\">\n";
00215 str += "<!--\n";
00216 str += "H3 { font-size: 19px; font-family: Tahoma; color: #cc0000;}\n";
00217 str += "TR { font-size: 12px; font-family: Tahoma; color: #000033}\n";
00218 str += "TD { font-size: 12px; font-family: Tahoma; color: #000033}\n";
00219 str += "A,A:visited,A:active { text-decoration: none; }\n";
00220 str += "A:hover { text-decoration: underline; }\n";
00221 str += "-->\n";
00222 str += "</style>\n";
00223 str += "<body>\n";
00224 str += "<h3>"; str += LS (L_FDMHIST); str += "</h3>\n";
00225 }
00226
00227 for (int i = 0; i < vpHist.size (); i++)
00228 {
00229 fsDLHistoryRecord* rec = vpHist [i];
00230
00231 str += "<table width=\"75%\" border=\"1\">\n";
00232 str += "<tr><td width=\"180\">"; str += LS (L_URLOFDOWNLOAD); str += ":</td>";
00233 str += "<td><a href=\""; str += rec->strURL; str += "\"> "; str += rec->strURL; str += "</a></td></tr>\n";
00234
00235 if (rec->dateDownloaded.dwHighDateTime) {
00236 str += "<tr><td>"; str += LS (L_DLDEDTOFILE); str += ":</td><td>"; str += rec->strSavedTo; str += "</td></tr>\n";
00237 str += "<tr><td>"; str += LS (L_SIZEOFFILE); str += ":</td><td>"; str += BytesToString (rec->uFileSize); str += "</td></tr>\n";
00238 str += "<tr><td>"; str += LS (L_DLDWASCOMPLETED); str += ":</td><td>"; str += TimeToStr (rec->dateDownloaded); str += "</td></tr>\n";
00239 }
00240 else {
00241 str += "<tr><td>"; str += LS (L_DLDWASDELETED); str += ":</td><td>"; str += TimeToStr (rec->dateRecordAdded); str += "</td></tr>\n";
00242 }
00243
00244 if (rec->strComment != "")
00245 {
00246 CString str2 = rec->strComment; str2.Replace ("\n", "<br>");
00247 str += "<tr><td>"; str += LS (L_DESC); str += ":</td><td>"; str += str2; str += "</td></tr>\n";
00248 }
00249
00250 str += "</table><br>\n";
00251 }
00252
00253 str += "</body></html>";
00254
00255 DWORD dw;
00256 WriteFile (hFile, str, str.GetLength (), &dw, NULL);
00257
00258 CloseHandle (hFile);
00259
00260 return TRUE;
00261 }
00262
00263 CString CDlgExportHistory::TimeToStr(FILETIME &time)
00264 {
00265 char szDate [100], szTime [100];
00266 FileTimeToStr (&time, szDate, szTime, TRUE);
00267 CString str = szDate; str += ", "; str += szTime;
00268 return str;
00269 }