|
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 \class XQSharableFile |
|
28 \inpublicgroup QtBaseModule |
|
29 |
|
30 \ingroup ipc |
|
31 \brief Encapsulates needed functionality to pass one Symbian data-caged file handle to service provider. |
|
32 */ |
|
33 |
|
34 /*! |
|
35 Constructor. |
|
36 */ |
|
37 XQSharableFile::XQSharableFile() : |
|
38 mHandle(0) |
|
39 { |
|
40 XQSERVICE_DEBUG_PRINT("XQSharableFile::XQSharableFile"); |
|
41 } |
|
42 |
|
43 /*! |
|
44 Constructor. |
|
45 \param file Existing and valid file handle to be set to this XQSharableFile. |
|
46 */ |
|
47 XQSharableFile::XQSharableFile(RFile &file) |
|
48 { |
|
49 XQSERVICE_DEBUG_PRINT("XQSharableFile::XQSharableFile(RFile)"); |
|
50 setHandle(file); |
|
51 } |
|
52 |
|
53 /*! |
|
54 Destroys the descriptor. |
|
55 |
|
56 \b Note! |
|
57 The destructor does not close the file handle. |
|
58 You have to close it via the close() method. |
|
59 */ |
|
60 XQSharableFile::~XQSharableFile() |
|
61 { |
|
62 XQSERVICE_DEBUG_PRINT("XQSharableFile::~XQSharableFile"); |
|
63 // One need to call close() explicitelly when all done |
|
64 } |
|
65 |
|
66 /*! |
|
67 Sets the existing and valid sharable file. Use this |
|
68 function if you obtained the handle from the other APIs. |
|
69 \return True if the handle is valid and can be set, |
|
70 false otherwise. |
|
71 */ |
|
72 bool XQSharableFile::setHandle(RFile &file) |
|
73 { |
|
74 XQSERVICE_DEBUG_PRINT("XQSharableFile::setHandle"); |
|
75 |
|
76 TFileName name; |
|
77 TInt err = file.FullName(name); |
|
78 if (err != KErrNone) |
|
79 { |
|
80 XQSERVICE_DEBUG_PRINT("XQSharableFile::fullName err%d", err); |
|
81 mHandle = 0; |
|
82 return false; |
|
83 } |
|
84 |
|
85 mFileName = QString::fromUtf16(name.Ptr(), name.Length()); |
|
86 |
|
87 mHandle = *((qlonglong *)&file); |
|
88 QString s = QString("File handle is %1").arg(mHandle); |
|
89 XQSERVICE_DEBUG_PRINT("%s", qPrintable(s)); |
|
90 |
|
91 return true; |
|
92 } |
|
93 |
|
94 /*! |
|
95 Gets the set handle, if any. |
|
96 \param handle Reference to handle to be set with |
|
97 this XQSharableFile's handle. |
|
98 \return True if the handle is valid, false otherwise. |
|
99 */ |
|
100 bool XQSharableFile::getHandle(RFile &handle) const |
|
101 { |
|
102 XQSERVICE_DEBUG_PRINT("XQSharableFile::getHandle"); |
|
103 QString s = QString("File handle is %1").arg(mHandle); |
|
104 if (mHandle == 0) |
|
105 return false; |
|
106 handle = *((RFile *)&mHandle); |
|
107 return true; |
|
108 } |
|
109 |
|
110 /*! |
|
111 Get file name associated with this XQSharableFile. |
|
112 \return Full file name associated with this XQSharableFile. |
|
113 */ |
|
114 QString XQSharableFile::fileName() const |
|
115 { |
|
116 XQSERVICE_DEBUG_PRINT("XQSharableFile::fileName"); |
|
117 return mFileName; |
|
118 } |
|
119 |
|
120 /*! |
|
121 Checks if this XQSharableFile is valid, that is if the associated |
|
122 file handle is valid. |
|
123 \return True if file handle of this XQSharableFile is valid, |
|
124 false otherwise. |
|
125 */ |
|
126 bool XQSharableFile::isValid() const |
|
127 { |
|
128 XQSERVICE_DEBUG_PRINT("XQSharableFile::isValid=%d", mHandle != 0); |
|
129 return mHandle != 0; |
|
130 } |
|
131 |
|
132 /*! |
|
133 Creates and sets the sharable file handle of the given file. |
|
134 The file can be private, data-caged directory. |
|
135 Currently only supported access mode is R/O. |
|
136 \b Note! After opening the file, it should be closed by calling close(). |
|
137 \param fileName File to be opened. |
|
138 \return True if the file name was ok and the file was opened successfuly, |
|
139 false otherwise. |
|
140 */ |
|
141 bool XQSharableFile::open(const QString &fileName) |
|
142 { |
|
143 close(); // Close possibly existing old one |
|
144 |
|
145 QString symbianFileName = fileName; |
|
146 symbianFileName.replace("/", "\\"); |
|
147 |
|
148 TInt err = mSharableFS.Connect(); |
|
149 if (err != KErrNone) |
|
150 { |
|
151 return false; |
|
152 } |
|
153 |
|
154 mSharableFS.ShareProtected(); |
|
155 TPtrC name( reinterpret_cast<const TUint16*>(symbianFileName.utf16())); |
|
156 RFile f; |
|
157 err = f.Open(mSharableFS, name, EFileShareReadersOnly); |
|
158 if (err != KErrNone) |
|
159 { |
|
160 // Error in opening, close the created session |
|
161 mSharableFS.Close(); |
|
162 return false; |
|
163 } |
|
164 |
|
165 this->setHandle(f); |
|
166 return true; |
|
167 } |
|
168 |
|
169 /*! |
|
170 Close the sharable file handle. |
|
171 <b>It is very important to close the handle if it is no longer needed.</b> |
|
172 */ |
|
173 void XQSharableFile::close() |
|
174 { |
|
175 XQSERVICE_DEBUG_PRINT("XQSharableFile::close"); |
|
176 RFile f; |
|
177 if (getHandle(f)) |
|
178 { |
|
179 f.Close(); |
|
180 } |
|
181 |
|
182 if (mSharableFS.Handle() != NULL) |
|
183 { |
|
184 mSharableFS.Close(); |
|
185 } |
|
186 |
|
187 } |
|
188 |
|
189 /*! |
|
190 Serializes XQSharableFile into a stream. |
|
191 \param s Stream to which this XQSharableFile will be serialized. |
|
192 */ |
|
193 template <typename Stream> void XQSharableFile::serialize(Stream &s) const |
|
194 { |
|
195 XQSERVICE_DEBUG_PRINT("XQSharableFile::serialize"); |
|
196 s << mFileName; |
|
197 s << mHandle; |
|
198 QString str = QString("\tFile handle is %1").arg(mHandle); |
|
199 XQSERVICE_DEBUG_PRINT("%s", qPrintable(str)); |
|
200 } |
|
201 |
|
202 /*! |
|
203 Deserializes XQSharableFile from a stream. |
|
204 \param s Stream from which XQSharableFile will be deserialized. |
|
205 */ |
|
206 template <typename Stream> void XQSharableFile::deserialize(Stream &s) |
|
207 { |
|
208 XQSERVICE_DEBUG_PRINT("XQSharableFile::deserialize"); |
|
209 |
|
210 s >> mFileName; |
|
211 s >> mHandle; |
|
212 |
|
213 QString str = QString("\tFile handle is %1").arg(mHandle); |
|
214 XQSERVICE_DEBUG_PRINT("%s", qPrintable(str)); |
|
215 |
|
216 } |
|
217 |
|
218 /*! |
|
219 Compares two XQSharableFile objects. |
|
220 \return True if both XQSharableFile objects are equal, false otherwise. |
|
221 */ |
|
222 bool XQSharableFile::operator==( const XQSharableFile &other ) |
|
223 { |
|
224 return (mFileName == other.mFileName) && (mHandle == other.mHandle); |
|
225 } |
|
226 |
|
227 |
|
228 Q_IMPLEMENT_USER_METATYPE(XQSharableFile) |