Sunday, 26 April 2009

Useful Bash Tips

TuxRadar has a very useful list of Bash keyboard shortcuts at http://www.tuxradar.com/content/bash-tips-power-users. Scroll to the bottom to see the list.

Sunday, 1 March 2009

Convert videos for Sony Walkman

I own a Sony Walkman NWZ-S639F. It's a great mp3 player possibly much better than the ipod itself. Best of all it integrates well with Linux without needing any extra libraries or software. Rhythmbox can be used for syncing music files from my computer to the Walkman and Nautilus can be used to simply drag and drop pictures or videos.

When copying videos however, the Walkman is only capable of playing mp4 files of size 320x240. It also needs a jpg file of the same size and name as the mp4 file to display a nice thumbnail in the player interface. ffmpeg can be used to achieve all this very simply.

To convert a flv video downloaded from the internet to Walkman compatible video:
ffmpeg -i video.flv -s 320x240 -ac 2 video.mp4
To generate the thumbnail:
ffmpeg -i video.flv -s 320x240 -an -ss 00:01:00 -r 1 -vframes 1 video.jpg

All that is left now is to copy the generated mp4 file and the jpg file to the video folder in your Walkman.

Monday, 9 February 2009

Office 2007 On Linux

I recently managed to get Office 2007 working on Fedora 10 with Wine 1.1.14. I was helped along the way by this article at the excellent wine-reviews blog.

1. Download the latest Wine source tarball from http://www.winehq.org
2. Extract the source. tar xvf wine-1.1.14.tar.bz2
3. Run the installer. This may take a long time. On my Dual Core 1.8GHz laptop with 2GB of RAM, the compilation took about an hour. cd wine-1.1.14
./tools/wineinstall

4. Download winetricks. wget http://www.kegel.com/wine/winetricks
5. Install the MSXML parser, .NET Framework 2, GDI+, RichEdit controls and Visual C++ 2005 Runtime. sh winetricks msxml3 dotnet20 gdiplus riched20 riched30 vcrun2005sp1
6. Run the Office 2007 installer. wine setup.exe

Office 2007 should now be installed in your machine.




Sunday, 25 January 2009

Enable Guest USB Access on VirtualBox

Although I have been using Linux exclusively for a long time now, there are still occasions where some pesky applications demand a Windows environment. For those rare moments, I have Windows XP installed as a guest OS on VirtualBox. VirtualBox is an absolutely wonderful virtualization tool that provides almost every function I need from a Windows box.

Recently however, I had to plug in a USB device to the Guest OS and I found out that the USB device menu is grayed out, preventing me from switching the device over from the host to the guest. I tried messing with the settings but none seemed to have an effect. A quick search of the internet revealed this article which worked like a charm!

First create a new group and add yourself to it
sudo groupadd usb
sudo usermod -G usb `whoami`

Find out the gid of the new group by running the following command
MYGID=`cat /etc/group | grep usb | awk -F ":" '{print $3}'`
Create a new entry in fstab
sudo echo "none /sys/bus/usb/drivers usbfs devgid=$MYGID,devmode=664 0 0" >> /etc/fstab
Restart the computer
sudo reboot

Now when you start VirtualBox, the USB devices can be freely assigned to the guest OS.

Monday, 19 January 2009

Mobile Phone Management With Wammu

I recently needed to backup the contents of my Sony Ericsson K750i. Since the utilities provided with the phone only work on Windows, I had to hunt for a similar application for Linux.

Wammu is a GUI application that can be used to manage a vast number of mobile phones. It uses the Gammu utilities in the back-end and provides a nice GUI front-end to control every aspect of the mobile phone such as the Address book, Calendar, Task list, Call lists, Messages etc. A large number of mobile phones are supported by this tool, a comprehensive list can be found here. Phones can be connected either via USB, IrDA or Bluetooth.


The setup was straightforward.
sudo yum install wammu pybluez

The pybluez package is only required if you want to connect to the phone via bluetooth.

The GUI is quite easy to navigate through. Therefore I am not going to go in to specifics. A screenshot is more than enough to see how intuitive the interface is.

Thursday, 1 January 2009

Fixing Bluetooth and PackageKit problems in Fedora 10

I began to suspect that something was wrong with the D-BUS permissions on my Fedora 10 system when I started getting the following error at the end of a Yum session:
ERROR:dbus.proxies:Introspect error on :1.46:/org/freedesktop/PackageKit: dbus.exceptions.DBusException: org.freedesktop.DBus.Error.AccessDenied: A security policy in place prevents this sender from sending this message to this recipient, see message bus configuration file (rejected message had interface "org.freedesktop.DBus.Introspectable" member "Introspect" error name "(unset)" destination ":1.46")

This seemed to have affected the Bluetooth daemon as well. No matter how I tried, I could not get bluetooth-properties to remember my preferences. My computer was not visible to other Bluetooth enabled devices and I could not browse my phone via Bluetooth. (It used to work flawlessly in Fedora 9). I diagnosed that the problem is with D-Bus by running the following command:
sudo bluetoothd -n -d
bluetoothd always terminated with the error:
Unable to get on D-BUS

I tried mucking around with D-Bus policy settings in /etc/dbus-1/system.d/ but had no luck until I came across this helpful post at FedoraForums.

After editing /etc/dbus-1/system.conf as mentioned, I restarted the computer and voila ! Bluetooth started working and yum stopped complaining !!

For completeness, this is the interesting bit from my new /etc/dbus-1/system.conf
<!-- allow sending valid replies -->
<!-- commented out part
<allow send_requested_reply="true" send_type="method_return"/>
<allow send_requested_reply="true" send_type="error"/>
-->
<!-- Newly added line to fix permission problems -->
<allow send_requested_reply="true" />


Kudos to all the helpful people at Fedora Forums !

