compile
[kdegraphics.git] / okular / ui / embeddedfilesdialog.cpp
blob3237fa42bb42280264fcf2ed3f8ce8b2f41d6ce5
1 /***************************************************************************
2 * Copyright (C) 2006 by Albert Astals Cid <aacid@kde.org> *
3 * *
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. *
8 ***************************************************************************/
10 #include "embeddedfilesdialog.h"
12 #include <QAction>
13 #include <QCursor>
14 #include <QDateTime>
15 #include <QMenu>
16 #include <QTreeWidget>
18 #include <kglobal.h>
19 #include <kicon.h>
20 #include <klocale.h>
21 #include <kmimetype.h>
22 #include <kstandardguiitem.h>
24 #include "core/document.h"
25 #include "guiutils.h"
27 Q_DECLARE_METATYPE( Okular::EmbeddedFile* )
29 static const int EmbeddedFileRole = Qt::UserRole + 100;
31 static QString dateToString( const QDateTime & date )
33 return date.isValid()
34 ? KGlobal::locale()->formatDateTime( date, KLocale::LongDate, true )
35 : i18nc( "Unknown date", "Unknown" );
38 EmbeddedFilesDialog::EmbeddedFilesDialog(QWidget *parent, const Okular::Document *document) : KDialog(parent)
40 setCaption(i18nc("@title:window", "Embedded Files"));
41 setButtons(Close | User1);
42 setDefaultButton(Close);
43 setButtonGuiItem(User1, KStandardGuiItem::save());
45 m_tw = new QTreeWidget(this);
46 setMainWidget(m_tw);
47 QStringList header;
48 header.append(i18nc("@title:column", "Name"));
49 header.append(i18nc("@title:column", "Description"));
50 header.append(i18nc("@title:column", "Size"));
51 header.append(i18nc("@title:column", "Created"));
52 header.append(i18nc("@title:column", "Modified"));
53 m_tw->setHeaderLabels(header);
54 m_tw->setRootIsDecorated(false);
55 m_tw->setSelectionMode(QAbstractItemView::ExtendedSelection);
56 m_tw->setContextMenuPolicy(Qt::CustomContextMenu);
58 foreach(Okular::EmbeddedFile* ef, *document->embeddedFiles())
60 QTreeWidgetItem *twi = new QTreeWidgetItem();
61 twi->setText(0, ef->name());
62 KMimeType::Ptr mime = KMimeType::findByPath( ef->name(), 0, true );
63 if (mime)
65 twi->setIcon(0, KIcon(mime->iconName()));
67 twi->setText(1, ef->description());
68 twi->setText(2, ef->size() <= 0 ? i18nc("Not available size", "N/A") : KGlobal::locale()->formatByteSize(ef->size()));
69 twi->setText(3, dateToString( ef->creationDate() ) );
70 twi->setText(4, dateToString( ef->modificationDate() ) );
71 twi->setData( 0, EmbeddedFileRole, qVariantFromValue( ef ) );
72 m_tw->addTopLevelItem(twi);
74 // Having filled the columns, it is nice to resize them to be able to read the contents
75 for (int lv = 0; lv < m_tw->columnCount(); ++lv) {
76 m_tw->resizeColumnToContents(lv);
78 // This is a bit dubious, but I'm not seeing a nice way to say "expand to fit contents"
79 m_tw->setMinimumWidth(640);
80 m_tw->updateGeometry();
82 connect(this, SIGNAL(user1Clicked()), this, SLOT(saveFile()));
83 connect(m_tw, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(attachViewContextMenu(QPoint)));
86 void EmbeddedFilesDialog::saveFile()
88 QList<QTreeWidgetItem *> selected = m_tw->selectedItems();
89 foreach(QTreeWidgetItem *twi, selected)
91 Okular::EmbeddedFile* ef = qvariant_cast< Okular::EmbeddedFile* >( twi->data( 0, EmbeddedFileRole ) );
92 saveFile(ef);
96 void EmbeddedFilesDialog::attachViewContextMenu( const QPoint& /*pos*/ )
98 QList<QTreeWidgetItem *> selected = m_tw->selectedItems();
99 if ( selected.isEmpty() )
100 return;
102 if ( selected.size() > 1 )
103 return;
105 QMenu menu( this );
106 QAction* saveAsAct = menu.addAction( KIcon( "document-save-as" ), i18nc( "@action:inmenu", "&Save As..." ) );
108 QAction* act = menu.exec( QCursor::pos() );
109 if ( !act )
110 return;
112 if ( act == saveAsAct )
114 Okular::EmbeddedFile* ef = qvariant_cast< Okular::EmbeddedFile* >( selected.at( 0 )->data( 0, EmbeddedFileRole ) );
115 saveFile( ef );
119 void EmbeddedFilesDialog::saveFile( Okular::EmbeddedFile* ef )
121 GuiUtils::saveEmbeddedFile( ef, this );
124 #include "embeddedfilesdialog.moc"