~ubuntu-branches/ubuntu/vivid/smb4k/vivid

« back to all changes in this revision

Viewing changes to core/smb4kpreviewer_p.cpp

  • Committer: Package Import Robot
  • Author(s): Mark Purcell
  • Date: 2014-06-15 16:27:38 UTC
  • mto: This revision was merged to the branch mainline in revision 33.
  • Revision ID: package-import@ubuntu.com-20140615162738-t1426an8s5beix1b
Tags: upstream-1.1.2
ImportĀ upstreamĀ versionĀ 1.1.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
    smb4kpreviewer_p  -  Private helper classes for Smb4KPreviewer class.
3
3
                             -------------------
4
4
    begin                : So Dez 21 2008
5
 
    copyright            : (C) 2008-2011 by Alexander Reinholdt
 
5
    copyright            : (C) 2008-2013 by Alexander Reinholdt
6
6
    email                : alexander.reinholdt@kdemail.net
7
7
 ***************************************************************************/
8
8
 
19
19
 *                                                                         *
20
20
 *   You should have received a copy of the GNU General Public License     *
21
21
 *   along with this program; if not, write to the                         *
22
 
 *   Free Software Foundation, 51 Franklin Street, Suite 500, Boston,      *
 
22
 *   Free Software Foundation, Inc., 51 Franklin Street, Suite 500, Boston,*
23
23
 *   MA 02110-1335, USA                                                    *
24
24
 ***************************************************************************/
25
25
 
 
26
#ifdef HAVE_CONFIG_H
 
27
#include <config.h>
 
28
#endif
 
29
 
 
30
// application specific includes
 
31
#include "smb4kpreviewer_p.h"
 
32
#include "smb4knotification.h"
 
33
#include "smb4khost.h"
 
34
#include "smb4kshare.h"
 
35
#include "smb4kglobal.h"
 
36
#include "smb4ksettings.h"
 
37
#include "smb4kcustomoptionsmanager.h"
 
38
#include "smb4kcustomoptions.h"
 
39
 
26
40
// Qt includes
27
 
#include <QTimer>
28
 
#include <QGridLayout>
29
 
#include <QDateTime>
 
41
#include <QtCore/QTimer>
 
42
#include <QtCore/QDateTime>
 
43
#include <QtCore/QLatin1String>
 
44
#include <QtGui/QGridLayout>
30
45
 
31
46
// KDE includes
32
47
#include <kdebug.h>
37
52
#include <kstatusbar.h>
38
53
#include <kshell.h>
39
54
 
40
 
// application specific includes
41
 
#include <smb4kpreviewer_p.h>
42
 
#include <smb4knotification.h>
43
 
#include <smb4khost.h>
44
 
#include <smb4kshare.h>
45
 
#include <smb4kglobal.h>
46
 
#include <smb4ksettings.h>
47
 
#include <smb4kcustomoptionsmanager.h>
48
 
#include <smb4kcustomoptions.h>
49
 
 
50
55
using namespace Smb4KGlobal;
51
56
 
52
57
 
64
69
void Smb4KPreviewJob::start()
65
70
{
66
71
  m_started = true;
67
 
  QTimer::singleShot( 0, this, SLOT( slotStartPreview() ) );
 
72
  QTimer::singleShot( 0, this, SLOT(slotStartPreview()) );
68
73
}
69
74
 
70
75
 
71
 
void Smb4KPreviewJob::setupPreview( Smb4KShare *share, const QUrl &url, QWidget *parent )
 
