Skip to content

Commit

Permalink
feat: support for import ics file
Browse files Browse the repository at this point in the history
支持ics文件的导入功能

Log:
Issues: linuxdeepin/developer-center#5018
  • Loading branch information
myml committed Jul 25, 2023
1 parent a5595bf commit e41b24d
Show file tree
Hide file tree
Showing 14 changed files with 268 additions and 16 deletions.
51 changes: 51 additions & 0 deletions calendar-client/src/customWidget/jobtypelistview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
#include <QToolTip>
#include <QPainter>
#include <QHeaderView>
#include <QTimer>
#include <DSpinner>

//Qt::UserRole + 1,会影响item的高度
static const int RoleJobTypeInfo = Qt::UserRole + 2;
Expand Down Expand Up @@ -178,6 +180,55 @@ void JobTypeListView::slotAddScheduleType()
}
}

void JobTypeListView::slotImportScheduleType()
{
AccountItem::Ptr account = gAccountManager->getAccountItemByAccountId(m_account_id);
if (!account)
return;

ScheduleTypeEditDlg dialog(ScheduleTypeEditDlg::DialogImportType, this);
dialog.setAccount(account);
// 按保存键退出则触发保存数据
if (QDialog::Accepted == dialog.exec())
{
// 显示等待对话框
auto waitDialog = new DDialog(this);
// 延迟销毁
waitDialog->deleteLater();
waitDialog->setFixedSize(400, 280);
auto progress = new DSpinner(waitDialog);
progress->setFixedSize(100, 100);
progress->start();
waitDialog->addContent(progress, Qt::AlignCenter);
waitDialog->show();
DScheduleType::Ptr type(new DScheduleType(dialog.newJsonType()));
auto icsFile = dialog.getIcsFile();
QEventLoop event;
QString typeID;
// 创建日程类型
account->createJobType(type, [&event, &typeID](CallMessge msg)
{
if(msg.code == 0) {
// 记录创建的类型ID
typeID = msg.msg.toString();
}
// 延迟一秒后再退出
// 一来可以避免导入小文件时,进度过快引起等待对话框闪烁
// 二来在导入大文件时堵塞dbus调用,延迟可以避免客户端在日程类型更新信号执行的槽函数被堵塞。
QTimer::singleShot(1000, &event, &QEventLoop::quit); });
// 等待日程创建完毕
event.exec();
// 导入ics文件
if (!typeID.isEmpty())
{
account->importSchedule(icsFile, typeID, true, [&event](CallMessge msg)
{ event.quit(); });
}
// 等待导入完毕
event.exec();
}
}

bool JobTypeListView::canAdd()
{
AccountItem::Ptr account = gAccountManager->getAccountItemByAccountId(m_account_id);
Expand Down
2 changes: 2 additions & 0 deletions calendar-client/src/customWidget/jobtypelistview.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ public slots:
void slotUpdateJobType();
void slotDeleteJobType();
void slotAddScheduleType();
// 导入ics文件
void slotImportScheduleType();

bool updateJobType();
void updateCalendarAccount(QString account_id);
Expand Down
10 changes: 10 additions & 0 deletions calendar-client/src/dataManage/accountitem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -453,3 +453,13 @@ void AccountItem::slotSearchUpdata()
}
}

void AccountItem::importSchedule(QString icsFilePath, QString typeID, bool cleanExists, CallbackFunc func)
{
m_dbusRequest->setCallbackFunc(func);
m_dbusRequest->importSchedule(icsFilePath, typeID, cleanExists);
}

void AccountItem::exportSchedule(QString icsFilePath, QString typeID)
{
m_dbusRequest->exportSchedule(icsFilePath, typeID);
}
5 changes: 4 additions & 1 deletion calendar-client/src/dataManage/accountitem.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,10 @@ class AccountItem : public QObject
//对外请求接口,同步
QString querySchedulesByExternal(const QString &key, const QDateTime &start, const QDateTime &end);
bool querySchedulesByExternal(const QString &key, const QDateTime &start, const QDateTime &end, QMap<QDate, DSchedule::List>& out);

// 导入日程
void importSchedule(QString icsFilePath, QString TypeID, bool cleanExists, CallbackFunc func);
// 导出日程
void exportSchedule(QString icsFilePath, QString TypeID);
signals:
void signalAccountDataUpdate();
void signalScheduleUpdate();
Expand Down
10 changes: 10 additions & 0 deletions calendar-client/src/dbus/dbusaccountrequest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -351,3 +351,13 @@ void DbusAccountRequest::onPropertiesChanged(const QString &, const QVariantMap
}
}
}

