src/gui/kernel/qclipboard_s60.cpp
changeset 0 1918ee327afb
child 3 41300fa6a67c
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     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 QtGui of the Qt Toolkit.
       
     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 
       
    42 #include "qclipboard.h"
       
    43 
       
    44 #ifndef QT_NO_CLIPBOARD
       
    45 
       
    46 #include "qapplication.h"
       
    47 #include "qbitmap.h"
       
    48 #include "qdatetime.h"
       
    49 #include "qbuffer.h"
       
    50 #include "qwidget.h"
       
    51 #include "qevent.h"
       
    52 #include "private/qcore_symbian_p.h"
       
    53 #include <QtDebug>
       
    54 
       
    55 // Symbian's clipboard
       
    56 #include <baclipb.h>
       
    57 QT_BEGIN_NAMESPACE
       
    58 
       
    59 //###  Mime Type mapping to UIDs
       
    60 
       
    61 const TUid KQtCbDataStream = {0x2001B2DD};
       
    62 
       
    63 
       
    64 class QClipboardData
       
    65 {
       
    66 public:
       
    67     QClipboardData();
       
    68    ~QClipboardData();
       
    69 
       
    70     void setSource(QMimeData* s)
       
    71     {
       
    72         if (s == src)
       
    73             return;
       
    74         delete src;
       
    75         src = s;
       
    76     }
       
    77     QMimeData* source()
       
    78     { return src; }
       
    79     bool connected()
       
    80     { return connection; }
       
    81     void clear();
       
    82 
       
    83 private:
       
    84     QMimeData* src;
       
    85     bool connection;
       
    86 };
       
    87 
       
    88 QClipboardData::QClipboardData():src(0),connection(true)
       
    89 {
       
    90     clear();
       
    91 }
       
    92 
       
    93 QClipboardData::~QClipboardData()
       
    94 {
       
    95     connection = false;
       
    96     delete src;
       
    97 }
       
    98 
       
    99 void QClipboardData::clear()
       
   100 {
       
   101     QMimeData* newSrc = new QMimeData;
       
   102     delete src;
       
   103     src = newSrc;
       
   104 }
       
   105 
       
   106 static QClipboardData *internalCbData = 0;
       
   107 
       
   108 static void cleanupClipboardData()
       
   109 {
       
   110     delete internalCbData;
       
   111     internalCbData = 0;
       
   112 }
       
   113 
       
   114 static QClipboardData *clipboardData()
       
   115 {
       
   116     if (internalCbData == 0) {
       
   117         internalCbData = new QClipboardData;
       
   118         if (internalCbData)
       
   119         {
       
   120             if (!internalCbData->connected())
       
   121             {
       
   122                 delete internalCbData;
       
   123                 internalCbData = 0;
       
   124             }
       
   125             else
       
   126             {
       
   127                 qAddPostRoutine(cleanupClipboardData);
       
   128             }
       
   129         }
       
   130     }
       
   131     return internalCbData;
       
   132 }
       
   133 
       
   134 void writeToStreamLX(const QMimeData* aData, RWriteStream& aStream)
       
   135 {
       
   136     // This function both leaves and throws exceptions. There must be no destructor
       
   137     // dependencies between cleanup styles, and no cleanup stack dependencies on stacked objects.
       
   138     QStringList headers = aData->formats();
       
   139     aStream << TCardinality(headers.count());
       
   140     for (QStringList::const_iterator iter= headers.constBegin();iter != headers.constEnd();iter++)
       
   141     {
       
   142         HBufC* stringData = TPtrC(reinterpret_cast<const TUint16*>((*iter).utf16())).AllocLC();
       
   143         QByteArray ba = aData->data((*iter));
       
   144         qDebug() << "copy to clipboard mime: " << *iter << " data: " << ba;
       
   145         // mime type
       
   146         aStream << TCardinality(stringData->Size());
       
   147         aStream << *(stringData);
       
   148         // mime data
       
   149         aStream << TCardinality(ba.size());
       
   150         aStream.WriteL(reinterpret_cast<const uchar*>(ba.constData()),ba.size());
       
   151         CleanupStack::PopAndDestroy(stringData);
       
   152     }
       
   153 }
       
   154 
       
   155 void readFromStreamLX(QMimeData* aData,RReadStream& aStream)
       
   156 {
       
   157     // This function both leaves and throws exceptions. There must be no destructor
       
   158     // dependencies between cleanup styles, and no cleanup stack dependencies on stacked objects.
       
   159     TCardinality mimeTypeCount;
       
   160     aStream >> mimeTypeCount;
       
   161     for (int i = 0; i< mimeTypeCount;i++)
       
   162     {
       
   163         // mime type
       
   164         TCardinality mimeTypeSize;
       
   165         aStream >> mimeTypeSize;
       
   166         HBufC* mimeTypeBuf = HBufC::NewLC(aStream,mimeTypeSize);
       
   167         QString mimeType = QString::fromUtf16(mimeTypeBuf->Des().Ptr(),mimeTypeBuf->Length());
       
   168         CleanupStack::PopAndDestroy(mimeTypeBuf);
       
   169         // mime data
       
   170         TCardinality dataSize;
       
   171         aStream >> dataSize;
       
   172         QByteArray ba;
       
   173         ba.reserve(dataSize);
       
   174         aStream.ReadL(reinterpret_cast<uchar*>(ba.data_ptr()->data),dataSize);
       
   175         ba.data_ptr()->size = dataSize;
       
   176         qDebug() << "paste from clipboard mime: " << mimeType << " data: " << ba;
       
   177         aData->setData(mimeType,ba);
       
   178     }
       
   179 }
       
   180 
       
   181 
       
   182 /*****************************************************************************
       
   183   QClipboard member functions
       
   184  *****************************************************************************/
       
   185 
       
   186 void QClipboard::clear(Mode mode)
       
   187 {
       
   188     setText(QString(), mode);
       
   189 }
       
   190 const QMimeData* QClipboard::mimeData(Mode mode) const
       
   191 {
       
   192     if (mode != Clipboard) return 0;
       
   193     QClipboardData *d = clipboardData();
       
   194     if (d)
       
   195     {
       
   196         TRAPD(err,{
       
   197             RFs fs = qt_s60GetRFs();
       
   198             CClipboard* cb = CClipboard::NewForReadingLC(fs);
       
   199             Q_ASSERT(cb);
       
   200             RStoreReadStream stream;
       
   201             TStreamId stid = (cb->StreamDictionary()).At(KQtCbDataStream);
       
   202             stream.OpenLC(cb->Store(),stid);
       
   203             QT_TRYCATCH_LEAVING(readFromStreamLX(d->source(),stream));
       
   204             CleanupStack::PopAndDestroy(2,cb);
       
   205             return d->source();
       
   206         });
       
   207         if (err != KErrNone){
       
   208             qDebug()<< "clipboard is empty/err: " << err;
       
   209         }
       
   210 
       
   211     }
       
   212     return 0;
       
   213 }
       
   214 
       
   215 
       
   216 void QClipboard::setMimeData(QMimeData* src, Mode mode)
       
   217 {
       
   218     if (mode != Clipboard) return;
       
   219     QClipboardData *d = clipboardData();
       
   220     if (d)
       
   221     {
       
   222         TRAPD(err,{
       
   223             RFs fs = qt_s60GetRFs();
       
   224             CClipboard* cb = CClipboard::NewForWritingLC(fs);
       
   225             RStoreWriteStream  stream;
       
   226             TStreamId stid = stream.CreateLC(cb->Store());
       
   227             QT_TRYCATCH_LEAVING(writeToStreamLX(src,stream));
       
   228             d->setSource(src);
       
   229             stream.CommitL();
       
   230             (cb->StreamDictionary()).AssignL(KQtCbDataStream,stid);
       
   231             cb->CommitL();
       
   232             CleanupStack::PopAndDestroy(2,cb);
       
   233         });
       
   234         if (err != KErrNone){
       
   235             qDebug()<< "clipboard write err :" << err;
       
   236         }
       
   237     }
       
   238     emitChanged(QClipboard::Clipboard);
       
   239 }
       
   240 
       
   241 bool QClipboard::supportsMode(Mode mode) const
       
   242 {
       
   243     return (mode == Clipboard);
       
   244 }
       
   245 
       
   246 bool QClipboard::ownsMode(Mode mode) const
       
   247 {
       
   248     if (mode == Clipboard)
       
   249         qWarning("QClipboard::ownsClipboard: UNIMPLEMENTED!");
       
   250     return false;
       
   251 }
       
   252 
       
   253 bool QClipboard::event(QEvent * /* e */)
       
   254 {
       
   255     return true;
       
   256 }
       
   257 
       
   258 void QClipboard::connectNotify( const char * )
       
   259 {
       
   260 }
       
   261 
       
   262 void QClipboard::ownerDestroyed()
       
   263 {
       
   264 }
       
   265 QT_END_NAMESPACE
       
   266 #endif // QT_NO_CLIPBOARD