~ubuntu-branches/ubuntu/raring/kdepim/raring-proposed

« back to all changes in this revision

Viewing changes to ktnef/ktnefmain.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2012-06-07 07:56:38 UTC
  • mfrom: (0.2.27)
  • Revision ID: package-import@ubuntu.com-20120607075638-0luhdq11z7sgvs4m
Tags: 4:4.8.80-0ubuntu1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
  This file is part of KTnef.
 
3
 
 
4
  Copyright (C) 2002 Michael Goffioul <kdeprint@swing.be>
 
5
  Copyright (c) 2012 Allen Winter <winter@kde.org>
 
6
 
 
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.
 
11
 
 
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
 
15
*/
 
16
 
 
17
#include "ktnefmain.h"
 
18
#include "attachpropertydialog.h"
 
19
#include "ktnefview.h"
 
20
#include "messagepropertydialog.h"
 
21
 
 
22
#include <KTNEF/KTNEFAttach>
 
23
#include <KTNEF/KTNEFMessage>
 
24
#include <KTNEF/KTNEFParser>
 
25
#include <KTNEF/KTNEFProperty>
 
26
 
 
27
#include <KAction>
 
28
#include <KActionCollection>
 
29
#include <KApplication>
 
30
#include <KDebug>
 
31
#include <KDialog>
 
32
#include <KEditToolBar>
 
33
#include <KFileDialog>
 
34
#include <KGlobal>
 
35
#include <KIconLoader>
 
36
#include <KLocale>
 
37
#include <KMenu>
 
38
#include <KMessageBox>
 
39
#include <KProcess>
 
40
#include <KRun>
 
41
#include <KShortcutsDialog>
 
42
#include <KStandardAction>
 
43
#include <KStandardDirs>
 
44
#include <KStatusBar>
 
45
#include <KTemporaryFile>
 
46
 
 
47
#include <QContextMenuEvent>
 
48
#include <QDir>
 
49
#include <QDrag>
 
50
#include <QMimeData>
 
51
#include <QPixmap>
 
52
 
 
53
KTNEFMain::KTNEFMain( QWidget *parent )
 
54
  : KXmlGuiWindow( parent )
 
55
{
 
56
  setupActions();
 
57
  setupStatusbar();
 
58
 
 
59
  setupTNEF();
 
60
 
 
61
  KConfigGroup config( KGlobal::config(), "Settings" );
 
62
  mDefaultDir = config.readPathEntry( "defaultdir", "/tmp/" );
 
63
  mLastDir = mDefaultDir;
 
64
 
 
65
  // create personale temo extract dir
 
66
  KStandardDirs::makeDir( KGlobal::dirs()->localkdedir() + "/share/apps/ktnef/tmp" );
 
67
 
 
68
  resize( 430, 350 );
 
69
 
 
70
  setStandardToolBarMenuEnabled( true );
 
71
 
 
72
  createStandardStatusBarAction();
 
73
 
 
74
  setupGUI( Keys | Save | Create, "ktnefui.rc" );
 
75
 
 
76
  setAutoSaveSettings();
 
77
}
 
78
 
 
79
KTNEFMain::~KTNEFMain()
 
80
{
 
81
  delete mParser;
 
82
  cleanup();
 
83
}
 
84
 
 
85
void KTNEFMain::setupActions()
 
86
{
 
87
  KStandardAction::quit( this, SLOT(close()), actionCollection() );
 
88
 
 
89
  KAction *action =
 
90
    KStandardAction::keyBindings( this, SLOT(slotConfigureKeys()), actionCollection() );
 
91
  action->setWhatsThis(
 
92
    i18nc( "@info:whatsthis",
 
93
           "You will be presented with a dialog where you can configure "
 
94
           "the application-wide shortcuts." ) );
 
95
 
 
96
  KStandardAction::configureToolbars( this, SLOT(slotEditToolbars()), actionCollection() );
 
97
 
 
98
  // File menu
 
99
  KStandardAction::open( this, SLOT(openFile()), actionCollection() );
 
100
 
 
101
  // Action menu
 
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()) );
 
106
 
 
107
  KAction *openAsAction = actionCollection()->addAction( "view_file_as" );
 
108
  openAsAction->setText( i18nc( "@action:inmenu", "View With..." ) );
 
109
  connect( openAsAction, SIGNAL(triggered()), this, SLOT(viewFileAs()) );
 
110
 
 
111
  KAction *extractAction = actionCollection()->addAction( "extract_file" );
 
112
  extractAction->setText( i18nc( "@action:inmenu", "Extract" ) );
 
