I’m not really fond of Opera’s translation menu; maybe it’s because it only offers a handful of languages (and I have some Finnish, Danish, Swedish and other -ishes friends on Facebook).

Hopefully Opera’s known power user, Tamil, had a solution for a similar task, which I changed a bit:

  1. Go to Tools -> Preferences -> Advanced -> Toolbar
  2. Find “Menu Setup” (bottom right), click “Opera Standard” and press “Duplication” (skip this part if you already have a “Copy of Opera Standard”).
  3. From the profile directory, open “menu” folder and edit the file called “standard menu (1).ini”
  4. Find the section named “[Hotclick Popup Menu]” and add the entry:

    Item, "Google Auto Translation"= Go to page, "http://translate.google.com/?hl=auto#auto|auto|%t"

  5. Save the file, restart Opera

Now simply select a word or phrase from the HTML document, right click and choose “Google Auto Translation”.

If you want to have the ability to translate the entire page, find the section named “[Document Popup Menu]” and add the following item:

Item, "Google Auto Translation"= Go to page, "http://translate.google.com/translate?hl=auto&sl=auto&tl=auto&u=%u"

A group of journalists, bloggers, professionals and creators want to express their firm opposition to the inclusion in a Draft Law of some changes to Spanish laws restricting the freedoms of expression, information and access to culture on the Internet.

Read the declaration here.

16 Nov 2009

Ride the Wave

Filled in: Mumble, tags:

I have just added a new page, which includes my first wave! I’m eager to see how this works. For this, I used wavr

12 Nov 2009

Just made

Filled in: Websites

02 Jun 2009

Ding

Filled in: Mumble

One thousand spam blocked….. I love Akismet.

Bring it on, punk.

The default XML serialization adds the xml header and a couple of namespace definitions – elements which aren’t always welcome. Here’s how to control the XML source.

Let’s start with a simple class called ‘Book’:

Public Class Book
    Public Title As String = String.Empty
    Public Sub New()
    End Sub
    Public Sub New(ByVal title As String)
        Me.Title = title
    End Sub
End Class

Our mini-project (in this case: a console application) should be able to create a new instance of the book class and serialize it:

Imports System.IO
Imports System.Xml
Imports System.Xml.Serialization
Module Main
    Sub Main()
        Dim book As New Book("XML Serialization")
        Dim result As String
        Dim serializer As New XmlSerializer(GetType(Book))
        Using writer As New StringWriter()
            serializer.Serialize(writer, book)
            result = writer.ToString
        End Using
        Console.WriteLine(result)
        Console.ReadLine()
    End Sub
End Module

The result looks like this:

<?xml version="1.0" encoding="utf-16"?>
<Book
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Title>XML Serialization</Title>
</Book>

Task #1: removing the namespace definitions

In order to remove the namespace definitions, we need to define a new set of namespaces and tell our serializer to use this set. We can do this by using the XmlSerializerNamespaces class. We need to add the following lines just after the XmlSerializer definition:

Dim nameSpaces As XmlSerializerNamespaces = _
    New XmlSerializerNamespaces()
nameSpaces.Add("", "")

and we also need to replace line 15 in the original code with the following statement:

serializer.Serialize(writer, book, nameSpaces)

This way our XML source becomes:

<?xml version="1.0" encoding="utf-16"?>
<Book>
  <Title>XML Serialization</Title>
</Book>

Task #2: Removing the XML header

Next thing on the to-do list is to find a way to produce a plain XML file without any definitions whatsoever. To accomplish this, we need to further modify our code and use the XmlWriterSettings class with OmitXmlDeclaration property set to true. Replace lines 14-18 with the following:

Dim settings As New XmlWriterSettings()
settings.OmitXmlDeclaration = True
settings.Indent = True
Using writer As New StringWriter()
  Using xmlWriter As XmlWriter = _
      xmlWriter.Create(writer, settings)
    serializer.Serialize(xmlWriter, book, nameSpaces)
    result = writer.ToString
  End Using
End Using

Our XML now becomes:

<Book>
  <Title>XML Serialization</Title>
</Book>

Task #3: adding our own namespace definition

Our final task is to insert our own namespace definition (and only that one). First thing to do is to instantiate the XmlSerializer with our own namespace. Then we need to add our namespace to our custom namespace list. Replace the serializer definition with:

Dim serializer As New XmlSerializer(GetType(Book), _
    "urn:our_namespace")

add change the nameSpaces.add statement with:

nameSpaces.Add("", "urn:our_namespace")

The last set of changes produce the following XML:

<Book xmlns="urn:our_namespace">
  <Title>XML Serialization</Title>
</Book>

There you have it.

02 Apr 2009

Back to school

Filled in: CakePHP|PHP, tags: ,

I’ve got a couple of messages from [apparently] junior programmers regarding my previous post, so I decided to go back and explain some of the magic that’s happening every day inside Cake applications.

As you know (or should have known) Cake does a lot of magic behind the scenes, as long as we’re following some naming conventions. The developers prefer convention over configuration (via files), which may seem a bit restrictive for some, but, once you get used to it, you will notice that it speeds up things.

If you recall from the previous post, the User Model looks like this:

class User extends AppModel {
  var $name = 'User';
}

This class extends a class named “AppModel“, which in turn extends a class named “Model“. Therefore, each method described in the Model class is also available to the AppModel and (our own) the User class in turn.

Why did the developers go so far as to create two classes instead of one? Simply because they wanted to give us more flexibility. We have the option to create a class called AppModel and place that code inside the app directory like this:

/**
 *
 * Filename: /app/app_model.php
 *
**/
class AppModel extends Model {
}

