File: scanthread.cpp

package info (click to toggle)
ktorrent 25.04.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 25,104 kB
  • sloc: cpp: 40,482; xml: 1,163; python: 182; sh: 10; makefile: 5
file content (185 lines) | stat: -rw-r--r-- 4,193 bytes parent folder | download
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
/*
    SPDX-FileCopyrightText: 2011 Joris Guisson <joris.guisson@gmail.com>
    SPDX-License-Identifier: GPL-2.0-or-later
*/

#include "scanthread.h"

#include <QCoreApplication>
#include <QDir>
#include <QEvent>
#include <QTimer>

#include <KLocalizedString>

#include <util/fileops.h>

namespace kt
{
const int UPDATE_FOLDER_EVENT = QEvent::User + 1;
const int RECURSIVE_SCAN_EVENT = QEvent::User + 2;

class UpdateFolderEvent : public QEvent
{
public:
    UpdateFolderEvent()
        : QEvent((QEvent::Type)UPDATE_FOLDER_EVENT)
    {
    }

    ~UpdateFolderEvent() override
    {
    }
};

class RecursiveScanEvent : public QEvent
{
public:
    RecursiveScanEvent(const QUrl &url)
        : QEvent((QEvent::Type)RECURSIVE_SCAN_EVENT)
        , url(url)
    {
    }

    ~RecursiveScanEvent() override
    {
    }

    QUrl url;
};

ScanThread::ScanThread()
    : stop_requested(false)
    , recursive(false)
{
    scan_folders.setAutoDelete(true);
    moveToThread(this);
}

ScanThread::~ScanThread()
{
}

void ScanThread::setRecursive(bool rec)
{
    recursive = rec;
}

void ScanThread::addDirectory(const QUrl &url, bool recursive)
{
    scan(url, recursive);
}

void ScanThread::setFolderList(const QStringList &folders)
{
    QMutexLocker lock(&mutex);
    if (this->folders != folders) {
        this->folders = folders;
        // Use custom event to wake up scanner thread
        QCoreApplication::postEvent(this, new UpdateFolderEvent());
    }
}

void ScanThread::customEvent(QEvent *ev)
{
    if (ev->type() == UPDATE_FOLDER_EVENT) {
        updateFolders();
    } else if (ev->type() == RECURSIVE_SCAN_EVENT) {
        RecursiveScanEvent *rev = (RecursiveScanEvent *)ev;
        scan(rev->url, true);
    }
    ev->accept();
}

void ScanThread::updateFolders()
{
    QStringList tmp;

    mutex.lock();
    tmp = folders; // Use tmp list to not block the mutex for to long
    mutex.unlock();

    // first erase folders we don't need anymore
    bt::PtrMap<QString, ScanFolder>::iterator i = scan_folders.begin();
    while (i != scan_folders.end()) {
        if (!tmp.contains(i->first)) {
            QString f = i->first;
            i++;
            scan_folders.erase(f);
        } else {
            i->second->setRecursive(recursive);
            i++;
        }
    }

    for (const QString &folder : std::as_const(tmp)) {
        if (scan_folders.find(folder))
            continue;

        if (QDir(folder).exists()) {
            // only add folder when it exists
            ScanFolder *sf = new ScanFolder(this, QUrl::fromLocalFile(folder), recursive);
            scan_folders.insert(folder, sf);
        }
    }
}

void ScanThread::run()
{
    updateFolders();
    exec();
}

void ScanThread::stop()
{
    stop_requested = true;

    // XXX seems like deleting KDirWatch object(s) created in scan_folders
    // in destructor of this QThread after it has been stopped
    // causes memory corruption, so we delete them early
    scan_folders.clear();
    exit();
    wait();
}

bool ScanThread::alreadyLoaded(const QDir &d, const QString &torrent)
{
    return d.exists(QLatin1Char('.') + torrent);
}

void ScanThread::scan(const QUrl &dir, bool recursive)
{
    if (stop_requested)
        return;

    QStringList filters;
    filters << QStringLiteral("*.torrent");
    QDir d(dir.toLocalFile());
    const QStringList files = d.entryList(filters, QDir::Readable | QDir::Files);

    QList<QUrl> torrents;
    for (const QString &tor : files) {
        if (!alreadyLoaded(d, tor))
            torrents.append(QUrl::fromLocalFile(d.absoluteFilePath(tor)));
    }

    Q_EMIT found(torrents);

    if (stop_requested)
        return;

    if (recursive) {
        const QString loaded_localized = i18nc("folder name part", "loaded");

        const QStringList dirs = d.entryList(QDir::Readable | QDir::Dirs);
        for (const QString &subdir : dirs) {
            if (subdir != QStringLiteral(".") && subdir != QStringLiteral("..") && subdir != loaded_localized) {
                QCoreApplication::postEvent(this, new RecursiveScanEvent(QUrl::fromLocalFile(d.absoluteFilePath(subdir))));
            }
        }
    }
}

}

#include "moc_scanthread.cpp"