~ubuntu-branches/ubuntu/utopic/libcmis/utopic-proposed

« back to all changes in this revision

Viewing changes to src/libcmis/gdrive-object.cxx

  • Committer: Package Import Robot
  • Author(s): Sebastien Bacher
  • Date: 2014-02-05 14:41:34 UTC
  • mfrom: (1.1.5) (2.1.6 experimental)
  • Revision ID: package-import@ubuntu.com-20140205144134-ffvov1moblfapn82
Tags: 0.4.1-3ubuntu1
* Resynchronize on Debian, remaining change:
* debian/patches/powerpc64le.diff: build on ppc64el

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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) 2013 SUSE <cbosdonnat@suse.com>
 
16
 *               2013 Cao Cuong Ngo <cao.cuong.ngo@gmail.com>   
 
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 "gdrive-object.hxx"
 
30
#include "gdrive-property.hxx"
 
31
#include "gdrive-allowable-actions.hxx"
 
32
#include "gdrive-repository.hxx"
 
33
#include "gdrive-utils.hxx"
 
34
 
 
35
using namespace std;
 
36
using namespace libcmis;
 
37
 
 
38
GDriveObject::GDriveObject( GDriveSession* session ) :
 
39
    libcmis::Object( session )
 
40
{
 
41
}
 
42
 
 
43
GDriveObject::GDriveObject( GDriveSession* session, Json json, string id, string name ) :
 
44
    libcmis::Object( session )
 
45
{
 
46
   initializeFromJson( json, id, name ); 
 
47
}
 
48
 
 
49
GDriveObject::GDriveObject( const GDriveObject& copy ) :
 
50
    libcmis::Object( copy )
 
51
{
 
52
}
 
53
 
 
54
GDriveObject& GDriveObject::operator=( const GDriveObject& copy )
 
55
{
 
56
    if ( this != &copy )
 
57
    {
 
58
        libcmis::Object::operator=( copy );
 
59
    }
 
60
    return *this;
 
61
}
 
62
 
 
63
void GDriveObject::initializeFromJson ( Json json, string id, string name )
 
64
{
 
65
    Json::JsonObject objs = json.getObjects( );
 
66
    Json::JsonObject::iterator it;
 
67
    for ( it = objs.begin( ); it != objs.end( ); ++it)
 
68
    {
 
69
        PropertyPtr property;
 
70
 
 
71
        // in case of a revision, get the ID and name of the original file
 
72
        if ( !id.empty( ) && it->first == "id" )
 
73
        {
 
74
            Json idJson( id.c_str( ) );
 
75
            property.reset( new GDriveProperty( "id", idJson ) );
 
76
            m_properties[ property->getPropertyType( )->getId()] = property;
 
77
            property.reset( new GDriveProperty( "revisionId", it->second ) );
 
78
            m_properties[ property->getPropertyType( )->getId()] = property;
 
79
 
 
80
            Json nameJson( name.c_str( ) );
 
81
            property.reset( new GDriveProperty( "cmis:name", nameJson) );
 
82
            m_properties[ property->getPropertyType( )->getId()] = property;
 
83
            property.reset( new GDriveProperty( "cmis:contentStreamFileName", nameJson) );
 
84
            m_properties[ property->getPropertyType( )->getId()] = property;
 
85
        }
 
86
        else 
 
87
        {
 
88
            property.reset( new GDriveProperty( it->first, it->second ) );
 
89
            m_properties[ property->getPropertyType( )->getId()] = property;
 
90
           
 
91
            // we map "title" to both "cmis:name" and "cmis:getContentStreamFileName"
 
92
            if ( it->first == "title" )
 
93
            {
 
94
                property.reset( new GDriveProperty( "cmis:name", it->second) );
 
95
                m_properties[ property->getPropertyType( )->getId()] = property;
 
96
            }
 
97
            
 
98
            // some revision keep the original file name.
 
99
            if ( it->first == "originalFilename" )
 
100
            {
 
101
                property.reset( new GDriveProperty( "cmis:name", it->second ) );
 
102
                m_properties[ property->getPropertyType( )->getId()] = property;
 
103
                property.reset( new GDriveProperty( "cmis:contentStreamFileName", it->second) );
 
104
                m_properties[ property->getPropertyType( )->getId()] = property;
 
105
            }
 
106
 
 
107
            // In case of a revision, get the modified date as creation date
 
108
            if ( it->first == "modifiedDate" && !id.empty( ) )
 
109
            {
 
110
                property.reset( new GDriveProperty( "cmis:creationDate", it->second) );
 
111
                m_properties[ property->getPropertyType( )->getId()] = property;
 
112
            }
 
113
            // In case of a revision, get the last modifying user and the creator
 
114
            if ( it->first == "lastModifyingUserName" && !id.empty( ) )
 
115
            {
 
116
                property.reset( new GDriveProperty( "cmis:createdBy", it->second) );
 
117
                m_properties[ property->getPropertyType( )->getId()] = property;
 
118
            }
 
119
        }
 
120
    }
 
121
    m_refreshTimestamp = time( NULL );
 
122
    
 
123
    // Create AllowableActions
 
124
    bool isFolder = json["mimeType"].toString( ) == GDRIVE_FOLDER_MIME_TYPE;
 
125
    m_allowableActions.reset( new GdriveAllowableActions( isFolder ) );
 
126
}
 
127
 
 
128
GDriveSession* GDriveObject::getSession( )
 
129
{
 
130
    return dynamic_cast< GDriveSession* > ( m_session );
 
131
}
 
132
 
 
133
void GDriveObject::refreshImpl( Json json )
 
134
{
 
135
    m_typeDescription.reset( );
 
136
    m_properties.clear( );
 
137
    initializeFromJson( json );
 
138
}
 
139
 
 
140
vector< RenditionPtr> GDriveObject::getRenditions( string /* filter */ ) throw ( libcmis::Exception )
 
141
{
 
142
    if ( m_renditions.empty( ) )
 
143
    {
 
144
        string downloadUrl = getStringProperty( "downloadUrl" );
 
145
        if ( !downloadUrl.empty( ) )
 
146
        {
 
147
            string mimeType = getStringProperty( "cmis:contentStreamMimeType" );
 
148
            if ( !mimeType.empty( ) )
 
149
            { 
 
150
                RenditionPtr rendition( 
 
151
                    new Rendition( mimeType, mimeType, mimeType, downloadUrl ));
 
152
                m_renditions.push_back( rendition );
 
153
            }
 
154
        }
 
155
 
 
156
        vector< string > exportLinks = getMultiStringProperty( "exportLinks" );
 
157
        for ( vector<string>::iterator it = exportLinks.begin( ); it != exportLinks.end( ); ++it)
 
158
        { 
 
159
            int pos = (*it).find(":\"");
 
160
            if ( pos == -1 ) continue;
 
161
            string mimeType = (*it).substr( 0, pos );
 
162
            string url = (*it).substr( pos + 2, (*it).length( ) - pos - 3 );
 
163
            RenditionPtr rendition(
 
164
                new Rendition( mimeType, mimeType, mimeType, url ) );
 
165
            m_renditions.push_back( rendition );
 
166
        }
 
167
 
 
168
        // thumbnail link        
 
169
        string thumbnailLink = getStringProperty( "thumbnailLink" );
 
170
        if ( !thumbnailLink.empty( ) )
 
171
        {
 
172
            string mimeType = "cmis:thumbnail";   
 
173
            RenditionPtr rendition( 
 
174
                new Rendition( mimeType, mimeType, mimeType, thumbnailLink ));
 
175
            m_renditions.push_back( rendition );
 
176
        }
 
177
    }
 
178
    return m_renditions;
 
179
}
 
180
 
 
181
libcmis::ObjectPtr GDriveObject::updateProperties(
 
182
        const PropertyPtrMap& properties ) throw ( libcmis::Exception )
 
183
{
 
184
    // Make Json object from properties
 
185
    Json json = GdriveUtils::toGdriveJson( properties );
 
186
 
 
187
    istringstream is( json.toString( ));
 
188
 
 
189
    libcmis::HttpResponsePtr response;
 
190
    try 
 
191
    {   
 
192
        vector< string > headers;
 
193
        headers.push_back( "Content-Type: application/json" );
 
194
        response = getSession( )->httpPutRequest( getUrl( ), is, headers );
 
195
    }
 
196
    catch ( const CurlException& e )
 
197
    {   
 
198
        throw e.getCmisException( );
 
199
    }
 
200
    
 
201
    string res = response->getStream( )->str( );
 
202
    Json jsonRes = Json::parse( res );
 
203
    libcmis::ObjectPtr updated( new GDriveObject ( getSession( ), jsonRes ) );
 
204
 
 
205
    if ( updated->getId( ) == getId( ) )
 
206
         refreshImpl( jsonRes );
 
207
 
 
208
    return updated;
 
209
}
 
210
 
 
211
void GDriveObject::refresh( ) throw ( libcmis::Exception )
 
212
{
 
213
    string res;
 
214
    try
 
215
    {
 
216
        res  = getSession()->httpGetRequest( getUrl( ) )->getStream( )->str( );
 
217
    }
 
218
    catch ( const CurlException& e )
 
219
    {
 
220
        throw e.getCmisException( );
 
221
    }
 
222
    Json json = Json::parse( res );
 
223
    refreshImpl( json );
 
224
}
 
225
 
 
226
void GDriveObject::remove( bool /*allVersions*/ ) throw ( libcmis::Exception )
 
227
{
 
228
    try
 
229
    {
 
230
        getSession( )->httpDeleteRequest( getUrl( ) );
 
231
    }
 
232
    catch ( const CurlException& e )
 
233
    {
 
234
        throw e.getCmisException( );
 
235
    }
 
236
}
 
237
 
 
238
void GDriveObject::move( FolderPtr /*source*/, FolderPtr destination ) 
 
239
                                        throw ( libcmis::Exception )
 
240
{  
 
241
    Json parentsJson;
 
242
    Json parentsValue = GdriveUtils::createJsonFromParentId( destination->getId( ) );
 
243
    parentsJson.add( "parents", parentsValue );
 
244
    
 
245
    istringstream is( parentsJson.toString( ) );
 
246
    libcmis::HttpResponsePtr response;
 
247
    try 
 
248
    {   
 
249
        vector< string > headers;
 
250
        headers.push_back( "Content-Type: application/json" );
 
251
        response = getSession( )->httpPutRequest( getUrl( ), is, headers );
 
252
    }
 
253
    catch ( const CurlException& e )
 
254
    {   
 
255
        throw e.getCmisException( );
 
256
    }
 
257
    string res = response->getStream( )->str( );
 
258
    Json jsonRes = Json::parse( res );
 
259
 
 
260
    refreshImpl( jsonRes );
 
261
}
 
262
 
 
263
string GDriveObject::getUrl( )
 
264
{
 
265
    return getSession( )->getBindingUrl( ) + "/files/" + getId( );
 
266
}
 
267
 
 
268
string GDriveObject::getUploadUrl( )
 
269
{
 
270
    return GDRIVE_UPLOAD_LINKS;
 
271
}
 
272
 
 
273
vector< string> GDriveObject::getMultiStringProperty( const string& propertyName )
 
274
{
 
275
    vector< string > values;
 
276
    PropertyPtrMap::const_iterator it = getProperties( ).find( string( propertyName ) );
 
277
    if ( it != getProperties( ).end( ) && it->second != NULL && !it->second->getStrings( ).empty( ) )
 
278
        values = it->second->getStrings( );
 
279
    return values; 
 
280
}
 
281