summaryrefslogtreecommitdiffstats
path: root/smb4k/core/smb4kmounter.cpp
blob: 68eccaf9315e6a78021022385037eddebaeaeae4 (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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
/***************************************************************************
    smb4kmounter.cpp  -  The core class that mounts the shares.
                             -------------------
    begin                : Die Jun 10 2003
    copyright            : (C) 2003-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>
#include <tqdir.h>
#include <tqtextstream.h>
#include <tqfile.h>
#include <tqtextstream.h>

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

// system includes
#if !defined(__FreeBSD__) && !defined(__solaris__) && !defined(USE_SOLARIS)
#include <sys/statfs.h>
#elif defined(__solaris__) || defined(USE_SOLARIS)
#include <sys/statvfs.h>
#elif defined(__FreeBSD__)
#include <sys/param.h>
#include <sys/mount.h>
#endif
#include <sys/types.h>
#include <pwd.h>
#include <unistd.h>
#include <errno.h>
#include <dirent.h>

#ifdef __FreeBSD__
#include <sys/param.h>
#include <sys/ucred.h>
#include <sys/mount.h>
#include <tqfileinfo.h>
#endif

// Application specific includes
#include "smb4kmounter.h"
#include "smb4kmounter_p.h"
#include "smb4kauthinfo.h"
#include "smb4ksambaoptionsinfo.h"
#include "smb4kerror.h"
#include "smb4tdeglobal.h"
#include "smb4ksambaoptionshandler.h"
#include "smb4kpasswordhandler.h"
#include "smb4kshare.h"
#include "smb4ksettings.h"

using namespace Smb4TDEGlobal;


Smb4KMounter::Smb4KMounter( TQObject *parent, const char *name ) : TQObject( parent, name )
{
  m_priv = new Smb4KMounterPrivate;

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

  m_working = false;

  m_queue.setAutoDelete( true );

  connect( m_proc,  TQT_SIGNAL( processExited( TDEProcess * ) ),
           this,    TQT_SLOT( slotProcessExited( TDEProcess * ) ) );

  connect( m_proc,  TQT_SIGNAL( receivedStdout( TDEProcess *, char *, int ) ),
           this,    TQT_SLOT( slotReceivedStdout( TDEProcess *, char *, int ) ) );

  connect( m_proc,  TQT_SIGNAL( receivedStderr( TDEProcess *, char *, int ) ),
           this,    TQT_SLOT( slotReceivedStderr( TDEProcess *, char *, int ) ) );

  connect( kapp,    TQT_SIGNAL( shutDown() ),
           this,    TQT_SLOT( slotShutdown() ) );
}


Smb4KMounter::~Smb4KMounter()
{
  abort();

  for ( TQValueList<Smb4KShare *>::Iterator it = m_mounted_shares.begin(); it != m_mounted_shares.end(); ++it )
  {
    delete *it;
  }

  m_mounted_shares.clear();

  delete m_priv;
}


void Smb4KMounter::init()
{
  m_queue.enqueue( new TQString( TQString( "%1:" ).arg( Import ) ) );
  m_queue.enqueue( new TQString( TQString( "%1:" ).arg( Remount ) ) );

  startTimer( TIMER_INTERVAL );
}


/***************************************************************************
   Aborts any action of the mounter.
***************************************************************************/


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

  if ( m_proc->isRunning() )
  {
    if ( Smb4KSettings::alwaysUseSuperUser() )
    {
      TQString suid_program;

      switch( Smb4KSettings::superUserProgram() )
      {
        case Smb4KSettings::EnumSuperUserProgram::Sudo:
        {
          suid_program = Smb4KSettings::sudo();

          break;
        }
        case Smb4KSettings::EnumSuperUserProgram::Super:
        {
          suid_program = Smb4KSettings::super();

          break;
        }
        default:
        {
          // FIXME: Throw an error?
          return;
        }
      }

      TDEProcess proc;
      proc.setUseShell( true );
      proc << TQString( "%1 smb4k_kill %2" ).arg( suid_program ).arg( m_proc->pid() );
      proc.start( TDEProcess::DontCare, TDEProcess::NoCommunication );
    }
    else
    {
      m_proc->kill();
    }
  }
}


/***************************************************************************
   Mounts recently used shares.
***************************************************************************/


void Smb4KMounter::remount()
{
  if ( Smb4KSettings::remountShares() )
  {
    const TQValueList<Smb4KSambaOptionsInfo *> *list = &(optionsHandler()->customOptionsList());

    for ( TQValueList<Smb4KSambaOptionsInfo *>::ConstIterator it = list->begin();
          it != list->end(); ++it )
    {
      if ( (*it)->remount() )
      {
        TQValueList<Smb4KShare> list = findShareByName( (*it)->itemName() );

        bool mount = true;

        if ( !list.isEmpty() )
        {
          for ( TQValueList<Smb4KShare>::ConstIterator it = list.begin(); it != list.end(); ++it )
          {
            if ( !(*it).isForeign() )
            {
              mount = false;

              break;
            }
            else
            {
              continue;
            }
          }
        }

        if ( mount )
        {
#ifndef __FreeBSD__
          mountShare( TQString(), (*it)->itemName().section( "/", 2, 2 ), TQString(), (*it)->itemName().section( "/", 3, 3 ) );
#else
          mountShare( TQString(), (*it)->itemName().section( "/", 2, 2 ).section( "@", 1, 1 ), TQString(), (*it)->itemName().section( "/", 3, 3 ) );
#endif
        }

        // If the share is to be remounted the next time,
        // slotShutdown() will tell the options handler.
        (*it)->setRemount( false );

        continue;
      }
      else
      {
        continue;
      }
    }

    m_working = false;
    emit state( MOUNTER_STOP );
  }
  else
  {
    m_working = false;
    emit state( MOUNTER_STOP );
  }
}


/***************************************************************************
   Imports all shares, that are mounted externally.
***************************************************************************/

