1 /* libcmis
2  * Version: MPL 1.1 / GPLv2+ / LGPLv2+
3  *
4  * The contents of this file are subject to the Mozilla Public License Version
5  * 1.1 (the "License"); you may not use this file except in compliance with
6  * the License or as specified alternatively below. You may obtain a copy of
7  * the License at http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS" basis,
10  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11  * for the specific language governing rights and limitations under the
12  * License.
13  *
14  * Major Contributor(s):
15  * Copyright (C) 2011 SUSE <cbosdonnat@suse.com>
16  *
17  *
18  * All Rights Reserved.
19  *
20  * For minor contributions see the git repository.
21  *
22  * Alternatively, the contents of this file may be used under the terms of
23  * either the GNU General Public License Version 2 or later (the "GPLv2+"), or
24  * the GNU Lesser General Public License Version 2 or later (the "LGPLv2+"),
25  * in which case the provisions of the GPLv2+ or the LGPLv2+ are applicable
26  * instead of those above.
27  */
28 
29 #include <libcmis-c/folder.h>
30 
31 #include "internals.hxx"
32 
33 using namespace std;
34 using libcmis::PropertyPtrMap;
35 using libcmis::FolderPtr;
36 using boost::dynamic_pointer_cast;
37 
libcmis_vector_folder_free(libcmis_vector_folder_Ptr vector)38 void libcmis_vector_folder_free( libcmis_vector_folder_Ptr vector )
39 {
40     delete vector;
41 }
42 
43 
libcmis_vector_folder_size(libcmis_vector_folder_Ptr vector)44 size_t libcmis_vector_folder_size( libcmis_vector_folder_Ptr vector )
45 {
46     size_t size = 0;
47     if ( vector != NULL )
48         size = vector->handle.size( );
49     return size;
50 }
51 
52 
libcmis_vector_folder_get(libcmis_vector_folder_Ptr vector,size_t i)53 libcmis_FolderPtr libcmis_vector_folder_get( libcmis_vector_folder_Ptr vector, size_t i )
54 {
55     libcmis_FolderPtr item = NULL;
56     if ( vector != NULL && i < vector->handle.size( ) )
57     {
58         libcmis::FolderPtr handle = vector->handle[i];
59         item = new ( nothrow ) libcmis_folder( );
60         if ( item )
61             item->handle = handle;
62     }
63     return item;
64 }
65 
66 
libcmis_is_folder(libcmis_ObjectPtr object)67 bool libcmis_is_folder( libcmis_ObjectPtr object )
68 {
69     bool isFolder = false;
70     if ( object != NULL && object->handle.get( ) != NULL )
71     {
72         libcmis::FolderPtr folder = boost::dynamic_pointer_cast< libcmis::Folder >( object->handle );
73         isFolder = folder.get( ) != NULL;
74     }
75     return isFolder;
76 }
77 
78 
libcmis_folder_cast(libcmis_ObjectPtr object)79 libcmis_FolderPtr libcmis_folder_cast( libcmis_ObjectPtr object )
80 {
81     libcmis_FolderPtr folder = NULL;
82 
83     if ( object != NULL && object->handle.get( ) != NULL )
84     {
85         libcmis::FolderPtr handle = boost::dynamic_pointer_cast< libcmis::Folder >( object->handle );
86         if ( handle.get( ) != NULL )
87         {
88             folder = new ( nothrow ) libcmis_folder( );
89             if ( folder )
90                 folder->handle = handle;
91         }
92     }
93 
94     return folder;
95 }
96 
97 
libcmis_folder_free(libcmis_FolderPtr folder)98 void libcmis_folder_free( libcmis_FolderPtr folder )
99 {
100     delete folder;
101 }
102 
103 
libcmis_folder_getParent(libcmis_FolderPtr folder,libcmis_ErrorPtr error)104 libcmis_FolderPtr libcmis_folder_getParent( libcmis_FolderPtr folder, libcmis_ErrorPtr error )
105 {
106     libcmis_FolderPtr parent = NULL;
107     if ( folder != NULL && folder->handle.get( ) != NULL )
108     {
109         try
110         {
111             FolderPtr folderHandle = dynamic_pointer_cast< libcmis::Folder >( folder->handle );
112             if ( folder )
113             {
114                 libcmis::FolderPtr handle = folderHandle->getFolderParent( );
115                 if ( handle.get( ) != NULL )
116                 {
117                     parent = new libcmis_folder( );
118                     parent->handle = handle;
119                 }
120             }
121         }
122         catch ( const libcmis::Exception& e )
123         {
124             if ( error != NULL )
125             {
126                 error->message = strdup( e.what() );
127                 error->type = strdup( e.getType().c_str() );
128             }
129         }
130         catch ( const bad_alloc& e )
131         {
132             if ( error != NULL )
133             {
134                 error->message = strdup( e.what() );
135                 error->badAlloc = true;
136             }
137         }
138     }
139     return parent;
140 }
141 
142 
libcmis_folder_getChildren(libcmis_FolderPtr folder,libcmis_ErrorPtr error)143 libcmis_vector_object_Ptr libcmis_folder_getChildren( libcmis_FolderPtr folder, libcmis_ErrorPtr error )
144 {
145     libcmis_vector_object_Ptr result = NULL;
146     if ( folder != NULL && folder->handle.get( ) != NULL )
147     {
148         try
149         {
150             libcmis::FolderPtr folderHandle = dynamic_pointer_cast< libcmis::Folder >( folder->handle );
151             if ( folder )
152             {
153                 std::vector< libcmis::ObjectPtr > handles = folderHandle->getChildren( );
154                 result = new libcmis_vector_object( );
155                 result->handle = handles;
156             }
157         }
158         catch ( const libcmis::Exception& e )
159         {
160             if ( error != NULL )
161             {
162                 error->message = strdup( e.what() );
163                 error->type = strdup( e.getType().c_str() );
164             }
165         }
166         catch ( const bad_alloc& e )
167         {
168             if ( error != NULL )
169             {
170                 error->message = strdup( e.what() );
171                 error->badAlloc = true;
172             }
173         }
174     }
175     return result;
176 }
177 
178 
libcmis_folder_getPath(libcmis_FolderPtr folder)179 char* libcmis_folder_getPath( libcmis_FolderPtr folder )
180 {
181     char* path = NULL;
182     if ( folder != NULL && folder->handle.get( ) != NULL )
183     {
184         libcmis::FolderPtr folderHandle = dynamic_pointer_cast< libcmis::Folder >( folder->handle );
185         if ( folder )
186             path = strdup( folderHandle->getPath( ).c_str( ) );
187     }
188     return path;
189 }
190 
191 
libcmis_folder_isRootFolder(libcmis_FolderPtr folder)192 bool libcmis_folder_isRootFolder( libcmis_FolderPtr folder )
193 {
194     bool isRoot = false;
195     if ( folder != NULL && folder->handle.get( ) != NULL )
196     {
197         libcmis::FolderPtr folderHandle = dynamic_pointer_cast< libcmis::Folder >( folder->handle );
198         if ( folder )
199             isRoot = folderHandle->isRootFolder( );
200     }
201     return isRoot;
202 }
203 
libcmis_folder_createFolder(libcmis_FolderPtr folder,libcmis_vector_property_Ptr properties,libcmis_ErrorPtr error)204 libcmis_FolderPtr libcmis_folder_createFolder(
205         libcmis_FolderPtr folder,
206         libcmis_vector_property_Ptr properties,
207         libcmis_ErrorPtr error )
208 {
209     libcmis_FolderPtr result = NULL;
210     if ( folder != NULL && folder->handle.get( ) != NULL )
211     {
212         libcmis::FolderPtr folderHandle = dynamic_pointer_cast< libcmis::Folder >( folder->handle );
213         if ( folder )
214         {
215             try
216             {
217                 PropertyPtrMap mappedProperties;
218                 if ( properties != NULL )
219                 {
220                     size_t size = properties->handle.size( );
221                     for ( size_t i = 0; i < size; ++i )
222                     {
223                         libcmis::PropertyPtr property = properties->handle[i];
224                         if ( property.get( ) != NULL )
225                         {
226                             string id = property->getPropertyType( )->getId( );
227                             mappedProperties.insert( pair< string, libcmis::PropertyPtr >( id, property ) );
228                         }
229                     }
230                 }
231 
232                 libcmis::FolderPtr handle = folderHandle->createFolder( mappedProperties );
233                 result = new libcmis_folder( );
234                 result->handle = handle;
235             }
236             catch ( const libcmis::Exception& e )
237             {
238                 if ( error != NULL )
239                 {
240                     error->message = strdup( e.what() );
241                     error->type = strdup( e.getType().c_str() );
242                 }
243             }
244             catch ( const bad_alloc& e )
245             {
246                 if ( error != NULL )
247                 {
248                     error->message = strdup( e.what() );
249                     error->badAlloc = true;
250                 }
251             }
252         }
253     }
254     return result;
255 }
256 
257 
libcmis_folder_createDocument(libcmis_FolderPtr folder,libcmis_vector_property_Ptr properties,libcmis_readFn readFn,void * userData,const char * contentType,const char * filename,libcmis_ErrorPtr error)258 libcmis_DocumentPtr libcmis_folder_createDocument(
259         libcmis_FolderPtr folder,
260         libcmis_vector_property_Ptr properties,
261         libcmis_readFn readFn,
262         void* userData,
263         const char* contentType,
264         const char* filename,
265         libcmis_ErrorPtr error )
266 {
267     libcmis_DocumentPtr created = NULL;
268     if ( folder != NULL && folder->handle.get( ) != NULL )
269     {
270         libcmis::FolderPtr folderHandle = dynamic_pointer_cast< libcmis::Folder >( folder->handle );
271         if ( folder )
272         {
273             try
274             {
275                 // Create the ostream
276                 boost::shared_ptr< std::ostream > stream( new stringstream( ) );
277 
278                 size_t bufSize = 2048;
279                 char* buf = new char[ bufSize ];
280                 size_t read = 0;
281                 do
282                 {
283                     read = readFn( ( void * )buf, size_t( 1 ), bufSize, userData );
284                     stream->write( buf, read );
285                 } while ( read == bufSize );
286                 delete[] buf;
287 
288                 // Create the property map
289                 PropertyPtrMap propertiesMap;
290                 if ( properties != NULL )
291                 {
292                     for ( vector< libcmis::PropertyPtr >::iterator it = properties->handle.begin( );
293                             it != properties->handle.end( ); ++it )
294                     {
295                         string id = ( *it )->getPropertyType( )->getId( );
296                         propertiesMap.insert( pair< string, libcmis::PropertyPtr >( id, *it ) );
297                     }
298                 }
299 
300                 libcmis::DocumentPtr handle = folderHandle->createDocument( propertiesMap, stream, contentType, filename );
301                 created = new libcmis_document( );
302                 created->handle = handle;
303             }
304             catch ( const libcmis::Exception& e )
305             {
306                 if ( error != NULL )
307                 {
308                     error->message = strdup( e.what() );
309                     error->type = strdup( e.getType().c_str() );
310                 }
311             }
312             catch ( const bad_alloc& e )
313             {
314                 if ( error != NULL )
315                 {
316                     error->message = strdup( e.what() );
317                     error->badAlloc = true;
318                 }
319             }
320             catch ( const exception& e )
321             {
322                 if ( error != NULL )
323                     error->message = strdup( e.what() );
324             }
325         }
326     }
327     return created;
328 }
329 
330 
libcmis_folder_removeTree(libcmis_FolderPtr folder,bool allVersion,libcmis_folder_UnfileObjects unfile,bool continueOnError,libcmis_ErrorPtr error)331 libcmis_vector_string_Ptr libcmis_folder_removeTree( libcmis_FolderPtr folder,
332         bool allVersion,
333         libcmis_folder_UnfileObjects unfile,
334         bool continueOnError,
335         libcmis_ErrorPtr error )
336 {
337     libcmis_vector_string_Ptr failed = NULL;
338     try
339     {
340         failed = new libcmis_vector_string( );
341         if ( folder != NULL && folder->handle.get( ) != NULL )
342         {
343             libcmis::FolderPtr folderHandle = dynamic_pointer_cast< libcmis::Folder >( folder->handle );
344             if ( folder )
345             {
346                 vector< string > handle = folderHandle->removeTree( allVersion,
347                         libcmis::UnfileObjects::Type( unfile ), continueOnError );
348                 failed->handle = handle;
349             }
350         }
351     }
352     catch ( const libcmis::Exception& e )
353     {
354         if ( error != NULL )
355         {
356             error->message = strdup( e.what() );
357             error->type = strdup( e.getType().c_str() );
358         }
359     }
360     catch ( const bad_alloc& e )
361     {
362         if ( error != NULL )
363         {
364             error->message = strdup( e.what() );
365             error->badAlloc = true;
366         }
367     }
368     return failed;
369 }
370