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