Wednesday, October 08, 2008

Basic .vimrc file



My first Vim configuration files: .vimrc, plugins, color schemes, etc were very small and modest. It's been eight years since I started using Vim and these configuration files have morphed, evolved, grown and recently shrink in too many ways, more than I can remember, and every time I check www.vim.org I find new ways I would like my Vim to evolve even further.

Here is the simplest version of my .vimrc file with no ctags or desktop integration as these topics require a separate post to explain. Most of the options here can be understood directly from Vim help files and by the comments in the file itself.

If you would like to use this file make sure to copy it in your home directory and read the comments as to get full functionality.

For comparison see the default install and my older configuration screenshot at the end of the post and compare it with my current configuration screenshot at the top of this post.



"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

"" General Vim Settings

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

"   In Kubuntu/Ubuntu make sure you have a complete vim install

"   i.e.  sudo aptitude install vim-ruby vim-scripts vim-common

"



" Disable compatibility with vi

set nocompatible



" Use the Unix file format

set fileformats=unix,dos



" Display line number in front of each line in the left margin.

set number



" I keep my own backups and all those files ending in '~' are

" anoying.

set nobackup



" Display commands in the bottom right corner as they are typed.

set showcmd



" Smoother redraws

set ttyfast



" Avoid vim complains about not written file when jumping

" between buffers using ctags.

set autowrite



" Vim Tip #1160 - Auto Save files when focus is lost.

au FocusLost * :wa



" Vim Tip #1279 - Highlight current line in Insert Mode

autocmd InsertLeave * se nocul

autocmd InsertEnter * se cul



" Enable syntax highlighting with 256 color support so we

" can use nicer color schemes. For more information and to

" get some nice color schemes check:

"   http://www.frexx.de/xterm-256-notes/

" Tested with Konsole-2.1 and yakuake 2.9.4

set t_Co=256

syntax on



" Set this background depending on your console and color scheme.

set background=light

"set background=dark



" My favorite color schemes. You must download the color themes

" and put them inside the color folder of your vim installation.

"colors default     " Good fro transparent consoles

"colors zenburn     " Vim Tip #415

colors 256_asu1dark " http://www.frexx.de/xterm-256-notes/

"colors pyte        " Vim Tip #1492



" If in diff mode (vimdiff) use the inkpot color scheme

" that better highlights file differences

if &diff

  colors inkpot    " Vim Tip #1143

endif



" Additional filetypes

au BufRead,BufNewFile sconstruct setlocal filetype=python

au BufRead,BufNewFile *.rl       setlocal filetype=ragel

au BufRead,BufNewFile *.rb       setlocal filetype=ruby



" Additional Syntax Files. The ragel.vim file can be downloaded

" from Ragel's home page http://www.complang.org/ragel/.

au! Syntax ragel source ragel.vim



" Set path to search (i.e. gf) files recursively

set path=/usr/include,/usr/local/include,**;$HOME



" Vim Tip #1274 - Highlight trailing whitespace characters

set list

set listchars=tab:->,trail:·



" Number of spaces used for (auto)indenting

"set shiftwidth=2



" Number of spaces to insert for a tab

"set softtabstop=2

set tabstop=2



" Insert spaces when the tab key is pressed

" If you want a real tab use "ctrl-v, tab" in insert mode. This

" is usefull for Makefiles that require real tabs to work.

set expandtab



" Word wrap with line breaks if they are longer than 80 characters

" long. If you prefer to wrap lines visually only use the wrap and

" lbr commands below instead. If you have a document that has lines

" longer than 80 characters you can use "gq}" to format it as a

" paragraph. This is more easier format for navigating the text.

set textwidth=80

set formatoptions=tcq



" Vim Tip #989: Word wrap without line breaks

"set wrap

"set lbr



" Enable specific indenting

set autoindent

set smartindent



" Enable code folding

set foldmethod=syntax



" Show briefly matching bracket when closing it.

set showmatch



" Show cursor position

set ruler



" Additonal key mappings

map <F12> <ESC>ggVGg?                             " ROT13 fun



"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

"" Mini buffer explorer features

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

let g:miniBufExplSplitBelow=0

let g:miniBufExplSplitToEdge = 0

let g:miniBufExplVSplit = 20

let g:miniBufExplMapCTabSwitchBufs = 1

let g:miniBufExplUseSingleClick = 1

let g:miniBufExplorerMoreThanOne=0 

let g:miniBufExplModSelTarget = 1



" Map Tab and Shift-Tab for buffer navigation

" Note that S-TAB does not work in certain consoles (i.e. KDE Konsole)

map <TAB> <ESC><C-TAB>

map <S-TAB> <ESC><C-S-TAB>



"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

"" Improve VIM autocomplete features

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

" Enable search as you type:

set incsearch



"improve autocomplete menu color

highlight Pmenu ctermbg=238 gui=bold



" http://www.cuberick.com/2008/10/ruby-autocomplete-in-vim.html

autocmd FileType ruby,eruby set omnifunc=rubycomplete#Complete

autocmd FileType ruby,eruby let g:rubycomplete_buffer_loading = 1

autocmd FileType ruby,eruby let g:rubycomplete_rails = 1

autocmd FileType ruby,eruby let g:rubycomplete_classes_in_global = 1



""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

" Vim tip #90 Enable VCS integration in vim

""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

"  Get vcscommand.zip from http://www.vim.org/scripts/script.php?script_id=90

"  Uncompress to your vim directory:  unzip -d ~/.vim vcscommand.zip

"

"  Now when editing a file that is under revision control (SVN, CVS, Git,...)