Now, whenever you create this file, you can add your own custom functions, which will be available to all your models. The same goes for all your controllers, i.e. you can create a file named app_controller.php and place it inside your app directory, where a class named AppController will extend the Controller class.

In case you didn’t figure it out yourself by now, there is a file named app_model.php which Cake uses if you haven’t created your own (take a look at /cake/lib/model). Cake automatically includes your own app_model.php if you have one, or its own (empty) version if you don’t. Talking about some serious magic here!

In general, whenever your application runs, all requests are redirected to the index.php file, which in turn includes all necessary files. Cake examines your app directory and, if it finds files which can override its core files, it includes those files instead.

Finally, if you follow the naming conventions mentioned earlier, you won’t have to include anything in your files (there are some exceptions to this rule, if you need a certain level of complexity). For instance, if you name your model User, that model will be automatically available in your UsersController controller, simply by typing $this->User.

Catch you all next time.

31 Mar 2009

The Indie scene

Filled in: Mumble, tags:

For those of you (like myself) who have been hiding under your shell, there’s a whole world of games out there, away from the publicity lights. It’s the “Indie” (independent) scene, where small groups of people create wonderful games.

I was only aware of such groups thanks to Uplink, a hacker simulation game from Introversion (“the last group of bedroom programmers”). This unique game, with great plot and replayability managed to keep me in front of the computer for weeks.

Recently (read: 4 months ago) I was lucky enough to discover another great game: World of Goo from 2DBoy. The idea is to go from point A to point B, by building structures consisted of small balls (goo), which are interconnected. It’s a game certainly worth its money and, if you love puzzle games, you should certainly check it out.

World of Goo

This is a tutorial which can be used by those who want to setup a basic user administration and authentication in CakePHP, one of the most popular MVC PHP frameworks around. We will be using the Auth component of Cake and we will create some administration screens.

Setting up the database

First of all we need one table to store our users. Cake’s Auth module can automate some of the process, as long as we are following some naming conventions. Our table consists of 4 fields: id, username, password and role:

CREATE TABLE users (
  id INT(10) UNSIGNED NOT NULL auto_increment,
  username VARCHAR(20) NOT NULL,
  password VARCHAR(50) NOT NULL,
  role ENUM('user','admin') NOT NULL DEFAULT 'user',
  PRIMARY KEY  (`id`)
);

The “id” is the primary key of the table. By using this name, we are helping Cake to take care of possible Model associations without having to modify anything. The pair of fields “username” and “password” are expected by the Auth component (again, this is configurable). Finally, the “role” field will be used in order to distinguish basic users from administrators.

Careful readers will notice that the “password” field is rather large. This is because all passwords stored in the database are hashed and, in the end, the field length will be much longer than the original (depending on the algorithm used).

Modeling the table

You can either bake the table into a Model or write the Model on your own. The basic Model looks like this:

/**
 *
 * Filename: /app/models/user.php
 *
**/
class User extends AppModel {
  var $name = 'User';
}

We are going to add some validation rules:

  • the username must be non-empty and unique
  • the password must be non-empty
  • the role can either be ‘user’ or ‘admin’

Additionally, we would like to have some sort of password confirmation mechanism, which can be used either during the user registration or during the user edit. By implementing those rules, our code becomes:

/**
 *
 * Filename: /app/models/user.php
 *
**/
class User extends AppModel {
  var $name = 'User';
  var $validate = array(
    'username' => array(
      'notEmpty' => array(
        'rule' => 'notEmpty',
        'message' => 'The username cannot be empty'
      ),
      'isUnique' => array(
        'rule' => 'isUnique',
        'message' => 'The username is already taken.'
      )
    ),
    'password' => array(
      'notEmpty' => array(
        'rule' => 'notEmpty',
        'message' => 'The password cannot be empty'
      ),
      'confirmPassword' => array(
        'rule' => array('confirmPassword', 'password_confirm'),
        'message' => 'Please enter the same password twice'
      )
    ),
    'role' => array(
      'rule' => array('inlist', array('user', 'admin')),
      'message' => "A user's role must either be 'user' or 'admin'"
      )
    );
  function confirmPassword( $original, $confirmationField )  {
    return $this->data[$this->name][$confirmationField] === $original['password'];
  }
}

The password confirmation is implemented in lines 27-29. This part of the code tells Cake that we will be providing the Model with two different fields (“password” and “password_confirm”) and that we would like to apply the function named “confirmPassword” listed bellow (lines 39-43). Have a look at the relevant manual page (“Adding your own Validation Methods“) to read how you can use your own functions for advanced field validation.

Go ahead ahead and play around with the code provided. Bake your own Controller and see if you can create some forms which will allow you to add new users and edit the existing ones.

~~~to be continued…

I recently tried the synchronization feature of Opera. Although it’s a neat feature, I wasn’t really needing it, therefore I decided to cancel it. But for some days I kept seeing a new message on the status bar: “Synchronization disabled”.

A quick search didn’t reveal any non-trivial (read: touch ini files) methods, but, after reading a relevant post on Opera’s forum, I came up with the solution:

  • Open a new tab and type “opera:config
  • On the “Quick find” text box type “Opera Account” (this may be a tad slow, depending on your computer – another way is to paste that text to avoid the delay)
  • Click on the “Opera Account” text which should appear bellow.
  • Uncheck the setting “Opera Account Used” and delete the text from the “Username” textbox.
  • Press “Save” and restart Opera.

You now got your status bar back again.

About

Hey visitor. I'm a young (in the sense of "spirited") man living in Athens, Greece and I decided to setup this blog, in order to share my thoughts with you. Jump along and feel free to retort.

Stackoverflow

Stop censorship