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

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

Feature 1015 added : save current image selection and reload preview when pLoader starts.

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