void Smb4KMounter::import()
{
  TQValueList<Smb4KShare *> shares;

#ifndef __FreeBSD__

  if ( m_proc_mounts.name().isEmpty() )
  {
    m_proc_mounts.setName( "/proc/mounts" );
  }

  if ( !TQFile::exists( m_proc_mounts.name() ) )
  {
    if ( !m_proc_error )
    {
      m_proc_error = true;
      Smb4KError::error( ERROR_FILE_NOT_FOUND, m_proc_mounts.name() );
    }
    else
    {
      // No need to do anything here
    }
  }
  else
  {
    TQStringList contents, list;

    // Read /proc/mounts:

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

      contents = TQStringList::split( "\n", ts.read(), false );

      m_proc_mounts.close();
    }
    else
    {
      Smb4KError::error( ERROR_OPENING_FILE, m_proc_mounts.name() );

      return;
    }

    // Process the SMBFS and CIFS entries:

    list += contents.grep( " smbfs ", true );
    list += contents.grep( " cifs ", true );

    if ( !list.isEmpty() )
    {
      for ( TQStringList::Iterator it = list.begin(); it != list.end(); it++ )
      {
        Smb4KShare *new_share = NULL;

        if ( (*it).contains( " smbfs ", false ) != 0 )
        {
          TQString share_and_path = (*it).section( " smbfs ", 0, 0 ).stripWhiteSpace();
          TQString name = share_and_path.section( " ", 0, 0 ).stripWhiteSpace().replace( "\\040", "\040" );
          TQString path = share_and_path.section( " ", 1, 1 ).stripWhiteSpace();

          if ( path.contains( "\\040" ) != 0 || path.contains( "\040" ) != 0 )
          {
            name.replace( "_", "\040" );
            path.replace( "\\040", "\040" );
          }

          int uid = (*it).section( "uid=", 1, 1 ).section( ",", 0, 0 ).stripWhiteSpace().toInt();
          int gid = (*it).section( "gid=", 1, 1 ).section( ",", 0, 0 ).stripWhiteSpace().toInt();

          new_share = new Smb4KShare( name, path, "smbfs", uid, gid );
        }
        else if ( (*it).contains( " cifs ", false ) != 0 )
        {
          TQString share_and_path = (*it).section( " cifs ", 0, 0 ).stripWhiteSpace();
          TQString name = share_and_path.section( " ", 0, 0 ).stripWhiteSpace().replace( "\\040", "\040" );
          TQString path = share_and_path.section( " ", 1, 1 ).stripWhiteSpace();

          if ( path.contains( "\\040" ) != 0 || path.contains( "\040" ) != 0 )
          {
            name.replace( "_", "\040" );
            path.replace( "\\040", "\040" );
          }

          TQString login = (*it).section( "username=", 1, 1 ).section( ",", 0, 0 ).stripWhiteSpace();

          new_share = new Smb4KShare( name, path, "cifs", login );
        }
        else
        {
          continue;
        }

        if ( new_share )
        {
          // If the a representative of the new share is already in the list of
          // mounted shares, replace the new with the old one.

          Smb4KShare *existing_share = findShareByPath( new_share->path() );

          if ( existing_share )
          {
            delete new_share;
            new_share = new Smb4KShare( *existing_share );
          }

          // Check if the share is broken and/or foreign.

          if ( (existing_share && !existing_share->isBroken()) || !existing_share )
          {
            checkAccessibility( new_share );
          }
          else
          {
            // Since new_share is a copy of existing_share, we do not need to do
            // anything here.
          }

          if ( !existing_share && TQString::compare( new_share->filesystem(), "cifs" ) == 0 )
          {
            bool foreign = true;

            if ( (!new_share->isBroken() &&
                 (tqstrncmp( new_share->canonicalPath(),
                            TQDir( Smb4KSettings::mountPrefix() ).canonicalPath(),
                            TQDir( Smb4KSettings::mountPrefix() ).canonicalPath().length() ) == 0 ||
                  tqstrncmp( new_share->canonicalPath(),
                            TQDir::home().canonicalPath(),
                            TQDir::home().canonicalPath().length() ) == 0)) ||
                 (new_share->isBroken() &&
                 (tqstrncmp( new_share->path(),
                            TQDir::homeDirPath(),
                            TQDir::homeDirPath().length() ) == 0 ||
                  tqstrncmp( new_share->path(),
                            Smb4KSettings::mountPrefix(),
                            Smb4KSettings::mountPrefix().length() ) == 0)) )
            {
              foreign = false;
            }

            new_share->setForeign( foreign );
          }

          shares.append( new_share );
        }
      }
    }
  }

#else

  struct statfs *buf;
  int count = getmntinfo( &buf, 0 );

  if ( count == 0 )
  {
    int err_code = errno;

    Smb4KError::error( ERROR_IMPORTING_SHARES, TQString(), strerror( err_code ) );

    m_working = false;
    return;
  }

  for ( int i = 0; i < count; ++i )
  {
    if ( !strcmp( buf[i].f_fstypename, "smbfs" ) )
    {
      TQString share_name( buf[i].f_mntfromname );
      TQString path( buf[i].f_mntonname );
      TQString fs( buf[i].f_fstypename );

      TQFileInfo info( TQString( buf[i].f_mntonname )+"/." );

      int uid = (int)info.ownerId();
      int gid = (int)info.groupId();

      Smb4KShare *existing_share = findShareByPath( path );
      Smb4KShare *new_share = NULL;

      if ( existing_share )
      {
        new_share = new Smb4KShare( *existing_share );
      }
      else
      {
        new_share = new Smb4KShare( share_name, path, fs, uid, gid );
      }

      // Test if share is broken
      if ( (existing_share && !existing_share->isBroken()) || !existing_share )
      {
        checkAccessibility( new_share );
      }
      else
      {
        // Since new_share is a copy of existing_share, we do not need to do
        // anything here.
      }

      shares.append( new_share );
    }
  }

  // Apparently, under FreeBSD we do not need to delete
  // the pointer (see manual page).

#endif

  // Delete all entries of m_mounted_shares.
  for ( TQValueList<Smb4KShare *>::Iterator it = m_mounted_shares.begin(); it != m_mounted_shares.end(); ++it )
  {
    delete *it;
  }

  m_mounted_shares.clear();

  m_mounted_shares = shares;

  emit updated();

  m_working = false;
}


