summaryrefslogtreecommitdiffstats
path: root/smb4k/core/smb4kpreviewer.cpp
blob: 68c70d27d3efccd4a42e25ee9eef7fcb49405d87 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/***************************************************************************
    smb4kpreviewer  -  This class queries a remote share for a preview
                             -------------------
    begin                : Mo Mai 28 2007
    copyright            : (C) 2007 by Alexander Reinholdt
    email                : dustpuppy@users.berlios.de
 ***************************************************************************/

/***************************************************************************
 *   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.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful, but   *
 *   WITHOUT ANY WARRANTY; without even the implied warranty of            *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
 *   General Public License for more details.                              *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,   *
 *   MA  02110-1301 USA                                                    *
 ***************************************************************************/

// TQt includes
#include <tqapplication.h>

// KDE includes
#include <tdeapplication.h>
#include <kdebug.h>

// application specific includes
#include "smb4kpreviewer.h"
#include "smb4kpreviewitem.h"
#include "smb4kdefs.h"
#include "smb4tdeglobal.h"
#include "smb4kpasswordhandler.h"
#include "smb4kauthinfo.h"
#include "smb4ksambaoptionshandler.h"
#include "smb4kerror.h"

using namespace Smb4TDEGlobal;


Smb4KPreviewer::Smb4KPreviewer( TQObject *parent, const char *name )
: TQObject( parent, name )
{
  m_item = NULL;

  m_buffer = TQString();

  m_working = false;

  m_proc = new TDEProcess( this, "PreviewProcess" );
  m_proc->setUseShell( true );

  connect( m_proc, TQ_SIGNAL( receivedStdout( TDEProcess *, char *, int ) ),
           this,   TQ_SLOT( slotReceivedStdout( TDEProcess *, char *, int ) ) );

  connect( m_proc, TQ_SIGNAL( processExited( TDEProcess* ) ),
           this,   TQ_SLOT( slotProcessExited( TDEProcess * ) ) );

  connect( m_proc, TQ_SIGNAL( receivedStderr( TDEProcess *, char *, int ) ),
           this,   TQ_SLOT( slotReceivedStderr( TDEProcess *, char *, int ) ) );
}


Smb4KPreviewer::~Smb4KPreviewer()
{
  // Do not delete m_item here, because it belongs to an
  // outside class.
}


bool Smb4KPreviewer::preview( Smb4KPreviewItem *item )
{
   // If there is no item, stop right here.
  if ( !item )
  {
    return false;
  }

  if ( TQString::compare( item->share(), "homes" ) == 0 )
  {
    TQString share_name = specifyUser( item->host(), kapp->mainWidget() ? kapp->mainWidget() : 0, "SpecifyUser" );

    if ( !share_name.isEmpty() )
    {
      // The Smb4KPreviewItem::setShare() function will take care
      // that no share name is overwritten, that is *not* named
      // 'homes'.
      item->setShare( share_name );
    }
    else
    {
      return false;
    }
  }

  m_timer_id = startTimer( TIMER_INTERVAL );

  m_queue.enqueue( item );

  return true;
}


void Smb4KPreviewer::abort()
{
  m_queue.clear();

  if ( m_proc->isRunning() )
  {
    m_proc->kill();
  }
}


void Smb4KPreviewer::timerEvent( TQTimerEvent * )
{
  if ( m_working )
  {
    return;
  }

  // Declare the previewer working:
  emit state( PREVIEWER_START );

  m_working = true;

  m_item = m_queue.dequeue();

  // Assemble the command.
  //
  // Here are some things to remember:
  // (a) Do not convert the path to local 8 bit. It won't work with umlauts or other
  //     special characters.
  // (b) Do not pass the path unquoted, or you'll get a NT_STATUS_OBJECT_NAME_NOT_FOUND
  //     error message in the case the path is empty.
  TQString command;
  command.append( TQString( "smbclient //%1/%2 " ).arg( TDEProcess::quote( m_item->host() ), TDEProcess::quote( m_item->share() ) ) );
  command.append( TQString( " -d1 -W %1 -D %2 " ).arg( TDEProcess::quote( m_item->workgroup() ), TDEProcess::quote( m_item->path() ) ) );
  command.append( " -c \"ls\" " );

  if ( !m_item->ip().isEmpty() )
  {
    command.append( TQString( " -I %1 " ).arg( m_item->ip() ) );
  }

  command.append( optionsHandler()->smbclientOptions( "//"+m_item->host()+"/"+m_item->share() ) );

  Smb4KAuthInfo *auth = passwordHandler()->readAuth( new Smb4KAuthInfo( m_item->workgroup(), m_item->host(), m_item->share() ) );

  if ( !auth->user().isEmpty() )
  {
    command.append( TQString( " -U %1" ).arg( TDEProcess::quote( auth->user() ) ) );

    if ( !auth->password().isEmpty() )
    {
      m_proc->setEnvironment( "PASSWD", auth->password() );
    }
  }
  else
  {
    command.append( " -U guest%" );
  }

  delete auth;

  *m_proc << command;

  TQApplication::setOverrideCursor( waitCursor );

  m_proc->start( TDEProcess::NotifyOnExit, TDEProcess::AllOutput );
}


/////////////////////////////////////////////////////////////////////////////
// SLOT IMPLEMENTATIONS
/////////////////////////////////////////////////////////////////////////////

void Smb4KPreviewer::slotReceivedStdout( TDEProcess *, char *buf, int len )
{
  m_buffer.append( TQString::fromLocal8Bit( buf, len ) );
}


