1 | #include "BCAccountLoadThread.h" |
---|
2 | |
---|
3 | /*short const AccountLoadThread::State::Unloaded = 0; |
---|
4 | short const AccountLoadThread::State::Loading = 1; |
---|
5 | short const AccountLoadThread::State::Loaded = 2; |
---|
6 | short const AccountLoadThread::State::Cancelling = 3; |
---|
7 | short const AccountLoadThread::State::Cancelled = 4; |
---|
8 | short const AccountLoadThread::State::Failed = 5;*/ |
---|
9 | |
---|
10 | BCAccountLoadThread::BCAccountLoadThread(IAccount* account, QObject* parent /* = 0 */) |
---|
11 | : QThread(parent) |
---|
12 | , account_(account) |
---|
13 | { |
---|
14 | qRegisterMetaType<AccountLoadState>("AccountLoadState"); |
---|
15 | loadState_ = Unloaded; |
---|
16 | } |
---|
17 | |
---|
18 | |
---|
19 | void BCAccountLoadThread::run() { |
---|
20 | if (account_->isInitialized()) { |
---|
21 | setLoadState(Loaded); |
---|
22 | loaded(account()); // Signal |
---|
23 | loadingFinished(account(), loadState()); // Signal |
---|
24 | |
---|
25 | return; |
---|
26 | } |
---|
27 | |
---|
28 | setLoadState(Loading); |
---|
29 | |
---|
30 | try { |
---|
31 | account_->initialize(); |
---|
32 | } catch (EException* exception) { |
---|
33 | setLoadState(Failed); |
---|
34 | loadingFailed(account(), exception); // Signal |
---|
35 | } catch (...) { |
---|
36 | setLoadState(Failed); |
---|
37 | loadingFailed(account(), new EException("Unknown error occured while loading an account.")); // Signal |
---|
38 | } |
---|
39 | |
---|
40 | switch (loadState()) { |
---|
41 | case Cancelling: |
---|
42 | setLoadState(Cancelled); |
---|
43 | break; |
---|
44 | case Loaded: |
---|
45 | case Failed: |
---|
46 | break; |
---|
47 | default: |
---|
48 | setLoadState(Loaded); |
---|
49 | loaded(account()); // Signal |
---|
50 | break; |
---|
51 | } |
---|
52 | loadingFinished(account(), loadState()); // Signal |
---|
53 | } |
---|
54 | |
---|
55 | void BCAccountLoadThread::cancelLoading() { |
---|
56 | account_->stopInitializing(); |
---|
57 | setLoadState(Cancelling); |
---|
58 | } |
---|
59 | |
---|
60 | |
---|
61 | void BCAccountLoadThread::setLoadState(AccountLoadState loadState) { |
---|
62 | if (loadState_ != loadState) { |
---|
63 | loadState_ = loadState; |
---|
64 | |
---|
65 | loadStateChanged(account(), this->loadState()); // Signal |
---|
66 | } |
---|
67 | } |
---|
68 | |
---|
69 | IAccount* BCAccountLoadThread::account() { |
---|
70 | return account_; |
---|
71 | } |
---|
72 | AccountLoadState BCAccountLoadThread::loadState() { |
---|
73 | return loadState_; |
---|
74 | } |
---|