Wednesday, 31 December 2008

Dumping Emesene Chat Logs to HTML

Emesene is a free GTK based client for the Microsoft Live Messenger network. If you do not need the multi-protocol support of Pidgin, Emesene is a nice tool to have. It is very customizable and emulates WLM pretty closely.
One drawback of Emesene is the way it stores chat logs. First, a plugin named "Logger" needs to be enabled for logging to work. The Logger plugin stores the chats in an SQLite database which makes it very difficult to obtain the chat logs back. As far as I could understand, there are two ways to view the chat logs:

1. Enable the Eval plugin and execute the following command in a chat window
/eval out(controller.pluginManager.getPlugin("Logger").get_last_message("name@hotmail.com", 15))
This effectively shows the last 15 messages received from the user with the email address "name@hotmail.com". Unfortunately, the displayed messages are not formatted nicely and it is very difficult to follow the conversation this way. (For interested Python hackers, more info can be found here.)

2. Browse the SQLite database manually.
If your WLM account is me@hotmail.com, then the chat logs will be stored in "~/.config/emesene1.0/me_hotmail_com/cache/me@hotmail.com.db". You can browse this file by using a tool like sqliteman. Simply run
sudo yum install sqliteman to install it from the repositories. Then you can invoke it from the command line by simply typing "sqliteman". Unfortunately since the tables are normalized, it is very difficult to follow a conversation this way. You can write a SQL query to do a join between the tables so that the conversation is more readable, but this is simply unacceptable for normal users who may not be familiar with SQL.

Since none of the above methods were satisfactory for exporting an Emesene chat log; I wrote a simple Perl script to read the database and dump the conversations in HTML format. The script takes three arguments.
1. Full path to the Emesene SQLite database file. (eg. ~/.config/emesene1.0/me_hotmail_com/cache/me@hotmail.com.db)
2. Email address of the other participant. Partial strings are accepted. (eg. If you want to dump all the conversations with some_person@hotmail.com, you can simply type some_person)
3. Path to output the HTML file. (Eg. ~/tmp/my_chat_log.html)

So for example, to dump all conversations between you and some_person@hotmail.com, the script should be invoked as follows:
./emlog.pl ~/.config/emesene1.0/me_hotmail_com/cache/me\@hotmail.com.db some_person my_chats_wth_some_person.html

To access the SQLite database, this script uses DBD::SQLite module from CPAN. You may need to install it first using the command:
sudo cpan -i DBD::SQLite
The script can be downloaded from here. It is distributed under the Apache license, so feel free to modify it to your own needs.

Source Code:


#!/usr/bin/perl
################################################################################
# Copyright 2008 Charith K. Ellawala
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
###############################################################################

use DBI qw(:sql_types);


# check command line arguments
if($#ARGV != 2)
{
die("Usage: emlog.pl <path_to_db_file> <participant_email> <output_file>\n");

}

# assign args to meaningful names
my($dbfile,$email,$outfile) = @ARGV;

# open a connection to the db

my $dbh = DBI->connect("dbi:SQLite:dbname=$dbfile","","", {RaiseError => 1}) or die $DBI::errstr;


# prepare the SQL and execute it
my $sth = $dbh->prepare("SELECT DISTINCT c.id, datetime(c.started,'unixepoch') FROM conversation c, conversation_event ce WHERE c.id = ce.id_conversation AND ce.id_user = (SELECT id FROM user WHERE account LIKE ?) ORDER BY c.started");
$sth->bind_param(1,"\%$email\%",SQL_VARCHAR);

$sth->execute();

# open the file for output
open(FH,">$outfile") or die "Unable to open $outfile for writing";


# HTML header. Change values here to customize the look
my $html = <<END;
<html>
<head>
<title>Emesene Chat Log</title>

<style>
body
{
font-family:Sans;
font-size:12px;
}

.conv0
{
background-color:#E0E0E0;
border:1px solid black;
padding:5px;
}


.conv1
{
background-color:#FFFFFF;
border:1px solid black;
padding:5px;
}

.user0
{
color:blue;
padding:3px;
display:block;
border-bottom:1px dashed gray;
}


.user1
{
color:red;
padding:3px;
display:block;
border-bottom:1px dashed gray;
}
</style>
</head>
<body>

END
print FH $html;

# iterate through the conversations
my ($cid, $uid);
$cid = $uid = 0;


while(($id,$time) = $sth->fetchrow_array)
{
$uid = 0;
print FH "<div class=\"conv$cid\">";
print FH "<h3>Conversation time: $time</h3><br/>\n";

# get the content of the conversation
my $sth2 = $dbh->prepare("SELECT u.account, ce.data, datetime(e.stamp,'unixepoch') FROM conversation_event ce, user u, event e WHERE ce.id_user = u.id AND ce.id_event = e.id AND ce.id_conversation = $id");
$sth2->execute();

my $prevacc = '';
while(($acc,$txt,$ts) = $sth2->fetchrow_array)

{
# bit of modulo logic to colourize the different participants
if($acc ne $prevacc)
{

$prevacc = $acc;
$uid = ($uid + 1) % 2;
}


# The first two lines of the chat are garbage. Get rid of them here
my @tmp = split(/\n/,$txt);
splice(@tmp,0,2);
$txt = join('<br/>',@tmp);
print FH "<span class=\"user$uid\"><i style=\"color:black;\">[$ts] $acc :</i><br/>&nbsp;&nbsp;&nbsp;&nbsp;$txt</span><br/>\n";
}

print FH "</div><br/>";
$sth2->finish();
$cid = ($cid + 1) % 2;

}

print FH "</body>\n</html>";
close(FH);

# we are done
$sth->finish();

$dbh->disconnect();







syntax highlighted by Code2HTML, v. 0.9.1