Changeset 72:fc91522ef9be in SMSSender for gateways/SwisscomXtraZone/src/business/BCAccount.cpp
- Timestamp:
- Feb 9, 2010 9:47:21 PM (13 years ago)
- Branch:
- 3.0
- Convert:
- svn:3639001d-8e34-449c-bb86-3782b86c4877/branches/3.0@71
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
gateways/SwisscomXtraZone/src/business/BCAccount.cpp
r71 r72 83 83 posts.insert("isiwebpasswd", password()); 84 84 85 lastHtml_ = httpHelper_->post(LOGIN_PAGE, QMap<QString, QString>(), posts); 85 if (!httpHelper_->post(LOGIN_PAGE, lastHtml_, QMap<QString, QString>(), posts)) { 86 throw LoginAccount::ELoginException(LoginAccount::ELoginException::RequestError).chain(httpHelper_->lastError()); 87 } 86 88 87 89 if (lastHtml_.contains("Begin error region content", Qt::CaseInsensitive)) { 88 throw LoginAccount::ELoginException(LoginAccount::ELoginException::InvalidLoginData) ;90 throw LoginAccount::ELoginException(LoginAccount::ELoginException::InvalidLoginData).chain(httpHelper_->lastError()); 89 91 } 90 92 91 93 if (!lastHtml_.contains("CobYouthSMSSenden:txtMessage", Qt::CaseInsensitive)) { 92 throw LoginAccount::ELoginException( LoginAccount::ELoginException::Unknown, "The returned page contains no message field.");94 throw LoginAccount::ELoginException("The returned page contains no message field.").chain(httpHelper_->lastError()); 93 95 } 94 96 … … 99 101 qDebug() << "Starting logout"; 100 102 101 httpHelper_->get(LOGOUT_PAGE); 103 QString requestResult; 104 if (!httpHelper_->get(LOGOUT_PAGE, requestResult)) { 105 qWarning() << tr("Error during logout: ") + httpHelper_->lastError().chainedWhat(); 106 qWarning() << tr("But ignoring it, since we clear the cookies and are therefore also logged out"); 107 } 102 108 httpHelper_->cookieManager()->clearCookies(); 103 109 … … 108 114 qDebug() << "Starting still-logged-in-check"; 109 115 110 bool result = httpHelper_->get(MAIN_PAGE).contains("CobYouthSMSSenden:txtMessage", Qt::CaseInsensitive); 116 QString requestResult; 117 bool result = true; 118 if (!httpHelper_->get(MAIN_PAGE, requestResult)) { 119 result = false; 120 qWarning() << tr("Error while checking if we are still logged in: ") + httpHelper_->lastError().chainedWhat(); 121 qWarning() << tr("But ignoring it, since we just do a new login attempt."); 122 } 123 result = result && 124 requestResult.contains("CobYouthSMSSenden:txtMessage", Qt::CaseInsensitive); 111 125 112 126 qDebug() << "Ended still-logged-in-check"; … … 128 142 } 129 143 130 lastHtml_ = httpHelper_->post(MAIN_PAGE, QMap<QString, QString>(), posts_); 144 if (!httpHelper_->post(MAIN_PAGE, lastHtml_, QMap<QString, QString>(), posts_)) { 145 throw Http::EReadException(tr("Error while doing a post")).addDebugInfos(posts_).chain(httpHelper_->lastError()); 146 } 131 147 132 148 rx.setPattern("<span.*id=\"CobYouthSMSSenden_lblErrorBox\".*>(.*)</span>"); … … 145 161 BCGateway* gtwy = static_cast<BCGateway*>(gateway()); 146 162 147 rx.setPattern("<input.*id=\"CobYouthSMSSenden_txtMessageDisabled\".*value=\"(.*)\".*/>"); 148 if (rx.indexIn(lastHtml_) == -1) { 149 throw EException(tr("Could not get the addon text!")); 150 } 151 gtwy->setAddonText(rx.cap(1)); 152 qDebug() << " Addon text: " + rx.cap(1); 153 154 rx.setPattern("<input.*id=\"lblcounter\".*value='(\\d*)'.*/?>"); 155 if (rx.indexIn(lastHtml_) == -1){ 156 throw EException(tr("Could not get the longSMSLength!")); 157 } 158 gtwy->setLongSMSLength(rx.cap(1).toInt() + gtwy->addonText().length()); 159 qDebug() << " Long SMS lenght: " + rx.cap(1); 160 161 rx.setPattern("<span.*id=\"CobYouthMMSSMSKonto_lblGuthaben\">(\\d*)</span>"); 162 if (rx.indexIn(lastHtml_) == -1) { 163 throw EException(tr("Could not get the free sms count!")); 163 try { 164 rx.setPattern("<input.*id=\"CobYouthSMSSenden_txtMessageDisabled\".*value=\"(.*)\".*/>"); 165 if (rx.indexIn(lastHtml_) == -1) { 166 throw EException(tr("Could not get the addon text!")); 167 } 168 gtwy->setAddonText(rx.cap(1)); 169 qDebug() << " Addon text: " + rx.cap(1); 170 171 rx.setPattern("<input.*id=\"lblcounter\".*value='(\\d*)'.*/?>"); 172 if (rx.indexIn(lastHtml_) == -1){ 173 throw EException(tr("Could not get the longSMSLength!")); 174 } 175 gtwy->setLongSMSLength(rx.cap(1).toInt() + gtwy->addonText().length()); 176 qDebug() << " Long SMS lenght: " + rx.cap(1); 177 178 rx.setPattern("<span.*id=\"CobYouthMMSSMSKonto_lblGuthaben\">(\\d*)</span>"); 179 if (rx.indexIn(lastHtml_) == -1) { 180 throw EException(tr("Could not get the free sms count!")); 181 } 182 } catch (EException e) { 183 throw EException(tr("Error while parsing the sms-page: ") + e.unchainedWhat()); 164 184 } 165 185 setFreeSMSCount(rx.cap(1).toInt()); … … 173 193 posts.insert("CobYouthSMSSenden:RecipientsDG:_ctl2:_ctl0", ""); 174 194 175 while (lastHtml_.contains("CobYouthSMSSenden:RecipientsDG:_ctl2:_ctl0", Qt::CaseInsensitive)){ 176 _doPost(posts); 195 try { 196 while (lastHtml_.contains("CobYouthSMSSenden:RecipientsDG:_ctl2:_ctl0", Qt::CaseInsensitive)){ 197 _doPost(posts); 198 } 199 } catch (EException e) { 200 throw EException(tr("Could not remove old recipients: ") + e.unchainedWhat()); 177 201 } 178 202 qDebug() << "Recipients removed"; … … 186 210 posts.insert("CobYouthSMSSenden:txtNewReceiver", recipient->number().toString("aaau")); 187 211 188 _doPost(posts); 212 try { 213 _doPost(posts); 214 } catch (EException e) { 215 throw EException(tr("Could not add a recipient: ") + e.what()); 216 } 189 217 qDebug() << "Recipient added"; 190 218 } 191 219 192 220 void BCAccount::_sendSMS(const QString& text) { 193 qDebug() << "Start ingsending part of SMS";221 qDebug() << "Start of sending part of SMS"; 194 222 195 223 QString txt = text; … … 201 229 posts.insert("CobYouthSMSSenden:txtMessage", txt); 202 230 203 _doPost(posts); 231 try { 232 _doPost(posts); 233 } catch (EException e) { 234 throw EException(tr("A part of the sms could not be sent to all recipients! ") + e.what()); 235 } 204 236 205 237 qDebug() << "Part of SMS sent"; … … 231 263 setProgress(0); 232 264 233 uncancelSendingSMS();234 235 try {236 setStatus(LoginAccount::Status::Login);237 login();238 }catch (...) {239 // TODO240 setStatus(LoginAccount::Status::Error);241 }242 243 _parseMainPage();244 245 QList<QString> longSMSLst = gateway()->splitTextToLongSMS(message);246 QList<QString> shortSMSLst = gateway()->splitTextToShortSMS(message);247 248 int freeSMSNeeded = shortSMSLst.size() * recipients.size();249 if (freeSMSNeeded > freeSMSCount()) {250 throw EException(tr("You have only %1 SMS for free. (You need at least %2 SMS left to send this message!)").arg(freeSMSCount()).arg(freeSMSNeeded));251 }252 253 setProgress(10);254 255 256 int percent_per_sms = 80 / longSMSLst.size();257 258 int x = 0;259 QListIterator<QString> i(longSMSLst);260 while (i.hasNext()) {261 int y = 0;262 263 QSetIterator<IContact*> j(recipients);264 do {265 setStatus(LoginAccount::Status::RemovingRecipients);266 _removeRecipients(); // Remove all recipients267 setProgress(percent_per_sms * 20/100, IncProgress);268 269 270 int recipientsNum = std::min(recipients.size(), (y+1)*MAX_MOBILE);271 setStatus(LoginAccount::Status::AddingRecipients.arg(y*MAX_MOBILE + 1).arg(recipientsNum).arg(recipients.size()));272 273 274 int percent_per_contact = (percent_per_sms * 60/100) / recipientsNum;275 276 int ix = 0;277 while (j.hasNext() && (ix < MAX_MOBILE)) {278 _addRecipient(j.next());279 setProgress(percent_per_contact, IncProgress);280 281 ix++;282 }283 284 setStatus(LoginAccount::Status::SendingSMS.arg(x+1).arg(longSMSLst.size()));285 _sendSMS(i.next());286 setStatus(LoginAccount::Status::SMSSent, percent_per_sms * 20/100, IncProgress);287 288 y++;289 } while ((y * MAX_MOBILE) < recipients.size()); // Repeat this step until the sms is sent to all recipients (if there are more than allowed to be addressed at once)290 291 x++;292 }293 294 _parseMainPage();295 setStatus(LoginAccount::Status::Logout, 90);296 logout();297 298 // TODO: check if sending was cancelled -> set status to "user abort"299 setStatus(LoginAccount::Status::SMSSent, 100);265 uncancelSendingSMS(); 266 267 try { 268 setStatus(LoginAccount::Status::Login); 269 login(); 270 }catch (...) { 271 // TODO 272 setStatus(LoginAccount::Status::Error); 273 } 274 275 _parseMainPage(); 276 277 QList<QString> longSMSLst = gateway()->splitTextToLongSMS(message); 278 QList<QString> shortSMSLst = gateway()->splitTextToShortSMS(message); 279 280 int freeSMSNeeded = shortSMSLst.size() * recipients.size(); 281 if (freeSMSNeeded > freeSMSCount()) { 282 throw EException(tr("You have only %1 SMS for free. (You need at least %2 SMS left to send this message!)").arg(freeSMSCount()).arg(freeSMSNeeded)); 283 } 284 285 setProgress(10); 286 287 288 int percent_per_sms = 80 / longSMSLst.size(); 289 290 int x = 0; 291 QListIterator<QString> i(longSMSLst); 292 while (i.hasNext()) { 293 int y = 0; 294 295 QSetIterator<IContact*> j(recipients); 296 do { 297 setStatus(LoginAccount::Status::RemovingRecipients); 298 _removeRecipients(); // Remove all recipients 299 setProgress(percent_per_sms * 20/100, IncProgress); 300 301 302 int recipientsNum = std::min(recipients.size(), (y+1)*MAX_MOBILE); 303 setStatus(LoginAccount::Status::AddingRecipients.arg(y*MAX_MOBILE + 1).arg(recipientsNum).arg(recipients.size())); 304 305 306 int percent_per_contact = (percent_per_sms * 60/100) / recipientsNum; 307 308 int ix = 0; 309 while (j.hasNext() && (ix < MAX_MOBILE)) { 310 _addRecipient(j.next()); 311 setProgress(percent_per_contact, IncProgress); 312 313 ix++; 314 } 315 316 setStatus(LoginAccount::Status::SendingSMS.arg(x+1).arg(longSMSLst.size())); 317 _sendSMS(i.next()); 318 setStatus(LoginAccount::Status::SMSSent, percent_per_sms * 20/100, IncProgress); 319 320 y++; 321 } while ((y * MAX_MOBILE) < recipients.size()); // Repeat this step until the sms is sent to all recipients (if there are more than allowed to be addressed at once) 322 323 x++; 324 } 325 326 _parseMainPage(); 327 setStatus(LoginAccount::Status::Logout, 90); 328 logout(); 329 330 // TODO: check if sending was cancelled -> set status to "user abort" 331 setStatus(LoginAccount::Status::SMSSent, 100); 300 332 301 333 }catch (EException e) { 302 334 // TODO 303 qDebug() << "An error occured: " + QString::fromLatin1(e.what());335 qDebug() << "An error occured: " + e.what(); 304 336 setStatus(LoginAccount::Status::Error); 305 337 }
Note: See TracChangeset
for help on using the changeset viewer.