qtmobility/src/messaging/modestengine_maemo.cpp
changeset 1 2b40d63a9c3d
child 4 90517678cc4f
equal deleted inserted replaced
0:cfcbf08528c4 1:2b40d63a9c3d
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 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 "modestengine_maemo_p.h"
       
    42 #include "maemohelpers_p.h"
       
    43 #include "qmessageaccount.h"
       
    44 #include "qmessageaccount_p.h"
       
    45 #include "qmessageaccountfilter.h"
       
    46 #include "qmessageaccountfilter_p.h"
       
    47 #include "qmessageservice.h"
       
    48 #include <libmodest-dbus-client/libmodest-dbus-client.h>
       
    49 #include <libmodest-dbus-client/libmodest-dbus-api.h>
       
    50 #include <QUrl>
       
    51 #include <QtDBus/QtDBus>
       
    52 #include <dbus/dbus.h>
       
    53 
       
    54 #include <QDebug>
       
    55 
       
    56 QTM_BEGIN_NAMESPACE
       
    57 
       
    58 /* configuration key definitions for modest */
       
    59 #define MODESTENGINE_ACCOUNT_NAMESPACE         "/apps/modest/accounts"
       
    60 #define MODESTENGINE_SERVER_ACCOUNT_NAMESPACE  "/apps/modest/server_accounts"
       
    61 #define MODESTENGINE_DEFAULT_ACCOUNT           "/apps/modest/default_account"
       
    62 #define MODESTENGINE_ACCOUNT_ENABLED           "enabled"
       
    63 #define MODESTENGINE_ACCOUNT_DISPLAY_NAME      "display_name"
       
    64 #define MODESTENGINE_ACCOUNT_FULLNAME          "fullname"
       
    65 #define MODESTENGINE_ACCOUNT_EMAIL             "email"
       
    66 #define MODESTENGINE_ACCOUNT_STORE_ACCOUNT     "store_account"
       
    67 #define MODESTENGINE_ACCOUNT_TRANSPORT_ACCOUNT "transport_account"
       
    68 
       
    69 Q_GLOBAL_STATIC(ModestEngine,modestEngine);
       
    70 
       
    71 ModestEngine::ModestEngine()
       
    72 {
       
    73     g_type_init();
       
    74     m_gconfclient = gconf_client_get_default();
       
    75     if (!m_gconfclient) {
       
    76         qWarning("qtmessaging: could not get gconf client");
       
    77     } else {
       
    78         updateEmailAccounts();
       
    79     }
       
    80 }
       
    81 
       
    82 ModestEngine::~ModestEngine()
       
    83 {
       
    84     g_object_unref(m_gconfclient);
       
    85     m_gconfclient=NULL;
       
    86 }
       
    87 
       
    88 ModestEngine* ModestEngine::instance()
       
    89 {
       
    90     return modestEngine();
       
    91 }
       
    92 
       
    93 void ModestEngine::updateEmailAccounts() const
       
    94 {
       
    95     iDefaultEmailAccountId = QMessageAccountId();
       
    96     iAccounts.clear();
       
    97 
       
    98     GSList *accounts;
       
    99     GError *error = NULL;
       
   100 
       
   101     accounts = gconf_client_all_dirs(m_gconfclient, MODESTENGINE_ACCOUNT_NAMESPACE, &error);
       
   102     if (error) {
       
   103 #ifdef MESSAGING_DEBUG
       
   104         qDebug("qtmessaging: failed to list '%s': '%s'", MODESTENGINE_ACCOUNT_NAMESPACE, error->message);
       
   105 #endif
       
   106         g_error_free(error);
       
   107     } else {
       
   108         gchar *default_account = gconf_client_get_string(m_gconfclient, MODESTENGINE_DEFAULT_ACCOUNT, &error);
       
   109         if (error) {
       
   110             qWarning("qtmessaging: failed to get '%s': %s", MODESTENGINE_DEFAULT_ACCOUNT, error->message);
       
   111             g_error_free(error);
       
   112         }
       
   113 
       
   114         GSList *iter = accounts;
       
   115         while (iter) {
       
   116             if (!(iter->data)) {
       
   117                 iter = iter->next;
       
   118                 continue;
       
   119             }
       
   120 
       
   121             const gchar* account_name_key = (const gchar*)iter->data;
       
   122 
       
   123             gboolean account_ok = FALSE;
       
   124             // Check if account is enabled
       
   125             if (account_name_key) {
       
   126                 gchar* key = g_strconcat(account_name_key, "/", MODESTENGINE_ACCOUNT_ENABLED, NULL);
       
   127                 account_ok = gconf_client_get_bool(m_gconfclient, key, NULL);
       
   128                 g_free(key);
       
   129             }
       
   130             // Check if account store is defined
       
   131             if (account_ok) {
       
   132                 gchar* key = g_strconcat(account_name_key, "/", MODESTENGINE_ACCOUNT_STORE_ACCOUNT, NULL);
       
   133                 gchar* server_account_name = gconf_client_get_string(m_gconfclient, key, NULL);
       
   134                 if (server_account_name) {
       
   135                     gchar* key = g_strconcat(MODESTENGINE_SERVER_ACCOUNT_NAMESPACE, "/", server_account_name, NULL);
       
   136                     if (!gconf_client_dir_exists(m_gconfclient, key, NULL)) {
       
   137                         account_ok = FALSE;
       
   138                     }
       
   139                     g_free(server_account_name);
       
   140                 }
       
   141                 g_free(key);
       
   142             }
       
   143             // Check if account transport is defined
       
   144             if (account_ok) {
       
   145                 gchar* key = g_strconcat(account_name_key, "/", MODESTENGINE_ACCOUNT_TRANSPORT_ACCOUNT, NULL);
       
   146                 gchar* server_account_name = gconf_client_get_string(m_gconfclient, key, NULL);
       
   147                 if (server_account_name) {
       
   148                     gchar* key = g_strconcat(MODESTENGINE_SERVER_ACCOUNT_NAMESPACE, "/", server_account_name, NULL);
       
   149                     if (!gconf_client_dir_exists(m_gconfclient, key, NULL)) {
       
   150                         account_ok = FALSE;
       
   151                     }
       
   152                     g_free(server_account_name);
       
   153                 }
       
   154                 g_free(key);
       
   155             }
       
   156 
       
   157             if (account_ok) {
       
   158                 QString accountId = QString::fromUtf8(account_name_key);
       
   159                 gchar* name_key = g_strconcat(account_name_key, "/", MODESTENGINE_ACCOUNT_DISPLAY_NAME, NULL);
       
   160                 QString accountName = QString::fromUtf8(gconf_client_get_string(m_gconfclient, name_key, NULL));
       
   161                 g_free(name_key);
       
   162                 gchar* email_key = g_strconcat(account_name_key, "/", MODESTENGINE_ACCOUNT_EMAIL, NULL);
       
   163                 QString accountAddress = QString::fromUtf8(gconf_client_get_string(m_gconfclient, email_key, NULL));
       
   164                 g_free(email_key);
       
   165                 QMessageAccount account = QMessageAccountPrivate::from(QMessageAccountId(accountId),
       
   166                                                                        accountName,
       
   167                                                                        QMessageAddress(QMessageAddress::Email, accountAddress),
       
   168                                                                        QMessage::Email);
       
   169                 iAccounts.insert(accountId, account);
       
   170 
       
   171                 if (strncmp(account_name_key, default_account, strlen(default_account))) {
       
   172                     iDefaultEmailAccountId = accountId;
       
   173                 }
       
   174             }
       
   175 
       
   176             g_free(iter->data);
       
   177             iter->data = NULL;
       
   178             iter = g_slist_next(iter);
       
   179         }
       
   180         // strings were freed in while loop
       
   181         // => it's enough to just free accounts list
       
   182         g_slist_free(accounts);
       
   183         g_free(default_account);
       
   184     }
       
   185 }
       
   186 
       
   187 QMessageAccountIdList ModestEngine::queryAccounts(const QMessageAccountFilter &filter, const QMessageAccountSortOrder &sortOrder,
       
   188                                                   uint limit, uint offset, bool &isFiltered, bool &isSorted) const
       
   189 {
       
   190     QMessageAccountIdList accountIds;
       
   191 
       
   192     updateEmailAccounts();
       
   193     foreach (QMessageAccount value, iAccounts) {
       
   194         accountIds.append(value.id());
       
   195     }
       
   196 
       
   197     MessagingHelper::filterAccounts(accountIds, filter);
       
   198     isFiltered = true;
       
   199 
       
   200     isSorted = false;
       
   201 
       
   202     return accountIds;
       
   203 }
       
   204 
       
   205 int ModestEngine::countAccounts(const QMessageAccountFilter &filter) const
       
   206 {
       
   207     bool isFiltered, isSorted;
       
   208     return queryAccounts(filter, QMessageAccountSortOrder(), 0, 0, isFiltered, isSorted).count();
       
   209 }
       
   210 
       
   211 QMessageAccount ModestEngine::account(const QMessageAccountId &id) const
       
   212 {
       
   213     updateEmailAccounts();
       
   214     return iAccounts[id.toString()];
       
   215 }
       
   216 
       
   217 QMessageAccountId ModestEngine::defaultAccount(QMessage::Type type) const
       
   218 {
       
   219     updateEmailAccounts();
       
   220     return iDefaultEmailAccountId;
       
   221 }
       
   222 
       
   223 bool ModestEngine::sendEmail(QMessage &message)
       
   224 {
       
   225     return composeEmail(message);
       
   226 }
       
   227 
       
   228 bool ModestEngine::composeEmail(const QMessage &message)
       
   229 {
       
   230     QString mailString("mailto:");
       
   231     QList<QMessageAddress> list(message.to());
       
   232     if (!list.empty()){
       
   233         for (int i = 0; i < list.size(); ++i) {
       
   234             if (i == 0) {
       
   235                 mailString += list.at(i).recipient();
       
   236             } else {
       
   237                 mailString += QString("%2C%20");
       
   238                 mailString += list.at(i).recipient();
       
   239             }
       
   240         }
       
   241     }
       
   242 
       
   243     bool firstParameter = true;
       
   244 
       
   245     QString subject = message.subject();
       
   246     if (!subject.isEmpty()) {
       
   247         if (firstParameter) {
       
   248             mailString += QString("?");
       
   249             firstParameter = false;
       
   250         } else {
       
   251             mailString += QString("&");
       
   252         }
       
   253         mailString += QString("subject=");
       
   254         mailString += subject;
       
   255     }
       
   256 
       
   257     QString body = message.textContent();
       
   258     if (!body.isEmpty()) {
       
   259         if (firstParameter) {
       
   260             mailString += QString("?");
       
   261             firstParameter = false;
       
   262         } else {
       
   263             mailString += QString("&");
       
   264         }
       
   265         mailString += QString("body=");
       
   266         mailString += QString(body);
       
   267     }
       
   268 
       
   269     QUrl url(mailString);
       
   270 
       
   271     QDBusInterface dbusif(MODEST_DBUS_SERVICE, MODEST_DBUS_OBJECT, MODEST_DBUS_IFACE);
       
   272     QDBusMessage msg = dbusif.call(MODEST_DBUS_METHOD_MAIL_TO, url.toString());
       
   273 
       
   274     return true;
       
   275 }
       
   276 
       
   277 bool ModestEngine::queryMessages(QMessageService& messageService, const QMessageFilter &filter, const QMessageSortOrder &sortOrder, uint limit, uint offset) const
       
   278 {
       
   279     return true;
       
   280 }
       
   281 
       
   282 bool ModestEngine::queryMessages(QMessageService& messageService, const QMessageFilter &filter, const QString &body, QMessageDataComparator::MatchFlags matchFlags, const QMessageSortOrder &sortOrder, uint limit, uint offset) const
       
   283 {
       
   284     return true;
       
   285 }
       
   286 
       
   287 bool ModestEngine::countMessages(QMessageService& messageService, const QMessageFilter &filter)
       
   288 {
       
   289     return true;
       
   290 }
       
   291 
       
   292 
       
   293 QTM_END_NAMESPACE