/***************************************************************************
   Mounts a share. (Public part)
***************************************************************************/

void Smb4KMounter::mountShare( const TQString &workgroup, const TQString &host, const TQString &ip, const TQString &share )
{
  TQString share_name = TQString();

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

  if ( !share_name.stripWhiteSpace().isEmpty() )
  {
    // Before doing anything else let's check that the
    // share has not been mounted by the user already:
    TQValueList<Smb4KShare> list = findShareByName( TQString( "//%1/%2" ).arg( host, share_name ) );

    for ( TQValueList<Smb4KShare>::ConstIterator it = list.begin(); it != list.end(); ++it )
    {
      if ( !(*it).isForeign() )
      {
        emit mountedShare( (*it).canonicalPath() );

        return;
      }
    }

    m_queue.enqueue( new TQString( TQString( "%1:%2:%3:%4:%5" ).arg( Mount )
                                                             .arg( workgroup, host )
                                                             .arg( ip, share_name ) ) );
  }
}



/***************************************************************************
   Mounts a share. (Private part)
***************************************************************************/

void Smb4KMounter::mount( const TQString &workgroup, const TQString &host, const TQString &ip, const TQString &share )
{
  m_priv->setWorkgroup( workgroup );
  m_priv->setHost( host );
  m_priv->setShare( share );
  m_priv->setIP( ip );

  // Create the mount point:
  TQDir *dir = new TQDir( Smb4KSettings::mountPrefix() );

  if ( !dir->exists() )
  {
    if ( !dir->mkdir( dir->canonicalPath() ) )
    {
      Smb4KError::error( ERROR_MKDIR_FAILED, dir->path() );
      m_working = false;
      emit state( MOUNTER_STOP );

      return;
    }
  }

  dir->setPath( dir->path() + "/" +
                (Smb4KSettings::forceLowerCaseSubdirs() ?
                m_priv->host().lower() :
                m_priv->host()) );

  if ( !dir->exists() )
  {
    if ( !dir->mkdir( dir->canonicalPath() ) )
    {
      Smb4KError::error( ERROR_MKDIR_FAILED, dir->path() );
      m_working = false;
      emit state( MOUNTER_STOP );

      return;
    }
  }

  dir->setPath( dir->path() + "/" +
                (Smb4KSettings::forceLowerCaseSubdirs() ?
                m_priv->share().lower() :
                m_priv->share()) );

  if ( !dir->exists() )
  {
    if ( !dir->mkdir( dir->canonicalPath() ) )
    {
      Smb4KError::error( ERROR_MKDIR_FAILED, dir->path() );
      m_working = false;
      emit state( MOUNTER_STOP );

      return;
    }
  }

  m_priv->setPath( TQDir::cleanDirPath( dir->path() ) );

  delete dir;

  // Now we are prepared to mount the share:
  TQString command, suid_program;

  switch ( Smb4KSettings::superUserProgram() )
  {
    case Smb4KSettings::EnumSuperUserProgram::Sudo:
    {
      suid_program = "sudo";

      break;
    }
    case Smb4KSettings::EnumSuperUserProgram::Super:
    {
      suid_program = "super";

      break;
    }
    default:
    {
      return;
    }
  }

  Smb4KAuthInfo authInfo( m_priv->workgroup(), m_priv->host(), m_priv->share() );
  (void) passwordHandler()->readAuth( &authInfo );

#ifndef __FreeBSD__

  // Let's see if the options handler knows the share:
  Smb4KSambaOptionsInfo *info = optionsHandler()->findItem( TQString( "//%1/%2" ).arg( m_priv->host(), m_priv->share() ), true );

  // Determine the file system we have to use:
  int filesystem;

  if ( info )
  {
    filesystem = TQString::compare( info->filesystem().lower(), "cifs" ) == 0 ?
                 Smb4KSettings::EnumFilesystem::CIFS :
                 Smb4KSettings::EnumFilesystem::SMBFS;
  }
  else
  {
    filesystem = Smb4KSettings::filesystem();
  }

  // Compile the mount command:
  switch ( filesystem )
  {
    case Smb4KSettings::EnumFilesystem::CIFS:
    {
      command.append( Smb4KSettings::alwaysUseSuperUser() ?   // FIXME: Check if suid program is installed
                      TQString( "%1 smb4k_mount -s -t cifs " ).arg( suid_program ) :
                      "smb4k_mount -n -t cifs " );

      command.append( "-o " );

      command.append( optionsHandler()->mountOptions( TQString( "//%1/%2" ).arg( m_priv->host(), m_priv->share() ) ) );

      command.append( !m_priv->workgroup().stripWhiteSpace().isEmpty() ?
                      TQString( "domain='%1'," ).arg( m_priv->workgroup() ) :
                      "" );

      command.append( !m_priv->ip().stripWhiteSpace().isEmpty() ?
                      TQString( "ip=%1," ).arg( m_priv->ip() ) :
                      "" );

      command.append( !authInfo.user().isEmpty() ?
                      TQString( "user=%1" ).arg( authInfo.user().data() ) :
                      "guest" );

      command.append( " -- " );

      command.append( TQString( "//'%1'/'%2' '%3'" ).arg( m_priv->host(), m_priv->share(), m_priv->path() ) );

      m_priv->setCIFSLogin( !authInfo.user().isEmpty() ?
                      authInfo.user() :
                      "guest" );

      m_priv->setFileSystem( "cifs" );

      break;
    }
    case Smb4KSettings::EnumFilesystem::SMBFS:
    {
      command.append( Smb4KSettings::alwaysUseSuperUser() ?   // FIXME: Check if suid program is installed
                      TQString( "%1 smb4k_mount -s -t smbfs " ).arg( suid_program ) :
                      "smb4k_mount -n -t smbfs " );

      command.append( "-o " );

      command.append( optionsHandler()->mountOptions( TQString( "//%1/%2" ).arg( m_priv->host(), m_priv->share() ) ) );

      command.append( !m_priv->workgroup().stripWhiteSpace().isEmpty() ?
                      TQString( "workgroup='%1'," ).arg( m_priv->workgroup() ) :
                      "" );

      command.append( !m_priv->ip().stripWhiteSpace().isEmpty() ?
                      TQString( "ip=%1," ).arg( m_priv->ip() ) :
                      "" );

      command.append( !authInfo.user().isEmpty() ?
                      TQString( "username=%1" ).arg( authInfo.user().data() ) :
                      "guest" );

      command.append( " -- " );

      command.append( TQString( "//'%1'/'%2' '%3'" ).arg( m_priv->host(), m_priv->share(), m_priv->path() ) );

      m_priv->setFileSystem( "smbfs" );

      break;
    }
    default:
    {
      return;
    }
  }

  m_proc->setEnvironment( "PASSWD", !authInfo.password().isEmpty() ? authInfo.password() : "" );

#else

  Smb4KSambaOptionsInfo *info = optionsHandler()->findItem( "//"+m_priv->host()+"/"+m_priv->share() );

  int port = info && info->port() != -1 ?
             info->port() :
             Smb4KSettings::remotePort();

  command.append( Smb4KSettings::alwaysUseSuperUser() ?   // FIXME: Check if suid program is installed
                  TQString( "%1 smb4k_mount " ).arg( suid_program ) :
                  "smb4k_mount " );

  command.append( optionsHandler()->mountOptions( TQString( "//%1/%2" ).arg( m_priv->host(), m_priv->share() ) ) );

  command.append( !m_priv->workgroup().stripWhiteSpace().isEmpty() ?
                  TQString( " -W '%1'" ).arg( m_priv->workgroup() ) :
                  "" );

  command.append( !m_priv->ip().stripWhiteSpace().isEmpty() ?
                  TQString( " -I %1" ).arg( m_priv->ip() ) :
                  "" );

  command.append( " -N" );

  command.append( " -- " );

  command.append( TQString( "//%1@'%2':%3/'%4' '%5'" ).arg( !authInfo.user().isEmpty() ? authInfo.user() : "guest" )
                                                     .arg( m_priv->host() )
                                                     .arg( port )
                                                     .arg( m_priv->share(), m_priv->path() ) );

#endif

  // Start the mount process:
  *m_proc << command;

  startProcess( Mount );
}


