Next Previous Table of Contents

p5 is our first application that will communicate with other applications (p6, in this case). This is the the source:
require 'Korundum'
class MainWindow < KDE::MainWindow
slots 'changeLocation()',
'openURLRequest(const KURL&, const KParts::URLArgs& )',
'bookLocation()'
def initialize( name )
super(nil, name)
setCaption("KDE Tutorial - p5")
filemenu = Qt::PopupMenu.new
filemenu.insertItem( i18n( "&Quit" ), $kapp, SLOT( 'quit()' ) )
about =
i18n("p5 1.0\n\n" +
"(C) 1999-2002 Antonio Larrosa Jimenez\n" +
"larrosa@kde.org\t\tantlarr@supercable.es\n" +
"Malaga (Spain)\n\n" +
"Simple KDE Tutorial\n" +
"This tutorial comes with ABSOLUTELY NO WARRANTY \n" +
"This is free software, and you are welcome to redistribute it\n" +
"under certain conditions\n");
helpmenu = helpMenu(about)
menu = menuBar()
menu.insertItem( i18n( "&File" ), filemenu)
menu.insertSeparator()
menu.insertItem(i18n("&Help"), helpmenu)
vbox = Qt::VBox.new( self )
@location = Qt::LineEdit.new( vbox )
@location.setText( "http://localhost" )
connect( @location , SIGNAL( 'returnPressed()' ),
self, SLOT( 'changeLocation()' ) )
split = Qt::Splitter.new( vbox )
split.setOpaqueResize()
bookmark = Qt::PushButton.new( i18n("Add to Bookmarks"), split );
connect( bookmark, SIGNAL( 'clicked()' ), self, SLOT( 'bookLocation()' ) )
@browser = KDE::HTMLPart.new( split )
@browser.openURL( KDE::URL.new(@location.text()) )
connect( @browser.browserExtension(),
SIGNAL( 'openURLRequest( const KURL&, const KParts::URLArgs& )' ),
self, SLOT( 'openURLRequest(const KURL&, const KParts::URLArgs& )' ) )
setCentralWidget(vbox)
end
def changeLocation()
@browser.openURL( KDE::URL.new(@location.text()) )
end
def openURLRequest(url, part)
@location.text = url.url()
changeLocation()
end
def bookLocation()
dcopRef = KDE::DCOPRef.new("p6", "BookMarkList")
if ! dcopRef.add(@location.text())
Qt.qWarning("Error with DCOP\n")
end
end
end
about = KDE::AboutData.new("p5", "Tutorial - p5", "0.1")
KDE::CmdLineArgs.init(ARGV, about)
a = KDE::Application.new()
window = MainWindow.new( "Tutorial - p5" )
window.resize( 300, 200 )
a.mainWidget = window
window.show
a.exec
This application and p6 are a simple example of the new technology for desktop communication, DCOP. DCOP is an ICE based IPC/RPC mechanism that uses unix domain sockets or TCP/IP sockets transparently.
p6 will be an application that stores the user bookmarks that p5 tells it to store. Let's see how easily we can do it:
slots 'changeLocation()',
'openURLRequest(const KURL&, const KParts::URLArgs& ),
'bookLocation()'
We are going to use a new slot that will be called when the user wants to store a location into his bookmark application. There are also a couple of new widgets we will need.
split = Qt::Splitter.new( vbox )
split.setOpaqueResize()
We want to display a big button at the side of the html browser widget. Instead of making them a fixed size, we will let the user to change the size of the button, this is done with a Qt::Splitter widget, that is a widget that distributes its size among its children.
We set split to do opaque resizing for the children to be
painted while the user changes the size instead of just when the user
finishes moving it.
bookmark = Qt::PushButton.new( i18n("Add to Bookmarks"), split )
connect( bookmark, SIGNAL( 'clicked()' ), self, SLOT( 'bookLocation()' ) )
Now we can create a new Qt::PushButton with localized text.
The clicked() signal is connected to our
bookLocation() slot.
@browser = KDE::HTMLPart.new( split )
Along with the bookmark button, browser is now a child of
split . This is done this way to let split
manage the sizes of both widgets as told earlier.
dcopRef = KDE::DCOPRef.new("p6", "BookMarkList")
We can attach this application to the DCOP server that
should be running on the system (dcopserver is a fundamental part of the
desktop so it will be always running), and get a DCOP reference to the
p6 application.
The first parameter is the name of the application that we want to connect to.
The second one is the name of the object in the application that we want
to use.
When the user clicks on the button to add the current url to the bookmark list this slot is called.
if ! dcopRef.add(@location.text())
Qt.qWarning("Error with DCOP\n")
end
Finally, we send a DCOP call to p6.
In other words, p6 is an application which has an object called
BookMarkList, and this object has a method called add
that has a String parameter, so this simple line calls the given method on
p6 with the given parameters. As we don't need a return value from p6, it will
return inmediately and continue running p5 while p6 adds the url to the
bookmark list . Isn't it lovely ? ;-)
We are checking the return value just in case the dcopserver couldn't complete the call. In this case, we use the Qt.qWarning routine to let the application show us there's something wrong. As this is a message intended only for developers, we don't need to use i18n here. And that's all in p5. You should run p5 and p6 at the same time to let them work in tandem and see how they communicate.
Next Previous Table of Contents