Kiosk Application Template #1

This is the first of the kiosk application template series.

In a typical kiosk application, you will have two main pieces of code:

  • The login dialog, and
  • The main application

So in this first template, we will first get familiar with how to set both of these up in PHP-GTK2.

I have also added the following features:

  • A logout button for the user to log out.
  • The auto-logout feature that will force a user out after he/she has used the program for more than 30 minutes.

Note

  • For this sample application, use the following for the login:
    username='user1'
    password='phpgtk2'
  • Note the use of GtkDialog for both login and main application. Keeps the code cleaner and mutually exclusive of each other. The main application will only be displayed when the user has successfully logged in.

The Code

1   
2   
3   
4   
5   
6   
8   
9   
10   
12   
13   
14   
15   
16   
17   
18   
20   
21   
25   
26   
27   
28   
29   
30   
31   
32   
33   
34   
35   
36   
37   
38   
39   
40   
41   
42   
43   
44   
46   
48   
49   
50   
51   
52   
54   
55   
56   
57   
58   
60   
61   
62   
63   
64   
67   
68   
69   
70   
71   
72   
74   
75   
76   
77   
78   
79   
81   
82   
83   
84   
85   
86   
87   
88   
89   
90   
91   
95   
96   
97   
98   
99   
100   
101   
102   
103   
105   
106   
108   
109   
110   
111   
112   
113   
114   
115   
116   
117   
118   
119   
121   
122   
123   
124   
125   
126   
127   
128   
129   
130   
131   
135   
137   
138   
139   
140   
141   
142   
143   
151   
152   
153   
155   
156   
157   
158   
159   
160   
161   
162   
163   
164   
165   
166   
167   
168   
169   
170   
171   
172   
173   
174   
175   
176   
178   
179   
180   
181   
182   
183   
184   
187   
188   
189   
190   
191   
192   
193   
194   
195   
196   
<?php

define('MAX_USAGE', 30);  // max usage time = 30 min

while(1) {
    $login_success = 0;
    while(!$login_success) {
        login(); // note 1
    }

    // starts the main program only if login successful
    main(); // note 2
}

// this is your main application
function main() {
    $dialog = new GtkDialog();
    $dialog->fullscreen(); // note 3
    $vbox = $dialog->vbox;
    $vbox->pack_start(new GtkLabel());

    // display title
    $title = new GtkLabel("This is the main program");
    $title->modify_font(new PangoFontDescription("Times New Roman Italic 10"));
    $title->modify_fg(Gtk::STATE_NORMAL, GdkColor::parse("#0000ff"));
    $title->set_size_request(-1, 40);
    $vbox->pack_start($title, 0);

    $vbox->pack_start(new GtkLabel("User verified"), 0);
    $vbox->pack_start(new GtkLabel("Main application starts..."), 0);
    $vbox->pack_start(new GtkLabel(
        "Note: application will auto-logout after 30 min of usage"), 0);
    $vbox->pack_start(new GtkLabel());

    // sets up a logout button
    $vbox->pack_start($hbox = new GtkHBox(), 0);
    $hbox->pack_start(new GtkLabel());
    $button = new GtkButton('Logout');
    $hbox->pack_start($button, 0);
    $button->connect('clicked', 'on_logout_button', $dialog);

    // auto-logout after 30 minutes of usage
    global $start_time, $timeout_maxtime;
    $start_time = time();
    $timeout_maxtime = Gtk::timeout_add(MAX_USAGE * 60 * 1000, 
        'logout', $dialog); // note 4

    $dialog->set_has_separator(false);
    $dialog->show_all();
    $dialog->run();
    $dialog->destroy();
}

function on_logout_button($button, $dialog) {
    logout($dialog);
}

function logout($dialog) {
    global $timeout_maxtime;
    Gtk::timeout_remove($timeout_maxtime);
    $dialog->destroy();
}