76
void Smb4KPreviewJob::setupPreview( Smb4KShare *share, const KUrl &url, QWidget *parent )
72
77
{
73
78
  Q_ASSERT( share );
74
79
  m_share = share;
113
118
  emit aboutToStart( m_share, m_url );
114
119
 
115
120
  // Get the path that has to be listed.
116
 
  QString path = m_url.toString( QUrl::RemoveScheme|QUrl::RemoveUserInfo|QUrl::RemovePort );
 
121
  QString path = m_url.path();
117
122
  
118
123
  if ( !m_share->isHomesShare() )
119
124
  {
120
 
    path.remove( m_share->unc(), Qt::CaseInsensitive );
 
125
    path.remove( m_share->shareName(), Qt::CaseInsensitive ); 
121
126
  }
122
127
  else
123
128
  {
124
 
    path.remove( m_share->homeUNC(), Qt::CaseInsensitive );
 
129
    path.remove( m_share->login(), Qt::CaseInsensitive );
125
130
  }
126
 
 
 
131
  
127
132
  // Compile the command line arguments
128
133
  QStringList arguments;
129
134
  
337
342
  m_proc->setEnv( "PASSWD", m_share->password(), true );
338
343
  m_proc->setShellCommand( arguments.join( " " ) );
339
344
 
340
 
  connect( m_proc, SIGNAL( readyReadStandardOutput() ), SLOT( slotReadStandardOutput() ) );
341
 
  connect( m_proc, SIGNAL( readyReadStandardError() ),  SLOT( slotReadStandardError() ) );
342
 
  connect( m_proc, SIGNAL( finished( int, QProcess::ExitStatus ) ), SLOT( slotProcessFinished( int, QProcess::ExitStatus ) ) );
 
345
  connect( m_proc, SIGNAL(readyReadStandardOutput()), SLOT(slotReadStandardOutput()) );
 
346
  connect( m_proc, SIGNAL(readyReadStandardError()),  SLOT(slotReadStandardError()) );
 
347
  connect( m_proc, SIGNAL(finished(int,QProcess::ExitStatus)), SLOT(slotProcessFinished(int,QProcess::ExitStatus)) );
343
348
 
344
349
  m_proc->start();
345
350
}
347
352
 
