1 /*****************************************************************************
2 * updatevlc.cpp : Check for VLC updates dialog
3 *****************************************************************************
4 * Copyright (C) 2000-2004 the VideoLAN team
7 * Authors: Antoine Cellerier <dionoea@videolan.org>
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.
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.
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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
30 #include <wx/progdlg.h>
32 #include "wxwindows.h"
34 #include "vlc_block.h"
35 #include "vlc_stream.h"
38 /* define UPDATE_VLC_OS and UPDATE_VLC_ARCH */
39 /* todo : move this somewhere else (isn't wx specific) */
42 # define UPDATE_VLC_OS "windows"
43 # define UPDATE_VLC_ARCH "i386"
46 # define UPDATE_VLC_OS "macosx"
47 # define UPDATE_VLC_ARCH "ppc"
49 # define UPDATE_VLC_OS "*"
50 # define UPDATE_VLC_ARCH "*"
54 /* arch == "*" and os == "*" concern non OS or arch specific stuff */
56 #define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status"
57 #define UPDATE_VLC_MIRRORS_URL "http://update.videolan.org/mirrors"
59 #define UPDATE_VLC_DOWNLOAD_BUFFER_SIZE 2048
61 class UpdatesTreeItem : public wxTreeItemData
64 UpdatesTreeItem( wxString _url ):wxTreeItemData()
71 /*****************************************************************************
73 *****************************************************************************/
75 /* IDs for the controls and the menu commands */
81 UpdatesTreeActivate_Event
84 BEGIN_EVENT_TABLE(UpdateVLC, wxFrame)
86 EVT_BUTTON(wxID_OK, UpdateVLC::OnButtonClose)
87 EVT_BUTTON(CheckForUpdate_Event, UpdateVLC::OnCheckForUpdate)
90 EVT_CHOICE(MirrorChoice_Event, UpdateVLC::OnMirrorChoice)
93 EVT_TREE_ITEM_ACTIVATED(UpdatesTreeActivate_Event, UpdateVLC::OnUpdatesTreeActivate)
95 /* Hide the window when the user closes the window */
96 EVT_CLOSE(UpdateVLC::OnClose)
100 /*****************************************************************************
102 *****************************************************************************/
103 UpdateVLC::UpdateVLC( intf_thread_t *_p_intf, wxWindow *p_parent ):
104 wxFrame( p_parent, -1, wxU(_("Check for updates ...")), wxDefaultPosition,
105 wxDefaultSize, wxDEFAULT_FRAME_STYLE )
107 /* Initializations */
109 release_type = wxT( "stable" );
110 SetIcon( *p_intf->p_sys->p_icon );
111 SetAutoLayout( TRUE );
113 /* Create a panel to put everything in */
114 wxPanel *panel = new wxPanel( this, -1 );
115 panel->SetAutoLayout( TRUE );
118 new wxTreeCtrl( panel, UpdatesTreeActivate_Event, wxDefaultPosition,
120 wxTR_HAS_BUTTONS | wxTR_HIDE_ROOT | wxSUNKEN_BORDER );
121 updates_tree->AddRoot( wxU(_("root" )), -1, -1, NULL );
123 /* Place everything in sizers */
124 wxBoxSizer *main_sizer = new wxBoxSizer( wxVERTICAL );
125 wxBoxSizer *panel_sizer = new wxBoxSizer( wxVERTICAL );
126 wxBoxSizer *subpanel_sizer = new wxBoxSizer( wxHORIZONTAL );
127 panel_sizer->Add( updates_tree, 0, wxGROW | wxALL, 5 );
128 wxButton *update_button =
129 new wxButton( panel, CheckForUpdate_Event,
130 wxU(_("Check for updates now !")) );
131 subpanel_sizer->Add( update_button, 0, wxALL, 5 );
132 //wxChoice constructor prototype changes with 2.5
133 #if wxCHECK_VERSION(2,5,0)
134 wxArrayString *choices_array = new wxArrayString();
135 choices_array->Add( wxT("") );
137 new wxChoice( panel, MirrorChoice_Event, wxDefaultPosition,
138 wxSize( 200, -1 ), *choices_array );
140 wxString choices_array = wxT("");
142 new wxChoice( panel, -1, wxDefaultPosition,
143 wxSize( 200, -1 ),1, *choices_array );
145 subpanel_sizer->Add( mirrors_choice, 0, wxALL, 5 );
146 subpanel_sizer->Layout();
147 panel_sizer->Add( subpanel_sizer, 0, wxALL , 0 );
148 panel_sizer->Layout();
149 panel->SetSizerAndFit( panel_sizer );
150 main_sizer->Add( panel, 0, wxALL | wxGROW, 0 );
151 main_sizer->Layout();
152 SetSizerAndFit( main_sizer );
154 UpdateMirrorsChoice();
159 UpdateVLC::~UpdateVLC()
163 /* this function gets all the info from the xml files hosted on
164 http://update.videolan.org/ and stores it in appropriate lists */
165 void UpdateVLC::GetData()
167 stream_t *p_stream = NULL;
168 char *psz_eltname = NULL;
169 char *psz_name = NULL;
170 char *psz_value = NULL;
171 char *psz_eltvalue = NULL;
173 xml_reader_t *p_xml_reader = NULL;
177 struct update_file_t tmp_file;
178 struct update_version_t tmp_version;
179 std::list<update_version_t>::iterator it;
180 std::list<update_file_t>::iterator it_files;
182 struct update_mirror_t tmp_mirror;
184 p_xml = xml_Create( p_intf );
187 msg_Err( p_intf, "Failed to open XML parser" );
188 // FIXME : display error message in dialog
192 p_stream = stream_UrlNew( p_intf, UPDATE_VLC_STATUS_URL );
195 msg_Err( p_intf, "Failed to open %s for reading",
196 UPDATE_VLC_STATUS_URL );
197 // FIXME : display error message in dialog
201 p_xml_reader = xml_ReaderCreate( p_xml, p_stream );
205 msg_Err( p_intf, "Failed to open %s for parsing",
206 UPDATE_VLC_STATUS_URL );
207 // FIXME : display error message in dialog
215 while( xml_ReaderRead( p_xml_reader ) == 1 )
217 switch( xml_ReaderNodeType( p_xml_reader ) )
221 // TODO : print message
224 case XML_READER_STARTELEM:
225 psz_eltname = xml_ReaderName( p_xml_reader );
228 // TODO : print message
231 msg_Dbg( p_intf, "element name : %s", psz_eltname );
232 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
234 psz_name = xml_ReaderName( p_xml_reader );
235 psz_value = xml_ReaderValue( p_xml_reader );
236 if( !psz_name || !psz_value )
238 // TODO : print message
242 msg_Dbg( p_intf, " attribute %s = %s",
243 psz_name, psz_value );
246 if( strcmp( psz_eltname, "version" ) == 0 )
248 if( !strcmp( psz_name, "type" ) )
249 tmp_version.type = wxU( psz_value );
250 if( !strcmp( psz_name, "major" ) )
251 tmp_version.major = wxU( psz_value );
252 if( !strcmp( psz_name, "minor" ) )
253 tmp_version.minor = wxU( psz_value );
254 if( !strcmp( psz_name, "revision" ) )
255 tmp_version.revision = wxU( psz_value );
256 if( !strcmp( psz_name, "extra" ) )
257 tmp_version.extra = wxU( psz_value );
259 if( !strcmp( psz_eltname, "file" ) )
261 if( !strcmp( psz_name, "type" ) )
262 tmp_file.type = wxU( psz_value );
263 if( !strcmp( psz_name, "md5" ) )
264 tmp_file.md5 = wxU( psz_value );
265 if( !strcmp( psz_name, "size" ) )
266 tmp_file.size = wxU( psz_value );
267 if( !strcmp( psz_name, "url" ) )
268 tmp_file.url = wxU( psz_value );
271 if( !strcmp( psz_name, "name" )
272 && ( !strcmp( psz_value, UPDATE_VLC_OS )
273 || !strcmp( psz_value, "*" ) )
274 && !strcmp( psz_eltname, "os" ) )
278 if( b_os && !strcmp( psz_name, "name" )
279 && ( !strcmp( psz_value, UPDATE_VLC_ARCH )
280 || !strcmp( psz_value, "*" ) )
281 && !strcmp( psz_eltname, "arch" ) )
288 if( ( b_os && b_arch && strcmp( psz_eltname, "arch" ) ) )
290 if( !strcmp( psz_eltname, "version" ) )
292 it = m_versions.begin();
293 while( it != m_versions.end() )
295 if( it->type == tmp_version.type
296 && it->major == tmp_version.major
297 && it->minor == tmp_version.minor
298 && it->revision == tmp_version.revision
299 && it->extra == tmp_version.extra )
305 if( it == m_versions.end() )
307 m_versions.push_back( tmp_version );
308 it = m_versions.begin();
309 while( it != m_versions.end() )
311 if( it->type == tmp_version.type
312 && it->major == tmp_version.major
313 && it->minor == tmp_version.minor
314 && it->revision == tmp_version.revision
315 && it->extra == tmp_version.extra )
322 tmp_version.type = wxT( "" );
323 tmp_version.major = wxT( "" );
324 tmp_version.minor = wxT( "" );
325 tmp_version.revision = wxT( "" );
326 tmp_version.extra = wxT( "" );
328 if( !strcmp( psz_eltname, "file" ) )
330 it->m_files.push_back( tmp_file );
331 tmp_file.type = wxT( "" );
332 tmp_file.md5 = wxT( "" );
333 tmp_file.size = wxT( "" );
334 tmp_file.url = wxT( "" );
335 tmp_file.description = wxT( "" );
341 case XML_READER_ENDELEM:
342 psz_eltname = xml_ReaderName( p_xml_reader );
345 // TODO : print message
348 msg_Dbg( p_intf, "element end : %s", psz_eltname );
349 if( !strcmp( psz_eltname, "os" ) )
351 if( !strcmp( psz_eltname, "arch" ) )
356 case XML_READER_TEXT:
357 psz_eltvalue = xml_ReaderValue( p_xml_reader );
358 msg_Dbg( p_intf, " text : %s", psz_eltvalue );
359 /* This doesn't look safe ... but it works */
360 if( !m_versions.empty() )
361 if( !it->m_files.empty() )
362 it->m_files.back().description = wxU( psz_eltvalue );
363 free( psz_eltvalue );
368 if( p_xml_reader && p_xml ) xml_ReaderDelete( p_xml, p_xml_reader );
369 if( p_stream ) stream_Delete( p_stream );
371 p_stream = stream_UrlNew( p_intf, UPDATE_VLC_MIRRORS_URL );
374 msg_Err( p_intf, "Failed to open %s for reading",
375 UPDATE_VLC_MIRRORS_URL );
376 // FIXME : display error message in dialog
380 p_xml_reader = xml_ReaderCreate( p_xml, p_stream );
384 msg_Err( p_intf, "Failed to open %s for parsing",
385 UPDATE_VLC_MIRRORS_URL );
386 // FIXME : display error message in dialog
393 while( xml_ReaderRead( p_xml_reader ) == 1 )
395 switch( xml_ReaderNodeType( p_xml_reader ) )
399 // TODO : print message
402 case XML_READER_STARTELEM:
403 psz_eltname = xml_ReaderName( p_xml_reader );
406 // TODO : print message
409 msg_Dbg( p_intf, "element name : %s", psz_eltname );
410 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
412 psz_name = xml_ReaderName( p_xml_reader );
413 psz_value = xml_ReaderValue( p_xml_reader );
414 if( !psz_name || !psz_value )
416 // TODO : print message
420 msg_Dbg( p_intf, " attribute %s = %s",
421 psz_name, psz_value );
422 if( !strcmp( psz_eltname, "mirror" ) )
424 if( !strcmp( psz_name, "name" ) )
425 tmp_mirror.name = wxU( psz_value );
426 if( !strcmp( psz_name, "location" ) )
427 tmp_mirror.location = wxU( psz_value );
429 if( !strcmp( psz_eltname, "url" ) )
431 if( !strcmp( psz_name, "type" ) )
432 tmp_mirror.type = wxU( psz_value );
433 if( !strcmp( psz_name, "base" ) )
434 tmp_mirror.base_url = wxU( psz_value );
439 if( !strcmp( psz_eltname, "url" ) )
441 m_mirrors.push_back( tmp_mirror );
442 tmp_mirror.type = wxT( "" );
443 tmp_mirror.base_url = wxT( "" );
448 case XML_READER_ENDELEM:
449 psz_eltname = xml_ReaderName( p_xml_reader );
452 // TODO : print message
455 msg_Dbg( p_intf, "element end : %s", psz_eltname );
456 if( !strcmp( psz_eltname, "mirror" ) )
458 tmp_mirror.name = wxT( "" );
459 tmp_mirror.location = wxT( "" );
464 case XML_READER_TEXT:
465 psz_eltvalue = xml_ReaderValue( p_xml_reader );
466 msg_Dbg( p_intf, " text : %s", psz_eltvalue );
467 free( psz_eltvalue );
473 if( p_xml_reader && p_xml ) xml_ReaderDelete( p_xml, p_xml_reader );
474 if( p_stream ) stream_Delete( p_stream );
475 if( p_xml ) xml_Delete( p_xml );
478 void UpdateVLC::UpdateUpdatesTree()
481 std::list<update_version_t>::iterator it;
482 std::list<update_file_t>::iterator it_files;
483 std::list<update_mirror_t>::iterator it_mirrors;
485 int selection = mirrors_choice->GetSelection();
486 wxString base_url = wxT( "" );
490 it_mirrors = m_mirrors.begin();
491 while( it_mirrors != m_mirrors.end() && selection )
496 if( it_mirrors != m_mirrors.end() ) base_url = it_mirrors->base_url;
500 updates_tree->DeleteAllItems();
503 parent=updates_tree->AddRoot( wxU(_("root" )), -1, -1, NULL );
504 updates_tree->AppendItem( parent,
505 wxT( "Current version : VLC media player "PACKAGE_VERSION_MAJOR"."PACKAGE_VERSION_MINOR"."PACKAGE_VERSION_REVISION"-"PACKAGE_VERSION_EXTRA ),
506 -1, -1, new UpdatesTreeItem( wxT( "" ) ));
507 it = m_versions.begin();
508 while( it != m_versions.end() )
510 if( atoi((const char *)it->major.mb_str()) <
511 atoi(PACKAGE_VERSION_MAJOR)
512 || ( atoi((const char *)it->major.mb_str()) ==
513 atoi(PACKAGE_VERSION_MAJOR)
514 && ( atoi((const char *)it->minor.mb_str()) <
515 atoi(PACKAGE_VERSION_MINOR)
516 || ( atoi((const char *)it->minor.mb_str()) ==
517 atoi(PACKAGE_VERSION_MINOR)
518 && ( atoi((const char *)it->revision.mb_str()) <
519 atoi(PACKAGE_VERSION_REVISION)
520 || ( atoi((const char *)it->revision.mb_str()) ==
521 atoi(PACKAGE_VERSION_REVISION) ) ) ) ) ) )
523 /* version is older or equal tu current version.
524 FIXME : how do we handle the extra version number ? */
528 wxTreeItemId cat = updates_tree->AppendItem( parent,
529 wxT("New Version : VLC media player ")
530 + it->major + wxT(".")
531 + it->minor + wxT(".") + it->revision + wxT("-")
532 + it->extra + wxT(" (") + it->type + wxT(")"),
533 -1, -1, new UpdatesTreeItem( wxT( "" ) ));
534 it_files = it->m_files.begin();
535 while( it_files != it->m_files.end() )
537 wxString url = (it_files->url[0]=='/' ? base_url : wxT( "" ) )
540 updates_tree->AppendItem( cat, it_files->description,
541 -1, -1, new UpdatesTreeItem( url ) );
542 updates_tree->AppendItem( file,
543 wxU(_("type : ")) + it_files->type,
544 -1, -1, new UpdatesTreeItem( url ));
545 updates_tree->AppendItem( file, wxU(_("URL : ")) + url,
546 -1, -1, new UpdatesTreeItem( url ));
547 if( it_files->size != wxT( "" ) )
548 updates_tree->AppendItem( file,
549 wxU(_("file size : ")) + it_files->size,
550 -1, -1, new UpdatesTreeItem( url ));
551 if( it_files->md5 != wxT( "" ) )
552 updates_tree->AppendItem( file,
553 wxU(_("file md5 hash : ")) + it_files->md5,
554 -1, -1, new UpdatesTreeItem( url ));
558 updates_tree->Expand( cat );
559 updates_tree->Expand( parent );
563 void UpdateVLC::UpdateMirrorsChoice()
565 std::list<update_mirror_t>::iterator it_mirrors;
567 mirrors_choice->Clear();
568 mirrors_choice->Append( wxU(_("Choose a mirror")) );
569 it_mirrors = m_mirrors.begin();
570 while( it_mirrors != m_mirrors.end() )
572 mirrors_choice->Append( it_mirrors->name + wxT(" (")
573 + it_mirrors->location + wxT(") [")
574 + it_mirrors->type + wxT("]") );
577 mirrors_choice->SetSelection( 0 );
580 /*void UpdateVLC::UpdateUpdateVLC()
583 UpdateMirrorsChoice();
586 void UpdateVLC::OnButtonClose( wxCommandEvent& event )
592 void UpdateVLC::OnClose( wxCloseEvent& WXUNUSED(event) )
597 void UpdateVLC::OnCheckForUpdate( wxCommandEvent& event )
600 UpdateMirrorsChoice();
604 void UpdateVLC::OnMirrorChoice( wxCommandEvent& event )
609 void UpdateVLC::OnUpdatesTreeActivate( wxTreeEvent& event )
612 ((UpdatesTreeItem *)(updates_tree->GetItemData(event.GetItem())))->url;
613 if( url != wxT( "" ) ? url[0] != '/' : false )
615 wxFileDialog *filedialog =
616 new wxFileDialog( updates_tree, wxU(_("Save file ...")),
617 wxT(""), url.AfterLast( '/' ), wxT("*.*"),
618 wxSAVE | wxOVERWRITE_PROMPT );
619 if( filedialog->ShowModal() == wxID_OK )
621 DownloadFile( url, filedialog->GetPath() );
626 void UpdateVLC::DownloadFile( wxString url, wxString dst )
628 msg_Dbg( p_intf, "Downloading %s to %s",
629 (const char *)url.mb_str(), (const char *)dst.mb_str() );
631 stream_t *p_stream = NULL;
632 p_stream = stream_UrlNew( p_intf, (const char *)url.mb_str() );
635 msg_Err( p_intf, "Failed to open %s for reading", (const char *)url.mb_str() );
636 // FIXME : display error message in dialog
641 p_file = fopen( (const char *)dst.mb_str(), "w" );
644 msg_Err( p_intf, "Failed to open %s for writing", (const char *)dst.mb_str() );
645 // FIXME : display error message in dialog
650 wxProgressDialog *progressdialog =
651 new wxProgressDialog( wxU(_("Downloading...")),
652 wxU(wxT("Src: ") +url + wxT("\nDst: ") +dst ),
653 (int)(stream_Size(p_stream)/UPDATE_VLC_DOWNLOAD_BUFFER_SIZE), NULL,
654 wxPD_ELAPSED_TIME | wxPD_REMAINING_TIME | wxPD_AUTO_HIDE
657 void *buffer = (void *)malloc( UPDATE_VLC_DOWNLOAD_BUFFER_SIZE );
658 while( stream_Read( p_stream, buffer, UPDATE_VLC_DOWNLOAD_BUFFER_SIZE ) )
660 fwrite( buffer, UPDATE_VLC_DOWNLOAD_BUFFER_SIZE, 1, p_file);
661 if( !progressdialog->Update(++i_progress) )
665 if( p_stream ) stream_Delete( p_stream );
666 progressdialog->Destroy();
667 msg_Warn( p_intf, "User aborted download" );
671 progressdialog->Destroy();
672 msg_Dbg( p_intf, "Download finished" );
675 if( p_stream ) stream_Delete( p_stream );