File indexing completed on 2024-04-21 15:42:59

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