1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz <peter.penz@gmx.at> *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
9 * This program 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 *
12 * GNU General Public License for more details. *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
20 #include <config-kmetadata.h>
22 #include "infosidebarpage.h"
28 #include <QPushButton>
31 #include <QFontMetrics>
33 #include <QInputDialog>
36 #include <kfileplacesmodel.h>
38 #include <kstandarddirs.h>
39 #include <kio/previewjob.h>
40 #include <kfileitem.h>
42 #include <kglobalsettings.h>
43 #include <kfilemetainfo.h>
45 #include <kseparator.h>
46 #include <kiconloader.h>
48 #include "pixmapviewer.h"
49 #include "dolphinsettings.h"
50 #include "metadatawidget.h"
52 InfoSidebarPage::InfoSidebarPage(QWidget
* parent
) :
54 m_multipleSelection(false), //TODO: check if I'm needed
55 m_pendingPreview(false),
57 m_currentSelection(KFileItemList()),
63 const int spacing
= KDialog::spacingHint();
65 m_timer
= new QTimer(this);
66 connect(m_timer
, SIGNAL(timeout()),
67 this, SLOT(slotTimeout()));
69 QVBoxLayout
* layout
= new QVBoxLayout
;
70 layout
->setSpacing(spacing
);
73 m_preview
= new PixmapViewer(this);
74 m_preview
->setMinimumWidth(K3Icon::SizeEnormous
);
75 m_preview
->setFixedHeight(K3Icon::SizeEnormous
);
78 m_name
= new QLabel(this);
79 m_name
->setTextFormat(Qt::RichText
);
80 m_name
->setAlignment(m_name
->alignment() | Qt::AlignHCenter
);
81 QFontMetrics
fontMetrics(m_name
->font());
82 m_name
->setMinimumHeight(fontMetrics
.height() * 3);
83 m_name
->setSizePolicy(QSizePolicy::MinimumExpanding
, QSizePolicy::Maximum
);
85 KSeparator
* sep1
= new KSeparator(this);
87 // general information
88 m_infos
= new QLabel(this);
89 m_infos
->setSizePolicy(QSizePolicy::Minimum
, QSizePolicy::Fixed
);
90 m_infos
->setTextFormat(Qt::RichText
);
92 KSeparator
* sep2
= new KSeparator(this);
94 if (MetaDataWidget::metaDataAvailable()) {
95 m_metadataWidget
= new MetaDataWidget(this);
98 layout
->addItem(new QSpacerItem(spacing
, spacing
, QSizePolicy::Preferred
, QSizePolicy::Fixed
));
99 layout
->addWidget(m_preview
);
100 layout
->addWidget(m_name
);
101 layout
->addWidget(sep1
);
102 layout
->addWidget(m_infos
);
103 layout
->addWidget(sep2
);
104 if (m_metadataWidget
) {
105 layout
->addWidget(m_metadataWidget
);
106 layout
->addWidget(new KSeparator(this));
108 // ensure that widgets in the information side bar are aligned towards the top
109 layout
->addStretch(1);
113 InfoSidebarPage::~InfoSidebarPage()
116 void InfoSidebarPage::setUrl(const KUrl
& url
)
118 if (!m_shownUrl
.equals(url
, KUrl::CompareWithoutTrailingSlash
)) {
125 void InfoSidebarPage::setSelection(const KFileItemList
& selection
)
127 // TODO: deactivated the following code, as it has side effects. To
128 // reproduce start Dolphin and open a folder -> the URL navigator gets
129 // reset. First guess: it seems that a setUrl signal is emitted
130 // by the following code
134 m_currentSelection = selection;
135 m_multipleSelection = (m_currentSelection.size() > 1);
139 void InfoSidebarPage::requestDelayedItemInfo(const KUrl
& url
)
143 if (!url
.isEmpty() && !m_multipleSelection
) {
144 m_urlCandidate
= url
;
145 m_timer
->setSingleShot(true);
150 void InfoSidebarPage::showItemInfo()
154 KFileItemList selectedItems
= m_currentSelection
;
156 if (selectedItems
.count() == 0) {
159 file
= selectedItems
[0]->url();
161 if (m_multipleSelection
) {
162 KIconLoader iconLoader
;
163 QPixmap icon
= iconLoader
.loadIcon("exec",
165 K3Icon::SizeEnormous
);
166 m_preview
->setPixmap(icon
);
167 m_name
->setText(i18n("%1 items selected", selectedItems
.count()));
168 } else if (!applyBookmark(file
)) {
169 // try to get a preview pixmap from the item...
173 m_pendingPreview
= true;
174 m_preview
->setPixmap(QPixmap());
176 KIO::PreviewJob
* job
= KIO::filePreview(list
,
178 K3Icon::SizeEnormous
,
183 job
->setIgnoreMaximumSize(true);
185 connect(job
, SIGNAL(gotPreview(const KFileItem
&, const QPixmap
&)),
186 this, SLOT(gotPreview(const KFileItem
&, const QPixmap
&)));
187 connect(job
, SIGNAL(failed(const KFileItem
&)),
188 this, SLOT(slotPreviewFailed(const KFileItem
&)));
191 text
.append(file
.fileName());
193 m_name
->setText(text
);
199 void InfoSidebarPage::slotTimeout()
201 m_shownUrl
= m_urlCandidate
;
205 void InfoSidebarPage::slotPreviewFailed(const KFileItem
& item
)
207 m_pendingPreview
= false;
208 if (!applyBookmark(item
.url())) {
209 m_preview
->setPixmap(item
.pixmap(K3Icon::SizeEnormous
));
213 void InfoSidebarPage::gotPreview(const KFileItem
& item
,
214 const QPixmap
& pixmap
)
217 if (m_pendingPreview
) {
218 m_preview
->setPixmap(pixmap
);
219 m_pendingPreview
= false;
223 bool InfoSidebarPage::applyBookmark(const KUrl
& url
)
225 KFilePlacesModel
*placesModel
= DolphinSettings::instance().placesModel();
226 int count
= placesModel
->rowCount();
228 for (int i
= 0; i
< count
; ++i
) {
229 QModelIndex index
= placesModel
->index(i
, 0);
231 if (url
.equals(placesModel
->url(index
), KUrl::CompareWithoutTrailingSlash
)) {
233 text
.append(placesModel
->text(index
));
235 m_name
->setText(text
);
237 m_preview
->setPixmap(placesModel
->icon(index
).pixmap(128, 128));
245 void InfoSidebarPage::cancelRequest()
248 m_pendingPreview
= false;
251 void InfoSidebarPage::createMetaInfo()
254 if (m_currentSelection
.size() == 0) {
255 KFileItem
fileItem(S_IFDIR
, KFileItem::Unknown
, m_shownUrl
);
258 if (fileItem
.isDir()) {
259 addInfoLine(i18n("Type:"), i18n("Directory"));
261 if (MetaDataWidget::metaDataAvailable())
262 m_metadataWidget
->setFile(fileItem
.url());
263 } else if (m_currentSelection
.count() == 1) {
264 KFileItem
* fileItem
= m_currentSelection
.at(0);
265 addInfoLine(i18n("Type:"), fileItem
->mimeComment());
267 QString
sizeText(KIO::convertSize(fileItem
->size()));
268 addInfoLine(i18n("Size:"), sizeText
);
269 addInfoLine(i18n("Modified:"), fileItem
->timeString());
271 const KFileMetaInfo
& metaInfo
= fileItem
->metaInfo();
272 if (metaInfo
.isValid()) {
273 QStringList keys
= metaInfo
.supportedKeys();
274 for (QStringList::Iterator it
= keys
.begin(); it
!= keys
.end(); ++it
) {
275 if (showMetaInfo(*it
)) {
276 KFileMetaInfoItem metaInfoItem
= metaInfo
.item(*it
);
277 addInfoLine(*it
, metaInfoItem
.value().toString());
281 if (MetaDataWidget::metaDataAvailable())
282 m_metadataWidget
->setFile(fileItem
->url());
284 if (MetaDataWidget::metaDataAvailable())
285 m_metadataWidget
->setFiles(m_currentSelection
.urlList());
286 unsigned long int totSize
= 0;
287 foreach(KFileItem
* item
, m_currentSelection
) {
288 totSize
+= item
->size(); //FIXME what to do with directories ? (same with the one-item-selected-code), item->size() does not return the size of the content : not very instinctive for users
290 addInfoLine(i18n("Total size:"), KIO::convertSize(totSize
));
295 void InfoSidebarPage::beginInfoLines()
297 m_infoLines
= QString();
300 void InfoSidebarPage::endInfoLines()
302 m_infos
->setText(m_infoLines
);
305 bool InfoSidebarPage::showMetaInfo(const QString
& key
) const
307 // sorted list of keys, where it's data should be shown
308 static const char* keys
[] = {
323 // do a binary search for the key...
325 int bottom
= sizeof(keys
) / sizeof(char*) - 1;
326 while (top
< bottom
) {
327 const int middle
= (top
+ bottom
) / 2;
328 const int result
= key
.compare(keys
[middle
]);
331 } else if (result
> 0) {
341 void InfoSidebarPage::addInfoLine(const QString
& labelText
, const QString
& infoText
)
343 if (!m_infoLines
.isEmpty())
344 m_infoLines
+= "<br/>";
345 m_infoLines
+= QString("<b>%1</b> %2").arg(labelText
).arg(infoText
);
348 #include "infosidebarpage.moc"