]> git.sesse.net Git - vlc/blob - modules/gui/wxwidgets/dialogs/updatevlc.cpp
Removes trailing spaces. Removes tabs.
[vlc] / modules / gui / wxwidgets / dialogs / updatevlc.cpp
1 /*****************************************************************************
2  * updatevlc.cpp : VLC Update checker
3  *****************************************************************************
4  * Copyright (C) 2000-2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Antoine Cellerier <dionoea@videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include "updatevlc.hpp"
28 #include <wx/imaglist.h>
29
30 #include "bitmaps/update_ascii.xpm"
31 #include "bitmaps/update_binary.xpm"
32 #include "bitmaps/update_document.xpm"
33 #include "bitmaps/update_info.xpm"
34 #include "bitmaps/update_source.xpm"
35
36 /*****************************************************************************
37  * Event Table.
38  *****************************************************************************/
39
40 /* IDs for the controls and the menu commands */
41 enum
42 {
43     Close_Event,
44     CheckForUpdate_Event,
45     ChooseItem_Event
46 };
47
48 BEGIN_EVENT_TABLE(UpdateVLC, wxFrame)
49     /* Button events */
50     EVT_BUTTON(wxID_OK, UpdateVLC::OnButtonClose)
51     EVT_BUTTON(CheckForUpdate_Event, UpdateVLC::OnCheckForUpdate)
52
53     /* CtrlList events */
54     EVT_LIST_ITEM_ACTIVATED( ChooseItem_Event, UpdateVLC::OnChooseItem )
55
56     /* Hide the window when the user closes the window */
57     EVT_CLOSE(UpdateVLC::OnClose)
58
59 END_EVENT_TABLE()
60
61 /*****************************************************************************
62  * Constructor.
63  *****************************************************************************/
64 UpdateVLC::UpdateVLC( intf_thread_t *_p_intf, wxWindow *p_parent ):
65     wxFrame( p_parent, -1, wxU(_("Updates")),
66              wxDefaultPosition, wxDefaultSize,
67              wxSYSTEM_MENU|wxCLOSE_BOX|wxFRAME_FLOAT_ON_PARENT
68              |wxFRAME_TOOL_WINDOW|wxCAPTION )
69 {
70     /* Initializations */
71     p_intf = _p_intf;
72     SetIcon( *p_intf->p_sys->p_icon );
73     SetAutoLayout( TRUE );
74
75     wxBoxSizer *main_sizer = new wxBoxSizer( wxVERTICAL );
76     wxButton *update_button =
77         new wxButton( this, CheckForUpdate_Event,
78                       wxU(_("Check for updates")) );
79     main_sizer->Add( update_button );
80     SetSizerAndFit( main_sizer );
81
82     p_u = update_New( p_intf );
83 }
84
85
86 UpdateVLC::~UpdateVLC()
87 {
88     update_Delete( p_u );
89 }
90
91 void UpdateVLC::OnButtonClose( wxCommandEvent& event )
92 {
93     wxCloseEvent cevent;
94     OnClose(cevent);
95 }
96
97 void UpdateVLC::OnClose( wxCloseEvent& WXUNUSED(event) )
98 {
99     Hide();
100 }
101
102 void UpdateVLC::OnCheckForUpdate( wxCommandEvent& event )
103 {
104     update_Check( p_u, VLC_FALSE );
105     update_iterator_t *p_uit = update_iterator_New( p_u );
106     if( p_uit )
107     {
108         wxBoxSizer *main_sizer = new wxBoxSizer( wxVERTICAL );
109
110         p_uit->i_rs = UPDATE_RELEASE_STATUS_NEWER;
111         p_uit->i_t = UPDATE_FILE_TYPE_ALL;
112         update_iterator_Action( p_uit, UPDATE_MIRROR );
113
114         DestroyChildren();
115
116         wxListCtrl *list =
117             new wxListCtrl( this, ChooseItem_Event,
118                             wxDefaultPosition, wxSize( 400, 300 ),
119                             wxLC_SINGLE_SEL|wxLC_LIST );
120         wxImageList *images = new wxImageList( 32, 32, TRUE );
121         images->Add( wxIcon( update_ascii_xpm ) );
122         images->Add( wxIcon( update_info_xpm ) );
123         images->Add( wxIcon( update_source_xpm ) );
124         images->Add( wxIcon( update_binary_xpm ) );
125         images->Add( wxIcon( update_document_xpm ) );
126         list->AssignImageList( images, wxIMAGE_LIST_SMALL );
127         while( update_iterator_Action( p_uit, UPDATE_FILE ) != UPDATE_FAIL )
128         {
129             int i_image;
130             switch( p_uit->file.i_type )
131             {
132                 case UPDATE_FILE_TYPE_INFO:
133                     i_image = 1;
134                     break;
135                 case UPDATE_FILE_TYPE_SOURCE:
136                     i_image = 2;
137                     break;
138                 case UPDATE_FILE_TYPE_BINARY:
139                     i_image = 3;
140                     break;
141                 case UPDATE_FILE_TYPE_PLUGIN:
142                     i_image = 4;
143                     break;
144                 default:
145                     i_image = 0;
146             }
147             char *psz_tmp = NULL;
148             if( p_uit->file.l_size )
149             {
150                 if( p_uit->file.l_size > 1024 * 1024 * 1024 )
151                      asprintf( &psz_tmp, "(%ld GB)",
152                                 p_uit->file.l_size / (1024*1024*1024) );
153                 if( p_uit->file.l_size > 1024 * 1024 )
154                     asprintf( &psz_tmp, "(%ld MB)",
155                                 p_uit->file.l_size / (1024*1024) );
156                 else if( p_uit->file.l_size > 1024 )
157                     asprintf( &psz_tmp, "(%ld kB)",
158                                 p_uit->file.l_size / 1024 );
159                 else
160                     asprintf( &psz_tmp, "(%ld B)", p_uit->file.l_size );
161             }
162             list->InsertItem( list->GetItemCount(),
163                               wxU(p_uit->file.psz_description)+wxU("\n")
164                               + wxU(p_uit->release.psz_version)+wxU(" ")
165                               + wxU(psz_tmp),
166                               i_image );
167             if( psz_tmp ) free( psz_tmp );
168         }
169
170         main_sizer->Add( new wxStaticText( this, -1, wxU( _("\nAvailable "
171                 "updates and related downloads.\n"
172                 "(Double click on a file to download it)\n" ) ) ) );
173         main_sizer->Add( list );
174         SetSizerAndFit( main_sizer );
175         Layout();
176         update_iterator_Delete( p_uit );
177     }
178 }
179
180 void UpdateVLC::OnChooseItem( wxListEvent& event )
181 {
182     update_iterator_t *p_uit = update_iterator_New( p_u );
183     if( p_uit )
184     {
185         p_uit->i_rs = UPDATE_RELEASE_STATUS_NEWER;
186         p_uit->i_t = UPDATE_FILE_TYPE_ALL;
187         update_iterator_Action( p_uit, UPDATE_MIRROR );
188
189         int i_count = 0;
190         while( update_iterator_Action( p_uit, UPDATE_FILE ) != UPDATE_FAIL )
191         {
192             if( i_count == event.GetIndex() )
193                 break;
194             i_count++;
195         }
196         wxString url = wxU( p_uit->file.psz_url );
197         wxFileDialog *filedialog =
198                     new wxFileDialog( this, wxU(_("Save file...")),
199                         wxT(""), url.AfterLast( '/' ), wxT("*.*"),
200                         wxSAVE | wxOVERWRITE_PROMPT );
201         if( filedialog->ShowModal() == wxID_OK )
202         {
203             update_download( p_uit, filedialog->GetPath().mb_str(wxConvUTF8) );
204         }
205         update_iterator_Delete( p_uit );
206         delete filedialog;
207     }
208 }