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

Last change on this file since 3456 was 3456, checked in by ronosman, 15 years ago

Feature 1035 added. Use system language as default language.

  • Property svn:eol-style set to LF
File size: 26.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_ABKHAZIAN   
42             wxLANGUAGE_AFAR   
43             wxLANGUAGE_AFRIKAANS   
44             wxLANGUAGE_ALBANIAN   
45             wxLANGUAGE_AMHARIC   
46             wxLANGUAGE_ARABIC   
47             wxLANGUAGE_ARABIC_ALGERIA   
48             wxLANGUAGE_ARABIC_BAHRAIN   
49             wxLANGUAGE_ARABIC_EGYPT   
50             wxLANGUAGE_ARABIC_IRAQ   
51             wxLANGUAGE_ARABIC_JORDAN   
52             wxLANGUAGE_ARABIC_KUWAIT   
53             wxLANGUAGE_ARABIC_LEBANON   
54             wxLANGUAGE_ARABIC_LIBYA   
55             wxLANGUAGE_ARABIC_MOROCCO   
56             wxLANGUAGE_ARABIC_OMAN   
57             wxLANGUAGE_ARABIC_QATAR   
58             wxLANGUAGE_ARABIC_SAUDI_ARABIA   
59             wxLANGUAGE_ARABIC_SUDAN   
60             wxLANGUAGE_ARABIC_SYRIA   
61             wxLANGUAGE_ARABIC_TUNISIA   
62             wxLANGUAGE_ARABIC_UAE   
63             wxLANGUAGE_ARABIC_YEMEN   
64             wxLANGUAGE_ARMENIAN   
65             wxLANGUAGE_ASSAMESE   
66             wxLANGUAGE_AYMARA   
67             wxLANGUAGE_AZERI   
68             wxLANGUAGE_AZERI_CYRILLIC   
69             wxLANGUAGE_AZERI_LATIN   
70             wxLANGUAGE_BASHKIR   
71             wxLANGUAGE_BASQUE   
72             wxLANGUAGE_BELARUSIAN   
73             wxLANGUAGE_BENGALI   
74             wxLANGUAGE_BHUTANI   
75             wxLANGUAGE_BIHARI   
76             wxLANGUAGE_BISLAMA   
77             wxLANGUAGE_BRETON   
78             wxLANGUAGE_BULGARIAN   
79             wxLANGUAGE_BURMESE   
80             wxLANGUAGE_CAMBODIAN   
81             wxLANGUAGE_CATALAN   
82             wxLANGUAGE_CHINESE   
83             wxLANGUAGE_CHINESE_SIMPLIFIED   
84             wxLANGUAGE_CHINESE_TRADITIONAL   
85             wxLANGUAGE_CHINESE_HONGKONG   
86             wxLANGUAGE_CHINESE_MACAU   
87             wxLANGUAGE_CHINESE_SINGAPORE   
88             wxLANGUAGE_CHINESE_TAIWAN   
89             wxLANGUAGE_CORSICAN   
90             wxLANGUAGE_CROATIAN   
91             wxLANGUAGE_CZECH   
92             wxLANGUAGE_DANISH   
93             wxLANGUAGE_DUTCH   
94             wxLANGUAGE_DUTCH_BELGIAN   
95             wxLANGUAGE_ENGLISH   
96             wxLANGUAGE_ENGLISH_UK   
97             wxLANGUAGE_ENGLISH_US   
98             wxLANGUAGE_ENGLISH_AUSTRALIA   
99             wxLANGUAGE_ENGLISH_BELIZE   
100             wxLANGUAGE_ENGLISH_BOTSWANA   
101             wxLANGUAGE_ENGLISH_CANADA   
102             wxLANGUAGE_ENGLISH_CARIBBEAN   
103             wxLANGUAGE_ENGLISH_DENMARK   
104             wxLANGUAGE_ENGLISH_EIRE   
105             wxLANGUAGE_ENGLISH_JAMAICA   
106             wxLANGUAGE_ENGLISH_NEW_ZEALAND   
107             wxLANGUAGE_ENGLISH_PHILIPPINES   
108             wxLANGUAGE_ENGLISH_SOUTH_AFRICA   
109             wxLANGUAGE_ENGLISH_TRINIDAD   
110             wxLANGUAGE_ENGLISH_ZIMBABWE   
111             wxLANGUAGE_ESPERANTO   
112             wxLANGUAGE_ESTONIAN   
113             wxLANGUAGE_FAEROESE   
114             wxLANGUAGE_FARSI   
115             wxLANGUAGE_FIJI   
116             wxLANGUAGE_FINNISH   
117             wxLANGUAGE_FRENCH   
118             wxLANGUAGE_FRENCH_BELGIAN   
119             wxLANGUAGE_FRENCH_CANADIAN   
120             wxLANGUAGE_FRENCH_LUXEMBOURG   
121             wxLANGUAGE_FRENCH_MONACO   
122             wxLANGUAGE_FRENCH_SWISS   
123             wxLANGUAGE_FRISIAN   
124             wxLANGUAGE_GALICIAN   
125             wxLANGUAGE_GEORGIAN   
126             wxLANGUAGE_GERMAN   
127             wxLANGUAGE_GERMAN_AUSTRIAN   
128             wxLANGUAGE_GERMAN_BELGIUM   
129             wxLANGUAGE_GERMAN_LIECHTENSTEIN   
130             wxLANGUAGE_GERMAN_LUXEMBOURG   
131             wxLANGUAGE_GERMAN_SWISS   
132             wxLANGUAGE_GREEK   
133             wxLANGUAGE_GREENLANDIC   
134             wxLANGUAGE_GUARANI   
135             wxLANGUAGE_GUJARATI   
136             wxLANGUAGE_HAUSA   
137             wxLANGUAGE_HEBREW   
138             wxLANGUAGE_HINDI   
139             wxLANGUAGE_HUNGARIAN   
140             wxLANGUAGE_ICELANDIC   
141             wxLANGUAGE_INDONESIAN   
142             wxLANGUAGE_INTERLINGUA   
143             wxLANGUAGE_INTERLINGUE   
144             wxLANGUAGE_INUKTITUT   
145             wxLANGUAGE_INUPIAK   
146             wxLANGUAGE_IRISH   
147             wxLANGUAGE_ITALIAN   
148             wxLANGUAGE_ITALIAN_SWISS   
149             wxLANGUAGE_JAPANESE   
150             wxLANGUAGE_JAVANESE   
151             wxLANGUAGE_KANNADA   
152             wxLANGUAGE_KASHMIRI   
153             wxLANGUAGE_KASHMIRI_INDIA   
154             wxLANGUAGE_KAZAKH   
155             wxLANGUAGE_KERNEWEK   
156             wxLANGUAGE_KINYARWANDA   
157             wxLANGUAGE_KIRGHIZ   
158             wxLANGUAGE_KIRUNDI   
159             wxLANGUAGE_KONKANI   
160             wxLANGUAGE_KOREAN   
161             wxLANGUAGE_KURDISH   
162             wxLANGUAGE_LAOTHIAN   
163             wxLANGUAGE_LATIN   
164             wxLANGUAGE_LATVIAN   
165             wxLANGUAGE_LINGALA   
166             wxLANGUAGE_LITHUANIAN   
167             wxLANGUAGE_MACEDONIAN   
168             wxLANGUAGE_MALAGASY   
169             wxLANGUAGE_MALAY   
170             wxLANGUAGE_MALAYALAM   
171             wxLANGUAGE_MALAY_BRUNEI_DARUSSALAM   
172             wxLANGUAGE_MALAY_MALAYSIA   
173             wxLANGUAGE_MALTESE   
174             wxLANGUAGE_MANIPURI   
175             wxLANGUAGE_MAORI   
176             wxLANGUAGE_MARATHI   
177             wxLANGUAGE_MOLDAVIAN   
178             wxLANGUAGE_MONGOLIAN   
179             wxLANGUAGE_NAURU   
180             wxLANGUAGE_NEPALI   
181             wxLANGUAGE_NEPALI_INDIA   
182             wxLANGUAGE_NORWEGIAN_BOKMAL   
183             wxLANGUAGE_NORWEGIAN_NYNORSK   
184             wxLANGUAGE_OCCITAN   
185             wxLANGUAGE_ORIYA   
186             wxLANGUAGE_OROMO   
187             wxLANGUAGE_PASHTO   
188             wxLANGUAGE_POLISH   
189             wxLANGUAGE_PORTUGUESE   
190             wxLANGUAGE_PORTUGUESE_BRAZILIAN   
191             wxLANGUAGE_PUNJABI   
192             wxLANGUAGE_QUECHUA   
193             wxLANGUAGE_RHAETO_ROMANCE   
194             wxLANGUAGE_ROMANIAN   
195             wxLANGUAGE_RUSSIAN   
196             wxLANGUAGE_RUSSIAN_UKRAINE   
197             wxLANGUAGE_SAMI   
198             wxLANGUAGE_SAMOAN   
199             wxLANGUAGE_SANGHO   
200             wxLANGUAGE_SANSKRIT   
201             wxLANGUAGE_SCOTS_GAELIC   
202             wxLANGUAGE_SERBIAN   
203             wxLANGUAGE_SERBIAN_CYRILLIC   
204             wxLANGUAGE_SERBIAN_LATIN   
205             wxLANGUAGE_SERBO_CROATIAN   
206             wxLANGUAGE_SESOTHO   
207             wxLANGUAGE_SETSWANA   
208             wxLANGUAGE_SHONA   
209             wxLANGUAGE_SINDHI   
210             wxLANGUAGE_SINHALESE   
211             wxLANGUAGE_SISWATI   
212             wxLANGUAGE_SLOVAK   
213             wxLANGUAGE_SLOVENIAN   
214             wxLANGUAGE_SOMALI   
215             wxLANGUAGE_SPANISH   
216             wxLANGUAGE_SPANISH_ARGENTINA   
217             wxLANGUAGE_SPANISH_BOLIVIA   
218             wxLANGUAGE_SPANISH_CHILE   
219             wxLANGUAGE_SPANISH_COLOMBIA   
220             wxLANGUAGE_SPANISH_COSTA_RICA   
221             wxLANGUAGE_SPANISH_DOMINICAN_REPUBLIC   
222             wxLANGUAGE_SPANISH_ECUADOR   
223             wxLANGUAGE_SPANISH_EL_SALVADOR   
224             wxLANGUAGE_SPANISH_GUATEMALA   
225             wxLANGUAGE_SPANISH_HONDURAS   
226             wxLANGUAGE_SPANISH_MEXICAN   
227             wxLANGUAGE_SPANISH_MODERN   
228             wxLANGUAGE_SPANISH_NICARAGUA   
229             wxLANGUAGE_SPANISH_PANAMA   
230             wxLANGUAGE_SPANISH_PARAGUAY   
231             wxLANGUAGE_SPANISH_PERU   
232             wxLANGUAGE_SPANISH_PUERTO_RICO   
233             wxLANGUAGE_SPANISH_URUGUAY   
234             wxLANGUAGE_SPANISH_US   
235             wxLANGUAGE_SPANISH_VENEZUELA   
236             wxLANGUAGE_SUNDANESE   
237             wxLANGUAGE_SWAHILI   
238             wxLANGUAGE_SWEDISH   
239             wxLANGUAGE_SWEDISH_FINLAND   
240             wxLANGUAGE_TAGALOG   
241             wxLANGUAGE_TAJIK   
242             wxLANGUAGE_TAMIL   
243             wxLANGUAGE_TATAR   
244             wxLANGUAGE_TELUGU   
245             wxLANGUAGE_THAI   
246             wxLANGUAGE_TIBETAN   
247             wxLANGUAGE_TIGRINYA   
248             wxLANGUAGE_TONGA   
249             wxLANGUAGE_TSONGA   
250             wxLANGUAGE_TURKISH   
251             wxLANGUAGE_TURKMEN   
252             wxLANGUAGE_TWI   
253             wxLANGUAGE_UIGHUR   
254             wxLANGUAGE_UKRAINIAN   
255             wxLANGUAGE_URDU   
256             wxLANGUAGE_URDU_INDIA   
257             wxLANGUAGE_URDU_PAKISTAN   
258             wxLANGUAGE_UZBEK   
259             wxLANGUAGE_UZBEK_CYRILLIC   
260             wxLANGUAGE_UZBEK_LATIN   
261             wxLANGUAGE_VALENCIAN   
262             wxLANGUAGE_VIETNAMESE   
263             wxLANGUAGE_VOLAPUK   
264             wxLANGUAGE_WELSH   
265             wxLANGUAGE_WOLOF   
266             wxLANGUAGE_XHOSA   
267             wxLANGUAGE_YIDDISH   
268             wxLANGUAGE_YORUBA   
269             wxLANGUAGE_ZHUANG   
270             wxLANGUAGE_ZULU 
271         /;
272use Uploader::GUI::wxFrameAUI;
273use File::Slurp ;
274use Data::Dumper;
275use Storable;
276use File::HomeDir;
277use File::Spec;
278use Uploader::PWG;
279use Uploader::ImageList;
280use Uploader::GUI::wxLoginDlg;
281use Wx::Locale qw/:default/;
282
283
284$|=1;
285
286__PACKAGE__->mk_accessors( 
287    qw/
288          pwg
289          site_url
290          site_username
291          site_password
292          http_username
293          http_password
294          rejects
295          imagelist
296          storable_file
297          wx_thumb_dir
298          resized_dir
299          preview_dir
300          userdata_dir
301          resized_dir
302          thumb_dir
303          conf_file
304          locale
305          current_language
306          version
307          imagelist_version
308          use_offline
309          login_dlg
310          branding
311          frame
312      / 
313);
314
315sub FilterEvent {
316    my( $self, $event ) = @_;
317   
318    Wx::LogMessage "EventType %s", $event->GetEventType();
319   
320    return -1;
321}
322
323
324sub OnInit {
325    my( $self ) = @_;
326   
327    $self->version(
328        '2.0.2c'
329    );
330   
331    # to check if we can use stored cache
332    $self->imagelist_version(
333        '3'
334    );
335
336    Wx::InitAllImageHandlers();
337    my $applicationName = "pLoader" ;
338    $self->SetAppName( $applicationName );
339    $self->SetVendorName( "Piwigo Team" );
340
341    $self->{IMGTYPE} = {
342        'jpg' => wxBITMAP_TYPE_JPEG,
343        'gif' => wxBITMAP_TYPE_GIF,
344        'png' => wxBITMAP_TYPE_PNG,
345    };
346
347    $self->_init_userdir;
348
349
350    my $conf = retrieve $self->conf_file if -e $self->conf_file;       
351
352    if(defined $conf ){
353        $self->SetKeyValues($conf);
354    }
355    else {
356        $self->_readParams( 'pLoader.ini' ) unless defined $conf ;
357    }
358
359
360    $self->site_url(
361        $self->{site_url}
362    );
363   
364
365    $self->site_username(
366        $self->{site_username}
367    );
368    $self->site_password(
369        $self->{site_password}
370    );
371
372    $self->http_username(
373        $self->{http_username}
374    );
375    $self->http_password(
376        $self->{http_password}
377    );
378
379
380    $self->current_language(
381        $self->{current_language}||Wx::Locale::GetSystemLanguage()
382    );
383   
384    $self->init_locale;
385
386    my $not_exit = $self->Login();
387    # user pressed OK
388    if($not_exit){
389        $self->StoreConnectionProperties;
390        if( !$self->use_offline ){
391            while( $not_exit and !$self->_is_connected ){
392                $not_exit = $self->Login();
393                last if $self->use_offline;
394            }
395        }
396        $self->_init_imagelist;
397        $self->_init_thumbimagelist;
398        $self->_init_frame;
399    }
400
401    $not_exit;
402}
403
404
405sub _is_connected {
406    my ( $self ) = @_;
407
408    my $is_connected;
409
410    if($self->pwg->login_result->{stat} eq 'ok'){
411        $is_connected = 1;
412    }
413    else{
414        Wx::MessageBox( 
415            sprintf(
416                "%s\n\n%s %s %s",
417                $self->pwg->login_result->{message},
418                gettext("Connection to"),
419                $self->site_url,
420                gettext("failed"),
421            ),
422            gettext("Piwigo login error"),
423            wxOK | wxICON_EXCLAMATION, 
424        );
425    }
426   
427    $is_connected;
428}
429
430
431my $locale;
432sub init_locale {
433    my ( $self, $language ) = @_;
434   
435    $self->current_language(
436        $language
437    ) if defined $language;
438
439    undef $locale;
440    $locale = Wx::Locale->new(
441        $self->current_language
442    );
443    $locale->AddCatalogLookupPathPrefix( '../locale');
444    if(!$locale->AddCatalog( 'pLoader.mo' )){
445        Wx::LogMessage gettext("Cannot find translation catalog files. Use default language");
446    }
447    $self->locale($locale);     
448}
449
450sub StoreConnectionProperties {
451    my ( $self ) = @_;
452
453    eval {   
454        store( 
455            {
456                map{
457                   $_ => $self->{$_},
458                }
459                qw/
460                    site_url
461                    site_username
462                    site_password
463                    http_username
464                    http_password
465                    current_language
466                /
467            },
468            $self->conf_file
469        );
470    };
471}
472
473sub _init_imagelist {
474    my ( $self ) = @_; 
475
476    my $stored_imagelist;
477   
478    my $use_new_imagelist;
479   
480    if( -e $self->storable_file ){
481        eval {
482            $stored_imagelist = retrieve $self->storable_file;
483        };
484        if($@){
485            Wx::LogMessage(
486                gettext("An error has occured. Can not read %s\n%s"),
487                $self->storable_file,
488                $@
489            );
490            $use_new_imagelist = 1 ;
491        }
492        # should have a valid imagelist
493        else{
494            $use_new_imagelist = 1 unless $self->imagelist_version eq $stored_imagelist->{imagelist_version};
495            if($use_new_imagelist){
496                Wx::LogMessage(gettext("pLoader has to reset image cache."));
497            }
498        }
499    }
500    else{
501        $use_new_imagelist = 1 ;
502    }
503
504    if($use_new_imagelist){
505        $stored_imagelist = $self->_default_imagelist_params ;
506    }
507
508
509    $self->imagelist(
510        Uploader::ImageList->new(
511            $stored_imagelist
512        )
513    );
514
515    $self->imagelist->RescaleCallback(
516        sub { $self->RescaleImage(@_) }
517    );
518
519    $self->imagelist->ResizeCallback(
520        sub { $self->ResizeImage(@_) }
521    );
522}
523
524
525sub _default_imagelist_params {
526    my ( $self ) = @_ ;
527
528    my $params = {
529        new_files        => [],
530        thumb_size       => 120,
531        site_thumb_dir   => $self->thumb_dir,
532        wx_thumb_size    => 120,
533        wx_thumb_dir     => $self->wx_thumb_dir,
534        preview_ratio    => 25, 
535        preview_dir      => $self->preview_dir,
536        resize_w         => 800,
537        resize_h         => 600,
538        site_resized_dir => $self->resized_dir,
539        type             => 'jpg',
540        filter           => 'Lanczos',
541        blur             => 0.9,
542        quality          => 100,
543        wx_quality       => 80,
544        th_quality       => 90,
545        auto_rotate      => 1,
546        remove_uploaded_from_selection => 1,
547        interlace        => 'Line',
548        create_resized   => 1,
549        use_exif_preview => 1,
550        prefix           => 'TN',
551        count            => 0,
552        storable_file    => $self->storable_file,
553        userdata_dir     => $self->userdata_dir,
554        default_photo_name => gettext('File name'),
555        default_name_prefix => gettext('Photo '),
556        upload_rejects   =>  [],
557        image_sums       => {},
558        sums             => [],
559        version          => $self->version,
560        imagelist_version => $self->imagelist_version,
561        RescaleCallback  => sub { $self->RescaleImage(@_) },
562        ResizeCallback   => sub { $self->ResizeImage(@_) },
563    };
564
565    return $params;
566}
567
568sub Login {
569    my ( $self ) = @_; 
570
571    $self->login_dlg( 
572        Uploader::GUI::wxLoginDlg->new(
573            {
574                title         => gettext("Piwigo login"),
575                site_url      => sub { $self->site_url(@_) },
576                site_username => sub { $self->site_username(@_) },     
577                site_password => sub { $self->site_password(@_) },
578                use_offline   => sub { $self->use_offline(@_) },       
579            }
580        )
581    ) unless $self->login_dlg;
582
583    my $icon = Wx::Icon->new();
584    $icon->LoadFile('../res/favicon.ico', wxBITMAP_TYPE_ICO);
585    $self->login_dlg->SetIcon($icon);   
586
587   
588    my $rval = $self->login_dlg->ShowModal();
589    $self->login_dlg->Show(0);
590
591    $self->_init_branding;
592   
593    if ($self->site_url !~ /^http:/){
594        $self->site_url(
595            sprintf(
596                "http://%s",
597                $self->site_url
598            )
599        );     
600    }
601
602    $self->pwg(
603        # get these parameters from dialog or from file
604        Uploader::PWG->new(
605            {
606                site_url       => $self->site_url,
607                site_username  => $self->site_username,
608                site_password  => $self->site_password,
609                http_username  => $self->http_username,
610                http_password  => $self->http_password,
611                branding       => $self->branding,
612            }
613        )
614    );
615   
616    $rval;
617}
618
619sub _init_userdir {
620    my ( $self ) = @_;
621   
622    my $applicationName = $self->GetAppName ;
623    my $userdatadir = File::Spec->canonpath(
624        File::Spec->catfile(
625            File::HomeDir->my_data(), 
626            "\.$applicationName"
627        )
628    );
629
630    if(! -d $userdatadir){
631        if(! mkdir $userdatadir){
632            Wx::MessageBox( 
633                sprintf(
634                    "%s directory creation failed",
635                    $userdatadir,
636                ),
637                "pLoader working directory creation error",
638                wxOK | wxICON_EXCLAMATION, 
639            );
640
641            $userdatadir = File::Spec->canonpath(
642                File::Spec->catfile(
643                    File::Spec->tmpdir(), 
644                    "\.$applicationName"
645                )
646            );
647            mkdir $userdatadir;
648        }       
649    }
650
651    $self->userdata_dir($userdatadir);
652   
653    $self->conf_file(
654        File::Spec->catfile(
655            $self->userdata_dir, 
656            ".$applicationName.conf"
657        )
658    );
659
660   
661
662    $self->storable_file(
663        File::Spec->catfile($self->userdata_dir, 'pLoader.dat')
664    );
665   
666    my $thumbdir = File::Spec->catfile($self->userdata_dir, 'thumbnails');
667    mkdir $thumbdir unless -d $thumbdir ;
668    $self->thumb_dir($thumbdir);
669
670    my $wxthumbdir = File::Spec->catfile($self->userdata_dir, 'wxthumbnails');
671    mkdir $wxthumbdir unless -d $wxthumbdir ;
672    $self->wx_thumb_dir($wxthumbdir);
673
674
675    my $resizedir = File::Spec->catfile($self->userdata_dir, 'resize');
676    mkdir $resizedir unless -d $resizedir ;
677    $self->resized_dir($resizedir);
678
679    my $previewdir = File::Spec->catfile($self->userdata_dir, 'preview');
680    mkdir $previewdir unless -d $previewdir ;
681    $self->preview_dir($previewdir);
682
683       
684}
685
686sub _init_thumbimagelist {
687    my ( $self ) = @_;
688
689   
690    $self->imagelist->wx_thumb_imglist(
691        Wx::ImageList->new( 
692            $self->imagelist->wx_thumb_size, 
693            $self->imagelist->wx_thumb_size, 
694            1,
695            0
696        )
697    );
698   
699    # reload images
700    $self->_reload_thumb_images;
701}
702
703
704sub _reload_thumb_images {
705    my ( $self ) = @_;
706   
707    my $wximagelist = $self->imagelist->wx_thumb_imglist;
708    my $sums = $self->imagelist->sums;
709
710    map {
711        my $image = $self->imagelist->image_sums->{$_};
712
713        $wximagelist->Add(
714            Wx::Bitmap->new( 
715                $image->wx_thumb_file, 
716                $self->GetWxBitmapType($self->imagelist->type), 
717            )
718        );
719    }
720    @$sums ;
721
722}
723
724sub GetWxBitmapType {
725    my ( $self, $type ) = @_;
726   
727    $self->{IMGTYPE}->{$type};
728}
729
730
731sub RescaleImage {
732    my ( $self, $image_file, $image_file_out, $type, $ratio, $width, $height, $quality ) = @_;
733   
734
735    my $image = Wx::Image->new(
736            $image_file, 
737            $self->GetWxBitmapType($type),
738            0
739    );
740   
741    my $w;
742    my $h;
743
744    my $img_w = $image->GetWidth;
745    my $img_h = $image->GetHeight;
746   
747    # use a ratio ( 25% default ) if defined
748    # default ratio is used for preview.
749    if($ratio){
750        $w = $ratio*$img_w/100 ;
751        $h = $ratio*$img_h/100 ;
752    }
753    # use specified width and height
754    else{
755        # portrait
756        if( $img_w < $img_h ){
757            $w = $height;
758        }
759        else{
760            $w = $width;
761        }
762        # to respect aspect ratio
763        $h = sprintf(
764            "%.0f",
765            ($w*$img_h)/$img_w
766        );
767    }
768
769    $image->Rescale(
770        $w,
771        $h,
772        wxIMAGE_QUALITY_HIGH
773    );
774   
775    $quality ||= 90;
776   
777    $image->SetOption( 
778        "quality", 
779        $quality 
780    );
781   
782    if(!$image->SaveFile(
783        $image_file_out,
784        $self->GetWxBitmapType($type),
785    )){
786        Wx::LogMessage(
787            gettext("An error has occured. Can not save file %s"),
788            $image_file_out,
789        )
790    };
791}
792
793sub ResizeImage {
794    my ( $self, $image_file, $image_file_out, $type, $width, $height, $quality ) = @_;
795   
796
797    my $image = Wx::Image->new(
798            $image_file, 
799            $self->GetWxBitmapType($type),
800            0
801    );
802
803    my $w;
804    my $h;
805
806    my $img_w = $image->GetWidth;
807    my $img_h = $image->GetHeight;
808   
809        # portrait
810        if( $img_w < $img_h ){
811            $w = $height;
812        }
813        else{
814            $w = $width;
815        }
816        # to respect aspect ratio
817        $h = sprintf(
818            "%.0f",
819            ($w*$img_h)/$img_w
820        );
821
822   
823
824    $image->Rescale(
825        $w,
826        $h,
827        wxIMAGE_QUALITY_HIGH
828    );
829
830    $image->Resize(
831        [ $width, $height ], [ 0, 20],
832    );
833   
834    $quality ||= 90;
835   
836    $image->SetOption( 
837        "quality", 
838        $quality 
839    );
840   
841    if(!$image->SaveFile(
842        $image_file_out,
843        $self->GetWxBitmapType($type),
844    )){
845        Wx::LogMessage(
846            gettext("An error has occured. Can not save file %s"),
847            $image_file_out,
848        )
849    };
850}
851
852# some labels differ with branding ( piwigo.com or piwigo.org )
853sub _init_branding {
854    my ( $self ) =@_;
855   
856    if( $self->site_url =~ /\.piwigo\.com/ ){
857        $self->branding(
858            {
859                category  => gettext("album"), 
860                Category  => gettext("Album"), 
861                categories => gettext("albums"),       
862                Categories => gettext("Albums"),
863                'Add new category' => gettext("Add new album"), 
864            }
865        );
866    }
867    else{
868        $self->branding(
869            {
870                categorie  => gettext("categorie"),     
871                Categorie  => gettext("Categorie"),     
872                categories => gettext("categories"),   
873                Categories => gettext("Categories"),   
874                'Add new category' => gettext("Add new category"),     
875            }
876        );
877    }   
878}
879
880sub SaveConfig {
881    my ( $self, $params ) = @_; 
882
883   my $config = Wx::ConfigBase::Get;
884
885   map {
886       $config->WriteInt( $_, $params->{$_} )
887   } keys %$params;
888
889   $config->Write( 'Perspective', $params->{Perspective} )
890       
891}
892
893
894sub _init_frame {
895    my ( $self ) = @_; 
896
897    my $config = Wx::ConfigBase::Get;
898
899    my $x = $config->ReadInt( "X", 50 );
900    my $y = $config->ReadInt( "Y", 50 );
901    my $w = $config->ReadInt( "Width", 1024 );
902    my $h = $config->ReadInt( "height", 768 );
903    my $prev_w = $config->ReadInt( "PreviewWidth", 400 );
904    my $prev_h = $config->ReadInt( "PreviewHeight", 300 );
905    my $perspective = $config->Read( "Perspective" );
906    my $imageviewer_index = $config->ReadInt( "ImageviewerIndex", 0 );
907   
908    #printf("Config x %s, y %s, w %s, h %s, prev_w %s, prev_h %s\n", $x, $y, $w, $h, $prev_w, $prev_h);
909   
910    my $url = $self->site_url;
911   
912    if($self->use_offline){
913        $url = gettext("Work Offline");
914    }
915
916    $self->frame(
917        Uploader::GUI::wxFrameAUI->new( 
918            {
919                x         => $x,
920                y         => $y,
921                width     => $w,
922                height    => $h,
923                title     => sprintf("pLoader - Piwigo uploader %s - [%s]", $self->version, $url),
924                pwg       => $self->pwg,
925                imagelist => $self->imagelist,
926                preview_w => $prev_w,
927                preview_h => $prev_h,
928                perspective => $perspective,
929                imageviewer_index => $imageviewer_index,
930            }
931        )
932    );
933       
934    $self->frame->SetSize( $x, $y, $w, $h );
935    $self->frame->Show( 1 );
936    $self->SetTopWindow( $self->frame );
937
938    my $icon = Wx::Icon->new();
939    $icon->LoadFile('../res/favicon.ico', wxBITMAP_TYPE_ICO);
940    $self->frame->SetIcon($icon);       
941}
942
943sub _readParams {
944        my( $self, $file ) = @_ ;
945
946
947        my $expr_params ;
948        eval { $expr_params = read_file( $file ); } ;
949       
950        my $paramValues = [] ;
951        if($expr_params){
952                my $expr = '$paramValues = ' ;
953                $expr .=  "$expr_params ; " ;
954                eval $expr ;
955        }
956       
957        return unless 'ARRAY' eq ref $paramValues ;
958       
959        if(scalar(@$paramValues )){
960            my $params = $paramValues->[0] ;
961            $self->SetKeyValues($params);
962        }
963}
964
965
966
967sub SetKeyValues {
968    my ( $self, $params )= @_; 
969
970    foreach( keys %$params ) {
971        $self->{$_} = $params->{$_} ;
972    }
973}
974
975
9761;
Note: See TracBrowser for help on using the repository browser.