File indexing completed on 2024-04-28 05:11:30
0001 /* 0002 SPDX-FileCopyrightText: 2003 Cornelius Schumacher <schumacher@kde.org> 0003 SPDX-FileCopyrightText: 2005 Reinhold Kainhofer <reinhold@kainhofer.com> 0004 SPDX-FileCopyrightText: 2005 Rafal Rzepecki <divide@users.sourceforge.net> 0005 SPDX-FileCopyrightText: 2010 Bertjan Broeksema <broeksema@kde.org> 0006 SPDX-FileCopyrightText: 2010 Klaralvdalens Datakonsult AB, a KDAB Group company <info@kdab.net> 0007 0008 SPDX-License-Identifier: GPL-2.0-or-later WITH Qt-Commercial-exception-1.0 0009 */ 0010 0011 #include "attachmenteditdialog.h" 0012 #include "attachmenticonview.h" 0013 #include "config-enterprise.h" 0014 #include "ui_attachmenteditdialog.h" 0015 #include <KIO/StoredTransferJob> 0016 #include <KJobWidgets> 0017 #include <KLocalizedString> 0018 #include <QDialogButtonBox> 0019 #include <QLocale> 0020 #include <QMimeDatabase> 0021 #include <QPushButton> 0022 0023 using namespace IncidenceEditorNG; 0024 0025 AttachmentEditDialog::AttachmentEditDialog(AttachmentIconItem *item, QWidget *parent, bool modal) 0026 : QDialog(parent) 0027 , 0028 #if KDEPIM_ENTERPRISE_BUILD 0029 mAttachment(KCalendarCore::Attachment('\0')) 0030 // use the non-uri constructor 0031 // as we want inline by default 0032 #else 0033 mAttachment(KCalendarCore::Attachment(QString())) 0034 #endif 0035 , mItem(item) 0036 , mUi(new Ui::AttachmentEditDialog) 0037 { 0038 setWindowTitle(i18nc("@title:window", "Edit Attachment")); 0039 QMimeDatabase db; 0040 mMimeType = db.mimeTypeForName(item->mimeType()); 0041 auto page = new QWidget(this); 0042 auto mainLayout = new QVBoxLayout(this); 0043 auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this); 0044 mOkButton = buttonBox->button(QDialogButtonBox::Ok); 0045 mOkButton->setDefault(true); 0046 mOkButton->setShortcut(Qt::CTRL | Qt::Key_Return); 0047 connect(buttonBox, &QDialogButtonBox::accepted, this, &AttachmentEditDialog::accept); 0048 connect(buttonBox, &QDialogButtonBox::rejected, this, &AttachmentEditDialog::reject); 0049 mainLayout->addWidget(page); 0050 mainLayout->addWidget(buttonBox); 0051 0052 mUi->setupUi(page); 0053 mUi->mLabelEdit->setText(item->label().isEmpty() ? item->uri() : item->label()); 0054 mUi->mIcon->setPixmap(item->icon()); 0055 mUi->mInlineCheck->setChecked(item->isBinary()); 0056 0057 const QString typecomment = item->mimeType().isEmpty() ? i18nc("@label unknown mimetype", "Unknown") : mMimeType.comment(); 0058 mUi->mTypeLabel->setText(typecomment); 0059 0060 setModal(modal); 0061 mOkButton->setEnabled(false); 0062 0063 mUi->mInlineCheck->setEnabled(false); 0064 if (item->attachment().isUri() || item->attachment().data().isEmpty()) { 0065 mUi->mStackedWidget->setCurrentIndex(0); 0066 mUi->mURLRequester->setUrl(QUrl(item->uri())); 0067 urlChanged(item->uri()); 0068 } else { 0069 mUi->mInlineCheck->setEnabled(true); 0070 mUi->mStackedWidget->setCurrentIndex(1); 0071 mUi->mSizeLabel->setText(QStringLiteral("%1 (%2)").arg(KIO::convertSize(item->attachment().size()), QLocale().toString(item->attachment().size()))); 0072 } 0073 0074 connect(mUi->mInlineCheck, &QCheckBox::stateChanged, this, &AttachmentEditDialog::inlineChanged); 0075 connect(mUi->mURLRequester, 0076 &KUrlRequester::urlSelected, 0077 this, 0078 static_cast<void (AttachmentEditDialog::*)(const QUrl &)>(&AttachmentEditDialog::urlChanged)); 0079 connect(mUi->mURLRequester, 0080 &KUrlRequester::textChanged, 0081 this, 0082 static_cast<void (AttachmentEditDialog::*)(const QString &)>(&AttachmentEditDialog::urlChanged)); 0083 } 0084 0085 AttachmentEditDialog::~AttachmentEditDialog() 0086 { 0087 delete mUi; 0088 } 0089 0090 void AttachmentEditDialog::accept() 0091 { 0092 slotApply(); 0093 QDialog::accept(); 0094 } 0095 0096 void AttachmentEditDialog::slotApply() 0097 { 0098 QUrl url = mUi->mURLRequester->url(); 0099 0100 if (mUi->mLabelEdit->text().isEmpty()) { 0101 if (url.isLocalFile()) { 0102 mItem->setLabel(url.fileName()); 0103 } else { 0104 mItem->setLabel(url.url()); 0105 } 0106 } else { 0107 mItem->setLabel(mUi->mLabelEdit->text()); 0108 } 0109 if (mItem->label().isEmpty()) { 0110 mItem->setLabel(i18nc("@label", "New attachment")); 0111 } 0112 mItem->setMimeType(mMimeType.name()); 0113 0114 QString correctedUrl = url.url(); 0115 if (!url.isEmpty() && url.isRelative()) { 0116 // If the user used KURLRequester's KURLCompletion 0117 // (used the line edit instead of the file dialog) 0118 // the returned url is not absolute and is always relative 0119 // to the home directory (not pwd), so we must prepend home 0120 0121 correctedUrl = QDir::home().filePath(url.toLocalFile()); 0122 url = QUrl::fromLocalFile(correctedUrl); 0123 if (url.isValid()) { 0124 urlChanged(url); 0125 mItem->setLabel(url.fileName()); 0126 mItem->setUri(correctedUrl); 0127 mItem->setMimeType(mMimeType.name()); 0128 } 0129 } 0130 0131 if (mUi->mStackedWidget->currentIndex() == 0) { 0132 if (mUi->mInlineCheck->isChecked()) { 0133 auto job = KIO::storedGet(url); 0134 KJobWidgets::setWindow(job, nullptr); 0135 if (job->exec()) { 0136 QByteArray data = job->data(); 0137 mItem->setData(data); 0138 } 0139 } else { 0140 mItem->setUri(correctedUrl); 0141 } 0142 } 0143 } 0144 0145 void AttachmentEditDialog::inlineChanged(int state) 0146 { 0147 mOkButton->setEnabled(!mUi->mURLRequester->url().toDisplayString().trimmed().isEmpty() || mUi->mStackedWidget->currentIndex() == 1); 0148 if (state == Qt::Unchecked && mUi->mStackedWidget->currentIndex() == 1) { 0149 mUi->mStackedWidget->setCurrentIndex(0); 0150 if (!mItem->savedUri().isEmpty()) { 0151 mUi->mURLRequester->setUrl(QUrl(mItem->savedUri())); 0152 } else { 0153 mUi->mURLRequester->setUrl(QUrl(mItem->uri())); 0154 } 0155 } 0156 } 0157 0158 void AttachmentEditDialog::urlChanged(const QString &url) 0159 { 0160 const bool urlIsNotEmpty = !url.trimmed().isEmpty(); 0161 mOkButton->setEnabled(urlIsNotEmpty); 0162 mUi->mInlineCheck->setEnabled(urlIsNotEmpty || mUi->mStackedWidget->currentIndex() == 1); 0163 } 0164 0165 void AttachmentEditDialog::urlChanged(const QUrl &url) 0166 { 0167 QMimeDatabase db; 0168 mMimeType = db.mimeTypeForUrl(url); 0169 mUi->mTypeLabel->setText(mMimeType.comment()); 0170 mUi->mIcon->setPixmap(AttachmentIconItem::icon(mMimeType, url.path())); 0171 } 0172 0173 #include "moc_attachmenteditdialog.cpp"