/**
 * Construct a new PendingChannelRequest object that always fails.
 *
 * \param account Account to use.
 * \param errorName The name of a D-Bus error.
 * \param errorMessage The error message.
 */
PendingChannelRequest::PendingChannelRequest(const AccountPtr &account,
        const QString &errorName, const QString &errorMessage)
    : PendingOperation(ConnectionPtr()),
      mPriv(new Private(account->dbusConnection()))
{
    setFinishedWithError(errorName, errorMessage);
}
/**
 * Construct a new PendingChannelRequest object.
 *
 * \param account Account to use.
 * \param requestedProperties A dictionary containing the desirable properties.
 * \param userActionTime The time at which user action occurred, or QDateTime()
 *                       if this channel request is for some reason not
 *                       involving user action.
 * \param preferredHandler Either the well-known bus name (starting with
 *                         org.freedesktop.Telepathy.Client.) of the preferred
 *                         handler for this channel, or an empty string to
 *                         indicate that any handler would be acceptable.
 * \param create Whether createChannel or ensureChannel should be called.
 * \param account The account the request was made through.
 */
PendingChannelRequest::PendingChannelRequest(const AccountPtr &account,
        const QVariantMap &requestedProperties, const QDateTime &userActionTime,
        const QString &preferredHandler, bool create, const ChannelRequestHints &hints)
    : PendingOperation(account),
      mPriv(new Private(account->dbusConnection()))
{
    QString channelDispatcherObjectPath =
        QString(QLatin1String("/%1")).arg(TP_QT_IFACE_CHANNEL_DISPATCHER);
    channelDispatcherObjectPath.replace(QLatin1String("."), QLatin1String("/"));
    Client::ChannelDispatcherInterface *channelDispatcherInterface =
        account->dispatcherInterface();

    QDBusPendingCallWatcher *watcher = 0;
    if (create) {
        if (hints.isValid()) {
            if (account->supportsRequestHints()) {
                watcher = new QDBusPendingCallWatcher(
                    channelDispatcherInterface->CreateChannelWithHints(
                        QDBusObjectPath(account->objectPath()),
                        requestedProperties,
                        userActionTime.isNull() ? 0 : userActionTime.toTime_t(),
                        preferredHandler, hints.allHints()), this);
            } else {
                warning() << "Hints passed to channel request won't have an effect"
                    << "because the Channel Dispatcher service in use is too old";
            }
        }

        if (!watcher) {
            watcher = new QDBusPendingCallWatcher(
                    channelDispatcherInterface->CreateChannel(
                        QDBusObjectPath(account->objectPath()),
                        requestedProperties,
                        userActionTime.isNull() ? 0 : userActionTime.toTime_t(),
                        preferredHandler), this);
        }
    } else {
        if (hints.isValid()) {
            if (account->supportsRequestHints()) {
                watcher = new QDBusPendingCallWatcher(
                    channelDispatcherInterface->EnsureChannelWithHints(
                        QDBusObjectPath(account->objectPath()),
                        requestedProperties,
                        userActionTime.isNull() ? 0 : userActionTime.toTime_t(),
                        preferredHandler, hints.allHints()), this);
            } else {
                warning() << "Hints passed to channel request won't have an effect"
                    << "because the Channel Dispatcher service in use is too old";
            }
        }

        if (!watcher) {
            watcher = new QDBusPendingCallWatcher(
                    channelDispatcherInterface->EnsureChannel(
                        QDBusObjectPath(account->objectPath()),
                        requestedProperties,
                        userActionTime.isNull() ? 0 : userActionTime.toTime_t(),
                        preferredHandler), this);
        }
    }

    // TODO: This is a Qt bug fixed upstream, should be in the next Qt release.
    //       We should not need to check watcher->isFinished() here, remove the
    //       check when we depend on the fixed Qt version.
    if (watcher->isFinished()) {
        onWatcherFinished(watcher);
    } else {
        connect(watcher,
                SIGNAL(finished(QDBusPendingCallWatcher*)),
                SLOT(onWatcherFinished(QDBusPendingCallWatcher*)));
    }
}