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

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

Feature 993 added : set Piwigo tags. Only existing tags are supported.

  • Property svn:eol-style set to LF
File size: 26.8 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          layout_file
305          locale
306          current_language
307          version
308          imagelist_version
309          use_offline
310          login_dlg
311          branding
312          frame
313          perspective
314          imageviewerIndex
315          frameLayout
316          chunk_size
317      / 
318);
319
320sub FilterEvent {
321    my( $self, $event ) = @_;
322   
323    Wx::LogMessage "EventType %s", $event->GetEventType();
324   
325    return -1;
326}
327
328
329sub OnInit {
330    my( $self ) = @_;
331   
332    $self->version(
333        '2.0.2c'
334    );
335   
336    # to check if we can use stored cache
337    $self->imagelist_version(
338        '4'
339    );
340
341    Wx::InitAllImageHandlers();
342    my $applicationName = "pLoader" ;
343    $self->SetAppName( $applicationName );
344    $self->SetVendorName( "Piwigo Team" );
345
346    $self->{IMGTYPE} = {
347        'jpg' => wxBITMAP_TYPE_JPEG,
348        'gif' => wxBITMAP_TYPE_GIF,
349        'png' => wxBITMAP_TYPE_PNG,
350    };
351
352    $self->_init_userdir;
353
354
355    my $conf = retrieve $self->conf_file if -e $self->conf_file;       
356    my $layout = retrieve $self->layout_file if -e $self->layout_file; 
357
358    if(defined $conf ){
359        $self->SetKeyValues($conf);
360    }
361    else {
362        $self->_readParams( 'pLoader.ini' ) unless defined $conf ;
363    }
364
365    if(defined $layout ){
366        $self->SetKeyValues($layout);
367    }
368
369
370    $self->site_url(
371        $self->{site_url}
372    );
373   
374
375    $self->site_username(
376        $self->{site_username}
377    );
378    $self->site_password(
379        $self->{site_password}
380    );
381
382    $self->http_username(
383        $self->{http_username}
384    );
385    $self->http_password(
386        $self->{http_password}
387    );
388
389
390    $self->current_language(
391        $self->{current_language}||Wx::Locale::GetSystemLanguage()
392    );
393
394    $self->chunk_size(
395        $self->{chunk_size}||500_000
396    );
397   
398    $self->init_locale;
399
400    my $not_exit = $self->Login();
401    # user pressed OK
402    if($not_exit){
403        $self->StoreConnectionProperties;
404        if( !$self->use_offline ){
405            while( $not_exit and !$self->_is_connected ){
406                $not_exit = $self->Login();
407                last if $self->use_offline;
408            }
409        }
410        $self->_init_imagelist;
411        $self->_init_thumbimagelist;
412        $self->_init_frame;
413    }
414
415    $not_exit;
416}
417
418
419sub _is_connected {
420    my ( $self ) = @_;
421
422    my $is_connected;
423
424    if($self->pwg->login_result->{stat} eq 'ok'){
425        $is_connected = 1;
426    }
427    else{
428        Wx::MessageBox( 
429            sprintf(
430                "%s\n\n%s %s %s",
431                $self->pwg->login_result->{message},
432                gettext("Connection to"),
433                $self->site_url,
434                gettext("failed"),
435            ),
436            gettext("Piwigo login error"),
437            wxOK | wxICON_EXCLAMATION, 
438        );
439    }
440   
441    $is_connected;
442}
443
444
445my $locale;
446sub init_locale {
447    my ( $self, $language ) = @_;
448   
449    $self->current_language(
450        $language
451    ) if defined $language;
452
453    undef $locale;
454    $locale = Wx::Locale->new(
455        $self->current_language
456    );
457    $locale->AddCatalogLookupPathPrefix( '../locale');
458    if(!$locale->AddCatalog( 'pLoader.mo' )){
459        Wx::LogMessage gettext("Cannot find translation catalog files. Use default language");
460    }
461    $self->locale($locale);     
462}
463
464sub StoreConnectionProperties {
465    my ( $self ) = @_;
466
467    eval {   
468        store( 
469            {
470                map{
471                   $_ => $self->{$_},
472                }
473                qw/
474                    site_url
475                    site_username
476                    site_password
477                    http_username
478                    http_password
479                    current_language
480                    chunk_size
481                /
482            },
483            $self->conf_file
484        );
485    };
486}
487
488sub StoreLayoutProperties {
489    my ( $self ) = @_;
490
491    eval {   
492        store( 
493            {
494                map{
495                   $_ => $self->{$_},
496                }
497                qw/
498                      perspective
499                      imageviewerIndex
500                      frameLayout
501                /
502            },
503            $self->layout_file
504        );
505    };
506}
507
508sub _init_imagelist {
509    my ( $self ) = @_; 
510
511    my $stored_imagelist;
512   
513    my $use_new_imagelist;
514   
515    if( -e $self->storable_file ){
516        eval {
517            $stored_imagelist = retrieve $self->storable_file;
518        };
519        if($@){
520            Wx::LogMessage(
521                gettext("An error has occured. Can not read %s\n%s"),
522                $self->storable_file,
523                $@
524            );
525            $use_new_imagelist = 1 ;
526        }
527        # should have a valid imagelist
528        else{
529            $use_new_imagelist = 1 unless $self->imagelist_version eq $stored_imagelist->{imagelist_version};
530            if($use_new_imagelist){
531                Wx::LogMessage(gettext("pLoader has to reset image cache."));
532            }
533        }
534    }
535    else{
536        $use_new_imagelist = 1 ;
537    }
538
539    if($use_new_imagelist){
540        $stored_imagelist = $self->_default_imagelist_params ;
541    }
542
543
544    $self->imagelist(
545        Uploader::ImageList->new(
546            $stored_imagelist
547        )
548    );
549
550    $self->imagelist->RescaleCallback(
551        sub { $self->RescaleImage(@_) }
552    );
553
554    $self->imagelist->ResizeCallback(
555        sub { $self->ResizeImage(@_) }
556    );
557}
558
559
560sub _default_imagelist_params {
561    my ( $self ) = @_ ;
562
563    my $params = {
564        new_files        => [],
565        thumb_size       => 120,
566        site_thumb_dir   => $self->thumb_dir,
567        wx_thumb_size    => 120,
568        wx_thumb_dir     => $self->wx_thumb_dir,
569        preview_ratio    => 25, 
570        preview_dir      => $self->preview_dir,
571        resize_w         => 800,
572        resize_h         => 600,
573        site_resized_dir => $self->resized_dir,
574        type             => 'jpg',
575        filter           => 'Lanczos',
576        blur             => 0.9,
577        quality          => 95,
578        wx_quality       => 80,
579        th_quality       => 90,
580        auto_rotate      => 1,
581        remove_uploaded_from_selection => 1,
582        interlace        => 'Line',
583        create_resized   => 1,
584        use_exif_preview => 1,
585        prefix           => 'TN',
586        count            => 0,
587        storable_file    => $self->storable_file,
588        userdata_dir     => $self->userdata_dir,
589        default_photo_name => gettext('File name'),
590        default_name_prefix => gettext('Photo '),
591        upload_rejects   =>  [],
592        image_sums       => {},
593        sums             => [],
594        version          => $self->version,
595        imagelist_version => $self->imagelist_version,
596        RescaleCallback  => sub { $self->RescaleImage(@_) },
597        ResizeCallback   => sub { $self->ResizeImage(@_) },
598    };
599
600    return $params;
601}
602
603sub Login {
604    my ( $self ) = @_; 
605
606    $self->login_dlg( 
607        Uploader::GUI::wxLoginDlg->new(
608            {
609                title         => gettext("Piwigo login"),
610                site_url      => sub { $self->site_url(@_) },
611                site_username => sub { $self->site_username(@_) },     
612                site_password => sub { $self->site_password(@_) },
613                use_offline   => sub { $self->use_offline(@_) },       
614            }
615        )
616    ) unless $self->login_dlg;
617
618    my $icon = Wx::Icon->new();
619    $icon->LoadFile('../res/favicon.ico', wxBITMAP_TYPE_ICO);
620    $self->login_dlg->SetIcon($icon);   
621
622   
623    my $rval = $self->login_dlg->ShowModal();
624    $self->login_dlg->Show(0);
625
626    $self->_init_branding;
627   
628    if ($self->site_url !~ /^http:/){
629        $self->site_url(
630            sprintf(
631                "http://%s",
632                $self->site_url
633            )
634        );     
635    }
636
637    $self->pwg(
638        # get these parameters from dialog or from file
639        Uploader::PWG->new(
640            {
641                site_url       => $self->site_url,
642                site_username  => $self->site_username,
643                site_password  => $self->site_password,
644                http_username  => $self->http_username,
645                http_password  => $self->http_password,
646                branding       => $self->branding,
647                chunk_size     => $self->chunk_size,
648                use_offline    => $self->use_offline,
649            }
650        )
651    );
652
653    $rval;
654}
655
656sub _init_userdir {
657    my ( $self ) = @_;
658   
659    my $applicationName = $self->GetAppName ;
660    my $userdatadir = File::Spec->canonpath(
661        File::Spec->catfile(
662            File::HomeDir->my_data(), 
663            "\.$applicationName"
664        )
665    );
666
667    if(! -d $userdatadir){
668        if(! mkdir $userdatadir){
669            Wx::MessageBox( 
670                sprintf(
671                    "%s directory creation failed",
672                    $userdatadir,
673                ),
674                "pLoader working directory creation error",
675                wxOK | wxICON_EXCLAMATION, 
676            );
677
678            $userdatadir = File::Spec->canonpath(
679                File::Spec->catfile(
680                    File::Spec->tmpdir(), 
681                    "\.$applicationName"
682                )
683            );
684            mkdir $userdatadir;
685        }       
686    }
687
688    $self->userdata_dir($userdatadir);
689   
690    $self->conf_file(
691        File::Spec->catfile(
692            $self->userdata_dir, 
693            ".$applicationName.conf"
694        )
695    );
696
697    $self->layout_file(
698        File::Spec->catfile(
699            $self->userdata_dir, 
700            ".$applicationName.layout"
701        )
702    );
703
704    $self->storable_file(
705        File::Spec->catfile($self->userdata_dir, 'pLoader.dat')
706    );
707   
708    my $thumbdir = File::Spec->catfile($self->userdata_dir, 'thumbnails');
709    mkdir $thumbdir unless -d $thumbdir ;
710    $self->thumb_dir($thumbdir);
711
712    my $wxthumbdir = File::Spec->catfile($self->userdata_dir, 'wxthumbnails');
713    mkdir $wxthumbdir unless -d $wxthumbdir ;
714    $self->wx_thumb_dir($wxthumbdir);
715
716
717    my $resizedir = File::Spec->catfile($self->userdata_dir, 'resize');
718    mkdir $resizedir unless -d $resizedir ;
719    $self->resized_dir($resizedir);
720
721    my $previewdir = File::Spec->catfile($self->userdata_dir, 'preview');
722    mkdir $previewdir unless -d $previewdir ;
723    $self->preview_dir($previewdir);
724
725       
726}
727
728sub _init_thumbimagelist {
729    my ( $self ) = @_;
730
731   
732    $self->imagelist->wx_thumb_imglist(
733        Wx::ImageList->new( 
734            $self->imagelist->wx_thumb_size, 
735            $self->imagelist->wx_thumb_size, 
736            1,
737            0
738        )
739    );
740   
741    # reload images
742    $self->_reload_thumb_images;
743}
744
745
746sub _reload_thumb_images {
747    my ( $self ) = @_;
748   
749    my $wximagelist = $self->imagelist->wx_thumb_imglist;
750    my $sums = $self->imagelist->sums;
751
752    map {
753        my $image = $self->imagelist->image_sums->{$_};
754
755        $wximagelist->Add(
756            Wx::Bitmap->new( 
757                $image->wx_thumb_file, 
758                $self->GetWxBitmapType($self->imagelist->type), 
759            )
760        );
761    }
762    @$sums ;
763
764}
765
766sub GetWxBitmapType {
767    my ( $self, $type ) = @_;
768   
769    $self->{IMGTYPE}->{$type};
770}
771
772
773sub RescaleImage {
774    my ( $self, $image_file, $image_file_out, $type, $ratio, $width, $height, $quality ) = @_;
775   
776
777    my $image = Wx::Image->new(
778            $image_file, 
779            $self->GetWxBitmapType($type),
780            0
781    );
782   
783    my $w;
784    my $h;
785
786    my $img_w = $image->GetWidth;
787    my $img_h = $image->GetHeight;
788   
789    # use a ratio ( 25% default ) if defined
790    # default ratio is used for preview.
791    if($ratio){
792        $w = $ratio*$img_w/100 ;
793        $h = $ratio*$img_h/100 ;
794    }
795    # use specified width and height
796    else{
797        # portrait
798        if( $img_w < $img_h ){
799            $w = $height;
800        }
801        else{
802            $w = $width;
803        }
804        # to respect aspect ratio
805        $h = sprintf(
806            "%.0f",
807            ($w*$img_h)/$img_w
808        );
809    }
810
811    $image->Rescale(
812        $w,
813        $h,
814        wxIMAGE_QUALITY_HIGH
815    );
816   
817    $quality ||= 90;
818   
819    $image->SetOption( 
820        "quality", 
821        $quality 
822    );
823   
824    if(!$image->SaveFile(
825        $image_file_out,
826        $self->GetWxBitmapType($type),
827    )){
828        Wx::LogMessage(
829            gettext("An error has occured. Can not save file %s"),
830            $image_file_out,
831        )
832    };
833}
834
835sub ResizeImage {
836    my ( $self, $image_file, $image_file_out, $type, $width, $height, $quality ) = @_;
837   
838
839    my $image = Wx::Image->new(
840            $image_file, 
841            $self->GetWxBitmapType($type),
842            0
843    );
844
845    my $w;
846    my $h;
847
848    my $img_w = $image->GetWidth;
849    my $img_h = $image->GetHeight;
850   
851        # portrait
852        if( $img_w < $img_h ){
853            $w = $height;
854        }
855        else{
856            $w = $width;
857        }
858        # to respect aspect ratio
859        $h = sprintf(
860            "%.0f",
861            ($w*$img_h)/$img_w
862        );
863
864   
865
866    $image->Rescale(
867        $w,
868        $h,
869        wxIMAGE_QUALITY_HIGH
870    );
871
872    $image->Resize(
873        [ $width, $height ], [ 0, 20],
874    );
875   
876    $quality ||= 90;
877   
878    $image->SetOption( 
879        "quality", 
880        $quality 
881    );
882   
883    if(!$image->SaveFile(
884        $image_file_out,
885        $self->GetWxBitmapType($type),
886    )){
887        Wx::LogMessage(
888            gettext("An error has occured. Can not save file %s"),
889            $image_file_out,
890        )
891    };
892}
893
894# some labels differ with branding ( piwigo.com or piwigo.org )
895sub _init_branding {
896    my ( $self ) =@_;
897   
898    if( $self->site_url =~ /\.piwigo\.com/ ){
899        $self->branding(
900            {
901                category  => gettext("album"), 
902                Category  => gettext("Album"), 
903                categories => gettext("albums"),       
904                Categories => gettext("Albums"),
905                'Add new category' => gettext("Add new album"), 
906                'Category name' => gettext("Album name :"),
907                'New category' => gettext("New album"),
908            }
909        );
910    }
911    else{
912        $self->branding(
913            {
914                category  => gettext("categorie"),     
915                Category  => gettext("Categorie"),     
916                categories => gettext("categories"),   
917                Categories => gettext("Categories"),   
918                'Add new category' => gettext("Add new category"),
919                'Category name' => gettext("Category name :"),
920                'New category' => gettext("New category"),
921            }
922        );
923    }   
924}
925
926sub SaveConfig {
927    my ( $self, $params ) = @_; 
928
929   my $config = Wx::ConfigBase::Get;
930
931   map {
932       $config->WriteInt( $_, $params->{$_} )
933   } keys %$params;
934
935   $config->Write( 'Perspective', $params->{Perspective} )
936       
937}
938
939
940sub _init_frame {
941    my ( $self ) = @_; 
942
943    my $url = $self->site_url;
944   
945    if($self->use_offline){
946        $url = gettext("Work Offline");
947    }
948
949    $self->frame(
950        Uploader::GUI::wxFrameAUI->new( 
951            {
952                title     => sprintf("pLoader - Piwigo uploader %s - [%s]", $self->version, $url),
953                pwg       => $self->pwg,
954                imagelist => $self->imagelist,
955                perspective => $self->perspective,
956                imageviewer_index => $self->imageviewerIndex,
957                frameLayout => $self->frameLayout,
958            }
959        )
960    );
961 
962    $self->frame->Show( 1 );
963    $self->SetTopWindow( $self->frame );
964
965    my $icon = Wx::Icon->new();
966    $icon->LoadFile('../res/favicon.ico', wxBITMAP_TYPE_ICO);
967    $self->frame->SetIcon($icon);       
968}
969
970sub _readParams {
971        my( $self, $file ) = @_ ;
972
973
974        my $expr_params ;
975        eval { $expr_params = read_file( $file ); } ;
976       
977        my $paramValues = [] ;
978        if($expr_params){
979                my $expr = '$paramValues = ' ;
980                $expr .=  "$expr_params ; " ;
981                eval $expr ;
982        }
983       
984        return unless 'ARRAY' eq ref $paramValues ;
985       
986        if(scalar(@$paramValues )){
987            my $params = $paramValues->[0] ;
988            $self->SetKeyValues($params);
989        }
990}
991
992
993
994sub SetKeyValues {
995    my ( $self, $params )= @_; 
996
997    foreach( keys %$params ) {
998        $self->{$_} = $params->{$_} ;
999    }
1000}
1001
1002
10031;
Note: See TracBrowser for help on using the repository browser.