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

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

bug 1201 fixed : pLoader crashes when path or filename contains unicode characters. All filename or path on windows must be ANSI converted when using perl file operators.

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