Messagelib

sendlaterdialog.cpp
1 /*
2  SPDX-FileCopyrightText: 2013-2021 Laurent Montel <[email protected]>
3 
4  SPDX-License-Identifier: GPL-2.0-or-later
5 */
6 
7 #include "sendlaterdialog.h"
8 #include "sendlaterinfo.h"
9 #include "sendlatertimedatewidget_p.h"
10 #include "ui_sendlaterwidget.h"
11 
12 #include <KLocalizedString>
13 #include <KSeparator>
14 #include <QIcon>
15 
16 #include <QCheckBox>
17 #include <QDialogButtonBox>
18 #include <QPushButton>
19 #include <QVBoxLayout>
20 
21 using namespace MessageComposer;
22 
23 SendLaterDialog::SendLaterDialog(SendLaterInfo *info, QWidget *parent)
24  : QDialog(parent)
25  , mSendLaterWidget(new Ui::SendLaterWidget)
26  , mInfo(info)
27 {
28  setWindowTitle(i18nc("@title:window", "Send Later"));
29  setWindowIcon(QIcon::fromTheme(QStringLiteral("kmail")));
30 
31  auto mainLayout = new QVBoxLayout(this);
32 
33  auto sendLaterWidget = new QWidget(this);
34  mSendLaterWidget->setupUi(sendLaterWidget);
35 
36  auto w = new QWidget(this);
37  auto lay = new QVBoxLayout(w);
38  lay->setContentsMargins({});
39 
41  mOkButton = buttonBox->button(QDialogButtonBox::Ok);
42  mOkButton->setObjectName(QStringLiteral("okbutton"));
43  mOkButton->setDefault(true);
44  mOkButton->setShortcut(Qt::CTRL | Qt::Key_Return);
45  connect(buttonBox, &QDialogButtonBox::rejected, this, &SendLaterDialog::reject);
46 
47  if (!info) {
48  mOkButton->setText(i18n("Send Later"));
49  mDelay = new QCheckBox(i18n("Delay"));
50  mDelay->setChecked(false);
51  slotDelay(false);
52  connect(mDelay, &QCheckBox::clicked, this, &SendLaterDialog::slotDelay);
53  lay->addWidget(mDelay);
54  }
55 
56  connect(mOkButton, &QPushButton::clicked, this, &SendLaterDialog::slotOkClicked);
57 
58  lay->addWidget(sendLaterWidget);
59 
61  t = t.addSecs(60 * 60);
62 
63  mSendLaterWidget->mDateTime->setDateTime(t);
64  connect(mSendLaterWidget->mRecurrence, &QCheckBox::clicked, this, &SendLaterDialog::slotRecurrenceClicked);
65  const QStringList unitsList = {i18n("Days"), i18n("Weeks"), i18n("Months"), i18n("Years")};
66  mSendLaterWidget->mRecurrenceComboBox->addItems(unitsList);
67  connect(mSendLaterWidget->mDateTime, &SendLaterTimeDateWidget::dateChanged, this, &SendLaterDialog::slotDateChanged);
68 
69  lay->addWidget(new KSeparator);
70 
71  mainLayout->addWidget(w);
72  mainLayout->addWidget(buttonBox);
73  slotRecurrenceClicked(false);
74  if (info) {
75  load(info);
76  }
77  resize(180, 120);
78 }
79 
80 SendLaterDialog::~SendLaterDialog()
81 {
82  delete mSendLaterWidget;
83 }
84 
85 void SendLaterDialog::slotDateChanged(const QString &date)
86 {
87  mOkButton->setEnabled(!date.isEmpty());
88 }
89 
90 void SendLaterDialog::slotRecurrenceClicked(bool clicked)
91 {
92  mSendLaterWidget->mRecurrenceValue->setEnabled(clicked);
93  mSendLaterWidget->mRecurrenceComboBox->setEnabled(clicked);
94 }
95 
96 void SendLaterDialog::load(SendLaterInfo *info)
97 {
98  mSendLaterWidget->mDateTime->setDateTime(info->dateTime());
99  const bool recurrence = info->isRecurrence();
100  mSendLaterWidget->mRecurrence->setChecked(recurrence);
101  slotRecurrenceClicked(recurrence);
102  mSendLaterWidget->mRecurrenceValue->setValue(info->recurrenceEachValue());
103  mSendLaterWidget->mRecurrenceComboBox->setCurrentIndex(static_cast<int>(info->recurrenceUnit()));
104 }
105 
106 SendLaterInfo *SendLaterDialog::info()
107 {
108  if (!mInfo) {
109  mInfo = new SendLaterInfo();
110  }
111  mInfo->setRecurrence(mSendLaterWidget->mRecurrence->isChecked());
112  mInfo->setRecurrenceEachValue(mSendLaterWidget->mRecurrenceValue->value());
113  mInfo->setRecurrenceUnit(static_cast<SendLaterInfo::RecurrenceUnit>(mSendLaterWidget->mRecurrenceComboBox->currentIndex()));
114  if (mSendDateTime.isValid()) {
115  mInfo->setDateTime(mSendDateTime);
116  } else {
117  mInfo->setDateTime(mSendLaterWidget->mDateTime->dateTime());
118  }
119  return mInfo;
120 }
121 
122 SendLaterDialog::SendLaterAction SendLaterDialog::action() const
123 {
124  return mAction;
125 }
126 
127 void SendLaterDialog::slotOkClicked()
128 {
129  if (!mDelay || mDelay->isChecked()) {
130  mSendDateTime = mSendLaterWidget->mDateTime->dateTime();
131  mAction = SendDeliveryAtTime;
132  } else {
133  mAction = PutInOutbox;
134  }
135  accept();
136 }
137 
138 void SendLaterDialog::slotDelay(bool delayEnabled)
139 {
140  mSendLaterWidget->mLabel->setEnabled(delayEnabled);
141  mSendLaterWidget->mDateTime->setEnabled(delayEnabled);
142  mSendLaterWidget->mRecurrence->setEnabled(delayEnabled);
143  mSendLaterWidget->mRecurrenceValue->setEnabled(delayEnabled && mSendLaterWidget->mRecurrence->isChecked());
144  mSendLaterWidget->mRecurrenceComboBox->setEnabled(delayEnabled && mSendLaterWidget->mRecurrence->isChecked());
145 }
QAction * load(const QObject *recvr, const char *slot, QObject *parent)
QString i18nc(const char *context, const char *text, const TYPE &arg...)
bool isEmpty() const const
void clicked(bool checked)
Send later information.
Definition: sendlaterinfo.h:17
QString i18n(const char *text, const TYPE &arg...)
QDateTime currentDateTime()
Simple interface that both EncryptJob and SignEncryptJob implement so the composer can extract some e...
QIcon fromTheme(const QString &name)
QDateTime addSecs(qint64 s) const const
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
Key_Return
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Mon Dec 6 2021 23:04:58 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.