KDE 4.0 Release Counter

Friday, March 20, 2009

A Journey to WebKit via Plasmaland.

While writing a little Plasma utility I was tempted to use the new QT WebKit toolkit to handle the User Interface.

To integrate WebKit in a KDE or QT program is no too diffcult, thanks to metadata that QT adds to C++ classes. It is possible to call directly methods of objects resident in our program from HTML pages via Javascipt taking advantage of slot and signals mechanism.

Firstly, we need to insert references to our application C++ objects in HTML pages, as if thery were native JavaScript variables, using an identifier that will be mapped later to physical objects.

After that we can encode JavaScript functions that take advantage of the methods declared as slots, at certain events, such as pressing a button.

We can then excute embedded objects methods from HTML passing as parameters a HTML level defined entities, such as the values of a form fields.

Each link between WebKit and our application will begin through Javascript functions, so WebKit will commence operations but we can call frame 'evaluateJavascript' to execute a script in the frame context from our host program.

Let's do an example:

Suppose that our goal is to recall the method 'reload' of an instance of a C++ class described in the following way from Javascript :

class DBusApplet : public Plasma::PopupApplet
{
Q_OBJECT
...
public
slots:
void reload(const QString &selection);
private:
...
};

In Javascript our application class instance may be mapped as follows, assuming that it is associated to the variable 'appletInterface':



function execute(selection)
{
appletInterface.reload(selection);
}


Since the conversion of basic types is automatic, what we should do is to 'inject' our C++ objects into WebKit component, but we have to wait until the component alerts us when is the right moment to do it.

After that WebKit has fully laoded a HTML page, assigned by the program directly as string or through a URL, the current frame emits the signal "javaScriptWindowObjectCleared" which must have taken care of in the usual way that QT will make available:


connect(page()->mainFrame(),

SIGNAL(javaScriptWindowObjectCleared()), this, SLOT(populateJavaScriptWindowObject()));

Upon receipt of the signal our 'populateJavaScriptWindowObject' method will be called so we can assign the reference of our objects to WebKit using frame 'addToJavaScriptWindowObject' method, whose arguments are the name of Javascript variable that will contain the reference, and our object address.

page->mainFrame()->addToJavaScriptWindowObject(”appletInterface”, this);


http://doc.trolltech.com/4.4/qtwebkit.html

http://webkit.org/

http://code.google.com/p/dbusapplet/

Friday, January 4, 2008

Happy Birthday KDE 4.0.0

Maybe it is not the move that can save the mankind or a masterpiece like the Gioconda, but KDE 4.0.0 is virtually ready for the half January release, in a style event at the Google base in Mountain View.
Actual version is not going to be an environment designed to replace KDE 3.5 right now, but instead it is a base over which tomorrow's KDE can grow. Open Source software development process is different from proprietary software one and Aaron Seigo's post explains how: http://aseigo.blogspot.com/2008/01/talking-bluntly.html.
Last moment translations (and code freeze) hurry and chaos to respect the settled deadline, seemed to me the mood of a newspaper. After all, more than 700 commit in a couple of days is not a usual event.
Maybe it is too late for desktop applications, but KDE Four is a really charming adventure.

Good Year, KDE; on Monday we have to return to work using Windows :(

Sunday, December 9, 2007

Android: how to show items with different styles in a ListView

In the Android environment we can visualize ListView items using ArrayAdapter object, which act as interface between textual data representation and ListView. Its constructor accept a layout resource id pointing to a TextView instanced at each data row visualization.
If we have the need to present the various items with different layouts, we can do it in a simple way by using the excellent engineering of the base Adapter class, adapting it for our needs. Let's suppose to want to show an icon next to the textual item description if item text contains a given character sequence, let's say it starts with 'a' character to make a simple example.
First of all we need two View class derived objects to show the data into the ListView. The first one, 'SView1' in our example, simply derives from TextView class and it will be populated with text string passed as parameter in its constructor.

public class SView1 extends TextView {
TextView mText;
public SView1 (Context context, String text)
{
super(context);
setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
setText(text);
}
}

The second one, 'SView2', is slightly more complex and extends LinearLayout, a class designed to group and organize child Views. In SView2 we'll create two children objects, an ImageView to handle the icon, read from the resources, and a TextView which will contains the data strings.

public class SView2 extends LinearLayout {
String Title="";
TextView mText;
ImageView mView;
public SView2 (Context context, String text)
{
super(context);
this.setOrientation(HORIZONTAL);
setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
mView = new ImageView(mContext);
mView.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
mView.setImageResource(R.drawable.icon);
addView(mView);
mText = new TextView(context);
mText.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
addView(mText);
setText(text);
}
public void setText(String text)
{
mText.setText(text);
}
}

Now we have only to personalize the data hander, Adapter class. We can create a class that extends directly ArrayAdapter<> class. The data handling methods can be inherited from the superclass, by overloading only getView() method, called by parent ListView, at each data row visualization. What we will do in this method is only to check string content, and decide view type, SView1 or SView2 instantiate. Parent ListView can propose us to reuse an existent View object, if we want to use it, we should check that the proposed View type is coherent with data. Final step is in the Activity object: we should instantiate an Adapter e populate it with our data.

I was forgetting: yesterday RubyOnRails 2.0 was born, congratulations.
http://weblog.rubyonrails.org/2007/12/7/rails-2-0-it-s-done

Adapter:
public class SimpleStringAdapter extends ArrayAdapter {

public SimpleStringAdapter(Context context, List Items ) {
super(context,R.layout.a,Items);
}
public View getView(int position, View convertView, ViewGroup parent) {
String s = getItem(position);
boolean isType1 = true;
if( s==null )
s = "";
if(s.startsWith("a"))
{
isType1 = false;
}
if(isType1)
{
SView1 theView ;
if((null==convertView )||!(convertView instanceof SView1))
theView = new SView1(super.getContext(),s);
else
{
theView = (SView1)convertView;
theView.setText(s);
}
return theView ;
} else
{
SView2 theView ;
if((null==convertView )||!(convertView instanceof SView2))
theView = new SView2(super.getContext(),s);
else
{
theView = (SView2)convertView;
theView.setText(s);
}
return theView ;
}
}
}

Activity:

public class SimpleView extends ListActivity {
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);

List items = new ArrayList();
items.add("a riga 1");
items.add("a riga 2");
items.add("b riga 3");
items.add("a riga 4");

SimpleStringAdapter strings =
new SimpleStringAdapter(this, items);
setListAdapter(strings);
}
}

Thursday, December 6, 2007

Life with Android

I was finally able to explore the Android development environment. The first thing I noted is the easy SDK installation process, you only have to unzip a file and to set an environment variable. If you want to use Eclipse as development environment, you need to download a plugin from Google site. A wonderful thing is the porting of the SDK under different Operating Systems: Linux, MacOS and Windows.
The SDK comes with a good emulator included but sometimes, at least in Linux environment, it loses the synchronism with Eclipse in debug phase.
Anyway the tools used under the hood are command line commands, adb in this case, that reside in SDK tools directory, and in a hurry we can kill its process so Eclipse can restart it in its next debug session. The same SDK seems to be the first step toward a more complete system and various features are reporting not working in the project mailing list. All things considered Android is a very good first step, documentation is complete and it covers each toolkit area, but we can expect a good growth. As a pilot project I wrote a little program to read planet.kde.org
feeds, one of my favorite sites. I explored some Android technologies, background services, sqlite database, custom views, and integrated browser, and I enjoyed the system very much, even though I didn't explore multimedia features. The project has been published on Google Code at http://code.google.com/p/planetandroid/ address. The license is obviously GPL.