2015-09-06 20:20:00 +02:00
# include "./attachmentsedit.h"
2015-04-22 19:33:53 +02:00
2015-09-06 20:20:00 +02:00
# include "./attachmentsmodel.h"
2015-04-22 19:33:53 +02:00
2015-12-05 22:55:05 +01:00
# include "ui_attachmentsedit.h"
2015-09-06 15:41:17 +02:00
2015-04-22 19:33:53 +02:00
# include <tagparser/mediafileinfo.h>
# include <tagparser/abstractattachment.h>
# include <c++utilities/io/copy.h>
2016-06-14 22:54:49 +02:00
# include <c++utilities/io/catchiofailure.h>
2015-04-22 19:33:53 +02:00
# include <QFileDialog>
# include <QMessageBox>
# include <fstream>
using namespace std ;
using namespace IoUtilities ;
using namespace Media ;
namespace QtGui {
AttachmentsEdit : : AttachmentsEdit ( MediaFileInfo * fileInfo , QWidget * parent ) :
QWidget ( parent ) ,
m_ui ( new Ui : : AttachmentsEdit ) ,
m_model ( nullptr )
{
// setup UI and model
m_ui - > setupUi ( this ) ;
m_model = new AttachmentsModel ( this ) ;
m_ui - > treeView - > setModel ( m_model ) ;
// connect signals and slots
connect ( m_ui - > restorePushButton , & QPushButton : : clicked , this , & AttachmentsEdit : : restore ) ;
connect ( m_ui - > clearPushButton , & QPushButton : : clicked , this , & AttachmentsEdit : : clear ) ;
connect ( m_ui - > addPushButton , & QPushButton : : clicked , this , & AttachmentsEdit : : showFileSelection ) ;
connect ( m_ui - > extractPushButton , & QPushButton : : clicked , this , & AttachmentsEdit : : extractSelected ) ;
connect ( m_ui - > treeView - > selectionModel ( ) , & QItemSelectionModel : : currentRowChanged , this , & AttachmentsEdit : : selectionChanged ) ;
// load specified file
setFileInfo ( fileInfo , true ) ;
}
AttachmentsEdit : : ~ AttachmentsEdit ( )
{ }
void AttachmentsEdit : : setFileInfo ( Media : : MediaFileInfo * fileInfo , bool updateUi )
{
m_fileInfo = fileInfo ;
m_currentAttachments . clear ( ) ;
if ( fileInfo & & fileInfo - > areAttachmentsSupported ( ) ) {
if ( AbstractContainer * container = fileInfo - > container ( ) ) {
auto count = container - > attachmentCount ( ) ;
m_currentAttachments . reserve ( count ) ;
for ( size_t i = 0 ; i < count ; + + i ) {
m_currentAttachments < < container - > attachment ( i ) ;
}
}
}
if ( updateUi ) {
setEnabled ( fileInfo & & fileInfo - > areAttachmentsSupported ( ) ) ;
setupUi ( ) ;
}
}
void AttachmentsEdit : : clear ( )
{
m_model - > repealSelection ( ) ;
}
void AttachmentsEdit : : restore ( )
{
m_model - > revert ( ) ;
}
void AttachmentsEdit : : apply ( )
{
m_model - > submit ( ) ;
}
void AttachmentsEdit : : invalidate ( )
{
// clear UI status
// TODO
// re-setup UI
setupUi ( ) ;
}
2015-10-06 22:41:02 +02:00
void AttachmentsEdit : : addFile ( const QString & path )
2015-04-22 19:33:53 +02:00
{
2015-10-06 22:41:02 +02:00
if ( fileInfo ( ) & & fileInfo ( ) - > attachmentsParsingStatus ( ) = = ParsingStatus : : Ok & & fileInfo ( ) - > container ( ) ) {
2015-04-22 19:33:53 +02:00
// create and add attachment
auto * attachment = fileInfo ( ) - > container ( ) - > createAttachment ( ) ;
attachment - > setIgnored ( true ) ;
2015-10-06 22:41:02 +02:00
attachment - > setFile ( path . toLocal8Bit ( ) . data ( ) ) ;
2015-04-22 19:33:53 +02:00
m_addedAttachments < < attachment ;
m_model - > addAttachment ( - 1 , attachment , true , path ) ;
} else {
2015-10-06 22:41:02 +02:00
throw Failure ( ) ;
2015-04-22 19:33:53 +02:00
}
}
void AttachmentsEdit : : showFileSelection ( )
{
auto path = QFileDialog : : getOpenFileName ( this , tr ( " Select a file to attach " ) ) ;
if ( ! path . isEmpty ( ) ) {
2015-10-06 22:41:02 +02:00
try {
addFile ( path ) ;
} catch ( const Failure & ) {
QMessageBox : : warning ( this , QApplication : : applicationName ( ) , tr ( " The file couldn't be added because the attachments of the file could not be parsed successfully. " ) ) ;
2016-06-14 22:54:49 +02:00
} catch ( . . . ) {
: : IoUtilities : : catchIoFailure ( ) ;
2015-10-06 22:41:02 +02:00
QMessageBox : : warning ( this , QApplication : : applicationName ( ) , tr ( " The file couldn't be added because an IO error occured. " ) ) ;
2015-04-22 19:33:53 +02:00
}
}
}
void AttachmentsEdit : : extractSelected ( )
{
auto selectedRows = m_ui - > treeView - > selectionModel ( ) - > selectedRows ( ) ;
if ( selectedRows . size ( ) = = 1 ) {
if ( auto * attachment = m_model - > attachment ( selectedRows . front ( ) ) ) {
if ( attachment - > data ( ) & & attachment - > data ( ) - > size ( ) ) {
auto fileName = QFileDialog : : getSaveFileName ( this , tr ( " Select where to store the extracted file " ) ) ;
if ( ! fileName . isEmpty ( ) ) {
auto * data = attachment - > data ( ) ;
auto & input = attachment - > data ( ) - > stream ( ) ;
fstream file ;
file . exceptions ( ios_base : : badbit | ios_base : : failbit ) ;
try {
input . seekg ( data - > startOffset ( ) ) ;
file . open ( fileName . toLocal8Bit ( ) . data ( ) , ios_base : : out | ios_base : : binary ) ;
CopyHelper < 0x1000 > helper ;
helper . copy ( input , file , data - > size ( ) ) ;
2016-06-14 22:54:49 +02:00
} catch ( . . . ) {
: : IoUtilities : : catchIoFailure ( ) ;
2015-04-22 19:33:53 +02:00
QMessageBox : : warning ( this , QApplication : : applicationName ( ) , tr ( " An IO error occured when extracting the attached file. " ) ) ;
}
}
} else {
}
return ;
}
}
QMessageBox : : warning ( this , QApplication : : applicationName ( ) , tr ( " There must be exactly one attachment selected. " ) ) ;
}
void AttachmentsEdit : : selectionChanged ( const QModelIndex & current , const QModelIndex & )
{
m_ui - > extractPushButton - > setEnabled ( current . isValid ( ) ) ;
}
void AttachmentsEdit : : setupUi ( )
{
static const QString currentlyAttached ( tr ( " currently attached " ) ) ;
m_model - > setAttachments ( m_currentAttachments , true , currentlyAttached ) ;
}
}