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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
|
# vim: set et ts=4 sw=4:
package ManaTools::MainDisplay;
#============================================================= -*-perl-*-
=head1 NAME
ManaTools::MainDisplay - class for ManaTools main window
=head1 SYNOPSIS
$mainDisplay = new ManaTools::MainDisplay();
$mainDisplay->start();
$mainDisplay->cleanup();
=head1 METHODS
=head1 DESCRIPTION
ManaTools::MainDisplay implements the main window panel adding buttons
reading the configuration for every categories and modules
=head1 SUPPORT
You can find documentation for this module with the perldoc command:
perldoc ManaTools::MainDisplay
=head1 AUTHOR
Steven Tucker
=head1 COPYRIGHT and LICENSE
Copyright (C) 2012-2015, Angelo Naselli.
Copyright (C) 2012, Steven Tucker.
ManaTools 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.
ManaTools 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 ManaTools. If not, see <http://www.gnu.org/licenses/>.
=head1 METHODS
=cut
use Moose;
extends qw( ManaTools::Module );
use I18N::LangTags::Detect;
use diagnostics;
use ManaTools::SettingsReader;
use ManaTools::ConfigReader;
use ManaTools::Category;
use ManaTools::Module;
use ManaTools::Shared;
use ManaTools::Shared::GUI;
use ManaTools::Shared::GUI::Dialog;
use ManaTools::Shared::Locales;
use File::ShareDir ':ALL';
use yui;
with 'ManaTools::LoggingRole';
has 'configDir' => (
is => 'ro',
isa => 'Str',
);
with 'ManaTools::ConfigDirRole';
#=============================================================
=head2 new
=head3 INPUT
hash ref containing
configDir: configuration files directory
name: application name, logging identity,
configuration subdirectory
=head3 other attributes
title: window title got from configuration file,
default is name.
categories: ArrayRef[ManaTools::Category]
settings: HashRef containing settings file content
currCategory: Selected category
mainWin: Main Dialog window
factory: yui::YUI::widgetFactory
menus: HashRef containing menu items
leftPane: left panel layout
rightPane: right panel layout
rightPaneFrame: right frame (needed for category title)
replacePoint: replace point where to set new layout on
category selection
selectedModule: module to be returned when selected
=head3 DESCRIPTION
This method instanziates the MainWindo object, and setups
the startup GUI.
=cut
#=============================================================
has '+name' => (
is => 'ro',
isa => 'Str',
default => 'mpan',
);
has '+icon' => (
is => 'rw',
isa => 'Str',
default => File::ShareDir::dist_file(ManaTools::Shared::distName(), 'images/mageia.png'),
);
has 'title' => (
is => 'rw',
isa => 'Str',
init_arg => undef,
lazy => 1,
builder => '_titleInitialize',
);
sub _titleInitialize {
my $self = shift;
return $self->name();
}
has 'settings' => (
is => 'rw',
isa => 'HashRef',
init_arg => undef,
default => sub {return {};},
);
has 'categories' => (
is => 'rw',
isa => 'ArrayRef[ManaTools::Category]',
init_arg => undef,
lazy => 1,
default => sub {[];},
);
has 'currCategory' => (
is => 'rw',
isa => 'Maybe[ManaTools::Category]',
init_arg => undef,
default => undef,
);
has 'factory' => (
is => 'ro',
isa => 'Maybe[yui::YWidgetFactory]',
lazy => 1,
init_arg => undef,
default => sub {
return yui::YUI::widgetFactory;
},
);
has 'mainWin' => (
is => 'rw',
isa => 'Maybe[ManaTools::Shared::GUI::Dialog]',
init_arg => undef,
default => undef,
);
has 'menus' => (
is => 'rw',
isa => 'HashRef',
init_arg => undef,
default => sub {
{
file => {},
help => {},
};
},
);
has 'leftPane' => (
is => 'rw',
isa => 'Maybe[yui::YLayoutBox]',
init_arg => undef,
default => undef,
);
has 'rightPane' => (
is => 'rw',
isa => 'Maybe[yui::YLayoutBox]',
init_arg => undef,
default => undef,
);
has 'rightPaneFrame' => (
is => 'rw',
isa => 'Maybe[yui::YFrame]',
init_arg => undef,
default => undef,
);
has 'replacePoint' => (
is => 'rw',
isa => 'Maybe[yui::YReplacePoint]',
init_arg => undef,
default => undef,
);
has 'selectedModule' => (
is => 'rw',
isa => 'Maybe[ManaTools::Module]',
init_arg => undef,
default => undef,
);
#=============================================================
=head2 configName
=head3 INPUT
$self: this object
=head3 OUTPUT
name: application name
=head3 DESCRIPTION
Returns the application name as configuration subdirectory.
This method is required by ConfifDirRole
=cut
#=============================================================
sub configName {
my $self = shift;
return $self->name();
}
#=============================================================
=head2 identifier
=head3 INPUT
$self: this object
=head3 OUTPUT
name: application name
=head3 DESCRIPTION
Returns the application name as logging identifier.
This method is required by LoggingRole
=cut
#=============================================================
sub identifier {
my $self = shift;
return $self->name();
}
sub _showAboutDialog {
my $self = shift;
my $translators = $self->{loc}->N("_: Translator(s) name(s) & email(s)\n");
$translators =~ s/\</\<\;/g;
$translators =~ s/\>/\>\;/g;
my $sh_gui = ManaTools::Shared::GUI->new();
$sh_gui->AboutDialog({ name => $self->{name},
version => $self->Version(),
credits => $self->loc()->N("Copyright (C) %s Mageia community", '2013-2015'),
license => $self->loc()->N("GPLv2"),
description => $self->loc()->N("mpan is the ManaTools panel that collects all the utilities."),
authors => $self->loc()->N("<h3>Developers</h3>
<ul><li>%s</li>
<li>%s</li>
</ul>
<h3>Translators</h3>
<ul><li>%s</li></ul>",
"Angelo Naselli <anaselli\@linux.it>",
"Matteo Pasotti <matteo.pasotti\@gmail.com>",
$translators
),
});
}
## Begin the program event loop
=head2 start
contains the main loop of the application
where we can check for events
=cut
sub start {
my $self = shift;
$self->_setupGui();
return $self->selectedModule();
}
#=============================================================
=head2 cleanup
=head3 INPUT
$self: this object
=head3 DESCRIPTION
This method cleanup data for a further start.
=cut
#=============================================================
sub cleanup {
my $self = shift;
$self->mainWin(undef);
$self->menus({
file => {},
help => {},
});
for (my $cat=0; $cat < scalar(@{$self->categories()}); $cat++ ) {
my $catSel = @{$self->categories()}[$cat];
$catSel->button(undef);
$catSel->removeButtons();
}
$self->leftPane(undef);
$self->rightPane(undef);
$self->rightPaneFrame(undef);
$self->replacePoint(undef);
}
#=============================================================
=head2 _setupGui
=head3 INPUT
$self: this object
=head3 DESCRIPTION
This method load configuration and build the GUI layout.
=cut
#=============================================================
sub _setupGui {
my $self = shift;
$self->selectedModule(undef);
# fill $self->settings from settings.conf
$self->_loadSettings();
$DB::single = 1;
$self->title($self->settings()->{title});
$self->icon($self->settings()->{icon}) if $self->settings()->{icon};
my $dialog = ManaTools::Shared::GUI::Dialog->new(
module => $self,
dialogType => ManaTools::Shared::GUI::Dialog::mainDialog,
title => $self->title(),
icon => $self->icon,
buttons => {
ManaTools::Shared::GUI::Dialog::aboutButton => sub {
my $event = shift; ## ManaTools::Shared::GUI::Event
my $self = $event->parentDialog()->module(); #this object
$self->_showAboutDialog();
return 1;
},
ManaTools::Shared::GUI::Dialog::closeButton => sub {return 0;},
},
layout => sub {
my $self = shift;
my $layoutstart = shift;
my $ydialog = $self->dialog();
my $module = $self->module();
my $info = $self->info();
my $factory = $self->factory();
my $optFactory = $self->optFactory();
my $mainLayout = $factory->createVBox($layoutstart);
my $menuLayout = $factory->createHBox($mainLayout);
## Menu File
my $align = $factory->createAlignment($menuLayout, 1, 0);
$module->menus()->{file} = {
widget => $factory->createMenuButton($align, $self->loc()->N("File")),
quit => new yui::YMenuItem($self->loc()->N("&Quit")),
};
my @ordered_menu_lines = qw(quit);
foreach (@ordered_menu_lines) {
$module->menus()->{file}->{widget}->addItem($module->menus()->{file}->{ $_ });
}
$module->menus()->{file}->{ widget }->rebuildMenuTree();
$align = $factory->createAlignment($menuLayout, 2, 0);
$module->menus()->{help} = {
widget => $factory->createMenuButton($align, $self->loc()->N("Help")),
help => new yui::YMenuItem($self->loc()->N("Help")),
about => new yui::YMenuItem($self->loc()->N("&About")),
};
## Menu Help
@ordered_menu_lines = qw(help about);
foreach (@ordered_menu_lines) {
$module->menus()->{help}->{ widget }->addItem($module->menus()->{help}->{ $_ });
}
$module->menus()->{help}->{ widget }->rebuildMenuTree();
ManaTools::Shared::GUI::Event->new(
name => 'AboutMenuEvent',
eventHandler => $self,
eventType => $yui::YEvent::MenuEvent,
item => $module->menus()->{help}->{about},
event => sub {
my $event = shift;
my $dialog = $event->parentDialog();
my $self = $dialog->module(); #this object
$self->_showAboutDialog();
return 1;
},
);
ManaTools::Shared::GUI::Event->new(
name => 'QuitMenuEvent',
eventHandler => $self,
eventType => $yui::YEvent::MenuEvent,
item => $module->menus()->{file}->{quit},
event => sub {
return 0;
},
);
my $layout = $factory->createHBox($mainLayout);
#create left Panel Frame no need to add a label for title
my $leftPaneFrame = $factory->createFrame($layout, $module->settings()->{category_title});
#create right Panel Frame no need to add a label for title (use setLabel when module changes)
my $rightPaneFrame = $factory->createFrame($layout, "");
#create replace point for dynamically created widgets
$module->replacePoint($factory->createReplacePoint($rightPaneFrame));
$module->rightPane($factory->createVBox($module->replacePoint()));
$module->leftPane($factory->createVBox($leftPaneFrame));
$module->rightPaneFrame($rightPaneFrame);
#logo from settings
my $logofile = defined($module->settings()->{logo}) ?
$module->settings()->{logo} :
File::ShareDir::dist_file(ManaTools::Shared::distName(), 'images/logo_mageia.png'
);
my $logo = $factory->createImage($module->leftPane(), $logofile);
$logo->setAutoScale(1);
#$leftPaneFrame->setWeight(0, 1);
$rightPaneFrame->setWeight(0, 2);
$module->_loadCategories();
$factory->createVStretch($module->leftPane());
my $closeButton = $self->widget('closeButton');
my $quitIcon = File::ShareDir::dist_file(ManaTools::Shared::distName(), 'images/quit.png');
$closeButton->setIcon($quitIcon);
if (!$module->currCategory()) {
$module->currCategory(@{$module->categories()}[0]);
}
$module->currCategory()->addButtons($module);
$module->rightPaneFrame()->setLabel($module->currCategory()->name());
$factory->createSpacing($module->rightPane(), 1, 1, 1.0 );
# Note that Since mpan is a Manatools::Module and creates other modules
# title and application are set into BUILD (e.g. constructor) so last
# built one is the one shown. Forcing setting again here
yui::YUI::app()->setApplicationTitle($self->title);
yui::YUI::app()->setApplicationIcon($self->icon);
return $self->widget('layout');
},
);
$self->mainWin($dialog);
return $dialog->call();
}
## internal methods
## Check if event is from current Category View
## If icon click, returns the module to be launched
sub _moduleSelected {
my ($self, $selectedWidget) = @_;
for(@{$self->currCategory()->modules()}) {
if( $_->button() == $selectedWidget ){
return $_;
}
}
return 0;
}
## Discover if a category button was selected.
## If category button is selected, sets right panel to display
## the selected Category Modules
## returns 1 if category button is selected
sub _categorySelected {
my ($self, $selectedWidget) = @_;
for (@{$self->categories()}) {
if( $_->button() == $selectedWidget ) {
#if current is already set then skips
if ($self->currCategory() == $_) {
## returns 1 to skip any other checks on
## the selected widget
return 1;
}
## Menu item selected, set right pane
my $ydialog = $self->mainWin()->dialog();
$ydialog->startMultipleChanges();
## NOTE widget and item events (Shared::GUI:Event) created in the
## previous replacePoint MUST be cleaned up
my $currCategory = $self->currCategory();
## Change Current Category to the selected one
$self->currCategory($_);
foreach my $mod (@{$currCategory->modules()}) {
$self->mainWin()->delWidget(
$self->mainWin()->widget($mod->name())
);
}
## Remove existing modules
$self->replacePoint()->deleteChildren();
$self->rightPane($self->factory()->createVBox($self->replacePoint()));
## Add new Module Buttons to Right Pane
$self->currCategory()->addButtons($self);
$self->rightPaneFrame()->setLabel($self->currCategory()->name());
$self->factory()->createSpacing($self->rightPane(), 1, 1, 1.0 );
$self->replacePoint()->showChild();
$ydialog->recalcLayout();
$ydialog->doneMultipleChanges();
return 1;
}
}
return 0;
}
## mpan settings
sub _loadSettings {
my ($self, $force_load) = @_;
# configuration file name
my $fileName = $self->configPathName() . "/settings.conf";
if (! -e $fileName) {
my $err = $self->loc()->N("Configuration file %s is missing", $fileName);
$self->logger()->E($err);
die $err;
}
$self->logger()->I($self->loc()->N("Reading configuration file %s", $fileName));
if (! scalar %{$self->settings()} || $force_load) {
my $settingsReader = ManaTools::SettingsReader->new({fileName => $fileName});
my $settings;
my @lang = I18N::LangTags::Detect::detect();
my $read = $settingsReader->settings();
foreach (keys %{$read}) {
my $key = $_;
# localized strings
if ($key eq "title" or $key eq "category_title") {
# default is en
$settings->{$key} = $read->{$key}->{en};
foreach my $l ( @lang ) {
if ($read->{$key}->{$l}) {
$settings->{$key} = $read->{$key}->{$l};
last;
}
}
$self->logger()->I($self->loc()->N("%s content is <<%s>>", $key, $settings->{$key}));
}
else {
$settings->{$key} = $read->{$key};
}
}
$self->settings($settings);
}
}
#=============================================================
# _categoryLoaded
#
# INPUT
#
# $self: this object
# $category: category to look for
#
# OUTPUT
#
# $present: category is present or not
#
# DESCRIPTION
#
# This method looks for the given category and if already in
# returns true.
#
#=============================================================
sub _categoryLoaded {
my ($self, $category) = @_;
my $present = 0;
if (!$category) {
return $present;
}
foreach my $cat (@{$self->categories()}) {
if ($cat->name() eq $category->name()) {
$present = 1;
last;
}
}
return $present;
}
#=============================================================
# _getCategory
#
# INPUT
#
# $self: this object
# $name: category name
#
# OUTPUT
#
# $category: category object if exists
#
# DESCRIPTION
#
# This method looks for the given category name and returns
# the realte object.
#=============================================================
sub _getCategory {
my ($self, $name) = @_;
my $category = undef;
foreach $category (@{$self->categories()}) {
if ($category->name() eq $name) {
return $category;
}
}
return $category;
}
# _loadCategory
#
# creates a new button representing a category
#
sub _loadCategory {
my ($self, $category) = @_;
if (!$self->_categoryLoaded($category)) {
push ( @{$self->categories()}, $category );
my $cat = @{$self->categories()}[-1];
$cat->button(
$self->factory()->createPushButton(
$self->leftPane(),
$cat->name()
)
);
$cat->setIcon();
$cat->button()->setStretchable(0, 1);
$self->mainWin()->addWidget(
$cat->name(),
$cat->button(), sub {
my $event = shift; ## ManaTools::Shared::GUI::Event
my $self = $event->parentDialog()->module(); #this object
$self->_categorySelected($event->widget());
return 1;
}
);
}
else {
for (my $cat=0; $cat < scalar(@{$self->categories()}); $cat++ ) {
my $catSelected = @{$self->categories()}[$cat];
if( $catSelected->name() eq $category->name() &&
!$catSelected->button()) {
$catSelected->button(
$self->factory()->createPushButton(
$self->leftPane(),
$catSelected->name()
)
);
$catSelected->setIcon();
$catSelected->button()->setStretchable(0, 1);
$self->mainWin()->addWidget(
$catSelected->name(),
$catSelected->button(), sub {
my $event = shift; ## ManaTools::Shared::GUI::Event
my $self = $event->parentDialog()->module(); #this object
$self->_categorySelected($event->widget());
return 1;
}
);
last;
}
}
}
}
sub _loadCategories {
my $self = shift;
# category files
my @categoryFiles;
my $fileName = $self->configPathName() . "/categories.conf";
# configuration file dir
my $directory = $self->configPathName() . "/categories.conf.d";
push(@categoryFiles, $fileName);
push(@categoryFiles, <$directory/*.conf>);
my $currCategory;
foreach $fileName (@categoryFiles) {
$self->logger()->I($self->loc()->N("Parsing category file %s", $fileName));
my $inFile = new ManaTools::ConfigReader({fileName => $fileName});
my $tmpCat;
my $tmp;
my $hasNextCat = $inFile->hasNextCat();
while( $hasNextCat ) {
$tmp = $inFile->getNextCat();
$tmpCat = $self->_getCategory($tmp->{title});
if (!$tmpCat) {
$tmpCat = new ManaTools::Category({
name => $tmp->{title},
icon => $tmp->{icon}
});
}
$self->_loadCategory($tmpCat);
$hasNextCat = $inFile->hasNextCat();
$currCategory = $tmpCat;
my $hasNextMod = $inFile->hasNextMod();
while( $hasNextMod ) {
$tmp = $inFile->getNextMod();
my $tmpMod;
my $loaded = 0;
if (exists $tmp->{title}) {
if (not $currCategory->moduleLoaded($tmp->{title})) {
$tmpMod = ManaTools::Module->create(name => $tmp->{title},
icon => $tmp->{icon},
launch => $tmp->{launcher}
);
}
}
elsif (exists $tmp->{class}) {
if (not $currCategory->moduleLoaded(-CLASS => $tmp->{class})) {
$tmpMod = ManaTools::Module->create(-CLASS => $tmp->{class});
}
}
if ($tmpMod) {
$loaded = $currCategory->loadModule($tmpMod);
undef $tmpMod if !$loaded;
}
$hasNextMod = $inFile->hasNextMod();
}
}
}
}
no Moose;
1;
|