Commit 03d82ee0 authored by Linus Jahn's avatar Linus Jahn Committed by GitHub

Switch to KDE's C++ coding style (#99)

I decided to keep using tabs for now. I know that KDE says to use spaces.

Closes #97.
parent 8c7a34cc
......@@ -63,8 +63,7 @@ Kaidan::Kaidan(Swift::NetworkFactories* networkFactories, QObject *parent) : QOb
password = settings->value("auth/password").toString();
// use Kaidan as resource, if no set
if (jidResource == "")
{
if (jidResource == "") {
jidResource = QString(APPLICATION_NAME);
settings->setValue("auth/resource", jidResource);
}
......@@ -86,8 +85,7 @@ Kaidan::Kaidan(Swift::NetworkFactories* networkFactories, QObject *parent) : QOb
Kaidan::~Kaidan()
{
if (connected)
{
if (connected) {
client->disconnect();
softwareVersionResponder->stop();
delete tracer;
......@@ -141,8 +139,7 @@ void Kaidan::mainConnect()
// we don't want to close client without disconnection
void Kaidan::mainDisconnect()
{
if (getConnectionState())
{
if (getConnectionState()) {
client->disconnect();
}
}
......@@ -193,13 +190,11 @@ QString Kaidan::getResourcePath(QString name_)
pathList << QString(DEBUG_SOURCE_PATH) + QString("/data"); // append debug directory
// search for file in directories
for(int i = 0; i < pathList.size(); i++)
{
for (int i = 0; i < pathList.size(); i++) {
// open directory
QDir directory(pathList.at(i));
// look up the file
if (directory.exists(name_))
{
if (directory.exists(name_)) {
// found the file, return the path
return QString("file://") + directory.absoluteFilePath(name_);
}
......@@ -211,14 +206,41 @@ QString Kaidan::getResourcePath(QString name_)
}
// if no jid or password, return true
bool Kaidan::newLoginNeeded() {return (jid == "") || (password == "");}
bool Kaidan::newLoginNeeded()
{
return (jid == "") || (password == "");
}
RosterController* Kaidan::getRosterController() {return rosterController;}
MessageController* Kaidan::getMessageController() {return messageController;}
VCardController* Kaidan::getVCardController() {return vCardController;}
bool Kaidan::getConnectionState() const {return connected;}
QString Kaidan::getVersionString() {return QString(VERSION_STRING);}
RosterController* Kaidan::getRosterController()
{
return rosterController;
}
MessageController* Kaidan::getMessageController()
{
return messageController;
}
VCardController* Kaidan::getVCardController()
{
return vCardController;
}
bool Kaidan::getConnectionState() const
{
return connected;
}
QString Kaidan::getVersionString()
{
return QString(VERSION_STRING);
}
QString Kaidan::getJid() {return jid;}
QString Kaidan::getJidResource() {return jidResource;}
QString Kaidan::getPassword() {return password;}
QString Kaidan::getJid()
{
return jid;
}
QString Kaidan::getJidResource()
{
return jidResource;
}
QString Kaidan::getPassword()
{
return password;
}
......@@ -96,8 +96,7 @@ void MessageController::handleMessageReceived(Swift::Message::ref message_)
{
boost::optional<std::string> bodyOpt = message_->getBody();
if (bodyOpt)
{
if (bodyOpt) {
//
// add the message to the db
//
......@@ -112,32 +111,30 @@ void MessageController::handleMessageReceived(Swift::Message::ref message_)
// get the timestamp from the message, if exists
boost::optional<boost::posix_time::ptime> timestampOpt = message_->getTimestamp();
if (timestampOpt)
{
if (timestampOpt) {
timestamp = QString::fromStdString(
boost::posix_time::to_iso_extended_string(*timestampOpt)
);
boost::posix_time::to_iso_extended_string(*timestampOpt)
);
}
messageModel->addMessage(&author, &author_resource, ownJid,
&recipient_resource, &timestamp, &message, &msgId, false);
&recipient_resource, &timestamp, &message, &msgId, false);
emit messageModelChanged();
// send a new notification | TODO: Resolve nickname from JID
Notifications::sendMessageNotification(
message_->getFrom().toBare().toString(),
*bodyOpt
message_->getFrom().toBare().toString(),
*bodyOpt
);
}
// XEP-0184: Message Delivery Receipts
// send a reply that the message has arrived
if (message_->getPayload<Swift::DeliveryReceiptRequest>())
{
if (message_->getPayload<Swift::DeliveryReceiptRequest>()) {
// create a new reply payload
boost::shared_ptr<Swift::DeliveryReceipt> receiptPayload =
boost::make_shared<Swift::DeliveryReceipt>();
boost::make_shared<Swift::DeliveryReceipt>();
receiptPayload->setReceivedID(message_->getID());
// create a new message
......@@ -155,11 +152,9 @@ void MessageController::handleMessageReceived(Swift::Message::ref message_)
// XEP-0184: Message Delivery Receipts
// get a reply of a delivered receipt request
Swift::DeliveryReceipt::ref receipt = message_->getPayload<Swift::DeliveryReceipt>();
if (receipt)
{
if (receipt) {
std::string receivedId = receipt->getReceivedID();
if (receivedId.length() > 0)
{
if (receivedId.length() > 0) {
messageModel->setMessageAsDelivered(QString::fromStdString(receivedId));
}
}
......@@ -168,14 +163,12 @@ void MessageController::handleMessageReceived(Swift::Message::ref message_)
// Update last exchanged, unread message count in roster controller
//
if (bodyOpt)
{
if (bodyOpt) {
const QString msgAuthor = QString::fromStdString(message_->getFrom().toBare().toString());
rosterController->updateLastExchangedOfJid(msgAuthor);
if (msgAuthor != this->recipient)
{
if (msgAuthor != this->recipient) {
rosterController->newUnreadMessageForJid(msgAuthor);
}
}
......@@ -197,7 +190,7 @@ void MessageController::sendMessage(const QString recipient_, const QString mess
const QString qmsgId = QString::fromStdString(msgId);
messageModel->addMessage(ownJid, &author_resource, &recipient_,
&recipient_resource, &timestamp, &message_, &qmsgId, true);
&recipient_resource, &timestamp, &message_, &qmsgId, true);
emit messageModelChanged();
......
......@@ -39,19 +39,19 @@ static void createTable()
QSqlQuery query;
if (!query.exec(
"CREATE TABLE IF NOT EXISTS 'Messages' ("
"'author' TEXT NOT NULL,"
"'author_resource' TEXT,"
"'recipient' TEXT NOT NULL,"
"'recipient_resource' TEXT,"
"'timestamp' TEXT NOT NULL,"
"'message' TEXT NOT NULL,"
"'id' TEXT NOT NULL,"
"'isSent' BOOL," // is sent to server
"'isDelivered' BOOL," // message has arrived at other client
"FOREIGN KEY('author') REFERENCES Roster ('jid'),"
"FOREIGN KEY('recipient') REFERENCES Roster ('jid')"
")"))
"CREATE TABLE IF NOT EXISTS 'Messages' ("
"'author' TEXT NOT NULL,"
"'author_resource' TEXT,"
"'recipient' TEXT NOT NULL,"
"'recipient_resource' TEXT,"
"'timestamp' TEXT NOT NULL,"
"'message' TEXT NOT NULL,"
"'id' TEXT NOT NULL,"
"'isSent' BOOL," // is sent to server
"'isDelivered' BOOL," // message has arrived at other client
"FOREIGN KEY('author') REFERENCES Roster ('jid'),"
"FOREIGN KEY('recipient') REFERENCES Roster ('jid')"
")"))
{
qFatal("Failed to query database: %s", qPrintable(query.lastError().text()));
}
......@@ -72,8 +72,8 @@ MessageModel::MessageModel(QObject *parent) : QSqlTableModel(parent)
void MessageModel::applyRecipientFilter(QString recipient_, QString author_)
{
const QString filterString = QString::fromLatin1("(recipient = '%1' AND "
"author = '%2') OR (recipient = '%2' AND author = '%1')").arg(
recipient_, author_);
"author = '%2') OR (recipient = '%2' AND author = '%1')").arg(
recipient_, author_);
setFilter(filterString);
select();
}
......@@ -112,9 +112,9 @@ void MessageModel::setMessageAsDelivered(const QString msgId)
}
void MessageModel::addMessage(const QString* author, const QString* author_resource,
const QString* recipient, const QString* recipient_resource,
const QString* timestamp, const QString* message, const QString* msgId,
bool sentByMe)
const QString* recipient, const QString* recipient_resource,
const QString* timestamp, const QString* message, const QString* msgId,
bool sentByMe)
{
//
// add the new message
......
......@@ -34,9 +34,9 @@ public:
void applyRecipientFilter(QString recipient_, QString author_);
void addMessage(const QString* author, const QString* author_resource,
const QString* recipient, const QString* recipient_resource,
const QString* timestamp, const QString* message,
const QString* msgId, bool sentByMe);
const QString* recipient, const QString* recipient_resource,
const QString* timestamp, const QString* message,
const QString* msgId, bool sentByMe);
void setMessageAsSent(const QString msgId);
void setMessageAsDelivered(const QString msgId);
......
......@@ -19,6 +19,7 @@
#include <string>
namespace Notifications {
void sendMessageNotification(std::string fromName, std::string message);
namespace Notifications
{
void sendMessageNotification(std::string fromName, std::string message);
}
......@@ -42,8 +42,7 @@ void PresenceController::setClient(Swift::Client* client_)
void PresenceController::handlePresenceReceived(Swift::Presence::ref presence)
{
// Automatically approve subscription requests
if (presence->getType() == Swift::Presence::Subscribe)
{
if (presence->getType() == Swift::Presence::Subscribe) {
Swift::Presence::ref response = Swift::Presence::create();
response->setTo(presence->getFrom());
response->setType(Swift::Presence::Subscribed);
......
......@@ -78,12 +78,9 @@ void RosterController::requestRosterFromClient()
void RosterController::handleRosterReceived(Swift::ErrorPayload::ref error_)
{
if (error_)
{
if (error_) {
std::cerr << "RosterController: Error receiving roster. Continuing anyway.\n";
}
else
{
} else {
// create a vector containing all roster items
std::vector<Swift::XMPPRosterItem> rosterItems = xmppRoster->getItems();
// create an iterator for it
......@@ -106,11 +103,9 @@ void RosterController::handleRosterReceived(Swift::ErrorPayload::ref error_)
// add all JIDs to the delete list that are in the original list
// but not in the new from the server
for (int i = 0; i < currentJids.length(); i++)
{
for (int i = 0; i < currentJids.length(); i++) {
QString jidAtI = currentJids.at(i);
if (!newJids.contains(jidAtI))
{
if (!newJids.contains(jidAtI)) {
jidsToDelete << jidAtI;
}
}
......@@ -122,17 +117,13 @@ void RosterController::handleRosterReceived(Swift::ErrorPayload::ref error_)
// Update the roster
//
for (it = rosterItems.begin(); it < rosterItems.end(); it++)
{
for (it = rosterItems.begin(); it < rosterItems.end(); it++) {
QString jid = QString::fromStdString((*it).getJID().toBare().toString());
QString name = QString::fromStdString((*it).getName());
if (currentJids.contains(jid))
{
if (currentJids.contains(jid)) {
rosterModel->updateContactName(jid, name);
}
else
{
} else {
rosterModel->insertContact(jid, name);
}
}
......@@ -155,8 +146,8 @@ void RosterController::handleInitialRosterPopulated()
void RosterController::handleJidAdded(const Swift::JID &jid_)
{
rosterModel->insertContact(
QString::fromStdString(jid_.toBare().toString()),
QString::fromStdString(xmppRoster->getNameForJID(jid_))
QString::fromStdString(jid_.toBare().toString()),
QString::fromStdString(xmppRoster->getNameForJID(jid_))
);
rosterModel->submitAll();
......@@ -167,18 +158,18 @@ void RosterController::handleJidAdded(const Swift::JID &jid_)
void RosterController::handleJidRemoved(const Swift::JID &jid_)
{
rosterModel->removeContactByJid(
QString::fromStdString(jid_.toBare().toString())
QString::fromStdString(jid_.toBare().toString())
);
emit rosterModelChanged();
}
void RosterController::handleJidUpdated(const Swift::JID &jid_, const std::string &name_,
const std::vector<std::string>&)
const std::vector<std::string>&)
{
rosterModel->updateContactName(
QString::fromStdString(jid_.toBare().toString()),
QString::fromStdString(name_)
QString::fromStdString(jid_.toBare().toString()),
QString::fromStdString(name_)
);
emit rosterModelChanged();
......@@ -212,7 +203,7 @@ void RosterController::addContact(const QString jid_, const QString name_)
// sent the request
iqRouter->sendIQ(
Swift::IQ::createRequest(Swift::IQ::Set, Swift::JID(), iqId, rosterPayload)
Swift::IQ::createRequest(Swift::IQ::Set, Swift::JID(), iqId, rosterPayload)
);
}
......@@ -227,13 +218,13 @@ void RosterController::removeContact(const QString jid_)
// create new roster payload, add roster item removal
boost::shared_ptr<Swift::RosterPayload> rosterPayload(new Swift::RosterPayload);
rosterPayload->addItem(Swift::RosterItemPayload(
Swift::JID(jid_.toStdString()), "",
Swift::RosterItemPayload::Remove
));
Swift::JID(jid_.toStdString()), "",
Swift::RosterItemPayload::Remove
));
// send the remove request
iqRouter->sendIQ(
Swift::IQ::createRequest(Swift::IQ::Set, Swift::JID(), iqId, rosterPayload)
Swift::IQ::createRequest(Swift::IQ::Set, Swift::JID(), iqId, rosterPayload)
);
}
......
......@@ -37,16 +37,16 @@ static void createTable()
QSqlQuery query;
if (!query.exec("CREATE TABLE IF NOT EXISTS 'Roster' ("
"'jid' TEXT NOT NULL,"
"'name' TEXT NOT NULL,"
"'lastExchanged' TEXT NOT NULL,"
"'unreadMessages' INTEGER,"
"'lastMessage' TEXT," // < UNUSED v
"'lastOnline' TEXT,"
"'activity' TEXT,"
"'status' TEXT,"
"'mood' TEXT" // < UNUSED ^
")"))
"'jid' TEXT NOT NULL,"
"'name' TEXT NOT NULL,"
"'lastExchanged' TEXT NOT NULL,"
"'unreadMessages' INTEGER,"
"'lastMessage' TEXT," // < UNUSED v
"'lastOnline' TEXT,"
"'activity' TEXT,"
"'status' TEXT,"
"'mood' TEXT" // < UNUSED ^
")"))
{
qFatal("Failed to query database: %s", qPrintable(query.lastError().text()));
}
......@@ -93,8 +93,7 @@ QVariant RosterModel::data(const QModelIndex &index, int role) const
void RosterModel::clearData()
{
// remove all rows / contacts from the model
for (int i = 0; i < rowCount(); ++i)
{
for (int i = 0; i < rowCount(); ++i) {
removeRow(i);
}
}
......@@ -113,7 +112,7 @@ void RosterModel::insertContact(QString jid_, QString name_)
// inster the record into the DB (or print error)
if (!insertRecord(rowCount(), newRecord)) {
qWarning() << "Failed to save Contact into DB:"
<< lastError().text();
<< lastError().text();
}
submitAll();
......@@ -151,8 +150,7 @@ QStringList RosterModel::getJidList()
void RosterModel::removeListOfJids(QStringList* jidList)
{
QSqlQuery query;
for (int i = 0; i < jidList->length(); i++)
{
for (int i = 0; i < jidList->length(); i++) {
query.exec(QString("DELETE FROM 'Roster' WHERE jid = '%1'").arg(jidList->at(i)));
}
submitAll();
......@@ -170,8 +168,7 @@ int RosterModel::getUnreadMessageCountOfJid(const QString* jid_)
QSqlQuery query;
query.prepare(QString("SELECT unreadMessages FROM Roster WHERE jid = '%1'").arg(*jid_));
if (!query.exec())
{
if (!query.exec()) {
qDebug("Failed to query database: %s", qPrintable(query.lastError().text()));
return 0;
}
......@@ -184,7 +181,7 @@ void RosterModel::setUnreadMessageCountOfJid(const QString* jid_, const int coun
{
QSqlQuery query;
query.prepare(QString("UPDATE Roster SET unreadMessages = %1 WHERE jid = '%2'")
.arg(QString::number(count_), *jid_));
.arg(QString::number(count_), *jid_));
if (!query.exec()) {
qDebug("Failed to query database: %s", qPrintable(query.lastError().text()));
......
......@@ -62,18 +62,18 @@ void ServiceDiscoveryManager::handleConnected()
//
Swift::GetDiscoInfoRequest::ref discoInfoRequest = Swift::GetDiscoInfoRequest::create(
Swift::JID(client->getJID().getDomain()),
client->getIQRouter()
);
Swift::JID(client->getJID().getDomain()),
client->getIQRouter()
);
discoInfoRequest->onResponse.connect(boost::bind(
&ServiceDiscoveryManager::handleServerDiscoInfoReceived, this, _1, _2
));
&ServiceDiscoveryManager::handleServerDiscoInfoReceived, this, _1, _2
));
discoInfoRequest->send();
}
void ServiceDiscoveryManager::handleServerDiscoInfoReceived(
boost::shared_ptr<Swift::DiscoInfo> info, Swift::ErrorPayload::ref error)
boost::shared_ptr<Swift::DiscoInfo> info, Swift::ErrorPayload::ref error)
{
// TODO: check if the server supports our XEPs
}
......@@ -37,7 +37,7 @@ public:
void setClient(Swift::Client*);
void handleConnected();
void handleServerDiscoInfoReceived(boost::shared_ptr<Swift::DiscoInfo> discoInfo,
Swift::ErrorPayload::ref error);
Swift::ErrorPayload::ref error);
private:
Swift::Client* client;
......
......@@ -42,13 +42,10 @@ VCard::~VCard()
void VCard::fromSwiftVCard(Swift::VCard::ref vCard_)
{
// only use the new VCard, if it's valid
if (vCard_)
{
if (vCard_) {
vCard = vCard_;
emit VCardChanged();
}
else
{
} else {
std::cout << "VCard::fromSwiftVCard - Not Valid / Empty" << '\n';
}
}
......
......@@ -60,11 +60,10 @@ void VCardController::setCurrentJid(QString jid_)
currentJid = jid_;
// get the new VCard, if client is connected
if (client->isAvailable())
{
if (client->isAvailable()) {
currentVCard->fromSwiftVCard(manager->getVCardAndRequestWhenNeeded(
Swift::JID(currentJid.toStdString())
));
Swift::JID(currentJid.toStdString())
));
emit currentVCardChanged();
}
}
......@@ -88,7 +87,7 @@ void VCardController::setOwnVCard(VCard* vCard_)
{
// publish new vCard
Swift::SetVCardRequest::ref vCardChangeRequest = manager->createSetVCardRequest(
vCard_->getSwiftVCard());
vCard_->getSwiftVCard());
vCardChangeRequest->send();
}
......@@ -100,11 +99,10 @@ void VCardController::requestOwnVCard()
void VCardController::requestCurrentVCard()
{
// if a new jid was already set, but the client wasn't connected
if (currentJid != "")
{
if (currentJid != "") {
currentVCard->fromSwiftVCard(manager->getVCardAndRequestWhenNeeded(
Swift::JID(currentJid.toStdString())
));
Swift::JID(currentJid.toStdString())
));
emit currentVCardChanged();
}
}
......@@ -112,8 +110,7 @@ void VCardController::requestCurrentVCard()
void VCardController::handleVCardChanged(const Swift::JID &jid_, Swift::VCard::ref vCard_)
{
std::cout << "VCardController::handleVCardChanged: Received new VCard from: " << jid_.toString() << '\n';
if (jid_.toString() == currentJid.toStdString())
{
if (jid_.toString() == currentJid.toStdString()) {
// update the current VCard
currentVCard->fromSwiftVCard(vCard_);
emit currentVCardChanged();
......
......@@ -40,8 +40,7 @@
#include "Kaidan.h"
#include "VCard.h"
enum CommandLineParseResult
{
enum CommandLineParseResult {
CommandLineOk,
CommandLineError,
CommandLineVersionRequested,
......@@ -55,8 +54,7 @@ CommandLineParseResult parseCommandLine(QCommandLineParser &parser, QString *err
QCommandLineOption versionOption = parser.addVersionOption();
// parse arguments
if (!parser.parse(QGuiApplication::arguments()))
{
if (!parser.parse(QGuiApplication::arguments())) {
*errorMessage = parser.errorText();
return CommandLineError;
}
......@@ -118,14 +116,14 @@ int main(int argc, char *argv[])
// Qt-Translator
QTranslator qtTranslator;
qtTranslator.load("qt_" + QLocale::system().name(),
QLibraryInfo::location(QLibraryInfo::TranslationsPath));
QLibraryInfo::location(QLibraryInfo::TranslationsPath));
app.installTranslator(&qtTranslator);
// Kaidan-Translator
QTranslator kaidanTranslator;
// load the systems locale or none
kaidanTranslator.load(QString(APPLICATION_NAME) + "_" + QLocale::system().name(),
":/i18n"); // load the qm files via. rcc (bundled in binary)
":/i18n"); // load the qm files via. rcc (bundled in binary)
app.installTranslator(&kaidanTranslator);
......@@ -149,23 +147,22 @@ int main(int argc, char *argv[])
// create parser and add a description
QCommandLineParser parser;
parser.setApplicationDescription(QString(APPLICATION_DISPLAY_NAME) +
" - " + QString(APPLICATION_DESCRIPTION));
" - " + QString(APPLICATION_DESCRIPTION));
// parse the arguments
QString commandLineErrorMessage;
switch (parseCommandLine(parser, &commandLineErrorMessage))
{
case CommandLineError:
qWarning() << commandLineErrorMessage;
return 1;
case CommandLineVersionRequested:
parser.showVersion();
return 0;
case CommandLineHelpRequested:
parser.showHelp();
return 0;
case CommandLineOk:
break;
switch (parseCommandLine(parser, &commandLineErrorMessage)) {
case CommandLineError:
qWarning() << commandLineErrorMessage;
return 1;
case CommandLineVersionRequested: