Changeset View
Changeset View
Standalone View
Standalone View
applets/notifications/plugin/thumbnailer.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | Copyright (C) 2016 Kai Uwe Broulik <kde@privat.broulik.de> | ||||
3 | | ||||
4 | This library is free software; you can redistribute it and/or | ||||
5 | modify it under the terms of the GNU Lesser General Public | ||||
6 | License as published by the Free Software Foundation; either | ||||
7 | version 2.1 of the License, or (at your option) any later version. | ||||
8 | | ||||
9 | This library is distributed in the hope that it will be useful, | ||||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||||
12 | Lesser General Public License for more details. | ||||
13 | | ||||
14 | You should have received a copy of the GNU Lesser General Public | ||||
15 | License along with this library; if not, write to the Free Software | ||||
16 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||||
17 | */ | ||||
18 | | ||||
19 | #include "thumbnailer.h" | ||||
20 | | ||||
21 | #include <KIO/PreviewJob> | ||||
22 | | ||||
23 | #include <QDebug> | ||||
24 | #include <QIcon> | ||||
25 | | ||||
26 | Thumbnailer::Thumbnailer(QObject *parent) : QObject(parent) | ||||
27 | { | ||||
28 | | ||||
29 | } | ||||
30 | | ||||
31 | Thumbnailer::~Thumbnailer() = default; | ||||
32 | | ||||
33 | void Thumbnailer::classBegin() | ||||
34 | { | ||||
35 | | ||||
36 | } | ||||
37 | | ||||
38 | void Thumbnailer::componentComplete() | ||||
39 | { | ||||
40 | m_inited = true; | ||||
41 | generatePreview(); | ||||
42 | } | ||||
43 | | ||||
44 | QUrl Thumbnailer::url() const | ||||
45 | { | ||||
46 | return m_url; | ||||
47 | } | ||||
48 | | ||||
49 | void Thumbnailer::setUrl(const QUrl &url) | ||||
50 | { | ||||
51 | if (m_url != url) { | ||||
52 | m_url = url; | ||||
53 | emit urlChanged(); | ||||
54 | | ||||
55 | generatePreview(); | ||||
56 | } | ||||
57 | } | ||||
58 | | ||||
59 | QSize Thumbnailer::size() const | ||||
60 | { | ||||
61 | return m_size; | ||||
62 | } | ||||
63 | | ||||
64 | void Thumbnailer::setSize(const QSize &size) | ||||
65 | { | ||||
66 | if (m_size != size) { | ||||
67 | m_size = size; | ||||
68 | emit sizeChanged(); | ||||
69 | | ||||
70 | generatePreview(); | ||||
71 | } | ||||
72 | } | ||||
73 | | ||||
74 | bool Thumbnailer::hasPreview() const | ||||
75 | { | ||||
76 | return !m_pixmap.isNull(); | ||||
77 | } | ||||
78 | | ||||
79 | QPixmap Thumbnailer::pixmap() const | ||||
80 | { | ||||
81 | return m_pixmap; | ||||
82 | } | ||||
83 | | ||||
84 | QSize Thumbnailer::pixmapSize() const | ||||
85 | { | ||||
86 | return m_pixmap.size(); | ||||
87 | } | ||||
88 | | ||||
89 | QString Thumbnailer::iconName() const | ||||
90 | { | ||||
91 | return m_iconName; | ||||
92 | } | ||||
93 | | ||||
94 | void Thumbnailer::generatePreview() | ||||
95 | { | ||||
96 | if (!m_inited) { | ||||
97 | return; | ||||
98 | } | ||||
99 | | ||||
100 | if (!m_url.isValid() || !m_url.isLocalFile() || !m_size.isValid()) { | ||||
101 | return; | ||||
102 | } | ||||
103 | | ||||
104 | KIO::PreviewJob *job = KIO::filePreview(KFileItemList({KFileItem(m_url)}), m_size); | ||||
105 | job->setIgnoreMaximumSize(true); | ||||
106 | | ||||
107 | connect(job, &KIO::PreviewJob::gotPreview, this, [this](const KFileItem &item, const QPixmap &preview) { | ||||
108 | Q_UNUSED(item); | ||||
109 | m_pixmap = preview; | ||||
110 | emit pixmapChanged(); | ||||
111 | | ||||
112 | if (!m_iconName.isEmpty()) { | ||||
113 | m_iconName.clear(); | ||||
114 | emit iconNameChanged(); | ||||
115 | } | ||||
116 | }); | ||||
117 | | ||||
118 | connect(job, &KIO::PreviewJob::failed, this, [this](const KFileItem &item) { | ||||
119 | m_pixmap = QPixmap(); | ||||
120 | emit pixmapChanged(); | ||||
121 | | ||||
122 | const QString &iconName = item.determineMimeType().iconName(); | ||||
123 | if (m_iconName != iconName) { | ||||
124 | m_iconName = iconName; | ||||
125 | emit iconNameChanged(); | ||||
126 | } | ||||
127 | }); | ||||
128 | | ||||
129 | job->start(); | ||||
130 | } |