Archive for April, 2008

Gediting as root

Posted at 11:02 pm in ubuntu

To open a file with root permissions using gedit you normally would have to drop to terminal and type gksudo gedit filename.

Waiting for devs to implement policykit into gedit too, you can install an handy nautilus plugin which makes appear a context menu entry to “open as administrator” on every file.

Installing is as simple as typing:

sudo apt-get install nautilus-gksu

Hardy’s package, sadly, has a bug that prevents the entry for appearing. If you don’t see the entry appearing even after restarting X here’s the fix:

sudo cp /usr/lib/nautilus/extensions-1.0/libnautilus-gksu.so /usr/lib/nautilus/extensions-2.0/
killall nautilus

The entry should now appear. Worth of noting that this workaround could be disabled from future updates (although probably next update will just fix the bug)

 
no comments

Written by Stefano Forenza on April 26th, 2008

How to toggle desktop icons on and off

Posted at 4:14 pm in ubuntu

If you like me like to have icons on desktop but sometimes need to just hide everything to better concentrate on what you’re doing you may like this little bash script I came out with. (warning: this post applies only to gnome)

Istructions

Verify you have gconf2 installed. Do that with: sudo apt-get install gconf2.

You need a folder where to store this script (along with other scripts maybe), so make one or just use your home folder. So, open this folder and create an empty file. Call it toggle_desktop.sh or something like that. Open it with gedit and paste this text in the file.

#!/bin/bash

if [ $(gconftool-2  --get /apps/nautilus/preferences/show_desktop) == "true"  ]; then
	echo "disabling desktop"
	gconftool-2   --type boolean --set /apps/nautilus/preferences/show_desktop "false"
else
	echo "enabling desktop"
	gconftool-2   --type boolean --set /apps/nautilus/preferences/show_desktop "true"
fi;

# nautilus may crash ( !#£%$$@à###!!!! )

echo "checking if nautilus is still up"
sleep 1
if [ $( ps xa | grep nautilus | grep -v grep ) -z ]; then
	echo "nautilus is not up or crashed. Restarting nautilus"
	nautilus -n &
	echo "everything done."
else
	echo "all good - finishing job"
fi;

Done ? Save it and close gedit. Right click it, choose properties, then permissions and mark the file as executable.

Now all you need to do is double click it to hide or show your desktop icons. You may want to drag the file to your panel to access it more easily:

Note: There are a few shortcomings.

  1. The first is that when you hide your icons, you cannot anymore right click on the desktop. Nor changing desktop background works, it will get applied when you show your icons again.
  2. The second thing is that while hiding your desktop is quite fast showing it again may take 1 second or two and your window manager may become unresponsive in the meantime.
  3. Third thing to keep in mind: sometimes nautilus just crashes. I’ve included a quick check in the script to restart it. Nautilus crashing means it could just wipe (close) away all you’re opened file browsers. In my tests I’ve never one file browser wiped away, but it could happen. Nothing dangerous, only annoying.
 
2 comments

Written by Stefano Forenza on April 26th, 2008

5 tips to make your PHP code beautiful (3)

Posted at 5:31 pm in php

Avoid unnecessary control structures.

Brackets everywhere. Try to get rid of those as much as you can. Unnecessary brackets add visual complexity and make your code much longer. In real world situation, when you need to quickly browse through your code, they just add clutter

//prints out the structure of a table
function describe( $table )
{
	$query = "SHOW COLUMNS FROM `$table `";
	$result =mysql_query( $query );
	if ( ! $result )
	{
		return $result;
	} else {
		$data = array();
		while ( $record = mysql_fetch_assoc( $result ) )
		{
			$data[] =  $record;
		}
		return $data;
	}
}

In the function above the if-else structure is not needed, just because when $result is false I can just return it, breaking the flow. Also while ( $record = ..) uses brackets for looping over just one statement.

We can make describe() look much more tidy:

function describe( $table )
{
	$query = "SHOW COLUMNS FROM `$table `";
	$result =mysql_query( $query );
	if ( ! $result ) return $result;
	$data = array();
	while ( $record = mysql_fetch_assoc( $result ) ) $data[] =  $record;
	return $data;
}

Seems nicer, uh ? While one could think that’s just a detail, when code complexity grows, this kind of tricks can help you cutting it down.

 
no comments

Written by Stefano Forenza on April 20th, 2008

5 tips to make your PHP code beautiful (2)

Posted at 4:58 pm in php

Thoughtful minimalism

Think about the most concise, clear and readable way to write the code your writing. Assume you were writing a function to get a file extension (.gif, .doc, etc.). You could be tempted to use string manipulation functions.

$filename = 'file.doc';
$dot_position = strpos( $filename, '.' );
if ( $dot_position !== false )
{
	$extension = substr( $filename, $dot_position + 1  );
}
var_dump($extension)

It’s a lot of code for such a simple task. Beside that, files with multiple dots ( filename.with.dots.html ) won’t be handled properly.

$filename = 'file.doc';
$extension = array_pop ( explode( '.', $filename ) );
var_dump($extension)

The lesson here is to restlessy try to find the best solutions , even when you thought you knew how to accomplish the task. The one-liner above, is more easy to read and much less error-prone. It will work with file with multiple dots or with no extension without any problem. (note: you may want to filter $filename with basename() to avoid directory names dot to interfere).

I am gonna give another example - imagine you’re trying to print a different string for a bunch of pages on you’re site.

if ( $page_id == 32 || $page_id == 10 || $page_id == null )
{
	echo 'inactive';
} else {
	echo 'active';
}

While this code is pretty ok, you could note that $page_id is repeated one time for every page you want to set as inactive.

$inactive_pages = array( 32, 10, null );
if ( in_array( $page_id, $inactive_pages ) )
{
	echo 'inactive';
} else {
	echo 'active';
}

This way you can add exceptions just inserting a new element in $inactive_pages array. This will make easier refactor the code to read $inactive_pages from a configuration file or from a database.
In situations where you need to keep your code even smaller you could even write a one-liner (but don’t exceed with them, as many people find them less readable - and often they’re right):

echo in_array( $page_id, array( 32, 10, null )  )  ? 'inactive' : 'active';
 
1 comment

Written by Stefano Forenza on April 20th, 2008

5 tips to make your PHP code beautiful (1)

Posted at 4:18 pm in php

Get your brackets right

..and indent your code properly, always. Always give to each of your curly brackets their very own line.
I am taking a snippet from Kohana to demonstrate this.

(bad) What do you think of this code ?

if ( ! is_array($data)){
if (strpos($data, '[') !== FALSE)
{$data = preg_replace('/\[.*\]/', '', $data);
}$data = empty($data) ? array() : array('for' => $data);}

(not nice) This is much better

if ( ! is_array($data)){
	if (strpos($data, '[') !== FALSE){
		$data = preg_replace('/\[.*\]/', '', $data);
	}
	$data = empty($data) ? array() : array('for' => $data);
}

(sweet) A nicely formatted code should show like this. You can tell at a glance where your every if begins and ends.

if ( ! is_array( $data) )
{
	if ( strpos( $data, '[') !== FALSE )
	{
		$data = preg_replace('/\[.*\]/', '', $data );
	}
	$data = empty( $data ) ? array() : array( 'for' => $data );
}
 
no comments

Written by Stefano Forenza on April 20th, 2008