-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanager.cpp
173 lines (130 loc) · 4.99 KB
/
manager.cpp
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
#include <QThreadPool>
#include <QPointer>
#include <QProcess>
#include "updater.h"
#include "tools.h"
#include "manager.h"
#include "handlers/handlers.h"
#include "settings/settings.h"
struct manager_t::Pimpl {
Pimpl()
: thread_count(std::max(QThread::idealThreadCount(), 2))
, active_updaters(0)
, active_syncers(0)
{
QThreadPool::globalInstance()->setMaxThreadCount(thread_count);
};
database_p db;
connection conn;
const int thread_count;
std::unique_ptr<QWebdav> network;
int active_updaters;
int active_syncers;
};
manager_t::manager_t(database_p db, connection conn)
: p_(new Pimpl)
{
p_->db = db;
p_->conn = conn;
p_->network.reset(new QWebdav(conn.url, 0, QWebdav::Async));
qDebug() << "[MANAGER]: using idealThreadCount:" << p_->thread_count;
}
manager_t::~manager_t()
{
stop();
}
bool manager_t::busy() const
{
return p_->active_updaters || p_->active_syncers;
}
QWebdav* manager_t::network() const
{
return p_->network.get();
}
void manager_t::start()
{
start_update();
}
void manager_t::start_update()
{
Q_ASSERT(!p_->active_updaters);
p_->active_updaters++;
auto updater = new updater_t(p_->db, p_->conn);
updater->setAutoDelete(true);
Q_VERIFY(connect(updater, SIGNAL(new_actions(Actions)), SLOT(receive_new_actions(Actions))));
Q_VERIFY(connect(updater, SIGNAL(error(QString)), SLOT(stop())));
Q_VERIFY(connect(updater, SIGNAL(error(QString)), SIGNAL(error(QString))));
Q_VERIFY(::connect(updater, SIGNAL(finished()), [this] {
p_->active_updaters--;
if (p_->active_syncers == 0 && p_->active_updaters == 0) {
Q_EMIT finished();
}
}, Qt::BlockingQueuedConnection));
Q_VERIFY(connect(this, SIGNAL(need_stop()), updater, SLOT(stop())));
QThreadPool::globalInstance()->start(updater);
}
void manager_t::receive_new_actions(const Actions& actions)
{
Q_ASSERT(busy());
Q_EMIT new_actions(actions);
Q_FOREACH(auto action, actions) {
handler_t* handler = create_handler(action);
if (!handler) continue;
p_->active_syncers++;
Q_VERIFY(connect(handler, SIGNAL(new_actions(Actions)), SLOT(receive_new_actions(Actions))));
Q_VERIFY(connect(handler, SIGNAL(started(action_t)), SIGNAL(action_started(action_t))));
Q_VERIFY(connect(handler, SIGNAL(progress(action_t,qint64,qint64)),SIGNAL(progress(action_t,qint64,qint64))));
Q_VERIFY(connect(handler, SIGNAL(finished(action_t)), SIGNAL(action_success(action_t))));
Q_VERIFY(connect(handler, SIGNAL(error(action_t, QString)), SIGNAL(action_error(action_t,QString))));
Q_VERIFY(connect(handler, SIGNAL(error(action_t, QString)), SLOT(stop())));
Q_VERIFY(connect(handler, SIGNAL(compare(conflict_ctx&,bool&)), SLOT(compare(conflict_ctx&,bool&)), Qt::BlockingQueuedConnection));
Q_VERIFY(connect(handler, SIGNAL(merge(conflict_ctx&,bool&)), SLOT(merge(conflict_ctx&,bool&)), Qt::BlockingQueuedConnection));
Q_VERIFY(connect(this, SIGNAL(need_stop()), handler, SLOT(stop())));
Q_VERIFY(::connect(handler, SIGNAL(finished(action_t)), [this] {
p_->active_syncers--;
if (p_->active_syncers == 0 && p_->active_updaters == 0) {
Q_EMIT finished();
}
}, Qt::BlockingQueuedConnection));
handler->setAutoDelete(true);
QThreadPool::globalInstance()->start(handler);
}
}
void manager_t::compare(conflict_ctx& ctx, bool& result)
{
result = !QProcess::execute(settings().compare(), QStringList() << ctx.local_file << ctx.remote_file);
}
void manager_t::merge(conflict_ctx& ctx, bool& result)
{
ctx.result_file = ctx.local_file + ".merged" + storage_t::tmpsuffix;
result = !QProcess::execute(settings().merge(), QStringList()
<< "-o"
<< ctx.result_file
<< ctx.local_file
<< ctx.remote_file);
}
handler_t* manager_t::create_handler(const action_t& action)
{
switch(action.type) {
case action_t::upload: return new upload_handler(p_->db, *this, action);
case action_t::download: return new download_handler(p_->db, *this, action);
case action_t::forgot: return new forgot_handler(p_->db, *this, action);
case action_t::remove_local: return new remove_local_handler(p_->db, *this, action);
case action_t::remove_remote: return new remove_remote_handler(p_->db, *this, action);
case action_t::local_changed: return new local_change_handler(p_->db, *this, action);
case action_t::remote_changed: return new remote_change_handler(p_->db, *this, action);
case action_t::upload_dir: return new upload_dir_handler(p_->db, *this, action);
case action_t::download_dir: return new download_dir_handler(p_->db, *this, action);
case action_t::conflict: return new conflict_handler(p_->db, *this, action);
case action_t::error: throw qt_exception_t("Error action");
};
return 0;
}
void manager_t::stop()
{
Q_EMIT need_stop();
}
void manager_t::wait()
{
QThreadPool::globalInstance()->waitForDone();
}