src/messaging/telepathyengine_maemo.cpp
changeset 0 876b1a06bc25
equal deleted inserted replaced
-1:000000000000 0:876b1a06bc25
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the Qt Mobility Components.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 #include "qmessageglobal.h"
       
    42 #include "qmessagemanager.h"
       
    43 #include "qmessageaccount.h"
       
    44 #include "qmessageaccountid.h"
       
    45 #include "qmessageaccount_p.h"
       
    46 #include "qmessageaccountfilter.h"
       
    47 #include "qmessageaccountfilter_p.h"
       
    48 #include "qmessageservice.h"
       
    49 #include "qmessage.h"
       
    50 #include "telepathyengine_maemo_p.h"
       
    51 #include "maemohelpers_p.h"
       
    52 
       
    53 
       
    54 QTM_BEGIN_NAMESPACE
       
    55 
       
    56 Q_GLOBAL_STATIC(TelepathyEngine,telepathyEngine);
       
    57 
       
    58 TelepathyEngine::TelepathyEngine()
       
    59 {
       
    60   tpSession=TpSession::instance(TRUE); // Create as sync, telephony "ring" as default
       
    61 }
       
    62 
       
    63 TelepathyEngine::~TelepathyEngine()
       
    64 {
       
    65 
       
    66 }
       
    67 
       
    68 TelepathyEngine* TelepathyEngine::instance()
       
    69 {
       
    70     return telepathyEngine();
       
    71 }
       
    72 
       
    73 bool TelepathyEngine::sendMessage(QMessage &message)
       
    74 {
       
    75   bool retVal=false;
       
    76   QMessage::Type type=message.type();
       
    77   QMessageAccountId account=message.parentAccountId();
       
    78   QString cm=type == QMessage::Sms ? "ring" :  type == QMessage::InstantMessage ? account.toString() : "";
       
    79   QMessageAddressList toList=message.to();
       
    80   TpSessionAccount *tpsa=tpSession->getAccount(account.toString());
       
    81   // qDebug() << "sendMessage account:" << account.toString() << tpsa;
       
    82   if(!tpsa) return false;
       
    83   if(!cm.isEmpty()) {
       
    84     foreach(QMessageAddress to,toList) {
       
    85       connect(tpsa,SIGNAL(messageQueued(TpSessionAccount *,bool)),SLOT(onMessageQueued(TpSessionAccount *,bool)));
       
    86       opBusy=true;
       
    87       tpsa->sendMessageToAddress(to.addressee(),message.textContent());
       
    88       if(opBusy) loop.exec(); // Wait untill this message has been queued to sent next one
       
    89      // qDebug() << "sendMessage loop exit";
       
    90       retVal=true;
       
    91     };
       
    92   }
       
    93   else
       
    94     qDebug() << "TelepathyEngine::sendMessage unsupported message type" << type;
       
    95   return retVal;
       
    96 }
       
    97 
       
    98 
       
    99  void TelepathyEngine::onMessageSent(const Tp::Message &,TpSessionAccount *)
       
   100  {
       
   101     qDebug() << "onMessageSent:" ;
       
   102     // loop.quit();
       
   103  };
       
   104 
       
   105  void TelepathyEngine::onMessageQueued(TpSessionAccount *,bool status)
       
   106  {
       
   107      //qDebug() << "onMessageQueued:" << status;
       
   108      opBusy=false;
       
   109      loop.quit(); // If queuinf failed, exit loop
       
   110  };
       
   111 
       
   112 
       
   113 
       
   114 void TelepathyEngine::updateImAccounts() const
       
   115 {
       
   116 //    iDefaultImAccountId = QMessageAccountId();
       
   117 //  qDebug() << "TelepathyEngine::updateImAccounts";
       
   118     iAccounts.clear();
       
   119     foreach (TpSessionAccount *tpacc, tpSession->accounts) {
       
   120       //     qDebug() << "TelepathyEngine::updateImAccounts" << tpacc->acc->cmName() << " " << tpacc->acc->protocol() << " " << tpacc->acc->displayName();
       
   121         bool account_ok = tpacc->acc->isEnabled() && tpacc->acc->isValidAccount();
       
   122         QString cm=tpacc->acc->cmName();
       
   123         if (account_ok) {
       
   124             if(cm=="ring") { // Ring connection manager for cellular telephony
       
   125                 QString accountId = tpacc->acc->uniqueIdentifier();
       
   126                 QString accountName = "SMS";
       
   127                 QString accountAddress = "";
       
   128                 QMessageAccount account = QMessageAccountPrivate::from(QMessageAccountId(accountId),
       
   129                                                                        accountName,
       
   130                                                                        QMessageAddress(QMessageAddress::Phone, accountAddress),
       
   131                                                                        QMessage::Sms);
       
   132                //  qDebug() << "updateImAccounts Sms id:" << accountId << " accountName:" << accountName << "accountAddress" << accountAddress;
       
   133                 iAccounts.insert(accountId, account);
       
   134                 defaultSmsAccountId=accountId;
       
   135             } else {
       
   136                 QString accountId = tpacc->acc->uniqueIdentifier();
       
   137                 QString accountName = tpacc->acc->normalizedName();
       
   138                 QString accountAddress = tpacc->acc->normalizedName();
       
   139                 // Some ugly hardcoded hacks
       
   140                 if(tpacc->acc->protocol()=="skype") accountName+=QString("@skype");
       
   141                 if(!accountName.contains("@")) accountName+=QString("@")+tpacc->acc->parameters()["server"].toString();
       
   142                 // qDebug() << "updateImAccounts InstantMessage id:" << accountId << " accountName:" << accountName << "accountAddress" << accountAddress;
       
   143                 QMessageAccount account = QMessageAccountPrivate::from(QMessageAccountId(accountId),
       
   144                                                                        accountName,
       
   145                                                                        QMessageAddress(QMessageAddress::InstantMessage, accountAddress),
       
   146                                                                        QMessage::InstantMessage);
       
   147                 iAccounts.insert(accountId, account);
       
   148             }
       
   149 //            else qDebug() << "Protocol " << tpacc->acc->protocol() << "with connectionmanager " << cm << "Is not yet supported";
       
   150 //                if (strncmp(account_name_key, default_account, strlen(default_account))) iDefaultEmailAccountId = accountId;
       
   151         }
       
   152         }
       
   153 }
       
   154 
       
   155 QMessageAccountIdList TelepathyEngine::queryAccounts(const QMessageAccountFilter &filter, const QMessageAccountSortOrder &sortOrder,
       
   156                                                   uint limit, uint offset, bool &isFiltered, bool &isSorted) const
       
   157 {
       
   158     Q_UNUSED(sortOrder);
       
   159     Q_UNUSED(limit);
       
   160     Q_UNUSED(offset);
       
   161   //  qDebug() << "TelepathyEngine::queryAccounts";
       
   162     QMessageAccountIdList accountIds;
       
   163 
       
   164     updateImAccounts();
       
   165     foreach (QMessageAccount value, iAccounts) {
       
   166         accountIds.append(value.id());
       
   167     }
       
   168 
       
   169     MessagingHelper::filterAccounts(accountIds, filter);
       
   170     isFiltered = true;
       
   171 
       
   172     isSorted = false;
       
   173 
       
   174     return accountIds;
       
   175 }
       
   176 
       
   177 int TelepathyEngine::countAccounts(const QMessageAccountFilter &filter) const
       
   178 {
       
   179     bool isFiltered, isSorted;
       
   180     return queryAccounts(filter, QMessageAccountSortOrder(), 0, 0, isFiltered, isSorted).count();
       
   181 }
       
   182 
       
   183 QMessageAccount TelepathyEngine::account(const QMessageAccountId &id) const
       
   184 {
       
   185     updateImAccounts();
       
   186     return iAccounts[id.toString()];
       
   187 }
       
   188 
       
   189 QMessageAccountId TelepathyEngine ::defaultAccount(QMessage::Type type) const
       
   190 {
       
   191   //  qDebug() << "TelepathyEngine::defaultAccount";
       
   192     updateImAccounts();
       
   193     return defaultSmsAccountId;
       
   194 }
       
   195 
       
   196 #include "moc_telepathyengine_maemo_p.cpp"
       
   197 QTM_END_NAMESPACE