[RulesEngine] metadata extraction for mail attachment and venue association
[itsme:guglielmo.git] / src / rulesengine / rules.cpp
1 #include <QDebug>
2 #include "rules.h"
3 #include <QtDBus/QtDBus>
4
5 Q_DECLARE_METATYPE(QVector<QStringList>)
6
7 struct Rules::Private
8 {
9     QDBusInterface *iface;
10         QDBusInterface *extractorIface;
11 };
12
13
14 Rules::Rules()
15 {
16     qDebug() << "Rules Initialized...";
17     m_p=new Private();
18
19     qRegisterMetaType<QVector<QStringList> >();
20     qDBusRegisterMetaType<QVector<QStringList> >();
21
22     m_p->iface = new QDBusInterface("org.freedesktop.Tracker1",
23                                     "/org/freedesktop/Tracker1/Resources",
24                                     "org.freedesktop.Tracker1.Resources",
25                                     QDBusConnection::sessionBus());
26     //Dbus tracker-extract iface setup
27     m_p->extractorIface = new QDBusInterface("org.freedesktop.Tracker1.Extract",
28                                              "/org/freedesktop/Tracker1/Extract",
29                                              "org.freedesktop.Tracker1.Extract",
30                                              QDBusConnection::sessionBus());
31
32 };
33
34 Rules::~Rules()
35 {
36     delete m_p;
37 };
38
39 //FIXME Temp method for handling url and string differently
40 // this should not be needed...
41 QString Rules::escapeString( const QString& _s) {
42     if(_s.contains(QRegExp("\\<[^\\<\\>\\s]+\\>")))
43     {
44         //url found
45         return QString(_s);
46     }
47     else
48         return QString("\""+_s+"\"");
49 };
50
51 void Rules::emlTitle( const QString subject)
52 {
53     /**
54        This rules check if a new message was arrived and the subject
55        match a venue name. If this hapends we put the new message in the right venue.
56      **/
57
58     QString query("select ?channel where  ");
59     QString where = "\n { ";
60     //type must be message
61     where += "<"+subject+">";
62     where += " a ";
63     where += " nmo:Message . \n"; //or nmo:Email?
64     where += "<"+subject+">";
65     where += " nmo:messageSubject ";
66     where += " ?subject . \n";
67     where += " ?venue ";
68     where += " a ";
69     where += " itsme:Venue . \n";
70     where += " ?venue ";
71     where += " dc:title ";
72     where += " ?subject . \n";
73     where += " ?channel ";
74     where += " itsme:belongsToVenue ";
75     where += " ?venue . ";
76     where += " ?channel ";
77     where += " a ";
78     where += " itsme:MessageChannel ";
79     query +=  where + " } ";
80
81     // execute the query against tracker
82     QDBusReply< QVector<QStringList> > reply = m_p->iface->call("SparqlQuery", query);
83
84
85     if ( reply.isValid() )
86     { // we  have a valid response
87         QVector<QStringList> results = reply.value();
88
89         for (int b = 0; b < results.size(); ++b)
90         {
91                 //check if the message is already in that venue
92                 query = "ASK { <" + subject + "> itsme:belongsToChannel <"+ results.at(b).at(0) +"> }";
93                 QDBusReply< QVector<QStringList> > check = m_p->iface->call("SparqlQuery", query);
94
95                 if ( check.isValid() )
96                 {
97                         if(check.value().at(0).at(0)=="0")
98                         {
99                                 //insert the new tuple
100                                 query = " insert { <" + subject + "> itsme:belongsToChannel <"+results.at(b).at(0) +"> }";
101                                 m_p->iface->call("SparqlUpdate", query);
102                                 qDebug() << "emlTitle Rule Triggered";
103
104                         }
105                 } else {
106                         // Something wrong with dbus call
107                         qWarning() << "Dbus Call failed quering : " <<  qPrintable(check.error().message());
108                         qDebug() << query;
109                 }
110
111         }
112
113     } else {
114         // Something wrong with dbus call
115         qWarning() << "Dbus Call failed quering: " <<  qPrintable(reply.error().message());
116         qDebug() << query;
117     }
118
119
120 }
121
122 void Rules::emailContact( const QString subject, const QString contactType)
123 {
124     /**
125        This rules add relation beetween venue and contact present in email
126      **/
127         if(!contactType.isNull() && !contactType.isEmpty())
128         {
129                 QString query("select ?contactPerson ?venue where  ");
130                 QString where = "\n { ";
131                 //type must be message
132                 where += "<"+subject+">";
133                 where += " a ";
134                 where += " nmo:Message ; \n"; //or nmo:Email?
135                 where += " itsme:belongsToChannel ";
136                 where += " ?channel ; \n";
137                 where += " " +contactType+ " ?contact . \n";
138                 where += " ?contact nco:hasEmailAddress ?eaddres . \n";
139                 where += " ?contactPerson nco:hasEmailAddress ?eaddres  . \n";
140                 where += " ?contactPerson a nco:PersonContact  . \n";
141                 where += " ?channel ";
142                 where += " itsme:belongsToVenue ";
143                 where += " ?venue . \n";
144
145                 query +=  where + " } ";
146
147                 // execute the query against tracker
148                 QDBusReply< QVector<QStringList> > reply = m_p->iface->call("SparqlQuery", query);
149
150
151                 if ( reply.isValid() )
152                 { // we  have a valid response
153                         QVector<QStringList> results = reply.value();
154                         for (int b = 0; b < results.size(); ++b)
155                         {
156                                 //check if the message is already in that venue
157                                 query = "ASK { <" + results.at(b).at(0) + "> itsme:belongsToVenue <"+ results.at(b).at(1) +"> }";
158                                 QDBusReply< QVector<QStringList> > check = m_p->iface->call("SparqlQuery", query);
159
160                                 if ( check.isValid() )
161                                 {
162                                         if(check.value().at(0).at(0)=="0")
163                                         {
164                                                 //insert the new tuple
165                                                 query = " insert { <" + results.at(b).at(0) + "> itsme:belongsToVenue <"+results.at(b).at(1) +"> }";
166                                                 m_p->iface->call("SparqlUpdate", query);
167                                                 qDebug() << "emailContact Rule Triggered";
168
169                                         }
170                                 } else {
171                                         // Something wrong with dbus call
172                                         qWarning() << "Dbus Call failed quering : " <<  qPrintable(check.error().message());
173                                         qDebug() << query;
174                                 }
175
176                         }
177
178                 } else {
179                         // Something wrong with dbus call
180                         qWarning() << "Dbus Call failed quering: " <<  qPrintable(reply.error().message());
181                         qDebug() << query;
182                 }
183         }
184
185 }
186
187 void Rules::emailContactTo( const QString subject)
188 {
189         emailContact(subject, "nmo:to");
190 }
191
192 void Rules::emailContactCc( const QString subject)
193 {
194         emailContact(subject, "nmo:cc");
195 }
196
197 void Rules::emailContactBcc( const QString subject)
198 {
199         emailContact(subject, "nmo:bcc");
200 }
201
202 void Rules::replyInVenue( const QString subject )
203 {
204     /**
205        This rule check if a message is in reply of another message
206        that are already in a venue. If yes we put the new message in the same venue
207      **/
208     QString query("select ?channel where  ");
209     QString where = "\n { ";
210     //type must be message
211     where += "<"+subject+">";
212     where += " nmo:inReplyTo ";
213     where += " ?messageid . \n";
214     where += " ?item ";
215     where += " nmo:messageId "; //FIXME use messageId property or subject?
216     where += " ?messageid . \n";
217     where += " ?item ";
218     where += " itsme:belongsToChannel "; //FIXME what to do in belongs to more than one channel?
219     where += " ?channel . \n";
220     query +=  where + " } ";
221
222     QDBusReply< QVector<QStringList> > reply = m_p->iface->call("SparqlQuery", query);
223
224     if ( reply.isValid() )
225     {
226         QVector<QStringList> results = reply.value();
227
228         for (int b = 0; b < results.size(); ++b)
229         {
230
231                 query = "ASK { <" + subject + "> itmse:belongsToChannel <"+results.at(b).at(0)+"> }";
232                 QDBusReply< QVector<QStringList> > check = m_p->iface->call("SparqlQuery", query);
233
234                 if ( check.isValid() )
235                 {
236                         if(check.value().at(0).at(0)=="0")
237                         {
238                     /*
239                         First of all, here we remove eventual assignments due other rules.
240                         "reply-to" rule has priority
241                     */
242                     query = "DELETE { <" + subject + "> itsme:belongsToChannel ?a } WHERE { ?a a itsme:MessageChannel }";
243                     m_p->iface->call("SparqlUpdate", query);
244
245                                 //insert the new tuple
246                                 query = " insert { <" + subject + "> itsme:belongsToChannel <"+results.at(b).at(0)+"> }";
247                                 m_p->iface->call("SparqlUpdate", query);
248
249                                 qDebug() << "replyInVenue Rule Triggered";
250                         }
251
252                 } else {
253                         // Something wrong with dbus call
254                         qWarning() << "Dbus Call failed quering-: " <<  qPrintable(reply.error().message());
255                         qDebug() << query;
256                 }
257
258         }
259
260     } else {
261         // Something wrong with dbus call
262         qWarning() << "Dbus Call failed quering:- " <<  qPrintable(reply.error().message());
263         qDebug() << query;
264     }
265
266
267 }
268
269 void Rules::relatedMessage( const QString subject)
270 {
271     /**
272        This rule add cross relation between two message
273      **/
274
275     QString query("select ?item where  ");
276     QString where = "\n { ";
277     where += "<"+subject+">";
278     where += " nmo:inReplyTo ";
279     where += " ?messageid . \n";
280     where += " ?item ";
281     where += " nmo:messageId ";
282     where += " ?messageid . \n";
283     query +=  where + " } ";
284
285     QDBusReply< QVector<QStringList> > reply = m_p->iface->call("SparqlQuery", query);
286
287     if ( reply.isValid() ) {
288
289         QVector<QStringList> results = reply.value();
290         for (int b = 0; b < results.size(); ++b)
291         {
292
293             /*
294              * Now we chek&add related property to the whole conversation
295              */
296
297             query = "select ?item where  ";
298             query += "\n { ";
299             query += "<"+results.at(b).at(0)+">";
300             query += " itsme:relatedTo ";
301             query += " ?item ";
302             query += " } ";
303
304             QDBusReply< QVector<QStringList> > replyRelated = m_p->iface->call("SparqlQuery", query);
305
306             if ( replyRelated.isValid() ) {
307
308                 QVector<QStringList> resultsRelated = replyRelated.value();
309                 for (int c = 0; c < resultsRelated.size(); ++c)
310                 {
311                         query = "ASK { <"+ resultsRelated.at(c).at(0) +"> itsme:relatedTo <" + subject + "> }";
312                         QDBusReply< QVector<QStringList> > check2R = m_p->iface->call("SparqlQuery", query);
313                         if ( check2R.isValid() ) {
314                                 if(check2R.value().at(0).at(0)=="0") {
315                                         //insert the new tuple
316                                         query = " insert {  <"+ resultsRelated.at(c).at(0)  +"> itsme:relatedTo <"
317                                                 + subject + "> }";
318                                         m_p->iface->call("SparqlUpdate", query);
319                                         qDebug() << "relatedMessage In conversation Rule Triggered";
320
321                                 }
322                         } else {
323                                 // Something wrong with dbus call
324                                 qWarning() << "Dbus Call failed quering .-  " <<  qPrintable(reply.error().message());
325                                 qDebug() << query << resultsRelated.at(c).at(0);
326                         }
327
328                         query = "ASK { <"+ subject +"> itsme:relatedTo <" + resultsRelated.at(c).at(0) + "> }";
329                         QDBusReply< QVector<QStringList> > checkR = m_p->iface->call("SparqlQuery", query);
330                         if ( checkR.isValid() ) {
331                                 if(checkR.value().at(0).at(0)=="0") {
332                                         //insert the new tuple
333                                         query = " insert {  <"+ subject  +"> itsme:relatedTo <"
334                                                 + resultsRelated.at(c).at(0) + "> }";
335                                         m_p->iface->call("SparqlUpdate", query);
336                                         qDebug() << "relatedMessage In conversation 2 Rule Triggered";
337                                 }
338                         } else {
339                                 // Something wrong with dbus call
340                                 qWarning() << "Dbus Call failed quering - : " <<  qPrintable(reply.error().message());
341                                 qDebug() << query;
342                         }
343
344                 }
345
346             }
347
348             // add relation: incoming message->inreplyto message
349
350             query = "ASK { <" + subject + "> itsme:relatedTo <"+ results.at(b).at(0)+"> }";
351             QDBusReply< QVector<QStringList> > check = m_p->iface->call("SparqlQuery", query);
352
353             if ( check.isValid() )
354             {
355
356                 if(check.value().at(0).at(0)=="0")
357                 {
358                     //insert the new tuple
359                     query = " insert { <" + subject + "> itsme:relatedTo <"+ results.at(b).at(0) +"> }";
360                     m_p->iface->call("SparqlUpdate", query);
361                     qDebug() << "relatedMessage 1 Rule Triggered";
362                 }
363             } else {
364                 // Something wrong with dbus call
365                 qWarning() << "Dbus Call failed quering: " <<  qPrintable(reply.error().message()) << query;
366             }
367
368             // add relation: inreplyto message -> incoming message
369
370             query = "ASK { <"+ results.at(b).at(0) +"> itsme:relatedTo <" + subject + "> }";
371             QDBusReply< QVector<QStringList> > check2 = m_p->iface->call("SparqlQuery", query);
372
373             if ( check2.isValid() ) {
374
375                 if(check2.value().at(0).at(0)=="0") {
376                     //insert the new tuple
377                     query = " insert {  <"+ results.at(b).at(0)  +"> itsme:relatedTo <" + subject + "> }";
378                     m_p->iface->call("SparqlUpdate", query);
379                     qDebug() << "relatedMessage 2 Rule Triggered";
380
381                 }
382             } else {
383                 // Something wrong with dbus call
384                 qWarning() << "Dbus Call failed quering - - " <<  qPrintable(reply.error().message());
385                 qDebug() << query;
386             }
387
388
389         }
390
391     } else {
392         // Something wrong with dbus call
393         qWarning() << "Dbus Call failed quering :: " <<  qPrintable(reply.error().message());
394         qDebug() << query;
395     }
396
397
398 }
399
400 void Rules::emailExtraction( const QString subject)
401 {
402     /**
403        This trigger Email MetadataExtraction if needed
404      **/
405
406         QString query;
407
408         query = "ASK { <" + subject + "> a nmo:Email }";
409         QDBusReply< QVector<QStringList> > check = m_p->iface->call("SparqlQuery", query);
410
411         if ( check.isValid() )
412         {
413                 if(check.value().at(0).at(0)=="1")
414                 { //email message, go ahead
415
416                         query = "ASK { <" + subject + "> nmo:messageSubject ?subject }";
417                         QDBusReply< QVector<QStringList> > check_subject = m_p->iface->call("SparqlQuery", query);
418                         if ( check_subject.isValid() )
419                         {
420                                 if(check_subject.value().at(0).at(0)=="0")
421                                 {
422                                         //start extraction
423
424                                         if (m_p->extractorIface->isValid()) {
425                                                 query = "select ?url where  ";
426                                                 query += "\n { ";
427                                                 query += "<"+ subject +">";
428                                                 query += " nie:url ";
429                                                 query += " ?url ";
430                                                 query += " } ";
431
432                                                 QDBusReply< QVector<QStringList> > hasUrl = m_p->iface->call("SparqlQuery", query);
433
434                                                 if ( hasUrl.isValid() ) {
435                                                         QVector<QStringList> urls = hasUrl.value();
436                                                         for (int c = 0; c < urls.size(); ++c)
437                                                         {
438                                                                 QDBusMessage reply = m_p->extractorIface->call("GetMetadata", urls.at(c).at(0), "message/rfc822");
439                                                                 if ( reply.type() == QDBusMessage::ReplyMessage ) {
440                                                                         QString preUpdate = reply.arguments().at(0).toString();
441                                                                         QString embedded = reply.arguments().at(1).toString();
442                                                                         qDebug() << "Extracted metadata from : " << urls.at(c).at(0);
443                                                                         query = preUpdate  + " INSERT INTO <"+subject+"> { <"+subject+">  "+embedded+" } ";
444                                                                         m_p->iface->call("SparqlUpdate", query);
445                                                                         qDebug() << "emailExtraction Rule Triggered";
446                                                                 }
447                                                                 else
448                                                                         qWarning() << "Error In query" << query;
449                                                         }
450                                                 }
451                                         }
452                                 }
453                         } else {
454                                 // Something wrong with dbus call
455                                 qWarning() << "Dbus Call failed quering-: " <<  qPrintable(check_subject.error().message());
456                                 qDebug() << query;
457                         }
458                         // not an email, do nothing
459                 }
460         } else {
461                 // Something wrong with dbus call
462                 qWarning() << "Dbus Call failed quering-: " <<  qPrintable(check.error().message());
463                 qDebug() << query;
464         }
465 }
466
467 void Rules::relatedInverse( const QString subject )
468 {
469     //add if needed reverse relation
470     QString query;
471     query = "select ?item where  ";
472     query += "\n { ";
473     query += "<"+ subject +">";
474     query += " itsme:relatedTo ";
475     query += " ?item ";
476     query += " } ";
477
478     QDBusReply< QVector<QStringList> > replyRelated = m_p->iface->call("SparqlQuery", query);
479
480     if ( replyRelated.isValid() ) {
481         QVector<QStringList> resultsRelated = replyRelated.value();
482         for (int c = 0; c < resultsRelated.size(); ++c)
483         {
484                 query = "ASK { <"+ resultsRelated.at(c).at(0) +"> itsme:relatedTo <" + subject + "> }";
485                 QDBusReply< QVector<QStringList> > check2R = m_p->iface->call("SparqlQuery", query);
486                 if ( check2R.isValid() ) {
487                         if(check2R.value().at(0).at(0)=="0") {
488                                 //insert the new tuple
489                                 query = " insert {  <"+ resultsRelated.at(c).at(0)  +"> itsme:relatedTo <"
490                                         + subject + "> }";
491                                 m_p->iface->call("SparqlUpdate", query);
492                                 qDebug() << "Inverse relation Rule Triggered ";
493                         }
494                 } else {
495                         // Something wrong with dbus call
496                         qWarning() << "Dbus Call failed quering --.-  " <<  qPrintable(replyRelated.error().message());
497                         qDebug() << query << resultsRelated.at(c).at(0);
498                 }
499         }
500     }
501 }
502
503 void Rules::mailFolderToVenue( const QString subject )
504 {
505     QString query;
506
507     query = "SELECT ?c { <"+ subject +"> nfo:belongsToContainer ?f . ?f a nmo:MailFolder . "
508             "?r a mto:TransferElement . ?r mto:source ?f . ?r mto:destination ?c . "
509             "?c a itsme:MessageChannel }";
510     QDBusReply< QVector<QStringList> > replyChannel = m_p->iface->call("SparqlQuery", query);
511
512     if ( replyChannel.isValid() ) {
513         QVector<QStringList> resultsChannel = replyChannel.value();
514
515         if (resultsChannel.size () >= 1) {
516             query = "INSERT { <"+ subject  +"> itsme:belongsToChannel <"+ resultsChannel.at(0).at(0) + "> }";
517             m_p->iface->call("SparqlUpdate", query);
518         }
519     }
520 }
521
522 void Rules::fileToChannel( const QString subject )
523 {
524     //add a default file->channel association for some file type
525         QString nfo("http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#");
526         QString media(nfo+"Media");
527         QString document(nfo+"Document");
528         QString message(nfo+"Message");
529
530         QString mediaChannel("");
531         QString documentChannel("");
532         QString messageChannel("");
533
534     QString query;
535     query = "SELECT ?venue where  ";
536     query += "\n { ";
537     query += "<"+ subject +">";
538     query += " itsme:belongsToVenue ";
539     query += " ?venue ";
540     query += " } ";
541     QDBusReply< QVector<QStringList> > reply = m_p->iface->call("SparqlQuery", query);
542
543
544     query = "SELECT ?type where { <"+ subject +"> rdf:type ?type } ";
545     QDBusReply< QVector<QStringList> > replyType = m_p->iface->call("SparqlQuery", query);
546
547     if ( reply.isValid() )
548             {
549         QVector<QStringList> results = reply.value();
550
551         for (int c = 0; c < results.size(); ++c)
552                 {
553                 query = "SELECT ?mediaChannel where { ?mediaChannel a itsme:MediaChannel; itsme:belongsToVenue <"+ results.at(c).at(0) +">  }";
554                 QDBusReply< QVector<QStringList> > mediaReply = m_p->iface->call("SparqlQuery", query);
555                 if ( mediaReply.isValid() ) mediaChannel = mediaReply.value().at(0).at(0);
556
557                 query = "SELECT ?documentChannel where { ?documentChannel a itsme:DocumentChannel; itsme:belongsToVenue <"+ results.at(c).at(0) +">  }";
558                 QDBusReply< QVector<QStringList> > documentReply = m_p->iface->call("SparqlQuery", query);
559                 if ( documentReply.isValid() ) documentChannel = documentReply.value().at(0).at(0);
560
561                 query = "SELECT ?messageChannel where { ?messageChannel a itsme:MessageChannel; itsme:belongsToVenue <"+ results.at(c).at(0) +">  }";
562                 QDBusReply< QVector<QStringList> > messageReply = m_p->iface->call("SparqlQuery", query);
563                 if ( messageReply.isValid() ) messageChannel = messageReply.value().at(0).at(0);
564
565                 if ( replyType.isValid() )
566                         {
567                                 QVector<QStringList> types = replyType.value();
568
569                                 for (int t = 0; t < types.size(); ++t)
570                                         {
571                                                 QString channelSubject;
572                                                 if (types.at(t).at(0) == media)
573                                                         channelSubject = mediaChannel;
574                                                 else if(types.at(t).at(0) == document)
575                                                         channelSubject = documentChannel;
576                                                 else if(types.at(t).at(0) == message)
577                                                         channelSubject = messageChannel;
578
579                                                 if(!channelSubject.isEmpty())
580                                                         {
581                                                                 query = "INSERT { <"+ subject +"> itsme:belongsToChannel <"+ channelSubject +"> } ";
582                                                                 m_p->iface->call("SparqlUpdate", query);
583
584                                                                 query = "DELETE { <"+ subject +"> itsme:belongsToVenue <"+ results.at(c).at(0) +"> }"
585                                                                         " WHERE { <"+ subject +"> itsme:belongsToVenue <"+ results.at(c).at(0) +"> } ";
586                                                                 m_p->iface->call("SparqlUpdate", query);
587                                                                 qDebug() << "fileToChannel rule triggered";
588                                                         }
589                                         }
590                         }
591
592                 messageChannel = "";
593                 documentChannel = "";
594                 mediaChannel = "";
595
596                 }
597             }
598 }
599
600 void Rules::venueToMailFolder( const QString subject )
601 {
602     QString query;
603
604     /*
605         First of all, we check the subject is a mail
606     */
607     query = "ASK { <" + subject + "> a nmo:Email }";
608     QDBusReply< QVector<QStringList> > check = m_p->iface->call("SparqlQuery", query);
609
610     if ( check.value().at(0).at(0) == "1" ) {
611         query = "SELECT ?f { <"+ subject +"> nmo:isSent true . <"+ subject +"> itsme:belongsToChannel ?c . "
612                 "?c a itsme:MessageChannel . "
613                 "?r a mto:TransferElement . ?r mto:source ?c . ?r mto:destination ?f . "
614                 "?f a nmo:MailFolder }";
615         QDBusReply< QVector<QStringList> > replyChannel = m_p->iface->call("SparqlQuery", query);
616
617         if ( replyChannel.isValid() ) {
618                 QVector<QStringList> resultsChannel = replyChannel.value();
619
620                 if (resultsChannel.size () >= 1) {
621                 query = "INSERT { <"+ subject  +"> itsme:belongsToChannel <"+ resultsChannel.at(0).at(0) + "> }";
622                 }
623                 else {
624                 /*
625                         Warning!!! Limbo's ID hard coded!!!
626                 */
627                 query = "INSERT { <"+ subject  +"> itsme:belongsToChannel ?a } \
628                         WHERE { ?a a itsme:MessageChannel . ?a itsme:belongsToVenue <itsme://0> }";
629                 }
630
631                 m_p->iface->call("SparqlUpdate", query);
632         }
633     }
634 }
635
636 void Rules::emailAttachHandler( const QString subject)
637 {
638     /**
639        This extract metadata from mail attachment
640        and associate it with the relative venue (channel) or limbo
641      **/
642
643         QString query;
644
645         query = "select ?uri ?url { <" + subject + "> nmo:hasAttachment ?uri . ?uri nie:url ?url }";
646         QDBusReply< QVector<QStringList> > check = m_p->iface->call("SparqlQuery", query);
647
648         if ( check.isValid() )
649         {
650           QVector<QStringList> attachment = check.value();
651           for (int index = 0; index < attachment.size(); ++index)
652           {
653
654                   QDBusMessage reply = m_p->extractorIface->call("GetMetadata", attachment.at(index).at(1), "");
655                   if ( reply.type() == QDBusMessage::ReplyMessage ) {
656                           QString preUpdate = reply.arguments().at(0).toString();
657                           QString embedded = reply.arguments().at(1).toString();
658                           qDebug() << "Extracted metadata from : " << attachment.at(index).at(1);
659                           query = preUpdate  + " INSERT INTO <"+attachment.at(index).at(0)+"> { <"+attachment.at(index).at(0)+">  "+embedded+" } ";
660                           m_p->iface->call("SparqlUpdate", query);
661                           qDebug() << "emailAttachExtraction Rule Triggered";
662                           
663                           QString mtype;
664                           query = "select ?type where { <"+ attachment.at(index).at(0) +"> rdf:type ?type } " ;
665                           QDBusReply< QVector<QStringList> > typeRes = m_p->iface->call("SparqlQuery", query);
666                                 if ( typeRes.isValid() )
667                                         {
668                                                 QVector<QStringList> typeList = typeRes.value();
669                                                 
670                                                 for (int ti = 0; ti < typeList.size(); ++ti)
671                                                         {
672                                                                 if(typeList.at(ti).at(0).endsWith("Media"))
673                                                                         {                                                 
674                                                                                 mtype = "itsme:MediaChannel";
675                                                                                 break;
676                                                                         }
677                                                                 else
678                                                                         mtype = "itsme:DocumentChannel";
679                                                         }
680
681                                                 //TODO remove this query and insert the "where" into the "insert" !
682                                                 query = "SELECT ?channel { <"+ subject +"> itsme:belongsToChannel ?messChan . "
683                                                         " ?messChan itsme:belongsToVenue ?venue . "
684                                                         " ?channel a " + mtype +"  . ?channel itsme:belongsToVenue ?venue } ";
685                                                 
686                                                 QDBusReply< QVector<QStringList> > replyChannel = m_p->iface->call("SparqlQuery", query);
687                                                 
688                                                 if ( replyChannel.isValid() ) {
689                                                         QVector<QStringList> resultsChannel = replyChannel.value();
690                                                         
691                                                         if (resultsChannel.size () >= 1) {
692                                                                 query = "INSERT { <"+ attachment.at(index).at(0)  +"> itsme:belongsToChannel <"+ resultsChannel.at(0).at(0) + "> }";
693                                                                 m_p->iface->call("SparqlUpdate", query);
694                                                                 
695                                                         }
696                                                 }
697                                         }
698                                 
699                                 
700                   }
701                   else
702                           qWarning() << "Error in MetadataExtraction!" << query;
703           }
704           
705           
706         }
707 }
708
709 QString Rules::execute( const QStringList & p_sSubjects)
710 {
711     for (int i = 0; i < p_sSubjects.size(); ++i)
712     {
713         emlTitle(p_sSubjects.at(i));
714         replyInVenue(p_sSubjects.at(i));
715         relatedInverse(p_sSubjects.at(i));
716         relatedMessage(p_sSubjects.at(i));
717         emailExtraction(p_sSubjects.at(i));
718         emailContactTo(p_sSubjects.at(i));
719         emailContactCc(p_sSubjects.at(i));
720         emailContactBcc(p_sSubjects.at(i));
721         fileToChannel(p_sSubjects.at(i));
722     }
723
724     return "Done";
725 }
726
727 void Rules::SubjectAddedHandler( const QStringList subjects)
728 {
729         //qDebug() << "Subject Added: start postprocessing. ";
730
731     for (int i = 0; i < subjects.size(); ++i)
732     {
733         emlTitle(subjects.at(i));
734         relatedInverse(subjects.at(i));
735         relatedMessage(subjects.at(i));
736         emailExtraction(subjects.at(i));
737         mailFolderToVenue(subjects.at(i));
738         venueToMailFolder(subjects.at(i));
739         emailContactTo(subjects.at(i));
740         emailContactCc(subjects.at(i));
741         emailContactBcc(subjects.at(i));
742
743         /*
744             "reply-to" rule has priority over others (venueToMailFolder()), so is evaluated at
745             last. The function already provides to remove eventual previous assignments
746         */
747         replyInVenue(subjects.at(i));
748         emailAttachHandler(subjects.at(i));
749
750     }
751 }; 
752
753 void Rules::SubjectChangedHandler( const QStringList subjects, const QStringList predicates)
754 {
755     //qDebug() << " Subject Changed " << subjects << predicates;
756
757     for (int i = 0; i < subjects.size(); ++i)
758     {
759         emlTitle(subjects.at(i));
760         replyInVenue(subjects.at(i));
761         relatedInverse(subjects.at(i));
762         relatedMessage(subjects.at(i));
763         emailExtraction(subjects.at(i));
764         emailContactTo(subjects.at(i));
765         emailContactCc(subjects.at(i));
766         emailContactBcc(subjects.at(i));
767         fileToChannel(subjects.at(i));
768     }
769 }