From 68b0f5235909a25a7cc4f679592fc7765e845cbf Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Tue, 28 Mar 2006 19:16:35 +0000 Subject: * configure.in qt/Makefile.am: add qt/examples * qt/examples: Add QtDBus example programs: - hello: Hello, World - ping: Simple method-calling program - pong: Simple object-exporting program (not using adaptors) - complexping: Interactive method-calling program (also gets and sets properties). - complexpong: Sample program exporting methods, signals and properties, using adaptors. - dbus: Simple implementation of a generic method-calling program, similar to 'dbus-send', but with semantics similar to 'dcop'. - chat: Simplistic chat program, implemented using signals and the system bus. Looks like IRC. --- qt/Makefile.am | 2 + qt/examples/.cvsignore | 11 + qt/examples/Makefile.am | 44 ++++ qt/examples/chat.cpp | 136 ++++++++++++ qt/examples/chat.h | 62 ++++++ qt/examples/chatadaptor.cpp | 36 ++++ qt/examples/chatadaptor.h | 40 ++++ qt/examples/chatmainwindow.ui | 188 ++++++++++++++++ qt/examples/chatsetnickname.ui | 149 +++++++++++++ qt/examples/com.trolltech.ChatInterface.xml | 15 ++ qt/examples/complexping.cpp | 91 ++++++++ qt/examples/complexping.h | 37 ++++ qt/examples/complexpong.cpp | 85 ++++++++ qt/examples/complexpong.h | 46 ++++ qt/examples/dbus.cpp | 318 ++++++++++++++++++++++++++++ qt/examples/hello.cpp | 33 +++ qt/examples/ping-common.h | 22 ++ qt/examples/ping.cpp | 48 +++++ qt/examples/pong.cpp | 53 +++++ qt/examples/pong.h | 34 +++ 20 files changed, 1450 insertions(+) create mode 100644 qt/examples/.cvsignore create mode 100644 qt/examples/Makefile.am create mode 100644 qt/examples/chat.cpp create mode 100644 qt/examples/chat.h create mode 100644 qt/examples/chatadaptor.cpp create mode 100644 qt/examples/chatadaptor.h create mode 100644 qt/examples/chatmainwindow.ui create mode 100644 qt/examples/chatsetnickname.ui create mode 100644 qt/examples/com.trolltech.ChatInterface.xml create mode 100644 qt/examples/complexping.cpp create mode 100644 qt/examples/complexping.h create mode 100644 qt/examples/complexpong.cpp create mode 100644 qt/examples/complexpong.h create mode 100644 qt/examples/dbus.cpp create mode 100644 qt/examples/hello.cpp create mode 100644 qt/examples/ping-common.h create mode 100644 qt/examples/ping.cpp create mode 100644 qt/examples/pong.cpp create mode 100644 qt/examples/pong.h (limited to 'qt') diff --git a/qt/Makefile.am b/qt/Makefile.am index 74502672..ce35b36f 100644 --- a/qt/Makefile.am +++ b/qt/Makefile.am @@ -1,3 +1,5 @@ +SUBDIRS = . examples + if HAVE_QT INCLUDES=-I$(top_srcdir) $(DBUS_CLIENT_CFLAGS) $(DBUS_QT_CFLAGS) -DDBUS_COMPILATION diff --git a/qt/examples/.cvsignore b/qt/examples/.cvsignore new file mode 100644 index 00000000..f6454f28 --- /dev/null +++ b/qt/examples/.cvsignore @@ -0,0 +1,11 @@ +.deps +.libs +Makefile +Makefile.in +*.lo +*.la +*.bb +*.bbg +*.da +*.gcov +*.moc diff --git a/qt/examples/Makefile.am b/qt/examples/Makefile.am new file mode 100644 index 00000000..9528e39b --- /dev/null +++ b/qt/examples/Makefile.am @@ -0,0 +1,44 @@ +if HAVE_QT +INCLUDES=-I$(top_srcdir) $(DBUS_CLIENT_CFLAGS) $(DBUS_QT_CFLAGS) -DDBUS_COMPILATION +LDADD = ../libdbus-qt4-1.la + +if HAVE_QT_GUI +chat_LDADD = $(LDADD) $(DBUS_QT_GUI_LIBS) +dist_chat_SOURCES = chat.cpp chat.h chatadaptor.cpp +nodist_chat_SOURCES = chatinterface.cpp +chat.o: chatmainwindow.h chatsetnickname.h chatinterface.h chatadaptor.h chat.moc chatadaptor.moc +chatmainwindow.h: chatmainwindow.ui +chatsetnickname.h: chatsetnickname.ui +chatinterface.cpp chatinterface.h: com.trolltech.ChatInterface.xml + ../dbusidl2cpp -m -p chatinterface $? + $(QT_MOC) -o chatinterface.moc chatinterface.h + +CHAT=chat +endif + + +noinst_PROGRAMS = hello dbus ping pong complexping complexpong $(CHAT) +hello_SOURCES = hello.cpp +dbus_SOURCES = dbus.cpp + +ping_SOURCES = ping.cpp +pong_SOURCES = pong.cpp pong.h +pong.o: pong.moc + +complexping_SOURCES = complexping.cpp complexping.h +complexpong_SOURCES = complexpong.cpp complexpong.h +complexpong.o: complexpong.moc +complexping.o: complexping.moc + +EXTRA_DIST = ping-common.h chatmainwindow.ui chatsetnickname.ui com.trolltech.ChatInterface.xml chatadaptor.h + +CLEANFILES = chat.moc chatadaptor.moc complexping.moc complexpong.moc pong.moc \ + chatinterface.cpp chatinterface.h chatinterface.moc \ + chatmainwindow.h chatsetnickname.h + +%.moc: %.h + $(QT_MOC) $< > $@ +%.h: %.ui + $(QT_UIC) -o $@ $? +endif + diff --git a/qt/examples/chat.cpp b/qt/examples/chat.cpp new file mode 100644 index 00000000..86ff873b --- /dev/null +++ b/qt/examples/chat.cpp @@ -0,0 +1,136 @@ +/* -*- C++ -*- + * + * Copyright (C) 2006 Trolltech AS. All rights reserved. + * Author: Thiago Macieira + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +#include "chat.h" +#include +#include + +#include "chatadaptor.h" +#include "chatinterface.h" + +ChatMainWindow::ChatMainWindow() + : m_nickname(QLatin1String("nickname")) +{ + setupUi(this); + sendButton->setEnabled(false); + + connect(messageLineEdit, SIGNAL(textChanged(QString)), + this, SLOT(textChangedSlot(QString))); + connect(sendButton, SIGNAL(clicked(bool)), this, SLOT(sendClickedSlot())); + connect(actionChangeNickname, SIGNAL(triggered(bool)), this, SLOT(changeNickname())); + connect(actionAboutQt, SIGNAL(triggered(bool)), this, SLOT(aboutQt())); + connect(qApp, SIGNAL(lastWindowClosed()), this, SLOT(exiting())); + + // add our D-Bus interface and connect to D-Bus + new ChatInterfaceAdaptor(this); + QDBus::systemBus().registerObject("/", this); + + com::trolltech::ChatInterface *iface; + iface = QDBus::systemBus().findInterface(QString(), QString()); + connect(iface, SIGNAL(message(QString,QString)), this, SLOT(messageSlot(QString,QString))); + connect(iface, SIGNAL(action(QString,QString)), this, SLOT(actionSlot(QString,QString))); + + NicknameDialog dialog; + dialog.cancelButton->setVisible(false); + dialog.exec(); + m_nickname = dialog.nickname->text().trimmed(); + emit action(m_nickname, QLatin1String("joins the chat")); +} + +ChatMainWindow::~ChatMainWindow() +{ +} + +void ChatMainWindow::rebuildHistory() +{ + QString history = m_messages.join( QLatin1String("\n" ) ); + chatHistory->setPlainText(history); +} + +void ChatMainWindow::messageSlot(const QString &nickname, const QString &text) +{ + QString msg( QLatin1String("<%1> %2") ); + msg = msg.arg(nickname, text); + m_messages.append(msg); + + if (m_messages.count() > 100) + m_messages.removeFirst(); + rebuildHistory(); +} + +void ChatMainWindow::actionSlot(const QString &nickname, const QString &text) +{ + QString msg( QLatin1String("* %1 %2") ); + msg = msg.arg(nickname, text); + m_messages.append(msg); + + if (m_messages.count() > 100) + m_messages.removeFirst(); + rebuildHistory(); +} + +void ChatMainWindow::textChangedSlot(const QString &newText) +{ + sendButton->setEnabled(!newText.isEmpty()); +} + +void ChatMainWindow::sendClickedSlot() +{ + emit message(m_nickname, messageLineEdit->text()); + messageLineEdit->setText(QString()); +} + +void ChatMainWindow::changeNickname() +{ + NicknameDialog dialog(this); + if (dialog.exec() == QDialog::Accepted) { + QString old = m_nickname; + m_nickname = dialog.nickname->text().trimmed(); + emit action(old, QString("is now known as %1").arg(m_nickname)); + } +} + +void ChatMainWindow::aboutQt() +{ + QMessageBox::aboutQt(this); +} + +void ChatMainWindow::exiting() +{ + emit action(m_nickname, QLatin1String("leaves the chat")); +} + +NicknameDialog::NicknameDialog(QWidget *parent) + : QDialog(parent) +{ + setupUi(this); +} + +int main(int argc, char **argv) +{ + QApplication app(argc, argv); + + ChatMainWindow chat; + chat.show(); + return app.exec(); +} + +#include "chat.moc" diff --git a/qt/examples/chat.h b/qt/examples/chat.h new file mode 100644 index 00000000..12c33d4c --- /dev/null +++ b/qt/examples/chat.h @@ -0,0 +1,62 @@ +/* -*- C++ -*- + * + * Copyright (C) 2006 Trolltech AS. All rights reserved. + * Author: Thiago Macieira + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +#ifndef CHAT_H +#define CHAT_H + +#include +#include +#include "chatmainwindow.h" +#include "chatsetnickname.h" + +class ChatMainWindow: public QMainWindow, Ui::ChatMainWindow +{ + Q_OBJECT + QString m_nickname; + QStringList m_messages; +public: + ChatMainWindow(); + ~ChatMainWindow(); + + void rebuildHistory(); + +signals: + void message(const QString &nickname, const QString &text); + void action(const QString &nickname, const QString &text); + +private slots: + void messageSlot(const QString &nickname, const QString &text); + void actionSlot(const QString &nickname, const QString &text); + void textChangedSlot(const QString &newText); + void sendClickedSlot(); + void changeNickname(); + void aboutQt(); + void exiting(); +}; + +class NicknameDialog: public QDialog, public Ui::NicknameDialog +{ + Q_OBJECT +public: + NicknameDialog(QWidget *parent = 0); +}; + +#endif diff --git a/qt/examples/chatadaptor.cpp b/qt/examples/chatadaptor.cpp new file mode 100644 index 00000000..525b9aad --- /dev/null +++ b/qt/examples/chatadaptor.cpp @@ -0,0 +1,36 @@ +/* + * This file was generated by dbusidl2cpp version 0.3 + * when processing input file /home/tjmaciei/src/kde4/playground/libs/qt-dbus/examples/com.trolltech.ChatInterface.xml + * + * dbusidl2cpp is Copyright (C) 2006 Trolltech AS. All rights reserved. + * + * This is an auto-generated file. + */ + +#include "chatadaptor.h" +#include +#include +#include +#include +#include +#include +#include + +/* + * Implementation of adaptor class ChatInterfaceAdaptor + */ + +ChatInterfaceAdaptor::ChatInterfaceAdaptor(QObject *parent) + : QDBusAbstractAdaptor(parent) +{ + // constructor + setAutoRelaySignals(true); +} + +ChatInterfaceAdaptor::~ChatInterfaceAdaptor() +{ + // destructor +} + + +#include "chatadaptor.moc" diff --git a/qt/examples/chatadaptor.h b/qt/examples/chatadaptor.h new file mode 100644 index 00000000..cbec0120 --- /dev/null +++ b/qt/examples/chatadaptor.h @@ -0,0 +1,40 @@ +/* + * This file was generated by dbusidl2cpp version 0.3 + * when processing input file /home/tjmaciei/src/kde4/playground/libs/qt-dbus/examples/com.trolltech.ChatInterface.xml + * + * dbusidl2cpp is Copyright (C) 2006 Trolltech AS. All rights reserved. + * + * This is an auto-generated file. + */ + +#ifndef CHATADAPTOR_H_88051142890130 +#define CHATADAPTOR_H_88051142890130 + +#include +#include +class QByteArray; +template class QList; +template class QMap; +class QString; +class QStringList; +class QVariant; + +/* + * Adaptor class for interface com.trolltech.ChatInterface + */ +class ChatInterfaceAdaptor: public QDBusAbstractAdaptor +{ + Q_OBJECT + Q_CLASSINFO("D-Bus Interface", "com.trolltech.ChatInterface") +public: + ChatInterfaceAdaptor(QObject *parent); + virtual ~ChatInterfaceAdaptor(); + +public: // PROPERTIES +public slots: // METHODS +signals: // SIGNALS + void action(const QString &nickname, const QString &text); + void message(const QString &nickname, const QString &text); +}; + +#endif diff --git a/qt/examples/chatmainwindow.ui b/qt/examples/chatmainwindow.ui new file mode 100644 index 00000000..0e4461c3 --- /dev/null +++ b/qt/examples/chatmainwindow.ui @@ -0,0 +1,188 @@ + + + + + ChatMainWindow + + + + 0 + 0 + 800 + 600 + + + + QtDBus Chat + + + + + 9 + + + 6 + + + + + 0 + + + 6 + + + + + false + + + Messages sent and received from other users + + + true + + + + + + + 0 + + + 6 + + + + + Message: + + + messageLineEdit + + + + + + + + + + + 1 + 0 + 0 + 0 + + + + Sends a message to other people + + + + + + Send + + + + + + + + + + + + + 0 + 0 + 800 + 31 + + + + + Help + + + + + + File + + + + + + + + + + + + Quit + + + Ctrl+Q + + + + + + + + About Qt... + + + + + Change nickname... + + + Ctrl+N + + + + + + chatHistory + messageLineEdit + sendButton + + + + + messageLineEdit + returnPressed() + sendButton + animateClick() + + + 299 + 554 + + + 744 + 551 + + + + + actionQuit + triggered(bool) + ChatMainWindow + close() + + + -1 + -1 + + + 399 + 299 + + + + + diff --git a/qt/examples/chatsetnickname.ui b/qt/examples/chatsetnickname.ui new file mode 100644 index 00000000..fb9894e0 --- /dev/null +++ b/qt/examples/chatsetnickname.ui @@ -0,0 +1,149 @@ + + + + + NicknameDialog + + + + 0 + 0 + 396 + 105 + + + + + 1 + 1 + 0 + 0 + + + + Set nickname + + + + 9 + + + 6 + + + + + 0 + + + 6 + + + + + + 1 + 1 + 0 + 0 + + + + New nickname: + + + + + + + + + + + + 0 + + + 6 + + + + + Qt::Horizontal + + + + 131 + 31 + + + + + + + + OK + + + + + + + Cancel + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + + + + + + okButton + clicked() + NicknameDialog + accept() + + + 278 + 253 + + + 96 + 254 + + + + + cancelButton + clicked() + NicknameDialog + reject() + + + 369 + 253 + + + 179 + 282 + + + + + diff --git a/qt/examples/com.trolltech.ChatInterface.xml b/qt/examples/com.trolltech.ChatInterface.xml new file mode 100644 index 00000000..666a9e35 --- /dev/null +++ b/qt/examples/com.trolltech.ChatInterface.xml @@ -0,0 +1,15 @@ + + + + + + + + + + + + + + diff --git a/qt/examples/complexping.cpp b/qt/examples/complexping.cpp new file mode 100644 index 00000000..b977edfc --- /dev/null +++ b/qt/examples/complexping.cpp @@ -0,0 +1,91 @@ +/* -*- C++ -*- + * + * Copyright (C) 2006 Trolltech AS. All rights reserved. + * Author: Thiago Macieira + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +#include + +#include +#include +#include +#include +#include + +#include "ping-common.h" +#include "complexping.h" + +void Ping::start(const QString &name, const QString &oldValue, const QString &newValue) +{ + Q_UNUSED(oldValue); + + if (name != SERVICE_NAME || newValue.isEmpty()) + return; + + // open stdin for reading + qstdin.open(stdin, QIODevice::ReadOnly); + + // find our remote + iface = QDBus::sessionBus().findInterface(SERVICE_NAME, "/"); + if (!iface) { + fprintf(stderr, "%s\n", + qPrintable(QDBus::sessionBus().lastError().message())); + QCoreApplication::instance()->quit(); + } + + connect(iface, SIGNAL(aboutToQuit()), QCoreApplication::instance(), SLOT(quit())); + + while (true) { + qDebug() << "Ready"; + + QString line = QString::fromLocal8Bit(qstdin.readLine()).trimmed(); + if (line.isEmpty()) { + iface->call("quit"); + return; + } else if (line == "value") { + QVariant reply = iface->property("value"); + if (!reply.isNull()) + qDebug() << "value =" << reply.toString(); + } else if (line.startsWith("value=")) { + iface->setProperty("value", line.mid(6)); + } else { + QDBusReply reply = iface->call("query", line); + if (reply.isSuccess()) + qDebug() << "Reply was:" << reply.value(); + } + + if (iface->lastError().isValid()) + fprintf(stderr, "Call failed: %s\n", qPrintable(iface->lastError().message())); + } +} + +int main(int argc, char **argv) +{ + QCoreApplication app(argc, argv); + Ping ping; + ping.connect(QDBus::sessionBus().busService(), + SIGNAL(nameOwnerChanged(QString,QString,QString)), + SLOT(start(QString,QString,QString))); + + QProcess pong; + pong.start("./complexpong"); + + app.exec(); +} + +#include "complexping.moc" diff --git a/qt/examples/complexping.h b/qt/examples/complexping.h new file mode 100644 index 00000000..4332ef8a --- /dev/null +++ b/qt/examples/complexping.h @@ -0,0 +1,37 @@ +/* -*- C++ -*- + * + * Copyright (C) 2006 Trolltech AS. All rights reserved. + * Author: Thiago Macieira + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +#ifndef COMPLEXPING_H +#define COMPLEXPING_H + +#include + +class Ping: public QObject +{ + Q_OBJECT +public slots: + void start(const QString &, const QString &, const QString &); +public: + QFile qstdin; + QDBusInterface *iface; +}; + +#endif diff --git a/qt/examples/complexpong.cpp b/qt/examples/complexpong.cpp new file mode 100644 index 00000000..1d9c9756 --- /dev/null +++ b/qt/examples/complexpong.cpp @@ -0,0 +1,85 @@ +/* -*- C++ -*- + * + * Copyright (C) 2006 Trolltech AS. All rights reserved. + * Author: Thiago Macieira + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +#include + +#include +#include +#include + +#include "ping-common.h" +#include "complexpong.h" + +// the property +QString Pong::value() const +{ + return m_value; +} + +void Pong::setValue(const QString &newValue) +{ + m_value = newValue; +} + +void Pong::quit() +{ + QTimer::singleShot(0, QCoreApplication::instance(), SLOT(quit())); +} + +QVariant Pong::query(const QString &query) +{ + QString q = query.toLower(); + if (q == "hello") + return "World"; + if (q == "ping") + return "Pong"; + if (q == "the answer to life, the universe and everything") + return 42; + if (q.indexOf("unladen swallow") != -1) { + if (q.indexOf("european") != -1) + return 11.0; + return QByteArray("african or european?"); + } + + return "Sorry, I don't know the answer"; +} + +int main(int argc, char **argv) +{ + QCoreApplication app(argc, argv); + + QDBusBusService *bus = QDBus::sessionBus().busService(); + + QObject obj; + Pong *pong = new Pong(&obj); + pong->connect(&app, SIGNAL(aboutToQuit()), SIGNAL(aboutToQuit())); + pong->setProperty("value", "initial value"); + QDBus::sessionBus().registerObject("/", &obj); + + if (bus->requestName(SERVICE_NAME, QDBusBusService::AllowReplacingName) != + QDBusBusService::PrimaryOwnerReply) + exit(1); + + app.exec(); + return 0; +} + +#include "complexpong.moc" diff --git a/qt/examples/complexpong.h b/qt/examples/complexpong.h new file mode 100644 index 00000000..eaf5065f --- /dev/null +++ b/qt/examples/complexpong.h @@ -0,0 +1,46 @@ +/* -*- C++ -*- + * + * Copyright (C) 2006 Trolltech AS. All rights reserved. + * Author: Thiago Macieira + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +#ifndef COMPLEXPONG_H +#define COMPLEXPONG_H + +#include + +class Pong: public QDBusAbstractAdaptor +{ + Q_OBJECT + Q_CLASSINFO("D-Bus Interface", "com.trolltech.QtDBus.ComplexPong.Pong") + Q_PROPERTY(QString value READ value WRITE setValue); +public: + QString m_value; + QString value() const; + void setValue(const QString &newValue); + + Pong(QObject *obj) : QDBusAbstractAdaptor(obj) + { } +signals: + void aboutToQuit(); +public slots: + QVariant query(const QString &query); + Q_ASYNC void quit(); +}; + +#endif diff --git a/qt/examples/dbus.cpp b/qt/examples/dbus.cpp new file mode 100644 index 00000000..e011381b --- /dev/null +++ b/qt/examples/dbus.cpp @@ -0,0 +1,318 @@ +/* -*- C++ -*- + * + * Copyright (C) 2006 Trolltech AS. All rights reserved. + * Author: Thiago Macieira + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +#include + +#include +#include +#include +#include +#include +#include + +Q_DECLARE_METATYPE(QVariant) +QDBusConnection *connection; + +void listObjects(const QString &service, const QString &path) +{ + QDBusInterface *iface = connection->findInterface(service, path.isEmpty() ? "/" : path, + "org.freedesktop.DBus.Introspectable"); + QDBusReply xml = iface->call("Introspect"); + + if (xml.isError()) + return; // silently + + QDomDocument doc; + doc.setContent(xml); + QDomElement node = doc.documentElement(); + QDomElement child = node.firstChildElement(); + while (!child.isNull()) { + if (child.tagName() == QLatin1String("node")) { + QString sub = path + '/' + child.attribute("name"); + printf("%s\n", qPrintable(sub)); + listObjects(service, sub); + } + child = child.nextSiblingElement(); + } + + delete iface; +} + +void listInterface(const QString &service, const QString &path, const QString &interface) +{ + QDBusInterface *iface = connection->findInterface(service, path, interface); + const QMetaObject *mo = iface->metaObject(); + + // properties + for (int i = mo->propertyOffset(); i < mo->propertyCount(); ++i) { + QMetaProperty mp = mo->property(i); + printf("property "); + + if (mp.isReadable() && mp.isWritable()) + printf("readwrite"); + else if (mp.isReadable()) + printf("read"); + else + printf("write"); + + printf(" %s %s.%s\n", mp.typeName(), qPrintable(interface), mp.name()); + } + + // methods (signals and slots) + for (int i = mo->methodOffset(); i < mo->methodCount(); ++i) { + QMetaMethod mm = mo->method(i); + + QByteArray signature = mm.signature(); + signature.truncate(signature.indexOf('(')); + printf("%s %s%s%s %s.%s(", + mm.methodType() == QMetaMethod::Signal ? "signal" : "method", + mm.tag(), *mm.tag() ? " " : "", + *mm.typeName() ? mm.typeName() : "void", + qPrintable(interface), signature.constData()); + + QList types = mm.parameterTypes(); + QList names = mm.parameterNames(); + bool first = true; + for (int i = 0; i < types.count(); ++i) { + printf("%s%s", + first ? "" : ", ", + types.at(i).constData()); + if (!names.at(i).isEmpty()) + printf(" %s", names.at(i).constData()); + first = false; + } + printf(")\n"); + } + delete iface; +} + +void listAllInterfaces(const QString &service, const QString &path) +{ + QDBusInterface *iface = connection->findInterface(service, path, + "org.freedesktop.DBus.Introspectable"); + QDBusReply xml = iface->call("Introspect"); + + if (xml.isError()) + return; // silently + + QDomDocument doc; + doc.setContent(xml); + QDomElement node = doc.documentElement(); + QDomElement child = node.firstChildElement(); + while (!child.isNull()) { + if (child.tagName() == QLatin1String("interface")) { + listInterface(service, path, child.attribute("name")); + } + child = child.nextSiblingElement(); + } + + delete iface; +} + +QDBusInterface *findMember(const QString &service, const QString &path, const QString &member) +{ + QDBusInterface *iface = connection->findInterface(service, path, + "org.freedesktop.DBus.Introspectable"); + QDBusReply xml = iface->call("Introspect"); + + if (xml.isError()) + return 0; + + QDomDocument doc; + doc.setContent(xml); + QDomElement node = doc.documentElement(); + QDomElement child = node.firstChildElement("interface"); + while (!child.isNull()) { + QDomElement subchild = child.firstChildElement("method"); + while (!subchild.isNull()) { + if (subchild.attribute("name") == member) { + QDBusInterface *retval; + retval = connection->findInterface(service, path, child.attribute("name")); + delete iface; + return retval; + } + subchild = subchild.nextSiblingElement("method"); + } + + child = child.nextSiblingElement("interface"); + } + + delete iface; + return 0; +} + +QStringList readList(int &argc, const char *const *&argv) +{ + --argc; + ++argv; + + QStringList retval; + while (argc && QLatin1String(argv[0]) != ")") + retval += QString::fromLocal8Bit(argv[0]); + + return retval; +} + +void placeCall(const QString &service, const QString &path, const QString &interface, + const QString &member, int argc, const char *const *argv) +{ + QDBusInterface *iface; + if (interface.isEmpty()) + iface = findMember(service, path, member); + else + iface = connection->findInterface(service, path, interface); + + if (!iface) { + fprintf(stderr, "Interface '%s' not available in object %s at %s\n", + qPrintable(interface), qPrintable(path), qPrintable(service)); + exit(1); + } + + const QMetaObject *mo = iface->metaObject(); + QByteArray match = member.toLatin1(); + match += '('; + + int midx; + for (int i = mo->methodOffset(); i < mo->methodCount(); ++i) { + QMetaMethod mm = mo->method(i); + QByteArray signature = mm.signature(); + if (signature.startsWith(match)) { + midx = i; + break; + } + } + + if (midx == -1) { + fprintf(stderr, "Cannot find '%s.%s' in object %s at %s\n", + qPrintable(interface), qPrintable(member), qPrintable(path), + qPrintable(service)); + exit(1); + } + + QMetaMethod mm = iface->metaObject()->method(midx); + QList types = mm.parameterTypes(); + + QVariantList params; + for (int i = 0; argc && i < types.count(); ++i) { + int id = QVariant::nameToType(types.at(i)); + if (id == QVariant::UserType || id == QVariant::Map) { + fprintf(stderr, "Sorry, can't pass arg of type %s yet\n", + types.at(i).constData()); + exit(1); + } + + Q_ASSERT(id); + + QVariant p; + if ((id == QVariant::List || id == QVariant::StringList) && QLatin1String("(") == argv[0]) + p = readList(argc, argv); + else + p = QString::fromLocal8Bit(argv[0]); + + p.convert( QVariant::Type(id) ); + params += p; + --argc; + ++argv; + } + if (params.count() != types.count()) { + fprintf(stderr, "Invalid number of parameters\n"); + exit(1); + } + + QDBusMessage reply = iface->callWithArgs(member, params); + if (reply.type() == QDBusMessage::ErrorMessage) { + QDBusError err = reply; + printf("Error: %s\n%s\n", qPrintable(err.name()), qPrintable(err.message())); + exit(2); + } else if (reply.type() != QDBusMessage::ReplyMessage) { + fprintf(stderr, "Invalid reply type %d\n", int(reply.type())); + exit(1); + } + + foreach (QVariant v, reply) { + if (v.userType() == qMetaTypeId()) + v = qvariant_cast(v); + printf("%s\n", qPrintable(v.toString())); + } + + delete iface; + exit(0); +} + +int main(int argc, char **argv) +{ + QCoreApplication app(argc, argv); + if (argc >= 1 && qstrcmp(argv[1], "--system") == 0) { + connection = &QDBus::systemBus(); + --argc; + ++argv; + } else + connection = &QDBus::sessionBus(); + + QDBusBusService *bus = connection->busService(); + + if (argc == 1) { + QStringList names = bus->ListNames(); + foreach (QString name, names) + printf("%s\n", qPrintable(name)); + exit(0); + } + + QString service = QLatin1String(argv[1]); + if (!QDBusUtil::isValidBusName(service)) { + fprintf(stderr, "Service '%s' is not a valid name.\n", qPrintable(service)); + exit(1); + } + if (!bus->NameHasOwner(service)) { + fprintf(stderr, "Service '%s' does not exist.\n", qPrintable(service)); + exit(1); + } + + if (argc == 2) { + printf("/\n"); + listObjects(service, QString()); + exit(0); + } + + QString path = QLatin1String(argv[2]); + if (!QDBusUtil::isValidObjectPath(path)) { + fprintf(stderr, "Path '%s' is not a valid path name.\n", qPrintable(path)); + exit(1); + } + if (argc == 3) { + listAllInterfaces(service, path); + exit(0); + } + + QString interface = QLatin1String(argv[3]); + QString member; + int pos = interface.lastIndexOf(QLatin1Char('.')); + if (pos == -1) { + member = interface; + interface.clear(); + } else { + member = interface.mid(pos + 1); + interface.truncate(pos); + } + + placeCall(service, path, interface, member, argc - 4, argv + 4); +} + diff --git a/qt/examples/hello.cpp b/qt/examples/hello.cpp new file mode 100644 index 00000000..a287bcb0 --- /dev/null +++ b/qt/examples/hello.cpp @@ -0,0 +1,33 @@ +/* -*- C++ -*- + * + * Copyright (C) 2006 Trolltech AS. All rights reserved. + * Author: Thiago Macieira + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +#include + +#include +#include + +int main(int argc, char **argv) +{ + QCoreApplication app(argc, argv); + + printf("Hello, World\n"); + return 0; +} diff --git a/qt/examples/ping-common.h b/qt/examples/ping-common.h new file mode 100644 index 00000000..1870275d --- /dev/null +++ b/qt/examples/ping-common.h @@ -0,0 +1,22 @@ +/* -*- C++ -*- + * + * Copyright (C) 2006 Trolltech AS. All rights reserved. + * Author: Thiago Macieira + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +#define SERVICE_NAME "com.trolltech.QtDBus.PingExample" diff --git a/qt/examples/ping.cpp b/qt/examples/ping.cpp new file mode 100644 index 00000000..ffa53a42 --- /dev/null +++ b/qt/examples/ping.cpp @@ -0,0 +1,48 @@ +/* -*- C++ -*- + * + * Copyright (C) 2006 Trolltech AS. All rights reserved. + * Author: Thiago Macieira + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +#include + +#include +#include + +#include "ping-common.h" + +int main(int argc, char **argv) +{ + QCoreApplication app(argc, argv); + + QDBusInterface *iface = QDBus::sessionBus().findInterface(SERVICE_NAME, "/"); + if (iface) { + QDBusReply reply = iface->call("ping", argc > 1 ? argv[1] : ""); + if (reply.isSuccess()) { + printf("Reply was: %s\n", qPrintable(reply.value())); + return 0; + } + + fprintf(stderr, "Call failed: %s\n", qPrintable(reply.error().message())); + return 1; + } + + fprintf(stderr, "%s\n", + qPrintable(QDBus::sessionBus().lastError().message())); + return 1; +} diff --git a/qt/examples/pong.cpp b/qt/examples/pong.cpp new file mode 100644 index 00000000..3917bb58 --- /dev/null +++ b/qt/examples/pong.cpp @@ -0,0 +1,53 @@ +/* -*- C++ -*- + * + * Copyright (C) 2006 Trolltech AS. All rights reserved. + * Author: Thiago Macieira + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +#include + +#include +#include +#include + +#include "ping-common.h" +#include "pong.h" + +QString Pong::ping(const QString &arg) +{ + QTimer::singleShot(0, QCoreApplication::instance(), SLOT(quit())); + return QString("ping(\"%1\") got called").arg(arg); +} + +int main(int argc, char **argv) +{ + QCoreApplication app(argc, argv); + + QDBusBusService *bus = QDBus::sessionBus().busService(); + if (bus->requestName(SERVICE_NAME, QDBusBusService::AllowReplacingName) != + QDBusBusService::PrimaryOwnerReply) + exit(1); + + Pong pong; + QDBus::sessionBus().registerObject("/", &pong, QDBusConnection::ExportSlots); + + app.exec(); + return 0; +} + +#include "pong.moc" diff --git a/qt/examples/pong.h b/qt/examples/pong.h new file mode 100644 index 00000000..de89598f --- /dev/null +++ b/qt/examples/pong.h @@ -0,0 +1,34 @@ +/* -*- C++ -*- + * + * Copyright (C) 2006 Trolltech AS. All rights reserved. + * Author: Thiago Macieira + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +#ifndef PONG_H +#define PONG_H + +#include + +class Pong: public QObject +{ + Q_OBJECT +public slots: + Q_SCRIPTABLE QString ping(const QString &arg); +}; + +#endif -- cgit