348
353
void Smb4KPreviewJob::slotReadStandardOutput()
349
354
{
350
 
  QStringList list = QString::fromUtf8( m_proc->readAllStandardOutput(), -1 ).split( "\n", QString::SkipEmptyParts );
351
 
  QList<Item> items;
352
 
 
353
 
  foreach ( const QString &line, list )
354
 
  {
355
 
    if ( line.contains( "blocks of size" ) || line.contains( "Domain=[" ) )
356
 
    {
357
 
      continue;
358
 
    }
359
 
    else if ( line.contains( "NT_STATUS_ACCESS_DENIED", Qt::CaseSensitive ) ||
360
 
              line.contains( "NT_STATUS_LOGON_FAILURE", Qt::CaseSensitive ) )
361
 
    {
362
 
      // This might happen if a directory cannot be accessed due to missing
363
 
      // read permissions.
364
 
      emit authError( this );
365
 
      break;
366
 
    }
367
 
    else
368
 
    {
369
 
      QString entry = line;
370
 
      
371
 
      QString left = entry.trimmed().section( "     ", 0, -2 ).trimmed();
372
 
      QString right = entry.remove( left );
373
 
 
374
 
      QString name = left.section( "  ", 0, -2 ).trimmed().isEmpty() ?
375
 
                     left :
376
 
                     left.section( "  ", 0, -2 ).trimmed();
377
 
 
378
 
      QString dir_string = left.right( 3 ).trimmed();
379
 
      bool is_dir = (!dir_string.isEmpty() && dir_string.contains( "D" ));
380
 
 
381
 
      QString tmp_size = right.trimmed().section( "  ", 0, 0 ).trimmed();
382
 
      QString size;
383
 
 
384
 
      if ( tmp_size[0].isLetter() )
385
 
      {
386
 
        size = right.trimmed().section( "  ", 1, 1 ).trimmed();
387
 
      }
388
 
      else
389
 
      {
390
 
        size = tmp_size;
391
 
      }
392
 
 
393
 
      QString date = QDateTime::fromString( right.section( QString( " %1 " ).arg( size ), 1, 1 ).trimmed() ).toString();
394
 
 
395
 
      if ( !name.isEmpty() )
396
 
      {
397
 
        Item item;
398
 
 
399
 
        if ( is_dir )
400
 
        {
401
 
          if ( name.startsWith( "." ) &&
402
 
              (QString::compare( name, "." ) != 0 && QString::compare( name, ".." ) != 0) )
403
 
          {
404
 
            item.first = HiddenDirectoryItem;
405
 
          }
406
 
          else
407
 
          {
408
 
            item.first = DirectoryItem;
409
 
          }
410
 
        }
411
 
        else
412
 
        {
413
 
          if ( name.startsWith( "." ) )
414
 
          {
415
 
            item.first = HiddenFileItem;
416
 
          }
417
 
          else
418
 
          {
419
 
            item.first = FileItem;
420
 
          }
421
 
        }
422
 
 
423
 
        item.second["name"] = name;
424
 
        item.second["size"] = size;
425
 
        item.second["date"] = date;
426
 
 
427
 
        items << item;
428
 
      }
429
 
      else
 
355
  QString stdout = QString::fromUtf8( m_proc->readAllStandardOutput(), -1 );
 
356
  
 
357
  if ( stdout.contains( "NT_STATUS_ACCESS_DENIED", Qt::CaseSensitive ) || 
 
358
       stdout.contains( "NT_STATUS_LOGON_FAILURE", Qt::CaseSensitive ) )
 
359
  {
 
360
    // This might happen if a directory cannot be accessed due to missing
 
361
    // read permissions.
 
362
    emit authError( this );
 
363
  }
 
364
  else
 
365
  {
 
366
    QStringList list = stdout.split( '\n', QString::SkipEmptyParts );
 
367
    QList<Item> items;
 
368
 
 
369
    foreach ( const QString &line, list )
 
370
    {
 
371
      if ( line.contains( "blocks of size" ) || line.contains( "Domain=[" ) )
430
372
      {
431
373
        continue;
432
374
      }
 
375
      else
 
376
      {
 
377
        QString entry = line;
 
378
        
 
379
        QString left = entry.trimmed().section( "     ", 0, -2 ).trimmed();
 
380
        QString right = entry.remove( left );
 
381
 
 
382
        QString name = left.section( "  ", 0, -2 ).trimmed().isEmpty() ?
 
383
                       left :
 
384
                       left.section( "  ", 0, -2 ).trimmed();
 
385
 
 
386
        QString dir_string = left.right( 3 ).trimmed();
 
387
        bool is_dir = (!dir_string.isEmpty() && dir_string.contains( "D" ));
 
388
 
 
389
        QString tmp_size = right.trimmed().section( "  ", 0, 0 ).trimmed();
 
390
        QString size;
 
391
 
 
392
        if ( tmp_size[0].isLetter() )
 
393
        {
 
394
          size = right.trimmed().section( "  ", 1, 1 ).trimmed();
 
395
        }
 
396
        else
 
397
        {
 
398
          size = tmp_size;
 
399
        }
 
400
 
 
401
        QString date = QDateTime::fromString( right.section( QString( " %1 " ).arg( size ), 1, 1 ).trimmed() ).toString();
 
402
 
 
403
        if ( !name.isEmpty() )
 
404
        {
 
405
          Item item;
 
406
 
 
407
          if ( is_dir )
 
408
          {
 
409
            if ( name.startsWith( '.' ) &&
 
410
                 (QString::compare( name, "." ) != 0 && QString::compare( name, ".." ) != 0) )
 
411
            {
 
412
              item.first = HiddenDirectoryItem;
 
413
            }
 
414
            else
 
415
            {
 
416
              item.first = DirectoryItem;
 
417
            }
 
418
          }
 
419
          else
 
420
          {
 
421
            if ( name.startsWith( '.' ) )
 
422
            {
 
423
              item.first = HiddenFileItem;
 
424
            }
 
425
            else
 
426
            {
 
427
              item.first = FileItem;
 
428
            }
 
429
          }
 
430
 
 
431
          item.second["name"] = name;
 
432
          item.second["size"] = size;
 
433
          item.second["date"] = date;
 
434
 
 
435
          items << item;
 
436
        }
 
437
        else
 
438
        {
 
439
          continue;
 
440
        }
 
441
      }
433
442
    }
 
443
 
 
444
    emit preview( m_url, items );
434
445
  }
435
 
 
436
 
  emit preview( m_url, items );
437
446
}
438
447
 
439
448
 
459
468
    {
460
469
      it.remove();
461
470
    }
462
 
    else if ( line.trimmed().startsWith( "Ignoring unknown parameter" ) )
 
471
    else if ( line.trimmed().startsWith( QLatin1String( "Ignoring unknown parameter" ) ) )
463
472
    {
464
473
      it.remove();
465
474
    }
561
570
 
562
571
  setupView();
563
572
 
564
 
  connect( this,                   SIGNAL( closeClicked() ),
565
 
           this,                   SLOT( slotCloseClicked() ) );
 
573
  connect( this,                   SIGNAL(closeClicked()),
 
574
           this,                   SLOT(slotCloseClicked()) );
566
575
 
567
576
  setMinimumWidth( sizeHint().width() > 350 ? sizeHint().width() : 350 );
568
577
 
569
578
  KConfigGroup group( Smb4KSettings::self()->config(), "PreviewDialog" );
570
579
  restoreDialogSize( group );
571
580
 
572
 
  QTimer::singleShot( 0, this, SLOT( slotRequestPreview() ) );
 
581
  QTimer::singleShot( 0, this, SLOT(slotRequestPreview()) );
573
582
}
574
583
 
575
584
 
630
639
  layout->addWidget( m_view, 0, 0, 0 );
631
640
  layout->addWidget( toolbar, 1, 0, 0 );
632
641
 
633
 
  connect( toolbar, SIGNAL( actionTriggered( QAction * ) ),
634
 
           this,      SLOT( slotActionTriggered( QAction * ) ) );
635
 
 
636
 
  connect( m_combo,   SIGNAL( activated( const QString & ) ),
637
 
           this,      SLOT( slotItemActivated( const QString & ) ) );
638
 
 
639
 
  connect( m_view,    SIGNAL( executed( QListWidgetItem * ) ),
640
 
           this,      SLOT( slotItemExecuted( QListWidgetItem * ) ) );
641
 
 
642
 
  connect( KGlobalSettings::self(), SIGNAL( iconChanged( int ) ),
643
 
           this,                    SLOT( slotIconSizeChanged( int ) ) );
 
642
  connect( toolbar, SIGNAL(actionTriggered(QAction*)),
 
643
           this,      SLOT(slotActionTriggered(QAction*)) );
 
644
 
 
645
  connect( m_combo,   SIGNAL(activated(QString)),
 
646
           this,      SLOT(slotItemActivated(QString)) );
 
647
 
 
648
  connect( m_view,    SIGNAL(executed(QListWidgetItem*)),
 
649
           this,      SLOT(slotItemExecuted(QListWidgetItem*)) );
 
650
 
 
651
  connect( KGlobalSettings::self(), SIGNAL(iconChanged(int)),
 
652
           this,                    SLOT(slotIconSizeChanged(int)) );
644
653
}
645
654
 