113
  connect( extractAction, SIGNAL(triggered()), this, SLOT(extractFile()) );
 
114
 
 
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()) );
 
119
 
 
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()) );
 
124
 
 
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()));
 
129
 
 
130
  KAction *messPropsAction = actionCollection()->addAction( "msg_properties" );
 
131
  messPropsAction->setText( i18nc( "@action:inmenu", "Message Properties" ) );
 
132
  connect( messPropsAction, SIGNAL(triggered()), this, SLOT(slotShowMessageProperties()) );
 
133
 
 
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()) );
 
138
 
 
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()) );
 
143
 
 
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 );
 
150
 
 
151
  // Options menu
 
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()) );
 
156
 
 
157
}
 
158
 
 
159
void KTNEFMain::slotConfigureKeys()
 
160
{
 
161
  KShortcutsDialog::configure( actionCollection(), KShortcutsEditor::LetterShortcutsAllowed, this );
 
162
}
 
163
 
 
164
void KTNEFMain::setupStatusbar()
 
165
{
 
166
  statusBar()->insertItem( i18nc( "@info:status", "100 attachments found" ), 0 );
 
167
  statusBar()->changeItem( i18nc( "@info:status", "No file loaded" ), 0 );
 
168
}
 
169
 
 
170
void KTNEFMain::setupTNEF()
 
171
{
 
172
  mView = new KTNEFView( this );
 
173
  mView->setAllColumnsShowFocus( true );
 
174
  mParser = new KTNEFParser;
 
175
 
 
176
  setCentralWidget(mView);
 
177
 
 
178
  connect( mView, SIGNAL(itemSelectionChanged()),
 
179
           SLOT(viewSelectionChanged()) );
 
180
 
 
181
  connect( mView, SIGNAL(itemDoubleClicked(QTreeWidgetItem*,int)),
 
182
           SLOT(viewDoubleClicked(QTreeWidgetItem*)) );
 
183
 
 
184
//PORTME:  connect( mView, SIGNAL(dragRequested(QList<KTNEFAttach*>)),
 
185
//PORTME:           SLOT(viewDragRequested(QList<KTNEFAttach*>)) );
 
186
}
 
187
 
 
188
void KTNEFMain::loadFile( const QString &filename )
 
189
{
 
190
  mFilename = filename;
 
191
  setCaption(mFilename);
 
192
  if ( !mParser->openFile( filename ) ) {
 
193
    KMessageBox::error(
 
194
      this,
 
195
      i18nc( "@info",
 
196
             "Unable to open file \"%1\".", filename ) );
 
197
    mView->setAttachments( QList<KTNEFAttach *>() );
 
198
    enableExtractAll( false );
 
199
  } else {
 
200
    QList<KTNEFAttach *> list = mParser->message()->attachmentList();
 
201
    QString msg;
 
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 );
 
208
  }
 
209
}
 
210
 
 
211
void KTNEFMain::openFile()
 
212
{
 
213
  QString filename =
 
214
    KFileDialog::getOpenFileName(
 
215
      KUrl(),
 
216
      QString(),
 
217
      this,
 
218
      i18nc( "@title:window", "Open TNEF File" ) );
 
219
  if ( !filename.isEmpty() ) {
 
220
    loadFile( filename );
 
221
  }
 
222
}
 
223
 
 
224
void KTNEFMain::viewFile()
 
225
{
 
226
  if ( !mView->getSelection().isEmpty() ) {
 
227
    KTNEFAttach *attach = mView->getSelection().first();
 
228
    KUrl url( "file:" + extractTemp( attach ) );
 
229
    QString mimename( attach->mimeTag() );
 
230
 
 
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;
 
235
    } else {
 
236
      kDebug() << "Mime type from attachment object: " << mimename;
 
237
    }
 
238
 
 
239
    KRun::runUrl( url, mimename, this, true );
 
240
  } else {
 
241
    KMessageBox::information(
 
242
      this,
 
243
      i18nc( "@info",
 
244
             "There is no file selected. Please select a file an try again." ) );
 
245
  }
 
246
 
 
247
}
 
248
 
 
249
QString KTNEFMain::extractTemp( KTNEFAttach *att )
 
250
{
 
251
  QString dir = KGlobal::dirs()->localkdedir() + "/share/apps/ktnef/tmp/";
 
252
  mParser->extractFileTo( att->name(), dir );
 
253
  dir.append( att->name() );
 
254
  return dir;
 
255
}
 
256
 
 
257
void KTNEFMain::viewFileAs()
 
