File indexing completed on 2024-05-12 05:46:37
0001 /* 0002 Copyright (c) 2003 Cornelius Schumacher <schumacher@kde.org> 0003 Copyright (C) 2005 Reinhold Kainhofer <reinhold@kainhofer.com> 0004 Copyright (c) 2005 Rafal Rzepecki <divide@users.sourceforge.net> 0005 Copyright (c) 2010 Bertjan Broeksema <broeksema@kde.org> 0006 Copyright (C) 2010 Klaralvdalens Datakonsult AB, a KDAB Group company <info@kdab.net> 0007 0008 This program is free software; you can redistribute it and/or modify 0009 it under the terms of the GNU General Public License as published by 0010 the Free Software Foundation; either version 2 of the License, or 0011 (at your option) any later version. 0012 0013 This program is distributed in the hope that it will be useful, 0014 but WITHOUT ANY WARRANTY; without even the implied warranty of 0015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0016 GNU General Public License for more details. 0017 0018 You should have received a copy of the GNU General Public License along 0019 with this program; if not, write to the Free Software Foundation, Inc., 0020 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 0021 0022 As a special exception, permission is given to link this program 0023 with any edition of Qt, and distribute the resulting executable, 0024 without including the source code for Qt in the source distribution. 0025 */ 0026 0027 #include "attachmenteditdialog.h" 0028 #include "attachmenticonview.h" 0029 #include "ui_attachmenteditdialog.h" 0030 #include <KLocalizedString> 0031 #include <KIO/StoredTransferJob> 0032 #include <KJobWidgets> 0033 #include <QLocale> 0034 #include <QDialogButtonBox> 0035 #include <QPushButton> 0036 #include <QMimeDatabase> 0037 0038 using namespace IncidenceEditorNG; 0039 0040 AttachmentEditDialog::AttachmentEditDialog(AttachmentIconItem *item, QWidget *parent, bool modal) 0041 : QDialog(parent) 0042 , 0043 #ifdef KDEPIM_ENTERPRISE_BUILD 0044 mAttachment(KCalendarCore::Attachment('\0')) 0045 //use the non-uri constructor 0046 // as we want inline by default 0047 #else 0048 mAttachment(KCalendarCore::Attachment(QString())) 0049 #endif 0050 , mItem(item) 0051 , mUi(new Ui::AttachmentEditDialog) 0052 { 0053 setWindowTitle(i18nc("@title:window", "Edit Attachment")); 0054 QMimeDatabase db; 0055 mMimeType = db.mimeTypeForName(item->mimeType()); 0056 QWidget *page = new QWidget(this); 0057 QVBoxLayout *mainLayout = new QVBoxLayout(this); 0058 QDialogButtonBox *buttonBox = new QDialogButtonBox( 0059 QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this); 0060 mOkButton = buttonBox->button(QDialogButtonBox::Ok); 0061 mOkButton->setDefault(true); 0062 mOkButton->setShortcut(Qt::CTRL | Qt::Key_Return); 0063 connect(buttonBox, &QDialogButtonBox::accepted, this, &AttachmentEditDialog::accept); 0064 connect(buttonBox, &QDialogButtonBox::rejected, this, &AttachmentEditDialog::reject); 0065 mainLayout->addWidget(page); 0066 mainLayout->addWidget(buttonBox); 0067 0068 mUi->setupUi(page); 0069 mUi->mLabelEdit->setText(item->label().isEmpty() ? item->uri() : item->label()); 0070 mUi->mIcon->setPixmap(item->icon()); 0071 mUi->mInlineCheck->setChecked(item->isBinary()); 0072 0073 const QString typecomment = item->mimeType().isEmpty() 0074 ? i18nc("@label unknown mimetype", "Unknown") 0075 : mMimeType.comment(); 0076 mUi->mTypeLabel->setText(typecomment); 0077 0078 setModal(modal); 0079 mOkButton->setEnabled(false); 0080 0081 mUi->mInlineCheck->setEnabled(false); 0082 if (item->attachment().isUri() || item->attachment().data().isEmpty()) { 0083 mUi->mStackedWidget->setCurrentIndex(0); 0084 mUi->mURLRequester->setUrl(QUrl(item->uri())); 0085 urlChanged(item->uri()); 0086 } else { 0087 mUi->mInlineCheck->setEnabled(true); 0088 mUi->mStackedWidget->setCurrentIndex(1); 0089 mUi->mSizeLabel->setText(QStringLiteral("%1 (%2)"). 0090 arg(KIO::convertSize(item->attachment().size()), 0091 QLocale().toString(item->attachment().size()))); 0092 } 0093 0094 connect(mUi->mInlineCheck, &QCheckBox::stateChanged, this, 0095 &AttachmentEditDialog::inlineChanged); 0096 connect(mUi->mURLRequester, qOverload<const QUrl &>(&KUrlRequester::urlSelected), this, static_cast<void (AttachmentEditDialog::*)(const QUrl &)>(&AttachmentEditDialog:: urlChanged)); 0097 connect(mUi->mURLRequester, &KUrlRequester::textChanged, this, static_cast<void (AttachmentEditDialog::*)(const QString &)>(&AttachmentEditDialog:: 0098 urlChanged)); 0099 } 0100 0101 AttachmentEditDialog::~AttachmentEditDialog() 0102 { 0103 delete mUi; 0104 } 0105 0106 void AttachmentEditDialog::accept() 0107 { 0108 slotApply(); 0109 QDialog::accept(); 0110 } 0111 0112 void AttachmentEditDialog::slotApply() 0113 { 0114 QUrl url = mUi->mURLRequester->url(); 0115 0116 if (mUi->mLabelEdit->text().isEmpty()) { 0117 if (url.isLocalFile()) { 0118 mItem->setLabel(url.fileName()); 0119 } else { 0120 mItem->setLabel(url.url()); 0121 } 0122 } else { 0123 mItem->setLabel(mUi->mLabelEdit->text()); 0124 } 0125 if (mItem->label().isEmpty()) { 0126 mItem->setLabel(i18nc("@label", "New attachment")); 0127 } 0128 mItem->setMimeType(mMimeType.name()); 0129 0130 QString correctedUrl = url.url(); 0131 if (!url.isEmpty() && url.isRelative()) { 0132 // If the user used KURLRequester's KURLCompletion 0133 // (used the line edit instead of the file dialog) 0134 // the returned url is not absolute and is always relative 0135 // to the home directory (not pwd), so we must prepend home 0136 0137 correctedUrl = QDir::home().filePath(url.toLocalFile()); 0138 url = QUrl::fromLocalFile(correctedUrl); 0139 if (url.isValid()) { 0140 urlChanged(url); 0141 mItem->setLabel(url.fileName()); 0142 mItem->setUri(correctedUrl); 0143 mItem->setMimeType(mMimeType.name()); 0144 } 0145 } 0146 0147 if (mUi->mStackedWidget->currentIndex() == 0) { 0148 if (mUi->mInlineCheck->isChecked()) { 0149 auto job = KIO::storedGet(url); 0150 KJobWidgets::setWindow(job, nullptr); 0151 if (job->exec()) { 0152 QByteArray data = job->data(); 0153 mItem->setData(data); 0154 } 0155 } else { 0156 mItem->setUri(correctedUrl); 0157 } 0158 } 0159 } 0160 0161 void AttachmentEditDialog::inlineChanged(int state) 0162 { 0163 mOkButton->setEnabled(!mUi->mURLRequester->url().toDisplayString().trimmed().isEmpty() 0164 || mUi->mStackedWidget->currentIndex() == 1); 0165 if (state == Qt::Unchecked && mUi->mStackedWidget->currentIndex() == 1) { 0166 mUi->mStackedWidget->setCurrentIndex(0); 0167 if (!mItem->savedUri().isEmpty()) { 0168 mUi->mURLRequester->setUrl(QUrl(mItem->savedUri())); 0169 } else { 0170 mUi->mURLRequester->setUrl(QUrl(mItem->uri())); 0171 } 0172 } 0173 } 0174 0175 void AttachmentEditDialog::urlChanged(const QString &url) 0176 { 0177 const bool urlIsNotEmpty = !url.trimmed().isEmpty(); 0178 mOkButton->setEnabled(urlIsNotEmpty); 0179 mUi->mInlineCheck->setEnabled(urlIsNotEmpty 0180 || mUi->mStackedWidget->currentIndex() == 1); 0181 } 0182 0183 void AttachmentEditDialog::urlChanged(const QUrl &url) 0184 { 0185 QMimeDatabase db; 0186 mMimeType = db.mimeTypeForUrl(url); 0187 mUi->mTypeLabel->setText(mMimeType.comment()); 0188 mUi->mIcon->setPixmap(AttachmentIconItem::icon(mMimeType, url.path())); 0189 }