]> git.sesse.net Git - vlc/blob - modules/gui/skins/src/themeloader.cpp
Compilation fixes
[vlc] / modules / gui / skins / src / themeloader.cpp
1 /*****************************************************************************
2  * themeloader.cpp: ThemeLoader class
3  *****************************************************************************
4  * Copyright (C) 2003 VideoLAN
5  * $Id: themeloader.cpp,v 1.2 2003/03/18 04:56:58 ipkiss Exp $
6  *
7  * Authors: Olivier Teulière <ipkiss@via.ecp.fr>
8  *          Emmanuel Puig    <karibu@via.ecp.fr>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111,
23  * USA.
24  *****************************************************************************/
25
26
27 //--- GENERAL ---------------------------------------------------------------
28 #include <string>
29 #include <fcntl.h>
30
31 //--- VLC -------------------------------------------------------------------
32 #include <vlc/vlc.h>
33 #include <vlc/intf.h>
34
35 #if defined( HAVE_LIBTAR_H ) && defined( HAVE_ZLIB_H )
36 #   include <zlib.h>
37 #   include <libtar.h>
38 #endif
39
40 //--- SKIN ------------------------------------------------------------------
41 #include "os_api.h"
42 #include "theme.h"
43 #include "os_theme.h"
44 #include "themeloader.h"
45 #include "skin_common.h"
46
47 #define DEFAULT_XML_FILE "theme.xml"
48
49 //---------------------------------------------------------------------------
50 extern "C"
51 {
52     extern FILE *yyin;
53     int yylex();
54 }
55
56 //---------------------------------------------------------------------------
57 ThemeLoader::ThemeLoader( intf_thread_t *_p_intf )
58 {
59     p_intf = _p_intf;
60 }
61 //---------------------------------------------------------------------------
62 ThemeLoader::~ThemeLoader()
63 {
64 }
65 //---------------------------------------------------------------------------
66 int gzopen_frontend( char *pathname, int oflags, int mode )
67 {
68 #if defined( HAVE_LIBTAR_H ) && defined( HAVE_ZLIB_H )
69     char *gzflags;
70     gzFile gzf;
71     int fd;
72
73     switch( oflags & O_ACCMODE )
74     {
75         case O_WRONLY:
76             gzflags = "wb";
77             break;
78         case O_RDONLY:
79             gzflags = "rb";
80             break;
81         case O_RDWR:
82         default:
83             errno = EINVAL;
84             return -1;
85     }
86
87     fd = open( pathname, oflags, mode );
88     if( fd == -1 )
89         return -1;
90
91 //    if( ( oflags & O_CREAT ) && fchmod( fd, mode ) )
92 //        return -1;
93
94     gzf = gzdopen( fd, gzflags );
95     if( !gzf )
96     {
97         errno = ENOMEM;
98         return -1;
99     }
100
101     return (int)gzf;
102 #else
103     return 0;
104 #endif
105 }
106 //---------------------------------------------------------------------------
107 #if defined( HAVE_LIBTAR_H ) && defined( HAVE_ZLIB_H )
108 bool ThemeLoader::ExtractTarGz( const string tarfile, const string rootdir )
109 {
110     TAR *t;
111     tartype_t gztype = { (openfunc_t) gzopen_frontend, (closefunc_t) gzclose,
112         (readfunc_t) gzread, (writefunc_t) gzwrite };
113
114     if( tar_open( &t, (char *)tarfile.c_str(), &gztype, O_RDONLY, 0,
115                   TAR_GNU ) == -1 )
116     {
117         return false;
118     }
119
120     if( tar_extract_all( t, (char *)rootdir.c_str() ) != 0 )
121     {
122         return false;
123     }
124
125     if( tar_close( t ) != 0 )
126     {
127         return false;
128     }
129
130     return true;
131 }
132 //---------------------------------------------------------------------------
133 bool ThemeLoader::Extract( const string FileName )
134 {
135     char *tmpdir = tempnam( NULL, "vlt" );
136     string TempPath = tmpdir;
137     free( tmpdir );
138
139     if( ! ExtractTarGz( FileName, TempPath ) )
140         return false;
141
142     if( ! Parse( TempPath + DIRECTORY_SEPARATOR + string( DEFAULT_XML_FILE ) ) )
143     {
144         DeleteTempFiles( TempPath );
145         return false;
146     }
147
148     DeleteTempFiles( TempPath );
149     return true;
150 }
151 //---------------------------------------------------------------------------
152 void ThemeLoader::DeleteTempFiles( const string Path )
153 {
154     OSAPI_RmDir( Path );
155 }
156 #endif
157 //---------------------------------------------------------------------------
158 void ThemeLoader::CleanTheme()
159 {
160     delete (OSTheme *)p_intf->p_sys->p_theme;
161     p_intf->p_sys->p_theme = (Theme *)new OSTheme( p_intf );
162 }
163 //---------------------------------------------------------------------------
164 bool ThemeLoader::Parse( const string XmlFile )
165 {
166     // Things to do before loading theme
167     p_intf->p_sys->p_theme->OnLoadTheme();
168
169     // Set the file to parse
170     yyin = fopen( XmlFile.c_str(), "r" );
171     if( yyin == NULL )
172     {
173         // Skin cannot be opened
174         msg_Warn( p_intf, "Cannot open the specified skin file: %s",
175                   XmlFile.c_str() );
176         return false;
177     }
178
179     // File loaded
180     msg_Dbg( p_intf, "Using skin file: %s", XmlFile.c_str() );
181
182     // Save current working directory
183     char *cwd = new char[MAX_PATH];
184     getcwd( cwd, MAX_PATH );
185
186     // Directory separator is different in each OS !
187     int p = XmlFile.rfind( DIRECTORY_SEPARATOR, XmlFile.size() );
188
189     // Change current working directory to xml file
190     string path = "";
191     if( p > 0 )
192         path = XmlFile.substr( 0, p );
193     chdir( path.c_str() );
194
195     p_intf->p_sys->b_all_win_closed = false;
196
197     // Start the parser
198     int lex = yylex();
199     fclose( yyin );
200     if( lex )
201     {
202         // Set old working directory to current
203         chdir( cwd );
204         delete[] cwd;
205
206         msg_Warn( p_intf, "yylex failed: %i", lex );
207         CleanTheme();
208         return false;
209     }
210
211     // Set old working directory to current
212     chdir( cwd );
213     delete[] cwd;
214
215     return true;
216 }
217 //---------------------------------------------------------------------------
218 bool ThemeLoader::Load( const string FileName )
219 {
220     // First, we try to un-targz the file, and if it fails we hope it's a XML
221     // file...
222 #if defined( HAVE_LIBTAR_H ) && defined( HAVE_ZLIB_H )
223     if( ! Extract( FileName ) && ! Parse( FileName ) )
224         return false;
225 #else
226     if( ! Parse( FileName ) )
227         return false;
228 #endif
229
230     // Check if the skin to load is in the config file, to load its config
231     char *skin_last = config_GetPsz( p_intf, "skin_last" );
232     if( skin_last != NULL && FileName == (string)skin_last )
233     {
234         p_intf->p_sys->p_theme->LoadConfig();
235     }
236     else
237     {
238         config_PutPsz( p_intf, "skin_last", FileName.c_str() );
239         config_SaveConfigFile( p_intf, "skin" );
240     }
241
242     return true;
243 }
244 //---------------------------------------------------------------------------