mirror of
https://github.com/aseprite/aseprite.git
synced 2025-02-10 06:40:35 +00:00
Cache RSS news file
This commit is contained in:
parent
200e612670
commit
057532e7d7
@ -76,6 +76,9 @@
|
|||||||
<option id="use_native_cursor" type="bool" default="false" />
|
<option id="use_native_cursor" type="bool" default="false" />
|
||||||
<option id="flash_layer" type="bool" default="false" />
|
<option id="flash_layer" type="bool" default="false" />
|
||||||
</section>
|
</section>
|
||||||
|
<section id="news">
|
||||||
|
<option id="cache_file" type="std::string" />
|
||||||
|
</section>
|
||||||
</global>
|
</global>
|
||||||
|
|
||||||
<tool>
|
<tool>
|
||||||
|
@ -11,11 +11,15 @@
|
|||||||
|
|
||||||
#include "app/ui/news_listbox.h"
|
#include "app/ui/news_listbox.h"
|
||||||
|
|
||||||
|
#include "app/app.h"
|
||||||
|
#include "app/pref/preferences.h"
|
||||||
#include "app/res/http_loader.h"
|
#include "app/res/http_loader.h"
|
||||||
#include "app/ui/skin/skin_theme.h"
|
#include "app/ui/skin/skin_theme.h"
|
||||||
#include "app/ui/skin/style.h"
|
#include "app/ui/skin/style.h"
|
||||||
#include "app/xml_document.h"
|
#include "app/xml_document.h"
|
||||||
|
#include "base/fs.h"
|
||||||
#include "base/string.h"
|
#include "base/string.h"
|
||||||
|
#include "base/time.h"
|
||||||
#include "ui/link_label.h"
|
#include "ui/link_label.h"
|
||||||
#include "ui/paint_event.h"
|
#include "ui/paint_event.h"
|
||||||
#include "ui/preferred_size_event.h"
|
#include "ui/preferred_size_event.h"
|
||||||
@ -182,6 +186,11 @@ NewsListBox::NewsListBox()
|
|||||||
, m_timer(250, this)
|
, m_timer(250, this)
|
||||||
{
|
{
|
||||||
m_timer.Tick.connect(&NewsListBox::onTick, this);
|
m_timer.Tick.connect(&NewsListBox::onTick, this);
|
||||||
|
|
||||||
|
std::string cache = App::instance()->preferences().news.cacheFile();
|
||||||
|
if (!cache.empty() && base::is_file(cache) && validCache(cache))
|
||||||
|
parseFile(cache);
|
||||||
|
else
|
||||||
reload();
|
reload();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -227,13 +236,21 @@ void NewsListBox::onTick()
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
parseFile(fn);
|
||||||
|
}
|
||||||
|
|
||||||
|
void NewsListBox::parseFile(const std::string& filename)
|
||||||
|
{
|
||||||
|
View* view = View::getView(this);
|
||||||
|
|
||||||
XmlDocumentRef doc;
|
XmlDocumentRef doc;
|
||||||
try {
|
try {
|
||||||
doc = open_xml(fn);
|
doc = open_xml(filename);
|
||||||
}
|
}
|
||||||
catch (...) {
|
catch (...) {
|
||||||
addChild(new ProblemsItem());
|
addChild(new ProblemsItem());
|
||||||
View::getView(this)->updateView();
|
if (view)
|
||||||
|
view->updateView();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -270,7 +287,23 @@ void NewsListBox::onTick()
|
|||||||
if (linkXml)
|
if (linkXml)
|
||||||
addChild(new NewsItem(linkXml->GetText(), "More...", ""));
|
addChild(new NewsItem(linkXml->GetText(), "More...", ""));
|
||||||
|
|
||||||
View::getView(this)->updateView();
|
if (view)
|
||||||
|
view->updateView();
|
||||||
|
|
||||||
|
// Save as cached news
|
||||||
|
App::instance()->preferences().news.cacheFile(filename);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool NewsListBox::validCache(const std::string& filename)
|
||||||
|
{
|
||||||
|
base::Time
|
||||||
|
now = base::current_time(),
|
||||||
|
time = base::get_modification_time(filename);
|
||||||
|
|
||||||
|
now.dateOnly();
|
||||||
|
time.dateOnly();
|
||||||
|
|
||||||
|
return (now == time);
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace app
|
} // namespace app
|
||||||
|
@ -12,6 +12,8 @@
|
|||||||
#include "ui/listbox.h"
|
#include "ui/listbox.h"
|
||||||
#include "ui/timer.h"
|
#include "ui/timer.h"
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
namespace app {
|
namespace app {
|
||||||
|
|
||||||
class HttpLoader;
|
class HttpLoader;
|
||||||
@ -25,6 +27,8 @@ namespace app {
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
void onTick();
|
void onTick();
|
||||||
|
void parseFile(const std::string& filename);
|
||||||
|
bool validCache(const std::string& filename);
|
||||||
|
|
||||||
ui::Timer m_timer;
|
ui::Timer m_timer;
|
||||||
HttpLoader* m_loader;
|
HttpLoader* m_loader;
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
# Aseprite Base Library
|
# Aseprite Base Library
|
||||||
# Copyright (c) 2001-2014 David Capello
|
# Copyright (c) 2001-2015 David Capello
|
||||||
|
|
||||||
include(CheckCSourceCompiles)
|
include(CheckCSourceCompiles)
|
||||||
include(CheckCXXSourceCompiles)
|
include(CheckCXXSourceCompiles)
|
||||||
@ -65,6 +65,7 @@ set(BASE_SOURCES
|
|||||||
system_console.cpp
|
system_console.cpp
|
||||||
temp_dir.cpp
|
temp_dir.cpp
|
||||||
thread.cpp
|
thread.cpp
|
||||||
|
time.cpp
|
||||||
trim_string.cpp
|
trim_string.cpp
|
||||||
version.cpp)
|
version.cpp)
|
||||||
|
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
// Aseprite Base Library
|
// Aseprite Base Library
|
||||||
// Copyright (c) 2001-2013 David Capello
|
// Copyright (c) 2001-2013, 2015 David Capello
|
||||||
//
|
//
|
||||||
// This file is released under the terms of the MIT license.
|
// This file is released under the terms of the MIT license.
|
||||||
// Read LICENSE.txt for more information.
|
// Read LICENSE.txt for more information.
|
||||||
@ -12,6 +12,8 @@
|
|||||||
|
|
||||||
namespace base {
|
namespace base {
|
||||||
|
|
||||||
|
class Time;
|
||||||
|
|
||||||
bool is_file(const std::string& path);
|
bool is_file(const std::string& path);
|
||||||
bool is_directory(const std::string& path);
|
bool is_directory(const std::string& path);
|
||||||
|
|
||||||
@ -23,6 +25,8 @@ namespace base {
|
|||||||
bool has_readonly_attr(const std::string& path);
|
bool has_readonly_attr(const std::string& path);
|
||||||
void remove_readonly_attr(const std::string& path);
|
void remove_readonly_attr(const std::string& path);
|
||||||
|
|
||||||
|
Time get_modification_time(const std::string& path);
|
||||||
|
|
||||||
void make_directory(const std::string& path);
|
void make_directory(const std::string& path);
|
||||||
void make_all_directories(const std::string& path);
|
void make_all_directories(const std::string& path);
|
||||||
void remove_directory(const std::string& path);
|
void remove_directory(const std::string& path);
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
// Aseprite Base Library
|
// Aseprite Base Library
|
||||||
// Copyright (c) 2001-2013 David Capello
|
// Copyright (c) 2001-2013, 2015 David Capello
|
||||||
//
|
//
|
||||||
// This file is released under the terms of the MIT license.
|
// This file is released under the terms of the MIT license.
|
||||||
// Read LICENSE.txt for more information.
|
// Read LICENSE.txt for more information.
|
||||||
@ -82,6 +82,19 @@ void remove_readonly_attr(const std::string& path)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Time get_modification_time(const std::string& path)
|
||||||
|
{
|
||||||
|
struct stat sts;
|
||||||
|
int result = stat(path.c_str(), &sts);
|
||||||
|
if (result != 0)
|
||||||
|
return Time();
|
||||||
|
|
||||||
|
struct tm* t = localtime(&sts.st_mtime);
|
||||||
|
return Time(
|
||||||
|
t->tm_year+1900, t->tm_mon+1, t->tm_mday,
|
||||||
|
t->tm_hour, t->tm_min, t->tm_sec);
|
||||||
|
}
|
||||||
|
|
||||||
void remove_directory(const std::string& path)
|
void remove_directory(const std::string& path)
|
||||||
{
|
{
|
||||||
int result = rmdir(path.c_str());
|
int result = rmdir(path.c_str());
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
// Aseprite Base Library
|
// Aseprite Base Library
|
||||||
// Copyright (c) 2001-2013 David Capello
|
// Copyright (c) 2001-2013, 2015 David Capello
|
||||||
//
|
//
|
||||||
// This file is released under the terms of the MIT license.
|
// This file is released under the terms of the MIT license.
|
||||||
// Read LICENSE.txt for more information.
|
// Read LICENSE.txt for more information.
|
||||||
@ -12,6 +12,7 @@
|
|||||||
#include "base/path.h"
|
#include "base/path.h"
|
||||||
#include "base/string.h"
|
#include "base/string.h"
|
||||||
#include "base/win32_exception.h"
|
#include "base/win32_exception.h"
|
||||||
|
#include "base/time.h"
|
||||||
|
|
||||||
namespace base {
|
namespace base {
|
||||||
|
|
||||||
@ -68,6 +69,24 @@ void remove_readonly_attr(const std::string& path)
|
|||||||
::SetFileAttributes(fn.c_str(), attr & ~FILE_ATTRIBUTE_READONLY);
|
::SetFileAttributes(fn.c_str(), attr & ~FILE_ATTRIBUTE_READONLY);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Time get_modification_time(const std::string& path)
|
||||||
|
{
|
||||||
|
WIN32_FILE_ATTRIBUTE_DATA data;
|
||||||
|
ZeroMemory(&data, sizeof(data));
|
||||||
|
|
||||||
|
std::wstring fn = from_utf8(path);
|
||||||
|
if (!GetFileAttributesEx(fn.c_str(), GetFileExInfoStandard, (LPVOID)&data))
|
||||||
|
return Time();
|
||||||
|
|
||||||
|
SYSTEMTIME utc, local;
|
||||||
|
FileTimeToSystemTime(&data.ftLastWriteTime, &utc);
|
||||||
|
SystemTimeToTzSpecificLocalTime(NULL, &utc, &local);
|
||||||
|
|
||||||
|
return Time(
|
||||||
|
local.wYear, local.wMonth, local.wDay,
|
||||||
|
local.wHour, local.wMinute, local.wSecond);
|
||||||
|
}
|
||||||
|
|
||||||
void make_directory(const std::string& path)
|
void make_directory(const std::string& path)
|
||||||
{
|
{
|
||||||
BOOL result = ::CreateDirectory(from_utf8(path).c_str(), NULL);
|
BOOL result = ::CreateDirectory(from_utf8(path).c_str(), NULL);
|
||||||
|
40
src/base/time.cpp
Normal file
40
src/base/time.cpp
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
// Aseprite Base Library
|
||||||
|
// Copyright (c) 2001-2013, 2015 David Capello
|
||||||
|
//
|
||||||
|
// This file is released under the terms of the MIT license.
|
||||||
|
// Read LICENSE.txt for more information.
|
||||||
|
|
||||||
|
#ifdef HAVE_CONFIG_H
|
||||||
|
#include "config.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include "base/time.h"
|
||||||
|
|
||||||
|
#if _WIN32
|
||||||
|
#include <windows.h>
|
||||||
|
#else
|
||||||
|
#include <ctime>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
namespace base {
|
||||||
|
|
||||||
|
Time current_time()
|
||||||
|
{
|
||||||
|
#if _WIN32
|
||||||
|
|
||||||
|
SYSTEMTIME st;
|
||||||
|
GetLocalTime(&st);
|
||||||
|
return Time(st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond);
|
||||||
|
|
||||||
|
#else
|
||||||
|
|
||||||
|
std::time_t now = std::time(nullptr);
|
||||||
|
struct tm* t = std::time(nullptr);
|
||||||
|
return Time(
|
||||||
|
t->tm_year+1900, t->tm_mon+1, t->tm_mday,
|
||||||
|
t->tm_hour, t->tm_min, t->tm_sec);
|
||||||
|
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace base
|
53
src/base/time.h
Normal file
53
src/base/time.h
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
// Aseprite Base Library
|
||||||
|
// Copyright (c) 2001-2013, 2015 David Capello
|
||||||
|
//
|
||||||
|
// This file is released under the terms of the MIT license.
|
||||||
|
// Read LICENSE.txt for more information.
|
||||||
|
|
||||||
|
#ifndef BASE_TIME_H_INCLUDED
|
||||||
|
#define BASE_TIME_H_INCLUDED
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
namespace base {
|
||||||
|
|
||||||
|
class Time {
|
||||||
|
public:
|
||||||
|
int year, month, day;
|
||||||
|
int hour, minute, second;
|
||||||
|
|
||||||
|
Time(int year = 0, int month = 0, int day = 0,
|
||||||
|
int hour = 0, int minute = 0, int second = 0)
|
||||||
|
: year(year), month(month), day(day)
|
||||||
|
, hour(hour), minute(minute), second(second) {
|
||||||
|
}
|
||||||
|
|
||||||
|
bool valid() const {
|
||||||
|
return (year != 0 && month != 0 && day != 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void dateOnly() {
|
||||||
|
hour = minute = second = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool operator==(const Time& other) {
|
||||||
|
return
|
||||||
|
year == other.year &&
|
||||||
|
month == other.month &&
|
||||||
|
day == other.day &&
|
||||||
|
hour == other.hour &&
|
||||||
|
minute == other.minute &&
|
||||||
|
second == other.second;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool operator!=(const Time& other) {
|
||||||
|
return !operator==(other);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
Time current_time();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
Loading…
x
Reference in New Issue
Block a user