File indexing completed on 2024-04-14 15:05:39

0001 /*
0002  * This file is part of TelepathyLoggerQt
0003  *
0004  * Copyright (C) 2011 Collabora Ltd. <http://www.collabora.co.uk/>
0005  *
0006  * This library is free software; you can redistribute it and/or
0007  * modify it under the terms of the GNU Lesser General Public
0008  * License as published by the Free Software Foundation; either
0009  * version 2.1 of the License, or (at your option) any later version.
0010  *
0011  * This library is distributed in the hope that it will be useful,
0012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0014  * Lesser General Public License for more details.
0015  *
0016  * You should have received a copy of the GNU Lesser General Public
0017  * License along with this library; if not, write to the Free Software
0018  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
0019  */
0020 
0021 #include "pending-dates.h"
0022 #include "entity.h"
0023 #include "log-manager.h"
0024 #include "utils.h"
0025 
0026 #include <QtCore/QDebug>
0027 
0028 #include <TelepathyQt/Account>
0029 #include <telepathy-logger/log-manager.h>
0030 
0031 #include <glib.h>
0032 
0033 using namespace Tpl;
0034 
0035 struct TELEPATHY_LOGGER_QT_NO_EXPORT PendingDates::Private
0036 {
0037     LogManagerPtr manager;
0038     Tp::AccountPtr account;
0039     TpAccount *tpAccount;
0040     EntityPtr entity;
0041     EventTypeMask typeMask;
0042     QDateList dates;
0043 
0044     static void onAccountPrepared(void *logManager, void *result, PendingDates *self);
0045     static void callback(void *logManager, void *result, PendingDates *self);
0046 };
0047 
0048 PendingDates::PendingDates(const LogManagerPtr & manager, const Tp::AccountPtr & account,
0049     const EntityPtr & entity, EventTypeMask typeMask)
0050     : PendingOperation(),
0051       mPriv(new Private())
0052 {
0053     mPriv->manager = manager;
0054     mPriv->account = account;
0055     mPriv->tpAccount = 0;
0056     mPriv->entity = entity;
0057     mPriv->typeMask = typeMask;
0058 }
0059 
0060 PendingDates::~PendingDates()
0061 {
0062     delete mPriv;
0063 }
0064 
0065 void PendingDates::start()
0066 {
0067     mPriv->tpAccount = Utils::instance()->tpAccount(mPriv->account);
0068     if (!mPriv->tpAccount) {
0069         setFinishedWithError(TP_QT_ERROR_INVALID_ARGUMENT, "Invalid account");
0070         return;
0071     }
0072 
0073     GQuark features[] = { TP_ACCOUNT_FEATURE_CORE, 0 };
0074     tp_proxy_prepare_async(mPriv->tpAccount, features, (GAsyncReadyCallback) Private::onAccountPrepared, this);
0075 }
0076 
0077 void PendingDates::Private::onAccountPrepared(void *logManager, void *result, PendingDates *self)
0078 {
0079     tpl_log_manager_get_dates_async(
0080         TPLoggerQtWrapper::unwrap<TplLogManager, LogManager>(self->mPriv->manager),
0081         self->mPriv->tpAccount,
0082         TPLoggerQtWrapper::unwrap<TplEntity, Entity>(self->mPriv->entity),
0083         self->mPriv->typeMask,
0084         (GAsyncReadyCallback) Private::callback,
0085         self);
0086 }
0087 
0088 QDateList PendingDates::dates() const
0089 {
0090     if (!isFinished()) {
0091         qWarning() << "PendingDates::dates called before finished, returning empty";
0092         return QDateList();
0093     } else if (!isValid()) {
0094         qWarning() << "PendingDates::dates called when not valid, returning empty";
0095         return QDateList();
0096     }
0097 
0098     return mPriv->dates;
0099 }
0100 
0101 const Tp::AccountPtr PendingDates::account() const
0102 {
0103     if (!isFinished()) {
0104         qWarning() << "PendingDates::account called before finished";
0105     } else if (!isValid()) {
0106         qWarning() << "PendingDates::account called when not valid";
0107     }
0108 
0109     return mPriv->account;
0110 }
0111 
0112 const EntityPtr PendingDates::entity() const
0113 {
0114     if (!isFinished()) {
0115         qWarning() << "PendingDates::entity called before finished";
0116     } else if (!isValid()) {
0117         qWarning() << "PendingDates::entity called when not valid";
0118     }
0119 
0120     return mPriv->entity;
0121 }
0122 
0123 
0124 void PendingDates::Private::callback(void *logManager, void *result, PendingDates *self)
0125 {
0126     if (!TPL_IS_LOG_MANAGER(logManager)) {
0127         self->setFinishedWithError(TP_QT_ERROR_INVALID_ARGUMENT, "Invalid log manager in callback");
0128         return;
0129     }
0130 
0131     if (!G_IS_ASYNC_RESULT(result)) {
0132         self->setFinishedWithError(TP_QT_ERROR_INVALID_ARGUMENT, "Invalid async result in callback");
0133         return;
0134     }
0135 
0136     GList *dates = NULL;
0137     GError *error = NULL;
0138     gboolean success = tpl_log_manager_get_dates_finish(TPL_LOG_MANAGER(logManager), G_ASYNC_RESULT(result), &dates, &error);
0139     if (error) {
0140         self->setFinishedWithError(TP_QT_ERROR_INVALID_ARGUMENT, error->message);
0141         g_error_free(error);
0142         return;
0143     }
0144 
0145     if (!success) {
0146         self->setFinishedWithError(TP_QT_ERROR_INVALID_ARGUMENT, "Query failed without specific error");
0147         return;
0148     }
0149 
0150     GList *i;
0151     for (i = dates; i; i = i->next) {
0152         GDate * item = (GDate *) i->data;
0153         self->mPriv->dates << QDate(item->year, item->month, item->day);
0154     }
0155 
0156     g_list_foreach(dates, (GFunc) g_date_free, NULL);
0157     g_list_free(dates);
0158 
0159     self->setFinished();
0160 }