Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion plugins/history/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
)

########### next target ###############
set(kopete_history_PART_SRCS chathistoryhandler.cpp)
set(kopete_history_PART_SRCS chathistoryplugin.cpp databaseconstants.cpp databasemanager.cpp)
kde4_add_plugin(kopete_history ${kopete_history_PART_SRCS} )
target_link_libraries(kopete_history ${KDE4_KHTML_LIBS} kopete ${QT_QTSQL_LIBRARY})

Expand Down
29 changes: 0 additions & 29 deletions plugins/history/chathistoryhandler.cpp

This file was deleted.

28 changes: 0 additions & 28 deletions plugins/history/chathistoryhandler.h

This file was deleted.

41 changes: 41 additions & 0 deletions plugins/history/chathistoryplugin.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include "chathistoryplugin.h"
#include "databasemanager.h"
#include "kopetemessage.h"
#include "kopetechatsessionmanager.h"
#include <kaboutdata.h>
#include <kgenericfactory.h>
#include "kapplication.h"

ChatHistoryPlugin *ChatHistoryPlugin::mInstance = 0;

K_PLUGIN_FACTORY(ChatHistoryPluginFactory, registerPlugin<ChatHistoryPlugin>();)
K_EXPORT_PLUGIN(ChatHistoryPluginFactory( "kopete_history" ))

ChatHistoryPlugin::ChatHistoryPlugin(QObject *parent, const QVariantList &)
: Kopete::Plugin(ChatHistoryPluginFactory::componentData(), parent)
{
//Initialize the database.
//TODO: Implement other DB Systems (MySQL, PostgreSQL etc)
DatabaseManager::instance()->initDatabase(DatabaseManager::SQLITE);
connect (Kopete::ChatSessionManager::self(), SIGNAL(aboutToDisplay(Kopete::Message&)),
this, SLOT(logMessage(Kopete::Message&)));
}

ChatHistoryPlugin::~ChatHistoryPlugin()
{

}

ChatHistoryPlugin *ChatHistoryPlugin::instance()
{
if (!mInstance) {
mInstance = new ChatHistoryPlugin(0, QVariantList());
}

return mInstance;
}

void ChatHistoryPlugin::logMessage(Kopete::Message &message)
{
DatabaseManager::instance()->insertMessage(message);
}
40 changes: 40 additions & 0 deletions plugins/history/chathistoryplugin.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#ifndef CHATHISTORYPLUGIN_H
#define CHATHISTORYPLUGIN_H

#include <QVariantList>
#include "kopeteplugin.h"
#include <QObject>

namespace Kopete {
class Message;
}

/**
* The ChatHistoryPlugin class is used to handle all history connected activities, and
* then call the respective class (logger, searcher etc).
*/
class ChatHistoryPlugin : public Kopete::Plugin
{
Q_OBJECT
public:
/**
* Constructs a new ChatHistoryPlugin class instance. There should only be one
* instance for every instance of Kopete running.
*/
explicit ChatHistoryPlugin(QObject *parent, const QVariantList &);

~ChatHistoryPlugin();
static ChatHistoryPlugin *instance();

public slots:
/**
* Insert a new chat message to the database.
* @param message The message to be logged. The message details to be stored
* in the database will be extracted from here.
*/
void logMessage(Kopete::Message &message);
private:
static ChatHistoryPlugin *mInstance;
};

#endif // CHATHISTORYPLUGIN_H
140 changes: 140 additions & 0 deletions plugins/history/databaseconstants.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
#include "databaseconstants.h"

QString DatabaseConstants::columnId()
{
return "id";
}

QString DatabaseConstants::columnTimeStamp()
{
return "timestamp";
}

QString DatabaseConstants::columnMessage()
{
return "message";
}

QString DatabaseConstants::columnProtocol()
{
return "protocol";
}

QString DatabaseConstants::columnAccount()
{
return "account";
}

QString DatabaseConstants::columnDirection()
{
return "direction";
}

QString DatabaseConstants::columnImportance()
{
return "importance";
}

QString DatabaseConstants::columnContact()
{
return "contact";
}

QString DatabaseConstants::columnSubject()
{
return "subject";
}

QString DatabaseConstants::columnSession()
{
return "session";
}

QString DatabaseConstants::columnSessionName()
{
return "session_name";
}

QString DatabaseConstants::columnFrom()
{
return "from";
}

QString DatabaseConstants::columnFromName()
{
return "from_name";
}

