boinc/clientgui/NoticeListCtrl.cpp

316 lines
9.3 KiB
C++
Raw Normal View History

2012-11-30 11:57:23 +00:00
// This file is part of BOINC.
// http://boinc.berkeley.edu
// Copyright (C) 2008 University of California
//
// BOINC is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License
// as published by the Free Software Foundation,
// either version 3 of the License, or (at your option) any later version.
//
// BOINC is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with BOINC. If not, see <http://www.gnu.org/licenses/>.
//
#if defined(__GNUG__) && !defined(__APPLE__)
#pragma implementation "NoticeListCtrl.h"
#endif
#include "stdwx.h"
#include "diagnostics.h"
#include "util.h"
#include "mfile.h"
#include "miofile.h"
#include "parse.h"
#include "error_numbers.h"
#include "wizardex.h"
#include "error_numbers.h"
#include "browser.h"
2012-11-30 11:57:23 +00:00
#include "Events.h"
#include "BOINCGUIApp.h"
#include "SkinManager.h"
2012-11-30 11:57:23 +00:00
#include "MainDocument.h"
#include "NoticeListCtrl.h"
////@begin XPM images
////@end XPM images
/* CNoticeListCtrl type definition
2012-11-30 11:57:23 +00:00
*/
IMPLEMENT_DYNAMIC_CLASS( CNoticeListCtrl, wxWindow )
2012-11-30 11:57:23 +00:00
/*!
* CNoticeListCtrl event table definition
*/
BEGIN_EVENT_TABLE( CNoticeListCtrl, wxWindow )
2012-11-30 11:57:23 +00:00
////@begin CNoticeListCtrl event table entries
EVT_WEBVIEW_NAVIGATING(ID_LIST_NOTIFICATIONSVIEW, CNoticeListCtrl::OnLinkClicked)
EVT_WEBVIEW_ERROR(ID_LIST_NOTIFICATIONSVIEW, CNoticeListCtrl::OnWebViewError)
2012-11-30 11:57:23 +00:00
////@end CNoticeListCtrl event table entries
END_EVENT_TABLE()
/*!
* CNoticeListCtrl constructors
*/
CNoticeListCtrl::CNoticeListCtrl( ) {
2012-11-30 11:57:23 +00:00
}
CNoticeListCtrl::CNoticeListCtrl( wxWindow* parent ) {
2012-11-30 11:57:23 +00:00
Create( parent );
}
CNoticeListCtrl::~CNoticeListCtrl( ) {
2012-11-30 11:57:23 +00:00
}
/*!
* CNoticeListCtrl creator
*/
bool CNoticeListCtrl::Create( wxWindow* parent ) {
2012-11-30 11:57:23 +00:00
////@begin CNoticeListCtrl member initialisation
////@end CNoticeListCtrl member initialisation
////@begin CNoticeListCtrl creation
wxWindow::Create( parent, ID_LIST_NOTIFICATIONSVIEW, wxDefaultPosition, wxDefaultSize,
2012-11-30 11:57:23 +00:00
wxSUNKEN_BORDER | wxTAB_TRAVERSAL );
m_browser = wxWebView::New( this, ID_LIST_NOTIFICATIONSVIEW );
2012-11-30 11:57:23 +00:00
////@end CNoticeListCtrl creation
wxBoxSizer *topsizer;
topsizer = new wxBoxSizer(wxVERTICAL);
topsizer->Add(m_browser, 1, wxEXPAND);
SetAutoLayout(true);
SetSizer(topsizer);
m_itemCount = 0;
m_noticesBody = wxT("<html><head></head><body></body></html>");
// Display the fetching notices message until we have notices
// to display or have determined that there are no notices.
m_bDisplayFetchingNotices = false;
2012-11-30 11:57:23 +00:00
m_bDisplayEmptyNotice = true;
m_bNeedsReloading = false;
2012-11-30 11:57:23 +00:00
return TRUE;
}
int CNoticeListCtrl::GetItemCount() {
return m_itemCount;
2012-11-30 11:57:23 +00:00
}
void CNoticeListCtrl::SetItemCount(int newCount) {
int i;
2012-11-30 11:57:23 +00:00
CMainDocument* pDoc = wxGetApp().GetDocument();
CSkinAdvanced* pSkinAdvanced = wxGetApp().GetSkinManager()->GetAdvanced();
2012-11-30 11:57:23 +00:00
wxString strTitle = wxEmptyString;
wxString strDescription = wxEmptyString;
wxString strProjectName = wxEmptyString;
wxString strURL = wxEmptyString;
wxString strCreateTime = wxEmptyString;
wxString strCategory = wxEmptyString;
2012-11-30 11:57:23 +00:00
wxString strBuffer = wxEmptyString;
wxString strTemp = wxEmptyString;
wxDateTime dtBuffer;
wxASSERT(pDoc);
wxASSERT(wxDynamicCast(pDoc, CMainDocument));
wxASSERT(pSkinAdvanced);
wxASSERT(wxDynamicCast(pSkinAdvanced, CSkinAdvanced));
2012-11-30 11:57:23 +00:00
m_itemCount = newCount;
m_noticesBody = wxT("<html><head></head><body><font face=helvetica>");
2012-11-30 11:57:23 +00:00
for (i=0; i<newCount; ++i) {
if (pDoc->IsConnected()) {
NOTICE* np = pDoc->notice((unsigned int)i);
strCategory = wxString(np->category, wxConvUTF8);
strProjectName = wxString(np->project_name, wxConvUTF8);
2012-11-30 11:57:23 +00:00
strURL = wxString(np->link, wxConvUTF8);
strTitle = wxString(np->title, wxConvUTF8);
// Fix-up title
if (strCategory == wxT("client")) {
strBuffer.Printf(
wxT("_(\"Notice from %s\")"),
pSkinAdvanced->GetApplicationShortName().c_str()
);
if (strProjectName.size()) {
strTemp.Printf(wxT("%s: %s"), strProjectName.c_str(), strBuffer.c_str());
} else {
strTemp.Printf(wxT("%s"), strBuffer.c_str());
}
} else if (strCategory == wxT("scheduler")) {
strTemp.Printf(wxT("%s: %s"), strProjectName.c_str(), wxT("_(\"Notice from server\")"));
2012-11-30 11:57:23 +00:00
} else {
if (strProjectName.size()) {
strTemp.Printf(wxT("%s: %s"), strProjectName.c_str(), strTitle.c_str());
} else {
strTemp = strTitle;
}
2012-11-30 11:57:23 +00:00
}
strTitle = strTemp;
eol_to_br(strTitle);
localize(strTitle);
2012-11-30 11:57:23 +00:00
strDescription = wxString(np->description.c_str(), wxConvUTF8);
eol_to_br(strDescription);
localize(strDescription);
2012-11-30 11:57:23 +00:00
// RSS feeds and web pages may use protocol-relative (scheme-relative)
// URLs, such as <img src="//sample.com/test.jpg"/>
// Since the html comes from a web server via http, the scheme is
// assumed to also be http. But we have cached the html in a local
// file, so it is no longer associated with the http protocol / scheme.
// Therefore all our URLs must explicity specify the http protocol.
//
// The second argument to wxWebView::SetPage is supposed to take care
// of this automatically, but fails to do so under Windows, so we do
// it here explicitly.
strDescription.Replace(wxT("\"//"), wxT("\"http://"));
strDescription.Replace(wxT("</a>"), wxT("</a> "));
dtBuffer.Set((time_t)np->create_time);
strCreateTime = dtBuffer.Format();
2012-11-30 11:57:23 +00:00
// Put dividers between notices, but not before first or after last
if (i == 0) {
strBuffer = wxEmptyString;
} else {
strBuffer = wxT("<hr>");
}
strBuffer += wxT("<table border=0 cellpadding=5><tr><td>");
2012-11-30 11:57:23 +00:00
if (!strTitle.IsEmpty()) {
strTemp.Printf(
wxT("<b>%s</b><br>"),
strTitle.c_str()
);
strBuffer += strTemp;
}
2012-11-30 11:57:23 +00:00
strBuffer += strDescription;
2012-11-30 11:57:23 +00:00
strBuffer += wxT("<br><font size=-2 color=#8f8f8f>");
2012-11-30 11:57:23 +00:00
strBuffer += strCreateTime;
2012-11-30 11:57:23 +00:00
if (!strURL.IsEmpty()) {
strTemp.Printf(
wxT(" &middot; <a href=%s>%s</a> "),
strURL.c_str(),
_("more...")
);
strBuffer += strTemp;
}
2012-11-30 11:57:23 +00:00
strBuffer += wxT("</font></td></tr></table>");
}
m_noticesBody += strBuffer;
2012-11-30 11:57:23 +00:00
}
m_noticesBody += wxT("</font></body></html>");
m_browser->SetPage(m_noticesBody, wxT("http://"));
2012-11-30 11:57:23 +00:00
}
void CNoticeListCtrl::Clear() {
m_bNeedsReloading = true;
2012-12-14 11:40:35 +00:00
UpdateUI();
2012-11-30 11:57:23 +00:00
}
void CNoticeListCtrl::OnLinkClicked( wxWebViewEvent& event ) {
if (event.GetURL().StartsWith(wxT("http://")) || event.GetURL().StartsWith(wxT("https://"))) {
event.Veto(); // Tell wxWebView not to follow link
wxLaunchDefaultBrowser(event.GetURL());
} else {
event.Skip();
}
}
void CNoticeListCtrl::OnWebViewError( wxWebViewEvent& event ) {
fprintf(stderr, "wxWebView error: target=%s, URL=%s\n",
2013-11-12 11:45:39 +00:00
(event.GetTarget().ToStdString()).c_str(), (event.GetURL().ToStdString()).c_str());
event.Skip();
}
2012-11-30 11:57:23 +00:00
/*!
* Update the UI.
*/
bool CNoticeListCtrl::UpdateUI() {
2012-11-30 11:57:23 +00:00
static bool bAlreadyRunning = false;
CMainDocument* pDoc = wxGetApp().GetDocument();
wxASSERT(pDoc);
wxASSERT(wxDynamicCast(pDoc, CMainDocument));
// Call Freeze() / Thaw() only when actually needed;
// otherwise it causes unnecessary redraws
int noticeCount = pDoc->GetNoticeCount();
if ((noticeCount < 0) || (!pDoc->IsConnected()) || m_bNeedsReloading) {
if (GetItemCount()) {
SetItemCount(0);
Refresh();
}
// Display "Fetching Notices" text only when connected
m_bDisplayFetchingNotices = pDoc->IsConnected();
m_bDisplayEmptyNotice = false;
m_bNeedsReloading = false;
return true;
}
if (noticeCount == 0) {
if (GetItemCount()) {
SetItemCount(0);
Refresh();
}
m_bDisplayFetchingNotices = false;
2012-11-30 11:57:23 +00:00
m_bDisplayEmptyNotice = true;
m_bNeedsReloading = false;
2012-11-30 11:57:23 +00:00
return true;
}
if (!bAlreadyRunning) {
bAlreadyRunning = true;
if (
pDoc->IsConnected() &&
(pDoc->notices.complete ||
((int)GetItemCount() != noticeCount))
2012-11-30 11:57:23 +00:00
) {
pDoc->notices.complete = false;
Freeze();
SetItemCount(noticeCount);
m_bDisplayFetchingNotices = false;
m_bDisplayEmptyNotice = false;
2012-11-30 11:57:23 +00:00
Thaw();
}
bAlreadyRunning = false;
}
return true;
}