File indexing completed on 2024-11-24 04:39:29

0001 /*
0002     This file is part of Contact Editor.
0003 
0004     SPDX-FileCopyrightText: 2016-2024 Laurent Montel <montel@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-or-later
0007 */
0008 
0009 #include "businesseditorwidget.h"
0010 
0011 #include <KLineEdit>
0012 #include <KLocalizedString>
0013 #include <QGridLayout>
0014 #include <QLabel>
0015 
0016 #include "../widgets/imagewidget.h"
0017 #include "freebusyeditwidget.h"
0018 
0019 #include <KContacts/Addressee>
0020 
0021 using namespace Akonadi;
0022 
0023 BusinessEditorWidget::BusinessEditorWidget(QWidget *parent)
0024     : QWidget(parent)
0025     , mOrganizationWidget(new KLineEdit(this))
0026     , mProfessionWidget(new KLineEdit(this))
0027     , mTitleWidget(new KLineEdit(this))
0028     , mDepartmentWidget(new KLineEdit(this))
0029     , mOfficeWidget(new KLineEdit(this))
0030     , mManagerWidget(new KLineEdit(this))
0031     , mAssistantWidget(new KLineEdit(this))
0032     , mFreeBusyWidget(new Akonadi::FreeBusyEditWidget(this))
0033     , mLogoWidget(new ImageWidget(ImageWidget::Logo, this))
0034 {
0035     auto topLayout = new QHBoxLayout(this);
0036 
0037     auto logoLayout = new QVBoxLayout;
0038     topLayout->addLayout(logoLayout);
0039 
0040     // setup general group box
0041     logoLayout->addWidget(mLogoWidget, Qt::AlignTop);
0042     logoLayout->addStretch(0);
0043 
0044     auto generalLayout = new QGridLayout;
0045     topLayout->addLayout(generalLayout);
0046 
0047     auto label = new QLabel(i18nc("@label The organization of a contact", "Organization:"), this);
0048     generalLayout->addWidget(label, 0, 0);
0049 
0050     mOrganizationWidget->setTrapReturnKey(true);
0051     mOrganizationWidget->setPlaceholderText(i18n("Add organization's name"));
0052     label->setBuddy(mOrganizationWidget);
0053     generalLayout->addWidget(mOrganizationWidget, 1, 0);
0054 
0055     label = new QLabel(i18nc("@label The profession of a contact", "Profession:"), this);
0056     generalLayout->addWidget(label, 0, 1);
0057 
0058     mProfessionWidget->setPlaceholderText(i18n("Add profession"));
0059     mProfessionWidget->setTrapReturnKey(true);
0060     label->setBuddy(mProfessionWidget);
0061     generalLayout->addWidget(mProfessionWidget, 1, 1);
0062 
0063     label = new QLabel(i18nc("@label The title of a contact", "Title:"), this);
0064     generalLayout->addWidget(label, 3, 0);
0065 
0066     mTitleWidget->setPlaceholderText(i18n("Add the title"));
0067     mTitleWidget->setTrapReturnKey(true);
0068     label->setBuddy(mTitleWidget);
0069     generalLayout->addWidget(mTitleWidget, 4, 0);
0070 
0071     label = new QLabel(i18nc("@label The department of a contact", "Department:"), this);
0072     generalLayout->addWidget(label, 3, 1);
0073 
0074     mDepartmentWidget->setPlaceholderText(i18n("Add the department"));
0075     mDepartmentWidget->setTrapReturnKey(true);
0076     label->setBuddy(mDepartmentWidget);
0077     generalLayout->addWidget(mDepartmentWidget, 4, 1);
0078 
0079     label = new QLabel(i18nc("@label The office of a contact", "Office:"), this);
0080     generalLayout->addWidget(label, 5, 0);
0081 
0082     mOfficeWidget->setTrapReturnKey(true);
0083     mOfficeWidget->setPlaceholderText(i18n("Add the office"));
0084 
0085     label->setBuddy(mOfficeWidget);
0086     generalLayout->addWidget(mOfficeWidget, 6, 0);
0087 
0088     label = new QLabel(i18nc("@label The manager's name of a contact", "Manager's name:"), this);
0089     generalLayout->addWidget(label, 5, 1);
0090 
0091     mManagerWidget->setPlaceholderText(i18n("Add manager's name"));
0092     mManagerWidget->setTrapReturnKey(true);
0093     label->setBuddy(mManagerWidget);
0094     generalLayout->addWidget(mManagerWidget, 6, 1);
0095 
0096     label = new QLabel(i18nc("@label The assistant's name of a contact", "Assistant's name:"), this);
0097     generalLayout->addWidget(label, 7, 0);
0098 
0099     mAssistantWidget->setPlaceholderText(i18n("Add assistant's name"));
0100     mAssistantWidget->setTrapReturnKey(true);
0101     label->setBuddy(mAssistantWidget);
0102     generalLayout->addWidget(mAssistantWidget, 8, 0);
0103 
0104     // setup groupware group box
0105     label = new QLabel(i18nc("@label The free/busy information of a contact", "Free/Busy:"));
0106     generalLayout->addWidget(label, 7, 1);
0107 
0108     label->setBuddy(mFreeBusyWidget);
0109     generalLayout->addWidget(mFreeBusyWidget, 8, 1);
0110     generalLayout->setRowStretch(9, 1);
0111     connect(mOrganizationWidget, &KLineEdit::textChanged, this, &BusinessEditorWidget::organizationChanged);
0112 }
0113 
0114 BusinessEditorWidget::~BusinessEditorWidget() = default;
0115 
0116 void BusinessEditorWidget::loadContact(const KContacts::Addressee &contact)
0117 {
0118     mLogoWidget->loadContact(contact);
0119     mOrganizationWidget->setText(contact.organization());
0120     mProfessionWidget->setText(contact.profession());
0121     mTitleWidget->setText(contact.title());
0122     mDepartmentWidget->setText(contact.department());
0123     mOfficeWidget->setText(contact.office());
0124     mManagerWidget->setText(contact.managersName());
0125     mAssistantWidget->setText(contact.assistantsName());
0126 
0127     // groupware group
0128     mFreeBusyWidget->loadContact(contact);
0129 }
0130 
0131 void BusinessEditorWidget::storeContact(KContacts::Addressee &contact)
0132 {
0133     // general group
0134     mLogoWidget->storeContact(contact);
0135     contact.setOrganization(mOrganizationWidget->text());
0136     contact.setProfession(mProfessionWidget->text().trimmed());
0137     contact.setTitle(mTitleWidget->text().trimmed());
0138     contact.setDepartment(mDepartmentWidget->text().trimmed());
0139     contact.setOffice(mOfficeWidget->text().trimmed());
0140     contact.setManagersName(mManagerWidget->text().trimmed());
0141     contact.setAssistantsName(mAssistantWidget->text().trimmed());
0142 
0143     // groupware group
0144     mFreeBusyWidget->storeContact(contact);
0145 }
0146 
0147 void BusinessEditorWidget::setReadOnly(bool readOnly)
0148 {
0149     mLogoWidget->setReadOnly(readOnly);
0150     mOrganizationWidget->setReadOnly(readOnly);
0151     mProfessionWidget->setReadOnly(readOnly);
0152     mTitleWidget->setReadOnly(readOnly);
0153     mDepartmentWidget->setReadOnly(readOnly);
0154     mOfficeWidget->setReadOnly(readOnly);
0155     mManagerWidget->setReadOnly(readOnly);
0156     mAssistantWidget->setReadOnly(readOnly);
0157 
0158     // widgets from groupware group
0159     mFreeBusyWidget->setReadOnly(readOnly);
0160 }
0161 
0162 #include "moc_businesseditorwidget.cpp"