Sunday, January 31, 2010

PHP Helps

One of my PHP page was giving me hassles, and in order to make my life easy, I use a PHP formatter such as PHP Formatter to format my code for me, and insert comments at the end of each block to help me to see if there are any miss match braces. It works pretty well.

Other toolds I found which can help web coding easier are:
More can be found at http://www.smashingmagazine.com/2007/07/12/time-savers-code-beautifier-and-formatter/

Sunday, January 17, 2010

Aiptek Tablet and Chinese handwriting recognition

There are many way to type Chinese, however, arguably, the most natural way is handwriting. Windows has handwriting recognition software bundled for a long time now ... and today, I finally find a way to get something similar setup on Ubunut 9.10

Below are the items you need to make it happen.

The tablet I am using is a cheap rebranded tablet that using Aiptek Tablet. By default, Ubuntu can't really make full use of it. To make it useful, you need to

1) install aiptek driver by
sudo aptitude install xserver-xorg-input-aiptek
2) create a 10-linuxaiptek.fdi with the contents as below

<?xml version="1.0" encoding="UTF-8"?>
<deviceinfo version="0.2">
<device>
<match key="info.product" contains="Aiptek">
<merge key="input.x11_driver" type="string">aiptek</merge>
<merge key="input.x11_options.SendCoreEvents" type="string">true</merge>
<merge key="input.x11_options.USB" type="string">On</merge>
<merge key="input.x11_options.Type" type="string">stylus</merge>
<merge key="input.x11_options.Mode" type="string">absolute</merge>
<merge key="input.x11_options.zMin" type="string">0</merge>
<merge key="input.x11_options.zMax" type="string">511</merge>
<merge key="input.x11_options.KeepShape" type="string">On</merge>
</match>
</device>

(ref: https://help.ubuntu.com/community/AiptekTablet)
3) place the 10-linuxaiptek.fdi into the directory /usr/share/hal/fdi/policy/20thirdparty/
4) restart the system, and plug in the tablet.

Once you have things setup, you should be able to run the HanziInput.jar, and use the tablet for writing as below.




Add custom checks to Spry

I was working on a web application, and it used Spry for performing client side checking. One of the problem I had was to made the rule for checking one of the textfield input changing dynamically depending on a radio buttons selected by a user previously. Though spry is reasonable good for most part, it seems to be very static.

To get around this problem, here were the steps I used:


1) Change the submit button to plain button, and assign your own form validation function to the onclick event, i.e.
<input type="button" value="submit" onclick="checkform()">
2) Within the checkform() function, do something like the below:

function customValidationFunction(){
// put your own validation functions here.
return true;
}




