1 /* -*- mode: C++; tab-width: 4; c-basic-offset: 4; -*- */
2 
3 /* AbiWord
4  * Copyright (C) 1998 AbiSource, Inc.
5  * Copyright (C) 2011 Ben Martin
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20  * 02110-1301 USA.
21  */
22 
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include "ut_types.h"
26 #include "ut_assert.h"
27 #include "ut_debugmsg.h"
28 #include "ut_string.h"
29 #include "ut_std_string.h"
30 #include "ut_iconv.h"
31 #include "ie_imp_RDF.h"
32 #include "pd_Document.h"
33 #include "xap_App.h"
34 #include "xap_EncodingManager.h"
35 
36 
37 #include "ap_Dialog_Id.h"
38 #include "xap_DialogFactory.h"
39 #include "xap_Dlg_Encoding.h"
40 #include "ap_Prefs.h"
41 
42 #include "pt_PieceTable.h"
43 #include "pf_Frag_Strux.h"
44 
45 #ifdef HAVE_CONFIG_H
46 #include "config.h"
47 #endif
48 
49 #include <sstream>
50 #include <list>
51 
52 #define IE_MIMETYPE_VCard			"text/x-vcard"
53 #define IE_MIMETYPE_Calendar        "text/calendar"
54 
IE_Imp_RDF_Sniffer(const char * n)55 IE_Imp_RDF_Sniffer::IE_Imp_RDF_Sniffer( const char * n )
56 	: IE_ImpSniffer( n, true )
57 {
58 }
59 
~IE_Imp_RDF_Sniffer()60 IE_Imp_RDF_Sniffer::~IE_Imp_RDF_Sniffer ()
61 {
62 }
63 
64 /*!
65   Check if buffer contains data meant for this importer.
66 
67  We don't attmpt to recognize since other filetypes (HTML) can
68  use the same encodings a text file can.
69  We also don't want to steal recognition when user wants to use
70  the Encoded Text importer.
71  */
72 UT_Confidence_t
recognizeContents(const char * szBuf,UT_uint32 iNumbytes)73 IE_Imp_RDF_Sniffer::recognizeContents( const char * szBuf, UT_uint32 iNumbytes )
74 {
75     UT_UNUSED( szBuf );
76     UT_UNUSED( iNumbytes );
77     return UT_CONFIDENCE_ZILCH;
78 }
79 
80 /**********/
81 /**********/
82 /**********/
83 
IE_Imp_RDF_VCard_Sniffer()84 IE_Imp_RDF_VCard_Sniffer::IE_Imp_RDF_VCard_Sniffer()
85     :
86     IE_Imp_RDF_Sniffer( IE_MIMETYPE_VCard )
87 {
88 }
89 
~IE_Imp_RDF_VCard_Sniffer()90 IE_Imp_RDF_VCard_Sniffer::~IE_Imp_RDF_VCard_Sniffer()
91 {
92 }
93 
getSuffixConfidence()94 const IE_SuffixConfidence * IE_Imp_RDF_VCard_Sniffer::getSuffixConfidence ()
95 {
96     static IE_SuffixConfidence ret[] = {
97         { "vcf", 	UT_CONFIDENCE_PERFECT 	},
98         { "vcard", 	UT_CONFIDENCE_PERFECT 	},
99         { "", 	UT_CONFIDENCE_ZILCH 	}
100     };
101     return ret;
102 }
103 
getMimeConfidence()104 const IE_MimeConfidence * IE_Imp_RDF_VCard_Sniffer::getMimeConfidence ()
105 {
106     static IE_MimeConfidence ret[] = {
107         { IE_MIME_MATCH_FULL, 	IE_MIMETYPE_VCard, 	UT_CONFIDENCE_GOOD 	},
108         { IE_MIME_MATCH_CLASS, 	"text", 			UT_CONFIDENCE_SOSO 	},
109         { IE_MIME_MATCH_BOGUS, 	"", 				UT_CONFIDENCE_ZILCH }
110     };
111     return ret;
112 }
113 
constructImporter(PD_Document * pDocument,IE_Imp ** ppie)114 UT_Error IE_Imp_RDF_VCard_Sniffer::constructImporter( PD_Document * pDocument,
115                                                       IE_Imp ** ppie)
116 {
117 	IE_Imp_RDF* p = new IE_Imp_RDF_VCard( pDocument, false );
118 	*ppie = p;
119 	return UT_OK;
120 }
121 
getDlgLabels(const char **,const char **,IEFileType *)122 bool IE_Imp_RDF_VCard_Sniffer::getDlgLabels( const char ** /*pszDesc*/,
123                                              const char ** /*pszSuffixList*/,
124                                              IEFileType * /*ft*/ )
125 {
126     return false;
127 	// *pszDesc = "VCard (.vcf, .vcard)";
128 	// *pszSuffixList = "*.vcf; *.vcard";
129 	// *ft = getFileType();
130 	// return true;
131 }
132 
133 
134 /**********/
135 /**********/
136 /**********/
137 
IE_Imp_RDF_Calendar_Sniffer()138 IE_Imp_RDF_Calendar_Sniffer::IE_Imp_RDF_Calendar_Sniffer()
139     :
140     IE_Imp_RDF_Sniffer( IE_MIMETYPE_Calendar )
141 {
142 }
143 
~IE_Imp_RDF_Calendar_Sniffer()144 IE_Imp_RDF_Calendar_Sniffer::~IE_Imp_RDF_Calendar_Sniffer()
145 {
146 }
147 
getSuffixConfidence()148 const IE_SuffixConfidence * IE_Imp_RDF_Calendar_Sniffer::getSuffixConfidence ()
149 {
150     static IE_SuffixConfidence ret[] = {
151 //        { "ical",  	    UT_CONFIDENCE_PERFECT 	},
152 //        { "ics",    	UT_CONFIDENCE_PERFECT 	},
153         { "", 	UT_CONFIDENCE_ZILCH 	}
154     };
155     return ret;
156 }
157 
getMimeConfidence()158 const IE_MimeConfidence * IE_Imp_RDF_Calendar_Sniffer::getMimeConfidence ()
159 {
160     static IE_MimeConfidence ret[] = {
161         { IE_MIME_MATCH_FULL, 	IE_MIMETYPE_Calendar, UT_CONFIDENCE_GOOD 	},
162         { IE_MIME_MATCH_CLASS, 	"text", 			UT_CONFIDENCE_SOSO 	},
163         { IE_MIME_MATCH_BOGUS, 	"", 				UT_CONFIDENCE_ZILCH }
164     };
165     return ret;
166 }
167 
constructImporter(PD_Document * pDocument,IE_Imp ** ppie)168 UT_Error IE_Imp_RDF_Calendar_Sniffer::constructImporter( PD_Document * pDocument,
169                                                       IE_Imp ** ppie)
170 {
171 	IE_Imp_RDF* p = new IE_Imp_RDF_Calendar( pDocument, false );
172 	*ppie = p;
173 	return UT_OK;
174 }
175 
getDlgLabels(const char **,const char **,IEFileType *)176 bool IE_Imp_RDF_Calendar_Sniffer::getDlgLabels( const char ** /*pszDesc*/,
177 												const char ** /*pszSuffixList*/,
178 												IEFileType * /*ft*/ )
179 {
180     return false;
181 	// *pszDesc = "Calendar (.ical, .ics)";
182 	// *pszSuffixList = "*.ical; *.ics";
183 	// *ft = getFileType();
184 	// return true;
185 }
186 
187 
188 
189 /*****************************************************************/
190 /*****************************************************************/
191 /*****************************************************************/
192 /*****************************************************************/
193 
IE_Imp_RDF(PD_Document * pDocument,bool bEncoded)194 IE_Imp_RDF::IE_Imp_RDF( PD_Document * pDocument, bool bEncoded )
195   : IE_Imp(pDocument)
196 {
197     UT_UNUSED( bEncoded );
198 }
199 
IE_Imp_RDF(PD_Document * pDocument,const char * encoding)200 IE_Imp_RDF::IE_Imp_RDF( PD_Document * pDocument, const char * encoding )
201   : IE_Imp(pDocument)
202 {
203     UT_UNUSED( encoding );
204 }
205 
~IE_Imp_RDF()206 IE_Imp_RDF::~IE_Imp_RDF ()
207 {
208 }
209 
210 
211 UT_Error
_loadFile(GsfInput * fp)212 IE_Imp_RDF::_loadFile( GsfInput * fp )
213 {
214     UT_UNUSED( fp );
215     return UT_ERROR;
216 }
217 
218 
219 bool
pasteFromBuffer(PD_DocumentRange * pDocRange,const unsigned char * pData,UT_uint32 lenData,const char * szEncoding)220 IE_Imp_RDF::pasteFromBuffer( PD_DocumentRange * pDocRange,
221                              const unsigned char * pData, UT_uint32 lenData,
222                              const char *szEncoding )
223 {
224     UT_UNUSED(szEncoding);
225 
226 	UT_return_val_if_fail(getDoc() == pDocRange->m_pDoc,false);
227 	UT_return_val_if_fail(pDocRange->m_pos1 == pDocRange->m_pos2,false);
228 
229     std::stringstream ss;
230     ss.write( (const char*)pData, lenData );
231     UT_DEBUGMSG(("IE_Imp_RDF::pasteFromBuffer() have data:%s\n", ss.str().c_str() ));
232 	setClipboard (pDocRange->m_pos1);
233 
234     bool ret = pasteFromBufferSS( pDocRange, ss, szEncoding );
235 
236 	return ret;
237 }
238 
239 bool
pasteFromBufferSS(PD_DocumentRange *,std::stringstream &,const char *)240 IE_Imp_RDF::pasteFromBufferSS( PD_DocumentRange * /*pDocRange*/,
241                                std::stringstream& /*ss*/,
242                                const char * /*szEncoding*/ )
243 {
244     UT_DEBUGMSG(("IE_Imp_RDF::pasteFromBufferSS() doing nothing...\n"));
245     return true;
246 }
247 
248 #include "xap_App.h"
249 #include "xap_Frame.h"
250 #include "fv_View.h"
251 #include "pd_DocumentRDF.h"
252 
253 std::pair< PT_DocPosition, PT_DocPosition >
insertTextWithXMLID(const std::string & textconst,const std::string & xmlid)254 IE_Imp_RDF::insertTextWithXMLID( const std::string& textconst,
255                                  const std::string& xmlid )
256 {
257     std::string text = " " + textconst + " ";
258     PT_DocPosition startpos = getDocPos();
259 	// FIXME
260     /*bool bRes =*/ appendSpan( text );
261     PT_DocPosition endpos = getDocPos();
262     startpos++;
263     endpos--;
264 
265     XAP_Frame* lff = XAP_App::getApp()->getLastFocussedFrame();
266     if(lff)
267     {
268         FV_View * pView = static_cast<FV_View*>( lff->getCurrentView() );
269         pView->selectRange( startpos, endpos );
270         pView->cmdInsertXMLID( xmlid );
271     }
272 
273     return std::make_pair( startpos, endpos );
274 }
275 
276 
277 
278 /**********/
279 /**********/
280 /**********/
281 
IE_Imp_RDF_VCard(PD_Document * pDocument,bool bEncoded)282 IE_Imp_RDF_VCard::IE_Imp_RDF_VCard( PD_Document * pDocument, bool bEncoded )
283     : IE_Imp_RDF( pDocument, bEncoded )
284 {
285 }
286 
IE_Imp_RDF_VCard(PD_Document * pDocument,const char * encoding)287 IE_Imp_RDF_VCard::IE_Imp_RDF_VCard( PD_Document * pDocument, const char * encoding )
288     : IE_Imp_RDF( pDocument, encoding )
289 {
290 }
291 
~IE_Imp_RDF_VCard()292 IE_Imp_RDF_VCard::~IE_Imp_RDF_VCard()
293 {
294 }
295 
296 
297 // #ifdef WITH_EVOLUTION_DATA_SERVER
298 // extern "C" {
299 //   #include <libebook/e-book.h>
300 // };
301 
302 // static std::string get( EVCard* c, const char* v )
303 // {
304 //     EVCardAttribute* a = e_vcard_get_attribute( c, v );
305 
306 //     if( a && e_vcard_attribute_is_single_valued(a) )
307 //     {
308 //         return e_vcard_attribute_get_value(a);
309 //     }
310 //     return "";
311 // }
312 
313 // static void addFoafProp( PD_DocumentRDFMutationHandle m,
314 //                          EVCard* c,
315 //                          const char* vckey,
316 //                          const PD_URI& uuidnode,
317 //                          const std::string& predend )
318 // {
319 //     PD_URI pred("http://xmlns.com/foaf/0.1/" + predend );
320 //     std::string objdata = get( c, vckey );
321 //     if( !objdata.empty() )
322 //     {
323 //         m->add( uuidnode, pred, PD_Literal( objdata ));
324 //     }
325 // }
326 
327 // #endif
328 
329 
330 bool
pasteFromBufferSS(PD_DocumentRange * pDocRange,std::stringstream & inputss,const char * szEncoding)331 IE_Imp_RDF_VCard::pasteFromBufferSS( PD_DocumentRange * pDocRange,
332                                      std::stringstream& inputss,
333                                      const char * szEncoding )
334 {
335 #ifndef WITH_EVOLUTION_DATA_SERVER
336 	UT_UNUSED(pDocRange);
337 	UT_UNUSED(inputss);
338 	UT_UNUSED(szEncoding);
339     UT_DEBUGMSG(("can not parse vcards!\n"));
340     return true;
341 #else
342 
343     UT_DEBUGMSG(("trying to get card for data:%s\n",inputss.str().c_str() ));
344 
345     PD_DocumentRDFHandle rdf = getDoc()->getDocumentRDF();
346     PD_RDFSemanticItemHandle obj = PD_RDFSemanticItem::createSemanticItem( rdf, "Contact" );
347     obj->importFromData( inputss, rdf, pDocRange );
348 
349     // if( EVCard* c = e_vcard_new_from_string( inputss.str().c_str() ) )
350     // {
351     //     std::string textrep = "";
352     //     typedef std::list< char* > charplist_t;
353     //     charplist_t textreplist;
354     //     textreplist.push_back( EVC_EMAIL );
355     //     textreplist.push_back( EVC_FN );
356     //     textreplist.push_back( EVC_NICKNAME );
357     //     textreplist.push_back( EVC_UID );
358     //     for( charplist_t::iterator iter = textreplist.begin();
359     //          iter != textreplist.end(); ++iter )
360     //     {
361     //         textrep = get( c, *iter );
362     //         if( !textrep.empty() )
363     //             break;
364     //     }
365     //     UT_DEBUGMSG(("have card!\n"));
366 
367     //     PD_DocumentRDFHandle rdf = getDoc()->getDocumentRDF();
368 
369     //     std::string fn    = get( c, EVC_FN );
370     //     std::string uid   = get( c, EVC_UID );
371     //     std::string xmlid = rdf->makeLegalXMLID( fn + "_" + uid );
372     //     std::string email = get( c, EVC_EMAIL );
373 
374     //     std::pair< PT_DocPosition, PT_DocPosition > se = insertTextWithXMLID( textrep, xmlid );
375     //     PT_DocPosition startpos = se.first;
376     //     PT_DocPosition   endpos = se.second;
377 
378     //     std::string uuid = "http://abicollab.net/rdf/foaf#" + xmlid;
379     //     PD_URI uuidnode(uuid);
380     //     PD_DocumentRDFMutationHandle m = rdf->createMutation();
381     //     m->add( PD_URI(uuid),
382     //             PD_URI("http://docs.oasis-open.org/opendocument/meta/package/common#idref"),
383     //             PD_Literal( xmlid ) );
384     //     m->add( PD_URI(uuid),
385     //             PD_URI("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"),
386     //             PD_Object("http://xmlns.com/foaf/0.1/Person") );
387     //     addFoafProp( m, c, EVC_TEL,      uuidnode, "phone" );
388     //     addFoafProp( m, c, EVC_NICKNAME, uuidnode, "nick" );
389     //     addFoafProp( m, c, EVC_FN,       uuidnode, "name" );
390     //     addFoafProp( m, c, EVC_N,        uuidnode, "givenName" );
391     //     addFoafProp( m, c, EVC_X_JABBER, uuidnode, "jabberID" );
392 
393     //     m->commit();
394     // }
395 
396     return true;
397 
398 #endif
399 }
400 
401 /**********/
402 /**********/
403 /**********/
404 
IE_Imp_RDF_Calendar(PD_Document * pDocument,bool bEncoded)405 IE_Imp_RDF_Calendar::IE_Imp_RDF_Calendar( PD_Document * pDocument, bool bEncoded )
406     : IE_Imp_RDF( pDocument, bEncoded )
407 {
408 }
409 
IE_Imp_RDF_Calendar(PD_Document * pDocument,const char * encoding)410 IE_Imp_RDF_Calendar::IE_Imp_RDF_Calendar( PD_Document * pDocument, const char * encoding )
411     : IE_Imp_RDF( pDocument, encoding )
412 {
413 }
414 
~IE_Imp_RDF_Calendar()415 IE_Imp_RDF_Calendar::~IE_Imp_RDF_Calendar()
416 {
417 }
418 
419 #ifdef WITH_LIBICAL
420 extern "C" {
421   #include <libical/ical.h>
422 };
423 
424 #if 0
425 static std::string tostr( time_t v )
426 {
427     std::stringstream ss;
428     ss << v;
429     return ss.str();
430 }
431 
432 
433 static void addCalProp( PD_DocumentRDFMutationHandle m,
434                         const PD_URI& uuidnode,
435                         const std::string& predend,
436                         const std::string& value )
437 {
438     std::string predBase = "http://www.w3.org/2002/12/cal/icaltzd#";
439     m->add( uuidnode,
440             PD_URI(predBase + predend),
441             PD_Literal( value ) );
442 }
443 static void addCalPropSZ( PD_DocumentRDFMutationHandle m,
444                           const PD_URI& uuidnode,
445                           const std::string& predend,
446                           const char* value )
447 {
448     std::string predBase = "http://www.w3.org/2002/12/cal/icaltzd#";
449     if( value )
450     {
451         addCalProp( m, uuidnode, predend, (std::string)value );
452     }
453 }
454 #endif
455 
456 // static void addFoafProp( PD_DocumentRDFMutationHandle m,
457 //                          ECalendar* c,
458 //                          const char* vckey,
459 //                          const PD_URI& uuidnode,
460 //                          const std::string& predend )
461 // {
462 //     PD_URI pred("http://xmlns.com/foaf/0.1/" + predend );
463 //     std::string objdata = get( c, vckey );
464 //     if( !objdata.empty() )
465 //     {
466 //         m->add( uuidnode, pred, PD_Literal( objdata ));
467 //     }
468 // }
469 
470 #endif
471 
472 
473 bool
pasteFromBufferSS(PD_DocumentRange * pDocRange,std::stringstream & inputss,const char * szEncoding)474 IE_Imp_RDF_Calendar::pasteFromBufferSS( PD_DocumentRange * pDocRange,
475                                         std::stringstream& inputss,
476                                         const char * szEncoding )
477 {
478 	UT_UNUSED(szEncoding);
479 #ifndef WITH_LIBICAL
480 	UT_UNUSED(pDocRange);
481 	UT_UNUSED(inputss);
482     UT_DEBUGMSG(("can not parse calendars!\n"));
483     return true;
484 #else
485 
486 
487     UT_DEBUGMSG(("trying to get calendar for data:%s\n",inputss.str().c_str() ));
488     PD_DocumentRDFHandle rdf = getDoc()->getDocumentRDF();
489     PD_RDFSemanticItemHandle obj = PD_RDFSemanticItem::createSemanticItem( rdf, "Event" );
490     obj->importFromData( inputss, rdf, pDocRange );
491 
492 
493     // if( icalcomponent* c = icalcomponent_new_from_string( inputss.str().c_str() ) )
494     // {
495     //     const char* desc = icalcomponent_get_description( c );
496     //     const char* loc  = icalcomponent_get_location( c );
497     //     const char* summary = icalcomponent_get_summary( c );
498     //     const char* uid  = icalcomponent_get_uid( c );
499     //     struct icaltimetype dtstart = icalcomponent_get_dtstart( c );
500     //     struct icaltimetype dtend   = icalcomponent_get_dtend( c );
501 
502     //     std::string textrep;
503     //     std::string xmlid;
504     //     if( summary )
505     //     {
506     //         xmlid += (std::string)summary + "_";
507     //         textrep = summary;
508     //     }
509     //     xmlid += uid;
510     //     PD_DocumentRDFHandle rdf = getDoc()->getDocumentRDF();
511     //     xmlid = rdf->makeLegalXMLID( xmlid );
512     //     if( textrep.empty() )
513     //     {
514     //         if( desc )
515     //             textrep = desc;
516     //         else
517     //             textrep = uid;
518     //     }
519 
520     //     std::pair< PT_DocPosition, PT_DocPosition > se = insertTextWithXMLID( textrep, xmlid );
521     //     PT_DocPosition startpos = se.first;
522     //     PT_DocPosition   endpos = se.second;
523 
524     //     std::string predBase = "http://www.w3.org/2002/12/cal/icaltzd#";
525     //     std::string uuid = "http://abicollab.net/rdf/cal#" + xmlid;
526     //     PD_URI uuidnode(uuid);
527     //     PD_DocumentRDFMutationHandle m = rdf->createMutation();
528     //     m->add( PD_URI(uuid),
529     //             PD_URI("http://docs.oasis-open.org/opendocument/meta/package/common#idref"),
530     //             PD_Literal( xmlid ) );
531     //     m->add( PD_URI(uuid),
532     //             PD_URI("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"),
533     //             PD_Object(predBase + "Vevent") );
534 
535     //     addCalPropSZ( m, uuidnode, "summary",     summary );
536     //     addCalPropSZ( m, uuidnode, "location",    loc );
537     //     addCalPropSZ( m, uuidnode, "uid",         uid );
538     //     addCalPropSZ( m, uuidnode, "description", desc );
539 
540     //     addCalProp( m, uuidnode, "dtstart", tostr(icaltime_as_timet( dtstart )));
541     //     addCalProp( m, uuidnode, "dtend",   tostr(icaltime_as_timet( dtend )));
542 
543     //     m->commit();
544 
545     // }
546 
547     return true;
548 
549 #endif
550 }
551