Archive for the ‘Programming’ Category

Writing and releasing a free software tool in two weeks

Tuesday, May 27th, 2008

Two weeks ago I started a mini-project to write a simple piece of software in C#, package it up and release it. Although I had initially given myself two weeks, the actual time I spent on this project was less than a week due to a pest problem, a birthday, and a family visit. The purpose of this project was to learn the basics of a new programming language, and to learn how to package and release the initial version of a new piece of simple software.

Picking a language

I had never written a line of C# in my life but knew that it had similarities to both C++ and Java. I’ve been programming with C++ and Java professionally (by which I mean that someone pays me to do it) for many years so it seemed an interesting new language to experiment with.

I downloaded the free Microsoft Visual C# 2008 Express Edition and installed it. I followed the initial tutorial to learn how to build a very basic application and then started experimenting with adding new features.

Visual C#

Version control

The version control system that I am used to working with is Perforce. I therefore wanted to use a different version control system for this project (remember this is a learning exercise so I want to try new technologies!).

I picked the well known subversion as the version control tool with TortoiseSVN as the front end to subversion. Subversion is a popular choice and is easy to use if you are used to using something like Perforce. It has many differences but also many similarities.

tortoisesvn

What to write?

I had decided to write a Windows clipboard extender. There are loads of other clipboard extenders available but that didn’t worry me. My goal isn’t to create something original, just something that I would use myself.

The core features that I wanted to include was stack and queue support for the clipboard. Normally copying an item of text to the clipboard overwrites the previous item. I wanted the previous values to be stored in either a queue or a stack. e.g.

I copy (using Ctrl-C) the values ‘one’, ‘two’, ‘three’.
I then paste (using Ctrl-V) and the pasted values are ‘one’, ‘two’ and then ‘three’. This is the queue behaviour.
If using the stack the pasted values would be ‘three’, ‘two’ and finally ‘one’.

Online help with writing the software

As I was completely new to C# and its APIs, I had to do a lot of searching to find out how to do Clipboard operations. The Visual C# help system was also very good.

There is a huge amount of help for almost any C# problem you can think of on the internet so it wasn’t hard to find out how to solve any problem that I had. In particular the following articles were very helpful:

  1. Create a Windows Clipboard Monitor in C# using SetClipboardViewer
  2. Global System Hooks in .NET

Writing a EULA

When releasing software you would normally ship an end-user licence agreement (EULA) along with the software. This tell the user what they can and can’t do with your software.

If you want to open source your software there is a good source of licensing information at the Free Software Foundation.

In my case I wanted the software to be free to use but proprietary (I’m not shipping the source). It is harder to find information on how to produce a licence in this case. One option is to look at existing licences and adapt any bits you like. I used the XS EULA Generator (update: sadly the tool no longer exists) and then adapted it for my needs.

Creating an install package

Once I had written my clipboard extender (which I named nbClipboard) I needed some way to package it up with a Windows installer.

After looking around the best option seemed to be Inno Setup combined with ISTool (a front end for Inno Setup). You can download both together by going to the Inno Setup website and downloading the QuickStart Pack.

ISTool

This creates a professional looking software install executable which looks identical to the install tools used by many popular software applications.

nbClipboard setup wizard

Making a PAD file

To release software you can add it to your own website and in addition you may want it to be added to the many software download websites out there. Almost all software download websites require you to submit a PAD file (Portable Application Description). This file describes all the attributes of your software (purpose, author, screenshots, OS requirements etc) in a standard format which allows these download sites to stay up to date.

The format was created by the Association of Shareware Professionals (ASP) and they have a free PADGen tool that you can use to create the PAD file.

padgen

Releasing the software

After creating the install package and the PAD file I uploded both of them to my website and then submitted the PAD file to the ASP repository (a store of PAD files that many software download sites use as a data source).

Just two hours after making the submission nbClipboard had appeared on two download sites and was even appearing in the Google search results as a result of one of these download sites being indexed. In the next few days it appeared on other download sites as well. You may want to note that the sites that pick these PAD files up automatically seem to be the small download sites. If you want your software to appear on better known directories such as Tucows and download.com you’ll need to handle the submission manually.

nbClipboard

nbClipboard is available from the below link and it might be available from some small software download sites as well – if you can find them!

nbClipboard Clipboard Extender

nbClipboard - adds queue and stack support to the Windows clipboard

It certainly won’t win any awards but then I was more interested in going through the whole process of writing and releasing a new software tool than creating something which was ground breaking.

C# – How to make a window stay on top and become transparent

Sunday, May 18th, 2008

Whilst looking for information on how to make a window stay on top using C#, I spotted some odd solutions on a number of other web sites. After looking through the Form properties in Visual Studio C# 2008 myself it turns out that it is really easy to make a window stay on top. I added a check box to by application and when it is ticked the window stays on top.

Visual C# buttons

private void stayOnTopCheckBox_CheckedChanged(object sender,
                                              EventArgs e)
{
    if (stayOnTopCheckBox.Checked)
    {
         this.TopMost = true;
    }
    else
    {
        this.TopMost = false;
    }
}

Along the same theme of ‘very cheap and quick tricks that you add to your C# program’ I added another box to make the window transparent. Below is the code and the result of ticking this box.

private void transparentCheckBox_CheckedChanged(object sender,
                                                EventArgs e)
{
    if (this.transparentCheckBox.Checked)
    {
        this.Opacity = 0.5;
    }
    else
    {
        this.Opacity = 1;
    }
}

Visual C# transparent buttons

FTP uploading a directory of files using perl

Monday, February 18th, 2008