function checkform(){
var f = document.getElementById("yourformname");
if(
customValidationFunction() && Spry.Widget.Form.validate(f){
f.submit();
}

}


The above would make the form validation process to include your own steps. Now, let's explain how spry shows its errors. Below is a sniplet of html code used by Spry. Spry enable and disable showing off an error message by the adding and removing the class "<error type>Stated" from an element, i.e. <span id="sprytextfield1" class="textfieldRequiredState"> to enable the error message; <span id="sprytextfield1" class=""> to disable the error message

/*Text Field styling classes*/
.textfieldRequiredMsg,
.textfieldInvalidFormatMsg,
.textfieldMinValueMsg,
.textfieldMaxValueMsg,
.textfieldMinCharsMsg,
.textfieldMaxCharsMsg,
.textfieldValidMsg {
display: none;
}
.textfieldRequiredState .textfieldRequiredMsg,
.textfieldInvalidFormatState .textfieldInvalidFormatMsg,
.textfieldMinValueState .textfieldMinValueMsg,
.textfieldMaxValueState .textfieldMaxValueMsg,
.textfieldMinCharsState .textfieldMinCharsMsg,
.textfieldMaxCharsState .textfieldMaxCharsMsg {
display: inline;
color: #CC3333;
border: 1px solid #CC3333;
}
.textfieldValidState input, input.textfieldValidState {
background-color: #B8F5B1;
}
input.textfieldRequiredState, .textfieldRequiredState input,
input.textfieldInvalidFormatState, .textfieldInvalidFormatState input,
input.textfieldMinValueState, .textfieldMinValueState input,
input.textfieldMaxValueState, .textfieldMaxValueState input,
input.textfieldMinCharsState, .textfieldMinCharsState input,
input.textfieldMaxCharsState, .textfieldMaxCharsState input {
background-color: #FF9F9F;
}
.textfieldFocusState input, input.textfieldFocusState {
background-color: #FFFFCC;
}
.textfieldFlashText input, input.textfieldFlashText {
color: red !important;
}
.textfieldHintState input, input.textfieldHintState {
}

<span id="sprytextfield1">
<input type="text" name="mytextfield" id="mytextfield" />
<!--Display an error message>.
<span class="textfieldRequiredMsg">Please enter a description</span>
</span>
Therefore, if you want to include your own error message, you can do something similar. Let us be a bit lazy, and use one of the unused predefined state for our custom purpose. Let's say, we, want to ensure the first letter of the input is a capital letter. To do so, I decided to use the textfieldMaxCharsState for displaying our error message.

<span id="sprytextfield1">
<input type="text" name="mytextfield" id="mytextfield" />
<!--Display an error message>.
<span class="textfieldRequiredMsg">Please enter a description</span>
<span class="textfieldMaxCharsMsg">Your custom error message here.</span>
</span>

Remember earlier, I mentioned that Spry enables and disables the error message by adding and removing the class name from a specific span. We need to do the same in our custom validation function .


function customValidationFunction(){
var elmt = document.getElementById("sprytextfield1");
if(validation is true){
// This would triggers the . textfieldMaxCharsMsg CSS directive alone, and
// hence hides the error message
elmt.removeAttribute("class");
return true;
}else{
// This would triggers the . textfieldMaxCharsState . textfieldMaxCharsMsg CSS directive, and // hence shows the error message
elmt.setAttribute("class", "textfieldMaxCharsState";
return false;
}
}

I hope this tutorial make sense to you. If you found any errors both syntactic or grammatical error, please leave me a comment, so I can correct them.

Tuesday, January 5, 2010

zd1211rw gets stuck in 1Mbps

I have this dongle which is using zd1211rw chipset for my wifi connection. However, it seems stuck on 1Mbps with its default set-up. After a search on Google, it seems there is a problem with the current driver implementation, and it won't automatically adjust its transfer rate as it should. A forum suggests the command below to force the speed to 54Mbps.

sudo iwconfig wlan0 rate 54M
You may want to give the above command a try, and see if it helps your speed.

Sunday, January 3, 2010

Lock-up and ACPI

I have recently reformatted my old, but perfectly working IBM R40e laptop from Windows XP to Ubuntu. Everything works better than before but it has this annoying lock-up every now and then. One suggestion is to disable the power management (ACPI) by putting the extra setting to the

GRUB_CMDLINE_LINUX_DEFAULT="quiet splash acpi=force pci=acpi"
in the /etc/default/grub file (I am using grub2). Show far, it seems doing the trick. If you are in similar situation, you may want to give this trick a try.

Friday, January 1, 2010

Redirect output and errors to null device

We often write scripts to automate tasks for us, however sometimes we don't want the output from individual programs to clutter the console output. In order to stop programs to output their console output, you can redirect them to the null device as below:

./example.pl > /dev/null 2>&1

This (1) redirects the output of example.pl to the null device and the (2) 2>&1 ensures that the output of the example.pl sends to the standard error (file descriptor 2), to wherever standard output (file descriptor 1) is going, which is, as already established, to the bitbucket.



JDownloader

File site such as Megaupload, hotfile etc are great to share big files but is a pain to download for free account users as they impose certain restrictions and limitations to download operations.

Today, I found this little Java utility that can ease the pain. The program is called JDownloader, which is a download manager which automates the process for you. All you need is provide a link to it, and it will take care the rest. It can even monitor your clipboard for links your copied. Since it is written in Java, you can run it on any platforms.