QString DatabaseConstants::columnTo()
{
return "to";
}

QString DatabaseConstants::columnToName()
{
return "to_name";
}

QString DatabaseConstants::columnState()
{
return "state";
}

QString DatabaseConstants::columnType()
{
return "type";
}

QString DatabaseConstants::columnIsGroup()
{
return "is_group";
}

QString DatabaseConstants::sql_prepareForMessageInsert()
{
return "INSERT INTO `messages` (`" + columnTimeStamp() + "`, `" + columnMessage()
+ "`, `" + columnAccount() + "`, `" + columnProtocol() + "`, `"
+ columnDirection() + "`, `" + columnImportance() + "`, `"
+ columnContact() + "`, `" + columnSubject() + "`, "
" `" + columnSession() + "`, `" + columnSessionName() + "`, `"
+ columnFrom() + "`, `" + columnFromName() + "`, `" + columnTo()
+ "`, `" + columnToName() + "`, `" + columnState() + "`, `"
+ columnType() + "`, `" + columnIsGroup() + "`) "
" VALUES (:" + columnTimeStamp() + ", :" + columnMessage() + ", :"
+ columnAccount() + ", :" + columnProtocol() + ", :"
+ columnDirection() + ", :" + columnImportance() + ", :"
+ columnContact() + ", :" + columnSubject() + ", :"
+ columnSession() + ", :" + columnSessionName() + ", "
" :" + columnFrom() + ", :" + columnFromName() + ", :"
+ columnTo() + ", :" + columnToName() + ", :"
+ columnState() + ", :" + columnType() + ", :"
+ columnIsGroup() + ")";
}

QString DatabaseConstants::sql_createMessagesTable()
{
return "CREATE TABLE IF NOT EXISTS \"messages\" ("
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any other way of using database statements, other than generating string queries ourselves ?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am working on seeing if there is another way to do this.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about we write for ourselves a really Simple Query builder ? we can either use a Builder Pattern or anything else of your choice.

"\"" + columnId() + "\" Integer Primary Key Autoincrement Not Null, "
"\"" + columnTimeStamp() + "\" Integer, "
"\"" + columnMessage() + "\" Text, "
"\"" + columnProtocol() + "\" Text Not Null, "
"\"" + columnAccount() + "\" Text Not Null, "
"\"" + columnDirection() + "\" Integer Not Null, "
"\"" + columnImportance() + "\" Integer, "
"\"" + columnContact() + "\" Text, "
"\"" + columnSubject() + "\" Text, "
"\"" + columnSession() + "\" Text, "
"\"" + columnSessionName() + "\" Text, "
"\"" + columnFrom() + "\" Text, "
"\"" + columnFromName() + "\" Text, "
"\"" + columnTo() + "\" Text, "
"\"" + columnToName() + "\" Text, "
"\"" + columnState() + "\" Integer, "
"\"" + columnType() + "\" Integer, "
"\"" + columnIsGroup() + "\" Integer Default'0') ";
}

QString DatabaseConstants::sql_getContactList(QString accountId)
{
return "SELECT DISTINCT contact, protocol FROM messages WHERE account = '" + accountId + "'";
}
55 changes: 55 additions & 0 deletions plugins/history/databaseconstants.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#ifndef DATABASECONSTANTS_H
#define DATABASECONSTANTS_H

#include <QObject>

/**
* The DatabaseConstants class is used to provide static representations
* of the various strings used by the database in the history plugin.
*/
class DatabaseConstants : public QObject
{
Q_OBJECT
public:
explicit DatabaseConstants(QObject *parent = 0);
/**
* These are the names of the various database columns
*/
static QString columnId();
static QString columnTimeStamp();
static QString columnMessage();
static QString columnProtocol();
static QString columnAccount();
static QString columnDirection();
static QString columnImportance();
static QString columnContact();
static QString columnSubject();
static QString columnSession();
static QString columnSessionName();
static QString columnFrom();
static QString columnFromName();
static QString columnTo();
static QString columnToName();
static QString columnState();
static QString columnType();
static QString columnIsGroup();

/**
* Query string for inserting a new message
*/
static QString sql_prepareForMessageInsert();

/**
* Query string for creating the messsages table
*/
static QString sql_createMessagesTable();

/**
* Query string to retrieve the contacts in an account who have chat history.
* @param accountId The account to search for.
* @return
*/
static QString sql_getContactList(QString accountId = QString());
};

#endif // DATABASECONSTANTS_H
Loading