void DbusAccountRequest::importSchedule(QString icsFilePath, QString typeID, bool cleanExists)
{
asyncCall("importSchedule", QVariant(icsFilePath), QVariant(typeID), QVariant(cleanExists));
}

void DbusAccountRequest::exportSchedule(QString icsFilePath, QString typeID)
{
asyncCall("exportSchedule", QVariant(icsFilePath), QVariant(typeID));
}
3 changes: 3 additions & 0 deletions calendar-client/src/dbus/dbusaccountrequest.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ class DbusAccountRequest : public DbusRequestBase

//获取最后一次同步时间
QString getDtLastUpdate();

void importSchedule(QString icsFilePath, QString TypeID, bool cleanExists);
void exportSchedule(QString icsFilePath, QString TypeID);
signals:
void signalGetAccountInfoFinish(DAccount::Ptr);
void signalGetScheduleTypeListFinish(DScheduleType::List);
Expand Down
73 changes: 67 additions & 6 deletions calendar-client/src/dialog/scheduletypeeditdlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,23 @@ ScheduleTypeEditDlg::ScheduleTypeEditDlg(const DScheduleType &jobTypeOld, QWidge
init();
}

ScheduleTypeEditDlg::ScheduleTypeEditDlg(const DialogType &type, QWidget *parent)
: DDialog(parent)
{
switch (type)
{
case DialogImportType:
m_title = tr("Import ics file");
m_dialogType = DialogImportType;
break;
default:
m_title = tr("New event type");
m_dialogType = DialogNewType;
break;
}
init();
}

DScheduleType ScheduleTypeEditDlg::newJsonType()
{
m_jobTypeNew.setTypeColor(*m_colorSeletor->getSelectedColorInfo());
Expand Down Expand Up @@ -94,6 +111,9 @@ void ScheduleTypeEditDlg::init()
connect(m_lineEdit, &DLineEdit::textChanged, this, &ScheduleTypeEditDlg::slotEditTextChanged);
connect(m_lineEdit, &DLineEdit::focusChanged, this, &ScheduleTypeEditDlg::slotFocusChanged);
connect(m_lineEdit, &DLineEdit::editingFinished, this, &ScheduleTypeEditDlg::slotEditingFinished);
// 在编辑框变动时,设置确认按钮状态
connect(m_lineEdit, &DLineEdit::textChanged, this, &ScheduleTypeEditDlg::slotCheckConfirmBtn);
connect(m_fileEdit, &DLineEdit::textChanged, this, &ScheduleTypeEditDlg::slotCheckConfirmBtn);
}

void ScheduleTypeEditDlg::initView()
Expand All @@ -113,10 +133,11 @@ void ScheduleTypeEditDlg::initView()
maintlayout->setMargin(0);
maintlayout->setSpacing(10);

auto nameWidth = m_dialogType == DialogImportType ? 50 : 42;
QHBoxLayout *eLayout = new QHBoxLayout;
m_eName = new QLabel(tr("Name:"));
m_eName->setToolTip(m_eName->text());
m_eName->setFixedWidth(42);
m_eName->setFixedWidth(nameWidth);

m_lineEdit = new DLineEdit();
m_lineEdit->setClearButtonEnabled(false); //不显示按钮
Expand All @@ -128,20 +149,37 @@ void ScheduleTypeEditDlg::initView()
m_cName->setToolTip(m_cName->text());
m_strLabelName = m_eName->text();
m_strLabelColor = m_cName->text();
m_cName->setFixedWidth(42);
m_cName->setFixedWidth(nameWidth);
m_colorSeletor = new ColorSeletorWidget();

cLayout->addWidget(m_cName);
cLayout->addWidget(m_colorSeletor);
cLayout->addStretch(1);

QHBoxLayout *fLayout = new QHBoxLayout;
m_fName = new QLabel(tr("<a href='https://wikipedia.org/wiki/ICalendar'>ICS</a> File:"));
m_fName->setOpenExternalLinks(true);
m_fName->setToolTip(m_fName->text());
m_fName->setFixedWidth(nameWidth);

m_fileEdit = new DFileChooserEdit;
fLayout->addWidget(m_fName);
fLayout->addWidget(m_fileEdit, 1);

maintlayout->addLayout(eLayout);
maintlayout->addLayout(cLayout);
if (m_dialogType == DialogImportType)
{
maintlayout->addLayout(fLayout);
setFixedSize(QSize(400, 300));
}

