epoc-smsevent.cpp

Go to the documentation of this file.
00001 /*
00002  !concept {:name => "Observing SMS events",
00003    :desc => "Detecting and getting information about incoming and outgoing SMS messages."}
00004 */
00005 
00006 #include "epoc-smsevent.hpp"
00007 
00008 #if __SMSEVENT_ENABLED__
00009 
00010 #include "er_errors.h"
00011 #include "ld_logging.h"
00012 #include "sa_sensor_list_log_db.h"
00013 #include "ut_telno_epoc.hpp"
00014 #include "utils_cl2.h"
00015 
00016 // -------------------------------------------------------------------
00017 // the sensor object implementation...
00018 
00019 CTOR_IMPL_CSensor_smsevent;
00020 
00021 void CSensor_smsevent::ConstructL()
00022 {
00023   // nothing to do
00024 }
00025 
00026 CSensor_smsevent::~CSensor_smsevent()
00027 {
00028   delete iSmsEventNotifier;
00029 }
00030 
00031 #define iLogDb ac_LogDb(iAppContext)
00032 
00033 gboolean CSensor_smsevent::StartL(GError** error)
00034 {
00035   if (!IsActive()) {
00036     ActivateL();
00037     //log_db_log_status(iLogDb, NULL, "smsevent sensor started");
00038   }
00039   return TRUE;
00040 }
00041 
00042 void CSensor_smsevent::Stop()
00043 {
00044   if ((IsActive())) {
00045     Disactivate();
00046     //log_db_log_status(iLogDb, NULL, "smsevent sensor stopped");
00047   }
00048 }
00049 
00050 void CSensor_smsevent::ActivateL()
00051 {
00052   iSmsEventNotifier = CSmsEventNotifier::NewL();
00053   iSmsEventNotifier->SetHandler(this);
00054 }
00055 
00056 void CSensor_smsevent::Disactivate()
00057 {
00058   DELETE_Z(iSmsEventNotifier);
00059 }
00060 
00061 // Caller must free non-NULL result.
00062 static gchar* GetBodyL(CRichText& richText)
00063 {
00064   if (richText.HasMarkupData())
00065     return NULL; // unexpected
00066 
00067   TInt bodyLen = richText.DocumentLength();
00068   HBufC* hbuf16 = HBufC::NewLC(bodyLen);
00069   TPtr bodyDes(hbuf16->Des());
00070   richText.Extract(bodyDes);
00071   gchar* bodyStr = ConvToUtf8CString(bodyDes);
00072   CleanupStack::PopAndDestroy(); // hbuf16
00073 
00074   if (G_UNLIKELY(!bodyStr)) {
00075     er_fatal_oom;
00076     return NULL;;
00077   }
00078 
00079   //guilogf("smsevent: body '%s'", bodyStr);
00080   
00081   return bodyStr;
00082   //g_free(bodyStr);
00083 }
00084 
00085 static gchar* GetBody(CRichText& richText)
00086 {
00087   gchar* result = NULL;
00088   TRAPD(error, result = GetBodyL(richText));
00089   if (error) {
00090     er_log_symbian(0, error, "smsevent: failure getting body content");
00091   }
00092   return result;
00093 }
00094 
00095 void CSensor_smsevent::LogEvent(const char* evType, 
00096         const TDesC& aTelNoDes,
00097         CRichText& aBody)
00098 {
00099   //logg("sms event type: '%s'", evType);
00100 
00101   gchar* telNo = NULL;
00102   gchar* contactName = NULL;
00103 
00104   if (aTelNoDes.Length() > 0) {
00105     telNo = ConvToUtf8CString(aTelNoDes);
00106     if (G_UNLIKELY(!telNo)) {
00107       er_fatal_oom;
00108       return;
00109     }
00110     //logg("sms remote party number is '%s'", telNo);
00111     contactName = GetContactNameByPhoneNo(aTelNoDes);
00112     if (contactName) {
00113       //logg("sms remote party name is '%s'", contactName);
00114     } else {
00115       //logt("could not get sms remote party name");
00116     }
00117   } else {
00118     logt("could not get sms remote party phone number");
00119   }
00120 
00121   gchar* bodyText = NULL;
00122   if (ac_STATIC_GET(log_sms_body)) {
00123     bodyText = GetBody(aBody);
00124   }
00125 
00126   LogDb* logDb = GetLogDb();
00127   GError* localError = NULL;
00128   gboolean ok = log_db_log_smsevent(logDb, evType, telNo, contactName, bodyText, &localError);
00129   guilogf("smsevent: %s '%s' name='%s' body='%s'",
00130     evType, telNo, 
00131     contactName ? contactName : "(N/A)",
00132     bodyText ? bodyText : "");
00133   g_free(telNo);
00134   g_free(contactName);
00135   g_free(bodyText);
00136 
00137   if (G_UNLIKELY(!ok)) {
00138     gx_txtlog_fatal_error_free(localError);
00139     return;
00140   }
00141 }
00142 
00143 void CSensor_smsevent::handle_reception(const TMsvId& entry_id,
00144           const TMsvId& folder_id,
00145           const TDesC& senderDes,
00146           CRichText& body)
00147 {
00148   //logt("smsevent receive");
00149   LogEvent("recv", senderDes, body);
00150 }
00151 
00152 void CSensor_smsevent::handle_sending(const TMsvId& entry_id,
00153               const TDesC& senderDes,
00154               CRichText& body)
00155 {
00156   //logt("smsevent send");
00157   LogEvent("send", senderDes, body);
00158 }
00159 
00160 void CSensor_smsevent::handle_error(TInt errCode)
00161 {
00162   assert(errCode != 0);
00163 
00164   Stop();
00165 
00166   LogDb* logDb = GetLogDb();
00167   GError* localError = NULL;
00168   if (!log_db_log_status(logDb, &localError,
00169        "INACTIVATE: smsevent: error: %s (%d)",
00170        plat_error_strerror(errCode), errCode)) {
00171     gx_txtlog_fatal_error_free(localError);
00172     return;
00173   }
00174 }
00175 
00176 // We must stop using the notifier session.
00177 void CSensor_smsevent::handle_close()
00178 {
00179   Stop();
00180 
00181   LogDb* logDb = GetLogDb();
00182   GError* localError = NULL;
00183   if (!log_db_log_status(logDb, &localError,
00184        "INACTIVATE: smsevent: session termination")) {
00185     gx_txtlog_fatal_error_free(localError);
00186     return;
00187   }
00188 }
00189 
00190 #endif // __SMSEVENT_ENABLED__
00191 
00192 /**
00193 
00194 epoc-smsevent.cpp
00195 
00196 Copyright 2009 Helsinki Institute for Information Technology (HIIT)
00197 and the authors. All rights reserved.
00198 
00199 Authors: Tero Hasu <tero.hasu@hut.fi>
00200 
00201 Permission is hereby granted, free of charge, to any person
00202 obtaining a copy of this software and associated documentation files
00203 (the "Software"), to deal in the Software without restriction,
00204 including without limitation the rights to use, copy, modify, merge,
00205 publish, distribute, sublicense, and/or sell copies of the Software,
00206 and to permit persons to whom the Software is furnished to do so,
00207 subject to the following conditions:
00208 
00209 The above copyright notice and this permission notice shall be
00210 included in all copies or substantial portions of the Software.
00211 
00212 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00213 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00214 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00215 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
00216 BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
00217 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
00218 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
00219 SOFTWARE.
00220 
00221  **/

ContextLogger2—ContextLogger2 Logger Daemon Internals—Generated on Mon May 2 13:49:52 2011 by Doxygen 1.6.1