Tautvidas Sipavičius

Web development, programming and tech

Enable Skype Icon on Unity Notification Panel on Ubuntu 12.04 LTS

| Comments

When you do a fresh install of Ubuntu 12.04 (Or any version with Unity for that matter), you install Skype and expect it to work as it used to. But if you close the main window - you soon notice that Skype icon is not being displayed in top panel.

To fix this you will need to run a few commands. So open up your terminal and run this:

gsettings get com.canonical.Unity.Panel systray-whitelist

You should get something like this:

['JavaEmbeddedFrame', 'Wine', 'Update-notifier']

Basically this means, that Skype is not whitelisted, thus is not allowed by default to display its tray icon. We need to fix this. Append 'Skype' to the list so that you have something like this:

['JavaEmbeddedFrame', 'Wine', 'Update-notifier', 'Skype']

Then wrap it with double quotes (”) and add gsettings set com.canonical.Unity.Panel systray-whitelist in front of it. At this point you should have something like this:

gsettings set com.canonical.Unity.Panel systray-whitelist "['JavaEmbeddedFrame', 'Wine', 'Update-notifier', 'Skype']"

Now just copy this to your Terminal and execute the command. Reboot afterwards. Now you should see the icon.

The Observer Pattern - Design Patterns

| Comments

Before we start, you might ask:

What is a design pattern?

Design pattern is a general solution to particular problem that is occurring commonly. However this is not an already implemented source code (you’ll have to do that yourself). It is not an already finished design either. You can incorporate design patterns into your application design.

It formalizes best practices and describes interactions between objects and classes. Basically it is a template for how to solve a problem.

If you want to get a better description of what a design pattern is, you can read about it on Wikipedia

Observer pattern

It is a one-to-many push type design pattern, in which an object (“Subject”) maintains a list of dependants (“observers”). Once the subject changes state, it automatically notifies each observer with its new state.

You can think of it as a mailing list. Mailing list is the subject, you attach to it by sending in your e-mail address. But there can be many subscribers - Observer pattern does not limit the number of observers.

When there is a new email, the state has changed and the server emails it to each of the subscribers, including you!

Example

Let’s build our own simple application that uses the Observer pattern. In this example we’re going to use PHP.

There will be at least 2 classes - one for the Subject, one for the Observer.

The Subject will have to have at least these three methods:

  • Attach - a method that the observers will use to register
  • Detach - observers may want to unregister
  • Notify - This will change subject’s state

As for the Observer, we can get away with this single method:

  • Notify - Notification method, which will be called by the subject

Ok, let’s start coding! Let’s start by creating a Subject class:

Subject.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?php
class Subject {
    private $observers = array(); // We will store observers here

    public function attach($observer) {
        if(!in_array($observer, $this->observers)) { // Make sure we don't add an observer twice
            $this->observers[] = $observer; // Add the observer
            return true;
        } else {
            return false; // Observer was not added
        }
    }

    public function detach($observer) {
        if(!in_array($observer, $this->observers)) { // Make sure the observer is registered
            return false;
        } else {
            $key = array_search($observer, $this->observers); // Find observer's key
            unset($this->observers[$key]); // Remove the observer
            $this->observers = array_values($this->observers); // Reindex the observer array, as unset leaves a gap
            return true;
        }
    }

    public function notify($message) {
        foreach($this->observers as $observer) { // Notify each observer
            $observer->notify($message); // Dispatch the message to each observer
        }
    }
};

Ok, we’ve implemented our Subject class. Observers will register through attach method and unregister through detach method. Notifications will be sent with notify method.

Now let’s build our Observer class:

Observer.php
1
2
3
4
5
6
7
8
<?php
class Observer {
    public function notify($message) {
        echo "I have received a message: \r\n";
        var_dump($message); // Just print out any information it may contain: strings, numbers, objects...
        echo "END OF MESSAGE\r\n";
    }
};

Our Observer class only needs notify method, which will output all received messages.

And lastly, let’s create our demo program that makes use of these classes:

main.php
1
2
3
4
5
6
7
8
9
10
11
12
13
<?php
require_once('Subject.php');
require_once('Observer.php');

$subject = new Subject(); // Create a Subject

// Let's try to change state. It should not output anything as it doesn't have any observers registered
$subject->notify('This shall not be read!');

// Let's create an Observer, register it and send another notification
$observer = new Observer(); // Create an Observer
$subject->attach($observer); // Register Observer to the Subject
$subject->notify('Can you see me?'); // Send notification to the Observer

If we run this, we should see Can you see me? text. If you do - good work! Now you know the Observer pattern.

I’ve created a Github repository with complete code from this article.

Blocking Time Consuming Sites on Linux

| Comments

