summaryrefslogtreecommitdiffstats
path: root/kdirstat/kdirsaver.h
blob: b9bbb38f8c4487e94687f1e4fca75d42ec995a92 (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
/*
 *   File name:	kdirsaver.h
 *   Summary:	Utility object to save current working directory
 *   License:	LGPL - See file COPYING.LIB for details.
 *   Author:	Stefan Hundhammer <sh@suse.de>
 *
 *   Updated:	2003-01-07
 */


#ifndef KDirSaver_h
#define KDirSaver_h

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

#include <kurl.h>
#include <tqdir.h>


/**
 * Helper class to change directories without losing the current context.
 * Will change back to the old working directory when destroyed.
 *
 * @short Directory changer with automatic restore
 **/
class KDirSaver
{
public:
    /**
     * Constructor. Will save the current working directory and change to the
     * path supplied. The old working directory will be restored when this
     * object is destroyed.
     **/
    KDirSaver( const TQString & newPath = "" );

    /**
     * Constructor from a KURL. Will issue error messages on stdout for
     * non-local objects.
     **/
    KDirSaver( const KURL & url );
    
    /**
     * Destructor. Restores the original working directory.
     **/
    virtual ~KDirSaver();

    /**
     * Change directory. Unlike @ref TQDir::cd(), this method really performs a
     * system chdir() so subsequent system calls will have the directory
     * specified as the new current working directory.
     **/
    void cd( const TQString & newPath );

    /**
     * Obtain the current working directory's absolute path.
     * This is useful for resolving/simplifying relative paths.
     **/
    TQString currentDirPath() const;
    
    /**
     * (Prematurely) restore the working directory. Unnecessary when this
     * object will be destroyed anyway since the destructor does exactly that.
     **/
    void restore();

protected:
    TQDir oldWorkingDir;
};

#endif // KDirSaver_h


// EOF