1 /* This file is part of the KDE libraries
2     Copyright (C) 2005 Stephan Binner <binner@kde.org>
3 
4     This library is free software; you can redistribute it and/or
5     modify it under the terms of the GNU Library General Public
6     License version 2 as published by the Free Software Foundation.
7 
8     This library is distributed in the hope that it will be useful,
9     but WITHOUT ANY WARRANTY; without even the implied warranty of
10     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11     Library General Public License for more details.
12 
13     You should have received a copy of the GNU Library General Public License
14     along with this library; see the file COPYING.LIB.  If not, write to
15     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16     Boston, MA 02110-1301, USA.
17 
18  */
19 
20 #include "kpreviewprops.h"
21 #include <kio/previewjob.h>
22 #include <kprotocolinfo.h>
23 
24 #include <QLayout>
25 
26 #include <kfilemetapreview_p.h>
27 #include <klocalizedstring.h>
28 
29 class Q_DECL_HIDDEN KPreviewPropsPlugin::KPreviewPropsPluginPrivate
30 {
31 public:
KPreviewPropsPluginPrivate()32     KPreviewPropsPluginPrivate()  {}
~KPreviewPropsPluginPrivate()33     ~KPreviewPropsPluginPrivate() {}
34 };
35 
KPreviewPropsPlugin(KPropertiesDialog * props)36 KPreviewPropsPlugin::KPreviewPropsPlugin(KPropertiesDialog *props)
37     : KPropertiesDialogPlugin(props), d(new KPreviewPropsPluginPrivate)
38 {
39 
40     if (properties->items().count() > 1) {
41         return;
42     }
43 
44     createLayout();
45 }
46 
createLayout()47 void KPreviewPropsPlugin::createLayout()
48 {
49     // let the dialog create the page frame
50     QFrame *topframe = new QFrame();
51     properties->addPage(topframe, i18n("P&review"));
52     topframe->setFrameStyle(QFrame::NoFrame);
53 
54     QVBoxLayout *tmp = new QVBoxLayout(topframe);
55     tmp->setContentsMargins(0, 0, 0, 0);
56 
57     preview = new KFileMetaPreview(topframe);
58 
59     tmp->addWidget(preview);
60     connect(properties, SIGNAL(currentPageChanged(KPageWidgetItem*,KPageWidgetItem*)), SLOT(currentPageChanged(KPageWidgetItem*,KPageWidgetItem*)));
61 }
62 
~KPreviewPropsPlugin()63 KPreviewPropsPlugin::~KPreviewPropsPlugin()
64 {
65     delete d;
66 }
67 
supports(const KFileItemList & _items)68 bool KPreviewPropsPlugin::supports(const KFileItemList &_items)
69 {
70     if (_items.count() != 1) {
71         return false;
72     }
73     bool metaDataEnabled = KProtocolInfo::showFilePreview(_items.first().url().scheme());
74     if (!metaDataEnabled) {
75         return false;
76     }
77     const QMimeType mime = _items.first().determineMimeType();
78     const QStringList supportedMimeTypes = KIO::PreviewJob::supportedMimeTypes();
79     foreach (const QString &supportedMime, supportedMimeTypes) {
80         if (mime.inherits(supportedMime)) {
81             return true;
82         }
83     }
84     return false;
85 }
86 
currentPageChanged(KPageWidgetItem * current,KPageWidgetItem *)87 void KPreviewPropsPlugin::currentPageChanged(KPageWidgetItem *current, KPageWidgetItem *)
88 {
89     if (current->widget() != preview->parent()) {
90         return;
91     }
92 
93     disconnect(properties, SIGNAL(currentPageChanged(KPageWidgetItem*,KPageWidgetItem*)), this, SLOT(currentPageChanged(KPageWidgetItem*,KPageWidgetItem*)));
94     preview->showPreview(properties->item().url());
95 }
96 
97 #include "moc_kpreviewprops.cpp"
98