aboutsummaryrefslogtreecommitdiffstats
path: root/MainDisplay.pm
blob: efdfbd15171574801c6f619b3eb42b9ab30df2aa (plain)
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# vim: set et ts=4 sw=4:
#    Copyright 2012 Steven Tucker
#
#    This file is part of AdminPanel
#
#    AdminPanel is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 2 of the License, or
#    (at your option) any later version.
#
#    AdminPanel is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with AdminPanel.  If not, see <http://www.gnu.org/licenses/>.


#Class MainDisplay
package MainDisplay;

use strict;
use warnings;
use diagnostics;
use SettingsReader;
use ConfigReader;
use Category;
use Module;
use Data::Dumper;
use yui;

sub new {

    my $self = {
        my $categories = 0,
        my $event = 0,
        my $factory = 0,
        my $mainWin = 0,
        my $mainLayout = 0,
        my $menuLayout = 0,
        my $menus = {
            my $file = 0,
            my $help = 0
        },
        my $layout = 0,
        my $leftPane = 0,
        my $rightPane = 0,
        my $currCategory = 0,
        my $confDir = 0,
        my $title   = 0,
        my $settings = 0,
        my $exitButton = 0,
#        my $justToGetRidOfERROR = 0,
        my $replacePoint = 0
    };
    bless $self, 'MainDisplay';
    
## Default values
    $self->{name} =     "Administration panel";
    $self->{categories} = [];
    $self->{confDir}    = "/etc/apanel",
    $self->{title}      = "apanel",
    $self->setupGui();
    
    return $self;
}

## Begin the program event loop
sub start {
    my ($self) = shift;
    my $reqExit = 0;

    ##Default category selection
    $self->{currCategory} = @{$self->{categories}}[0]; 
    $self->{currCategory}->addButtons($self->{rightPane}, $self->{factory});
    $self->{rightPaneFrame}->setLabel($self->{currCategory}->{name});
    $self->{factory}->createSpacing($self->{rightPane}, 1, 1, 1.0 );

    my $launch = 0;
    while(!$launch) {

        ## Grab Event
        $self->{event} = $self->{mainWin}->waitForEvent();

        ## Check for window close
        if ($self->{event}->eventType() == $yui::YEvent::CancelEvent)
        {
            last;
        }

## why i can't test item() with $self->{menus}->{file}[0]?
        ## Check for Exit button push or menu
        if($self->{event}->widget() == $self->{exitButton} || 
           ($self->{event}->item() && ($self->{event}->item()->label() eq $self->{menus}->{file}[0]->label() ))) {
            last;
        };

        ## Discover if a menu button was selected.
        ## If menu button selected, set right panel to display
        ## selected Category Modules
        for(@{$self->{categories}}){
            if( $_->{button} == $self->{event}->widget() ){
                ## Menu item selected, set right pane
                $self->{mainWin}->startMultipleChanges();
                ## Remove existing modules
                $self->{replacePoint}->deleteChildren();
                $self->{rightPane} = $self->{factory}->createVBox($self->{replacePoint});

                ## Change Current Category to the selected one
                $self->{currCategory} = $_;
                ## Add new Module Buttons to Right Pane
                $self->{currCategory}->addButtons($self->{rightPane}, $self->{factory});
                $self->{rightPaneFrame}->setLabel($self->{currCategory}->{name});
                $self->{factory}->createSpacing($self->{rightPane}, 1, 1, 1.0 );
                $self->{replacePoint}->showChild();
                $self->{mainWin}->recalcLayout();
                $self->{mainWin}->doneMultipleChanges();

                last;
            }
        }

        ## Check if event is from current Category View
        ## If icon click, launch the Module
        for(@{$self->{currCategory}->{modules}}) {
            if( $_->{button} == $self->{event}->widget() ){
                $launch = $_->{launch};
                last;
            }
        }
    }

    return $launch;
}

sub destroy {
    my ($self) = shift;
    $self->{mainWin}->destroy();
}

