Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: auto update holiday data #171

Merged
merged 1 commit into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions calendar-common/src/units.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
// SPDX-License-Identifier: LGPL-3.0-or-later

#include "units.h"
#include <QDir>
#include <QProcess>

#include <QTimeZone>
#include <QStandardPaths>
#include <QLocale>
#include <QSharedPointer>

QString dtToString(const QDateTime &dt)
{
Expand Down Expand Up @@ -58,6 +61,24 @@ QString getHomeConfigPath()
return configPath;
}

QDir getAppConfigDir()
{
return QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation);
}

QDir getAppCacheDir()
{
return QStandardPaths::writableLocation(QStandardPaths::CacheLocation);
}

QSharedPointer<QProcess> DownloadFile(QString url, QString filename)
{
auto process = QSharedPointer<QProcess>::create();
process->setEnvironment({"LANGUAGE=en"});
process->start("wget", { "-c", "-N", "-O", filename, url });
return process;
}

bool withinTimeFrame(const QDate &date)
{
return date.isValid() && (date.year() >= 1900 && date.year() <=2100);
Expand Down
6 changes: 6 additions & 0 deletions calendar-common/src/units.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
#include <QString>
#include <QDateTime>
#include <QMap>
#include <QDir>
#include <QProcess>
#include <QSharedPointer>

const QString serviceBaseName = "com.deepin.dataserver.Calendar";
const QString serviceBasePath = "/com/deepin/dataserver/Calendar";
Expand Down Expand Up @@ -38,6 +41,9 @@ bool isChineseEnv();

//获取家配置目录
QString getHomeConfigPath();
QDir getAppConfigDir();
QDir getAppCacheDir();
QSharedPointer<QProcess> DownloadFile(QString url, QString filename);

//时间转换
QDateTime dtConvert(const QDateTime &datetime);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ DAccountManageModule::DAccountManageModule(QObject *parent)
, m_syncFileManage(new SyncFileManage())
, m_accountManagerDB(new DAccountManagerDataBase)
, m_reginFormatConfig(DTK_CORE_NAMESPACE::DConfig::createGeneric("org.deepin.region-format", QString(), this))
, m_settings(QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation) + "/config.ini", QSettings::IniFormat)
, m_settings( getAppConfigDir().filePath( "config.ini"), QSettings::IniFormat)
{
if (m_reginFormatConfig->isValid()) {
connect(m_reginFormatConfig,
Expand Down Expand Up @@ -334,7 +334,7 @@ void DAccountManageModule::setUidSwitchStatus(const DAccount::Ptr &account)
DCalendarGeneralSettings::Ptr DAccountManageModule::getGeneralSettings()
{
auto cg = m_accountManagerDB->getCalendarGeneralSettings();
if(getFirstDayOfWeekSource()==DCalendarGeneralSettings::Source_System){
if (getFirstDayOfWeekSource() == DCalendarGeneralSettings::Source_System) {
bool ok;
auto dayofWeek = Qt::DayOfWeek(m_reginFormatConfig->value(firstDayOfWeek_key).toInt(&ok));
if (ok) {
Expand All @@ -343,7 +343,7 @@ DCalendarGeneralSettings::Ptr DAccountManageModule::getGeneralSettings()
qWarning() << "Unable to get first day of week from control center config file";
}
}
if(getTimeFormatTypeSource()==DCalendarGeneralSettings::Source_System){
if (getTimeFormatTypeSource() == DCalendarGeneralSettings::Source_System) {
auto shortTimeFormat = m_reginFormatConfig->value(shortTimeFormat_key).toString();
if (shortTimeFormat.isEmpty()) {
qWarning() << "Unable to short time format from control center config file";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ class DAccountManageModule : public QObject
void updateUIdAccount(const DAccount::Ptr &oldAccount, const DAccount::Ptr &uidAccount);
//获取设置开关状态
void setUidSwitchStatus(const DAccount::Ptr &account);

// 获取通用配置
DCalendarGeneralSettings::Ptr getGeneralSettings();

// 保存通用配置
void setGeneralSettings(const DCalendarGeneralSettings::Ptr &cgSet);

signals:
Expand Down
39 changes: 38 additions & 1 deletion calendar-service/src/dbmanager/dhuanglidatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,38 @@
#include "dhuanglidatabase.h"

#include "commondef.h"
#include "units.h"
#include <QProcess>
#include <QTimer>

#include <QDebug>
#include <QSqlError>
#include <QFile>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QRandomGenerator>

const QString HolidayDir = ":/holiday-cn";
const QString HolidayUpdateURLPrefix ="https://cdn-nu-common.uniontech.com/deepin-calendar";
const QString HolidayUpdateDateSetKey = "festivalUpdateDate";

DHuangLiDataBase::DHuangLiDataBase(QObject *parent)
: DDataBase(parent)
, m_settings(getAppConfigDir().filePath("config.ini"), QSettings::IniFormat)
{
qDebug()<< getAppConfigDir().filePath("config.ini");
QString huangliPath = QStandardPaths::locate(QStandardPaths::GenericDataLocation,
QString("dde-calendar/data/huangli.db"),
QStandardPaths::LocateFile);
setDBPath(huangliPath);
qCDebug(ServiceLogger) << "huangli database" << huangliPath;
setConnectionName("HuangLi");
dbOpen();
// 延迟一段时间后,从网络更新节假日
QTimer::singleShot(QRandomGenerator::global()->bounded(1000, 9999),
this,
&DHuangLiDataBase::updateFestivalList);
}

// readJSON 会读取一个JSON文件,如果 cache 为 true,则会缓存对象,以供下次使用
Expand All @@ -46,13 +58,38 @@ QJsonDocument DHuangLiDataBase::readJSON(QString filename, bool cache)
return readJSONCache.value(filename);
}

void DHuangLiDataBase::updateFestivalList()
{
auto now = QDateTime::currentDateTime();
auto festivalUpdateDate = now.toString("yyyy-MM-dd");
// 每天更新一次
if (m_settings.value(HolidayUpdateDateSetKey, "") == festivalUpdateDate) {
return;
}
m_settings.setValue(HolidayUpdateDateSetKey, festivalUpdateDate);
// 获取今年和明年的节假日数据
for (auto i = 0; i < 2; i++) {
auto year = now.date().year() + i;
auto filename = getAppCacheDir().filePath(QString("%1.json").arg(year));
auto url = QString("%1/%2.json").arg(HolidayUpdateURLPrefix).arg(year);
auto process = DownloadFile(url, filename);
qCDebug(ServiceLogger) << "Download File" << url << filename;
connect(process.get(), &QProcess::readyReadStandardError, [process]() {
qCDebug(ServiceLogger) << "Download Error: " << process->readAllStandardError();
});
}
}

// queryFestivalList 查询指定月份的节假日列表
QJsonArray DHuangLiDataBase::queryFestivalList(quint32 year, quint8 month)
{
qCDebug(ServiceLogger) << "query festival list"
<< "year" << year << "month" << month;
QJsonArray dataset;
auto filename = QString("%1/%2.json").arg(HolidayDir).arg(year);
auto filename = getAppCacheDir().filePath(QString("%1.json").arg(year));
if (!QFile(filename).exists()) {
filename = QString("%1/%2.json").arg(HolidayDir).arg(year);
}
qCDebug(ServiceLogger) << "festival file name" << filename;
auto doc = readJSON(filename, true);
for (auto val : doc.object().value("days").toArray()) {
Expand Down
3 changes: 3 additions & 0 deletions calendar-service/src/dbmanager/dhuanglidatabase.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "lunarandfestival.h"
#include <QJsonArray>
#include <QJsonDocument>
#include <QSettings>

class DHuangLiDataBase : public DDataBase
{
Expand All @@ -23,6 +24,8 @@ class DHuangLiDataBase : public DDataBase
private:
QJsonDocument readJSON(QString filename, bool cache);
QHash<QString, QJsonDocument> readJSONCache;
void updateFestivalList();
QSettings m_settings;
protected:
//创建数据库
void createDB() override;
Expand Down
Loading