summaryrefslogtreecommitdiffstats
path: root/smb4k/core/smb4kmounter.h
blob: dd151d614cd59205f175a0590aa09d0756c3c98a (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
/***************************************************************************
    smb4kmounter.h  -  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                                                    *
 ***************************************************************************/

#ifndef SMB4KMOUNTER_H
#define SMB4KMOUNTER_H

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

// TQt includes
#include <tqobject.h>
#include <tqstringlist.h>
#include <tqptrqueue.h>
#include <tqfile.h>

// KDE includes
#include <kprocess.h>

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

// forward declarations
class Smb4KMounterPrivate;
class Smb4KShare;


/**
 * This is one of the core classes of Smb4K. It manages the mounting
 * and unmounting of remote Samba/Windows shares. Additionally it maintains a
 * list of all mounts with SMBFS and CIFS file system that are present
 * on the system.
 *
 * @author Alexander Reinholdt <dustpuppy@users.berlios.de>
 */

class Smb4KMounter : public TQObject
{
  Q_OBJECT
  

  public:
    /**
     * The constructor.
     */
    Smb4KMounter( TQObject *parent = 0, const char *name = 0 );
    /**
     * The destructor.
     */
    ~Smb4KMounter();

    /**
     * Aborts a running process.
     */
    void abort();

    /**
     * Enumeration for the state of the process. The members are also
     * used in the results() signal as return values.
     */
    enum State{ Remount, Import, Mount, Unmount, UnmountAll, Idle };

    /**
     * Unmounts a share. This can either be done the "normal" way, or you may
     * force it. If you decide the force the unmounting by setting @p force to TRUE,
     * a lazy unmount will be initiated. With the parameter @p noMessage you can
     * suppress any error messages.
     *
     * @param share       The share object that should be unmounted.
     *
     * @param force       Force the unmounting of the share.
     *
     * @param noMessage   Determines whether this function should emit an error code in case of an error.
     *                    The default value is FALSE.
     */
    void unmountShare( Smb4KShare *share, bool force = false, bool noMessage = false );

    /**
     * Unmounts all shares at once.
     */
    void unmountAllShares();

    /**
     * Mounts a share.
     *
     * @param workgroup   The workgroup of the share.
     *
     * @param host        The server where the share is located.
     *
     * @param ip          The IP address of the host.
     *
     * @param share       The name of the share.
     */
    void mountShare( const TQString &workgroup, const TQString &host, const TQString &ip, const TQString &share );

    /**
     * Returns the unsorted list of mounted shares.
     */
    const TQValueList<Smb4KShare *> &getShares() { return m_mounted_shares; };

    /**
     * Find a share in the list with its path.
     */
    Smb4KShare* findShareByPath( const TQString &path );

    /**
     * Find all mounts of the particular share @p share on the system.
     *
     * @param name        The name of the share
     *
     * @returns the complete list of mounts of @p share.
     */
    TQValueList<Smb4KShare> findShareByName( const TQString &name );

    /**
     * This function returns TRUE if a share is mounted and FALSE otherwise.
     *
     * @param name        The name of the share. It has to look like this:
     *                    //HOST/SHARE.
     *
     * @param userOnly    Specifies whether this function should only consider
     *                    shares mounted by the user or all shares that are
     *                    available on the system. The default is TRUE.
     *
     * @return TRUE if the share is mounted and FALSE otherwise.
     */
    bool isMounted( const TQString &name, bool userOnly = true );

    /**
     * This function reports if the mounter is running or not.
     *
     * @returns             TRUE if the scanner is running and FALSE otherwise.
     */
    bool isRunning() { return m_working; }

    /**
     * This function returns the list of broken shares.
     *
     * @returns             The list of broken shares.
     */
    const TQValueList<Smb4KShare *> getBrokenShares();

    /**
     * This function executes Smb4KMounter::slotShutdown(). Under normal circumstances,
     * there is no need to use this function, because everything that is done here is
     * also done when KApplication::shutDown() is emitted. However, it comes in handy
     * when you need to perform last actions in a plugin.
     */
    void prepareForShutdown();

    /**
     * This function initializes import of mounted shares and the remounting of recently
     * used shares.
     */
    void init();

  signals:
    /**
     * This signal emits the run state.
     *
     * @param state           The so-called run state. There are several defined
     *                        in the smb4kdefs.h header file.
     */
    void state( int state );

    /**
     * Tells the program, that the shares list has been updated.
     */
    void updated();

    /**
     * This signal is emitted when a share has successfully been mounted or
     * if it has already been mounted.
     *
     * @param mountpoint      The mount point of the share.
     */
    void mountedShare( const TQString &mountpoint );

    /**
     * This signal is emitted just before a share is unmounted.
     *
     * @param mountpoint       The mount point of the share that is going to
     *                         be unmounted.
     */
    void aboutToUnmount( const TQString &mountpoint );

  protected:
    /**
     * Starts a process. It takes an interger, that determines,
     * which process has to be started.
     */
    void startProcess( int state );

    /**
     * Is called, when the process ended.
     */
    void endProcess();

    /**
     * Finishes the mounting of shares.
     */
    void processMount();

    /**
     * Finishes the unmounting of a single share.
     */
    void processUnmount();

    /**
     * Reimplemented from TQObject
     */
    void timerEvent( TQTimerEvent *e );

  protected slots:
    /**
     * Is called, when the process exits.
     */
    void slotProcessExited( KProcess * );

    /**
     * Is called, if output is received on Stdout.
     */
    void slotReceivedStdout( KProcess *, char *buf, int len );

    /**
     * Is called, if output is received on Stderr.
     */
    void slotReceivedStderr( KProcess *, char *buf, int len );

    /**
     * This slot is called by the KApplication::shutDown() signal.
     * Is does everything that has to be done before the program
     * really exits.
     */
    void slotShutdown();

  private:
    /**
     * Remount shares that were used in the last session.
     */
    void remount();

    /**
     * Imports mounted shares.
     */
    void import();

    /**
     * Mounts a selected share.
     */
    void mount( const TQString &workgroup, const TQString &host, const TQString &ip, const TQString &share );

    /**
     * Unmounts the selected item.
     */
    void unmount( const TQString &mountpoint, bool noMessage, bool force = false );

    /**
     * Unmounts all shares at once.
     */
    void unmountAll();

    /**
     * Checks if the share @p share is accessible or not and sets Smb4KShare::isBroken()
     * accordingly. Additionally, Smb4KShare::totalDiskSpace() and Smb4KShare::freeDiskSpace()
     * are set to the current values.
     *
     * @param share           The share that should be checked.
     */
    void checkAccessibility( Smb4KShare *share );

    /**
     * The KProcess object.
     */
    KProcess *m_proc;

    /**
     * The buffer.
     */
    TQString m_buffer;

    /**
     * The queue, where the incoming requests are stored.
     */
    TQPtrQueue<TQString> m_queue;

    /**
     * Determines, whether the mounter is running or not.
     */
    bool m_working;

    /**
     * Holds the list of currently mounted shares as a pointer list.
     */
    TQValueList<Smb4KShare *> m_mounted_shares;

    /**
     * Makes sure that the error message concerning the missing of
     * the file /proc/mounts is only shown once.
     */
    bool m_proc_error;

    /**
     * The internal state of the process. Do not mix this up with
     * the state that's emitted to notify the application about changes.
     */
    int m_state;

#ifndef __FreeBSD__
    /**
     * This file object points to /proc/mounts.
     */
    TQFile m_proc_mounts;
#endif

    /**
     * This is the pointer to the private helper class.
     */
    Smb4KMounterPrivate *m_priv;
};

#endif