summaryrefslogtreecommitdiffstats
path: root/smb4k/core/smb4khomesshareshandler.cpp
blob: 0b0bea0d95b45dc774ceacfd2612e6a9c846496c (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
/***************************************************************************
    smb4khomesshareshandler  -  This class handles the homes shares.
                             -------------------
    begin                : Do Aug 10 2006
    copyright            : (C) 2006 by Alexander Reinholdt
    email                : dustpuppy@mail.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 <tqmap.h>
#include <layout.h>
#include <tqlabel.h>
#include <tqfile.h>

// KDE includes
#include <kdebug.h>
#include <kstandarddirs.h>
#include <klocale.h>
#include <kiconloader.h>
#include <kcombobox.h>

// application specific includes
#include "smb4khomesshareshandler.h"
#include "smb4kdefs.h"
#include "smb4kerror.h"


Smb4KHomesSharesHandler::Smb4KHomesSharesHandler( TQObject *parent, const char *name )
: TQObject( parent, name )
{
  // First we need the directory.
  KStandardDirs *stddir = new KStandardDirs();
  TQString dir = locateLocal( "data", "smb4k", KGlobal::instance() );

  if ( !stddir->exists( dir ) )
  {
    stddir->makeDir( dir );
  }

  delete stddir;

  m_dlg = NULL;
}


Smb4KHomesSharesHandler::~Smb4KHomesSharesHandler()
{
  delete m_dlg;
}


const TQString Smb4KHomesSharesHandler::specifyUser( const TQString &host, TQWidget *parent, const char *name )
{
  TQString username = TQString();

  m_dlg = new KDialogBase( KDialogBase::Plain, i18n( "Specify User" ), KDialogBase::User1|KDialogBase::Ok|KDialogBase::Cancel, KDialogBase::Ok, parent, name, true, true );
  m_dlg->setButtonGuiItem( KDialogBase::User1, KGuiItem( i18n( "Clear List" ), "editdelete", 0, 0 ) );
  m_dlg->enableButton( KDialogBase::Ok, false );
  m_dlg->enableButton( KDialogBase::User1, false );

  // Set up the ask pass dialog.
  TQFrame *frame = m_dlg->plainPage();
  TQGridLayout *layout = new TQGridLayout( frame );
  layout->setSpacing( 5 );

  TQLabel *pic = new TQLabel( frame );
  pic->setPixmap( DesktopIcon( "personal" ) );
  pic->setMargin( 10 );

  TQLabel *text = new TQLabel( i18n( "Please specify a user name." ), frame );

  TQLabel *userLabel = new TQLabel( i18n( "User:" ), frame );
  KComboBox *userCombo = new KComboBox( true, frame, "UserComboBox" );
  userCombo->setDuplicatesEnabled( false );

  TQSpacerItem *spacer1 = new TQSpacerItem( 10, 10, TQSizePolicy::Expanding, TQSizePolicy::Preferred );

  layout->addWidget( pic, 0, 0, 0 );
  layout->addMultiCellWidget( text, 0, 0, 1, 3, 0 );
  layout->addWidget( userLabel, 1, 0, 0 );
  layout->addMultiCellWidget( userCombo, 1, 1, 1, 4, 0 );
  layout->addItem( spacer1, 0, 2 );

  connect( userCombo, TQT_SIGNAL( textChanged( const TQString &) ),
           this,      TQT_SLOT( slotTextChanged( const TQString & ) ) );
  connect( m_dlg,     TQT_SIGNAL( user1Clicked() ),
           this,      TQT_SLOT( slotClearClicked() ) );

  // Read the list of logins, that are already defined
  // for this 'homes' share.
  TQStringList list = read_names( host );

  if ( !list.isEmpty() )
  {
    userCombo->insertStringList( list, -1 );
    m_dlg->enableButton( KDialogBase::User1, true );
  }

  userCombo->setCurrentText( TQString() );

  // Do the last things before showing.
  userCombo->setFocus();
  m_dlg->setFixedSize( m_dlg->sizeHint() );

  if ( m_dlg->exec() == KDialogBase::Accepted )
  {
    // First make sure, the list is cleared:
    list.clear();

    // Write the new list of logins to the config file.
    if ( !userCombo->lineEdit()->text().isEmpty() )
    {
      list.append( userCombo->lineEdit()->text() );
    }

    int index = 0;

    while ( index < userCombo->count() )
    {
      if ( list.find( userCombo->text( index ) ) == list.end() )
      {
        list.append( userCombo->text( index ) );
      }

      index++;
    }

    list.sort();

    write_names( host, list );

    username = userCombo->currentText();
  }

  delete m_dlg;
  m_dlg = NULL;

  return username;
}


const TQStringList &Smb4KHomesSharesHandler::read_names( const TQString &host )
{
  // Clear the old contents of this list:
  m_names.clear();

  TQFile file( locateLocal( "data", "smb4k/homes_shares", KGlobal::instance() ) );

  if ( file.open( IO_ReadOnly ) )
  {
    TQTextStream ts( &file );
    ts.setEncoding( TQTextStream::Locale );

    TQString line;
    bool get_names = false;

    while ( !ts.atEnd() )
    {
      line = ts.readLine();

      if ( !get_names )
      {
        if ( TQString::compare( line.stripWhiteSpace(), "["+host.upper()+"]" ) == 0 )
        {
          // Found the host:
          get_names = true;

          continue;
        }
        else
        {
          // No match yet...
          continue;
        }
      }
      else
      {
        if ( !line.stripWhiteSpace().isEmpty() )
        {
          // Write the names to the list:
          m_names = TQStringList::split( ",", line, false );

          // This is not needed, but let's do it anyway:
          get_names = false;

          break;
        }
      }
    }

    file.close();
  }
  else
  {
    if ( file.exists() )
    {
      Smb4KError::error( ERROR_READING_FILE, file.name() );

      // The list is empty:
      return m_names;
    }
  }

  return m_names;
}


void Smb4KHomesSharesHandler::write_names( const TQString &host, const TQStringList &names )
{
  // First get the whole contents of the file, so that
  // we can easily modify it:
  TQStringList contents;

  TQFile file( locateLocal( "data", "smb4k/homes_shares", KGlobal::instance() ) );

  if ( file.open( IO_ReadOnly ) )
  {
    TQTextStream ts( &file );
    ts.setEncoding( TQTextStream::Locale );

    contents = TQStringList::split( '\n', ts.read(), true );

    file.close();
  }
  else
  {
    if ( file.exists() )
    {
      Smb4KError::error( ERROR_READING_FILE, file.name() );

      return;
    }
  }

  // Now search for the host:
  TQStringList::Iterator it;

  for ( it = contents.begin(); it != contents.end(); ++it )
  {
    if ( TQString::compare( (*it).stripWhiteSpace().upper(), "["+host.upper()+"]" ) == 0 )
    {
      if ( !names.isEmpty() )
      {
        // Move to the line with the names:
        it++;
        // Change the line:
        *it = names.join( "," );
      }
      else
      {
        // Remove the host entry:
        it = contents.remove( it );
        // Remove the user names:
        it = contents.remove( it );

        // Remove the blank line following the entry:
        if ( it != contents.end() && (*it).stripWhiteSpace().isEmpty() )
        {
          contents.remove( it );
        }
      }

      break;
    }
    else
    {
      continue;
    }
  }

  // If we haven't found the host, append it to the end:
  if ( it == contents.end() )
  {
    if ( !contents.isEmpty() )
    {
      contents.append( "" );
    }

    contents.append( "["+host.upper()+"]");
    contents.append( names.join( "," ) );
  }


  // Now write or remove the file:
  if ( !contents.isEmpty() )
  {
    if ( file.open( IO_WriteOnly ) )
    {
      TQTextStream ts( &file );
      ts.setEncoding( TQTextStream::Locale );

      ts << contents.join( "\n" );

      file.close();
    }
    else
    {
      Smb4KError::error( ERROR_WRITING_FILE, file.name() );

      return;
    }
  }
  else
  {
    file.remove();
  }
}


/////////////////////////////////////////////////////////////////////////////
// TQT_SLOT IMPLEMENTATIONS
/////////////////////////////////////////////////////////////////////////////

void Smb4KHomesSharesHandler::slotTextChanged( const TQString &text )
{
  if ( !text.isEmpty() )
  {
    m_dlg->enableButtonOK( true );
  }
  else
  {
    m_dlg->enableButtonOK( false );
  }
}


void Smb4KHomesSharesHandler::slotClearClicked()
{
  if ( m_dlg )
  {
    KComboBox *cb = (KComboBox *)m_dlg->child( "UserComboBox", "KComboBox", true );

    if ( cb )
    {
      cb->clearEdit();
      cb->clear();

      m_dlg->enableButton( KDialogBase::User1, false );
    }
  }
}

#include "smb4khomesshareshandler.moc"