?? updatebar.cpp.svn-base
字號:
// save feed
CString feedPathName = GetCacheFile(FILE_TYPE_FEED, Config.CacheLocation, si->Info->FileName);
CreatePath(feedPathName);
si->Feed->Save(feedPathName);
// set file time
HANDLE hFile = CreateFile(feedPathName, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
if (hFile != INVALID_HANDLE_VALUE) {
FILETIME ftNow;
SYSTEMTIME stNow;
GetLocalTime(&stNow);
SystemTimeToFileTime(&stNow, &ftNow);
SetFileTime(hFile, NULL, NULL, &ftNow);
CloseHandle(hFile);
}
// notify today plugin
NotifyTodayPlugin(CheckFeedsMessage);
// clean items
ClearImages(itemsToClean);
ClearHtmlPages(itemsToClean);
ClearEnclosures(itemsToClean);
for (i = 0; i < itemsToClean.GetSize(); i++)
delete itemsToClean[i];
// check keywords in new items
for (i = 0; i < newItems.GetSize(); i++)
newItems.GetAt(i)->SearchKeywords(Config.Keywords);
// cache
if (!ui->UpdateOnly) {
// enqueue items to cache
if (si->Info->UseGlobalCacheOptions) {
if (Config.CacheImages) EnqueueImages(newItems); // cache item images
if (Config.CacheHtml) EnqueueHtmls(newItems); // cache HTML content
}
else {
if (si->Info->CacheItemImages) EnqueueImages(newItems); // cache item images
if (si->Info->CacheHtml) EnqueueHtmls(newItems); // cache HTML content
}
// cache enclosures
if (si->Info->CacheEnclosures) EnqueueEnclosures(newItems, si->Info->EnclosureLimit);
}
// get favicon if neccessary
if (si->CheckFavIcon) {
// temp file name
CString faviconFileName = GetCacheFile(FILE_TYPE_FAVICON, Config.CacheLocation, si->Info->FileName);
// get favicon
if (DownloadFavIcon(si->Feed->HtmlUrl, faviconFileName)) {
if (frame != NULL) frame->SendMessage(UWM_UPDATE_FAVICON, 0, (LPARAM) si); // update favicon in GUI
}
si->CheckFavIcon = FALSE;
}
// send message to update the feed view
if (frame != NULL) frame->SendMessage(UWM_UPDATE_FEED, 0, (LPARAM) si);
SaveSiteItemUnreadCount(si, SiteList.GetIndexOf(si));
SaveSiteItemFlaggedCount(si, SiteList.GetIndexOf(si));
// process
NewItemsCount += si->Feed->GetNewCount();
}
else {
if (Downloader->Error == DOWNLOAD_ERROR_DISK_FULL) {
CErrorItem *ei = new CErrorItem(IDS_DISK_FULL);
ei->Type = CErrorItem::System;
Errors.Add(ei);
Terminate = TRUE;
}
else {
CString sMsg;
sMsg.Format(_T("%s: %s"), si->Name, sync->GetErrorMsg());
CErrorItem *ei = new CErrorItem(sMsg);
ei->Type = CErrorItem::Site;
ei->SiteIdx = SiteList.GetIndexOf(si);
ei->UpdateOnly = ui->UpdateOnly;
Errors.Add(ei);
}
}
delete feed;
m_ctlProgress.SetStep(1);
m_ctlProgress.StepIt();
m_ctlProgress.Redraw(FALSE);
}
else {
Errors.Add(new CErrorItem(IDS_AUTHENTICATION_FAILED));
}
EnterCriticalSection(&CSDownloader);
// delete sync;
// delete Downloader;
Downloader = NULL;
LeaveCriticalSection(&CSDownloader);
while (!UpdateList.IsEmpty())
delete UpdateList.RemoveHead();
}
void CUpdateBar::DownloadHtmlPage(CDownloadItem *di) {
LOG0(1, "CUpdateBar::DownloadHtmlPage()");
if (FileExists(di->FileName))
return; // file already exists
BOOL ok = FALSE;
m_ctlProgress.SetRange(0, 150000);
CString url = di->URL;
CString tmpFileName = di->FileName + _T(".part");
Downloader->SetUAString(Config.UserAgent);
if (Downloader->SaveHttpObject(url, tmpFileName))
ok = TRUE;
else {
if (Downloader->Error != DOWNLOAD_ERROR_DISK_FULL) {
// optimizing failed -> use original url
Downloader->Reset();
if (Config.UseHtmlOptimizer && Downloader->SaveHttpObject(di->URL, tmpFileName))
ok = TRUE;
}
}
if (ok) {
if (Downloader->GetMimeType().CompareNoCase(_T("text/html")) == 0) {
if (!TranslateForOfflineReading(tmpFileName, di->FileName, Downloader->GetCharset()))
DeleteFile(di->FileName);
DeleteFile(tmpFileName);
}
else {
// it was not an HTML page -> move it among enclosures
CString fileName = GetCacheFile(FILE_TYPE_ENCLOSURE, Config.CacheLocation, di->URL);
CreatePath(fileName);
MoveFile(tmpFileName, fileName);
CString rd = GetCachePath(FILE_TYPE_HTML, Config.CacheLocation);
RemoveEmptyDirs(tmpFileName, rd);
}
}
else {
if (Downloader->Error == DOWNLOAD_ERROR_DISK_FULL) {
CErrorItem *ei = new CErrorItem(IDS_DISK_FULL);
ei->Type = CErrorItem::System;
Errors.Add(ei);
Terminate = TRUE;
}
else {
CString sErrMsg;
sErrMsg.Format(IDS_ERROR_DOWNLOADING_FILE, di->URL);
CErrorItem *ei = new CErrorItem(sErrMsg);
ei->Type = CErrorItem::File;
ei->SiteIdx = di->SiteIdx;
ei->Url = di->URL;
ei->FileType = FILE_TYPE_HTML;
Errors.Add(ei);
}
}
}
void CUpdateBar::DownloadFile(CDownloadItem *di) {
LOG0(1, "CUpdateBar::DownloadFile()");
if (FileExists(di->FileName))
return; // file already exists
BOOL ok = FALSE;
Downloader->SetUAString(_T(""));
CString tmpFileName = di->FileName + _T(".part");
CString url = di->URL;
if (Downloader->SaveHttpObject(url, tmpFileName)) {
MoveFile(tmpFileName, di->FileName);
}
else {
if (Downloader->Error == DOWNLOAD_ERROR_DISK_FULL) {
CErrorItem *ei = new CErrorItem(IDS_DISK_FULL);
ei->Type = CErrorItem::System;
Errors.Add(ei);
Terminate = TRUE;
}
else {
CString sErrMsg;
sErrMsg.Format(IDS_ERROR_DOWNLOADING_FILE, di->URL);
CErrorItem *ei = new CErrorItem(sErrMsg);
ei->Type = CErrorItem::File;
ei->SiteIdx = SITE_INVALID;
ei->Url = di->URL;
ei->FileType = di->Type;
Errors.Add(ei);
}
}
}
void CUpdateBar::DownloadItems() {
LOG0(1, "CUpdateBar::DownloadItems()");
State = UPDATE_STATE_CACHING;
while (!Terminate && !DownloadQueue.IsEmpty()) {
CDownloadItem *di = DownloadQueue.Dequeue();
EnterCriticalSection(&CSDownloader);
Downloader = new CDownloader();
m_ctlProgress.SetDownloader(Downloader);
LeaveCriticalSection(&CSDownloader);
m_ctlProgress.SetPos(0);
UpdateProgressText();
m_ctlProgress.Redraw(TRUE);
switch (di->Type) {
case FILE_TYPE_HTML: DownloadHtmlPage(di); break;
default: DownloadFile(di); break;
}
DownloadQueue.FinishedItems++;
int lo, hi;
m_ctlProgress.GetRange(lo, hi);
m_ctlProgress.SetPos(hi);
m_ctlProgress.Redraw(FALSE);
EnterCriticalSection(&CSDownloader);
m_ctlProgress.SetDownloader(NULL);
delete Downloader;
Downloader = NULL;
LeaveCriticalSection(&CSDownloader);
delete di;
}
// empty download queue
while (!DownloadQueue.IsEmpty())
delete DownloadQueue.Dequeue();
}
void CUpdateBar::UpdateThread() {
LOG0(3, "CUpdateBar::UpdateThread() - begin");
//////
CSuspendKiller suspendKiller;
CMainFrame *frame = (CMainFrame *) AfxGetMainWnd();
Terminate = FALSE;
ErrorCount = 0;
NewItemsCount = 0;
// update feeds
BOOL disconnect;
if (CheckConnection(Config.AutoConnect, disconnect)) {
// show update bar
m_ctlProgress.ShowWindow(SW_SHOW);
m_ctlText.ShowWindow(SW_HIDE);
if (frame != NULL) frame->PostMessage(UWM_SHOW_UPDATEBAR, TRUE);
if (UpdateList.GetCount() > 0)
UpdateFeeds();
// download items
if (DownloadQueue.GetCount() > 0)
DownloadItems();
// notify
if (Config.NotifyNew && GetForegroundWindow()->GetSafeHwnd() != frame->GetSafeHwnd()) {
if (NewItemsCount > 0) {
prssrNotificationRemove();
prssrNotification(NewItemsCount);
}
}
if (disconnect)
Connection.HangupConnection();
// done
if (Errors.GetCount() > 0) {
ShowErrorCount();
}
else {
if (frame != NULL) frame->SendMessage(UWM_SHOW_UPDATEBAR, FALSE);
}
}
else {
Terminate = TRUE;
Errors.Add(new CErrorItem(IDS_NO_INTERNET_CONNECTION));
ShowErrorCount();
if (frame != NULL) frame->PostMessage(UWM_SHOW_UPDATEBAR, TRUE);
}
// end
CloseHandle(HUpdateThread);
HUpdateThread = NULL;
if (frame != NULL) frame->SendMessage(UWM_UPDATE_FINISHED);
LOG0(3, "CUpdateBar::UpdateThread() - end");
}
void CUpdateBar::UpdateProgressText() {
CString sText;
CString sTitle;
switch (State) {
case UPDATE_STATE_CACHING:
sText.Format(IDS_DOWNLOADING);
sTitle.Format(_T("%d / %d: %s..."), DownloadQueue.FinishedItems + 1, DownloadQueue.FinishedItems + DownloadQueue.GetCount() + 1, sText);
m_ctlProgress.SetText(sTitle);
break;
case UPDATE_STATE_RSS:
sText.Format(IDS_UPDATING);
if (SiteName.IsEmpty())
sTitle = sText;
else
sTitle.Format(_T("%s: %s"), SiteName, sText);
m_ctlProgress.SetText(sTitle);
break;
case UPDATE_STATE_AUTHENTICATING:
sText.Format(IDS_AUTHENTICATING);
m_ctlProgress.SetText(sText);
break;
case UPDATE_STATE_SYNCING:
sText.Format(IDS_SYNCING);
sTitle.Format(_T("%s: %s"), SiteName, sText);
m_ctlProgress.SetText(sTitle);
break;
default:
sText.Format(IDS_UPDATING);
m_ctlProgress.SetText(sText);
break;
}
}
void CUpdateBar::ShowErrorCount() {
CString strError;
if (Errors.GetCount() > 1)
strError.Format(IDS_N_ERRORS, Errors.GetCount());
else {
POSITION pos = Errors.GetFirstPos();
if (pos != NULL) {
CErrorItem *ei = Errors.GetNext(pos);
strError = ei->Message;
}
else
strError.Format(IDS_N_ERRORS, Errors.GetCount()); // this sould not happend, but for sure
}
ShowError(strError);
}
void CUpdateBar::ShowError(UINT nID) {
LOG1(5, "CUpdateBar::ShowError(%d)", nID);
CString str;
str.LoadString(nID);
ShowError(str);
}
void CUpdateBar::ShowError(const CString &str) {
LOG1(3, "CUpdateBar::ShowError(%S)", str);
CMainFrame *frame = (CMainFrame *) AfxGetMainWnd();
// error
m_ctlProgress.ShowWindow(SW_HIDE);
m_ctlText.ShowWindow(SW_SHOW);
m_ctlText.SetWindowText(str);
if (frame != NULL) frame->SendMessage(UWM_SHOW_UPDATEBAR, TRUE);
}
BEGIN_MESSAGE_MAP(CUpdateBar, CControlBar)
//{{AFX_MSG_MAP(CUpdateBar)
ON_WM_PAINT()
ON_WM_SIZE()
ON_BN_CLICKED(IDC_UPDATE_STOP, OnStop)
ON_WM_TIMER()
ON_BN_CLICKED(IDC_UPDATE_TEXT, OnTextClicked)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
void CUpdateBar::OnStop() {
LOG0(1, "CUpdateBar::OnStop()");
EnterCriticalSection(&CSDownloader);
if (Downloader != NULL)
Downloader->Terminate();
LeaveCriticalSection(&CSDownloader);
Terminate = TRUE;
// hide update bar
CMainFrame *frame = (CMainFrame *) AfxGetMainWnd();
if (frame != NULL) frame->SendMessage(UWM_SHOW_UPDATEBAR, FALSE);
if (frame != NULL) frame->SendMessage(UWM_UPDATE_FINISHED);
}
void CUpdateBar::OnTextClicked() {
LOG0(1, "CUpdateBar::OnTextClicked()");
CMainFrame *frame = (CMainFrame *) AfxGetMainWnd();
if (frame != NULL) {
frame->SendMessage(UWM_SHOW_UPDATEBAR, FALSE);
frame->PostMessage(WM_COMMAND, ID_TOOLS_ERRORS, NULL); // show errors
}
}
void CUpdateBar::OnPaint() {
CControlBar::OnPaint();
CDC *pDC = GetDC();
if (pDC) {
CRect rc;
GetClientRect(&rc);
pDC->FillSolidRect(rc, ::GetSysColor(COLOR_3DFACE));
}
ReleaseDC(pDC);
}
void CUpdateBar::OnSize(UINT nType, int cx, int cy) {
LOG0(1, "CUpdateBar::OnSize()");
CControlBar::OnSize(nType, cx, cy);
// reposition controls
if (IsWindow(m_ctlProgress.GetSafeHwnd())) {
m_ctlProgress.SetWindowPos(NULL, SCALEX(2), SCALEY(3),
cx - SCALEX(5) - SCALEX(16) - 1, SCALEY(16) - 1, SWP_NOACTIVATE | SWP_NOZORDER);
}
if (IsWindow(m_ctlText.GetSafeHwnd())) {
m_ctlText.SetWindowPos(NULL, SCALEX(2), SCALEY(4),
cx - SCALEX(5) - SCALEX(16) - 1, SCALEY(16) - 1, SWP_NOACTIVATE | SWP_NOZORDER);
}
if (IsWindow(m_ctlStopBtn.GetSafeHwnd()))
m_ctlStopBtn.SetWindowPos(NULL, cx - SCALEX(2) - SCALEX(16) + 1, SCALEY(3),
SCALEX(16) - 1, SCALEY(16) - 1, SWP_NOACTIVATE | SWP_NOZORDER);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -