void SyncSession::UpdateRetryActivity() { MojString retryActName; m_builderFactory->GetFolderRetrySyncActivityName(retryActName, m_folderId); // Cancel retry activity on successful sync ActivityPtr activity = m_activities->GetOrCreateActivity(retryActName); activity->SetEndAction(Activity::EndAction_Cancel); // We want to remove the retry activity last, only after we're sure that the // scheduled sync and watch activities have been created. activity->SetEndOrder(Activity::EndOrder_Last); }
UpdateAccountCommand::UpdateAccountCommand(ImapClient& client, ActivityPtr activity, bool credentialsChanged) : ImapClientCommand(client), m_activitySet(new ActivitySet(client)), m_credentialsChanged(credentialsChanged), m_getAccountTransportSlot(this, &UpdateAccountCommand::GetAccountTransportResponse), m_updateFolderActivitiesSlot(this, &UpdateAccountCommand::UpdateFolderActivitiesDone), m_endActivitiesSlot(this, &UpdateAccountCommand::ActivitiesEnded), m_notifySmtpSlot(this, &UpdateAccountCommand::NotifySmtpResponse) { // FIXME wait for adoption to complete // EndActivities will wait for us, but it's not as polite if(activity.get() && activity->CanAdopt()) { m_activitySet->AddActivity(activity); activity->Adopt(client); activity->SetEndOrder(Activity::EndOrder_Last); } }
void ImapSession::CommandComplete(Command* command) { m_commandManager->CommandComplete(command); if(m_reconnectRequested && m_state == State_OkToSync && m_commandManager->GetActiveCommandCount() == 0 && m_commandManager->GetPendingCommandCount() > 0) { // If requested (and no active commands), disconnect from the server after finishing this command MojLogInfo(m_log, "disconnecting and reconnecting to server"); Logout(); } else if(m_state == State_OkToSync && m_commandManager->GetPendingCommandCount() == 0 && m_commandManager->GetActiveCommandCount() == 0) { MojLogInfo(m_log, "no commands active or pending"); // Either disconnect or run IDLE command // TODO also check account settings if(IsValidId(m_folderId) && IsPushEnabled(m_folderId)) { m_shouldPush = CheckNetworkHealthForPush(); } else { m_shouldPush = false; } if(m_shouldPush) { //MojLogInfo(m_log, "running idle command for folderId %s", AsJsonString(m_folderId).c_str()); if(m_account->IsYahoo()) { m_idleCommand.reset(new IdleYahooCommand(*this, m_folderId)); m_idleMode = IdleMode_YahooPush; } else { m_idleCommand.reset(new IdleCommand(*this, m_folderId)); m_idleMode = IdleMode_IDLE; } // Create activity to maintain idle. // If the device is rebooted or IMAP crashes, the callback will restart idle. // The activity will get cancelled if the connection goes into retry mode. if(true) { ImapActivityFactory factory; ActivityBuilder ab; factory.BuildStartIdle(ab, m_client->GetAccountId(), m_folderId); // Create new activity only if one doesn't already exist if(GetActivitySet()->FindActivityByName(ab.GetName()).get() == NULL) { ActivityPtr activity = Activity::PrepareNewActivity(ab, true, true); activity->SetName(ab.GetName()); activity->SetEndAction(Activity::EndAction_Complete); activity->SetEndOrder(Activity::EndOrder_Last); GetActivitySet()->AddActivity(activity); // Start activity right away activity->Create(*m_client); } } m_idleStartTime = 0; SetState(State_PreparingToIdle); m_commandManager->RunCommand(m_idleCommand); } else if(GetNoopIdleTimeout() > 0) { // TODO: this code is currently disabled MojLogInfo(m_log, "running NOOP idle command"); m_idleCommand.reset(new NoopIdleCommand(*this, m_folderId, GetNoopIdleTimeout())); m_idleMode = IdleMode_NOOP; m_idleStartTime = 0; // Reset flag m_recentUserInteraction = false; SetState(State_PreparingToIdle); m_commandManager->RunCommand(m_idleCommand); } else { if(IsPushRequested(m_folderId) && !m_shouldPush) { MojLogInfo(m_log, "setting up scheduled sync instead of push"); // If we can't push, set up a scheduled sync ImapActivityFactory factory; ActivityBuilder ab; factory.BuildScheduledSync(ab, m_client->GetAccountId(), m_folderId, FALLBACK_SYNC_INTERVAL, false); GetActivitySet()->ReplaceActivity(ab.GetName(), ab.GetActivityObject()); } else { MojLogInfo(m_log, "nothing left to do; disconnecting"); } Logout(); } } else if(m_state == State_Disconnecting && m_commandManager->GetActiveCommandCount() == 0) { Disconnected(); } }