]> git.sesse.net Git - vlc/blob - modules/gui/wxwidgets/dialogs/updatevlc.cpp
Fix misplaced include (triggered error on win32 build only ... weird)
[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 #include <wx/thread.h>
30
31 #include "bitmaps/update_ascii.xpm"
32 #include "bitmaps/update_binary.xpm"
33 #include "bitmaps/update_document.xpm"
34 #include "bitmaps/update_info.xpm"
35 #include "bitmaps/update_source.xpm"
36
37 #include "vlc_block.h"
38 #include "vlc_stream.h"
39
40 #define UPDATE_VLC_DOWNLOAD_BUFFER_SIZE 2048
41
42 /*****************************************************************************
43  * Event Table.
44  *****************************************************************************/
45
46 /* IDs for the controls and the menu commands */
47 enum
48 {
49     Close_Event,
50     CheckForUpdate_Event,
51     ChooseItem_Event
52 };
53
54 BEGIN_EVENT_TABLE(UpdateVLC, wxFrame)
55     /* Button events */
56     EVT_BUTTON(wxID_OK, UpdateVLC::OnButtonClose)
57     EVT_BUTTON(CheckForUpdate_Event, UpdateVLC::OnCheckForUpdate)
58
59     /* CtrlList events */
60     EVT_LIST_ITEM_ACTIVATED( ChooseItem_Event, UpdateVLC::OnChooseItem )
61
62     /* Hide the window when the user closes the window */
63     EVT_CLOSE(UpdateVLC::OnClose)
64
65 END_EVENT_TABLE()
66
67 /*****************************************************************************
68  * Constructor.
69  *****************************************************************************/
70 UpdateVLC::UpdateVLC( intf_thread_t *_p_intf, wxWindow *p_parent ):
71     wxFrame( p_parent, -1, wxU(_("VLC media player - Updates")),
72              wxDefaultPosition, wxDefaultSize,
73          wxSYSTEM_MENU|wxCLOSE_BOX|wxFRAME_FLOAT_ON_PARENT|wxFRAME_TOOL_WINDOW)
74 {
75     /* Initializations */
76     p_intf = _p_intf;
77     SetIcon( *p_intf->p_sys->p_icon );
78     SetAutoLayout( TRUE );
79
80     wxBoxSizer *main_sizer = new wxBoxSizer( wxVERTICAL );
81     wxButton *update_button =
82         new wxButton( this, CheckForUpdate_Event,
83                       wxU(_("Check for updates now !")) );
84     main_sizer->Add( update_button );
85     SetSizerAndFit( main_sizer );
86
87     p_u = update_New( p_intf );
88 }
89
90
91 UpdateVLC::~UpdateVLC()
92 {
93     update_Delete( p_u );
94 }
95
96 void UpdateVLC::OnButtonClose( wxCommandEvent& event )
97 {
98     wxCloseEvent cevent;
99     OnClose(cevent);
100 }
101
102 void UpdateVLC::OnClose( wxCloseEvent& WXUNUSED(event) )
103 {
104     Hide();
105 }
106
107 void UpdateVLC::OnCheckForUpdate( wxCommandEvent& event )
108 {
109     update_Check( p_u, VLC_FALSE );
110     update_iterator_t *p_uit = update_iterator_New( p_u );
111     if( p_uit )
112     {
113         wxBoxSizer *main_sizer = new wxBoxSizer( wxVERTICAL );
114
115         p_uit->i_rs = UPDATE_RELEASE_STATUS_NEWER;
116         p_uit->i_t = UPDATE_FILE_TYPE_ALL;
117         update_iterator_Action( p_uit, UPDATE_MIRROR );
118
119         DestroyChildren();
120
121         wxListCtrl *list =
122             new wxListCtrl( this, ChooseItem_Event,
123                             wxDefaultPosition, wxSize( 400, 300 ),
124                             wxLC_AUTOARRANGE|wxLC_SINGLE_SEL );
125         wxImageList *images = new wxImageList( 32, 32, TRUE );
126         images->Add( wxIcon( update_ascii_xpm ) );
127         images->Add( wxIcon( update_info_xpm ) );
128         images->Add( wxIcon( update_source_xpm ) );
129         images->Add( wxIcon( update_binary_xpm ) );
130         images->Add( wxIcon( update_document_xpm ) );
131         list->AssignImageList( images, wxIMAGE_LIST_SMALL );
132         while( update_iterator_Action( p_uit, UPDATE_FILE ) != UPDATE_FAIL )
133         {
134             /*wxButton *update_button =
135                 new wxButton( this, Download_Event,
136                       wxU(p_uit->file.psz_description) );
137             main_sizer->Add( update_button, 0, wxALIGN_CENTER );*/
138             int i_image;
139             switch( p_uit->file.i_type )
140             {
141                 case UPDATE_FILE_TYPE_INFO:
142                     i_image = 1;
143                     break;
144                 case UPDATE_FILE_TYPE_SOURCE:
145                     i_image = 2;
146                     break;
147                 case UPDATE_FILE_TYPE_BINARY:
148                     i_image = 3;
149                     break;
150                 case UPDATE_FILE_TYPE_PLUGIN:
151                     i_image = 4;
152                     break;
153                 default:
154                     i_image = 0;
155             }
156             list->InsertItem( list->GetItemCount(),
157                               wxU(p_uit->file.psz_description)+wxU("\n")
158                               + wxU(p_uit->release.psz_version)+wxU(" (")
159                               + wxU(p_uit->release.psz_svn_revision)+wxU(")"),
160                               i_image );
161         }
162
163         main_sizer->Add( new wxStaticText( this, -1, wxU( _("\nAvailable updates and related downloads:\n(Double click on a file to download it)\n" ) ) ) );
164         main_sizer->Add( list/*, 0, wxEXPAND */);
165         SetSizerAndFit( main_sizer );
166         Layout();
167         update_iterator_Delete( p_uit );
168     }
169 }
170
171 class DownloadThread : public wxThread
172 {
173     public:
174     DownloadThread( update_iterator_t *p_uit, char *psz_dest )
175         :p_uit( p_uit ),psz_dest( psz_dest )
176     {
177             Create();
178             Run();
179     }
180     ExitCode Entry()
181     {
182         update_download( p_uit, psz_dest );
183         update_iterator_Delete( p_uit );
184         LocaleFree( psz_dest );
185         return 0;
186     };
187     update_iterator_t *p_uit;
188     char *psz_dest;
189 };
190
191 void UpdateVLC::OnChooseItem( wxListEvent& event )
192 {
193     update_iterator_t *p_uit = update_iterator_New( p_u );
194     if( p_uit )
195     {
196         p_uit->i_rs = UPDATE_RELEASE_STATUS_NEWER;
197         p_uit->i_t = UPDATE_FILE_TYPE_ALL;
198         update_iterator_Action( p_uit, UPDATE_MIRROR );
199
200         int i_count = 0;
201         while( update_iterator_Action( p_uit, UPDATE_FILE ) != UPDATE_FAIL )
202         {
203             if( i_count == event.GetIndex() )
204                 break;
205             i_count++;
206         }
207         wxString url = wxU( p_uit->file.psz_url );
208         wxFileDialog *filedialog =
209                     new wxFileDialog( this, wxU(_("Save file ...")),
210                         wxT(""), url.AfterLast( '/' ), wxT("*.*"),
211                         wxSAVE | wxOVERWRITE_PROMPT );
212         if( filedialog->ShowModal() == wxID_OK )
213         {
214             char *psz_dest = ToLocale( filedialog->GetPath().mb_str() );
215
216             /* Launch the download process in a new thread so it doesn't
217              * block the interface */
218             new DownloadThread( p_uit, psz_dest );
219         }
220         else
221         {
222             update_iterator_Delete( p_uit );
223         }
224         delete filedialog;
225     }
226 }