summaryrefslogtreecommitdiffstats
path: root/ksquirrel/sidebar/sq_threaddirlister.h
blob: 7b8cd6cd40fc9b62741a02838389f08da37d83ff (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
/***************************************************************************
                          sq_threaddirlister.h  -  description
                             -------------------
    begin                : Feb 10 2007
    copyright            : (C) 2007 by Baryshev Dmitry
    email                : ksquirrel.iv@gmail.com
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/


#ifndef SQ_THREADDIRLISTER_H
#define SQ_THREADDIRLISTER_H

#include <tqthread.h>
#include <tqevent.h> 
#include <tqmutex.h>

#include <kurl.h>

#include <sys/types.h>
#include <dirent.h>

class TQObject;

class KConfig;

/********************************************************/

#define SQ_ItemsEventId (TQEvent::User + 1)

class SQ_ItemsEvent : public TQCustomEvent
{
    public:
        SQ_ItemsEvent(const KURL &tqparent, int c1, int c2)
            : TQCustomEvent(SQ_ItemsEventId), m_files(c1), m_dirs(c2), m_url(tqparent)
        {}

        int files() const;
        int dirs() const;

        KURL url() const;

    private:
        int m_files, m_dirs;
        KURL m_url;
};

inline
KURL SQ_ItemsEvent::url() const
{
    return m_url;
}

inline
int SQ_ItemsEvent::files() const
{
    return m_files;
}

inline
int SQ_ItemsEvent::dirs() const
{
    return m_dirs;
}

/********************************************************/

/*
 *  This thread will read directory and determine
 *  number of files in it. Finally
 *  it will post an event to main thread with
 *  calculated count. This function is threaded
 *  due to perfomance reason.
 */
class SQ_ThreadDirLister : public TQThread
{
    public:
        SQ_ThreadDirLister(TQObject *o);
        ~SQ_ThreadDirLister();

        void appendURL(const KURL &url);

        bool hasURL(const KURL &url);
        bool isCurrent(const KURL &url);

        void lock();
        void unlock();

        void closeDir();

    protected:
        virtual void run();

    private:
        void waitMutex();

    private:
        // urls to read
        KURL::List todo;

        // this object will recieve our events
        TQObject *obj;
        TQMutex mutex;
        KConfig *cache;
        DIR     *dir;
};

inline
void SQ_ThreadDirLister::appendURL(const KURL &url)
{
    todo.append(url);
}

inline
bool SQ_ThreadDirLister::hasURL(const KURL &url)
{
    return todo.find(url) != todo.end();
}

inline
bool SQ_ThreadDirLister::isCurrent(const KURL &url)
{
    return todo.first().equals(url, true);
}

inline
void SQ_ThreadDirLister::lock()
{
    mutex.lock();
}

inline
void SQ_ThreadDirLister::unlock()
{
    mutex.unlock();
}

inline
void SQ_ThreadDirLister::waitMutex()
{
    while(!mutex.tryLock())
        TQThread::msleep(1);
}

#endif