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