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 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
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.
26 NOTE: May, 2010. Extracted this code from
27 kdepim/incidenceeditors/editorattachments.{h,cpp}
30 #include <config-enterprise.h>
32 #include "attachmenticonview.h"
34 #include <KIconLoader>
35 #include <KUrlMimeData>
36 #include <QTemporaryFile>
42 #include <QMimeDatabase>
45 using namespace IncidenceEditorNG
;
47 AttachmentIconItem::AttachmentIconItem(const KCalCore::Attachment::Ptr
&att
, QListWidget
*parent
)
48 : QListWidgetItem(parent
)
51 mAttachment
= KCalCore::Attachment::Ptr(new KCalCore::Attachment(*att
.data()));
52 mAttachment
->setLabel(att
->label());
54 // for the enteprise, inline attachments are the default
55 #ifdef KDEPIM_ENTERPRISE_BUILD
57 KCalCore::Attachment::Ptr(
58 new KCalCore::Attachment(QByteArray())); // use the non-uri constructor
59 // as we want inline by default
61 mAttachment
= KCalCore::Attachment::Ptr(new KCalCore::Attachment(QString()));
65 setFlags(flags() | Qt::ItemIsDragEnabled
);
68 AttachmentIconItem::~AttachmentIconItem()
72 KCalCore::Attachment::Ptr
AttachmentIconItem::attachment() const
77 const QString
AttachmentIconItem::uri() const
79 return mAttachment
->uri();
82 const QString
AttachmentIconItem::savedUri() const
87 void AttachmentIconItem::setUri(const QString
&uri
)
90 mAttachment
->setUri(mSaveUri
);
94 void AttachmentIconItem::setData(const QByteArray
&data
)
96 mAttachment
->setDecodedData(data
);
100 const QString
AttachmentIconItem::mimeType() const
102 return mAttachment
->mimeType();
105 void AttachmentIconItem::setMimeType(const QString
&mime
)
107 mAttachment
->setMimeType(mime
);
111 const QString
AttachmentIconItem::label() const
113 return mAttachment
->label();
116 void AttachmentIconItem::setLabel(const QString
&description
)
118 if (mAttachment
->label() == description
) {
121 mAttachment
->setLabel(description
);
125 bool AttachmentIconItem::isBinary() const
127 return mAttachment
->isBinary();
130 QPixmap
AttachmentIconItem::icon() const
133 return icon(db
.mimeTypeForName(mAttachment
->mimeType()),
134 mAttachment
->uri(), mAttachment
->isBinary());
137 QPixmap
AttachmentIconItem::icon(QMimeType mimeType
,
141 //QT5 use KIconEngine
143 QString iconStr
= mimeType
.iconName(uri
);
144 QStringList overlays
;
145 if (!uri
.isEmpty() && !binary
) {
146 overlays
<< "emblem-link";
149 return KIconLoader::global()->loadIcon(iconStr
, KIconLoader::Desktop
, 0,
150 KIconLoader::DefaultState
,
157 void AttachmentIconItem::readAttachment()
159 setText(mAttachment
->label());
160 setFlags(flags() | Qt::ItemIsEditable
);
163 if (mAttachment
->mimeType().isEmpty() || !(db
.mimeTypeForName(mAttachment
->mimeType()).isDefault())) {
165 if (mAttachment
->isUri()) {
166 mimeType
= db
.mimeTypeForUrl(mAttachment
->uri());
168 mimeType
= db
.mimeTypeForData(mAttachment
->decodedData());
170 mAttachment
->setMimeType(mimeType
.name());
176 AttachmentIconView::AttachmentIconView(QWidget
*parent
)
177 : QListWidget(parent
)
180 setAcceptDrops(true);
181 setSelectionMode(ExtendedSelection
);
182 setSelectionRectVisible(false);
183 setIconSize(QSize(KIconLoader::SizeLarge
, KIconLoader::SizeLarge
));
184 setFlow(LeftToRight
);
186 #ifndef QT_NO_DRAGANDDROP
187 setDragDropMode(DragDrop
);
188 setDragEnabled(true);
190 setEditTriggers(EditKeyPressed
);
191 setContextMenuPolicy(Qt::CustomContextMenu
);
194 QUrl
AttachmentIconView::tempFileForAttachment(const KCalCore::Attachment::Ptr
&attachment
) const
196 if (mTempFiles
.contains(attachment
)) {
197 return mTempFiles
.value(attachment
);
199 QTemporaryFile
*file
;
202 QStringList patterns
= db
.mimeTypeForName(attachment
->mimeType()).globPatterns();
204 if (!patterns
.empty()) {
205 file
= new QTemporaryFile(QDir::tempPath() + QLatin1String("/attachementview_XXXXX") + patterns
.first().remove('*'));
207 file
= new QTemporaryFile();
209 file
->setParent(const_cast<AttachmentIconView
*>(this));
211 file
->setAutoRemove(true);
213 // read-only not to give the idea that it could be written to
214 file
->setPermissions(QFile::ReadUser
);
215 file
->write(QByteArray::fromBase64(attachment
->data()));
216 mTempFiles
.insert(attachment
, file
->fileName());
218 return mTempFiles
.value(attachment
);
221 QMimeData
*AttachmentIconView::mimeData(const QList
< QListWidgetItem
*> items
) const
223 // create a list of the URL:s that we want to drag
226 foreach (QListWidgetItem
*it
, items
) {
227 if (it
->isSelected()) {
228 AttachmentIconItem
*item
= static_cast<AttachmentIconItem
*>(it
);
229 if (item
->isBinary()) {
230 urls
.append(tempFileForAttachment(item
->attachment()));
232 urls
.append(item
->uri());
234 labels
.append(QUrl::toPercentEncoding(item
->label()));
237 if (selectionMode() == NoSelection
) {
238 AttachmentIconItem
*item
= static_cast<AttachmentIconItem
*>(currentItem());
240 urls
.append(item
->uri());
241 labels
.append(QUrl::toPercentEncoding(item
->label()));
245 QMap
<QString
, QString
> metadata
;
246 metadata
["labels"] = labels
.join(":");
248 QMimeData
*mimeData
= new QMimeData
;
249 mimeData
->setUrls(urls
);
250 KUrlMimeData::setMetaData(metadata
, mimeData
);
254 QMimeData
*AttachmentIconView::mimeData() const
256 return mimeData(selectedItems());
259 void AttachmentIconView::startDrag(Qt::DropActions supportedActions
)
261 Q_UNUSED(supportedActions
);
262 #ifndef QT_NO_DRAGANDDROP
264 if (selectedItems().size() > 1) {
265 pixmap
= KIconLoader::global()->loadIcon("mail-attachment", KIconLoader::Desktop
);
267 if (pixmap
.isNull()) {
268 pixmap
= static_cast<AttachmentIconItem
*>(currentItem())->icon();
271 const QPoint
hotspot(pixmap
.width() / 2, pixmap
.height() / 2);
273 QDrag
*drag
= new QDrag(this);
274 drag
->setMimeData(mimeData());
276 drag
->setPixmap(pixmap
);
277 drag
->setHotSpot(hotspot
);
278 drag
->exec(Qt::CopyAction
);
282 void AttachmentIconView::keyPressEvent(QKeyEvent
*event
)
284 if ((event
->key() == Qt::Key_Return
|| event
->key() == Qt::Key_Enter
) &&
285 currentItem() && state() != EditingState
) {
286 emit
itemDoubleClicked(currentItem()); // ugly, but itemActivated() also includes single click
289 QListWidget::keyPressEvent(event
);