"  we can access the versioning commands:

"

"  VCSAdd

"  VCSAnnotate

"  VCSCommit

"  VCSDelete

"  VCSDiff

"  VCSGotoOriginal

"  VCSGotoOriginal!

"  VCSInfo

"  VCSLog

"  VCSLock

"  VCSReview

"  VCSStatus

"  VCSUpdate

"  VCSUnlock

"  VCSVimDiff



" Map F4 to VimDiff to check differences

map <F4> <ESC>:VCSVimDiff<CR>



""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

" Latex Configuration

" http://vim-latex.sourceforge.net/documentation/latex-suite/recommended-settings.html

""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

au BufRead,BufNewFile *.cls      setlocal filetype=tex

"let g:tex_flavor='latex'   " loads latexsuite with empty tex files.

set iskeyword+=:           " type /ref{fig: and press <C-n> to autocomplete references



""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

" Working with Makefiles

"

" Press F5 to compile and open the error window if there

" are errors. If there are errors you can use :cn and :cN to

" jump foward and backward thru the error list. Press :ccl to

" close the error list or F5 again to recompile

map <F5> <ESC>:make<CR><ESC>:botright cwindow<CR> " Compile and open quick fix list

map <F6> <ESC>:cN<CR>                             " Jump to prev error/warn

map <F7> <ESC>:cn<CR>                             " Jump to next error/warn



""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

" Enable spell checking in latex, bib and txt files

" Commands:

"            [s      ->  jump to next bad word

"            ]s      ->  jump to prev bad word

"            z=      ->  suggest word

"            zg      ->  mark word as good (add to dictionary)

"            zw      ->  mark word as bad  (remove from dictionary)

"            :h spell -> get more details about spelling

""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

au BufRead,BufNewFile *.txt,*.tex,*.bib  setlocal spell spelllang=en_us



"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

" Other Good Vim tips not used here

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

" Vim Tip #64   - Set working dir to the file you are editing

"                 Why not used: Makes difficult the management of sessions and

"                   makefiles.

" Vim Tip #305  - List of editing vim tips

"                 Why not used: I can remember all the commands I use.

" Vim Tip #1203 - mapping to set up vtreeexplorer and taglist in left window

"                 Why not used: I use MiniBufExpl instead of vtreeexplorer

" Vim Tip #1318 - An attempt to emulate TextMate's snippet expansion

"                 Why not used: I am unable to get this working correctly.

" Vim Tip #???? - Force gf to open in new tabs  

"                 Command: nnoremap gf <C-W>gf

"                 Why not used: I use MiniBufExpl so no more tabs for me.

" Vim Tip #565  - Never see ^M and pesky trailing spaces again

"                 Command: autocmd BufRead * silent! %s/[\r \t]\+$//

"                 Why not used: Not a good idea when working with others source

"                  code. All the automatically deleted chars will be seen as

"                  changes in the diff files making it difficult to review the

"                  changes. Never use this if you plan to submit patches to ffmpeg

"                  or the Linux kernel or they will spam filter you forever!




Thursday, September 25, 2008

Canon Satera LBP5910 Linux Driver 2.20

If you have a Canon printer based on the LIPSLX driver (i.e. LBP family) and you use Ubuntu 10.10 or 11.04 then you are lucky because Canon provides the linux drivers for these printers. Simply go to this page Canon Linux Driver check for the supported printers and the respective drivers. For my case I have the LBP5910F printer that is supported by the LIPS LX Printer Driver so I enter this page and download the two corresponding .deb packages for Ubuntu/Debian. If you use Redhat or any other distribution that uses RPMs then download the corresponding RPMs.

Install Canon LBP5910F Driver on Ubuntu 10.10 and 11.04 32bit