/****************************************************************************
   Unmount a share. (Public part)
****************************************************************************/

void Smb4KMounter::unmountShare( Smb4KShare *share, bool force, bool noMessage )
{
  // Do *not* change share->canonicalPath(). It is necessary for the
  // checks below to work.
  m_queue.enqueue( new TQString( TQString( "%1:%2:%3:%4" ).arg( Unmount )
                                                        .arg( share->canonicalPath().data() )
                                                        .arg( force, noMessage ) ) );
}


/***************************************************************************
   Unmount a share. (Private part)
***************************************************************************/

void Smb4KMounter::unmount( const TQString &mountpoint, bool force, bool noMessage )
{
  // First let's see if all requirements are fullfilled:

  if ( force )
  {
    // Check that the user enabled the "Force Unmounting" ability:
    if ( !Smb4KSettings::useForceUnmount() )
    {
      Smb4KError::error( ERROR_FEATURE_NOT_ENABLED );
      m_working = false;
      emit state( MOUNTER_STOP );

      return;
    }
  }

  // Compose the unmount command:
  if ( !mountpoint.stripWhiteSpace().isEmpty() )
  {
    bool execute = false;

    TQString path = mountpoint;
    m_priv->setPath( path.replace( '\044', "\044" ) );

    TQString suid_program, command;

    if ( Smb4KSettings::useForceUnmount() || Smb4KSettings::alwaysUseSuperUser() )
    {
      switch ( Smb4KSettings::superUserProgram() )
      {
        case Smb4KSettings::EnumSuperUserProgram::Sudo:
        {
          suid_program = Smb4KSettings::sudo();

          break;
        }
        case Smb4KSettings::EnumSuperUserProgram::Super:
        {
          suid_program = Smb4KSettings::super();

          break;
        }
        default:
        {
          // FIXME: Throw an error?
          return;
        }
      }
    }

    Smb4KShare *share = findShareByPath( mountpoint );

    if ( share )
    {
      if ( !share->isForeign() )
      {
        if ( force )
        {
          if ( KMessageBox::questionYesNo( 0, i18n( "Do you really want to force the unmounting of this share?" ), TQString(), KStdGuiItem::yes(), KStdGuiItem::no(), "Dont Ask Forced", KMessageBox::Notify ) == KMessageBox::Yes )
          {
#ifdef __linux__
            command.append( TQString( "%1 smb4k_umount -s -l " ).arg( suid_program ) );
#else
#ifdef __FreeBSD__
            command.append( TQString( "%1 smb4k_umount " ).arg( suid_program ) );
#else
            command.append( TQString( "%1 smb4k_umount -s " ).arg( suid_program ) );
#endif
#endif
            execute = true;
          }
          else
          {
            m_working = false;
            emit state( MOUNTER_STOP );

            return;
          }
        }
        else
        {
          if ( !Smb4KSettings::alwaysUseSuperUser() )
          {
#ifndef __FreeBSD__
            command.append( "smb4k_umount -n " );
#else
            command.append( "smb4k_umount " );
#endif
          }
          else
          {
#ifndef __FreeBSD__
            command.append( TQString( "%1 smb4k_umount -s " ).arg( suid_program ) );
#else
            command.append( TQString( "%1 smb4k_umount " ).arg( suid_program ) );
#endif
          }
        }
      }
      else
      {
        if ( Smb4KSettings::unmountForeignShares() )
        {
          if ( force )
          {
            if ( KMessageBox::questionYesNo( 0, i18n( "Do you really want to force the unmounting of this share?" ), TQString(), KStdGuiItem::yes(), KStdGuiItem::no(), "Dont Ask Forced", KMessageBox::Notify ) == KMessageBox::Yes )
            {
#ifdef __linux__
            command.append( TQString( "%1 smb4k_umount -s -l " ).arg( suid_program ) );
#else
#ifdef __FreeBSD__
            command.append( TQString( "%1 smb4k_umount " ).arg( suid_program ) );
#else
            command.append( TQString( "%1 smb4k_umount -s " ).arg( suid_program ) );
#endif
#endif
              execute = true;
            }
            else
            {
              m_working = false;
              emit state( MOUNTER_STOP );

              return;
            }
          }
          else
          {
            if ( !Smb4KSettings::alwaysUseSuperUser() )
            {
#ifndef __FreeBSD__
              command.append( "smb4k_umount -n " );
#else
              command.append( "smb4k_umount " );
#endif
            }
            else
            {
#ifndef __FreeBSD__
              command.append( TQString( "%1 smb4k_umount -s " ).arg( suid_program ) );
#else
              command.append( TQString( "%1 smb4k_umount " ).arg( suid_program ) );
#endif
            }
          }
        }
        else
        {
          if ( !noMessage )
          {
            Smb4KError::error( ERROR_UNMOUNTING_NOT_ALLOWED );
          }

          m_working = false;
          emit state( MOUNTER_STOP );

          return;
        }
      }

#ifndef __FreeBSD__
      command.append( TQString( "-t %1 " ).arg( share->filesystem() ) );
#endif
      command.append( TQString( "'%1'" ).arg( m_priv->path() ) );

      if ( force && !execute )
      {
        return;
      }

      emit aboutToUnmount( mountpoint );

      *m_proc << command;
      startProcess( Unmount );
    }
    else
    {
      // FIXME: Throw an error?
      return;
    }
  }
  else
  {
    Smb4KError::error( ERROR_MOUNTPOINT_EMPTY );
    m_working = false;
    emit state( MOUNTER_STOP );

    return;
  }
}


