main.cpp Example File
sfwecho/sfwecho_client/main.cpp
#include <QApplication>
#include <QMetaObject>
#include <QTimer>
#include <QVBoxLayout>
#include <QWidget>
#include <QLineEdit>
#include <QPushButton>
#include <QProcess>
#include <QDateTime>
#include <QLabel>
#include <qservice.h>
#include <qservicemanager.h>
#if defined (Q_OS_SYMBIAN) || defined(Q_OS_WINCE) || defined(Q_WS_MAEMO_5) || defined(Q_WS_MAEMO_6)
#include "ui_sfwecho_client_mobile.h"
#else
#include "ui_sfwecho_client.h"
#endif
QTM_USE_NAMESPACE
class EchoClient : public QMainWindow, public Ui_EchoClient
{
Q_OBJECT
public:
EchoClient(QWidget *parent = 0, Qt::WindowFlags flags = 0)
: QMainWindow(parent, flags)
{
setupUi(this);
globalEcho = 0;
privateEcho = 0;
echo = 0;
on_globalChat_toggled(true);
}
~EchoClient()
{
}
public slots:
void on_sendButton_clicked()
{
QString message = userName->text() + ": " + messageEdit->text();
messageEdit->setText("");
QMetaObject::invokeMethod(echo, "sendMessage", Q_ARG(QString, message));
}
void on_globalChat_toggled(bool checked)
{
privateChat->setChecked(!checked);
if (checked) {
if (!connectToChat())
echoBox->append("**Unable to connect to global Echo Chat server**");
else
echoBox->append("**Connected to global Echo Chat server**");
}
}
void on_privateChat_toggled(bool checked)
{
globalChat->setChecked(!checked);
if (checked) {
if (!connectToChat())
echoBox->append("**Unable to connect to private Echo Chat server**");
else
echoBox->append("**Connected to private Echo Chat server**");
}
}
void receivedMessage(const QString &msg, const QDateTime &ts)
{
QString newMsg = "[" + ts.toString("hh:mm") + "]" + " " + msg;
echoBox->append(newMsg);
}
void errorIPC(QService::UnrecoverableIPCError error)
{
QDateTime ts = QDateTime::currentDateTime();
QString newMsg = "[" + ts.toString("hh:mm") + "]" + " " + "IPC Error! ";
if (error == QService::ErrorServiceNoLongerAvailable)
newMsg += "Service no longer available";
echoBox->append(newMsg);
}
private:
QObject *globalEcho;
QObject *privateEcho;
QObject *echo;
bool connectToChat()
{
int version = 0;
if (globalChat->isChecked())
version = 1;
if (privateEcho && version == 0) {
echo = privateEcho;
return true;
}
if (globalEcho && version == 1) {
echo = globalEcho;
return true;
}
QServiceManager manager;
QList<QServiceInterfaceDescriptor> list = manager.findInterfaces("EchoService");
if(list.count() < version+1){
echoBox->append("**Unable to find a registered service**");
return false;
}
QServiceInterfaceDescriptor desc = list[version];
if (!desc.isValid()) {
qWarning() << "EchoService interface not found";
return false;
}
QObject *service = manager.loadInterface(desc);
if (!service) {
qWarning() << "EchoService unable to connect";
return false;
}
if (version == 0) {
privateEcho = service;
echo = privateEcho;
} else {
globalEcho = service;
echo = globalEcho;
}
echo->setParent(this);
QObject::connect(echo, SIGNAL(broadcastMessage(QString,QDateTime)),
this, SLOT(receivedMessage(QString,QDateTime)));
QObject::connect(echo, SIGNAL(errorUnrecoverableIPCFault(QService::UnrecoverableIPCError)),
this, SLOT(errorIPC(QService::UnrecoverableIPCError)));
return true;
}
};
int main(int argc, char **argv)
{
QApplication app(argc, argv);
EchoClient dialog;
#ifdef Q_OS_SYMBIAN
dialog.showMaximized();
#else
dialog.show();
#endif
return app.exec();
}
#include "main.moc"
[+] Documentation Feedback