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