1 /* -*- Mode: c; c-basic-offset: 2 -*-
2  *
3  * photos-notes-api.c - Flickr flickr.photos.notes.* API calls
4  *
5  * Copyright (C) 2007-2012, David Beckett http://www.dajobe.org/
6  *
7  * This file is licensed under the following three licenses as alternatives:
8  *   1. GNU Lesser General Public License (LGPL) V2.1 or any newer version
9  *   2. GNU General Public License (GPL) V2 or any newer version
10  *   3. Apache License, V2.0 or any newer version
11  *
12  * You may not use this file except in compliance with at least one of
13  * the above three licenses.
14  *
15  * See LICENSE.html or LICENSE.txt at the top of this package for the
16  * complete terms and further detail along with the license texts for
17  * the licenses in COPYING.LIB, COPYING and LICENSE-2.0.txt respectively.
18  *
19  */
20 
21 #include <stdio.h>
22 #include <string.h>
23 #include <stdarg.h>
24 
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28 
29 #ifdef WIN32
30 #include <win32_flickcurl_config.h>
31 #endif
32 
33 #ifdef HAVE_STDLIB_H
34 #include <stdlib.h>
35 #undef HAVE_STDLIB_H
36 #endif
37 #ifdef HAVE_UNISTD_H
38 #include <unistd.h>
39 #endif
40 
41 #include <flickcurl.h>
42 #include <flickcurl_internal.h>
43 
44 
45 /**
46  * flickcurl_photos_notes_add:
47  * @fc: flickcurl context
48  * @photo_id: The id of the photo to add a note to
49  * @note_x: The left coordinate of the note
50  * @note_y: The top coordinate of the note
51  * @note_w: The width of the note
52  * @note_h: The height of the note
53  * @note_text: The description of the note
54  *
55  * Add a note to a photo.
56  *
57  * Coordinates and sizes are in pixels, based on the 500px image size
58  * shown on individual photo pages.
59  *
60  * Implements flickr.photos.notes.add (0.12)
61  *
62  * Return value: note ID or NULL on failure
63  **/
64 char*
flickcurl_photos_notes_add(flickcurl * fc,const char * photo_id,int note_x,int note_y,int note_w,int note_h,const char * note_text)65 flickcurl_photos_notes_add(flickcurl* fc, const char* photo_id,
66                            int note_x, int note_y, int note_w, int note_h,
67                            const char* note_text)
68 {
69   xmlDocPtr doc = NULL;
70   xmlXPathContextPtr xpathCtx = NULL;
71   char *id = NULL;
72   char note_x_s[10];
73   char note_y_s[10];
74   char note_w_s[10];
75   char note_h_s[10];
76 
77   flickcurl_init_params(fc, 1);
78 
79   if(!photo_id || !note_text)
80     return NULL;
81 
82   flickcurl_add_param(fc, "photo_id", photo_id);
83   sprintf(note_x_s, "%d", note_x);
84   flickcurl_add_param(fc, "note_x", note_x_s);
85   sprintf(note_y_s, "%d", note_y);
86   flickcurl_add_param(fc, "note_y", note_y_s);
87   sprintf(note_w_s, "%d", note_w);
88   flickcurl_add_param(fc, "note_w", note_w_s);
89   sprintf(note_h_s, "%d", note_h);
90   flickcurl_add_param(fc, "note_h", note_h_s);
91   flickcurl_add_param(fc, "note_text", note_text);
92 
93   flickcurl_end_params(fc);
94 
95   if(flickcurl_prepare(fc, "flickr.photos.notes.add"))
96     goto tidy;
97 
98   doc = flickcurl_invoke(fc);
99   if(!doc)
100     goto tidy;
101 
102 
103   xpathCtx = xmlXPathNewContext(doc);
104   if(!xpathCtx) {
105     flickcurl_error(fc, "Failed to create XPath context for document");
106     fc->failed = 1;
107     goto tidy;
108   }
109 
110   id = flickcurl_xpath_eval(fc, xpathCtx, (const xmlChar*)"/rsp/note/@id");
111 
112   tidy:
113   if(xpathCtx)
114     xmlXPathFreeContext(xpathCtx);
115 
116   if(fc->failed) {
117     if(id)
118       free(id);
119     id = NULL;
120   }
121 
122   return id;
123 }
124 
125 
126 /**
127  * flickcurl_photos_notes_delete:
128  * @fc: flickcurl context
129  * @note_id: The id of the note to delete
130  *
131  * Delete a note from a photo.
132  *
133  * Implements flickr.photos.notes.delete (0.12)
134  *
135  * Return value: non-0 on failure
136  **/
137 int
flickcurl_photos_notes_delete(flickcurl * fc,const char * note_id)138 flickcurl_photos_notes_delete(flickcurl* fc, const char* note_id)
139 {
140   xmlDocPtr doc = NULL;
141   int result = 1;
142 
143   flickcurl_init_params(fc, 1);
144 
145   if(!note_id)
146     return 1;
147 
148   flickcurl_add_param(fc, "note_id", note_id);
149 
150   flickcurl_end_params(fc);
151 
152   if(flickcurl_prepare(fc, "flickr.photos.notes.delete"))
153     goto tidy;
154 
155   doc = flickcurl_invoke(fc);
156   if(!doc)
157     goto tidy;
158 
159   result = 0;
160 
161   tidy:
162   if(fc->failed)
163     result = 1;
164 
165   return result;
166 }
167 
168 
169 /**
170  * flickcurl_photos_notes_edit:
171  * @fc: flickcurl context
172  * @note_id: The id of the note to edit
173  * @note_x: The left coordinate of the note
174  * @note_y: The top coordinate of the note
175  * @note_w: The width of the note
176  * @note_h: The height of the note
177  * @note_text: The description of the note
178  *
179  * Edit a note on a photo. Coordinates and sizes are in pixels, based on the 500px image size shown on individual photo pages.
180 
181  *
182  * Implements flickr.photos.notes.edit (0.12)
183  *
184  * Return value: non-0 on failure
185  **/
186 int
flickcurl_photos_notes_edit(flickcurl * fc,const char * note_id,int note_x,int note_y,int note_w,int note_h,const char * note_text)187 flickcurl_photos_notes_edit(flickcurl* fc,
188                             const char* note_id,
189                             int note_x, int note_y, int note_w, int note_h,
190                             const char* note_text)
191 {
192   xmlDocPtr doc = NULL;
193   int result = 1;
194   char note_x_s[10];
195   char note_y_s[10];
196   char note_w_s[10];
197   char note_h_s[10];
198 
199   flickcurl_init_params(fc, 1);
200 
201   if(!note_id || !note_text)
202     return 1;
203 
204   flickcurl_add_param(fc, "note_id", note_id);
205   sprintf(note_x_s, "%d", note_x);
206   flickcurl_add_param(fc, "note_x", note_x_s);
207   sprintf(note_y_s, "%d", note_y);
208   flickcurl_add_param(fc, "note_y", note_y_s);
209   sprintf(note_w_s, "%d", note_w);
210   flickcurl_add_param(fc, "note_w", note_w_s);
211   sprintf(note_h_s, "%d", note_h);
212   flickcurl_add_param(fc, "note_h", note_h_s);
213   flickcurl_add_param(fc, "note_text", note_text);
214 
215   flickcurl_end_params(fc);
216 
217   if(flickcurl_prepare(fc, "flickr.photos.notes.edit"))
218     goto tidy;
219 
220   doc = flickcurl_invoke(fc);
221   if(!doc)
222     goto tidy;
223 
224   result = 0;
225 
226   tidy:
227   if(fc->failed)
228     result = 1;
229 
230   return result;
231 }
232 
233 
234