qthighway/xqserviceutil/src/xqsharablefile.cpp
changeset 1 2b40d63a9c3d
child 4 90517678cc4f
equal deleted inserted replaced
0:cfcbf08528c4 1:2b40d63a9c3d
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 *
       
     5 * This program is free software: you can redistribute it and/or modify
       
     6 * it under the terms of the GNU Lesser General Public License as published by
       
     7 * the Free Software Foundation, version 2.1 of the License.
       
     8 * 
       
     9 * This program is distributed in the hope that it will be useful,
       
    10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    12 * GNU Lesser General Public License for more details.
       
    13 *
       
    14 * You should have received a copy of the GNU Lesser General Public License
       
    15 * along with this program.  If not, 
       
    16 * see "http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html/".
       
    17 *
       
    18 * Description:                                                         
       
    19 *
       
    20 */
       
    21 
       
    22 #include "xqservicelog.h"
       
    23 #include "xqSharableFile.h"
       
    24 #include <QVariant>
       
    25 
       
    26 
       
    27 XQSharableFile::XQSharableFile() :
       
    28    mHandle(0)
       
    29 {
       
    30     XQSERVICE_DEBUG_PRINT("XQSharableFile::XQSharableFile");
       
    31 }
       
    32 
       
    33 XQSharableFile::XQSharableFile(RFile &file)
       
    34 {
       
    35     XQSERVICE_DEBUG_PRINT("XQSharableFile::XQSharableFile(RFile)");
       
    36     setHandle(file);
       
    37 }
       
    38 
       
    39 XQSharableFile::~XQSharableFile()
       
    40 {
       
    41     XQSERVICE_DEBUG_PRINT("XQSharableFile::~XQSharableFile");
       
    42     // One need to call close() explicitelly when all done
       
    43 }
       
    44 
       
    45 
       
    46 bool XQSharableFile::setHandle(RFile &file)
       
    47 {
       
    48     XQSERVICE_DEBUG_PRINT("XQSharableFile::setNativeHandle");
       
    49     
       
    50     TFileName name;
       
    51     TInt err = file.FullName(name);
       
    52     if (err != KErrNone)
       
    53     {
       
    54         XQSERVICE_DEBUG_PRINT("XQSharableFile::fullName err%d", err);
       
    55         mHandle = 0;
       
    56         return false;
       
    57     }
       
    58         
       
    59     mFileName = QString::fromUtf16(name.Ptr(), name.Length());
       
    60 
       
    61     mHandle = *((qlonglong *)&file);
       
    62     QString s = QString("File handle is %1").arg(mHandle);
       
    63     XQSERVICE_DEBUG_PRINT("%s", qPrintable(s));
       
    64 
       
    65     return true;
       
    66 }
       
    67 
       
    68 bool XQSharableFile::getHandle(RFile &handle) const
       
    69 {
       
    70     XQSERVICE_DEBUG_PRINT("XQSharableFile::nativeHandle");
       
    71     if (mHandle == 0)
       
    72         return false;
       
    73     
       
    74     handle = *((RFile *)&mHandle);
       
    75     return true;
       
    76 }
       
    77 
       
    78 
       
    79 QString XQSharableFile::fileName() const
       
    80 {
       
    81     XQSERVICE_DEBUG_PRINT("XQSharableFile::fileName");
       
    82     return mFileName;
       
    83 }
       
    84 
       
    85 bool XQSharableFile::isValid() const
       
    86 {
       
    87     XQSERVICE_DEBUG_PRINT("XQSharableFile::isValid=%d", mHandle != 0);
       
    88     return mHandle != 0;
       
    89 }
       
    90 
       
    91 bool XQSharableFile::open(const QString &fileName)
       
    92 {
       
    93     close();   // Close possibly existing old one
       
    94         
       
    95     TInt err = mSharableFS.Connect();
       
    96     if (err != KErrNone)
       
    97     {
       
    98         return false;
       
    99     }
       
   100     
       
   101     mSharableFS.ShareProtected();
       
   102     TPtrC name( reinterpret_cast<const TUint16*>(fileName.utf16()));
       
   103     RFile f;
       
   104     err = f.Open(mSharableFS, name, EFileShareReadersOnly);
       
   105     if (err != KErrNone)
       
   106     {
       
   107         // Error in opening, close the created session
       
   108         mSharableFS.Close();
       
   109         return false;
       
   110     }
       
   111 
       
   112     this->setHandle(f);
       
   113     return true;
       
   114 }
       
   115 
       
   116 void XQSharableFile::close()
       
   117 {
       
   118     XQSERVICE_DEBUG_PRINT("XQSharableFile::close");
       
   119     RFile f;
       
   120     if (getHandle(f))
       
   121     {
       
   122         f.Close();
       
   123     }
       
   124 
       
   125     if (mSharableFS.Handle() != NULL)
       
   126     {
       
   127         mSharableFS.Close();
       
   128     }
       
   129     
       
   130 }
       
   131 
       
   132 
       
   133 template <typename Stream> void XQSharableFile::serialize(Stream &s) const
       
   134 {
       
   135     XQSERVICE_DEBUG_PRINT("XQSharableFile::serialize");
       
   136     s << mFileName;
       
   137     s << mHandle;
       
   138     QString str = QString("\tFile handle is %1").arg(mHandle);
       
   139     XQSERVICE_DEBUG_PRINT("%s", qPrintable(str));
       
   140 }
       
   141 
       
   142 template <typename Stream> void XQSharableFile::deserialize(Stream &s)
       
   143 {
       
   144     XQSERVICE_DEBUG_PRINT("XQSharableFile::deserialize");
       
   145     
       
   146     s >> mFileName;
       
   147     s >> mHandle;
       
   148     
       
   149     QString str = QString("\tFile handle is %1").arg(mHandle);
       
   150     XQSERVICE_DEBUG_PRINT("%s", qPrintable(str));
       
   151     
       
   152 }
       
   153 
       
   154 bool XQSharableFile::operator==( const XQSharableFile &other )
       
   155 {
       
   156     return (mFileName == other.mFileName) && (mHandle == other.mHandle);
       
   157 }
       
   158 
       
   159 
       
   160 Q_IMPLEMENT_USER_METATYPE(XQSharableFile)