javaextensions/wma/sms_cbs/pushplugin/src/serverconnectionbase.cpp
branchRCL_3
changeset 19 04becd199f91
child 23 98ccebc37403
equal deleted inserted replaced
16:f5050f1da672 19:04becd199f91
       
     1 /*
       
     2 * Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 #include "logger.h"
       
    20 #include "fileutilities.h"
       
    21 #include "exceptionbase.h"
       
    22 #include "javacommonutils.h"
       
    23 #include "serverconnectionbase.h"
       
    24 
       
    25 using namespace java::util;
       
    26 using namespace java::fileutils;
       
    27 
       
    28 namespace java
       
    29 {
       
    30 namespace wma
       
    31 {
       
    32 
       
    33 const TInt KFirstMessage = 10000001;
       
    34 
       
    35 ServerConnectionBase::ServerConnectionBase(const std::wstring& aUri,
       
    36         const std::wstring& aFilter)
       
    37         :mIsAppLaunched(false), mIsListening(false),mPort(0), mUri(aUri),
       
    38         mFilter(aFilter), mMessagesOnStore(0), mNextMessageInStore(KFirstMessage),
       
    39         mFirstMessageInStore(-1)
       
    40 {
       
    41     JELOG2(EWMA);
       
    42 }
       
    43 
       
    44 void ServerConnectionBase::deleteMessage()
       
    45 {
       
    46     JELOG2(EWMA);
       
    47     std::wstring path(mMessageStoreDirName);
       
    48     path += JavaCommonUtils::intToWstring(mFirstMessageInStore);
       
    49     char* messagePath = JavaCommonUtils::wstringToUtf8(path);
       
    50     //Delete message from the store
       
    51     if ((remove(messagePath)) != 0)
       
    52     {
       
    53         // If deleting of message fails , increment the first message
       
    54         // in store so next read retrieves the next message.
       
    55         ELOG1(EWMA, "WMA : Removing File  Failed : %s", messagePath);
       
    56     }
       
    57     mFirstMessageInStore++;
       
    58     delete[] messagePath;
       
    59 }
       
    60 
       
    61 OS_EXPORT std::wstring ServerConnectionBase::getUri() const
       
    62 {
       
    63     JELOG2(EWMA);
       
    64     return mUri;
       
    65 }
       
    66 
       
    67 OS_EXPORT void ServerConnectionBase::setFilter(const std::wstring& aFilter)
       
    68 {
       
    69     JELOG2(EWMA);
       
    70     mFilter = aFilter;
       
    71 }
       
    72 
       
    73 OS_EXPORT std::wstring ServerConnectionBase::getFilter() const
       
    74 {
       
    75     JELOG2(EWMA);
       
    76     return mFilter;
       
    77 }
       
    78 
       
    79 OS_EXPORT int ServerConnectionBase::getMessagesOnStore() const
       
    80 {
       
    81     JELOG2(EWMA);
       
    82     return mMessagesOnStore;
       
    83 }
       
    84 
       
    85 int ServerConnectionBase::createMessageStore(std::wstring aDirectoryPath)
       
    86 {
       
    87     JELOG2(EWMA);
       
    88     aDirectoryPath += JavaCommonUtils::intToWstring(mPort);
       
    89     aDirectoryPath +=L"\\" ;
       
    90     mMessageStoreDirName = aDirectoryPath;
       
    91     LOG1(EWMA, EInfo , " Creating message store in path %S",
       
    92          mMessageStoreDirName.c_str());
       
    93     // See if directory is present
       
    94     if (FileUtilities::isDirectory(mMessageStoreDirName))
       
    95     {
       
    96         std::list<std::wstring> dirList = FileUtilities::getDirContentsList
       
    97                                           (mMessageStoreDirName);
       
    98         mMessagesOnStore = dirList.size();
       
    99         if (mMessagesOnStore> 0)
       
   100         {
       
   101             // Get the id of the first message in the store
       
   102             dirList.sort();
       
   103             mFirstMessageInStore = JavaCommonUtils::wstringToInt(
       
   104                                        dirList.front());
       
   105             // Get the id of the next message to be added to the store,
       
   106             // which will be one more than the value of the last
       
   107             // message in the store
       
   108             mNextMessageInStore = JavaCommonUtils::wstringToInt(dirList.back());
       
   109             mNextMessageInStore++;
       
   110         }
       
   111         return 0;
       
   112     }
       
   113     else
       
   114     {
       
   115         // Otherwise create a new directory
       
   116         return makeDirAll(mMessageStoreDirName);
       
   117     }
       
   118 }
       
   119 
       
   120 int ServerConnectionBase::makeDirAll(const std::wstring aDirPath)
       
   121 {
       
   122     JELOG2(EWMA);
       
   123     bool ableToOpen = true;
       
   124     std::wstring path;
       
   125     wchar_t *last, *tok, *delim =L"\\" ;
       
   126     wchar_t *stringToTokenize = new wchar_t[aDirPath.length()+ 1];
       
   127     wcscpy(stringToTokenize, aDirPath.c_str());
       
   128 
       
   129     struct stat temp;
       
   130 
       
   131     for (tok = wcstok(stringToTokenize,delim,&last); tok!=NULL;
       
   132             tok = wcstok(NULL, delim, &last))
       
   133     {
       
   134         path += std::wstring(tok);
       
   135         path += L"\\";
       
   136         char *dirName = JavaCommonUtils::wstringToUtf8(path);
       
   137         if (ableToOpen)
       
   138         {
       
   139             if (0 != lstat(dirName, &temp))
       
   140             {
       
   141                 ableToOpen = false;
       
   142                 if (mkdir(dirName,0666) < 0)
       
   143                 {
       
   144                     ELOG1(EWMA,"WMA : Directory Creation Failed : %s",dirName);
       
   145                     delete[] dirName;
       
   146                     delete[] stringToTokenize;
       
   147                     return -1;
       
   148                 }
       
   149             }
       
   150         }
       
   151         else
       
   152         {
       
   153             if (mkdir(dirName,0666) < 0)
       
   154             {
       
   155                 ELOG1(EWMA,"WMA : Directory Creation Failed : %s",dirName);
       
   156                 delete[] dirName;
       
   157                 delete[] stringToTokenize;
       
   158                 return -1;
       
   159             }
       
   160         }
       
   161         delete[] dirName;
       
   162     }
       
   163     delete[] stringToTokenize;
       
   164     return 0;
       
   165 }
       
   166 
       
   167 int ServerConnectionBase::removeDir(const std::wstring aDirPath)
       
   168 {
       
   169     JELOG2(EWMA);
       
   170     std::wstring path1;
       
   171     path1 +=  aDirPath;
       
   172     int error = 0;
       
   173     try
       
   174     {
       
   175         char* path = JavaCommonUtils::wstringToUtf8(path1);
       
   176         LOG1(EWMA, EInfo, "WMA : Removing Message Store %s",path);
       
   177         struct stat temp;
       
   178         if (0 != lstat(path, &temp))
       
   179         {
       
   180             ELOG1(EWMA, "WMA : Directory doesn't Exists : %s", path);
       
   181             delete[] path;
       
   182             return -1;
       
   183         }
       
   184         std::list<std::wstring> dirList =
       
   185             FileUtilities::getDirContentsList(aDirPath);
       
   186         std::list<std::wstring>::iterator it;
       
   187         if (dirList.size())
       
   188         {
       
   189             for (it = dirList.begin(); it != dirList.end(); it++)
       
   190             {
       
   191                 std::wstring element(aDirPath);
       
   192                 element += *it;
       
   193                 char *fileName = JavaCommonUtils::wstringToUtf8(element);
       
   194                 if ((error = remove(fileName)) != 0)
       
   195                 {
       
   196                     ELOG1(EWMA, "WMA : Removing File  Failed : %s", fileName);
       
   197                     delete[] fileName;
       
   198                     delete[] path;
       
   199                     return error;
       
   200                 }
       
   201                 delete[] fileName;
       
   202             }
       
   203         }
       
   204         if ((error = remove(path)) != 0)
       
   205         {
       
   206             ELOG1(EWMA, "WMA : Removing Directory  Failed : %s", path);
       
   207             delete[] path;
       
   208             return error;
       
   209         }
       
   210         delete[] path;
       
   211     }
       
   212     catch (ExceptionBase ex)
       
   213     {
       
   214         ELOG(EWMA,"WMA : Cought an exception while removing Dir");
       
   215     }
       
   216     return error;
       
   217 }
       
   218 
       
   219 }// end of namespace wma
       
   220 }// end of namespace java