File indexing completed on 2024-04-21 15:43:00

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-search.h"
0022 #include "entity.h"
0023 #include "log-manager.h"
0024 #include "search-hit.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 PendingSearch::Private
0037 {
0038     LogManagerPtr manager;
0039     QString text;
0040     EventTypeMask typeMask;
0041     SearchHitList hits;
0042 
0043     static void callback(void *logManager, void *result, PendingSearch *self);
0044 };
0045 
0046 PendingSearch::PendingSearch(const LogManagerPtr & manager, const QString &text, EventTypeMask typeMask)
0047     : PendingOperation(),
0048       mPriv(new Private())
0049 {
0050     mPriv->manager = manager;
0051     mPriv->text = text;
0052     mPriv->typeMask = typeMask;
0053 }
0054 
0055 PendingSearch::~PendingSearch()
0056 {
0057     delete mPriv;
0058 }
0059 
0060 void PendingSearch::start()
0061 {
0062     tpl_log_manager_search_async(
0063         TPLoggerQtWrapper::unwrap<TplLogManager, LogManager>(mPriv->manager),
0064         mPriv->text.toUtf8(),
0065         mPriv->typeMask,
0066         (GAsyncReadyCallback) Private::callback,
0067         this);
0068 }
0069 
0070 SearchHitList PendingSearch::hits() const
0071 {
0072     if (!isFinished()) {
0073         qWarning() << "PendingSearch::dates called before finished, returning empty";
0074         return SearchHitList();
0075     } else if (!isValid()) {
0076         qWarning() << "PendingSearch::dates called when not valid, returning empty";
0077         return SearchHitList();
0078     }
0079 
0080     return mPriv->hits;
0081 }
0082 
0083 void PendingSearch::Private::callback(void *logManager, void *result, PendingSearch *self)
0084 {
0085     if (!TPL_IS_LOG_MANAGER(logManager)) {
0086         self->setFinishedWithError(TP_QT_ERROR_INVALID_ARGUMENT, "Invalid log manager in callback");
0087         return;
0088     }
0089 
0090     if (!G_IS_ASYNC_RESULT(result)) {
0091         self->setFinishedWithError(TP_QT_ERROR_INVALID_ARGUMENT, "Invalid async result in callback");
0092         return;
0093     }
0094 
0095     GList *hits = NULL;
0096     GError *error = NULL;
0097     gboolean success = tpl_log_manager_search_finish(TPL_LOG_MANAGER(logManager), G_ASYNC_RESULT(result), &hits, &error);
0098     if (error) {
0099         self->setFinishedWithError(TP_QT_ERROR_INVALID_ARGUMENT, error->message);
0100         g_error_free(error);
0101         return;
0102     }
0103 
0104     if (!success) {
0105         self->setFinishedWithError(TP_QT_ERROR_INVALID_ARGUMENT, "Query failed without specific error");
0106         return;
0107     }
0108 
0109     GList *i;
0110     int count = 0;
0111     for (i = hits; i; i = i->next) {
0112         TplLogSearchHit *item = (TplLogSearchHit *) i->data;
0113         qDebug() << "hit " << count++ << "account=" << item->account
0114                   << "date=" << g_date_get_year(item->date) << g_date_get_month(item->date) << g_date_get_day(item->date)
0115                   << "target=" << item->target
0116                   << tpl_entity_get_identifier(item->target)
0117                   << "/" << tpl_entity_get_alias(item->target)
0118                   << "/" << tpl_entity_get_entity_type(item->target)
0119                   << "/" << tpl_entity_get_avatar_token(item->target);
0120     QDate date;
0121     if (item->date) {
0122         date.setDate(item->date->year, item->date->month, item->date->day);
0123     }
0124         SearchHit hit(Utils::instance()->accountPtr(item->account), TPLoggerQtWrapper::wrap<TplEntity, Entity>(item->target, true), date);
0125         self->mPriv->hits << hit;
0126     }
0127 
0128     tpl_log_manager_search_free(hits);
0129 
0130     self->setFinished();
0131 }