258
{
 
259
  if ( !mView->getSelection().isEmpty() ) {
 
260
    KUrl::List list;
 
261
    list.append( KUrl( extractTemp( mView->getSelection().first() ) ) );
 
262
 
 
263
    if ( !list.isEmpty() ) {
 
264
      KRun::displayOpenWithDialog( list, this );
 
265
    }
 
266
  } else {
 
267
    KMessageBox::information(
 
268
      this,
 
269
      i18nc( "@info",
 
270
             "There is no file selected. Please select a file an try again." ) );
 
271
  }
 
272
}
 
273
 
 
274
void KTNEFMain::extractFile()
 
275
{
 
276
  extractTo( mDefaultDir );
 
277
}
 
278
 
 
279
void KTNEFMain::extractFileTo()
 
280
{
 
281
  QString dir = KFileDialog::getExistingDirectory( mLastDir, this );
 
282
  if ( !dir.isEmpty() ) {
 
283
    extractTo( dir );
 
284
    mLastDir = dir;
 
285
  }
 
286
}
 
287
 
 
288
void KTNEFMain::extractAllFiles()
 
289
{
 
290
  QString dir = KFileDialog::getExistingDirectory( mLastDir, this );
 
291
  if ( !dir.isEmpty() ) {
 
292
    mLastDir = dir;
 
293
    dir.append( "/" );
 
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 ) ) {
 
299
        KMessageBox::error(
 
300
          this,
 
301
          i18nc( "@info",
 
302
                 "Unable to extract file \"%1\".", (*it)->name() ) );
 
303
        return;
 
304
      }
 
305
    }
 
306
  }
 
307
}
 
308
 
 
309
void KTNEFMain::propertiesFile()
 
310
{
 
311
  KTNEFAttach *attach = mView->getSelection().first();
 
312
  AttachPropertyDialog dlg( this );
 
313
  dlg.setAttachment( attach );
 
314
  dlg.exec();
 
315
}
 
316
 
 
317
void KTNEFMain::optionDefaultDir()
 
318
{
 
319
  const QString dirname = KFileDialog::getExistingDirectory( mDefaultDir, this );
 
320
  if ( !dirname.isEmpty() ) {
 
321
    mDefaultDir = dirname;
 
322
 
 
323
    KConfigGroup config( KGlobal::config(), "Settings" );
 
324
    config.writePathEntry( "defaultdir", mDefaultDir );
 
325
  }
 
326
}
 
327
 
 
328
void KTNEFMain::viewSelectionChanged()
 
329
{
 
330
  QList<KTNEFAttach *> list = mView->getSelection();
 
331
  bool on1 = ( list.count() == 1 );
 
332
  bool on2 = ( list.count() > 0 );
 
333
 
 
334
  actionCollection()->action( "view_file" )->setEnabled( on1 );
 
335
  actionCollection()->action( "view_file_as" )->setEnabled( on1 );
 
336
  actionCollection()->action( "properties_file" )->setEnabled( on1 );
 
337
 
 
338
  actionCollection()->action( "extract_file" )->setEnabled( on2 );
 
339
  actionCollection()->action( "extract_file_to" )->setEnabled( on2 );
 
340
}
 
341
 
 
342
void KTNEFMain::enableExtractAll( bool on )
 
343
{
 
344
  if ( !on ) {
 
345
    enableSingleAction( false );
 
346
  }
 
347
 
 
348
  actionCollection()->action( "extract_all_files" )->setEnabled( on );
 
349
 
 
350
}
 
351
 
 
352
void KTNEFMain::enableSingleAction( bool on )
 
353
{
 
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 );
 
359
}
 
360
 
 
361
void KTNEFMain::cleanup()
 
362
{
 
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() );
 
368
  }
 
369
}
 
370
 
 
371
void KTNEFMain::extractTo( const QString &dirname )
 
372
{
 
373
  QString dir = dirname;
 
374
  if ( dir.right(1) != "/" ) {
 
375
    dir.append( "/" );
 
376
  }
 
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 ) ) {
 
382
      KMessageBox::error(
 
383
        this,
 
384
        i18nc( "@info",
 
385
               "Unable to extract file \"%1\".", (*it)->name() ) );
 
386
      return;
 
387
    }
 
388
  }
 
389
}
 
390
 
 
391
/* This breaks the saveMainWindowSettings stuff....
 
392
  void KTNEFMain::closeEvent(QCloseEvent *e)
 
393
{
 
394
  e->accept();
 
395
}*/
 
396
 
 
397
void KTNEFMain::contextMenuEvent( QContextMenuEvent *event )
 
