Commit 71f1f797 authored by David Edmundson's avatar David Edmundson
Browse files

Start of a log viewer

parent ec32f257
......@@ -32,3 +32,4 @@ add_subdirectory(app)
add_subdirectory(config)
add_subdirectory(data)
add_subdirectory(adiumxtra-protocol-handler)
add_subdirectory(logviewer)
macro_optional_find_package(TelepathyLoggerQt4)
macro_log_feature(TELEPATHY_LOGGER_QT4_FOUND "TelepathyLoggerQt4" "Qt4 wrapper around telepathy-logger" FALSE "" "Needed for optional backlog support in the chat-window")
macro_display_feature_log()
#Only do anything with this app if the logger is available.
if(TELEPATHY_LOGGER_QT4_FOUND)
add_definitions( -DTELEPATHY_LOGGER_QT4_FOUND )
set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TELEPATHY_LOGGER_QT4_DEFINITIONS}" )
include_directories(${CMAKE_SOURCE_DIR}/lib
${TELEPATHY_LOGGER_QT4_INCLUDE_DIRS}
)
set(ktp-log-viewer_SRCS
main.cpp
log-viewer.cpp
entity-model.cpp
message-view.cpp
)
kde4_add_ui_files(ktp-log-viewer_SRCS log-viewer.ui)
kde4_add_executable(ktp-log-viewer ${ktp-log-viewer_SRCS})
target_link_libraries(ktp-log-viewer
${KDE4_KDECORE_LIBS}
${KDE4_KDEUI_LIBS}
${KDE4_KIO_LIBS}
${TELEPATHY_QT4_LIBRARIES}
${KDE4_KEMOTICONS_LIBS}
${KDE4_KCMUTILS_LIBS}
${TELEPATHY_LOGGER_QT4_LIBRARIES}
ktpchat)
# change to binary install dir
# install(TARGETS ktp-log-viewer DESTINATION ${LIBEXEC_INSTALL_DIR})
endif()
#include "entity-model.h"
#include <TelepathyLoggerQt4/LogManager>
#include <TelepathyLoggerQt4/PendingEntities>
#include <TelepathyLoggerQt4/PendingOperation>
#include <TelepathyLoggerQt4/Entity>
#include <TelepathyQt/AccountManager>
#include <TelepathyQt/Account>
#include <QPixmap>
EntityModel::EntityModel(QObject *parent) :
QAbstractListModel(parent)
{
}
void EntityModel::setAccountManager(const Tp::AccountManagerPtr &accountManager)
{
Tpl::LogManagerPtr logManager = Tpl::LogManager::instance();
Q_FOREACH(const Tp::AccountPtr &account, accountManager->allAccounts()) {
connect(logManager->queryEntities(account),
SIGNAL(finished(Tpl::PendingOperation*)),
SLOT(onEntitiesSearchFinished(Tpl::PendingOperation*)));
}
}
int EntityModel::rowCount(const QModelIndex &parent) const
{
if (parent == QModelIndex()) {
return m_entities.size();
}
else {
return 0;
}
}
QVariant EntityModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid()) {
return QVariant();
}
Tpl::EntityPtr entity = m_entities[index.row()].entity;
switch (role) {
case Qt::DisplayRole:
return entity->alias();
case Qt::DecorationRole:
return QPixmap(entity->avatarToken());
case EntityModel::IdRole:
return entity->identifier();
case EntityModel::TypeRole:
return entity->entityType();
case EntityModel::EntityRole:
return QVariant::fromValue(entity);
case EntityModel::AccountRole:
return QVariant::fromValue(m_entities[index.row()].account);
}
return QVariant();
}
void EntityModel::onEntitiesSearchFinished(Tpl::PendingOperation *operation)
{
Tpl::PendingEntities *pendingEntities = qobject_cast<Tpl::PendingEntities*>(operation);
Tpl::EntityPtrList newEntries = pendingEntities->entities();
if (newEntries.size() > 0) {
beginInsertRows(QModelIndex(), m_entities.size(), m_entities.size() + newEntries.size()-1);
Q_FOREACH(const Tpl::EntityPtr entity, newEntries) {
EntityModelItem item;
item.account = pendingEntities->account();
item.entity = entity;
qDebug() << entity->alias();
m_entities.append(item);
}
endInsertRows();
}
}
#ifndef ENTITYMODEL_H
#define ENTITYMODEL_H
#include <QAbstractListModel>
#include <TelepathyQt/Types>
#include <TelepathyLoggerQt4/Entity>
#include <TelepathyQt/Account>
/**
Lists all avilable entities.
roles:
- Qt::DisplayRole - name
- Qt::DecorationRole - avatar
- EntityModel::IdRole
- EntityModel::TypeRole - EntityType (EntityTypeContact/Room/Self/Unknown)
- EntityModel::EntityRole - relevant Tpl::EntityPtr
*/
namespace Tpl{
class PendingOperation;
}
class EntityModelItem {
public:
Tpl::EntityPtr entity;
Tp::AccountPtr account;
};
class EntityModel : public QAbstractListModel
{
Q_OBJECT
public:
enum Role {
IdRole = Qt::UserRole,
TypeRole,
EntityRole,
AccountRole
};
explicit EntityModel(QObject *parent = 0);
void setAccountManager(const Tp::AccountManagerPtr &accountManager);
int rowCount(const QModelIndex &parent) const;
QVariant data(const QModelIndex &index, int role) const;
Q_SIGNALS:
public Q_SLOTS:
private Q_SLOTS:
void onEntitiesSearchFinished(Tpl::PendingOperation*);
private:
QList<EntityModelItem> m_entities;
};
Q_DECLARE_METATYPE(Tpl::EntityPtr);
Q_DECLARE_METATYPE(Tp::AccountPtr);
#endif // ENTITYMODEL_H
#include "log-viewer.h"
#include "ui_log-viewer.h"
#include <TelepathyQt/AccountManager>
#include <TelepathyQt/PendingReady>
#include <TelepathyLoggerQt4/Init>
#include <TelepathyLoggerQt4/Entity>
#include <TelepathyLoggerQt4/LogManager>
#include <glib-object.h>
#include <QGlib/Init>
#include "entity-model.h"
LogViewer::LogViewer(QWidget *parent) :
QWidget(parent),
ui(new Ui::LogViewer)
{
ui->setupUi(this);
Tp::registerTypes();
g_type_init();
QGlib::init(); //are these 4 really needed?
Tpl::init();
m_accountManager = Tp::AccountManager::create();
m_entityModel = new EntityModel(this);
ui->entityList->setModel(m_entityModel);
//TODO parse args and update all views as appropriate
connect(m_accountManager->becomeReady(), SIGNAL(finished(Tp::PendingOperation*)), SLOT(onAccountManagerReady()));
connect(ui->entityList, SIGNAL(activated(QModelIndex)), SLOT(onEntitySelected(QModelIndex)));
}
LogViewer::~LogViewer()
{
delete ui;
}
void LogViewer::onAccountManagerReady()
{
Tpl::LogManagerPtr logManager = Tpl::LogManager::instance();
logManager->setAccountManagerPtr(m_accountManager);
m_entityModel->setAccountManager(m_accountManager);
}
void LogViewer::onEntitySelected(const QModelIndex &index)
{
//calendar needs to get pendingDates
Tpl::EntityPtr entity = index.data(EntityModel::EntityRole).value<Tpl::EntityPtr>();
Tp::AccountPtr account = index.data(EntityModel::AccountRole).value<Tp::AccountPtr>();
ui->messageView->loadLog(account, entity, QDate::currentDate());
//main view needs to show pendingEvents
}
void LogViewer::onDateSelected()
{
//update main view
}
#ifndef LOGVIEWER_H
#define LOGVIEWER_H
#include <QWidget>
#include <TelepathyQt/AccountManager>
namespace Ui {
class LogViewer;
}
class EntityModel;
class LogViewer : public QWidget
{
Q_OBJECT
public:
explicit LogViewer(QWidget *parent = 0);
~LogViewer();
private Q_SLOTS:
void onAccountManagerReady();
void onEntitySelected(const QModelIndex &index);
void onDateSelected();
private:
Ui::LogViewer *ui;
Tp::AccountManagerPtr m_accountManager;
EntityModel *m_entityModel;
};
#endif // LOGVIEWER_H
#include <QtGui/QApplication>
#include "log-viewer.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
LogViewer w;
w.show();
return a.exec();
}
#include "message-view.h"
#include "adium-theme-view.h"
#include <KDebug>
#include <TelepathyLoggerQt4/LogManager>
#include <TelepathyLoggerQt4/PendingEvents>
#include <TelepathyLoggerQt4/TextEvent>
#include <TelepathyQt/Account>
MessageView::MessageView(QWidget *parent) :
AdiumThemeView(parent)
{
connect(this, SIGNAL(loadFinished(bool)), SLOT(onLoadFinished()));
}
void MessageView::loadLog(const Tp::AccountPtr &account, const Tpl::EntityPtr &entity, const QDate &date)
{
m_account = account;
m_entity = entity;
m_date = date;
//FIXME check entity type
load(AdiumThemeView::SingleUserChat);
AdiumThemeHeaderInfo headerInfo;
headerInfo.setDestinationDisplayName(m_entity->alias());
headerInfo.setChatName(m_entity->alias());
// other headerInfo here.
initialise(headerInfo);
}
void MessageView::onLoadFinished()
{
//load stuff here.
qDebug() << "load stuff";
Tpl::LogManagerPtr logManager = Tpl::LogManager::instance();
Tpl::PendingEvents *pendingEvents = logManager->queryEvents(m_account, m_entity, Tpl::EventTypeMaskText, m_date);
connect(pendingEvents, SIGNAL(finished(Tpl::PendingOperation*)), SLOT(onEventsLoaded(Tpl::PendingOperation*)));
}
void MessageView::onEventsLoaded(Tpl::PendingOperation *po)
{
Tpl::PendingEvents *pe = qobject_cast<Tpl::PendingEvents*>(po);
QList<AdiumThemeContentInfo> messages;
qDebug() << "showing " << pe->events().size() << "messages";
Q_FOREACH(const Tpl::EventPtr &event, pe->events()) {
const Tpl::TextEventPtr textEvent(event.staticCast<Tpl::TextEvent>());
AdiumThemeMessageInfo::MessageType type;
QString iconPath;
if(event->sender()->identifier() == m_account->normalizedName()) {
type = AdiumThemeMessageInfo::HistoryLocalToRemote;
} else {
type = AdiumThemeMessageInfo::HistoryRemoteToLocal;
}
AdiumThemeContentInfo message(type);
message.setMessage(textEvent->message());
message.setService(m_account->serviceName());
message.setSenderDisplayName(textEvent->sender()->alias());
message.setSenderScreenName(textEvent->sender()->alias());
message.setTime(textEvent->timestamp());
message.setUserIconPath(iconPath);
kDebug() << textEvent->timestamp()
<< "from" << textEvent->sender()->identifier()
<< "to" << textEvent->receiver()->identifier()
<< textEvent->message();
messages.append(message);
addContentMessage(message);
}
}
#ifndef MESSAGEVIEW_H
#define MESSAGEVIEW_H
#include "adium-theme-view.h"
#include <QDate>
#include <TelepathyLoggerQt4/Entity>
#include <TelepathyLoggerQt4/PendingOperation>
class MessageView : public AdiumThemeView
{
Q_OBJECT
public:
explicit MessageView(QWidget *parent = 0);
void loadLog(const Tp::AccountPtr &account, const Tpl::EntityPtr &entity, const QDate &date);
private Q_SLOTS:
void onLoadFinished();
void onEventsLoaded(Tpl::PendingOperation* po);
private:
Tpl::EntityPtr m_entity;
Tp::AccountPtr m_account;
QDate m_date;
};
#endif // MESSAGEVIEW_H
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment