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 Cao Cuong Ngo <cao.cuong.ngo@gmail.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 "gdrive-utils.hxx"
30 
31 #include <libcmis/xml-utils.hxx>
32 
33 #include "json-utils.hxx"
34 
35 using namespace std;
36 using libcmis::PropertyPtrMap;
37 
toCmisKey(const string & key)38 string GdriveUtils::toCmisKey( const string& key )
39 {
40     string convertedKey;
41     if ( key == "id")
42         convertedKey = "cmis:objectId";
43     else if ( key == "ownerNames" )
44         convertedKey = "cmis:createdBy";
45     else if ( key == "description" )
46         convertedKey = "cmis:description";
47     else if ( key == "createdTime" )
48         convertedKey = "cmis:creationDate";
49     else if ( key == "lastModifyingUserName" )
50         convertedKey = "cmis:lastModifiedBy";
51     else if ( key == "modifiedTime" )
52         convertedKey = "cmis:lastModificationDate";
53     else if ( key == "name" )
54         convertedKey = "cmis:contentStreamFileName";
55     else if ( key == "mimeType" )
56         convertedKey = "cmis:contentStreamMimeType";
57     else if ( key == "size" )
58         convertedKey = "cmis:contentStreamLength";
59     else if ( key == "editable" )
60         convertedKey = "cmis:isImmutable";
61     else if ( key == "parents" )
62         convertedKey = "cmis:parentId";
63     else convertedKey = key;
64     return convertedKey;
65 }
66 
toGdriveKey(const string & key)67 string GdriveUtils::toGdriveKey( const string& key )
68 {
69     string convertedKey;
70     if ( key == "cmis:objectId")
71         convertedKey = "id";
72     else if ( key == "cmis:createdBy" )
73         convertedKey = "ownerNames";
74     else if ( key == "cmis:creationDate" )
75         convertedKey = "createdTime";
76     else if ( key == "cmis:description" )
77         convertedKey = "description";
78     else if ( key == "cmis:lastModifiedBy" )
79         convertedKey = "lastModifyingUserName";
80     else if ( key == "cmis:lastModificationDate" )
81         convertedKey = "modifiedTime";
82     else if ( key == "cmis:contentStreamFileName" )
83         convertedKey = "name";
84     else if ( key == "cmis:name" )
85         convertedKey = "name";
86     else if ( key == "cmis:contentStreamMimeType" )
87         convertedKey = "mimeType";
88     else if ( key == "cmis:contentStreamLength" )
89         convertedKey = "size";
90     else if ( key == "cmis:isImmutable" )
91         convertedKey = "editable";
92     else if ( key == "cmis:parentId" )
93         convertedKey = "parents";
94     else convertedKey = key;
95     return convertedKey;
96 }
97 
toGdriveJson(const PropertyPtrMap & properties)98 Json GdriveUtils::toGdriveJson( const PropertyPtrMap& properties )
99 {
100     Json propsJson;
101 
102     // check if cmis:name and cmis:contentStreamFileName has been duplicated
103     bool duplicated = false;
104     for ( PropertyPtrMap::const_iterator it = properties.begin() ;
105             it != properties.end() ; ++it )
106     {
107         string key = it->first;
108         Json value( it->second );
109 
110         // Convert the key back to the gdrive key
111         // take one of the two: cmis:name and cmis:contentStreamFileName
112         const bool isName = key == "cmis:name" || key == "cmis:contentStreamFileName";
113 
114         if ( !isName || !duplicated )
115             propsJson.add( toGdriveKey( key ), value );
116 
117         if ( isName )
118             duplicated = true;
119     }
120 
121     return propsJson;
122 }
123 
checkUpdatable(const string & key)124 bool GdriveUtils::checkUpdatable( const string& key )
125 {
126     // taken from https://developers.google.com/drive/v2/reference/files
127     bool updatable = ( key == "name" ||
128                   key == "description" ||
129                   key == "modifiedTime" ||
130                   key == "lastViewedByMeDate" );
131     return updatable;
132 }
133 
checkMultiValued(const string & key)134 bool GdriveUtils::checkMultiValued( const string& key )
135 {
136     bool bMultiValued = ( key == "parents" ||
137                   key == "exportLinks" ||
138                   key == "labels" ||
139                   key == "ownersName" ||
140                   key == "owners");
141     return bMultiValued;
142 }
143 
createJsonFromParentId(const string & parentId)144 Json GdriveUtils::createJsonFromParentId( const string& parentId )
145 {
146     // parents is a Json array
147     Json firstParent;
148     firstParent.add( Json( parentId.c_str() ) );
149 
150     return firstParent;
151 }
152 
parseGdriveProperty(string key,Json json)153 vector< string > GdriveUtils::parseGdriveProperty( string key, Json json )
154 {
155     vector< string > values;
156     if ( key == "owners" )
157     {
158         Json::JsonVector owners = json.getList( );
159         for ( Json::JsonVector::iterator it = owners.begin( );
160                 it != owners.end( ); ++it )
161         {
162             string ownerName = ( *it )["displayName"].toString( );
163             values.push_back( ownerName);
164         }
165     }
166     else if ( key == "lastModifyingUser" )
167     {
168         string ownerName = json["displayName"].toString( );
169         values.push_back( ownerName);
170     }
171     else if ( key == "userPermission" )
172     {
173         string ownerName = json["role"].toString( );
174         values.push_back( ownerName);
175     }
176     else if ( key == "ownerNames" )
177     {
178         Json::JsonVector owners = json.getList( );
179         for ( Json::JsonVector::iterator it = owners.begin( );
180                 it != owners.end( ); ++it )
181         {
182             string ownerName = ( *it )[""].toString( );
183             values.push_back( ownerName);
184         }
185     }
186     else if ( key == "parents" )
187     {
188         Json::JsonVector owners = json.getList( );
189         for ( Json::JsonVector::iterator it = owners.begin( );
190                 it != owners.end( ); ++it )
191         {
192             string ownerName = ( *it )["id"].toString( );
193             values.push_back( ownerName);
194         }
195     }
196     else if ( key == "exportLinks" )
197     {
198         Json::JsonObject exportLinks = json.getObjects( );
199         for ( Json::JsonObject::iterator it = exportLinks.begin( );
200                 it != exportLinks.end( ); ++it )
201         {
202             string mimeType = it->first;
203             string link = it->second.toString( );
204             values.push_back( mimeType + ":\"" + link +"\"");
205         }
206     }
207     else if ( key == "labels" )
208     {
209         Json::JsonObject labels = json.getObjects( );
210         for ( Json::JsonObject::iterator it = labels.begin( );
211                 it != labels.end( ); ++it )
212         {
213             string label = it->first;
214             string isSet = it->second.toString( );
215             values.push_back( label + ": " + isSet );
216         }
217     }
218     else values.push_back( json.toString( ) );
219     return values;
220 }
221 
222