646
655
 
789
798
    }
790
799
    else if ( kaction == m_up )
791
800
    {
792
 
      if ( QString::compare( m_share->unc(), m_url.toString( QUrl::RemoveScheme|QUrl::RemoveUserInfo|QUrl::RemovePort ), Qt::CaseInsensitive ) != 0 )
 
801
      QString test = QString("//%1%2%3")
 
802
                     .arg(m_url.host())
 
803
                     .arg(m_url.path().startsWith('/') ? "" : "/")
 
804
                     .arg(m_url.path(KUrl::RemoveTrailingSlash));
 
805
      
 
806
      if ( QString::compare( m_share->unc(), test, Qt::CaseInsensitive ) != 0 )
793
807
      {
794
808
        // Clear the history
795
809
        m_history.clear();
796
810
 
797
811
        // Adjust the path
 
812
        // FIXME: Use KUrl::upUrl() here when we have found to adjust 
 
813
        // the path
798
814
        QString path = m_url.path();
799
 
        m_url.setPath( path.section( "/", 0, -2 ) );
 
815
        m_url.setPath( path.section( '/', 0, -2 ) );
800
816
 
801
817
        // Request the preview
802
818
        slotRequestPreview();
820
836
 
821
837
void Smb4KPreviewDialog::slotRequestPreview()
822
838
{
823
 
  // Set the current URL in the combo box
824
 
  QString current = m_url.toString( QUrl::RemoveScheme|QUrl::RemoveUserInfo|QUrl::RemovePort ).replace( m_url.host(), m_share->hostName() );
825
 
 
 
839
  // Set the current item
 
840
  KUrl u = m_url;
 
841
  u.setProtocol( QString() );
 
842
  u.setUserInfo( QString() );
 
843
  u.setPort( -1 );
 
844
  
 
845
  QString current = u.url().replace( u.host(), u.host().toUpper() );
 
846
    
826
847
  // Set the history
827
848
  QStringList history;
828
849
 
849
870
}
850
871
 
851
872
 
852
 
void Smb4KPreviewDialog::slotDisplayPreview( const QUrl &url, const QList<Item> &contents )
 
873
void Smb4KPreviewDialog::slotDisplayPreview( const KUrl &url, const QList<Item> &contents )
853
874
{
854
875
  if ( m_url != url )
855
876
  {
934
955
  m_forward->setEnabled( enable_forward );
935
956
 
936
957
  // Enable/disable the up action.
937
 
  bool enable_up = (QString::compare( m_share->unc(),
938
 
                                      m_url.toString( QUrl::RemoveScheme|QUrl::RemoveUserInfo|QUrl::RemovePort ),
939
 
                                      Qt::CaseInsensitive ) != 0);
 
958
  QString test = QString("//%1%2%3")
 
959
                 .arg(m_url.host())
 
960
                 .arg(m_url.path().startsWith('/') ? "" : "/")
 
961
                 .arg(m_url.path(KUrl::RemoveTrailingSlash));
 
962
  bool enable_up = (QString::compare( m_share->unc(), test, Qt::CaseInsensitive ) != 0);
940
963
  m_up->setEnabled( enable_up );
941
964
}
942
965
 
943
966
 
944
 
void Smb4KPreviewDialog::slotAboutToStart( Smb4KShare *share, const QUrl &url )
 
967
void Smb4KPreviewDialog::slotAboutToStart( Smb4KShare *share, const KUrl &url )
945
968
{
946
969
  if ( share == m_share && url == m_url )
947
970
  {
955
978
}
956
979
 
957
980
 
958
 
void Smb4KPreviewDialog::slotFinished( Smb4KShare *share, const QUrl &url )
 
981
void Smb4KPreviewDialog::slotFinished( Smb4KShare *share, const KUrl &url )
959
982
{
960
983
  if ( share == m_share && url == m_url )
961
984
  {
1012
1035
    // Clear the history
1013
1036
    m_history.clear();
1014
1037
 
1015
 
    m_url.setPath( QUrl( item ).path() );
 
1038
    KUrl u;
 
1039
    u.setUrl( item, KUrl::TolerantMode );
 
1040
    u.setProtocol( "smb" );
 
1041
    
 
1042
    m_url.setPath( u.path() );
1016
1043
    slotRequestPreview();
1017
1044
  }
1018
1045
  else
1048
1075
}
1049
1076
 
1050
1077
 
1051
 
Smb4KPreviewerPrivate::Smb4KPreviewerPrivate()
1052
 
{
1053
 
}
1054
 
 
1055
 
 
1056
 
Smb4KPreviewerPrivate::~Smb4KPreviewerPrivate()
1057
 
{
1058
 
}
1059
 
 
1060
1078
#include "smb4kpreviewer_p.moc"
1061
1079