~ubuntu-branches/ubuntu/maverick/kdeutils/maverick-proposed

« back to all changes in this revision

Viewing changes to okteta/kasten/gui/io/abstractbytearraystreamencoder.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2010-05-28 09:49:30 UTC
  • mfrom: (1.2.44 upstream)
  • Revision ID: james.westby@ubuntu.com-20100528094930-jzynf0obv1n2v13a
Tags: 4:4.4.80-0ubuntu1~ppa1
New upstream beta release

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
    This file is part of the Okteta Kasten module, part of the KDE project.
3
 
 
4
 
    Copyright 2007 Friedrich W. H. Kossebau <kossebau@kde.org>
5
 
 
6
 
    This library is free software; you can redistribute it and/or
7
 
    modify it under the terms of the GNU Lesser General Public
8
 
    License as published by the Free Software Foundation; either
9
 
    version 2.1 of the License, or (at your option) version 3, or any
10
 
    later version accepted by the membership of KDE e.V. (or its
11
 
    successor approved by the membership of KDE e.V.), which shall
12
 
    act as a proxy defined in Section 6 of version 3 of the license.
13
 
 
14
 
    This library is distributed in the hope that it will be useful,
15
 
    but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17
 
    Lesser General Public License for more details.
18
 
 
19
 
    You should have received a copy of the GNU Lesser General Public
20
 
    License along with this library. If not, see <http://www.gnu.org/licenses/>.
21
 
*/
22
 
 
23
 
#include "abstractbytearraystreamencoder.h"
24
 
 
25
 
// lib
26
 
#include <bytearrayview.h>
27
 
#include <bytearraydocument.h>
28
 
#include <bytearrayselection.h>
29
 
// Okteta core
30
 
#include <abstractbytearraymodel.h>
31
 
// Qt
32
 
#include <QtCore/QBuffer>
33
 
 
34
 
 
35
 
namespace Kasten
36
 
{
37
 
 
38
 
static const int MaxPreviewSize = 100;
39
 
 
40
 
 
41
 
AbstractByteArrayStreamEncoder::AbstractByteArrayStreamEncoder( const QString &remoteTypeName,
42
 
                                                                  const QString &remoteMimeType )
43
 
 : AbstractModelStreamEncoder( remoteTypeName, remoteMimeType )
44
 
{}
45
 
 
46
 
QString AbstractByteArrayStreamEncoder::modelTypeName( AbstractModel* model, const AbstractModelSelection* selection ) const
47
 
{
48
 
Q_UNUSED( selection )
49
 
 
50
 
    const ByteArrayDocument* byteArrayDocument = model->findBaseModel<const ByteArrayDocument*>();
51
 
 
52
 
    return ( byteArrayDocument == 0 ) ? QString() : byteArrayDocument->typeName();
53
 
}
54
 
 
55
 
 
56
 
bool AbstractByteArrayStreamEncoder::encodeToStream( QIODevice* device,
57
 
                                                     AbstractModel* model, const AbstractModelSelection* selection )
58
 
{
59
 
    const ByteArrayView* byteArrayView = qobject_cast<const ByteArrayView*>( model );
60
 
 
61
 
    const ByteArrayDocument* byteArrayDocument =
62
 
        byteArrayView ? qobject_cast<const ByteArrayDocument*>( byteArrayView->baseModel() ) : 0;
63
 
    if( byteArrayDocument == 0 )
64
 
        return false;
65
 
 
66
 
    const Okteta::AbstractByteArrayModel* byteArray = byteArrayDocument->content();
67
 
 
68
 
    const ByteArraySelection* byteArraySelection =
69
 
        selection ? static_cast<const ByteArraySelection*>( selection ) : 0;
70
 
 
71
 
    const Okteta::AddressRange range = byteArraySelection && byteArraySelection->isValid() ?
72
 
        byteArraySelection->range() :
73
 
        Okteta::AddressRange::fromWidth( 0, byteArray->size() );
74
 
 
75
 
    const bool success = encodeDataToStream( device, byteArrayView, byteArray, range );
76
 
 
77
 
    return success;
78
 
}
79
 
 
80
 
QString AbstractByteArrayStreamEncoder::previewData( AbstractModel* model, const AbstractModelSelection* selection )
81
 
{
82
 
    const ByteArrayView* byteArrayView = qobject_cast<const ByteArrayView*>( model );
83
 
 
84
 
    const ByteArrayDocument* byteArrayDocument =
85
 
        byteArrayView ? qobject_cast<const ByteArrayDocument*>( byteArrayView->baseModel() ) : 0;
86
 
    if( byteArrayDocument == 0 )
87
 
        return QString();
88
 
 
89
 
    const Okteta::AbstractByteArrayModel* byteArray = byteArrayDocument->content();
90
 
 
91
 
    const ByteArraySelection* byteArraySelection =
92
 
        selection ? static_cast<const ByteArraySelection*>( selection ) : 0;
93
 
 
94
 
    Okteta::AddressRange range = byteArraySelection && byteArraySelection->isValid() ?
95
 
        byteArraySelection->range() :
96
 
        Okteta::AddressRange::fromWidth( 0, byteArray->size() );
97
 
    range.restrictEndByWidth( MaxPreviewSize );
98
 
 
99
 
    QByteArray data;
100
 
    QBuffer dataBuffer( &data );
101
 
    dataBuffer.open( QIODevice::WriteOnly );
102
 
 
103
 
    const bool success = encodeDataToStream( &dataBuffer, byteArrayView, byteArray, range );
104
 
    dataBuffer.close();
105
 
 
106
 
    return success ? QString(data) : QString();
107
 
}
108
 
 
109
 
AbstractByteArrayStreamEncoder::~AbstractByteArrayStreamEncoder() {}
110
 
 
111
 
}