Now enter CUPS configuration page (i.e. http://localhost:631) and follow the steps. In my case the printer was already listed in the select box of printers with IP address and everithing. In Make/Manufacturer select Canon and finally in Model/Driver select the corresponding driver: And that is all now you can try to print a test page.

Install Canon LBP5910F Driver on Ubuntu 10.10 and 11.04 64bit

For some reason there are no deb packages for 64bit architecture but fortunately is easy to convert the 64bit rpms to deb packages using alien. First install the alien package: then using this tool convert the rpm packages to deb packages: this will generate two 64bit deb packages that work perfectly with Ubuntu/Kubuntu and can be installed as the 32 bit versions above: after installing the packages you can proceed with the installation the same way as with the 32bit packages.

Wednesday, June 25, 2008

Use PuTTY generated keys with SftpDrive

So you created a public/private key pair using PuTTYgen and now you want to use these keys in other applications like OpenSSH and/or SftpDrive but you realise that the private key generated by PuTTYgen is not compatible with these programs??

No worries... all you have to do is convert your PuTTYgen key (.ppk) to and OpenSSH compatible key and it happens to be that PuTTYgen can do this conversion.



Open PuTTYgen and load your private key using the "load" button and opening your private key .ppk file. Once loaded select from the top menu "Conversions -> Export OpenSSH key" and select a file name to save the exported key. Now this saved key can be used in other applications that are OpenSSH compatible.



To use this key with SftpDrive open SftpDrive Manager and on "New Driver" change the Authentication method (see image above) to "Use a public key to log in...". This will open a dialog box (see image below).



Here you can generate a new public/private key pair but we want to use the one we generated with PuTTYgen so select "Import Existing Key Pair" that will open yet another dialog box (see image below):



Here simply browse the location of the private and the corresponding public keys and press "OK". Now SftpDrive will use these keys to connect to your SSH server.

If you get errors make sure you have selected the exported OpenSSH compatible key and not the PuTTYgen generated one (i.e. .ppk). Also if you saved the public key using the PuTTYgen "save public key" button then this key has a format SftpDrive does not understands so it will be unable to read the key. To solve this copy and paste the public key as it appears in PuTTYgen text box (i.e. the large string starting with ssh-rsa or ssh-dsa) in a text file (i.e. mykey.pub) and then import that file as the public key. This format is OpenSSH compatible so should work with no problems.

Tuesday, June 24, 2008

Subversion + OpenSSH Server + PuTTY Client Setup

This setup is of a subversion server running on Linux (Kubuntu 8.04) and subversion clients (Tortoise SVN) running in Windows (WinXP).

Pre-requisites

I assume you already have a subversion repository working and now you want to access it using ssh tunnelling for security reasons. Also the server must have remote ssh access and be configured to do Private Key authentication.

Setting up Putty

The best SSH client and related tools for Windows are the PuTTY family of programs that can be found at http://www.chiark.greenend.org.uk/~sgtatham/putty/. You can download the individual ".exe" files an put them in your path but I recommend you to use the installer that will install all the tools and set them nicely in your start menu.

Create public/private key pair


Objective

To access the subversion server via ssh you need a public/private key pair. To generate these key pair you can use the PuTTY Key Generator (PuTTYgen.exe) that was installed along with PuTTY installer.

Simply execute the PuTTYgen.exe program and press the "generate" button. After that keep moving your mouse, to generate some randomness, until the progress bar reaches the end. At this point you will get your public and private keys generated.

Make sure you add a "Key passphrase" that will be like a password to access the keys. The comment part is not necessary but is customary to put your email address.

Also make sure you are generating a SSH-2 key pair as SSH-1 is not secure. Choosing between RSA or DSA keys is a matter of application. It is known that DSA is fast at key generation and signing while RSA is faster at verification. So if your application requires a lot of signing (i.e. SSL Web Application) then DSA is good but if your application requires a lot of verifying (i.e. Subversion) then RSA is best. Since we are going to use this key for Subversion I recommend subversion but you are free to use the one you prefer.

Now we need to save your generated keys somewhere in your hard disk. The public key is visible in the PuTTYgen application widget (i.e. the long sequence of letters in the top text box). This key starts usually with "ssh-rsa" or "ssh-dsa" depending on what type of key you generated (RSA or DSA).

To save your public key on disk do not use the "save public key" button as this will generate a key in a format that is not compatible with OpenSSH. Simply copy and paste the key from the text box to a file on disk. You can save this file with whatever name you like but I recommend putting a ".pub" extension just as OpenSSH does.

The private key you must save it using the "save private key" button as this generates a file with ".ppk" extension that is understood only by PuTTY programs. Make sure you put the same name as the public key but with the ".ppk" extension instead of ".pub".

As an additional step if you would like to use your private key with other applications like SftpDrive so these can also connect to the OpenSSH server without need for a password, then you must convert your private key (.ppk file) to the OpenSSH key format. To do this simply select "Conversions" in the PuTTYgen.exe menu and then "Export OpenSSH key" to generate a key that can be used with OpenSSH compatible applications.

Note that with PuTTYgen.exe you can convert your private keys between PuTTY (.ppk) format and OpenSSH format so if you generate your public/private keys using OpenSSH ssh-keygen program you can use them in PuTTY by importing the private key using PuTTYgen.

Setting your public keys

The way the public/private key pair are used is simple. You keep the private key in your local machine usually in a key manager like ssh-agent or pageant and send your public key to all the servers you want to access via ssh.

To set up your public key in the SSH server (assumed OpenSSH server) simply log to the server shell using your account username and password (if you don't have one ask you administrator or if you are the administrator create one yourself). Then copy the contents of your public key file inside the file "~/.ssh/authorized_keys2".

To do this edit the "~/.ssh/authorized_keys2" file with any text editor (Vi, Nano, Kwrite, gEdit) and make sure it looks like in the next figure:



The key must start with ssh-rsa or ssh-dsa depending on the key you generated followed by the key all in one line (i.e. no new lines and spaces) and finally the comment again all in one line.

Finally make sure the authorized_keys2 file has correct permissions by executing "chmod 600 authorized_keys2".

You can set the public key in as many SSH servers as you like so you can access them all using the same private key.

Setting your private key

Your private key must be your most guarded secret and shall not be passed to anyone. Only you should have access to this key because anyone who gets his/her hands on that private key can access all the servers where you installed your public key and we don't want anyone to have that access power do we?

To use a your private key we usually use a SSH authentication agent like ssh-agent in Linux/Unix or pageant in Windows. The PuTTY installer comes with pageant.exe that is the one I use and recommend.

Simply execute pageant.exe from the application menu and a small icon will appear in your system tray. Right click the icon and select "Add key" on the context menu, then browse the directory where you saved your private key (i.e. .ppk) and select it. And that is it....

Testing SSH access

To test open the PuTTY.exe SSH client and create a new session to connect to your SSH server (make sure you save the session). When you open the session it should log you in without asking for any username and passwords. If it asks you for username and password then you did something wrong. Make sure the publickey format is correct on the server and that the private key is set in pageant. Of course make sure the OpenSSH server is running on the remote machine and that pageant is running on the local machine.

Once you can connect to the SSH server without need for a username and password then we can proceed to set up TortoiseSVN to access your Subversion repositories via SSH tunnels.

Setting TortoiseSVN with SSH tunnelling

Once you have SSH set to work with public/private key authentication and have pageant configured configuring TortoiseSVN is a breeze. All you have to do is make sure you use the correct URL format to access the repository.

If you have TortoiseSVN installed try to make a checkout and in the URL put something like:

svn+ssh://username@hostname/dir/to/repo

And you should be able to ckeck out that project over an SSH tunnel. You don't even need to have svnserve running on the server as the svn+ssh scheme tells the TortoiseSVN to start it for you and tunnel it over SSH.

Of course replace username with the username you use to access the SSH server and hostname with the IP address or FQDN of your SSH server. The path to the repository must be absolute path starting from the root "/" directory and have correct access privileges (read/write) for the username you use.


Resources

http://neubia.com/archives/000191.html
http://www.unixwiz.net/techtips/putty-openssh.html

Saturday, June 14, 2008

Best gift ever??

Now I can reaffirm that I married the right person, just look what I got for fathers day!!!



I know I do not blog that often but be assured that now I will be blogging none at all for at least a few weeks, or months....

Thursday, April 17, 2008

Plotting SVN history??

Found a blog post by Samuel Jansen were he plots his subversion repository history so out of curiosity I plotted mine too...

actually nothing interesting, steady development almost linear and as you can see but it made me notice that I am almost near my 1000 mark!! when I reach it then I will have an excuse to celebrate and give a nice "1000 SVN passed mark present".

Sunday, April 13, 2008

Linux Cafe in Akihabara

Have been in Akihabara (Electric Town) several times but never saw this shop before:





If you read the details below the logo you will see this shop is been there since 2001!! and I never saw it... I must go check my eyes. Anyway if you are an Open Source person walking around Akihabara you may well pass a get yourself a nice cup of open source coffee.

Wednesday, April 09, 2008

Simple Mail Server (Kubuntu)

If you need a small personal mail server to send log reports and system alarms from your Kubuntu machines to a local and/or external mail address then you may use Exim4 that is the default mail server (MTA) that comes with (K)Ubuntu.

To install is very easy:


1 sudo aptitude install exim4 exim4-base



and to configure simply run:


1 sudo dpkg-reconfigure exim4-config



and in the first dialog (image below) select "internet site". For all next options I used the defaults that would allow local users and services to send emails locally and to remote addresses (i.e. gmail accounts).


Now to test the new server you can send a simple email to your Gmail account:


1 mail -s test mygmail@gmail.com

2 test mail

3 .

4 Cc:



The "-s" switch is the mail subject. After the command simply type whatever you want in the mail body. To finish the email press followed by a dot "." and then again. After this you will be prompted with "Cc:" so press and then check your Gmail account. You should have received an email with the message and subject you used above.

Now say you want to receive LogWatch reports or any other mail directed to root in your Gmail account. Then you can simply create an alias in the "/etc/aliases" file like:


1 # Added by installer for initial user

2 root:   mygmail@gmail.com



then rebuild the aliases database


1 sudo newaliases



If you require more complex mail configurations you may check my previous posts about Sendmail and Postfix setup in (K)Ubuntu here.

Friday, April 04, 2008

Get latest ffmpeg (svn) in FreeBSD-7.0

If you want to compile the latest ffmpeg from subversion with some external libraries support like x264, faac, Theora and Vorbis in the latest FreeBSD release (7.0) then follow this simple steps:

If you do not have the ports tree installed (i.e. /usr/ports is empty or non existent) then you can use portsnap to download and update it (these commands must be done as root):


1 pkg_add -r portsnap

2 mkdir /usr/ports

3 portsnap fetch

4 portsnap extract

5 portsnap update



Install some ports to enable more funcionality in ffmpeg (these commands also must be done as root):


 1 # faac support

 2 cd /usr/ports/audio/faac

 3 make install clean

 4

 5 # vorbis support

 6 cd /usr/ports/audio/vorbis-tools

 7 make install clean

 8

 9 # x264 support

10 cd /usr/ports/multimedia/x264

11 make install clean

12

13 # theora support

14 cd /usr/ports/multimedia/libtheora

15 make install clean

16

17 # ffplay support

18 cd /usr/ports/deve/sdl12

19 make install clean



Checkout ffmpeg from subversion, configure, compile and install (only the last command needs root priviledges);


 1 # checkout ffmpeg from subversion

 2 svn co svn://svn.mplayerhq.hu/ffmpeg/trunk ffmpeg

 3 cd ffmpeg

 4

 5 # export library and include paths

 6 # so configure can find the external

 7 # libraries (i.e. x264)

 8 export LIBRARY_PATH=/usr/local/lib

 9 export CPATH=/usr/local/include

10

11 # run configure with desired options

12 ./configure --enable-gpl --enable-pthreads \

13  --enable-shared --enable-swscale  \

14  --enable-libx264 --enable-libfaac \

15  --enable-libtheora --enable-libvorbis \

16  --enable-encoder=g726 --enable-encoder=ljpeg \

17  --enable-encoder=mjpeg

18

19 # compile using GNU Make (gmake), not BSD Make

20 gmake

21

22 # install

23 su root -c 'gmake install'



If you want to enable other external codec libraries supported by ffmpeg make sure to install their respective ports (if available) and then run the configure script with the desired libraries enabled. You can see a list of supported codec libraries with ".configure --help".

Note that this instructions were tested with revision 12684 of ffmpeg subverion but ffmpeg is a very active project and this instructions are not guarantee to work with the latest subversion revision.

Thursday, April 03, 2008

Japanese Input in KDE 4.0.3 (Anthy)

So you want to try the newest and greatest KDE 4.0.3 release but you can't because you need to be able to write other languages other than English like in my case Japanese, Chinese and/or Korean??

After googling I found here that there is a scim input method that allows input of these languages in KDE4 and also fixes some problems with proprietary software like Skype, Adobe Reader, etc.

This input method is called scim-bridge and can be easily installed in Kubuntu with the command:

sudo aptitude install scim-bridge-agent scim-bridge-client-qt4 scim-bridge-client-qt scim-bridge-client-gtk

Then to enable scim-bridge as your input method you can use im-switch:

im-switch -s scim-bridge

or if you do not have im-switch then you can do it manually with:

sudo ln -sf /etc/alternatives/xinput-all_ALL /etc/X11/xinit/xinput.d/scim-bridge


Seems that scim-bridge is now the default input method in Ubuntu/Kubuntu but not sure, see resources below to get more detailed explanation on how to setup SCIM in Ubuntu.

https://help.ubuntu.com/community/SCIM
https://help.ubuntu.com/community/SCIM/Kubuntu
https://wiki.ubuntu.com/InputMethods/SCIM/Setup

Monday, March 03, 2008

Developing Android in Netbeans

Anyone who reads this blog would know that I like Netbeans a lot for mobile development (see here, here, here and here) and when I found that Google's mobile platform (Android) does not have a Netbeans plugin I was very disappointed.

Of course Netbeans enthusiasts developed a plugin (Undroid) but it is not supported by Google, I could not get it to launch the emulator directly from the IDE and currently it does not work with the latest Android SDK so still we do not have a full solution for Android development in Netbeans.

Now if you don't mind working in a mixed setting using Android tools from command line and using Netbeans to code, compile and install your Android applications then here is how you can do it.

Prerequisites

I assume you have the latest Netbeans 6.0.1 already installed and the latest Android SDK m5-rc14 also installed in your computer. I have tested this configuration in my Linux (Kubuntu) machine but should also work in Windows and other Linux distributions.


Creating a new project

The first thing is to create a new project using the activityCreator.py script that comes with the Android SDK:

activityCreator.py --out HelloAndroid org.piaotech.android

this script will create the project skeleton and more important the build.xml file that can be used by Netbeans to build/install your Android applications.



Start Netbeans and create a new "Java Project with Existing Ant Script" as shown in the figure above. In the next screen browse the location of the project directory created by the activityCreator.py script (HelloAndroid above) and the rest of the fields will be filled automatically by Netbeans. You can change the project name if you wish but the default is ok.



In the next screen (figure above) select the Ant tasks that each of the Netbeans commands should execute. I simply put compile for Build Project and install for Run Project.



In the last screen make sure to add the src and res of the project to the Source Package Folders as shown above.



finally select Java Sources Classpath in categories and add the doc directory that is inside the Android SDK as shown in the picture above and press finish.

Running you project

Now you should have your new Android project in Netbeans project view and now you can code and build it as you would using any other Netbeans project. Now if you try to run the application you will get an error indicating that there is no device. This is because I still have not found a way to launch the emulator from within Netbeans so we must launch it manually. From the command line run the emulator:


android-sdk_m5-rc14_linux-x86/tools/emulator


now with the emulator running if you run your project from within Netbeans the Android application will be automatically installed in the emulator.



It takes some effort to setup an Android project in Netbeans and actually there is no benefit from doing this as any java editor with support for Ant scripts can do the same. In my case is simply a matter of preference as all my Java projects are in Netbeans I did not want to have a separate project format only for Android applications.

Note that there is no need to close the emulator each time you rebuild your application so leave it running during the time you are developing. I also recommend you set up a console running the adb logcat command so you can see all the debugging messages (Log.v()) that Android throws.

Saturday, February 23, 2008

Friday, February 22, 2008

Reduce GIF size using RMagick

If you are creating GIF images using RMagick and your application has very stringent size restrictions (i.e. mobile devices) then this post may help you get the most of RMagick.

While it is possible to control the compression of JPEG and PNG files using the quality parameter for GIF files we are left with almost no options. Fortunately a little understanding of the default, and only, compression scheme used in GIF can help you improve the compression performance of your images.

The LZW compression algorithm used in GIF images and developed by Abraham Lempel, Jakob Ziv and Terry Welch, contructs a color table for an image wherein each color value is matched to a pixel. Thus, images with large areas of one color will be compressed far better than those that those that do not have such color blocks.

So to get better compression (smaller images) we must ensure we have large contiguous blocks of the same color in our image. When creating images in ImageMagick we can use these tips to ensure small size results:

First: Make use simple graphics with flat colors (i.e. avoid using Magick::GradientFill).

Second: when using an RMagick::Draw object always make sure you call the stroke_antialias(false) method on it:



1 require "RMagick"

2

3 gc = Magick::Draw.new

4 gc.stroke_antialias(false)

5

6 # your drawing code below




Third: When writing text using the annotate method from the Magick::Draw or Magick::Image classes do not use text antialiasing:



1 require "RMagick"

2

3 gc = Magick::Draw.new

4

5 gc.annotate(img,...) {

6   self.text_antialias = false

7 }




A very simple method to reduce the image size is using the despeckle filter on the image. This will reduce the color dither making the color distribution more planar so we have larger portions of the image with the same color. By doing this the LZW algorithm can reduce the final image size up to a 20% of the original size depending on the image.



1 require "RMagick"
2     
3 small_image = big_image.despeckle
4 small_image.write("small_image.gif"){
5     self.compression = Magick::LZWCompression
6     self.dither = false
7 }




Actually calling self.compression is not needed as it is set by default by RMagick but is good just to make sure.

Resources
http://rmagick.rubyforge.org/
http://www.faqs.org/faqs/jpeg-faq/part1/section-9.html
http://rmagick.rubyforge.org/src_over.html
http://www.webdevelopersnotes.com/graphics/gifs_compression_algorithm.php3

Monday, February 18, 2008

Using ActiveRecord with Microsoft SQL Server (MSSQL)

There seems to be a lot of confusion on how to use ActiveRecord from Ruby on Rails to query a MSSQL (Microsoft SQL) Database. During my last two projects I had to research this topic and found that this confusion stems from the fact that the documentation is sparced all over the web and most of the time it is incomplete and misleading.

After some tests and real projects I found there are mainly two ways to get ActiveRecord working with a MSSQL Database server: (1) Using the ADO driver and (2) using an ODBC driver. How these methods are used differ if we are in a Windows client or in a Linux client so we get a total of four different ways to connect ActiveRecord to MSSQL, in fact I only got three as we will see later, but in theory there can be four if I get ADO to work in Linux.

The confusion most people new to this topic confront is that the documentation found in the web usually mention these methods separately and without much detail on the configuration. For example some pages in the web explain how to connect to MSSQL using the ADO driver but they don't mention that this works only if you are using a Windows client where win32ole can be installed. Some others talk about success using ODBC and present the configuration but do not explain how to create the DSN needed for connection because they assume this is common knowledge (it was not for me).

To avoid the pain I had to suffer when getting my work done to others, here I present my experiences in this topic in four subsections. Each one explains how to use the ADO or ODBC methods from a Windows or Linux client.

Windows Client using ADO driver

I have tested this configuration using a WinXP Home client with the ruby one click installer (184-20 and 186-26) connecting to a Windows Server 2003 running Microsoft SQL Enterprise 2000 SP3.

To start download an extract the ruby-dbi package (dbi-0.1.1.tar.gz) somewhere in your Windows machine.

The following instructions are to be executed from within a command shell (cmd.exe).



1 gem update --system

2 gem install activerecord-sqlserver-adapter --source=http://gems.rubyonrails.org

3 mkdir c:\ruby\lib\ruby\site_ruby\1.8\DBD\ADO

4 copy ruby-dbi\lib\dbd\ADO.rb c:\ruby\lib\ruby\site_ruby\1.8\DBD\ADO




These commands simply update the RubyGems system and install the Active Record gem with all dependencies needed to connect to MSSQL. We then copy the ADO driver from ruby-dbi inside our ruby install directory. If you have ruby installed in a place other than "c:\ruby" make the appropiate changes to the commands above.

Now in your code you can connect to the MSSQL database using the following:



1 ActiveRecord::Base.establish_connection(

2     :adapter => "sqlserver",

3     :mode => "ADO",

4     :username => "youruser",

5     :password => "yourpass",

6     :host => "serverhostname_or_ipaddress",

7     :database => "YourDbName"

8 )




I have tested this configuration with ActiveRecord version 1.14.4 and 2.0.2 but I assume this should work with all version in between too.

Windows Client using ODBC

I have only tested this with the latest ActiveRecord 2.0.2 so I don't know if this works with previous versions and using a WinXP Home client with the ruby one click installer (184-20 and 186-26) connecting to a Windows Server 2003 running Microsoft SQL Enterprise 2000 SP3

The following instructions are to be executed from within a command shell (cmd.exe).



1 gem update --system

2 gem install activerecord-sqlserver-adapter --source=http://gems.rubyonrails.org



As with the ADO driver these commands simply update the RubyGems system and install the ActiveRecord gem with all dependencies needed to connect to MSSQL.

The next step is where most documentation I have seen fails. They say you must specify a DSN to connect via ODBC but they do not mention what this DSN is or where to get it. I guess these gurus assume we must know this things or we would not be looking for this info but in my case it took me a while to figure out where this DSN thing is supposed to come from.

The DSN (Data Source Name) must be created and in Windows we do it by using a tool called "odbcad32.exe" or choosing "Data Sources (ODBC)" from the "Administrative Tools" inside Windows "Control Panel". For instructions on how to use this tool refer to the detailed explanation at www.truthsolutions.com.



When creating the new DSN make note of the name you set on it (see figure above) because this is the name you must use to connect to the MSSQL server when using ActiveRecord. Also make sure you select the "SQL Server" driver as instructed at www.truthsolutions.com. The authentication method depends on how you have your MSSQL server configured but I always use the using Login and Password entered by the user option so I don't know about the network login ID option. At the end make sure you test the DSN to make sure it is valid and is working.

Once the DSN is created you can connect to the MSSQL server using ActiveRecord like:



1 ActiveRecord::Base.establish_connection(

2     :adapter => "sqlserver",

3     :mode => "odbc",

4     :username => "youruser",

5     :password => "yourpass",

6     :dsn => "SQLServer"

7 )




and make sure to replace "SQLServer" with the DSN name you set up when creating it using the "odbcad32.exe" tool.

Linux Client using ODBC

The following instructions were tested in Kubuntu Gusty (7.10) using the latest ActiveRecord 2.0.2 as client connecting to a Windows Server 2003 running Microsoft SQL Enterprise 2000 SP3.

From console install the following packages:



1 sudo aptitude install ruby1.8 unixodbc tdsodbc libodbc-ruby1.8 rubygems




Next you can choose to install ActiveRecord using Ubuntu repositories or using RubyGems repositories. I chose to use RubyGems as this provides the latest versions and is updated more often.



1 sudo gem install activerecord-sqlserver-adapter --source=http://gems.rubyonrails.org




Now as in Windows we must create a DSN for unixODBC. The first step is to add the freeTDS driver to unixODBC and to to this we create a configuration file (simple text file) called for example "myodbcdriver.ini" that contains:



1 [FreeTDS]

2 Description     = TDS driver (Sybase/MS SQL)

3 Driver          = /usr/lib/odbc/libtdsodbc.so

4 Setup           = /usr/lib/odbc/libtdsS.so

5 CPTimeout       =

6 CPReuse         =




If you are not using Kubuntu/Ubuntu then make sure you have installed unixODBC and the freeTDS ODBC driver and change the "Driver" and "Setup" paths to reflect your system installation. In Kubuntu the "tdsodbc" package we installed above provides us with this configuration file in "/usr/share/tdsodbc/odbcinst.ini" so we do not need to create it.

With this configuration we can add the freeTDS driver to unixODBC by issuing the following command:


1 sudo odbcinst -i -d -f myodbcdriver.ini




or in Kubuntu that provides the configuration file



1 sudo odbcinst -i -d -f /usr/share/tdsodbc/odbcinst.ini




Next we must create a DSN configuration file, say mydsn.ini, that looks like:



1 [SQLServer]

2 Driver      = FreeTDS

3 Description = My First SQLServer Database

4 Trace       = No

5 Server      = hostname_or_ipaddress

6 Port        = 1433

7 Database    = SQLServerDB




As in Windows take note of the name you set to the DSN ([SQLServer]) as this is used for the connection. Finally we add the DSN to unixODBC with the following command:



1 odbcinst -i -s -f mydsn.ini -h




Note that we execute this command without the "sudo" because we are installing a User DSN that will be accessible only to the system user that installed the DSN. To install a system wide DSN we use a similar command but with the "-l" switch instead of the "-h".



1 odbcinst -i -s -f mydsn.ini -l




You can run the odbcinst command without any arguments to get a list of possible options. To test the newly created DSN we can use the isql command tool that comes with unixODBC:



1 isql SQLServer username password




replacing SQLServer with the name you used when creating the DSN and if everything is working you should get a message similar to:



1 +---------------------------------------+

2 | Connected!                            |

3 |                                       |

4 | sql-statement                         |

5 | help [tablename]                      |

6 | quit                                  |

7 |                                       |

8 +---------------------------------------+

9 SQL>                              




input "quit" to exit the SQL prompt. As in Windows, once the DSN is created you can connect to the MSSQL server using ActiveRecord using:



1 ActiveRecord::Base.establish_connection(

2     :adapter => "sqlserver",

3     :mode => "odbc",

4     :username => "youruser",

5     :password => "yourpass",

6     :dsn => "SQLServer"

7 )




again make sure to replace "SQLServer" with the DSN name you set up when creating it.



Some notes about the Linux to MSSQL connection:


  • There are GUI based tools similar to "odbcad32.exe" in Windows that allows you to manage DSN's and ODBC drivers more easily. In Kubuntu simply install the "unixodbc-bin" package and then execute "OBDCConfig". As you can se from the image above this tool is quite similar to the Windows counterpart.

  • The freeTDS driver is free and even thought it is rather stable I have found some small issues specially when working with non-ascii character sets like SJIS and ISO-2022-JP used in Japanese OSes. There is a commercial alternative driver produced by EasySoft in case you need commercial support and reliability.

  • There is another implementation of ODBC in Linux called iODBC but have never used it. It may be interesting to test the Windows ODBC, unixODBC and iODBC in performance.



Linux Client using ADO driver

So far I have been unable to make this configuration work. It seems that the ADO driver uses some components from the win32ole library that is only available in Windows machines. This means the only way to connect to a MSSQL server from a Linux client is using the unixODBC/freeTDS driver method described before.

If someone knows of other ways to make Linux communicate with a MSSQL database please let me know.

Resources

http://softiesonrails.com/2006/6/28/activerecord-with-sqlserver-without-rails
http://www.imarichardson.com/
http://www.themolehill.com/
http://www.freetds.org/
http://www.unixodbc.org/
http://www.unixodbc.org/doc/FreeTDS2.html
http://www.truthsolutions.com/sql/odbc/creating_a_new_odbc_dsn.htm
http://josiah.ritchietribe.net/blog/archive/2006/01/1176/

RubyGems Explicit Versioning

One of the most useful features of RubyGems as a package manager is the ability to force explicit versions of the packages in your ruby code. Unfortunately worst than not being documented this feature is wrongly documented even in RubyGems own manual pages.

So for the record here is how to tell your ruby code to use a specific gem version:

For RubyGems version < 0.9.4 we use:



1 require 'rubygems'

2 require_gem 'mechanize','=0.4.7'

3 require 'mechanize'



What we do is call "require_gem" to set up the version we want and then load the gem using "require".

After RubyGems 0.9.4 onwards the "Kernel#require_gem" method was deprecated and replaced with "Kernel#gem". So if you have the latest RubyGems the code above becomes:



1 require 'rubygems'

2 gem 'mechanize','=0.4.7'

3 require 'mechanize'



Instead of using "=0.4.7" it is also possible to use "<0.4.7" or ">0.4.7" if the specific version is not required.

We can be more professional and make our scripts handle both cases above using the Gem::RubyGemsVersion constant:



1 require 'rubygems'

2 if Gem::RubyGemsVersion < "0.9.4"

3     require_gem 'mechanize','=0.4.7'

4 else

5     gem 'mechanize','=0.4.7'

6 end

7 require 'mechanize'



This way our code will (hopefully) work with all versions of RubyGems.

References:
http://redhanded.hobix.com/inspect/autorequireIsBasicallyGoneEveryone.html
http://piao-tech.blogspot.com/2006/11/tips-on-rubygems-installation-and.html

Tuesday, February 12, 2008

Per user Latex style and bst files

Easy way to tell latex where to look for custom sty, cls and bst files:



1 # The double slash at the end means recursive.

2 export BSTINPUTS=.:~/.latex//:

3 export BIBINPUTS=.:~/.latex//:

4 export TEXINPUTS=.:~/.latex//:




Change the path to reflect your system and make sure to include the current dir "." in the list of paths or latex won't be able to find the tex files in the current directory.

Monday, January 07, 2008

Reset Master Password in Thunderbird 2.0.0.9

For some mysterious reason the master password of Mozilla Thunderbird got set in my wife's computer (Windows XP) and every minutes a dialog box would pop up asking for it. Of course no one knew what the master password was so using Thunderbird in that machine was starting to get annoying.

After searching in Google I found two solutions: one consisted in create a new profile and transfer the mail data from the problematic profile to the new one.This solution required too much effort for my liking. The second solution that everybody said would not fail was running the following command:

"C:\path to thunderbird\thunderbird.exe" -chrome chrome://pippki/content/resetpassword.xul

Unfortunately for me this command never worked no matter what I did and as this forum thread shows I am not alone so I assume is a Thunderbird problem and not mine. I even tested the command in two different machines one with a clean Windows installation and I can assure this command does not work!.

So a little more research showed me that the passwords are stored in a file called key3.db that resides inside the mail profile folder. If you don't know where is your profile folder check this link.

So I simply closed Thunderbird and renamed the "key3.db" file to "key3.db.bak" and started Thunderbird. And that was it... all passwords were gone but also the annoying master password was gone too.

If you need to reset your master password for any reason and you do not care loosing the already stored passwords (web account passwords) then this is the simplest method to do it.

Thursday, December 27, 2007

Shade windows in KDE using double click in the title bar

In KDE using compiz-fusion to shade the current active window we need to press Ctrl+Alt+s that is not a very easy thing to do specially in small keyboards (i.e. laptops).

Another way to shade windows is by double clicking in the window title bar but for this to work we must first enable it in kwin by editing the file: ~/.kde/share/config/kwinrc. Inside this file after the section [Windows] simply add the following line:


TitlebarDoubleClickCommand=Shade


Now after you have restarted you KDE session you will be able to shade windows by double clicking their title bars.

Sunday, October 21, 2007

How to fix the "Warning: Unable to load the OpenGL" error in Matlab

If you use Matlab in K/Ubuntu and you get a warning message like "Warning: Unable to load the OpenGL" everytime you start Matlab it means you are not using the powerful OpenGL rendering engine to draw your plots.

This may not be a big problem as Matlab will work as expected but if you have a 3D accelated graphics card (ATI or NVidia) then you could greatly benefit from having Matlab use the OpenGL renderer. Even without a 3D accelerated graphics card I would say that using OpenGL for rendering is a plus.

So solve this problem you simply need to remove a file (libgcc_s.so.1) from the Matlab install directory in order for it to use the one in the system that has correct links to the OpenGL libraries.

Find the file inside $MATHROOT/sys/os/glnx86 and move it to another place like:

mv libgcc_s.so.1 libgcc_s.so.1.old

Restart Matlab and the warning message should have dissapeared and the command "opengl info" should give and output like:

Version = 2.0.6473 (8.37.6)
Vendor = ATI Technologies Inc.
Renderer = ATI MOBILITY FireGL V3200
MaxTextureSize = 2048
Visual = 0x29 (TrueColor, depth 24, RGB mask 0xff0000 0xff00 0x00ff)
...

The output may vary depending on your graphics card or if you have accelerated or non-accelerated (i.e. Mesa) OpenGL graphics.

Wednesday, August 01, 2007

Dealing with Errno::EBADF in ruby net/http

In August 2006 I posted a question in a forum about an error (Bad File Descriptor) I was getting when using the net/http library of Ruby. Today, almost one year latter, I received an email from someone with that same problem asking for my advice and a few hours latter the same person wrote again saying he found a satisfactory solution and wanted to share it with me.

As part of this wonderful online community I am obliged to share my new knowledge in hopes it is useful to others as it was useful for me.

The Bad File Descriptor error (Errno::EBADF) occurred sporadically while using the net/http library to connect to a lot of pages (web spider) in a short time span. The main problem was that I could never catch that error (i.e. rescue it) and the script would not finish leaving a lot of pages without processing. To solve this problem at the time I split my script is several smaller ones and added a small delay between web pages.

My solution works but that small delay for a thousand pages add up and the scripts take not minutes but hours to finish.

The email I got explained that the cause of this error is that the operating system (Win XP) is running out of TCP ports for new connections. Many of the sockets open are put in TIME_WAIT state (meaning that the client has closed but the server has yet to close from its side).

The first approach to solve this is to force Ruby to close the connection but there is no such facility, at least in Ruby 1.8.x, for doing it. The second solution and the one I received by mail was to increase the upper range of dynamically allocated to client TCP/IP connections to a value.

Here are the instruction on how to do it:
http://msdn2.microsoft.com/en-us/library/Aa560610.aspx

If you expect to make a lot of http connections fast using Ruby in a Windows XP machine then you better increase that number of ports or you will be around asking yourself what this random EBADF error is all about.