source: extensions/pLoader/trunk/src/Uploader/GUI/wxApp.pm @ 4632

Last change on this file since 4632 was 4632, checked in by ronosman, 14 years ago

Feature 1369 added : no longer use relative path to access resource and locale directories.

  • Property svn:eol-style set to LF
File size: 27.4 KB
Line 
1# +-----------------------------------------------------------------------+
2# | pLoader - a Perl photo uploader for Piwigo                            |
3# +-----------------------------------------------------------------------+
4# | Copyright(C) 2008      Piwigo Team                  http://piwigo.org |
5# +-----------------------------------------------------------------------+
6# | This program is free software; you can redistribute it and/or modify  |
7# | it under the terms of the GNU General Public License as published by  |
8# | the Free Software Foundation                                          |
9# |                                                                       |
10# | This program is distributed in the hope that it will be useful, but   |
11# | WITHOUT ANY WARRANTY; without even the implied warranty of            |
12# | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
13# | General Public License for more details.                              |
14# |                                                                       |
15# | You should have received a copy of the GNU General Public License     |
16# | along with this program; if not, write to the Free Software           |
17# | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
18# | USA.                                                                  |
19# +-----------------------------------------------------------------------+
20package Uploader::GUI::wxApp;
21use strict;
22use base qw/Wx::App Class::Accessor::Fast/;   
23use Wx qw/
24             wxBITMAP_TYPE_GIF
25             wxBITMAP_TYPE_ICO
26             wxBITMAP_TYPE_BMP
27             wxBITMAP_TYPE_PNG
28             wxBITMAP_TYPE_JPEG
29             wxIMAGE_QUALITY_NORMAL
30             wxIMAGE_QUALITY_HIGH
31             wxSPLASH_CENTRE_ON_SCREEN
32             wxSPLASH_TIMEOUT
33             wxDefaultPosition
34             wxDefaultSize
35             wxSIMPLE_BORDER
36             wxFRAME_TOOL_WINDOW
37             wxFRAME_NO_TASKBAR wxSTAY_ON_TOP
38             wxWHITE
39             wxICON_EXCLAMATION
40             wxOK
41             wxLANGUAGE_CHINESE_SIMPLIFIED   
42             wxLANGUAGE_CZECH   
43             wxLANGUAGE_DANISH   
44             wxLANGUAGE_DUTCH   
45             wxLANGUAGE_ENGLISH   
46             wxLANGUAGE_FRENCH   
47             wxLANGUAGE_GERMAN   
48             wxLANGUAGE_HUNGARIAN
49             wxLANGUAGE_ITALIAN   
50             wxLANGUAGE_JAPANESE   
51             wxLANGUAGE_POLISH   
52             wxLANGUAGE_PORTUGUESE   
53             wxLANGUAGE_PORTUGUESE_BRAZILIAN   
54             wxLANGUAGE_RUSSIAN   
55             wxLANGUAGE_SLOVAK   
56             wxLANGUAGE_SPANISH   
57         /;
58use Uploader::GUI::wxFrameAUI;
59use File::Slurp ;
60use Data::Dumper;
61use Storable;
62use File::HomeDir;
63use File::Spec;
64use Uploader::PWG;
65use Uploader::ImageList;
66use Uploader::GUI::wxLoginDlg;
67use Wx::Locale qw/:default/;
68use utf8;
69
70$|=1;
71
72__PACKAGE__->mk_accessors( 
73    qw/
74          pwg
75          site_url
76          site_username
77          site_password
78          http_username
79          http_password
80          rejects
81          imagelist
82          storable_file
83          wx_thumb_dir
84          resized_dir
85          userdata_dir
86          resized_dir
87          thumb_dir
88          conf_file
89          layout_file
90          locale
91          current_language
92          languages
93          available_languages
94          version
95          imagelist_version
96          use_offline
97          login_dlg
98          branding
99          frame
100          perspective
101          imageviewerIndex
102          frameLayout
103          chunk_size
104          layout_clean
105          colors
106          eng_colors
107          positions
108          eng_positions
109          default_photo_names
110          eng_default_photo_names
111          upload_hd
112          eng_upload_hd
113          privacy_level
114          user_defined_properties
115          bin_dir
116          resource_dir
117          locale_dir
118      / 
119);
120
121sub FilterEvent {
122    my( $self, $event ) = @_;
123   
124    Wx::LogMessage "EventType %s", $event->GetEventType();
125   
126    return -1;
127}
128
129# not very clean but :
130# at that moment, object is not yet instanciated
131# only way : class variable
132my $bin_dir;
133my $resource_dir;
134my $locale_dir;
135sub new {
136    my ( $self, $params ) = @_;
137
138    $bin_dir = $params->{bin_dir}||'.';
139    $locale_dir = $params->{locale_dir}||'.';
140    $resource_dir = $params->{resource_dir}||'.';
141
142    $self->SUPER::new();
143}
144
145sub OnInit {
146    my( $self ) = @_;
147
148    $self->bin_dir(
149        $bin_dir
150    );
151
152    $self->locale_dir(
153        $locale_dir
154    );
155
156    $self->resource_dir(
157        $resource_dir
158    );
159
160    $self->version(
161        '2.0.7b'
162    );
163   
164    # to check if we can use stored cache
165    $self->imagelist_version(
166        '11'
167    );
168
169    $self->languages(
170      [
171             ["中文 (%s)", wxLANGUAGE_CHINESE_SIMPLIFIED, 'Chinese simplified'],   
172             ["Česky (%s)", wxLANGUAGE_CZECH, 'Czech'],   
173             ["Dansk (%s)", wxLANGUAGE_DANISH, 'Danish'],   
174             ["Deutsch (%s)", wxLANGUAGE_GERMAN, 'German'],   
175             ["English (%s)", wxLANGUAGE_ENGLISH, 'English'],   
176             ["Español (%s)", wxLANGUAGE_SPANISH, 'Spanish'],   
177             ["Français (%s)", wxLANGUAGE_FRENCH, 'French'],   
178             ["Italiano (%s)", wxLANGUAGE_ITALIAN, 'Italian'],   
179             ["日本語 (にほんご) (%s)", wxLANGUAGE_JAPANESE, 'Japanese'],   
180             ["Magyar (%s)", wxLANGUAGE_HUNGARIAN, 'Hungarian'],
181             ["Nederlands (%s)", wxLANGUAGE_DUTCH, 'Dutch'],   
182             ["Polski (%s)", wxLANGUAGE_POLISH, 'Polish'],   
183             ["Português Brasileiro (%s)", wxLANGUAGE_PORTUGUESE_BRAZILIAN, 'Portuguese Brazil'],   
184             ["Português Portugal (%s)", wxLANGUAGE_PORTUGUESE, 'Portuguese Portugal'],   
185             ["Русский (%s)", wxLANGUAGE_RUSSIAN, 'Russian'],
186             ["Slovenčina (%s)", wxLANGUAGE_SLOVAK, 'Slovak'],
187      ]
188    );
189    # some languages may be unavailable due to system configuration.
190    $self->_filter_available_languages;
191    Wx::InitAllImageHandlers();
192    my $applicationName = "pLoader" ;
193    $self->SetAppName( $applicationName );
194    $self->SetVendorName( "Piwigo Team" );
195
196    $self->{IMGTYPE} = {
197        'jpg' => wxBITMAP_TYPE_JPEG,
198        'gif' => wxBITMAP_TYPE_GIF,
199        'png' => wxBITMAP_TYPE_PNG,
200    };
201
202    $self->_init_userdir;
203
204    my $conf = retrieve $self->conf_file if -e $self->conf_file;       
205
206    if(defined $conf ){
207        $self->SetKeyValues($conf);
208    }
209    else {
210        $self->_readParams( $self->bin_path('pLoader.ini') ) unless defined $conf ;
211    }
212
213    $self->_read_default_user_properties( $self->bin_path('properties.ini') ) ;
214
215    $self->site_url(
216        $self->{site_url}
217    );
218   
219
220    $self->site_username(
221        $self->{site_username}
222    );
223    $self->site_password(
224        $self->{site_password}
225    );
226
227    $self->http_username(
228        $self->{http_username}
229    );
230    $self->http_password(
231        $self->{http_password}
232    );
233
234
235    $self->current_language(
236        $self->{current_language}||Wx::Locale::GetSystemLanguage()
237    );
238
239    $self->chunk_size(
240        $self->{chunk_size}||500_000
241    );
242   
243    $self->init_locale;
244    $self->_init_localized_properties;
245
246    my $not_exit = $self->Login();
247    # user pressed OK
248    if($not_exit){
249        $self->StoreConnectionProperties;
250        if( !$self->use_offline ){
251            while( $not_exit and !$self->_is_connected ){
252                $not_exit = $self->Login();
253                last if $self->use_offline;
254            }
255        }
256        $self->_init_imagelist;
257        $self->_init_thumbimagelist;
258        $self->_set_stored_layout;
259        $self->_init_frame;
260    }
261
262    $not_exit;
263}
264
265
266# helper method to get the full path for a resource
267sub resource_path{
268    my ( $self, $file ) = @_;
269
270    File::Spec->catfile($self->bin_dir, '..', $self->resource_dir, $file);
271}
272
273sub bin_path{
274    my ( $self, $file ) = @_;
275
276    File::Spec->catfile($self->bin_dir, $file);
277}
278
279sub locale_path{
280    my ( $self, $file ) = @_;
281
282    File::Spec->catfile($self->bin_dir, '..', $self->locale_dir, $file);
283}
284
285sub locale_catalog_path{
286    my ( $self, $file ) = @_;
287
288    File::Spec->catfile($self->bin_dir, '..', $self->locale_dir, $self->locale->GetCanonicalName, $file);
289}
290
291sub _set_stored_layout {
292    my ( $self ) = @_;
293
294
295    # when language is switched, need a new layout
296    unlink $self->layout_file if $self->layout_clean;
297    $self->layout_clean(0);
298
299    my $layout = retrieve $self->layout_file if -e $self->layout_file; 
300    if(defined $layout ){
301        $self->SetKeyValues($layout);
302    }
303
304}
305
306# some properties are displayed in choice list, with translated strings
307# the translation has to be reverted to store a language independant value
308sub _init_localized_properties {
309    my ( $self ) = @_;
310
311    $self->colors(
312        ['Black', 'White']
313    );
314    # We need to translate back to english when we store properties
315    $self->eng_colors(
316        {
317            map { gettext($_) => $_ } @{$self->colors} 
318        }
319    );
320    $self->positions(
321        [
322            'Top',
323            'Left',
324            'Right',
325            'Bottom',
326            'Top left',
327            'Top right',
328            'Bottom left',
329            'Bottom right',
330            'Center',
331        ]
332    );
333
334    $self->eng_positions(
335        { 
336             map { gettext($_) => $_ } @{$self->positions} 
337        }
338    );
339       
340        $self->upload_hd(
341            [
342                    'No',
343                        'Yes, use HD resized of the original photo',
344                        'Yes, use a copy of the original photo',
345                ]
346        );
347
348    $self->eng_upload_hd(
349        {
350             map { gettext($_) => $_ } @{$self->upload_hd} 
351        }
352    );
353       
354    $self->default_photo_names(
355        [
356             'None',
357             'File name',
358             'File path and name',
359             'Prefix',
360             'Prefix + rank number',
361             'Rank number + prefix',
362             'Prefix + create date chrono',
363             'Create date chrono + prefix',
364        ]
365    );
366    $self->eng_default_photo_names(
367        {
368             map { gettext($_) => $_ } @{$self->default_photo_names} 
369        }
370    );
371       
372        # hard coded because the piwigo api to read custom privacy level is not yet available
373        $self->privacy_level(
374            [
375                    'everybody',
376                        'contacts',
377                        'friends',
378                        'family',
379                        'admins'
380                ]
381        );
382
383       
384}
385
386# display privacy level list in a pyramid way :
387# ['everybody', 'contacts', friends, family, admins] -> [everybody, 'contacts, friends, family, admins', 'friends, family, admins', 'family, admins', 'admins only' ]
388sub privacy_level_choices{
389    my ( $self ) = @_;
390
391    my $pl = $self->privacy_level;     
392    my $n = scalar @$pl - 1;
393    my $list = [ gettext($pl->[0]) ];
394    my $i=0;
395    while(++$i<$n){   
396        push @$list, join(
397                    ', ', 
398                        map{ gettext($_) }
399                        @$pl[$i..$n] 
400                );
401    }
402    push @$list, gettext($pl->[$n]);
403       
404        $list;
405}
406
407sub _filter_available_languages {
408    my ( $self ) = @_;
409
410    # check if the locale can be set and the translation catalog available
411    $self->available_languages(
412        [
413            grep {$_} 
414            map{
415                            #  a locale may be unavailable due to system limitations ( ex: chinese, japanese when language pack are not installed )
416                            if(Wx::Locale::IsAvailable($_->[1])){
417                            my $locale = Wx::Locale->new($_->[1]);
418                            $locale->AddCatalogLookupPathPrefix(
419                        File::Spec->catfile($self->bin_dir, '..', $self->locale_dir)
420                    );
421                            $_ if $locale->AddCatalog('pLoader');
422                                }
423            }
424            @{$self->languages}
425        ]
426    );
427}
428
429sub _is_connected {
430    my ( $self ) = @_;
431
432    my $is_connected;
433
434    if($self->pwg->login_result->{stat} eq 'ok'){
435        $is_connected = 1;
436    }
437    else{
438        Wx::MessageBox( 
439            sprintf(
440                "%s\n\n%s %s %s",
441                $self->pwg->login_result->{message},
442                gettext("Connection to"),
443                $self->site_url,
444                gettext("failed"),
445            ),
446            gettext("Piwigo login error"),
447            wxOK | wxICON_EXCLAMATION, 
448        );
449    }
450   
451    $is_connected;
452}
453
454
455my $locale;
456sub init_locale {
457    my ( $self, $language ) = @_;
458   
459    $self->current_language(
460        $language
461    ) if defined $language;
462
463    undef $locale;
464    $locale = Wx::Locale->new(
465        $self->current_language
466    );
467    $locale->AddCatalogLookupPathPrefix(
468        File::Spec->catfile($self->bin_dir, '..', $self->locale_dir)
469    );
470    if(!$locale->AddCatalog( 'pLoader.mo' )){
471        Wx::LogMessage gettext("Cannot find translation catalog files for %s. Use default language"), $locale->GetCanonicalName();
472    }
473    $self->locale($locale);     
474}
475
476sub StoreConnectionProperties {
477    my ( $self ) = @_;
478
479    eval {   
480        store( 
481            {
482                map{
483                   $_ => $self->{$_},
484                }
485                qw/
486                    site_url
487                    site_username
488                    site_password
489                    http_username
490                    http_password
491                    current_language
492                    chunk_size
493                    layout_clean
494                /
495            },
496            $self->conf_file
497        );
498    };
499}
500
501sub StoreLayoutProperties {
502    my ( $self ) = @_;
503
504    eval {   
505        store( 
506            {
507                map{
508                   $_ => $self->{$_},
509                }
510                qw/
511                      perspective
512                      imageviewerIndex
513                      frameLayout
514                /
515            },
516            $self->layout_file
517        );
518    };
519}
520
521sub _init_imagelist {
522    my ( $self ) = @_; 
523
524    my $stored_imagelist;
525   
526    my $use_new_imagelist;
527   
528    if( -e $self->storable_file ){
529        eval {
530            $stored_imagelist = retrieve $self->storable_file;
531        };
532        if($@){
533            Wx::LogMessage(
534                gettext("An error has occured. Can not read %s\n%s"),
535                $self->storable_file, 
536                $@
537            );
538            $use_new_imagelist = 1 ;
539        }
540        # should have a valid imagelist
541        else{
542            $use_new_imagelist = 1 unless $self->imagelist_version eq $stored_imagelist->{imagelist_version};
543            if($use_new_imagelist){
544                Wx::LogMessage(gettext("pLoader has to reset image cache."));
545                # and silently clean layout
546                $self->layout_clean(1);
547            }
548        }
549    }
550    else{
551        $use_new_imagelist = 1 ;
552    }
553
554    if($use_new_imagelist){
555        $stored_imagelist = $self->_default_imagelist_params ;
556    }
557
558
559    $self->imagelist(
560        Uploader::ImageList->new(
561            $stored_imagelist
562        )
563    );
564
565    $self->imagelist->SetCurrentImage(-1);
566
567    $self->imagelist->RescaleCallback(
568        sub { $self->RescaleImage(@_) }
569    );
570
571    $self->imagelist->ResizeCallback(
572        sub { $self->ResizeImage(@_) }
573    );
574
575    $self->imagelist->YieldCallback(
576        sub { Wx::Yield }
577    );
578}
579
580
581sub _default_imagelist_params {
582    my ( $self ) = @_ ;
583
584    my $params = {
585        new_files        => [],
586        hd_filter        => $self->user_defined_properties->{hd_filter}||'Lanczos',
587        hd_blur          => $self->user_defined_properties->{hd_blur}||0.9,
588        hd_quality       => $self->user_defined_properties->{hd_quality}||95,
589        hd_w             => $self->user_defined_properties->{hd_w}||1600,
590        hd_h             => $self->user_defined_properties->{hd_h}||1200,
591        hd_interlace     => $self->user_defined_properties->{hd_interlace}||'Line',
592        thumb_size       => $self->user_defined_properties->{thumbnail_size}||120,
593        site_thumb_dir   => $self->thumb_dir,
594        wx_thumb_size    => $self->user_defined_properties->{wx_thumbnail_size}||100,
595        wx_thumb_dir     => $self->wx_thumb_dir,
596        resize_w         => $self->user_defined_properties->{resize_w}||800,
597        resize_h         => $self->user_defined_properties->{resize_h}||600,
598        site_resized_dir => $self->resized_dir,
599        type             => 'jpg',
600        filter           => $self->user_defined_properties->{resize_filter}||'Lanczos',
601        blur             => $self->user_defined_properties->{resize_blur}||0.9,
602        quality          => $self->user_defined_properties->{resize_quality}||95,
603        wx_quality       => $self->user_defined_properties->{wx_thumbnail_quality}||90,
604        th_quality       => $self->user_defined_properties->{thumbnail_quality}||90,
605        auto_rotate      => $self->user_defined_properties->{auto_rotate}||1,
606        upload_hd        => $self->user_defined_properties->{upload_hd}||'No',
607        remove_uploaded_from_selection => $self->user_defined_properties->{remove_uploaded_from_selection}||1,
608        interlace        => $self->user_defined_properties->{resize_interlace}||'Line',
609        create_resized   => $self->user_defined_properties->{create_resized}||1,
610        prefix           => 'TN',
611        count            => 0,
612        storable_file    => $self->storable_file,
613        userdata_dir     => $self->userdata_dir,
614        default_photo_name => $self->user_defined_properties->{default_photo_name}||'File name',
615        default_name_prefix => $self->user_defined_properties->{default_name_prefix}||gettext('Photo '),
616        upload_rejects   =>  [],
617        image_sums       => {},
618        sums             => [],
619        version          => $self->version,
620        imagelist_version => $self->imagelist_version,
621        RescaleCallback  => sub { $self->RescaleImage(@_) },
622        ResizeCallback   => sub { $self->ResizeImage(@_) },
623        watermark_text => $self->user_defined_properties->{watermark_text}||gettext("my watermark"),
624        watermark_text_size => $self->user_defined_properties->{watermark_text_size}||12,
625        watermark_position => $self->user_defined_properties->{watermark_position}||'Center',
626        watermark_y => $self->user_defined_properties->{watermark_y}||10,
627        watermark_x => $self->user_defined_properties->{watermark_x}||10,
628        watermark_color => $self->user_defined_properties->{watermark_color}||'White',
629        reupload_action_files => 1,
630        reupload_action_properties => 2,
631        reupload_action_properties_m => 1,
632        display_mode => $self->user_defined_properties->{display_mode}||'Thumbnail and caption',
633    };
634
635    return $params;
636}
637
638sub Login {
639    my ( $self ) = @_; 
640
641    $self->login_dlg( 
642        Uploader::GUI::wxLoginDlg->new(
643            {
644                title         => gettext("Piwigo login"),
645                site_url      => sub { $self->site_url(@_) },
646                site_username => sub { $self->site_username(@_) },     
647                site_password => sub { $self->site_password(@_) },
648                use_offline   => sub { $self->use_offline(@_) },       
649            }
650        )
651    ) unless $self->login_dlg;
652
653    my $icon = Wx::Icon->new();
654    $icon->LoadFile(
655        File::Spec->catfile($self->bin_dir, '..', 'res', 'favicon.ico'), 
656        wxBITMAP_TYPE_ICO
657    );
658    $self->login_dlg->SetIcon($icon);   
659
660   
661    my $rval = $self->login_dlg->ShowModal();
662    $self->login_dlg->Show(0);
663
664    $self->_init_branding;
665   
666    if ($self->site_url !~ /^http:/){
667        $self->site_url(
668            sprintf(
669                "http://%s",
670                $self->site_url
671            )
672        );     
673    }
674
675    $self->pwg(
676        # get these parameters from dialog or from file
677        Uploader::PWG->new(
678            {
679                site_url       => $self->site_url,
680                site_username  => $self->site_username,
681                site_password  => $self->site_password,
682                http_username  => $self->http_username,
683                http_password  => $self->http_password,
684                branding       => $self->branding,
685                chunk_size     => $self->chunk_size,
686                use_offline    => $self->use_offline,
687                version        => $self->version,
688            }
689        )
690    );
691
692    $rval;
693}
694
695sub _init_userdir {
696    my ( $self ) = @_;
697   
698    my $applicationName = $self->GetAppName ;
699    my $userdatadir = File::Spec->canonpath(
700        File::Spec->catfile(
701            File::HomeDir->my_data(), 
702            "\.$applicationName"
703        )
704    );
705
706    if(! -d $userdatadir){
707        if(! mkdir $userdatadir){
708            Wx::MessageBox( 
709                sprintf(
710                    "%s directory creation failed",
711                    $userdatadir,
712                ),
713                "pLoader working directory creation error",
714                wxOK | wxICON_EXCLAMATION, 
715            );
716
717            $userdatadir = File::Spec->canonpath(
718                File::Spec->catfile(
719                    File::Spec->tmpdir(), 
720                    "\.$applicationName"
721                )
722            );
723            mkdir $userdatadir;
724        }       
725    }
726
727    $self->userdata_dir($userdatadir);
728   
729    $self->conf_file(
730        File::Spec->catfile(
731            $self->userdata_dir, 
732            ".$applicationName.conf"
733        )
734    );
735
736    $self->layout_file(
737        File::Spec->catfile(
738            $self->userdata_dir, 
739            ".$applicationName.layout"
740        )
741    );
742
743    $self->storable_file(
744        File::Spec->catfile($self->userdata_dir, 'pLoader.dat')
745    );
746   
747    my $thumbdir = File::Spec->catfile($self->userdata_dir, 'thumbnails');
748    mkdir $thumbdir unless -d $thumbdir ;
749    $self->thumb_dir($thumbdir);
750
751    my $wxthumbdir = File::Spec->catfile($self->userdata_dir, 'wxthumbnails');
752    mkdir $wxthumbdir unless -d $wxthumbdir ;
753    $self->wx_thumb_dir($wxthumbdir);
754
755
756    my $resizedir = File::Spec->catfile($self->userdata_dir, 'resize');
757    mkdir $resizedir unless -d $resizedir ;
758    $self->resized_dir($resizedir);
759}
760
761sub _init_thumbimagelist {
762    my ( $self ) = @_;
763
764   
765    $self->imagelist->wx_thumb_imglist(
766        Wx::ImageList->new( 
767            $self->imagelist->wx_thumb_size, 
768            $self->imagelist->wx_thumb_size, 
769            1,
770            0
771        )
772    );
773   
774    # reload images
775    $self->_reload_thumb_images;
776}
777
778
779sub _reload_thumb_images {
780    my ( $self ) = @_;
781   
782    my $wximagelist = $self->imagelist->wx_thumb_imglist;
783    my $sums = $self->imagelist->sums;
784
785    map {
786        my $image = $self->imagelist->image_sums->{$_};
787
788        $wximagelist->Add(
789            Wx::Bitmap->new( 
790                $image->wx_thumb_file, 
791                $self->GetWxBitmapType($self->imagelist->type), 
792            )
793        );
794    }
795    @$sums ;
796
797}
798
799sub GetWxBitmapType {
800    my ( $self, $type ) = @_;
801   
802    $self->{IMGTYPE}->{$type};
803}
804
805
806sub RescaleImage {
807    my ( $self, $image_file, $image_file_out, $type, $ratio, $width, $height, $quality ) = @_;
808   
809
810    my $image = Wx::Image->new(
811            $image_file, 
812            $self->GetWxBitmapType($type),
813            0
814    );
815   
816    my $w;
817    my $h;
818
819    my $img_w = $image->GetWidth;
820    my $img_h = $image->GetHeight;
821   
822    # use a ratio ( 25% default ) if defined
823    # default ratio is used for preview.
824    if($ratio){
825        $w = $ratio*$img_w/100 ;
826        $h = $ratio*$img_h/100 ;
827    }
828    # use specified width and height
829    else{
830        # portrait
831        if( $img_w < $img_h ){
832            $w = $height;
833        }
834        else{
835            $w = $width;
836        }
837        # to respect aspect ratio
838        $h = sprintf(
839            "%.0f",
840            ($w*$img_h)/$img_w
841        );
842    }
843
844    $image->Rescale(
845        $w,
846        $h,
847        wxIMAGE_QUALITY_HIGH
848    );
849   
850    $quality ||= 90;
851   
852    $image->SetOption( 
853        "quality", 
854        $quality 
855    );
856   
857    if(!$image->SaveFile(
858        $image_file_out,
859        $self->GetWxBitmapType($type),
860    )){
861        Wx::LogMessage(
862            gettext("An error has occured. Can not save file %s"),
863            $image_file_out,
864        )
865    };
866}
867
868sub ResizeImage {
869    my ( $self, $image_file, $image_file_out, $type, $width, $height, $quality ) = @_;
870   
871
872    my $image = Wx::Image->new(
873            $image_file, 
874            $self->GetWxBitmapType($type),
875            0
876    );
877
878    my $w;
879    my $h;
880
881    my $img_w = $image->GetWidth;
882    my $img_h = $image->GetHeight;
883   
884        # portrait
885        if( $img_w < $img_h ){
886            $w = $height;
887        }
888        else{
889            $w = $width;
890        }
891        # to respect aspect ratio
892        $h = sprintf(
893            "%.0f",
894            ($w*$img_h)/$img_w
895        );
896
897   
898
899    $image->Rescale(
900        $w,
901        $h,
902        wxIMAGE_QUALITY_HIGH
903    );
904
905    $image->Resize(
906        [ $width, $height ], [ 0, 20],
907    );
908   
909    $quality ||= 90;
910   
911    $image->SetOption( 
912        "quality", 
913        $quality 
914    );
915   
916    if(!$image->SaveFile(
917        $image_file_out,
918        $self->GetWxBitmapType($type),
919    )){
920        Wx::LogMessage(
921            gettext("An error has occured. Can not save file %s"),
922            $image_file_out,
923        )
924    };
925}
926
927# some labels differ with branding ( piwigo.com or piwigo.org )
928sub _init_branding {
929    my ( $self ) =@_;
930   
931    if( $self->site_url =~ /\.piwigo\.com/ ){
932        $self->branding(
933            {
934                category  => gettext("album"), 
935                Category  => gettext("Album"), 
936                categories => gettext("albums"),       
937                Categories => gettext("Albums"),
938                'Add new category' => gettext("Add new album"), 
939                'Category name' => gettext("Album name :"),
940                'New category' => gettext("New album"),
941            }
942        );
943    }
944    else{
945        $self->branding(
946            {
947                category  => gettext("categorie"),     
948                Category  => gettext("Categorie"),     
949                categories => gettext("categories"),   
950                Categories => gettext("Categories"),   
951                'Add new category' => gettext("Add new category"),
952                'Category name' => gettext("Category name :"),
953                'New category' => gettext("New category"),
954            }
955        );
956    }   
957}
958
959sub SaveConfig {
960    my ( $self, $params ) = @_; 
961
962   my $config = Wx::ConfigBase::Get;
963
964   map {
965       $config->WriteInt( $_, $params->{$_} )
966   } keys %$params;
967
968   $config->Write( 'Perspective', $params->{Perspective} )
969       
970}
971
972
973sub _init_frame {
974    my ( $self ) = @_; 
975
976    my $url = $self->site_url;
977   
978    if($self->use_offline){
979        $url = gettext("Work Offline");
980    }
981
982    $self->frame(
983        Uploader::GUI::wxFrameAUI->new( 
984            {
985                title     => sprintf("pLoader - Piwigo uploader %s - [%s]", $self->version, $url),
986                pwg       => $self->pwg,
987                imagelist => $self->imagelist,
988                perspective => $self->perspective,
989                imageviewer_index => $self->imageviewerIndex,
990                frameLayout => $self->frameLayout,
991            }
992        )
993    );
994 
995    $self->frame->Show( 1 );
996    $self->SetTopWindow( $self->frame );
997
998    my $icon = Wx::Icon->new();
999    $icon->LoadFile(
1000        File::Spec->catfile(
1001            $self->bin_dir, '..', 'res', 'favicon.ico'
1002        ), 
1003        wxBITMAP_TYPE_ICO
1004    );
1005    $self->frame->SetIcon($icon);       
1006}
1007
1008sub _readParams {
1009        my( $self, $file ) = @_ ;
1010
1011
1012        my $expr_params ;
1013        eval { $expr_params = read_file( $file ); } ;
1014       
1015        my $paramValues = [] ;
1016        if($expr_params){
1017                my $expr = '$paramValues = ' ;
1018                $expr .=  "$expr_params ; " ;
1019                eval $expr ;
1020        }
1021       
1022        return unless 'ARRAY' eq ref $paramValues ;
1023       
1024        if(scalar(@$paramValues )){
1025            my $params = $paramValues->[0] ;
1026            $self->SetKeyValues($params);
1027        }
1028}
1029
1030sub _read_default_user_properties {
1031        my( $self, $file ) = @_ ;
1032
1033
1034        my $expr_params ;
1035        eval { $expr_params = read_file( $file ); } ;
1036        my $properties;
1037        if($expr_params){
1038                my $expr = '$properties = ' ;
1039                $expr .=  " { $expr_params }; " ;
1040                eval $expr ;
1041        }
1042        $properties = {} unless 'HASH' eq ref $properties;
1043        $self->user_defined_properties($properties);
1044
1045}
1046
1047
1048
1049sub SetKeyValues {
1050    my ( $self, $params )= @_; 
1051
1052    foreach( keys %$params ) {
1053        $self->{$_} = $params->{$_} ;
1054    }
1055}
1056
1057
10581;
Note: See TracBrowser for help on using the repository browser.