[osx/ios] remove unused function
[vuplus_xbmc] / xbmc / interfaces / python / xbmcmodule / xbmcvfsmodule.cpp
1 /*
2  *      Copyright (C) 2005-2011 Team XBMC
3  *      http://www.xbmc.org
4  *
5  *  This Program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2, or (at your option)
8  *  any later version.
9  *
10  *  This Program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with XBMC; see the file COPYING.  If not, write to
17  *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
18  *  http://www.gnu.org/copyleft/gpl.html
19  *
20  */
21
22 #include "system.h"
23 #if (defined USE_EXTERNAL_PYTHON)
24 #if (defined HAVE_LIBPYTHON2_6)
25 #include <python2.6/Python.h>
26 #elif (defined HAVE_LIBPYTHON2_5)
27 #include <python2.5/Python.h>
28 #elif (defined HAVE_LIBPYTHON2_4)
29 #include <python2.4/Python.h>
30 #else
31 #error "Could not determine version of Python to use."
32 #endif
33 #else
34 #include "python/Include/Python.h"
35 #endif
36 #include "../XBPythonDll.h"
37
38 #include "filesystem/File.h"
39 #include "pyutil.h"
40 #include "pythreadstate.h"
41
42 using namespace std;
43 using namespace XFILE;
44 using namespace PYXBMC;
45
46 #ifndef __GNUC__
47 #pragma code_seg("PY_TEXT")
48 #pragma data_seg("PY_DATA")
49 #pragma bss_seg("PY_BSS")
50 #pragma const_seg("PY_RDATA")
51 #endif
52
53 #if defined(__GNUG__) && (__GNUC__>4) || (__GNUC__==4 && __GNUC_MINOR__>=2)
54 #pragma GCC diagnostic ignored "-Wstrict-aliasing"
55 #endif
56
57 #ifdef __cplusplus
58 extern "C" {
59 #endif
60   
61   namespace xbmcvfs
62   {
63     /*****************************************************************
64      * start of xbmcvfs methods
65      *****************************************************************/
66     typedef struct {
67       PyObject_HEAD
68       CFile* pFile;
69     } File;
70     
71     // copy() method
72     PyDoc_STRVAR(copy__doc__,
73       "copy(source, destination) -- copy file to destination, returns true/false.\n"
74       "\n"
75       "source          : file to copy.\n"
76       "destination     : destination file"
77       "\n"
78       "example:\n"
79       "  success = xbmcvfs.copy(source, destination)\n");
80     
81     PyObject* vfs_copy(PyObject *self, PyObject *args)
82     {
83       PyObject *f_line;
84       PyObject *d_line;
85       if (!PyArg_ParseTuple(
86         args,
87         (char*)"OO",
88         &f_line,
89         &d_line))
90       {
91         return NULL;
92       }
93       CStdString strSource;
94       CStdString strDestnation;
95       bool bResult = true;
96       
97       if (!PyXBMCGetUnicodeString(strSource, f_line, 1)) return NULL;
98       if (!PyXBMCGetUnicodeString(strDestnation, d_line, 1)) return NULL;
99
100       CPyThreadState pyState;
101       bResult = CFile::Cache(strSource, strDestnation);
102       pyState.Restore();
103       
104       return Py_BuildValue((char*)"b", bResult);
105     }
106     PyDoc_STRVAR(delete__doc__,
107       "delete(file)\n"
108       "\n"
109       "file        : file to delete"
110       "\n"
111       "example:\n"
112       "  xbmcvfs.delete(file)\n");
113     
114     // delete a file
115     PyObject* vfs_delete(File *self, PyObject *args, PyObject *kwds)
116     {
117       PyObject *f_line;
118       if (!PyArg_ParseTuple(
119         args,
120         (char*)"O",
121         &f_line))
122       {
123         return NULL;
124       }
125       CStdString strSource;
126       if (!PyXBMCGetUnicodeString(strSource, f_line, 1)) return NULL;
127       
128       CPyThreadState pyState;
129       self->pFile->Delete(strSource);
130       pyState.Restore();
131       
132       Py_INCREF(Py_None);
133       return Py_None;
134       
135     }
136     
137     PyDoc_STRVAR(rename__doc__,
138       "rename(file, newFileName)\n"
139       "\n"
140       "file        : file to reaname"
141       "newFileName : new filename, including the full path"
142       "\n"
143       "example:\n"
144       "  success = xbmcvfs.rename(file,newFileName)\n");
145     
146     // rename a file
147     PyObject* vfs_rename(File *self, PyObject *args, PyObject *kwds)
148     {
149       PyObject *f_line;
150       PyObject *d_line;
151       if (!PyArg_ParseTuple(
152         args,
153         (char*)"OO",
154         &f_line,
155         &d_line))
156       {
157         return NULL;
158       }
159       CStdString strSource;
160       CStdString strDestnation;
161       if (!PyXBMCGetUnicodeString(strSource, f_line, 1)) return NULL;
162       if (!PyXBMCGetUnicodeString(strDestnation, d_line, 1)) return NULL;
163       
164       bool bResult;
165
166       CPyThreadState pyState;
167       bResult = self->pFile->Rename(strSource,strDestnation);
168       pyState.Restore();
169       
170       return Py_BuildValue((char*)"b", bResult);
171       
172     }  
173
174     PyDoc_STRVAR(exists__doc__,
175       "exists(path)\n"
176       "\n"
177       "path        : file or folder"
178       "\n"
179       "example:\n"
180       "  success = xbmcvfs.exists(path)\n");
181    
182     // check for a file or folder existance, mimics Pythons os.path.exists()
183     PyObject* vfs_exists(File *self, PyObject *args, PyObject *kwds)
184     {
185       PyObject *f_line;
186       if (!PyArg_ParseTuple(
187         args,
188         (char*)"O",
189         &f_line))
190       {
191         return NULL;
192       }
193       CStdString strSource;
194       if (!PyXBMCGetUnicodeString(strSource, f_line, 1)) return NULL;
195      
196       bool bResult;
197      
198       CPyThreadState pyState;
199       bResult = self->pFile->Exists(strSource, false);
200       pyState.Restore();
201
202       return Py_BuildValue((char*)"b", bResult);
203     }      
204     
205     // define c functions to be used in python here
206     PyMethodDef xbmcvfsMethods[] = {
207       {(char*)"copy", (PyCFunction)vfs_copy, METH_VARARGS, copy__doc__},
208       {(char*)"delete", (PyCFunction)vfs_delete, METH_VARARGS, delete__doc__},
209       {(char*)"rename", (PyCFunction)vfs_rename, METH_VARARGS, rename__doc__},
210       {(char*)"exists", (PyCFunction)vfs_exists, METH_VARARGS, exists__doc__},
211       {NULL, NULL, 0, NULL}
212     };
213     
214     /*****************************************************************
215      * end of methods and python objects
216      * initxbmc(void);
217      *****************************************************************/
218     
219     
220     PyMODINIT_FUNC
221     DeinitVFSModule()
222     {
223       // no need to Py_DECREF our objects (see InitXBMCMVFSModule()) as they were created only
224       // so that they could be added to the module, which steals a reference.
225     }
226     
227     PyMODINIT_FUNC
228     InitVFSModule()
229     {
230       // init general xbmc modules
231       PyObject* pXbmcvfsModule;
232       pXbmcvfsModule = Py_InitModule((char*)"xbmcvfs", xbmcvfsMethods);
233       if (pXbmcvfsModule == NULL) return;
234     }    
235   }
236   
237 #ifdef __cplusplus
238 }
239 #endif