Skip to content

Commit

Permalink
Cleaning.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ummon committed Sep 13, 2014
1 parent 433a24f commit 7a191d1
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 59 deletions.
3 changes: 3 additions & 0 deletions application/Common/RemoteCoreController/ICoreConnection.h
Expand Up @@ -99,6 +99,9 @@ namespace RCC

virtual Common::Hash getRemoteID() const = 0;

/**
* This method can only be called when connecting or when a connection is established. Otherwise the result is undefined.
*/
virtual bool isLocal() const = 0;

/**
Expand Down
103 changes: 53 additions & 50 deletions application/Common/RemoteCoreController/priv/CoreConnection.cpp
Expand Up @@ -74,29 +74,32 @@ void CoreConnection::connectToCore(const QString& address, quint16 port, Common:
{
if (!this->connectToCorePrepare(address))
return;
this->temp()->connectToCore(address, port, password);
this->temp().connectToCore(address, port, password);
}

void CoreConnection::connectToCore(const QString& address, quint16 port, const QString& password)
{
if (!this->connectToCorePrepare(address))
return;
this->temp()->connectToCore(address, port, password);
this->temp().connectToCore(address, port, password);
}

Common::Hash CoreConnection::getRemoteID() const
{
return this->current()->getRemoteID();
return this->current().getRemoteID();
}

bool CoreConnection::isLocal() const
{
return this->current()->isLocal();
if (this->connectingInProgress)
return this->temp().isLocal();
else
return this->current().isLocal();
}

bool CoreConnection::isConnected() const
{
return this->current()->isConnected();
return this->current().isConnected();
}

bool CoreConnection::isConnecting() const
Expand All @@ -106,101 +109,101 @@ bool CoreConnection::isConnecting() const

void CoreConnection::disconnectFromCore()
{
this->current()->disconnectFromCore();
this->temp()->disconnectFromCore();
this->current().disconnectFromCore();
this->temp().disconnectFromCore();
}

QSharedPointer<ISendChatMessageResult> CoreConnection::sendChatMessage(const QString& message)
{
return this->current()->sendChatMessage(this->SOCKET_TIMEOUT, message);
return this->current().sendChatMessage(this->SOCKET_TIMEOUT, message);
}

QSharedPointer<ISendChatMessageResult> CoreConnection::sendChatMessage(const QString& message, const QString& roomName)
{
return this->current()->sendChatMessage(this->SOCKET_TIMEOUT, message, roomName);
return this->current().sendChatMessage(this->SOCKET_TIMEOUT, message, roomName);
}

QSharedPointer<ISendChatMessageResult> CoreConnection::sendChatMessage(const QString& message, const QString& roomName, const QList<Common::Hash>& peerIDsAnswered)
{
return this->current()->sendChatMessage(this->SOCKET_TIMEOUT, message, roomName, peerIDsAnswered);
return this->current().sendChatMessage(this->SOCKET_TIMEOUT, message, roomName, peerIDsAnswered);
}

void CoreConnection::joinRoom(const QString& room)
{
this->current()->joinRoom(room);
this->current().joinRoom(room);
}

void CoreConnection::leaveRoom(const QString& room)
{
this->current()->leaveRoom(room);
this->current().leaveRoom(room);
}

void CoreConnection::setCoreSettings(const Protos::GUI::CoreSettings settings)
{
this->current()->setCoreSettings(settings);
this->current().setCoreSettings(settings);
}

void CoreConnection::setCoreLanguage(const QLocale& locale)
{
this->current()->setCoreLanguage(locale);
this->temp()->setCoreLanguage(locale);
this->current().setCoreLanguage(locale);
this->temp().setCoreLanguage(locale);
}

bool CoreConnection::setCorePassword(const QString& newPassword, const QString& oldPassword)
{
return this->current()->setCorePassword(newPassword, oldPassword);
return this->current().setCorePassword(newPassword, oldPassword);
}

void CoreConnection::resetCorePassword()
{
this->current()->resetCorePassword();
this->current().resetCorePassword();
}

QSharedPointer<IBrowseResult> CoreConnection::browse(const Common::Hash& peerID)
{
return this->current()->browse(peerID, this->SOCKET_TIMEOUT);
return this->current().browse(peerID, this->SOCKET_TIMEOUT);
}

QSharedPointer<IBrowseResult> CoreConnection::browse(const Common::Hash& peerID, const Protos::Common::Entry& entry)
{

return this->current()->browse(peerID, entry, this->SOCKET_TIMEOUT);
return this->current().browse(peerID, entry, this->SOCKET_TIMEOUT);
}

QSharedPointer<IBrowseResult> CoreConnection::browse(const Common::Hash& peerID, const Protos::Common::Entries& entries, bool withRoots)
{

return this->current()->browse(peerID, entries, withRoots, this->SOCKET_TIMEOUT);
return this->current().browse(peerID, entries, withRoots, this->SOCKET_TIMEOUT);
}

QSharedPointer<ISearchResult> CoreConnection::search(const Protos::Common::FindPattern& findPattern, bool local)
{
return this->current()->search(findPattern, local, this->SOCKET_TIMEOUT);
return this->current().search(findPattern, local, this->SOCKET_TIMEOUT);
}

void CoreConnection::download(const Common::Hash& peerID, const Protos::Common::Entry& entry)
{
this->current()->download(peerID, entry);
this->current().download(peerID, entry);
}

void CoreConnection::download(const Common::Hash& peerID, const Protos::Common::Entry& entry, const Common::Hash& sharedFolderID, const QString& path)
{
this->current()->download(peerID, entry, sharedFolderID, path);
this->current().download(peerID, entry, sharedFolderID, path);
}

void CoreConnection::download(const Common::Hash& peerID, const Protos::Common::Entry& entry, const QString& absolutePath)
{
this->current()->download(peerID, entry, Common::Hash(), absolutePath);
this->current().download(peerID, entry, Common::Hash(), absolutePath);
}

void CoreConnection::cancelDownloads(const QList<quint64>& downloadIDs, bool complete)
{
this->current()->cancelDownloads(downloadIDs, complete);
this->current().cancelDownloads(downloadIDs, complete);
}

void CoreConnection::pauseDownloads(const QList<quint64>& downloadIDs, bool pause)
{
this->current()->pauseDownloads(downloadIDs, pause);
this->current().pauseDownloads(downloadIDs, pause);
}

void CoreConnection::moveDownloads(quint64 downloadIDRef, const QList<quint64>& downloadIDs, Protos::GUI::MoveDownloads::Position position)
Expand All @@ -210,51 +213,51 @@ void CoreConnection::moveDownloads(quint64 downloadIDRef, const QList<quint64>&

void CoreConnection::moveDownloads(const QList<quint64>& downloadIDRefs, const QList<quint64>& downloadIDs, Protos::GUI::MoveDownloads::Position position)
{
this->current()->moveDownloads(downloadIDRefs, downloadIDs, position);
this->current().moveDownloads(downloadIDRefs, downloadIDs, position);
}

void CoreConnection::refresh()
{
this->current()->refresh();
this->current().refresh();
}

void CoreConnection::refreshNetworkInterfaces()
{
this->current()->refreshNetworkInterfaces();
this->current().refreshNetworkInterfaces();
}

ICoreConnection::ConnectionInfo CoreConnection::getConnectionInfo() const
{
return this->current()->getConnectionInfo();
return this->current().getConnectionInfo();
}

ICoreConnection::ConnectionInfo CoreConnection::getConnectionInfoConnecting() const
{
return this->temp()->getConnectionInfo();
return this->temp().getConnectionInfo();
}

void CoreConnection::tempConnectingError(RCC::ICoreConnection::ConnectionErrorCode code)
{
this->connectingInProgress = false;
emit connectingError(code);
this->temp()->disconnect(this);
this->temp().disconnect(this);
}

void CoreConnection::tempConnected()
{
this->current()->disconnectFromCore(); // May throw a 'disconnected()' signal.
this->current().disconnectFromCore(); // May throw a 'disconnected()' signal.

this->current()->disconnect(this);
this->temp()->disconnect(this);
this->current().disconnect(this);
this->temp().disconnect(this);

this->connectingInProgress = false;

this->swap();

connect(this->current(), SIGNAL(disconnected(bool)), this, SIGNAL(disconnected(bool)));
connect(this->current(), SIGNAL(newState(const Protos::GUI::State&)), this, SIGNAL(newState(const Protos::GUI::State&)));
connect(this->current(), SIGNAL(newChatMessages(const Protos::Common::ChatMessages&)), this, SIGNAL(newChatMessages(const Protos::Common::ChatMessages&)));
connect(this->current(), SIGNAL(newLogMessages(QList<QSharedPointer<LM::IEntry>>)), this, SIGNAL(newLogMessages(QList<QSharedPointer<LM::IEntry>>)));
connect(&this->current(), SIGNAL(disconnected(bool)), this, SIGNAL(disconnected(bool)));
connect(&this->current(), SIGNAL(newState(const Protos::GUI::State&)), this, SIGNAL(newState(const Protos::GUI::State&)));
connect(&this->current(), SIGNAL(newChatMessages(const Protos::Common::ChatMessages&)), this, SIGNAL(newChatMessages(const Protos::Common::ChatMessages&)));
connect(&this->current(), SIGNAL(newLogMessages(QList<QSharedPointer<LM::IEntry>>)), this, SIGNAL(newLogMessages(QList<QSharedPointer<LM::IEntry>>)));
emit connected();
}

Expand Down Expand Up @@ -285,31 +288,31 @@ bool CoreConnection::connectToCorePrepare(const QString& address)
return false;
}

connect(this->temp(), SIGNAL(connectingError(RCC::ICoreConnection::ConnectionErrorCode)), this, SLOT(tempConnectingError(RCC::ICoreConnection::ConnectionErrorCode)));
connect(this->temp(), SIGNAL(connected()), this, SLOT(tempConnected()));
connect(this->temp(), SIGNAL(disconnected(bool)), this, SLOT(tempDisconnected()));
connect(&this->temp(), SIGNAL(connectingError(RCC::ICoreConnection::ConnectionErrorCode)), this, SLOT(tempConnectingError(RCC::ICoreConnection::ConnectionErrorCode)));
connect(&this->temp(), SIGNAL(connected()), this, SLOT(tempConnected()));
connect(&this->temp(), SIGNAL(disconnected(bool)), this, SLOT(tempDisconnected()));

return true;
}

InternalCoreConnection* CoreConnection::current()
InternalCoreConnection& CoreConnection::current()
{
return this->currentConnected == FIRST_CONNECTION ? &this->connection1 : &this->connection2;
return this->currentConnected == FIRST_CONNECTION ? this->connection1 : this->connection2;
}

const InternalCoreConnection* CoreConnection::current() const
const InternalCoreConnection& CoreConnection::current() const
{
return this->currentConnected == FIRST_CONNECTION ? &this->connection1 : &this->connection2;
return this->currentConnected == FIRST_CONNECTION ? this->connection1 : this->connection2;
}

InternalCoreConnection* CoreConnection::temp()
InternalCoreConnection& CoreConnection::temp()
{
return this->currentConnected == FIRST_CONNECTION ? &this->connection2 : &this->connection1;
return this->currentConnected == FIRST_CONNECTION ? this->connection2 : this->connection1;
}

const InternalCoreConnection* CoreConnection::temp() const
const InternalCoreConnection& CoreConnection::temp() const
{
return this->currentConnected == FIRST_CONNECTION ? &this->connection2 : &this->connection1;
return this->currentConnected == FIRST_CONNECTION ? this->connection2 : this->connection1;
}

void CoreConnection::swap()
Expand Down
8 changes: 4 additions & 4 deletions application/Common/RemoteCoreController/priv/CoreConnection.h
Expand Up @@ -111,11 +111,11 @@ namespace RCC
private:
bool connectToCorePrepare(const QString& address);

InternalCoreConnection* current();
const InternalCoreConnection* current() const;
InternalCoreConnection& current();
const InternalCoreConnection& current() const;

InternalCoreConnection* temp();
const InternalCoreConnection* temp() const;
InternalCoreConnection& temp();
const InternalCoreConnection& temp() const;

void swap();

Expand Down
Expand Up @@ -98,6 +98,14 @@ void InternalCoreConnection::connectToCore(const QString& address, quint16 port,
this->connectToCore(address, port, Common::Hash());
}

bool InternalCoreConnection::isLocal() const
{
if (this->socket->peerAddress().isNull())
return Common::Global::isLocal(QHostAddress(this->connectionInfo.address));
else
return MessageSocket::isLocal();
}

bool InternalCoreConnection::isConnected() const
{
return MessageSocket::isConnected() && this->authenticated;
Expand Down
Expand Up @@ -66,12 +66,13 @@ namespace RCC

public:
InternalCoreConnection(CoreController& coreController);
~InternalCoreConnection();
~InternalCoreConnection() override;

void connectToCore(const QString& address, quint16 port, Common::Hash password);
void connectToCore(const QString& address, quint16 port, const QString& password);

bool isConnected() const;
bool isLocal() const override;
bool isConnected() const override;

void disconnectFromCore();

Expand Down Expand Up @@ -123,8 +124,8 @@ namespace RCC

void sendCurrentLanguage();

void onNewMessage(const Common::Message& message);
void onDisconnected();
void onNewMessage(const Common::Message& message) override;
void onDisconnected() override;

friend class BrowseResult;
friend class SearchResult;
Expand Down
2 changes: 1 addition & 1 deletion application/GUI/MainWindow.cpp
Expand Up @@ -172,7 +172,7 @@ void MainWindow::coreConnectionError(RCC::ICoreConnection::ConnectionErrorCode e

QMessageBox msgBox(this);
msgBox.setWindowTitle(tr("Unable to connect to the core"));
msgBox.setText(QString("<p>%1</p><p>%2 <em>%3:%4</em></p>").arg(error).arg(tr("Remote core address:")).arg(this->coreConnection->getConnectionInfoConnecting().address).arg(this->coreConnection->getConnectionInfoConnecting().port));
msgBox.setText(QString("<p>%1</p><p>%2 <em>%3:%4</em></p>").arg(error).arg(tr("Core address:")).arg(this->coreConnection->getConnectionInfoConnecting().address).arg(this->coreConnection->getConnectionInfoConnecting().port));
msgBox.setIcon(QMessageBox::Information);
msgBox.setStandardButtons(QMessageBox::Ok);
msgBox.exec();
Expand Down

0 comments on commit 7a191d1

Please sign in to comment.