void GlobalPresence::onAccountAdded(const Tp::AccountPtr &account)
{
    connect(account.data(), SIGNAL(connectionStatusChanged(Tp::ConnectionStatus)), SLOT(onConnectionStatusChanged()));
    connect(account.data(), SIGNAL(requestedPresenceChanged(Tp::Presence)), SLOT(onRequestedPresenceChanged()));
    connect(account.data(), SIGNAL(currentPresenceChanged(Tp::Presence)), SLOT(onCurrentPresenceChanged()));

    Q_EMIT enabledAccountsChanged();
}
void GlobalPresence::onCurrentPresenceChanged()
{
    /* basic idea of choosing global presence it to make it reflects the presence
     * over all accounts, usually this is used to indicates user the whole system
     * status.
     *
     * If there isn't any account, currentPresence should be offline, since there is nothing
     * online.
     * If there's only one account, then currentPresence should represent the presence
     * of this account.
     * If there're more than one accounts, the situation is more complicated.
     * There can be some accounts is still connecting (thus it's offline), and there can be
     * some accounts doesn't support the presence you're choosing. The user-chosen presence
     * priority will be higher than standard presence order.
     *
     * Example:
     * user choose to be online, 1 account online, 1 account offline, current presence
     * should be online, since online priority is higher than offline.
     * user chooses a presence supported by part of the account, current presence will be
     * the one chosen by user, to indicate there is at least one account supports it.
     * user choose a presence supported by no account, current presence will be chosen
     * from all accounts based on priority, and it also indicates there is no account support
     * the user-chosen presence.
     */
    Tp::Presence highestCurrentPresence = Tp::Presence::offline();
    Q_FOREACH(const Tp::AccountPtr &account, m_enabledAccounts->accounts()) {
        if (account->currentPresence().type() == m_requestedPresence.type()) {
            highestCurrentPresence = account->currentPresence();
            break;
        }

        if (Presence::sortPriority(account->currentPresence().type()) < Presence::sortPriority(highestCurrentPresence.type())) {
            highestCurrentPresence = account->currentPresence();
        }
    }

    qCDebug(KTP_COMMONINTERNALS) << "Current presence changed";

    if (highestCurrentPresence.type() != m_currentPresence.type() ||
            highestCurrentPresence.status() != m_currentPresence.status() ||
            highestCurrentPresence.statusMessage() != m_currentPresence.statusMessage()) {

        m_currentPresence = Presence(highestCurrentPresence);
        Q_EMIT currentPresenceChanged(m_currentPresence);
    }

    onChangingPresence();
}
void PresenceSource::onAccountReady(Tp::PendingOperation *op)
{
    // Check if the operation succeeded or not
    if (op->isError()) {
        kWarning() << "PresenceSource::onAccountReady: readying "
            "Account failed:" << op->errorName() << ":" << op->errorMessage();
        return;
    }

    // Check that the account is valid
    if (!m_account->isValidAccount()) {
        // TODO should source be removed?
        kWarning() << "Invalid account in source:" << objectName();
        return;
    }

    // set protocol icon
    setData("AccountIcon", m_account->iconName());

    connect(m_account.data(),
            SIGNAL(currentPresenceChanged(Tp::Presence)),
            SLOT(onAccountCurrentPresenceChanged(Tp::Presence)));
    connect(m_account.data(),
            SIGNAL(nicknameChanged(const QString &)),
            SLOT(onNicknameChanged(const QString &)));
    connect(m_account.data(),
            SIGNAL(displayNameChanged(const QString &)),
            SLOT(onDisplayNameChanged(const QString &)));
    connect(m_account.data(),
            SIGNAL(avatarChanged(const Tp::Avatar &)),
            SLOT(onAvatarChanged(const Tp::Avatar &)));
    connect(m_account.data(),
            SIGNAL(iconNameChanged(QString)),
            SLOT(onIconNameChanged(QString)));
    connect(m_account.data(),
            SIGNAL(stateChanged(bool)),
            SLOT(onStateChanged(bool)));

    // Force initial settings
    onAccountCurrentPresenceChanged(m_account->currentPresence());
    onNicknameChanged(m_account->nickname());
    onDisplayNameChanged(m_account->displayName());
    onAvatarChanged(m_account->avatar());
    onStateChanged(m_account->isEnabled());
}
AppletAvialabilityWidget::AppletAvialabilityWidget(QGraphicsItem *parent, Tp::AccountPtr account):
    MWidget(parent),
    m_account(account)
{
    setMaximumSize(64,64);
    setMinimumSize(64,64);

    MFeedback* feedback1 = new MFeedback("priority2_static_press", this);
    connect(this, SIGNAL(pressed()), feedback1, SLOT(play()));
    MFeedback* feedback2 = new MFeedback("priority2_static_release", this);
    connect(this, SIGNAL(released()), feedback2, SLOT(play()));
    MFeedback* feedback3 = new MFeedback("priority2_grab", this);
    connect(this, SIGNAL(longpressed()), feedback3, SLOT(play()));

    m_iconService = new MImageWidget("icon-m-common-presence-unknown", this);
    m_iconService->setAspectRatioMode(Qt::KeepAspectRatio);
    m_iconService->setMaximumSize(64,64);
    m_iconService->setMinimumSize(64,64);
    m_iconService->setPos(0,0);

    m_iconPresence = new MImageWidget("icon-m-common-presence-unknown", this);
    m_iconPresence->setAspectRatioMode(Qt::KeepAspectRatio);
    m_iconPresence->setMaximumSize(24,24);
    m_iconPresence->setPos(40, 0);
    currentIconId = presenceToIconId(m_account->currentPresence());
    requestedIconId = presenceToIconId(m_account->requestedPresence());

    if (m_account->currentPresence().type() == Tp::ConnectionPresenceTypeOffline)
        nextPresence = Tp::Presence(Tp::ConnectionPresenceTypeAvailable, "", "");
    else
        nextPresence = Tp::Presence(Tp::ConnectionPresenceTypeOffline, "", "");

    m_iconPresence->setImage(currentIconId);

    isFakePresence = false;

    m_account->setConnectsAutomatically(false);

    QString serviceName = m_account->serviceName();
    Accounts::Manager *manager = new Accounts::Manager();
    Accounts::Service *service = manager->service(serviceName);
    QString serviceIconId = service->iconName();

    m_iconService->setImage(serviceIconId);

    connect(m_account.data(),
            SIGNAL(changingPresence(bool)),
            SLOT(onChangingPresence(bool)));

    connect(m_account.data(),
            SIGNAL(currentPresenceChanged(Tp::Presence)),
            SLOT(onCurrentPresenceChanged(Tp::Presence)));

    connect(m_account.data(),
            SIGNAL(requestedPresenceChanged(Tp::Presence)),
            SLOT(onRequestedPresenceChanged(Tp::Presence)));

    connect(m_account.data(),
            SIGNAL(removed()),
            SLOT(onRemoved()));

    connect(m_account.data(),
            SIGNAL(stateChanged(bool)),
            SLOT(onStateChanged(bool)));

    presenceFaker = new QTimer(this);
    connect(presenceFaker, SIGNAL(timeout()), this, SLOT(fakerupdate()));

    grabGesture(Qt::TapAndHoldGesture);

//    pressTime = new QTimer(this);
//    pressTime->setSingleShot(true);
//    pressTime->setInterval(800);
//    connect(pressTime, SIGNAL(timeout()), this, SLOT(openPresence()));
}
void ContactWrapper::setupAccountConnects()
{
    // keep track of presence (online/offline is all we need)
    connect(m_account.data(), SIGNAL(connectionChanged(Tp::ConnectionPtr)), this, SLOT(onConnectionChanged(Tp::ConnectionPtr)));
    connect(m_account.data(), SIGNAL(currentPresenceChanged(Tp::Presence)), this, SIGNAL(accountPresenceChanged()));
}