2
This file is part of KTnef.
4
Copyright (C) 2002 Michael Goffioul <kdeprint@swing.be>
5
Copyright (c) 2012 Allen Winter <winter@kde.org>
7
This program is free software; you can redistribute it and/or modify
8
it under the terms of the GNU General Public License as published by
9
the Free Software Foundation; either version 2 of the License, or
10
(at your option) any later version.
12
You should have received a copy of the GNU General Public License
13
along with this program; if not, write to the Free Software Foundation,
14
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
17
#include "ktnefmain.h"
18
#include "attachpropertydialog.h"
19
#include "ktnefview.h"
20
#include "messagepropertydialog.h"
22
#include <KTNEF/KTNEFAttach>
23
#include <KTNEF/KTNEFMessage>
24
#include <KTNEF/KTNEFParser>
25
#include <KTNEF/KTNEFProperty>
28
#include <KActionCollection>
29
#include <KApplication>
32
#include <KEditToolBar>
33
#include <KFileDialog>
35
#include <KIconLoader>
38
#include <KMessageBox>
41
#include <KShortcutsDialog>
42
#include <KStandardAction>
43
#include <KStandardDirs>
45
#include <KTemporaryFile>
47
#include <QContextMenuEvent>
53
KTNEFMain::KTNEFMain( QWidget *parent )
54
: KXmlGuiWindow( parent )
61
KConfigGroup config( KGlobal::config(), "Settings" );
62
mDefaultDir = config.readPathEntry( "defaultdir", "/tmp/" );
63
mLastDir = mDefaultDir;
65
// create personale temo extract dir
66
KStandardDirs::makeDir( KGlobal::dirs()->localkdedir() + "/share/apps/ktnef/tmp" );
70
setStandardToolBarMenuEnabled( true );
72
createStandardStatusBarAction();
74
setupGUI( Keys | Save | Create, "ktnefui.rc" );
76
setAutoSaveSettings();
79
KTNEFMain::~KTNEFMain()
85
void KTNEFMain::setupActions()
87
KStandardAction::quit( this, SLOT(close()), actionCollection() );
90
KStandardAction::keyBindings( this, SLOT(slotConfigureKeys()), actionCollection() );
92
i18nc( "@info:whatsthis",
93
"You will be presented with a dialog where you can configure "
94
"the application-wide shortcuts." ) );
96
KStandardAction::configureToolbars( this, SLOT(slotEditToolbars()), actionCollection() );
99
KStandardAction::open( this, SLOT(openFile()), actionCollection() );
102
KAction *openAction = actionCollection()->addAction( "view_file" );
103
openAction->setText( i18nc( "@action:inmenu", "View" ) );
104
openAction->setIcon( KIcon( "document-open" ) );
105
connect( openAction, SIGNAL(triggered()), this, SLOT(viewFile()) );
107
KAction *openAsAction = actionCollection()->addAction( "view_file_as" );
108
openAsAction->setText( i18nc( "@action:inmenu", "View With..." ) );
109
connect( openAsAction, SIGNAL(triggered()), this, SLOT(viewFileAs()) );
111
KAction *extractAction = actionCollection()->addAction( "extract_file" );
112
extractAction->setText( i18nc( "@action:inmenu", "Extract" ) );
113
connect( extractAction, SIGNAL(triggered()), this, SLOT(extractFile()) );
115
KAction *extractToAction = actionCollection()->addAction( "extract_file_to" );
116
extractToAction->setText( i18nc( "@action:inmenu", "Extract To..." ) );
117
extractToAction->setIcon( KIcon( "archive-extract" ) );
118
connect( extractToAction, SIGNAL(triggered()), this, SLOT(extractFileTo()) );
120
KAction *extractAllToAction = actionCollection()->addAction( "extract_all_files" );
121
extractAllToAction->setText( i18nc( "@action:inmenu", "Extract All To..." ) );
122
extractAllToAction->setIcon( KIcon( "archive-extract" ) );
123
connect( extractAllToAction, SIGNAL(triggered()), this, SLOT(extractAllFiles()) );
125
KAction *filePropsAction = actionCollection()->addAction( "properties_file" );
126
filePropsAction->setText( i18nc( "@action:inmenu", "Properties" ) );
127
filePropsAction->setIcon( KIcon( "document-properties" ) );
128
connect( filePropsAction, SIGNAL(triggered()), this, SLOT(propertiesFile()));
130
KAction *messPropsAction = actionCollection()->addAction( "msg_properties" );
131
messPropsAction->setText( i18nc( "@action:inmenu", "Message Properties" ) );
132
connect( messPropsAction, SIGNAL(triggered()), this, SLOT(slotShowMessageProperties()) );
134
KAction *messShowAction = actionCollection()->addAction( "msg_text" );
135
messShowAction->setText( i18nc( "@action:inmenu", "Show Message Text" ) );
136
messShowAction->setIcon( KIcon( "document-preview-archive" ) );
137
connect( messShowAction, SIGNAL(triggered()), this, SLOT(slotShowMessageText()) );
139
KAction *messSaveAction = actionCollection()->addAction( "msg_save" );
140
messSaveAction->setText( i18nc( "@action:inmenu", "Save Message Text As..." ) );
141
messSaveAction->setIcon( KIcon( "document-save" ) );
142
connect( messSaveAction, SIGNAL(triggered()), this, SLOT(slotSaveMessageText()) );
144
actionCollection()->action( "view_file" )->setEnabled( false );
145
actionCollection()->action( "view_file_as" )->setEnabled( false );
146
actionCollection()->action( "extract_file" )->setEnabled( false );
147
actionCollection()->action( "extract_file_to" )->setEnabled( false );
148
actionCollection()->action( "extract_all_files" )->setEnabled( false );
149
actionCollection()->action( "properties_file" )->setEnabled( false );
152
KAction *defFolderAction = actionCollection()->addAction( "options_default_dir" );
153
defFolderAction->setText( i18nc( "@action:inmenu", "Default Folder..." ) );
154
defFolderAction->setIcon( KIcon( "folder-open" ) );
155
connect( defFolderAction, SIGNAL(triggered()), this, SLOT(optionDefaultDir()) );
159
void KTNEFMain::slotConfigureKeys()
161
KShortcutsDialog::configure( actionCollection(), KShortcutsEditor::LetterShortcutsAllowed, this );
164
void KTNEFMain::setupStatusbar()
166
statusBar()->insertItem( i18nc( "@info:status", "100 attachments found" ), 0 );
167
statusBar()->changeItem( i18nc( "@info:status", "No file loaded" ), 0 );
170
void KTNEFMain::setupTNEF()
172
mView = new KTNEFView( this );
173
mView->setAllColumnsShowFocus( true );
174
mParser = new KTNEFParser;
176
setCentralWidget(mView);
178
connect( mView, SIGNAL(itemSelectionChanged()),
179
SLOT(viewSelectionChanged()) );
181
connect( mView, SIGNAL(itemDoubleClicked(QTreeWidgetItem*,int)),
182
SLOT(viewDoubleClicked(QTreeWidgetItem*)) );
184
//PORTME: connect( mView, SIGNAL(dragRequested(QList<KTNEFAttach*>)),
185
//PORTME: SLOT(viewDragRequested(QList<KTNEFAttach*>)) );
188
void KTNEFMain::loadFile( const QString &filename )
190
mFilename = filename;
191
setCaption(mFilename);
192
if ( !mParser->openFile( filename ) ) {
196
"Unable to open file \"%1\".", filename ) );
197
mView->setAttachments( QList<KTNEFAttach *>() );
198
enableExtractAll( false );
200
QList<KTNEFAttach *> list = mParser->message()->attachmentList();
202
msg = i18ncp( "@info:status",
203
"%1 attachment found", "%1 attachments found", list.count() );
204
statusBar()->changeItem( msg, 0 );
205
mView->setAttachments( list );
206
enableExtractAll( ( list.count() > 0 ) );
207
enableSingleAction( false );
211
void KTNEFMain::openFile()
214
KFileDialog::getOpenFileName(
218
i18nc( "@title:window", "Open TNEF File" ) );
219
if ( !filename.isEmpty() ) {
220
loadFile( filename );
224
void KTNEFMain::viewFile()
226
if ( !mView->getSelection().isEmpty() ) {
227
KTNEFAttach *attach = mView->getSelection().first();
228
KUrl url( "file:" + extractTemp( attach ) );
229
QString mimename( attach->mimeTag() );
231
if ( mimename.isEmpty() || mimename == "application/octet-stream" ) {
232
kDebug() << "No mime type found in attachment object, trying to guess...";
233
mimename = KMimeType::findByUrl( url, 0, true )->name();
234
kDebug() << "Detected mime type: " << mimename;
236
kDebug() << "Mime type from attachment object: " << mimename;
239
KRun::runUrl( url, mimename, this, true );
241
KMessageBox::information(
244
"There is no file selected. Please select a file an try again." ) );
249
QString KTNEFMain::extractTemp( KTNEFAttach *att )
251
QString dir = KGlobal::dirs()->localkdedir() + "/share/apps/ktnef/tmp/";
252
mParser->extractFileTo( att->name(), dir );
253
dir.append( att->name() );
257
void KTNEFMain::viewFileAs()
259
if ( !mView->getSelection().isEmpty() ) {
261
list.append( KUrl( extractTemp( mView->getSelection().first() ) ) );
263
if ( !list.isEmpty() ) {
264
KRun::displayOpenWithDialog( list, this );
267
KMessageBox::information(
270
"There is no file selected. Please select a file an try again." ) );
274
void KTNEFMain::extractFile()
276
extractTo( mDefaultDir );
279
void KTNEFMain::extractFileTo()
281
QString dir = KFileDialog::getExistingDirectory( mLastDir, this );
282
if ( !dir.isEmpty() ) {
288
void KTNEFMain::extractAllFiles()
290
QString dir = KFileDialog::getExistingDirectory( mLastDir, this );
291
if ( !dir.isEmpty() ) {
294
QList<KTNEFAttach *> list = mParser->message()->attachmentList();
295
QList<KTNEFAttach *>::ConstIterator it;
296
QList<KTNEFAttach *>::ConstIterator end( list.constEnd() );
297
for ( it = list.constBegin(); it != end; ++it ) {
298
if ( !mParser->extractFileTo( (*it)->name(), dir ) ) {
302
"Unable to extract file \"%1\".", (*it)->name() ) );
309
void KTNEFMain::propertiesFile()
311
KTNEFAttach *attach = mView->getSelection().first();
312
AttachPropertyDialog dlg( this );
313
dlg.setAttachment( attach );
317
void KTNEFMain::optionDefaultDir()
319
const QString dirname = KFileDialog::getExistingDirectory( mDefaultDir, this );
320
if ( !dirname.isEmpty() ) {
321
mDefaultDir = dirname;
323
KConfigGroup config( KGlobal::config(), "Settings" );
324
config.writePathEntry( "defaultdir", mDefaultDir );
328
void KTNEFMain::viewSelectionChanged()
330
QList<KTNEFAttach *> list = mView->getSelection();
331
bool on1 = ( list.count() == 1 );
332
bool on2 = ( list.count() > 0 );
334
actionCollection()->action( "view_file" )->setEnabled( on1 );
335
actionCollection()->action( "view_file_as" )->setEnabled( on1 );
336
actionCollection()->action( "properties_file" )->setEnabled( on1 );
338
actionCollection()->action( "extract_file" )->setEnabled( on2 );
339
actionCollection()->action( "extract_file_to" )->setEnabled( on2 );
342
void KTNEFMain::enableExtractAll( bool on )
345
enableSingleAction( false );
348
actionCollection()->action( "extract_all_files" )->setEnabled( on );
352
void KTNEFMain::enableSingleAction( bool on )
354
actionCollection()->action( "extract_file" )->setEnabled( on );
355
actionCollection()->action( "extract_file_to" )->setEnabled( on );
356
actionCollection()->action( "view_file" )->setEnabled( on );
357
actionCollection()->action( "view_file_as" )->setEnabled( on );
358
actionCollection()->action( "properties_file" )->setEnabled( on );
361
void KTNEFMain::cleanup()
363
QDir d( KGlobal::dirs()->localkdedir() + "/share/apps/ktnef/tmp/" );
364
QFileInfoList list = d.entryInfoList( QDir::Files | QDir::Hidden, QDir::Unsorted );
365
QFileInfoList::iterator it;
366
for ( it = list.begin(); it != list.end(); ++it ) {
367
d.remove( it->absoluteFilePath() );
371
void KTNEFMain::extractTo( const QString &dirname )
373
QString dir = dirname;
374
if ( dir.right(1) != "/" ) {
377
QList<KTNEFAttach *>list = mView->getSelection();
378
QList<KTNEFAttach *>::ConstIterator it;
379
QList<KTNEFAttach *>::ConstIterator end( list.constEnd() );
380
for ( it = list.constBegin(); it != end; ++it ) {
381
if ( !mParser->extractFileTo( (*it)->name(), dir ) ) {
385
"Unable to extract file \"%1\".", (*it)->name() ) );
391
/* This breaks the saveMainWindowSettings stuff....
392
void KTNEFMain::closeEvent(QCloseEvent *e)
397
void KTNEFMain::contextMenuEvent( QContextMenuEvent *event )
399
QList<KTNEFAttach *> list = mView->getSelection();
400
if ( !list.count() ) {
405
QAction *viewWith = 0;
407
KMenu *menu = new KMenu();
408
if ( list.count() == 1 ) {
409
view = menu->addAction( KIcon( "document-open" ),
410
i18nc( "@action:inmenu", "View" ) );
411
viewWith = menu->addAction( i18nc( "@action:inmenu", "View With..." ) );
412
menu->addSeparator();
414
QAction *extract = menu->addAction( i18nc( "@action:inmenu", "Extract" ) );
415
QAction *extractTo = menu->addAction( KIcon( "archive-extract" ),
416
i18nc( "@action:inmenu", "Extract To..." ) );
417
if ( list.count() == 1 ) {
418
menu->addSeparator();
419
prop = menu->addAction( KIcon( "document-properties" ),
420
i18nc( "@action:inmenu", "Properties" ) );
423
QAction *a = menu->exec( event->globalPos(), 0 );
427
} else if ( a == viewWith ) {
429
} else if ( a == extract ) {
431
} else if ( a == extractTo ) {
433
} else if ( a == prop ) {
440
void KTNEFMain::viewDoubleClicked( QTreeWidgetItem *item )
442
if ( item && item->isSelected() ) {
447
void KTNEFMain::viewDragRequested( const QList<KTNEFAttach *>& list )
450
QList<KTNEFAttach *>::ConstIterator end( list.constEnd() );
451
for ( QList<KTNEFAttach *>::ConstIterator it = list.constBegin();
453
urlList << KUrl( extractTemp( *it ) );
456
if ( !list.isEmpty() ) {
457
QMimeData *mimeData = new QMimeData;
458
urlList.populateMimeData( mimeData );
460
QDrag *drag = new QDrag( this );
461
drag->setMimeData( mimeData );
465
void KTNEFMain::slotEditToolbars()
467
saveMainWindowSettings( KGlobal::config()->group( "MainWindow" ) );
469
KEditToolBar dlg( factory() );
470
connect( &dlg, SIGNAL(newToolBarConfig()), this, SLOT(newToolbarConfig()) );
474
void KTNEFMain::slotNewToolbarConfig()
476
createGUI( "ktnefui.rc" );
477
applyMainWindowSettings( KGlobal::config()->group( "MainWindow" ) );
480
void KTNEFMain::slotShowMessageProperties()
482
MessagePropertyDialog dlg( this, mParser->message() );
486
void KTNEFMain::slotShowMessageText()
488
if ( !mParser->message() ) {
492
QString rtf = mParser->message()->rtfString();
493
if ( !rtf.isEmpty() ) {
494
KTemporaryFile *tmpFile = new KTemporaryFile();
495
tmpFile->setPrefix( KGlobal::dirs()->localkdedir() + "/share/apps/ktnef/tmp/" );
496
tmpFile->setSuffix( QLatin1String( ".rtf" ) );
498
tmpFile->setPermissions( QFile::ReadUser );
499
tmpFile->write( rtf.toLocal8Bit() );
502
KRun::runUrl( KUrl( tmpFile->fileName() ), "text/rtf", this, true );
508
"The message does not contain any Rich Text data." ) );
512
void KTNEFMain::slotSaveMessageText()
514
if ( !mParser->message() ) {
518
QString rtf = mParser->message()->rtfString();
519
QString filename = KFileDialog::getSaveFileName( QString(), QString(), this );
520
if ( !filename.isEmpty() ) {
522
if ( f.open( QIODevice::WriteOnly ) ) {
529
"Unable to open file \"%1\" for writing, check file permissions.", filename ) );
534
#include "ktnefmain.moc"