/***************************************************************************
   Unmounts all shares at once. (Public part)
***************************************************************************/

void Smb4KMounter::unmountAllShares()
{
  m_queue.enqueue( new TQString( TQString( "%1" ).arg( UnmountAll ) ) );
}


/***************************************************************************
   Unmounts all shares at once.
***************************************************************************/

void Smb4KMounter::unmountAll()
{
  for ( TQValueListIterator<Smb4KShare *> it = m_mounted_shares.begin(); it != m_mounted_shares.end(); ++it )
  {
    unmountShare( *it, false, true );
  }

  m_working = false;
}


/***************************************************************************
   Starts any process.
***************************************************************************/

void Smb4KMounter::startProcess( int state )
{
  m_buffer = TQString();
  m_state = state;

  if ( m_state != Import )
  {
    TQApplication::setOverrideCursor( waitCursor );
  }

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


/***************************************************************************
   Ends any process. This functions tells the mounter what to do
   afterwards.
***************************************************************************/

void Smb4KMounter::endProcess()
{
  switch ( m_state )
  {
    case Mount:
      processMount();
      break;
    case Unmount:
      processUnmount();
      break;
    default:
      break;
  }

  m_state = Idle;

  m_priv->clearData();

  TQApplication::restoreOverrideCursor();
  m_proc->clearArguments();

  m_working = false;
  emit state( MOUNTER_STOP );
}


/***************************************************************************
   Process mounts.
***************************************************************************/

void Smb4KMounter::processMount()
{
  Smb4KShare *share = NULL;

#ifndef __FreeBSD__

  if ( m_proc->normalExit() )
  {
    if ( m_buffer.contains( "smb4k_mount:", true ) == 0 &&
         m_buffer.contains( "failed", true ) == 0 &&
         m_buffer.contains( "ERR", true ) == 0 &&
         m_buffer.contains( "/bin/sh:" ) == 0 &&
         m_buffer.contains( "mount:", true ) == 0 &&
         m_buffer.contains( "smbmnt" ) == 0 &&
         m_buffer.contains( m_priv->path() ) == 0 &&
         m_buffer.contains( "mount error" ) == 0 &&
         m_buffer.contains( "bad user name" ) == 0 &&
         m_buffer.contains( "bad group name" ) == 0 )
    {
      TQString name = TQString( "//%1/%2" ).arg( m_priv->host() ).arg( m_priv->share() );

      // Check file system
#if !defined(__solaris__)
      struct statfs filesystem;
#else
      struct statvfs filesystem;
#endif

#if !defined(__solaris__) && !defined(__irix__)
      if ( statfs( m_priv->path(), &filesystem ) == -1 )
#elif defined(__irix__)
      if ( statfs( m_priv->path(), &filesystem, sizeof( filesystem ), 0 ) == -1 )
#else
      if ( statvfs( m_priv->path(), &filesystem ) == -1 )
#endif
      {
        // The query failed. Go with the file system already defined in m_priv.
        if ( TQString::compare( m_priv->filesystem(), "smbfs" ) == 0 )
        {
          share =  new Smb4KShare( name, m_priv->path(), m_priv->filesystem(), (int)getuid(), (int)getgid() );
          m_mounted_shares.append( share );
        }
        else if ( TQString::compare( m_priv->filesystem(), "cifs" ) == 0 )
        {
          // The user name will be send if no login was specified.
          TQString cifs_login = !m_priv->cifsLogin().isEmpty() ?
                               m_priv->cifsLogin() :
                               getpwuid( getuid() )->pw_name;

          share = new Smb4KShare( name, m_priv->path(), m_priv->filesystem(), cifs_login, false );
          m_mounted_shares.append( share );
        }
      }
      else
      {
#if !defined(__FreeBSD__) && !defined(__solaris__) && !defined(__irix__)
        if ( (uint)filesystem.f_type == 0xFF534D42 /* CIFS */)
        {
          // The user name will be send if no login was specified.
          TQString cifs_login = !m_priv->cifsLogin().isEmpty() ?
                               m_priv->cifsLogin() :
                               getpwuid( getuid() )->pw_name;

          share = new Smb4KShare( name, m_priv->path(), "cifs", cifs_login, false );
          m_mounted_shares.append( share );
        }
        else if ( (uint)filesystem.f_type == 0x517B /* SMBFS */)
        {
          share =  new Smb4KShare( name, m_priv->path(), "smbfs", (int)getuid(), (int)getgid() );
          m_mounted_shares.append( share );
        }
#elif defined(__solaris__)
        if ( (uint)filesystem.f_basetype == 0xFF534D42 /* CIFS */)
        {
          // The user name will be send if no login was specified.
          TQString cifs_login = !m_priv->cifsLogin().isEmpty() ?
                               m_priv->cifsLogin() :
                               getpwuid( getuid() )->pw_name;

          share = new Smb4KShare( name, m_priv->path(), "cifs", cifs_login, false );
          m_mounted_shares.append( share );
        }
        else if ( (uint)filesystem.f_basetype == 0x517B /* SMBFS */)
        {
          share =  new Smb4KShare( name, m_priv->path(), "smbfs", (int)getuid(), (int)getgid() );
          m_mounted_shares.append( share );
        }
#elif defined(__irix__)
        if ( (uint)filesystem.f_fstyp == 0xFF534D42 /* CIFS */)
        {
          // The user name will be send if no login was specified.
          TQString cifs_login = !m_priv->cifsLogin().isEmpty() ?
                               m_priv->cifsLogin() :
                               getpwuid( getuid() )->pw_name;

          share = new Smb4KShare( name, m_priv->path(), "cifs", cifs_login, false );
          m_mounted_shares.append( share );
        }
        else if ( (uint)filesystem.f_basetype == 0x517B && !strncmp( fs, "smbfs", strlen( fs )+1 ) )
        {
          share =  new Smb4KShare( name, m_priv->path(), "smbfs", (int)getuid(), (int)getgid() );
          m_mounted_shares.append( share );
        }
#endif
        else
        {
          // Error... We don't create a share.
        }
      }

      if ( share )
      {
        // Check that the share is accessible:
        checkAccessibility( share );

        emit mountedShare( m_priv->path() );
      }
    }
    else
    {
      if ( m_buffer.contains( "ERRbadpw" ) != 0 ||
           m_buffer.contains( "ERRnoaccess" ) != 0 ||
           m_buffer.contains( "mount error 13 = Permission denied" ) != 0 )
      {
        int state = Smb4KPasswordHandler::None;

        if ( m_buffer.contains( "ERRbadpw" ) != 0 )
        {
          state = Smb4KPasswordHandler::BadPassword;
        }
        else if ( m_buffer.contains( "ERRnoaccess" ) != 0 )
        {
          state = Smb4KPasswordHandler::AccessDenied;
        }
        else if ( m_buffer.contains( "mount error 13 = Permission denied" ) != 0 )
        {
          state = Smb4KPasswordHandler::PermDenied;
        }

        // If the user supplied auth information, we will retry mounting.
        if ( passwordHandler()->askpass( m_priv->workgroup(), m_priv->host(), m_priv->share(), state ) )
        {
          mountShare( m_priv->workgroup(), m_priv->host(), m_priv->ip(), m_priv->share() );
        }
      }
      else if ( m_buffer.contains( "ERRnosuchshare" ) != 0 && m_priv->share().contains( "_" ) != 0 )
      {
        TQString share_string = static_cast<TQString>( m_priv->share() ).replace( "_", " " );
        mountShare( m_priv->workgroup(), m_priv->host(), m_priv->ip(), share_string );
      }
      else
      {
        TQString name = TQString( "//%1/%2" ).arg( m_priv->host() ).arg( m_priv->share() );

        Smb4KError::error( ERROR_MOUNTING_SHARE, name, m_buffer );
      }
    }
  }

#else

  if ( m_proc->normalExit() )
  {
    if ( m_buffer.contains( "smb4k_mount:", true ) == 0 &&
         m_buffer.contains( "syserr =", true ) == 0 &&
         /* To make sure we catch all errors, also check for the following
            strings. Maybe we can remove them?? */
         m_buffer.contains( "Authentication error", true ) == 0 &&
         m_buffer.contains( "Connection refused", true ) == 0 &&
         m_buffer.contains( "Operation not permitted", true ) == 0 )
    {
      import();  // FIXME: *cough* What is this for???

      Smb4KAuthInfo authInfo( m_priv->workgroup(), m_priv->host(), m_priv->share() );
      (void) passwordHandler()->readAuth( &authInfo );

      TQString name = TQString( "//%1@%2/%3" ).arg( authInfo.user().upper(), m_priv->host().upper(), m_priv->share().upper() );

      share = new Smb4KShare( name, m_priv->path(), m_priv->filesystem(), (int)getuid(), (int)getgid() );
      m_mounted_shares.append( share );

      // Check that the share is accessible:
      checkAccessibility( share );

      emit mountedShare( m_priv->path() );
    }
    else
    {
      if ( m_buffer.contains( "Authentication error" ) != 0 )
      {
        // If the user supplied auth information, we will retry mounting.
        if ( passwordHandler()->askpass( m_priv->workgroup(), m_priv->host(), m_priv->share(), Smb4KPasswordHandler::AuthError ) )
        {
          mountShare( m_priv->workgroup(), m_priv->host(), m_priv->ip() , m_priv->share() );
        }
      }
      else
      {
        Smb4KAuthInfo authInfo(  m_priv->workgroup(), m_priv->host(), m_priv->share() );
        (void) passwordHandler()->readAuth( &authInfo );

        TQString name = TQString( "//%1@%2/%3" ).arg( authInfo.user().upper(), m_priv->host().upper(), m_priv->share().upper() );

        Smb4KError::error( ERROR_MOUNTING_SHARE, name, m_buffer );
      }
    }
  }

#endif

  emit updated();
}


/***************************************************************************
   Process unmounts.
***************************************************************************/

void Smb4KMounter::processUnmount()
{
  // Get the share:
  Smb4KShare *share = findShareByPath( m_priv->path() );

  if ( m_proc->normalExit() )
  {
    if ( m_buffer.isEmpty() )
    {
      if ( tqstrncmp( share->canonicalPath(),
                     TQDir( Smb4KSettings::mountPrefix() ).canonicalPath().local8Bit(),
                     TQDir( Smb4KSettings::mountPrefix() ).canonicalPath().local8Bit().length() ) == 0 )
      {
        TQDir dir( share->canonicalPath() );

        if ( dir.rmdir( dir.canonicalPath(), true ) )
        {
          dir.cdUp();
          dir.rmdir( dir.canonicalPath(), true );
        }
      }

      m_mounted_shares.remove(share);
    }
    else
    {
      // If the user's computer is configured by a DHCP server, under
      // rare circumstances it might occur that sudo reports an error,
      // because it is not able to resolve the host. This error message
      // will be removed, because it does not affect the unmounting:
      if ( m_buffer.contains( "sudo: unable to resolve host", true ) != 0 )
      {
        size_t hostnamelen = 255;
        char *hostname = new char[hostnamelen];

        if ( gethostname( hostname, hostnamelen ) == -1 )
        {
          int error_number = errno;
          Smb4KError::error( ERROR_GETTING_HOSTNAME, TQString(), strerror( error_number ) );
        }
        else
        {
          TQString str = TQString( "sudo: unable to resolve host %1\n" ).arg( hostname );

          m_buffer.remove( str, false /* case insensitive */ );

          if ( !m_buffer.isEmpty() )
          {
            Smb4KError::error( ERROR_UNMOUNTING_SHARE, share->name(), m_buffer );
          }
          else
          {
            if ( tqstrncmp( share->canonicalPath(),
                           TQDir( Smb4KSettings::mountPrefix() ).canonicalPath().local8Bit(),
                           TQDir( Smb4KSettings::mountPrefix() ).canonicalPath().local8Bit().length() ) == 0 )
            {
              TQDir dir( share->canonicalPath() );

              if ( dir.rmdir( dir.canonicalPath(), true ) )
              {
                dir.cdUp();
                dir.rmdir( dir.canonicalPath(), true );
              }
            }

            m_mounted_shares.remove(share);
          }
        }

        delete [] hostname;
      }
      else
      {
        Smb4KError::error( ERROR_UNMOUNTING_SHARE, share->name(), m_buffer );
      }
    }
  }

  emit updated();
}


/***************************************************************************
   Check if a share is already mounted
***************************************************************************/

bool Smb4KMounter::isMounted( const TQString &name, bool userOnly )
{
  TQValueList<Smb4KShare> list = findShareByName( name );

  bool mounted = false;

  if ( !list.isEmpty() && userOnly )
  {
    for ( TQValueList<Smb4KShare>::ConstIterator it = list.begin(); it != list.end(); ++it )
    {
      if ( !(*it).isForeign() )
      {
        mounted = true;

        break;
      }
      else
      {
        continue;
      }
    }
  }
  else
  {
    mounted = !list.isEmpty();
  }

  return mounted;
}


/***************************************************************************
   Find a share in the list with its path
***************************************************************************/

Smb4KShare* Smb4KMounter::findShareByPath( const TQString &path )
{
  if ( path.isEmpty() || m_mounted_shares.isEmpty() )
  {
    return NULL;
  }

  Smb4KShare *share = NULL;

  for ( TQValueListIterator<Smb4KShare *> it = m_mounted_shares.begin(); it != m_mounted_shares.end(); ++it )
  {
    if( TQString::compare( path.upper(), TQString(TQString::fromLocal8Bit( (*it)->path(), -1 )).upper() ) == 0 ||
        TQString::compare( path.upper(), TQString(TQString::fromLocal8Bit( (*it)->canonicalPath(), -1 )).upper() ) == 0 )
    {
      share = *it;

      break;
    }
  }

  return share;
}


/***************************************************************************
   Find the list of mounts of a share
***************************************************************************/

TQValueList<Smb4KShare> Smb4KMounter::findShareByName( const TQString &name )
{
  TQValueList<Smb4KShare> list;

  if ( name.isEmpty() || m_mounted_shares.isEmpty() )
  {
    return list;  // is empty
  }

  TQString n = name;

  for ( TQValueListIterator<Smb4KShare *> it = m_mounted_shares.begin(); it != m_mounted_shares.end(); ++it )
  {
    if( TQString::compare( (*it)->name().upper(), name.upper() ) == 0 ||
        TQString::compare( (*it)->name().upper(), n.replace( " ", "_" ).upper() ) == 0 )
    {
      list.append( *(*it) );

      continue;
    }
    else
    {
      continue;
    }
  }

  return list;
}


/***************************************************************************
   Returns a list of mount points that belong to broken shares
***************************************************************************/

const TQValueList<Smb4KShare *> Smb4KMounter::getBrokenShares()
{
  TQValueList<Smb4KShare *> broken_shares;

  for ( TQValueListIterator<Smb4KShare *> it = m_mounted_shares.begin(); it != m_mounted_shares.end(); ++it )
  {
    if ( (*it)->isBroken() )
    {
      broken_shares.append( *it );

      continue;
    }
    else
    {
      continue;
    }
  }

  return broken_shares;
}


void Smb4KMounter::prepareForShutdown()
{
  slotShutdown();
}


void Smb4KMounter::checkAccessibility( Smb4KShare *share )
{
  if ( share )
  {
    m_priv->thread.setMountpoint( share->path() );
    m_priv->thread.start();
    m_priv->thread.wait( THREAD_WAITING_TIME );
    m_priv->thread.terminate();
    m_priv->thread.wait();

    share->setBroken( m_priv->thread.isBroken() );
    share->setTotalDiskSpace( m_priv->thread.totalDiskSpace() );
    share->setFreeDiskSpace( m_priv->thread.freeDiskSpace() );
  }
  else
  {
    // FIXME: Should we throw an error here?
  }
}


void Smb4KMounter::timerEvent( TQTimerEvent * )
{
  if ( !m_working && !m_queue.isEmpty() )
  {
    // Tell the mounter, that it is busy.
    m_working = true;

    TQString *item = m_queue.dequeue();
    int todo = item->section( ":", 0, 0 ).toInt();

    switch ( todo )
    {
      case Remount:
      {
        remount();
        break;
      }
      case Import:
      {
        import();
        break;
      }
      case Mount:
      {
        emit state( MOUNTER_MOUNTING );
        mount( item->section( ":", 1, 1 ), item->section( ":", 2, 2 ), item->section( ":", 3, 3 ), item->section( ":", 4, 4 ) );
        break;
      }
      case Unmount:
      {
        emit state( MOUNTER_UNMOUNTING );
        unmount( item->section( ":", 1, 1 ), (bool)item->section( ":", 2, 2 ).toInt() /* force */, (bool)item->section( ":", 3, 3 ).toInt() /* noMessage */);
        break;
      }
      case UnmountAll:
      {
        unmountAll();
        break;
      }
      default:
      {
        break;
      }
    }

    delete item;
  }

  m_priv->timerTicks++;

  if ( m_priv->timerTicks * timerInterval() >= Smb4KSettings::checkInterval() /* msec */ &&
       (!m_working || m_queue.isEmpty()) )
  {
    m_queue.enqueue( new TQString( TQString( "%1:" ).arg( Import ) ) );
    m_priv->timerTicks = 0;
  }
}


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


void Smb4KMounter::slotProcessExited( TDEProcess * )
{
  endProcess();
}


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


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


void Smb4KMounter::slotShutdown()
{
  // Abort any action:
  abort();

  // Prepare for shutdown:
  if ( Smb4KSettings::remountShares() && !m_mounted_shares.isEmpty() )
  {
    for ( TQValueList<Smb4KShare *>::ConstIterator it = m_mounted_shares.begin(); it != m_mounted_shares.end(); ++it )
    {
      optionsHandler()->remount( *it, !(*it)->isForeign() );
    }
  }

  optionsHandler()->sync();

  TQDir dir;

  dir.cd( Smb4KSettings::mountPrefix() );

  TQStringList dirs = dir.entryList( TQDir::Dirs, TQDir::DefaultSort );

  TQValueList<Smb4KShare *> broken_shares = getBrokenShares();

  for ( TQStringList::ConstIterator it = dirs.begin(); it != dirs.end(); ++it )
  {
    if ( TQString::compare( *it, "." ) != 0 && TQString::compare( *it, ".." ) != 0 )
    {
      bool broken = false;

      for ( TQValueListIterator<Smb4KShare *> bs = broken_shares.begin(); bs != broken_shares.end(); ++bs )
      {
        if ( tqstrncmp( (*bs)->path(),
                       Smb4KSettings::mountPrefix()+*it,
                       (Smb4KSettings::mountPrefix()+*it).length() ) == 0 ||
             tqstrncmp( (*bs)->canonicalPath(),
                       Smb4KSettings::mountPrefix()+*it,
                       (Smb4KSettings::mountPrefix()+*it).length() ) == 0 )
        {
          broken = true;

          break;
        }
        else
        {
          continue;
        }
      }

      if ( !broken )
      {
        dir.cd( *it );

        TQStringList subdirs = dir.entryList( TQDir::Dirs, TQDir::DefaultSort );

        for ( TQStringList::ConstIterator i = subdirs.begin(); i != subdirs.end(); ++i )
        {
          if ( TQString::compare( *i, "." ) != 0 && TQString::compare( *i, ".." ) != 0 )
          {
            dir.rmdir( *i );
          }
        }

        dir.cdUp();
        dir.rmdir( *it );
      }
    }
  }

  broken_shares.clear();

  if ( Smb4KSettings::unmountSharesOnExit() )
  {
    TQString suid_program, command;

    switch( Smb4KSettings::superUserProgram() )
    {
      case Smb4KSettings::EnumSuperUserProgram::Sudo:
      {
        suid_program = Smb4KSettings::sudo();

        break;
      }
      case Smb4KSettings::EnumSuperUserProgram::Super:
      {
        suid_program = Smb4KSettings::super();

        break;
      }
      default:
      {
        // FIXME: Throw an error?
        return;
      }
    }

    TDEProcess proc;
    proc.setUseShell( true );
    proc.detach();

    for ( TQValueListIterator<Smb4KShare *> it = m_mounted_shares.begin(); it != m_mounted_shares.end(); ++it )
    {
      if ( !(*it)->isForeign() )
      {
        if ( Smb4KSettings::alwaysUseSuperUser() )
        {
#ifndef __FreeBSD__
          command.append( TQString( "%1 smb4k_umount -s -t %2 " ).arg( suid_program ).arg( (*it)->filesystem() ) );
#else
          command.append( TQString( "%1 smb4k_umount " ).arg( suid_program ) );
#endif
          command.append( TDEProcess::quote( (*it)->path() ) );
          command.append( " ; " );
        }
        else
        {
#ifndef __FreeBSD__
          command.append( TQString( "smb4k_umount -n -t %1 " ).arg( (*it)->filesystem() ) );
#else
          command.append( "smb4k_umount " );
#endif
          command.append( TDEProcess::quote( (*it)->path() ) );
          command.append( " ; " );
        }

        dir.setPath( (*it)->canonicalPath() );

#ifndef __FreeBSD__
        command.append( "rmdir --ignore-fail-on-non-empty " );
        command.append( TDEProcess::quote( dir.canonicalPath() ) );
        command.append( " ; " );
        command.append( "rmdir --ignore-fail-on-non-empty " );
        dir.cdUp();
        command.append( TDEProcess::quote( dir.canonicalPath() ) );
        command.append( " ; " );
#else
        command.append( "rmdir " );
        command.append( TDEProcess::quote( dir.canonicalPath() ) );
        command.append( " ; " );
        command.append( "rmdir " );
        dir.cdUp();
        command.append( TDEProcess::quote( dir.canonicalPath() ) );
        command.append( " ; " );
#endif
      }
      else
      {
        continue;
      }
    }

    command.truncate( command.length() - 2 );

    proc << command;
    proc.start( TDEProcess::DontCare, TDEProcess::NoCommunication );
  }
}


#include "smb4kmounter.moc"