Welcome to mirror list, hosted at ThFree Co, Russian Federation.

folderwatcher_win.cpp « mirall « src - github.com/owncloud/client.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 992065828e6696d60c313f9c2cb54ac14502fe3a (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
/*
 * Copyright (C) by Daniel Molkentin <danimo@owncloud.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.
 *
 * 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.
 */

#include <QThread>
#include <QDebug>

#include "mirall/folderwatcher.h"
#include "mirall/folderwatcher_win.h"

#include <stdlib.h>
#include <stdio.h>
#include <tchar.h>

namespace Mirall {

void WatcherThread::run()
{
    _handle = FindFirstChangeNotification((wchar_t*)_path.utf16(),
                                         true, // recursive watch
                                         FILE_NOTIFY_CHANGE_FILE_NAME |
                                         FILE_NOTIFY_CHANGE_DIR_NAME |
                                         FILE_NOTIFY_CHANGE_LAST_WRITE);

    if (_handle == INVALID_HANDLE_VALUE)
    {
        qDebug() << Q_FUNC_INFO << "FindFirstChangeNotification function failed, stopping watcher!";
        FindCloseChangeNotification(_handle);
        _handle = 0;
        return;
    }

    if (_handle == NULL)
    {
        qDebug() << Q_FUNC_INFO << "FindFirstChangeNotification returned null, stopping watcher!";
        FindCloseChangeNotification(_handle);
        _handle = 0;
        return;
    }

    while(true) {
        switch(WaitForSingleObject(_handle, /*wait*/ INFINITE)) {
        case WAIT_OBJECT_0:
            if (FindNextChangeNotification(_handle) == false) {
                qDebug() << Q_FUNC_INFO << "FindFirstChangeNotification returned FALSE, stopping watcher!";
                FindCloseChangeNotification(_handle);
                _handle = 0;
                return;
            }
            qDebug() << Q_FUNC_INFO << "Change detected in" << _path << "from" << QThread::currentThread    ();
            emit changed(_path);
            break;
        default:
            qDebug()  << Q_FUNC_INFO << "Error while watching";
        }
    }
}

WatcherThread::~WatcherThread()
{
    if (_handle)
        FindCloseChangeNotification(_handle);
}

FolderWatcherPrivate::FolderWatcherPrivate(FolderWatcher *p)
    : _parent(p)
{
    _thread = new WatcherThread(p->root());
    connect(_thread, SIGNAL(changed(const QString&)),
            _parent,SLOT(changeDetected(const QString&)));
    _thread->start();
}

FolderWatcherPrivate::~FolderWatcherPrivate()
{
    _thread->terminate();
    _thread->wait();
    delete _thread;
}

} // namespace Mirall