45#define NEWS_REG_SECTION "NEWS"
46#define NEWS_MAX_SHOWN 5
47#define NEWS_DISABLED 4102444800LL
48#define NEWS_WRAP_COLUMNS 80
49#define NEWS_MAX_BYTES (1024 * 1024)
60FXIMPLEMENT(
GUINewsFeed, FXObject, GUINewsFeedMap, ARRAYNUMBER(GUINewsFeedMap))
68collect(
char* data,
size_t size,
size_t nmemb,
void* userdata) {
69 std::string* result =
static_cast<std::string*
>(userdata);
70 const size_t bytes = size * nmemb;
74 result->append(data, bytes);
79checkAbort(
void* userdata, curl_off_t, curl_off_t, curl_off_t, curl_off_t) {
80 return static_cast<std::atomic<bool>*
>(userdata)->load() ? 1 : 0;
86wrapLine(
const std::string& line,
const size_t width) {
89 size_t lastSpace = std::string::npos;
90 for (
size_t i = 0; i < line.size(); i++) {
94 if (i - lineStart >= width && lastSpace != std::string::npos && lastSpace > lineStart) {
95 result += line.substr(lineStart, lastSpace - lineStart) +
"\n";
96 lineStart = lastSpace + 1;
97 lastSpace = std::string::npos;
100 return result + line.substr(lineStart);
105getElement(
const std::string& xml,
const std::string& tag,
size_t& pos, std::string& result) {
106 const std::string open =
"<" + tag;
108 while ((start = xml.find(open, start)) != std::string::npos) {
109 const size_t after = start + open.size();
110 if (after < xml.size() && (xml[after] ==
'>' || xml[after] ==
' ')) {
115 if (start == std::string::npos) {
118 const size_t contentStart = xml.find(
'>', start);
119 const size_t end = xml.find(
"</" + tag +
">", contentStart);
120 if (contentStart == std::string::npos || end == std::string::npos) {
123 result =
StringUtils::trim(xml.substr(contentStart + 1, end - contentStart - 1));
135 std::istringstream in(text);
136 in >> std::get_time(&tm,
"%a, %d %b %Y %H:%M:%S");
141 return _mkgmtime(&tm);
154 myStatusBar(statusBar),
157 myThreadEvent(this, ID_FETCHED) {
187 FXMessageBox::information(
myOwner, MBOX_OK,
TL(
"SUMO news"),
"%s",
TL(
"The news feed is not available."));
199 const time_t now = time(
nullptr);
216 std::string document;
218 CURL* curl = curl_easy_init();
219 if (curl !=
nullptr) {
220 curl_easy_setopt(curl, CURLOPT_URL,
myURL.c_str());
221#if LIBCURL_VERSION_NUM >= 0x075500
222 curl_easy_setopt(curl, CURLOPT_PROTOCOLS_STR,
"http,https");
224 curl_easy_setopt(curl, CURLOPT_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS);
226 curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
227 curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 3L);
228 curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 5L);
229 curl_easy_setopt(curl, CURLOPT_TIMEOUT, 15L);
230 curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1L);
231 curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1L);
232#if defined(WIN32) && LIBCURL_VERSION_NUM >= 0x074700
234 curl_easy_setopt(curl, CURLOPT_SSL_OPTIONS, (
long)(CURLSSLOPT_NATIVE_CA | CURLSSLOPT_REVOKE_BEST_EFFORT));
236 curl_easy_setopt(curl, CURLOPT_USERAGENT,
"sumo-gui-news");
237 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, collect);
238 curl_easy_setopt(curl, CURLOPT_WRITEDATA, &document);
239 curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0
L);
240 curl_easy_setopt(curl, CURLOPT_XFERINFOFUNCTION, checkAbort);
241 curl_easy_setopt(curl, CURLOPT_XFERINFODATA, &
myAbort);
242 ok = curl_easy_perform(curl) == CURLE_OK;
243 curl_easy_cleanup(curl);
249 std::lock_guard<std::mutex> lock(
myMutex);
260 std::string document;
263 std::lock_guard<std::mutex> lock(
myMutex);
275 FXMessageBox::information(
myOwner, MBOX_OK,
TL(
"SUMO news"),
"%s",
TL(
"The news feed is currently not available."));
293 FXMessageBox::information(
myOwner, MBOX_OK,
TL(
"SUMO news"),
"%s",
TL(
"There is currently no news."));
316 static const std::regex urlRegex(
"^(https?://\\S+|([A-Za-z0-9-]+\\.)+[A-Za-z]{2,}(/\\S*)?)$");
317 const char*
const leadChars =
"(\"'";
318 const char*
const trailChars =
".,;:!?)\"'";
319 FXHorizontalFrame* row =
new FXHorizontalFrame(parent, LAYOUT_FILL_X, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
322 const auto flush = [&]() {
324 new FXLabel(row, text.c_str(),
nullptr, LABEL_NORMAL | JUSTIFY_LEFT, 0, 0, 0, 0, 0, 0, 0, 0);
328 std::istringstream words(line);
330 while (words >> word) {
331 const size_t coreStart = word.find_first_not_of(leadChars);
332 const size_t coreEnd = word.find_last_not_of(trailChars);
333 if (coreStart != std::string::npos && coreEnd != std::string::npos && coreStart <= coreEnd
334 && std::regex_match(word.substr(coreStart, coreEnd - coreStart + 1), urlRegex)) {
335 const std::string core = word.substr(coreStart, coreEnd - coreStart + 1);
336 text += (first ?
"" :
" ") + word.substr(0, coreStart);
338 MFXLinkLabel* link =
new MFXLinkLabel(row, core.c_str(),
nullptr, LABEL_NORMAL | JUSTIFY_LEFT, 0, 0, 0, 0, 0, 0, 0, 0);
339 link->setTipText((core.compare(0, 4,
"http") == 0 ? core :
"https://" + core).c_str());
340 text = word.substr(coreEnd + 1);
342 text += (first ?
"" :
" ") + word;
355 FXVerticalFrame* content =
new FXVerticalFrame(scroll, LAYOUT_FILL_X | LAYOUT_FILL_Y);
363 std::string dateText = item.link;
364 if (item.published > 0) {
366 const std::tm* tm = std::gmtime(&item.published);
367 if (tm !=
nullptr && std::strftime(date,
sizeof(date),
"%Y-%m-%d", tm) > 0) {
371 if (!item.link.empty()) {
372 MFXLinkLabel* link =
new MFXLinkLabel(content, dateText.c_str(),
nullptr, LABEL_NORMAL | JUSTIFY_LEFT, 0, 0, 0, 0, 0, 0, 0, 0);
373 link->setTipText(item.link.c_str());
376 std::vector<std::string> lines;
377 for (
const std::string& part : {
378 item.title, item.description
380 std::istringstream in(part);
382 while (std::getline(in, line)) {
386 for (
const std::string& line : lines) {
390 while (std::getline(wrapped, part)) {
398 FXCheckButton* autoCheck =
new FXCheckButton(dialog,
TL(
"Check for news automatically"),
nullptr, 0, CHECKBUTTON_NORMAL | LAYOUT_LEFT);
399 autoCheck->setCheck(wasEnabled);
405 dialog->execute(PLACEMENT_OWNER);
406 if (autoCheck->getCheck() != wasEnabled) {
419std::vector<GUINewsFeed::Item>
421 std::vector<Item> result;
437 result.push_back(item);
#define GUIDesignAuxiliarHorizontalFrame
design for auxiliar (Without borders) horizontal frame used to pack another frames
#define GUIDesignDialogBox
#define GUIDesignScrollWindowFixed(width, height)
design for the content scroll of UndoList
#define GUIDesignHorizontalSeparator
#define GUIDesignButtonDialog
#define GUIDesignHorizontalFrame
Horizontal frame extended over frame parent with padding and spacing.
static std::string wrapLine(const std::string &line, const size_t width)
inserts line breaks at spaces so that lines have at most the given number of bytes (if possible)
FXDEFMAP(GUINewsFeed) GUINewsFeedMap[]
static void addRichLine(FXComposite *parent, const std::string &line)
adds a row of labels to parent, words that look like URLs (with or without scheme) become clickable l...
static time_t parseDate(const std::string &text)
parses an RFC 822 date like "Thu, 19 Nov 2026 10:00:00 +0000" (always UTC)
#define NEWS_WRAP_COLUMNS
static bool getElement(const std::string &xml, const std::string &tag, size_t &pos, std::string &result)
returns the text of the first <tag> element in xml (starting at pos), the feed is created by tools/ne...
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
static FXButton * buildFXButton(FXComposite *p, const std::string &text, const std::string &tip, const std::string &help, FXIcon *ic, FXObject *tgt, FXSelector sel, FXuint opts=BUTTON_NORMAL, FXint x=0, FXint y=0, FXint w=0, FXint h=0, FXint pl=DEFAULT_PAD, FXint pr=DEFAULT_PAD, FXint pt=DEFAULT_PAD, FXint pb=DEFAULT_PAD)
build button
static FXIcon * getIcon(const GUIIcon which)
returns a icon previously defined in the enum GUIIcon
Periodically fetches an RSS feed with news about upcoming releases.
FXButton * myButton
the button in the status bar (null while there is nothing new)
GUINewsFeed(FXApp *app, FXWindow *owner, FXStatusBar *statusBar, const std::string &url=DEFAULT_URL)
constructor
FXStatusBar * myStatusBar
the status bar holding the news button
time_t getLastAccess() const
time of the last successful access (0 if never)
static const std::string DEFAULT_URL
default feed location
std::atomic< bool > myAbort
tells the worker thread to give up
std::mutex myMutex
protects myDocument and myOK
time_t myPreviousAccess
the last access time before this download started
void fetch()
the download, executed in myThread
bool myShowAfterFetch
whether the dialog should open as soon as the running download finishes
void showLatest()
shows the latest items, downloading them first if necessary
static std::vector< Item > parseRSS(const std::string &xml)
extracts the items of an RSS document
std::string myURL
the feed address
long onCmdShow(FXObject *, FXSelector, void *)
called when the news button was clicked, shows the latest items
std::string myDocument
the downloaded document
FXEX::MFXThreadEvent myThreadEvent
wakes the GUI thread
FXApp * myApp
the application (registry access)
FXWindow * myOwner
parent of the news dialog
~GUINewsFeed()
destructor, aborts a running download
static const time_t MIN_INTERVAL
minimum time between two accesses (seconds)
std::vector< Item > myItems
the latest items (empty until a download succeeded)
bool myOK
whether the download succeeded
bool startCheck(const bool force=false)
starts a download if enabled, curl is available and the last access is long enough ago
std::thread myThread
the worker thread
long onFetched(FXObject *, FXSelector, void *)
called in the GUI thread when the download has finished
static long long int toLong(const std::string &sData)
converts a string into the long value described by it by calling the char-type converter,...
static std::string trim(const std::string s, const std::string &t=" \t\n")
remove leading and trailing whitespace
static std::string replace(std::string str, const std::string &what, const std::string &by)
Replaces all occurrences of the second string by the third string within the first string.
a single entry of the feed
time_t published
publication time (seconds since epoch), 0 if unknown