398
{
 
399
  QList<KTNEFAttach *> list = mView->getSelection();
 
400
  if ( !list.count() ) {
 
401
    return;
 
402
  }
 
403
 
 
404
  QAction *view = 0;
 
405
  QAction *viewWith = 0;
 
406
  QAction *prop = 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();
 
413
  }
 
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" ) );
 
421
  }
 
422
 
 
423
  QAction *a = menu->exec( event->globalPos(), 0 );
 
424
  if ( a ) {
 
425
    if ( a == view ) {
 
426
      viewFile();
 
427
    } else if ( a == viewWith ) {
 
428
      viewFileAs();
 
429
    } else if ( a == extract ) {
 
430
      extractFile();
 
431
    } else if ( a == extractTo ) {
 
432
      extractFileTo();
 
433
    } else if ( a == prop ) {
 
434
      propertiesFile();
 
435
    }
 
436
  }
 
437
  delete menu;
 
438
}
 
439
 
 
440
void KTNEFMain::viewDoubleClicked( QTreeWidgetItem *item )
 
441
{
 
442
  if ( item && item->isSelected() ) {
 
443
    viewFile();
 
444
  }
 
445
}
 
446
 
 
447
void KTNEFMain::viewDragRequested( const QList<KTNEFAttach *>& list )
 
448
{
 
449
  KUrl::List urlList;
 
450
  QList<KTNEFAttach *>::ConstIterator end( list.constEnd() );
 
451
  for ( QList<KTNEFAttach *>::ConstIterator it = list.constBegin();
 
452
        it != end; ++it ) {
 
453
    urlList << KUrl( extractTemp( *it ) );
 
454
  }
 
455
 
 
456
  if ( !list.isEmpty() ) {
 
457
    QMimeData *mimeData = new QMimeData;
 
458
    urlList.populateMimeData( mimeData );
 
459
 
 
460
    QDrag *drag = new QDrag( this );
 
461
    drag->setMimeData( mimeData );
 
462
  }
 
463
}
 
464
 
 
465
void KTNEFMain::slotEditToolbars()
 
466
{
 
467
  saveMainWindowSettings( KGlobal::config()->group( "MainWindow" ) );
 
468
 
 
469
  KEditToolBar dlg( factory() );
 
470
  connect( &dlg, SIGNAL(newToolBarConfig()), this, SLOT(newToolbarConfig()) );
 
471
  dlg.exec();
 
472
}
 
473
 
 
474
void KTNEFMain::slotNewToolbarConfig()
 
475
{
 
476
  createGUI( "ktnefui.rc" );
 
477
  applyMainWindowSettings( KGlobal::config()->group( "MainWindow" ) );
 
478
}
 
479
 
 
480
void KTNEFMain::slotShowMessageProperties()
 
481
{
 
482
  MessagePropertyDialog dlg( this, mParser->message() );
 
483
  dlg.exec();
 
484
}
 
485
 
 
486
void KTNEFMain::slotShowMessageText()
 
487
{
 
488
  if ( !mParser->message() ) {
 
489
    return;
 
490
  }
 
491
 
 
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" ) );
 
497
    tmpFile->open();
 
498
    tmpFile->setPermissions( QFile::ReadUser );
 
499
    tmpFile->write( rtf.toLocal8Bit() );
 
500
    tmpFile->close();
 
501
 
 
502
    KRun::runUrl( KUrl( tmpFile->fileName() ), "text/rtf", this, true );
 
503
    delete tmpFile;
 
504
  } else {
 
505
      KMessageBox::error(
 
506
        this,
 
507
        i18nc( "@info",
 
508
               "The message does not contain any Rich Text data." ) );
 
509
  }
 
510
}
 
511
 
 
512
void KTNEFMain::slotSaveMessageText()
 
513
{
 
514
  if ( !mParser->message() ) {
 
515
    return;
 
516
  }
 
517
 
 
518
  QString rtf = mParser->message()->rtfString();
 
519
  QString filename = KFileDialog::getSaveFileName( QString(), QString(), this );
 
520
  if ( !filename.isEmpty() ) {
 
521
    QFile f( filename );
 
522
    if ( f.open( QIODevice::WriteOnly ) ) {
 
523
      QTextStream t( &f );
 
524
      t << rtf;
 
525
    } else {
 
526
      KMessageBox::error(
 
527
        this,
 
528
        i18nc( "@info",
 
529
               "Unable to open file \"%1\" for writing, check file permissions.", filename ) );
 
530
    }
 
531
  }
 
532
}
 
533
 
 
534
#include "ktnefmain.moc"