sub setupGui {
    my ($self) = shift;

    my $cmdline = new yui::YCommandLine;

    ## TODO add parameter check
    my $pos       = $cmdline->find("--name");
    if ($pos > 0)
    {
        $self->{title} = $cmdline->arg($pos+1);
    }
    $pos       = $cmdline->find("--conf_dir");
    if ($pos > 0)
    {
        $self->{confDir} = $cmdline->arg($pos+1);
    }
    else
    {
        $self->{confDir} = "/etc/$self->{title}";
    }
#     print "name     = ".$self->{title}."\n";
#     print "conf dir = ".$self->{confDir}."\n";
    
    $self->loadSettings();
    yui::YUILog::setLogFileName($self->{settings}->{log});
    $self->{name} = $self->{settings}->{title};
    yui::YUI::app()->setApplicationTitle($self->{name});
    yui::YUI::app()->setApplicationIcon($self->{settings}->{icon});

    $self->{factory} = yui::YUI::widgetFactory;
    $self->{mainWin} = $self->{factory}->createMainDialog;
#print "Title:  ".yui::YUI::app()->applicationTitle()."\n";

    $self->{mainLayout} = $self->{factory}->createVBox($self->{mainWin});
    $self->{menuLayout} = $self->{factory}->createHBox($self->{mainLayout});
   
    ## Menu file
    ## TODO i8n 
    my $align = $self->{factory}->createAlignment($self->{menuLayout}, 1, 0);
    my $menu            = $self->{factory}->createMenuButton($align, "File");
    my $item = new yui::YMenuItem("Exit");

    push(@{$self->{menus}->{file}}, $item);
    $menu->addItem($item);
    $menu->rebuildMenuTree();

    $align = $self->{factory}->createAlignment($self->{menuLayout}, 2, 0);
    $menu           = $self->{factory}->createMenuButton($align, "Help");
    $item = new yui::YMenuItem("Help");
    $menu->addItem($item);
    push(@{$self->{menus}->{help}}, $item);
    $item = new yui::YMenuItem("About");
    $menu->addItem($item);
    push(@{$self->{menus}->{help}}, $item);
    $menu->rebuildMenuTree();

    $self->{layout}     = $self->{factory}->createHBox($self->{mainLayout});

    #create left Panel Frame no need to add a label for title 
    $self->{leftPaneFrame} = $self->{factory}->createFrame($self->{layout}, $self->{settings}->{category_title});
    #create right Panel Frame no need to add a label for title (use setLabel when module changes) 
    $self->{rightPaneFrame} = $self->{factory}->createFrame($self->{layout}, "");
    #create replace point for dynamically created widgets
    $self->{replacePoint} = $self->{factory}->createReplacePoint($self->{rightPaneFrame});

    $self->{rightPane} = $self->{factory}->createVBox($self->{replacePoint});
    $self->{leftPane} = $self->{factory}->createVBox($self->{leftPaneFrame});

    #logo from settings
    my $logo = $self->{factory}->createImage($self->{leftPane}, $self->{settings}->{logo});
    $logo->setAutoScale(1);

    $self->{leftPaneFrame}->setWeight(0, 25);
    $self->{rightPaneFrame}->setWeight(0, 75);

    $self->loadCategories();
    $self->{factory}->createVStretch($self->{leftPane});

    $self->{exitButton} = $self->{factory}->createPushButton($self->{leftPane}, "Exit");
    $self->{exitButton}->setIcon("$self->{settings}->{images_dir}/quit.png");    
    $self->{exitButton}->setStretchable(0, 1);
#     $self->{exitButton}->setStretchable(1, 1);
}

## adpanel settings
sub loadSettings {
    my ($self) = @_;
# configuration file name
    my $fileName = "$self->{confDir}/settings.conf";
    $self->{settings} = new SettingsReader($fileName);
}

sub loadCategory {
    my ($self, $category) = @_;

    push ( @{$self->{categories}}, $category );

    @{$self->{categories}}[-1]->{button} = $self->{factory}->createPushButton(
                                                                $self->{leftPane},
                                                                $self->{categories}[-1]->{name}
                                                                );
    @{$self->{categories}}[-1]->setIcon();

    @{$self->{categories}}[-1]->{button}->setStretchable(0, 1);
#     @{$self->{categories}}[-1]->{button}->setStretchable(1, 1);
}

sub loadCategories {
    my ($self) = @_;

# configuration file name
    my $fileName = "$self->{confDir}/categories.conf";

    my $inFile = new ConfigReader($fileName);
    my $tmpCat;
    my $tmp;
    my $hasNextCat = $inFile->hasNextCat();
    while( $hasNextCat ) {
        $tmp = $inFile->getNextCat();
        $tmpCat = new Category($tmp->{title}, $tmp->{icon});
        $self->loadCategory($tmpCat);
        $hasNextCat = $inFile->hasNextCat();
        $self->{currCategory} = $tmpCat;
    
        my $hasNextMod = $inFile->hasNextMod();
        while( $hasNextMod ) {
            $tmp = $inFile->getNextMod();
            my $tmpMod = new Module($tmp->{title}, 
                                    $tmp->{icon},
                                    $tmp->{launcher}
                                   );
            $self->{currCategory}->loadModule($tmpMod);

            $hasNextMod = $inFile->hasNextMod();
        }
    }
    undef($tmpCat);
}

sub menuEventIndex {
    my ($self) = shift;
    
    my $index = -1;

    for(my $i = 0; $i < scalar(@{$self->{categories}} ); ++$i)
    {
        print "Current Index = ".$index."\n";
        if($self->{event}->widget() == @{$self->{categories}}[$i]->{button})
        {
            $index = $i;
            print "Index found is : ".$index;
            last;
        }
    }
    return $index;
}

1;

=pod

=head1 NAME

       MainDisplay - class for the main window

=head1 SYNOPSIS

       $mainDisplay = new MainDisplay();

=head1 METHODS

=head2 start

       contains the main loop of the application
              where we can check for events

=head2 setupGui

       creates a popupDialog using a YUI::WidgetFactory
              and then populate this dialog with some components

=head2 loadCategory

       creates a new button representing a category

=head3 category (String)

=cut