KMail: Fix scrolling up/down on the message viewer
[kdepim.git] / incidenceeditor-ng / attachmenteditdialog.cpp
blobc9747fb7600f9cae2521ab3dd4c2d614ed4ef5d4
1 /*
2 Copyright (c) 2003 Cornelius Schumacher <schumacher@kde.org>
3 Copyright (C) 2005 Reinhold Kainhofer <reinhold@kainhofer.com>
4 Copyright (c) 2005 Rafal Rzepecki <divide@users.sourceforge.net>
5 Copyright (c) 2010 Bertjan Broeksema <broeksema@kde.org>
6 Copyright (C) 2010 Klaralvdalens Datakonsult AB, a KDAB Group company <info@kdab.net>
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 As a special exception, permission is given to link this program
23 with any edition of Qt, and distribute the resulting executable,
24 without including the source code for Qt in the source distribution.
27 #include "attachmenteditdialog.h"
28 #include "attachmenticonview.h"
29 #include "ui_attachmenteditdialog.h"
30 #include <KLocalizedString>
32 #include <KIO/NetAccess>
33 #include <KLocale>
34 #include <KConfigGroup>
35 #include <QDialogButtonBox>
36 #include <QPushButton>
37 #include <QMimeDatabase>
38 #include <QMimeType>
40 using namespace IncidenceEditorNG;
42 AttachmentEditDialog::AttachmentEditDialog(AttachmentIconItem *item,
43 QWidget *parent,
44 bool modal)
45 : QDialog(parent),
46 #ifdef KDEPIM_ENTERPRISE_BUILD
47 mAttachment(new KCalCore::Attachment('\0')), //use the non-uri constructor
48 // as we want inline by default
49 #else
50 mAttachment(new KCalCore::Attachment(QString())),
51 #endif
52 mItem(item),
53 mUi(new Ui::AttachmentEditDialog)
55 QMimeDatabase db;
56 mMimeType = db.mimeTypeForName(item->mimeType());
57 QWidget *page = new QWidget(this);
58 QVBoxLayout *mainLayout = new QVBoxLayout;
59 setLayout(mainLayout);
60 QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
61 mOkButton = buttonBox->button(QDialogButtonBox::Ok);
62 mOkButton->setDefault(true);
63 mOkButton->setShortcut(Qt::CTRL | Qt::Key_Return);
64 connect(buttonBox, &QDialogButtonBox::accepted, this, &AttachmentEditDialog::accept);
65 connect(buttonBox, &QDialogButtonBox::rejected, this, &AttachmentEditDialog::reject);
66 mainLayout->addWidget(page);
67 mainLayout->addWidget(buttonBox);
69 mUi->setupUi(page);
70 mUi->mLabelEdit->setText(item->label().isEmpty() ? item->uri() : item->label());
71 mUi->mIcon->setPixmap(item->icon());
72 mUi->mInlineCheck->setChecked(item->isBinary());
74 QString typecomment = item->mimeType().isEmpty() ?
75 i18nc("@label unknown mimetype", "Unknown") :
76 mMimeType.comment();
77 mUi->mTypeLabel->setText(typecomment);
79 setModal(modal);
80 mOkButton->setEnabled(false);
82 mUi->mInlineCheck->setEnabled(false);
83 if (item->attachment()->isUri() || item->attachment()->data().isEmpty()) {
84 mUi->mStackedWidget->setCurrentIndex(0);
85 mUi->mURLRequester->setUrl(item->uri());
86 urlChanged(item->uri());
87 } else {
88 mUi->mInlineCheck->setEnabled(true);
89 mUi->mStackedWidget->setCurrentIndex(1);
90 mUi->mSizeLabel->setText(QStringLiteral("%1 (%2)").
91 arg(KIO::convertSize(item->attachment()->size())).
92 arg(KLocale::global()->formatNumber(
93 item->attachment()->size(), 0)));
96 connect(mUi->mInlineCheck, &QCheckBox::stateChanged, this, &AttachmentEditDialog::inlineChanged);
97 connect(mUi->mURLRequester, static_cast<void (KUrlRequester::*)(const QUrl &)>(&KUrlRequester::urlSelected),
98 this, static_cast<void (AttachmentEditDialog::*)(const QUrl &)>(&AttachmentEditDialog::urlChanged));
99 connect(mUi->mURLRequester, &KUrlRequester::textChanged,
100 this, static_cast<void (AttachmentEditDialog::*)(const QString &)>(&AttachmentEditDialog::urlChanged));
103 AttachmentEditDialog::~AttachmentEditDialog()
105 delete mUi;
108 void AttachmentEditDialog::accept()
110 slotApply();
111 QDialog::accept();
114 void AttachmentEditDialog::slotApply()
116 QUrl url = mUi->mURLRequester->url();
118 if (mUi->mLabelEdit->text().isEmpty()) {
119 if (url.isLocalFile()) {
120 mItem->setLabel(url.fileName());
121 } else {
122 mItem->setLabel(url.url());
124 } else {
125 mItem->setLabel(mUi->mLabelEdit->text());
127 if (mItem->label().isEmpty()) {
128 mItem->setLabel(i18nc("@label", "New attachment"));
130 mItem->setMimeType(mMimeType.name());
132 QString correctedUrl = url.url();
133 if (!url.isEmpty() && url.isRelative()) {
134 // If the user used KURLRequester's KURLCompletion
135 // (used the line edit instead of the file dialog)
136 // the returned url is not absolute and is always relative
137 // to the home directory (not pwd), so we must prepend home
139 correctedUrl = QDir::home().filePath(url.toLocalFile());
140 url = QUrl::fromLocalFile(correctedUrl);
141 if (url.isValid()) {
142 urlChanged(url);
143 mItem->setLabel(url.fileName());
144 mItem->setUri(correctedUrl);
145 mItem->setMimeType(mMimeType.name());
149 if (mUi->mStackedWidget->currentIndex() == 0) {
150 if (mUi->mInlineCheck->isChecked()) {
151 QString tmpFile;
152 if (KIO::NetAccess::download(correctedUrl, tmpFile, this)) {
153 QFile f(tmpFile);
154 if (!f.open(QIODevice::ReadOnly)) {
155 return;
157 QByteArray data = f.readAll();
158 f.close();
159 mItem->setData(data);
161 KIO::NetAccess::removeTempFile(tmpFile);
162 } else {
163 mItem->setUri(correctedUrl);
168 void AttachmentEditDialog::inlineChanged(int state)
170 mOkButton->setEnabled(!mUi->mURLRequester->url().isEmpty() ||
171 mUi->mStackedWidget->currentIndex() == 1);
172 if (state == Qt::Unchecked && mUi->mStackedWidget->currentIndex() == 1) {
173 mUi->mStackedWidget->setCurrentIndex(0);
174 if (!mItem->savedUri().isEmpty()) {
175 mUi->mURLRequester->setUrl(mItem->savedUri());
176 } else {
177 mUi->mURLRequester->setUrl(mItem->uri());
182 void AttachmentEditDialog::urlChanged(const QString &url)
184 mOkButton->setEnabled(!url.isEmpty());
185 mUi->mInlineCheck->setEnabled(!url.isEmpty() ||
186 mUi->mStackedWidget->currentIndex() == 1);
189 void AttachmentEditDialog::urlChanged(const QUrl &url)
191 QMimeDatabase db;
192 mMimeType = db.mimeTypeForUrl(url);
193 mUi->mTypeLabel->setText(mMimeType.comment());
194 mUi->mIcon->setPixmap(AttachmentIconItem::icon(mMimeType, url.path()));