eLayout->setSpacing(10);
eLayout->setMargin(0);
cLayout->setSpacing(10);
cLayout->setMargin(0);
fLayout->setSpacing(10);
fLayout->setMargin(0);
maintlayout->setSpacing(20);
maintlayout->setContentsMargins(0, 10, 0, 0);

Expand Down Expand Up @@ -196,22 +234,19 @@ void ScheduleTypeEditDlg::slotEditTextChanged(const QString &strName)
//内容清空时,消除警告色和提示信息
m_lineEdit->setAlert(false);
m_lineEdit->hideAlertMessage();
this->getButton(1)->setEnabled(false);

return;
}
if (tStitlename.trimmed().isEmpty()) {
//名称为全空格,返回
m_lineEdit->showAlertMessage(tr("The name can not only contain whitespaces"));
m_lineEdit->setAlert(true);
this->getButton(1)->setEnabled(false);
return;
}
m_jobTypeNew.setDisplayName(tStitlename);

m_lineEdit->setAlert(false);
m_lineEdit->hideAlertMessage();
this->getButton(1)->setEnabled(true);
return;
}
void ScheduleTypeEditDlg::changeEvent(QEvent *e) {
Expand Down Expand Up @@ -264,6 +299,32 @@ void ScheduleTypeEditDlg::slotEditingFinished()
//名称为空,返回
m_lineEdit->showAlertMessage(tr("Enter a name please"));
m_lineEdit->setAlert(true);
this->getButton(1)->setEnabled(false);
}
}

// 获取ICS文件路径
QString ScheduleTypeEditDlg::getIcsFile()
{
return m_fileEdit->text();
}

// 检查并设置‘确认按钮’的状态
void ScheduleTypeEditDlg::slotCheckConfirmBtn()
{
auto confirmBtn = this->getButton(1);

if (m_lineEdit->text().isEmpty() || m_lineEdit->isAlert())
{
confirmBtn->setEnabled(false);
return;
}
if (m_dialogType == DialogType::DialogImportType)
{
if (m_fileEdit->text().isEmpty() || m_fileEdit->isAlert())
{
confirmBtn->setEnabled(false);
return;
}
}
confirmBtn->setEnabled(true);
}
28 changes: 19 additions & 9 deletions calendar-client/src/dialog/scheduletypeeditdlg.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,30 @@

#include <DDialog>
#include <DLineEdit>
#include <DFileChooserEdit>

DWIDGET_USE_NAMESPACE

class ScheduleTypeEditDlg : public DDialog
{
Q_OBJECT
public:
enum DialogType
{ // 对话框类型
DialogNewType, // 新建日程类型
DialogEditType, // 编辑日程类型
DialogImportType // 导入日程类型
};
//新增
explicit ScheduleTypeEditDlg(QWidget *parent = nullptr);
//iJobTypeNo==0 ? 新增 or 修改
explicit ScheduleTypeEditDlg(const DScheduleType &jobTypeOld, QWidget *parent = nullptr);
// 新增 or 导入
explicit ScheduleTypeEditDlg(const DialogType &type, QWidget *parent = nullptr);

DScheduleType newJsonType();

void setAccount(AccountItem::Ptr account);

private:
enum DialogType { //对话框类型
DialogNewType, //新建日程类型
DialogEditType //编辑日程类型
};

signals:
/**
* @brief refreshInfo:刷新信息信号
Expand Down Expand Up @@ -65,6 +67,12 @@ public slots:

void setLabelText();

// 获取ICS文件
QString getIcsFile();

// 检查确认按钮是否需要禁用
void slotCheckConfirmBtn();

private:
void init();
void initView();
Expand All @@ -75,14 +83,16 @@ public slots:
DScheduleType m_jobTypeOld; //被修改的日程类型
DScheduleType m_jobTypeNew; //修改后的日程类型
QString m_title = ""; //弹窗名
DLineEdit *m_lineEdit = nullptr; //编辑器
QLabel *m_titleLabel = nullptr; //弹窗名控件
DLineEdit *m_lineEdit = nullptr; // 类型名编辑框
DFileChooserEdit *m_fileEdit = nullptr; // ics文件编辑框
QLabel *m_titleLabel = nullptr; //弹窗名控件
ColorSeletorWidget *m_colorSeletor = nullptr; //颜色选择器
DialogType m_dialogType;
QString m_typeText; //输入框上一次输入后的文本

QLabel * m_eName = Q_NULLPTR;
QLabel * m_cName = Q_NULLPTR;
QLabel * m_fName = Q_NULLPTR;


QString m_strLabelName;
Expand Down
Loading

0 comments on commit e41b24d

Please sign in to comment.