// the login function
function login() {
    $dialog = new GtkDialog('Login', null, Gtk::DIALOG_MODAL);
    $dialog->fullscreen(); // note 5
    $vbox = $dialog->vbox;
    $vbox->pack_start(new GtkLabel());

    // display title
    $title = new GtkLabel("Login");
    $title->modify_font(new PangoFontDescription("Times New Roman Italic 10"));
    $title->modify_fg(Gtk::STATE_NORMAL, GdkColor::parse("#0000ff"));
    $title->set_size_request(-1, 40);
    $vbox->pack_start($title, 0, 0);


    $field_labels = array("Username:", "Password:");
    $table = new GtkTable();
    $row = 0;
    $input = array();
    foreach ($field_labels as $field_label) {
        $label = new GtkLabel($field_label);
        $label->set_alignment(0,0);
        $table->attach($label, 0, 1, $row, $row+1);
        $input[$row] = new GtkEntry();
        $table->attach($input[$row], 1, 2, $row, $row+1);
        if (eregi("password", $field_label))
            $input[$row]->set_visibility(false);
        ++$row;
    }

    $vbox->pack_start($hbox = new GtkHBox(), 0);
    $vbox->pack_start(new GtkLabel());
    $hbox->pack_start(new GtkLabel());
    $hbox->pack_start($table, 0);
    $hbox->pack_start(new GtkLabel());

    $button_hbox = new GtkHBox();
    $button_ok = GtkButton::new_from_stock(Gtk::STOCK_OK);
    $button_ok->set_size_request(86, -1);
    $button_hbox->pack_start($button_ok, 0);
    $button_hbox->pack_start(new GtkLabel());
    $table->attach($button_hbox, 1, 2, $row, $row+1);
    ++$row;

    $status = new GtkLabel('    ');
    $table->attach($status, 1, 2, $row, $row+1);
    $button_ok->connect('clicked', 'on_button_ok', $dialog, $input, $status);

    $dialog->connect('key-press-event', 'on_key', $button_ok, $status);

    $dialog->set_has_separator(false);
    $dialog->show_all();
    $dialog->run();
    $dialog->destroy();
}

function on_button_ok($button, $dialog, $input, $status) {
    $username = $input[0]->get_text();
    $passwd = $input[1]->get_text();

    if ($username=='') {
        $input[0]->grab_focus();
        return true;
    }
    if ($passwd=='') {
        $input[1]->grab_focus();
        return true;
    }
    if ($username!='' && $passwd!='') {
        if (validate($username, $passwd)) {
            global $login_success;
            $login_success = 1;
            $dialog->destroy(); // note 6
        } else {
            $status->set_text('invalid username/password');
        }
    }
}

function validate($username, $password) { // note 7
    if ($username=='user1' && $password=='phpgtk2') {
        return true;
    } else {
        return false;
    }
}

function on_key($widget, $event, $button_ok, $status) {
    $status->set_text('');
    if ($event->keyval==Gdk::KEY_Return) {
        $button_ok->clicked();
        return true;
    } else {
        return false;
    }
}

?>
 

Explanation

  1. Loops until login is successful.
  2. Starts the main application only if login is successful
  3. Goes into fullscreen mode.
  4. This is the part that limits each user's usage to the time set by the constant MAX_USAGE.
  5. Puts the login dialog in fullscreen mode too.
  6. User will stay at the login screen until he/she enters the correct username/password.
  7. Put your validation code here.

Related Articles


Responses

  1. kksou:
    February 08, 2010 8:35pm

    Is this article useful?

    Found any bugs?

    Improvements for the above article?

    Please enter below...

Leave a Reply

Name:
Country: (optional)
Your Comments:
ANTI-SPAM key:
 

Search This Site

Google
Web This Site

Search PHP-GTK2 Manual

Full-text search on php-gtk2 manual

Members Login

Username:
Password:
Key:
What is this?
  Forget Password?