Here is a simple script to upload the files which are in a particular directory. I use something similar to automate the file uploading for my tube walking blog.

Whenever I take photos for the blog I put them into a directory with the current date as the directory name. So I have a structure like this:

c:\website\080115\
c:\website\080203\
c:\website\080205\
c:\website\080218\

I want the images in these directories to be uploaded to a new directory on the FTP server where the FTP server directory is named with the date e.g. ‘080218’.

The script is run with a command such as:

perl imageupload.pl c:\website\080218\

The script will then:

  1. Extract the date part of the directory – ‘080218’.
  2. Connect to the FTP server.
  3. Create a directory called ‘080218’ on the FTP server.
  4. Get a list of all the local files in the directory that you passed in as the argument.
  5. Upload each of these files to the FTP server.

Here’s the script. You’ll need to modify the FTP server address, username and password if you want to use it.

use File::Basename;
use Net::FTP;
my $directory = $ARGV[0];
my @parts = split(/\\/, $directory);
# get leaf directory name from whole path
my $length = $parts;
my $dateDir = $parts[$length-1];
$ftp = Net::FTP->new("ftp.yourwebsite.com", Debug => 1)
    or die "Cannot connect to hostname: $@";
$ftp->login("username", "password")
    or die "Cannot login ", $ftp->message;
$ftp->cwd("/website/data")
    or die "Cannot change working directory ", $ftp->message;
# create 'date named' directory on FTP server
$ftp->mkdir($dateDir);
$ftp->cwd($dateDir);
# set binary mode which is needed for image upload
$ftp->binary();
opendir(DIR, "$directory");
my @files = readdir(DIR);
foreach my $file (@files)
    {
    if (not -d $file)
        {
        # make sure the names are lower case on the server
        $file = lc($file);
        # upload the file
        $ftp->put("$directory\\$file");
        }
    }
$ftp->quit();

The script is simple, but it can easily be integrated into a more complex set of scripts to help you manage your web site.

Backup / restore of last modified and created file time attributes on Windows

Saturday, January 12th, 2008

Last month I found that I needed to be able to backup and restore the last modified and created file times of a load of files in Windows XP. This is probably a bit of an obscure thing to need to do so naturally I couldn’t find any existing tools to do it.

I’ve therefore made a Perl script to allow this to be done. Just so you are clear, this isn’t backing up the files, it is backing up the last modified and created file time attributes.

This allows changes to be made to the files whilst keeping these attributes intact. In theory Perl’s utime function should allow me to change these attributes. However on my Windows XP machine it just didn’t work. I’ve therefore used the rather excellent nircmd tool to handle the file attribute modifications. You’ll need to download it and modify backupLastModified.pl to point to it.

Usage is simple:

perl backupLastModified.pl c:\yourdir -backup
perl backupLastModified.pl c:\yourdir -restore

Make sure you do a test of this script before you use it for real. It has only ever been tested on one machine.

  1. Download backupLastModified.zip
  2. View backupLastModified.pl

This script will create a file ‘backuplist.txt’ in the directory that you pass in as the first argument. If you run -backup multiple times then any new files will be appended to the backuplist.txt. The stored last modified / created times of any existing files will be preserved.

Adding a date/time stamp to a jpeg image in Java

Monday, October 22nd, 2007

I’ve been looking for a simple tool to add a date/time stamp to some jpeg images. There are quite a lot of tools to do this but they all tend to cost money. I decided to make my own in Java. The actual process of loading a jpeg, adding a stamp and then saving it again is very simple. The below snippet will do the job.

BufferedImage bi = ImageIO.read(sourceFile);
Graphics2D graphics = bi.createGraphics();
Font font = new Font("ARIAL", Font.PLAIN, 20);
graphics.setFont(font);
graphics.drawString(stamp, 50, 50);
bi.flush();
ImageIO.write(bi, "jpg", targetFile);

I added some options (you can change font size, position, colour) to meet my needs. You can download it for free from https://www.reviewmylife.co.uk/blog/2008/07/03/photostamper-utility-with-java-source/. It is very basic – command line only – but does the job well enough. The Java source code and Eclipse project files are also available for download.

ProGuard Eclipse Ant build.xml

Saturday, October 20th, 2007

If you are building J2SE apps with Eclipse and want to use ProGuard to compress, optimise or obfuscate your code you’ll need to create an Ant build.xml file to do this. I found various bits of help on the internet explaining parts of the build.xml file, but couldn’t find anywhere that gave the complete build.xml to do the full compile, jar and proguard steps.

Here is the full build.xml I cobbled together to do all three:

<?xml version="1.0" ?>
<project default="main">
<taskdef resource="proguard/ant/task.properties"
	classpath="D:\apps\proguard4.0.1\lib\proguard.jar" />
<target name="main" depends="compile, jar, obfuscate"
	description="Create project">
	<echo>Creating project.</echo>
</target>
<target name="compile" description="Compile target">
	<javac srcdir="src" destdir="bin"/>
</target>
<target name="jar" description="Jar target">
	<jar jarfile="PhotoStamper_debug.jar"
		basedir="bin" includes="*.class">
		<manifest>
		<attribute name="Main-Class" value="PhotoStamper" />
		</manifest>
	</jar>
</target>
<target name="obfuscate" depends="jar"
	description="Obfuscate compiled classes">
	<proguard>
		  -libraryjars "${java.home}\lib\rt.jar"
		  -injars      PhotoStamper_debug.jar
		  -outjars     PhotoStamper.jar
		  -keep public class PhotoStamper {
			public static void main(java.lang.String[]);
			  }
	</proguard>
</target>
</project>