1 | |
---|
2 | #include "AbstractAccount.h" |
---|
3 | |
---|
4 | AbstractAccount::AbstractAccount(IStorageOfficer* storageOfficer, IValidator* validator) |
---|
5 | : id_(-1) |
---|
6 | |
---|
7 | , enabled_(true) |
---|
8 | |
---|
9 | , freeSMSCount_(-1) |
---|
10 | |
---|
11 | , initializing_(false) |
---|
12 | , initialized_(false) |
---|
13 | , stoppingInitialization_(false) |
---|
14 | |
---|
15 | , cancelSMSSending_(false) |
---|
16 | , storageOfficer_(storageOfficer) |
---|
17 | , validator_(validator) |
---|
18 | { |
---|
19 | eventMapper_ = new AccountEventMapper(this); |
---|
20 | } |
---|
21 | |
---|
22 | AccountEventMapper* AbstractAccount::eventMapper() const { |
---|
23 | return eventMapper_; |
---|
24 | } |
---|
25 | IAccountEvents* AbstractAccount::accountEvents() const { |
---|
26 | return static_cast<IAccountEvents*>(eventMapper()); |
---|
27 | } |
---|
28 | |
---|
29 | int AbstractAccount::id() const { |
---|
30 | return id_; |
---|
31 | } |
---|
32 | void AbstractAccount::setId(int id) { |
---|
33 | if (id_ == id) return; |
---|
34 | |
---|
35 | int oldId = id_; |
---|
36 | id_ = id; |
---|
37 | |
---|
38 | emit accountEvents()->idChanged(oldId, id); |
---|
39 | emit accountEvents()->dataChanged(); |
---|
40 | } |
---|
41 | |
---|
42 | QString AbstractAccount::name() const { |
---|
43 | return name_; |
---|
44 | } |
---|
45 | void AbstractAccount::setName(const QString& name) { |
---|
46 | SET_IF_DIFFERENT(name_, name.trimmed()); |
---|
47 | emit accountEvents()->dataChanged(); |
---|
48 | } |
---|
49 | |
---|
50 | |
---|
51 | void AbstractAccount::setStatus(const QString& status, int progress /* = -1 */, const ProgressMethod& progressMethod /* = SetProgress*/) { |
---|
52 | if (status_ != status) { |
---|
53 | status_ = status; |
---|
54 | |
---|
55 | emit accountEvents()->statusChanged(status); |
---|
56 | } |
---|
57 | |
---|
58 | if (progress > -1) { |
---|
59 | setProgress(progress, progressMethod); |
---|
60 | } |
---|
61 | } |
---|
62 | void AbstractAccount::setProgress(int progress, const ProgressMethod& progressMethod /* = SetProgress */) { |
---|
63 | switch (progressMethod) { |
---|
64 | case IncProgress: |
---|
65 | progress_ += progress; |
---|
66 | break; |
---|
67 | |
---|
68 | case SetProgress: |
---|
69 | SET_IF_DIFFERENT(progress_, progress); |
---|
70 | break; |
---|
71 | } |
---|
72 | |
---|
73 | emit accountEvents()->progressChanged(progress_); |
---|
74 | } |
---|
75 | |
---|
76 | bool AbstractAccount::isEnabled() const { |
---|
77 | return enabled_; |
---|
78 | } |
---|
79 | void AbstractAccount::setEnabled(bool enabled) { |
---|
80 | SET_IF_DIFFERENT(enabled_, enabled); |
---|
81 | emit accountEvents()->dataChanged(); |
---|
82 | |
---|
83 | if (!enabled) { |
---|
84 | unInitialize(); |
---|
85 | } |
---|
86 | } |
---|
87 | |
---|
88 | |
---|
89 | int AbstractAccount::freeSMSCount() const { |
---|
90 | return freeSMSCount_; |
---|
91 | } |
---|
92 | void AbstractAccount::setFreeSMSCount(int freeSMSCount) { |
---|
93 | SET_IF_DIFFERENT(freeSMSCount_, freeSMSCount); |
---|
94 | emit accountEvents()->dataChanged(); |
---|
95 | } |
---|
96 | |
---|
97 | IValidator* AbstractAccount::getValidator() const { |
---|
98 | return validator_; |
---|
99 | } |
---|
100 | IStorageOfficer* AbstractAccount::getStorageOfficer() const { |
---|
101 | return storageOfficer_; |
---|
102 | } |
---|
103 | |
---|
104 | |
---|
105 | |
---|
106 | void AbstractAccount::initialize(){ |
---|
107 | if (isInitializing() || isInitialized() || !isEnabled()) { |
---|
108 | return; |
---|
109 | } |
---|
110 | |
---|
111 | /* TODO: Do this in a queue... */ |
---|
112 | |
---|
113 | while (stoppingInitialization_){ |
---|
114 | // TODO: sleep... |
---|
115 | } |
---|
116 | |
---|
117 | initializing_ = true; |
---|
118 | try{ |
---|
119 | doInitialize(); |
---|
120 | |
---|
121 | if (!stoppingInitialization_) |
---|
122 | setInitialized(true); |
---|
123 | }catch (...) { |
---|
124 | initializing_ = false; |
---|
125 | throw; |
---|
126 | } |
---|
127 | initializing_ = false; |
---|
128 | stoppingInitialization_ = false; |
---|
129 | } |
---|
130 | |
---|
131 | void AbstractAccount::stopInitializing() { |
---|
132 | if (isInitializing()) |
---|
133 | stoppingInitialization_ = true; |
---|
134 | } |
---|
135 | |
---|
136 | bool AbstractAccount::isInitializing() { |
---|
137 | return initializing_; |
---|
138 | } |
---|
139 | |
---|
140 | bool AbstractAccount::isInitialized() { |
---|
141 | return initialized_; |
---|
142 | } |
---|
143 | |
---|
144 | void AbstractAccount::setInitialized(bool initialized) { |
---|
145 | SET_IF_DIFFERENT(initialized_, initialized); |
---|
146 | emit accountEvents()->dataChanged(); |
---|
147 | } |
---|
148 | |
---|
149 | |
---|
150 | void AbstractAccount::unInitialize() { |
---|
151 | if (isInitializing()) |
---|
152 | stopInitializing(); |
---|
153 | |
---|
154 | setInitialized(false); |
---|
155 | } |
---|
156 | |
---|
157 | |
---|
158 | void AbstractAccount::cancelSMSSending() { |
---|
159 | cancelSMSSending_ = true; |
---|
160 | } |
---|
161 | |
---|
162 | bool AbstractAccount::isSendingCanceled() { |
---|
163 | return cancelSMSSending_; |
---|
164 | } |
---|
165 | |
---|
166 | void AbstractAccount::uncancelSendingSMS() { |
---|
167 | cancelSMSSending_ = false; |
---|
168 | } |
---|