Avoiding Windows Update

Illustration

Work day is over. You pack your things and you shut down your laptop. And then Windows update kicks in and tells you not to turn off your computer until it is done. Congratulations, you have just wasted a ten minutes waiting for computer.

This annoyance is usually avoided by putting your computer to sleep instead of shutting it down. But that option is not a valid one for those with dual boot. At one point or another you will have to go through shut down procedure.

If you have a laptop (or UPS) there is a trick that you can pull. Just unplug your laptop from power. Windows will detect that it is running on battery power and it will not apply updates during that shut down. You’ve got quick shut down and update will wait for whenever you actually want it.

To Round a Penny

Canada decided to withdraw penny coin. In order to keep everything going smoothly and fair they also specified rules for rounding. Rules that are not readily available in any programming language.

In time before rounding functions you would round stuff by multiplying by 100 (in case of pennies) and adding 0.5. You would truncate resulting number and divide it with 100 thus abusing implicit rounding when stuff gets converted to int. Something like this:

int pennies = (int)(value * 100 + 0.5);
return pennies / 100.0;

With nickel rounding we shall use almost same algorithm:

private static Decimal RoundPenny(Decimal value) {
    var pennies = Math.Round(value * 100, 0);
    var nickels = Math.Truncate((pennies + 2) / 5);
    var roundedPennies = nickels * 5;
    return roundedPennies / 100;
}

First we just get a number of pennies. Then we increase number by two pennies (half of nickel) and divide everything by one nickel. Truncating result gives us number of nickel coins that we need for a given penny count. Multiplication by five converts everything back to pennies and final division by 100 ensures that dollars are returned.

Code is here.

PS: Yes, you can write this more concise but I think that this makes example much more readable. Concise example would be:

return Math.Round(value * 20, 0) / 20;

Trimming Text in AfterLabelEdit

Editing text of ListView item is quite easy. Just set LabelEdit = True and you are good. This will allow user to change text of item and it will even let us validate it in AfterLabelEdit handler. But can we additionally adjust edited text?

Most obvious choice would be setting e.Label to some other value. That will unfortunately never compile since Label property is read-only. Next guess will be just hijacking AfterLabelEdit and editing text in code. Something like this:

if (e.Label == null) { return; }
var text = e.Label.Trim();
if (string.IsNullOrEmpty(text)) {
    e.CancelEdit = true;
} else {
    list.Items[e.Item].Text = text;
}

And this works - almost.

Editing label at this point does not really matter. Whatever you write into Text property will be overwritten with e.Label once AfterLabelEdit handler is returns.

In order to handle things our self we just need to pretend that we canceled original edit:

if (e.Label == null) { return; }
var text = e.Label.Trim();
if (string.IsNullOrEmpty(text)) {
    e.CancelEdit = true;
} else {
    list.Items[e.Item].Text = text;
    e.CancelEdit = true;
}

Full code is here.

We Are Sorry

Illustration

I am an honest man. Not a perfect one but I try. Given choice and acceptable price I will always try to buy.

I already got pissed a few times before with my inability to buy content in Croatia. It wasn’t question of money. Content was just not available for my IP.

Since I am in States now I though that no further issues would arise - I have a money and I have a proper address. Armed with confidence I tried to rent a movie on Amazon. Guess what? I was denied opportunity to actually pay.

Amazon has policy of not allowing movie purchases with foreign credit cards. It does not really matter that I am actually living in US. It does not matter that I can use my credit card for other Amazon purchases. I don’t have an US credit card and that is enough to deny me a purchase.

And then movie industry wonders why people turn to pirates…

PostgreSQL on CentOS

It is not really a possibility to be Windows-only developer these days. Chances are that you will end up connecting to one Linux server or another. And best way to prepare is to have some test environment ready. For cold days there is nothing better than once nice database server.

For this particular installation I (again) opted for minimal install of CentOS 6.3. I will assume that it is installed (just bunch of Nexts) and your network interfaces are already set (e.g. DHCP).

First step after this is actually installing PostgreSQL:

yum install postgresql postgresql-server
 Complete!

Next step is initializing database:

service postgresql initdb
 Initializing database:                                     [  OK  ]

Start service and basic setup is done:

chkconfig postgresql on
service postgresql start
 Starting postgresql service:                               [  OK  ]

Next step is allowing TCP/IP connections to be made. For that we need to edit postgresql.conf:

su - postgres
vi /var/lib/pgsql/data/postgresql.conf

There we find listen_addresses and port parameters and un-comment them (along with small change from all to *):

listen_addresses = '*'
port = 5432

While we are at it, we might add all hosts as friends in pg_hba.conf (yes, don’t do this in production):

vi /var/lib/pgsql/data/pg_hba.conf

Add following line at the bottom:

host    all         all         0.0.0.0/0             trust

Finish up editing and restart service

exit
 logout
/etc/init.d/postgresql restart
 Stopping postgresql service:                               [  OK  ]
 Starting postgresql service:                               [  OK  ]

Quick check with psql is in order (notice that \q is used for exit):

psql -h 192.168.56.101 -U postgres -d postgres
 psql (8.4.13)
 Type "help" for help.
\q

If external connections are needed, we must handle firewall. And easiest way to do this is disabling it. For production environment this is a big no-no. For simple testing of virtual machine it will suffice:

/etc/init.d/iptables stop
chkconfig iptables off

And with this we are ready to accept clients.