Incidenceeditor

attachmenteditdialog.cpp
1/*
2 SPDX-FileCopyrightText: 2003 Cornelius Schumacher <schumacher@kde.org>
3 SPDX-FileCopyrightText: 2005 Reinhold Kainhofer <reinhold@kainhofer.com>
4 SPDX-FileCopyrightText: 2005 Rafal Rzepecki <divide@users.sourceforge.net>
5 SPDX-FileCopyrightText: 2010 Bertjan Broeksema <broeksema@kde.org>
6 SPDX-FileCopyrightText: 2010 Klaralvdalens Datakonsult AB, a KDAB Group company <info@kdab.net>
7
8 SPDX-License-Identifier: GPL-2.0-or-later WITH Qt-Commercial-exception-1.0
9*/
10
11#include "attachmenteditdialog.h"
12#include "attachmenticonview.h"
13#include "config-enterprise.h"
14#include "ui_attachmenteditdialog.h"
15#include <KFormat>
16#include <KIO/StoredTransferJob>
17#include <KIconLoader>
18#include <KJobWidgets>
19#include <KLocalizedString>
20#include <QDialogButtonBox>
21#include <QLocale>
22#include <QMimeDatabase>
23#include <QPushButton>
24
25using namespace IncidenceEditorNG;
26
27AttachmentEditDialog::AttachmentEditDialog(AttachmentIconItem *item, QWidget *parent, bool modal)
28 : QDialog(parent)
29 ,
30#if KDEPIM_ENTERPRISE_BUILD
31 mAttachment(KCalendarCore::Attachment('\0'))
32// use the non-uri constructor
33// as we want inline by default
34#else
35 mAttachment(KCalendarCore::Attachment(QString()))
36#endif
37 , mItem(item)
38 , mUi(new Ui::AttachmentEditDialog)
39{
40 setWindowTitle(i18nc("@title:window", "Edit Attachment"));
42 mMimeType = db.mimeTypeForName(item->mimeType());
43 auto page = new QWidget(this);
44 auto mainLayout = new QVBoxLayout(this);
46 mOkButton = buttonBox->button(QDialogButtonBox::Ok);
47 mOkButton->setDefault(true);
48 mOkButton->setShortcut(Qt::CTRL | Qt::Key_Return);
49 connect(buttonBox, &QDialogButtonBox::accepted, this, &AttachmentEditDialog::accept);
50 connect(buttonBox, &QDialogButtonBox::rejected, this, &AttachmentEditDialog::reject);
51 mainLayout->addWidget(page);
52 mainLayout->addWidget(buttonBox);
53
54 mUi->setupUi(page);
55 mUi->mLabelEdit->setText(item->label().isEmpty() ? item->uri() : item->label());
56 mUi->mIcon->setPixmap(item->icon().pixmap(KIconLoader::SizeSmallMedium));
57 mUi->mInlineCheck->setChecked(item->isBinary());
58
59 const QString typecomment = item->mimeType().isEmpty() ? i18nc("@label unknown mimetype", "Unknown") : mMimeType.comment();
60 mUi->mTypeLabel->setText(typecomment);
61
62 setModal(modal);
63 mOkButton->setEnabled(false);
64
65 mUi->mInlineCheck->setEnabled(false);
66 if (item->attachment().isUri() || item->attachment().data().isEmpty()) {
67 mUi->mStackedWidget->setCurrentIndex(0);
68 mUi->mURLRequester->setUrl(QUrl(item->uri()));
69 urlChanged(item->uri());
70 } else {
71 mUi->mInlineCheck->setEnabled(true);
72 mUi->mStackedWidget->setCurrentIndex(1);
73 KFormat format;
74 mUi->mSizeLabel->setText(
75 QStringLiteral("%1 (%2)").arg(format.formatByteSize(item->attachment().size()), QLocale().toString(item->attachment().size())));
76 }
77
78 connect(mUi->mInlineCheck, &QCheckBox::checkStateChanged, this, &AttachmentEditDialog::inlineChanged);
79 connect(mUi->mURLRequester,
81 this,
82 static_cast<void (AttachmentEditDialog::*)(const QUrl &)>(&AttachmentEditDialog::urlChanged));
83 connect(mUi->mURLRequester,
85 this,
86 static_cast<void (AttachmentEditDialog::*)(const QString &)>(&AttachmentEditDialog::urlChanged));
87}
88
89AttachmentEditDialog::~AttachmentEditDialog()
90{
91 delete mUi;
92}
93
94void AttachmentEditDialog::accept()
95{
96 slotApply();
98}
99
100void AttachmentEditDialog::slotApply()
101{
102 QUrl url = mUi->mURLRequester->url();
103
104 if (mUi->mLabelEdit->text().isEmpty()) {
105 if (url.isLocalFile()) {
106 mItem->setLabel(url.fileName());
107 } else {
108 mItem->setLabel(url.url());
109 }
110 } else {
111 mItem->setLabel(mUi->mLabelEdit->text());
112 }
113 if (mItem->label().isEmpty()) {
114 mItem->setLabel(i18nc("@label", "New attachment"));
115 }
116 mItem->setMimeType(mMimeType.name());
117
118 QString correctedUrl = url.url();
119 if (!url.isEmpty() && url.isRelative()) {
120 // If the user used KURLRequester's KURLCompletion
121 // (used the line edit instead of the file dialog)
122 // the returned url is not absolute and is always relative
123 // to the home directory (not pwd), so we must prepend home
124
125 correctedUrl = QDir::home().filePath(url.toLocalFile());
126 url = QUrl::fromLocalFile(correctedUrl);
127 if (url.isValid()) {
128 urlChanged(url);
129 mItem->setLabel(url.fileName());
130 mItem->setUri(correctedUrl);
131 mItem->setMimeType(mMimeType.name());
132 }
133 }
134
135 if (mUi->mStackedWidget->currentIndex() == 0) {
136 if (mUi->mInlineCheck->isChecked()) {
137 auto job = KIO::storedGet(url);
138 KJobWidgets::setWindow(job, nullptr);
139 if (job->exec()) {
140 QByteArray data = job->data();
141 mItem->setData(data);
142 }
143 } else {
144 mItem->setUri(correctedUrl);
145 }
146 }
147}
148
149void AttachmentEditDialog::inlineChanged(int state)
150{
151 mOkButton->setEnabled(!mUi->mURLRequester->url().toDisplayString().trimmed().isEmpty() || mUi->mStackedWidget->currentIndex() == 1);
152 if (state == Qt::Unchecked && mUi->mStackedWidget->currentIndex() == 1) {
153 mUi->mStackedWidget->setCurrentIndex(0);
154 if (!mItem->savedUri().isEmpty()) {
155 mUi->mURLRequester->setUrl(QUrl(mItem->savedUri()));
156 } else {
157 mUi->mURLRequester->setUrl(QUrl(mItem->uri()));
158 }
159 }
160}
161
162void AttachmentEditDialog::urlChanged(const QString &url)
163{
164 const bool urlIsNotEmpty = !url.trimmed().isEmpty();
165 mOkButton->setEnabled(urlIsNotEmpty);
166 mUi->mInlineCheck->setEnabled(urlIsNotEmpty || mUi->mStackedWidget->currentIndex() == 1);
167}
168
169void AttachmentEditDialog::urlChanged(const QUrl &url)
170{
171 QMimeDatabase db;
172 mMimeType = db.mimeTypeForUrl(url);
173 mUi->mTypeLabel->setText(mMimeType.comment());
174 mUi->mIcon->setPixmap(AttachmentIconItem::icon(mMimeType, url.path()).pixmap(KIconLoader::SizeSmallMedium));
175}
176
177#include "moc_attachmenteditdialog.cpp"
QByteArray data() const
QString formatByteSize(double size, int precision=1, KFormat::BinaryUnitDialect dialect=KFormat::DefaultBinaryDialect, KFormat::BinarySizeUnits units=KFormat::DefaultBinaryUnits) const
void urlSelected(const QUrl &)
void textChanged(const QString &)
QString i18nc(const char *context, const char *text, const TYPE &arg...)
char * toString(const EngineQuery &query)
KIOCORE_EXPORT StoredTransferJob * storedGet(const QUrl &url, LoadType reload=NoReload, JobFlags flags=DefaultFlags)
void setWindow(QObject *job, QWidget *widget)
char * data()
bool isEmpty() const const
virtual void accept()
QString filePath(const QString &fileName) const const
QDir home()
QPixmap pixmap(QWindow *window, const QSize &size, Mode mode, State state) const const
QMimeType mimeTypeForName(const QString &nameOrAlias) const const
QMimeType mimeTypeForUrl(const QUrl &url) const const
bool isEmpty() const const
QString trimmed() const const
Unchecked
Key_Return
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
QString fileName(ComponentFormattingOptions options) const const
QUrl fromLocalFile(const QString &localFile)
bool isEmpty() const const
bool isLocalFile() const const
bool isRelative() const const
bool isValid() const const
QString path(ComponentFormattingOptions options) const const
QString toLocalFile() const const
QString url(FormattingOptions options) const const
void setEnabled(bool)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:58:15 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.