2006-05-28 16:54:54 +00:00
/***************************************************************************
* Copyright ( C ) 2006 by Albert Astals Cid < aacid @ kde . org > *
* *
* This program is free software ; you can redistribute it and / or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation ; either version 2 of the License , or *
* ( at your option ) any later version . *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2007-04-19 18:30:20 +00:00
# include "embeddedfilesdialog.h"
2007-07-03 15:30:44 +00:00
# include <QAction>
# include <QCursor>
2006-05-28 16:54:54 +00:00
# include <QDateTime>
2007-07-03 15:30:44 +00:00
# include <QMenu>
2006-05-28 16:54:54 +00:00
# include <QTreeWidget>
# include <kfiledialog.h>
2007-07-03 14:47:16 +00:00
# include <kglobal.h>
2006-05-28 18:16:28 +00:00
# include <kicon.h>
2006-05-28 16:54:54 +00:00
# include <klocale.h>
# include <kmessagebox.h>
2006-05-28 18:16:28 +00:00
# include <kmimetype.h>
2006-05-28 16:54:54 +00:00
# include "core/document.h"
2006-12-28 15:48:13 +00:00
static QString dateToString ( const QDateTime & date )
{
return date . isValid ( )
2007-04-09 23:36:26 +00:00
? KGlobal : : locale ( ) - > formatDateTime ( date , KLocale : : LongDate , true )
2006-12-28 15:48:13 +00:00
: i18nc ( " Unknown date " , " Unknown " ) ;
}
2006-09-21 08:45:36 +00:00
EmbeddedFilesDialog : : EmbeddedFilesDialog ( QWidget * parent , const Okular : : Document * document ) : KDialog ( parent )
2006-05-28 16:54:54 +00:00
{
2006-06-13 08:44:08 +00:00
setCaption ( i18n ( " Embedded Files " ) ) ;
setButtons ( Close | User1 ) ;
2006-06-16 15:12:55 +00:00
setDefaultButton ( Close ) ;
2006-12-27 10:03:34 +00:00
setButtonGuiItem ( User1 , KStandardGuiItem : : save ( ) ) ;
2006-06-13 08:44:08 +00:00
2006-05-28 16:54:54 +00:00
m_tw = new QTreeWidget ( this ) ;
setMainWidget ( m_tw ) ;
QStringList header ;
header . append ( i18n ( " Name " ) ) ;
header . append ( i18n ( " Description " ) ) ;
2007-07-03 14:47:16 +00:00
header . append ( i18n ( " Size " ) ) ;
2006-05-28 16:54:54 +00:00
header . append ( i18n ( " Created " ) ) ;
2006-09-16 03:49:40 +00:00
header . append ( i18n ( " Modified " ) ) ;
2006-05-28 16:54:54 +00:00
m_tw - > setHeaderLabels ( header ) ;
m_tw - > setRootIsDecorated ( false ) ;
2007-07-03 10:33:11 +00:00
m_tw - > setSelectionMode ( QAbstractItemView : : ExtendedSelection ) ;
2007-07-03 15:30:44 +00:00
m_tw - > setContextMenuPolicy ( Qt : : CustomContextMenu ) ;
2006-09-16 03:49:40 +00:00
2006-09-21 08:45:36 +00:00
foreach ( Okular : : EmbeddedFile * ef , * document - > embeddedFiles ( ) )
2006-05-28 16:54:54 +00:00
{
QTreeWidgetItem * twi = new QTreeWidgetItem ( ) ;
twi - > setText ( 0 , ef - > name ( ) ) ;
2006-05-28 18:16:28 +00:00
KMimeType : : Ptr mime = KMimeType : : findByPath ( ef - > name ( ) , 0 , true ) ;
2006-06-17 10:22:24 +00:00
if ( mime )
{
2006-08-29 21:09:55 +00:00
twi - > setIcon ( 0 , KIcon ( mime - > iconName ( ) ) ) ;
2006-06-17 10:22:24 +00:00
}
2006-05-28 16:54:54 +00:00
twi - > setText ( 1 , ef - > description ( ) ) ;
2007-07-03 14:47:16 +00:00
twi - > setText ( 2 , ef - > size ( ) < = 0 ? i18nc ( " Not available size " , " N/A " ) : KGlobal : : locale ( ) - > formatByteSize ( ef - > size ( ) ) ) ;
twi - > setText ( 3 , dateToString ( ef - > creationDate ( ) ) ) ;
twi - > setText ( 4 , dateToString ( ef - > modificationDate ( ) ) ) ;
2006-05-28 16:54:54 +00:00
m_tw - > addTopLevelItem ( twi ) ;
m_files . insert ( twi , ef ) ;
}
2006-09-16 03:49:40 +00:00
// Having filled the columns, it is nice to resize them to be able to read the contents
for ( int lv = 0 ; lv < m_tw - > columnCount ( ) ; + + lv ) {
m_tw - > resizeColumnToContents ( lv ) ;
}
// This is a bit dubious, but I'm not seeing a nice way to say "expand to fit contents"
m_tw - > setMinimumWidth ( 640 ) ;
m_tw - > updateGeometry ( ) ;
2006-05-28 16:54:54 +00:00
connect ( this , SIGNAL ( user1Clicked ( ) ) , this , SLOT ( saveFile ( ) ) ) ;
2007-07-03 15:30:44 +00:00
connect ( m_tw , SIGNAL ( customContextMenuRequested ( QPoint ) ) , this , SLOT ( attachViewContextMenu ( QPoint ) ) ) ;
2006-05-28 16:54:54 +00:00
}
void EmbeddedFilesDialog : : saveFile ( )
{
QList < QTreeWidgetItem * > selected = m_tw - > selectedItems ( ) ;
foreach ( QTreeWidgetItem * twi , selected )
{
2006-09-21 08:45:36 +00:00
Okular : : EmbeddedFile * ef = m_files [ twi ] ;
2007-07-03 15:30:44 +00:00
saveFile ( ef ) ;
2006-05-28 16:54:54 +00:00
}
}
2007-07-03 15:30:44 +00:00
void EmbeddedFilesDialog : : attachViewContextMenu ( const QPoint & /*pos*/ )
{
QList < QTreeWidgetItem * > selected = m_tw - > selectedItems ( ) ;
if ( selected . isEmpty ( ) )
return ;
if ( selected . size ( ) > 1 )
return ;
QMenu menu ( this ) ;
QAction * saveAsAct = menu . addAction ( KIcon ( " document-save-as " ) , i18n ( " Save As... " ) ) ;
QAction * act = menu . exec ( QCursor : : pos ( ) ) ;
if ( ! act )
return ;
if ( act = = saveAsAct )
{
Okular : : EmbeddedFile * ef = m_files [ selected . at ( 0 ) ] ;
saveFile ( ef ) ;
}
}
void EmbeddedFilesDialog : : saveFile ( Okular : : EmbeddedFile * ef )
{
QString path = KFileDialog : : getSaveFileName ( ef - > name ( ) , QString ( ) , this , i18n ( " Where do you want to save %1? " , ef - > name ( ) ) ) ;
if ( path . isEmpty ( ) )
return ;
QFile f ( path ) ;
if ( ! f . exists ( ) | | KMessageBox : : warningContinueCancel ( this , i18n ( " A file named \" %1 \" already exists. Are you sure you want to overwrite it? " , path ) , QString ( ) , KGuiItem ( i18n ( " Overwrite " ) ) ) = = KMessageBox : : Continue )
{
f . open ( QIODevice : : WriteOnly ) ;
f . write ( ef - > data ( ) ) ;
f . close ( ) ;
}
}
2006-05-28 16:54:54 +00:00
# include "embeddedfilesdialog.moc"