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 <KJobWidgets>
18#include <KLocalizedString>
19#include <QDialogButtonBox>
20#include <QLocale>
21#include <QMimeDatabase>
22#include <QPushButton>
23
24using namespace IncidenceEditorNG;
25
26AttachmentEditDialog::AttachmentEditDialog(AttachmentIconItem *item, QWidget *parent, bool modal)
27 : QDialog(parent)
28 ,
29#if KDEPIM_ENTERPRISE_BUILD
30 mAttachment(KCalendarCore::Attachment('\0'))
31// use the non-uri constructor
32// as we want inline by default
33#else
34 mAttachment(KCalendarCore::Attachment(QString()))
35#endif
36 , mItem(item)
37 , mUi(new Ui::AttachmentEditDialog)
38{
39 setWindowTitle(i18nc("@title:window", "Edit Attachment"));
41 mMimeType = db.mimeTypeForName(item->mimeType());
42 auto page = new QWidget(this);
43 auto mainLayout = new QVBoxLayout(this);
45 mOkButton = buttonBox->button(QDialogButtonBox::Ok);
46 mOkButton->setDefault(true);
47 mOkButton->setShortcut(Qt::CTRL | Qt::Key_Return);
48 connect(buttonBox, &QDialogButtonBox::accepted, this, &AttachmentEditDialog::accept);
49 connect(buttonBox, &QDialogButtonBox::rejected, this, &AttachmentEditDialog::reject);
50 mainLayout->addWidget(page);
51 mainLayout->addWidget(buttonBox);
52
53 mUi->setupUi(page);
54 mUi->mLabelEdit->setText(item->label().isEmpty() ? item->uri() : item->label());
55 mUi->mIcon->setPixmap(item->icon());
56 mUi->mInlineCheck->setChecked(item->isBinary());
57
58 const QString typecomment = item->mimeType().isEmpty() ? i18nc("@label unknown mimetype", "Unknown") : mMimeType.comment();
59 mUi->mTypeLabel->setText(typecomment);
60
61 setModal(modal);
62 mOkButton->setEnabled(false);
63
64 mUi->mInlineCheck->setEnabled(false);
65 if (item->attachment().isUri() || item->attachment().data().isEmpty()) {
66 mUi->mStackedWidget->setCurrentIndex(0);
67 mUi->mURLRequester->setUrl(QUrl(item->uri()));
68 urlChanged(item->uri());
69 } else {
70 mUi->mInlineCheck->setEnabled(true);
71 mUi->mStackedWidget->setCurrentIndex(1);
72 KFormat format;
73 mUi->mSizeLabel->setText(
74 QStringLiteral("%1 (%2)").arg(format.formatByteSize(item->attachment().size()), QLocale().toString(item->attachment().size())));
75 }
76
77 connect(mUi->mInlineCheck, &QCheckBox::checkStateChanged, this, &AttachmentEditDialog::inlineChanged);
78 connect(mUi->mURLRequester,
80 this,
81 static_cast<void (AttachmentEditDialog::*)(const QUrl &)>(&AttachmentEditDialog::urlChanged));
82 connect(mUi->mURLRequester,
84 this,
85 static_cast<void (AttachmentEditDialog::*)(const QString &)>(&AttachmentEditDialog::urlChanged));
86}
87
88AttachmentEditDialog::~AttachmentEditDialog()
89{
90 delete mUi;
91}
92
93void AttachmentEditDialog::accept()
94{
95 slotApply();
97}
98
99void AttachmentEditDialog::slotApply()
100{
101 QUrl url = mUi->mURLRequester->url();
102
103 if (mUi->mLabelEdit->text().isEmpty()) {
104 if (url.isLocalFile()) {
105 mItem->setLabel(url.fileName());
106 } else {
107 mItem->setLabel(url.url());
108 }
109 } else {
110 mItem->setLabel(mUi->mLabelEdit->text());
111 }
112 if (mItem->label().isEmpty()) {
113 mItem->setLabel(i18nc("@label", "New attachment"));
114 }
115 mItem->setMimeType(mMimeType.name());
116
117 QString correctedUrl = url.url();
118 if (!url.isEmpty() && url.isRelative()) {
119 // If the user used KURLRequester's KURLCompletion
120 // (used the line edit instead of the file dialog)
121 // the returned url is not absolute and is always relative
122 // to the home directory (not pwd), so we must prepend home
123
124 correctedUrl = QDir::home().filePath(url.toLocalFile());
125 url = QUrl::fromLocalFile(correctedUrl);
126 if (url.isValid()) {
127 urlChanged(url);
128 mItem->setLabel(url.fileName());
129 mItem->setUri(correctedUrl);
130 mItem->setMimeType(mMimeType.name());
131 }
132 }
133
134 if (mUi->mStackedWidget->currentIndex() == 0) {
135 if (mUi->mInlineCheck->isChecked()) {
136 auto job = KIO::storedGet(url);
137 KJobWidgets::setWindow(job, nullptr);
138 if (job->exec()) {
139 QByteArray data = job->data();
140 mItem->setData(data);
141 }
142 } else {
143 mItem->setUri(correctedUrl);
144 }
145 }
146}
147
148void AttachmentEditDialog::inlineChanged(int state)
149{
150 mOkButton->setEnabled(!mUi->mURLRequester->url().toDisplayString().trimmed().isEmpty() || mUi->mStackedWidget->currentIndex() == 1);
151 if (state == Qt::Unchecked && mUi->mStackedWidget->currentIndex() == 1) {
152 mUi->mStackedWidget->setCurrentIndex(0);
153 if (!mItem->savedUri().isEmpty()) {
154 mUi->mURLRequester->setUrl(QUrl(mItem->savedUri()));
155 } else {
156 mUi->mURLRequester->setUrl(QUrl(mItem->uri()));
157 }
158 }
159}
160
161void AttachmentEditDialog::urlChanged(const QString &url)
162{
163 const bool urlIsNotEmpty = !url.trimmed().isEmpty();
164 mOkButton->setEnabled(urlIsNotEmpty);
165 mUi->mInlineCheck->setEnabled(urlIsNotEmpty || mUi->mStackedWidget->currentIndex() == 1);
166}
167
168void AttachmentEditDialog::urlChanged(const QUrl &url)
169{
170 QMimeDatabase db;
171 mMimeType = db.mimeTypeForUrl(url);
172 mUi->mTypeLabel->setText(mMimeType.comment());
173 mUi->mIcon->setPixmap(AttachmentIconItem::icon(mMimeType, url.path()));
174}
175
176#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)
QString label(StandardShortcut id)
char * data()
bool isEmpty() const const
virtual void accept()
QString filePath(const QString &fileName) const const
QDir home()
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 Fri Jul 26 2024 11:52:44 by doxygen 1.11.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.