void Smb4KPreviewer::slotReceivedStderr( TDEProcess *, char  *buf, int len )
{
  m_buffer.append( TQString::fromLocal8Bit( buf, len ) );
}


void Smb4KPreviewer::slotProcessExited( TDEProcess * )
{
  // Disconnect the timer:
  if ( m_queue.isEmpty() )
  {
    killTimer( m_timer_id );
  }

  m_proc->clearArguments();

  TQStringList list = TQStringList::split( "\n", m_buffer, false );

  m_buffer = TQString();

  // Check whether an error occurred:
  if ( list.grep( "NT_STATUS" ).count() != 0 )
  {
    // Something went wrong. Let's check if this "only" an
    // authentication issue or if we have to error out:

    TQString error_code = list.grep( "NT_STATUS" ).first().stripWhiteSpace().section( " ", 0, 0 );

    // The error output of smbclient is a little bit inconsistent:
    if ( error_code.contains( "NT_STATUS" ) == 0 )
    {
      error_code = list.grep( "NT_STATUS" ).first().stripWhiteSpace().section( " ", -1, -1 );
    }

    // Authentication issue?
    if ( TQString::compare( error_code, "NT_STATUS_ACCESS_DENIED" ) == 0 ||
         TQString::compare( error_code, "NT_STATUS_LOGON_FAILURE" ) == 0 )
    {
      int state = Smb4KPasswordHandler::None;

      if ( TQString::compare( error_code, "NT_STATUS_ACCESS_DENIED" ) == 0 )
      {
        state = Smb4KPasswordHandler::AccessDenied;
      }
      else if ( TQString::compare( error_code, "NT_STATUS_LOGON_FAILURE" ) == 0 )
      {
        state = Smb4KPasswordHandler::LogonFailure;
      }

      if ( passwordHandler()->askpass( m_item->workgroup(), m_item->host(),
                                       m_item->share(), state,
                                       kapp->mainWidget() ? kapp->mainWidget() : 0,
                                       "AskPass" ) )
      {
        // Now we have a password. Retry.
        // NOTE: Since the item is appended to the queue, there might
        // be the case, that another preview is generated before the
        // retry is executed. I think, we can live with that.
        preview( m_item );
      }
      else
      {
        // The user cancelled the askpass dialog. We won't show an
        // error dialog here, but will only clear the contents of
        // the preview item and emit the failed() signal.
        m_item->clearContents();

        emit failed();
      }
    }
    else
    {
      // OK, error out. We cannot recover from it:
      Smb4KError::error( ERROR_GETTING_PREVIEW, TQString(), m_buffer );

      m_item->clearContents();

      emit failed();
    }
  }
  else if ( list.grep( "Connection to" ).count() != 0 ||
            (list.grep( "Error returning browse list:" ).count() != 0 &&
            list.grep( "NT_STATUS" ).count() == 0) )
  {
    // These are errors that we cannot work around. Error out.
    Smb4KError::error( ERROR_GETTING_PREVIEW, TQString(), m_buffer );

    m_item->clearContents();

    emit failed();
  }
  else
  {
    for ( TQStringList::Iterator it = list.begin(); it != list.end(); ++it )
    {
      if ( (*it).stripWhiteSpace().startsWith( "Domain" ) ||
          (*it).stripWhiteSpace().startsWith( "OS" ) ||
          (*it).stripWhiteSpace().startsWith( "Anonymous" ) )
      {
        continue;
      }
      else if ( (*it).contains( "blocks of size" ) != 0 )
      {
        continue;
      }
      else
      {
        TQString tmp = (*it).stripWhiteSpace().section( " ", 0, -9 ).stripWhiteSpace();

        TQString item = tmp.section( "  ", 0, -2 ).stripWhiteSpace();

        if ( !item.isEmpty() && tmp.section( "  ", -1, -1 ).contains( "D" ) != 0 )
        {
          // We have a directory here.
          if ( item.startsWith( "." ) &&
               (TQString::compare( item.stripWhiteSpace(), "." ) != 0 &&
               TQString::compare( item.stripWhiteSpace(), ".." ) != 0) )
          {
            m_item->addContents( ContentsItem( Smb4KPreviewItem::HiddenDirectory, item ) );
          }
          else
          {
            m_item->addContents( ContentsItem( Smb4KPreviewItem::Directory, item ) );
          }

          continue;
        }
        else if ( item.isEmpty() || tmp.section( "  ", -1, -1 ).contains( "D" ) == 0 )
        {
          // We have a file
          if ( item.isEmpty() )
          {
            if ( tmp.startsWith( "." ) )
            {
              m_item->addContents( ContentsItem( Smb4KPreviewItem::HiddenFile, tmp ) );
            }
            else
            {
              m_item->addContents( ContentsItem( Smb4KPreviewItem::File, tmp ) );
            }
          }
          else
          {
            if ( item.startsWith( "." ) )
            {
              m_item->addContents( ContentsItem( Smb4KPreviewItem::HiddenFile, item ) );
            }
            else
            {
              m_item->addContents( ContentsItem( Smb4KPreviewItem::File, item ) );
            }
          }

          continue;
        }
        else
        {
          continue;
        }
      }
    }
  }

  emit result( m_item );

  TQApplication::restoreOverrideCursor();

  m_working = false;

  emit state( PREVIEWER_STOP );
}

#include "smb4kpreviewer.moc"