Have you noticed that you spend quite a bit of time on time consuming sites, such as youtube, reddit, Hacker news, CNN.com and etc.? I certainly did. When it comes to productivity, such websites don’t help at all. But now that you think of it, maybe it’s worth to block them completely?

Blocking websites with hosts files

Linux has a host file at /etc/hosts, Windows have one as well (but it’s hidden somewhere in Windows folder).

You simply need to add entries in such manner:

127.0.0.1 example.com

127.0.0.1 is your internal localhost IP address and instead of example.com you enter your time consuming site. Add as many entries as you need.

Just as an example, here’s my /etc/hosts file:

127.0.0.1 localhost
127.0.0.1 reddit.com
127.0.0.1 twitter.com
127.0.0.1 news.ycombinator.com

And when you’re done, you won’t be able to access these sites that easily (but you will still be able to do that by using certain methods).

And that’s it! This change is easily to apply and easy to revert. It blocks access to your most time consuming websites and reminds you your decision not to visit them.

Setting Up PHP5 With Nginx Through FastCGI

| Comments

I need to run a LNMP (Linux Nginx Mysql PHP) stack for development. Here’s an easy way how you can run PHP5 through FastCGI on Nginx as well.

Install required packages

You need to install these packages: php5-common, php5-cgi and nginx:

sudo apt-get update
sudo apt-get install php5-common php5-cgi nginx

Start/stop PHP-FastCGI

Create a start script in your /etc/init.d directory:

sudo gedit /etc/init.d/php-fastcgi

And add this code in it:

#!/bin/bash
BIND=127.0.0.1:9000
USER=www-data
PHP_FCGI_CHILDREN=15
PHP_FCGI_MAX_REQUESTS=1000

PHP_CGI=/usr/bin/php-cgi
PHP_CGI_NAME=`basename $PHP_CGI`
PHP_CGI_ARGS="- USER=$USER PATH=/usr/bin PHP_FCGI_CHILDREN=$PHP_FCGI_CHILDREN PHP_FCGI_MAX_REQUESTS=$PHP_FCGI_MAX_REQUESTS $PHP_CGI -b $BIND"
RETVAL=0

start() {
    echo -n "Starting PHP FastCGI: "
    start-stop-daemon --quiet --start --background --chuid "$USER" --exec /usr/bin/env -- $PHP_CGI_ARGS
    RETVAL=$?
    echo "$PHP_CGI_NAME."
}
stop() {
    echo -n "Stopping PHP FastCGI: "
    killall -q -w -u $USER $PHP_FASTCGI
    RETVAL=$?
    echo "$PHP_CGI_NAME."
}

case "$1" in
        start)
            start
    ;;
        stop)
            stop
    ;;
        restart)
            stop
            start
    ;;
        *)
            echo "Usage: php-fastcgi {start|stop|restart}"
            exit 1
    ;;
esac
exit $RETVAL

Make it executable:

sudo chmod +x /etc/init.d/php-fastcgi

Make it launch at startup (optional):

sudo update-rc.d php-fastcgi defaults

Launch it manually:

sudo /etc/init.d/php-fastcgi start

That’s it, now you should be able to use PHP on Nginx through PHP-FastCGI

Nginx configuration example

Example server config:

location ~ \.php$ {
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME /var/www/$fastcgi_script_name;
    include fastcgi_params;
}

Restart NginX:

sudo /etc/init.d/nginx restart

Set up a phpinfo() sample page:

echo "<?php phpinfo();" > /var/www/index.php

Visit the page as you normally would. You should see a PHP information page.

Remapping Unity Hotkeys for Ubuntu 12.04 LTS

| Comments

Upon installing the new Ubuntu 12.04 release I noticed terrible inconsistencies in window management. E.g. to snap window to left half (or right half for that matter) the default hotkey is Ctrl+Alt+Left, however snapping window to the upper half is Ctrl+Alt+Kp8 (Keypad 8). Let’s fix that.

If you don’t have Compiz Settings Manager installed (it is not installed by default), type this command into Terminal to install it:

sudo apt-get install compizconfig-settings-manager

Once installed, launch the Compiz Settings Manager. Under “Window Management” click “Grid”, then select “Bindings” tab. Now you can set up your own Window Management hotkeys by clicking on each already defined binding and modifying it to your own desire. If you have a keypad, I suggest you to use these key bindings:

  • Put Center: <Control><Alt>KP5
  • Put Left: <Control><Alt>KP4
  • Put Right: <Control><Alt>KP6
  • Put Top: <Control><Alt>KP8
  • Put Bottom: <Control><Alt>KP2
  • Put Top Left: <Control><Alt>KP7
  • Put Top Right: <Control><Alt>KP9
  • Put Bottom Left: <Control><Alt>KP1
  • Put Bottom Right: <Control><Alt>KP3
  • Maximize: <Control><Alt>KP0

If I remember this correctly, these key bindings were default on Ubuntu 11.10