08 May 2010

Using D-Bus to shutdown the machine

I don't know why I don't like using /sbin/shutdown to switch off the computer :) But that is the fact! I am not using Gnome\KDE\XFCE\whatever_else_complete_DE. I'm running Archlinux with Openbox and several tools. So I wanted to allow non-privileged user to be able to shutdown the computer by a single click. Firstly I had to find out, what should I send over D-Bus (and whom to) to make my PC shutdown. Crawling the web I found an utility which allowed me to find what I needed in a few minutes. It's called DBus-Inspector. So having a message and destination it just remained to send it using dbus-send:
$ dbus-send --system --print-reply --dest=org.freedesktop.Hal \
          /org/freedesktop/Hal/devices/computer \
          org.freedesktop.Hal.Device.SystemPowerManagement.Shutdown
If you will try to execute it, you may receive and error saying that you don't have permissions to send messages to org.freedesktop.Hal.devices.computer interface. If you did, you should open hal.conf from system config directory of D-Bus (mine is /etc/dbus-1/system.d/hal.conf) and add the following policy:
 <policy group="shutdown">
   <allow send_destination="org.freedesktop.Hal"
          send_interface="org.freedesktop.Hal.Device.SystemPowerManagement"/>
 </policy>
Now all the members of group shutdown will be able to switch off the computer by D-Bus. You just have to create the group:
# groupadd shutdown
And add your user to this group:
# gpasswd -a user shutdown
Finally you may want to create a script or desktop entry:
[Desktop Entry]
Icon=system-shutdown
Name=Shutdown
Exec=dbus-send --system --print-reply --dest=org.freedesktop.Hal /org/freedesktop/Hal/devices/computer org.freedesktop.Hal.Device.SystemPowerManagement.Shutdown

12 April 2010

Google maps: getMaxZoomAtLatLng and error 500

When I had to catch zoom level when 'no images available for this zoom level', I thought I had pretty quickly found the solution. But I was completely disappointed when this one didn't work. After some research I've managed to make it work :) First of all I needed this in GWT application. We were using GWT Maps (1.0.4 by that time) which lack this function as such. So here is how we can bridge this function using JSNI:
class GoogleMapsJsBridgeSample {
    /**
     * Wrapper function to native call
     */
    public void getMaxZoomAtLatLng(MapType mapType, double lat, double lon) {
        getMaxZoomAtLatLng(mapType.getPeer(), lat, lon);
    }
    
    private native void getMaxZoomAtLatLng(JavaScriptObject mapTypePeer, double lat, double lon) /*-{
        var latLng = new $wnd.GLatLng(lat, lon);
        mapTypePeer.getMaxZoomAtLatLng(latLng, function(response) {
            if (response && response['status'] == $wnd.G_GEO_SUCCESS) {
                this@com.company.my.GoogleMapsJsBridgeSample::doSomethingWithMaxZoomAtLatLng(I)(response['zoom'])-*/;
            }
        });
    }
    
    private void doSomethingWithMaxZoomAtLatLng(int maxZoom) {
        getMap().setZoom(maxZoom);
    }
}

The point here is is NOT using MapWidget.getCurrentMapType() due a bug in gmaps-api (not GWT specific). According to the comments for the issue, problem appears only for hybrid map type, but for satellite and normal it is ok! So you can use to code above in the following manner in GWT:

GoogleMapsJsBridgeSample jsBridge = new GoogleMapsJsBridgeSample();
// ...
jsBridge.getMaxZoomAtLatLng(MapType.getSatelliteMap(), lat, lon);
// or use MapType.getNormalMap() for street map view

Such issues are sometimes specific to the version of gmaps-api you are using. You can always make sure what particular version is used in your application by outputting G_API_VERSION JavaScript variable.

Changelogs for each gmaps-api version are available at http://mapki.com/

17 March 2010

Eclipse to IntelliJ IDEA keymap migration cheatsheet

EclipseIntelliJ IDEAAction
Ctrl+OCtrl+F12Go to member popup
Eclipse: outline current source
IDEA: file structure popup
Ctrl+Shift+TCtrl+NOpen class by name
Eclipse: Open Type
IDEA: Go to - Class...
Ctrl+Shift+RCtrl+Shift+NOpen file by name
Eclipse: Jump to file
IDEA: Go to - File...
?F11Toggle bookmark
?Shift+F11Eclipse: ?
IDEA: Show Bookmarks
?Ctrl+]\[Eclipse: ?
IDEA: Move codeblock end\start

15 March 2010

IE Developer Toolbar for IEs4Linux

IEs4Linux is know the best approach of running Internet Explorer under Linux. Working on a web project I faced a need to fix some style bugs in IE. So I tried to install IE Developer Toolbar (for those who don't know - just like FireBug, but far less cool) and saw error message from msiexec saying "Internet Explorer Development Toolbar requires Windows 2000 or newer".

So this post is about how to install the toolbar anyway! :)

The point is that IEs4Linux runs IE in wine environment with Win 98 version (at least for IE6 this is true), that why developer toolbar doesn't installs. To fix this, execute:

$ cat `which ie6` | grep WINEPREFIX
export WINEPREFIX="/opt/.ies4linux/ie6" 

You will most probably see other path instead of  /opt/.ies4linux/ie6. So now we know where the wine environment for wine is located. The next thing to do is to run winecfg for this environment and change target Win version:

$ WINEPREFIX="/opt/.ies4linux/ie6" winecfg

A dialog window will appear and in drop-down in the bottom of the window select Windows XP and press OK.

With this changes your IE won't run. So, download MSI of developer toolbar to your home directory with your favourite browser and install it to your wine environment with the following:

$ cd ~ 
$ WINEPREFIX="/opt/.ies4linux/ie6" msiexec /i IEDevToolBarSetup.msi
 
After installing the toolbar, run winecfg just like we previously did and switch Win version back to Windows 98.

Voila, we have IE with developer toolbar :)

17 February 2010

Analogue of FreeBSD watch for Linux

Being using watch (8) utility in FreeBSD I have never knew it's analogue for Linux. When facing such a need I always managed to find another ways to meet it. Just yesterday I had much alike case :) and finally I've found what I needed all this time. Well, there are plenty of them actually :). I have found snoop, ttysnoop (haven't found web site, but there are a lot of prebuilt packages in the web) and ttyrpld.

Though the most watch-like of them seem to be snoop, other also may be worth trying. I have yet tried nothing of them, so comments are appreciated.

And before the end I would mention well-known screen.