summaryrefslogtreecommitdiffstats
path: root/khexedit/lib/kbufferdrag.cpp
blob: 80b1d1c6f471a7f932a836af84e9f85fabd34bb2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/***************************************************************************
                          kbufferdrag.cpp  -  description
                             -------------------
    begin                : Mon Jul 07 2003
    copyright            : (C) 2003 by Friedrich W. H. Kossebau
    email                : Friedrich.W.H@Kossebau.de
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This library is free software; you can redistribute it and/or         *
 *   modify it under the terms of the GNU Library General Public           *
 *   License version 2 as published by the Free Software Foundation.       *
 *                                                                         *
 ***************************************************************************/


// qt specific
#include <tqcstring.h>
#include <textcodec.h>
// kde specific
#include <kglobal.h>
#include <klocale.h>
// lib specific
#include "kbordercoltextexport.h"
#include "koffsetcoltextexport.h"
#include "kvaluecoltextexport.h"
#include "kcharcoltextexport.h"
#include "kcharcodec.h"
#include "kbufferdrag.h"


using namespace std;
using namespace KHE;

static const char OctetStream[] =        "application/octet-stream";
static const char TextPlainUTF8[] =      "text/plain;charset=UTF-8";
static const char TextPlain[] =          "text/plain";
static const char TextPlainLocalStub[] = "text/plain;charset=";

//static const char *BaseTypes[3] = { OctetStream, TextPlainUTF8, TextPlain };

// creates the name for the local text/plain
static const char *localTextPlain()
{
  static TQCString TextPlainLocal;

  if( TextPlainLocal.isNull() )
  {
    TextPlainLocal = TQCString(KGlobal::locale()->encoding()).lower();
    // remove the whitespaces
    int s;
    while( (s=TextPlainLocal.find(' ')) >= 0 )
      TextPlainLocal.remove( s, 1 );

    TextPlainLocal.prepend( TextPlainLocalStub );
  }

  return TextPlainLocal;
}

// tries to create a codec by the given charset description
static TQTextCodec* codecForCharset( const TQCString& Desc )
{
  int i = Desc.find( "charset=" );
  if( i >= 0 )
  {
    TQCString CharSetName = Desc.mid( i+8 );
    // remove any further attributes
    if( (i=CharSetName.find( ';' )) >= 0 )
      CharSetName = CharSetName.left( i );

    // try to find codec
    return TQTextCodec::codecForName( CharSetName );
  }
  // no charset=, use locale
  return KGlobal::locale()->codecForEncoding();
}



KBufferDrag::KBufferDrag( const TQByteArray &D, KCoordRange Range,
                          const KOffsetColumn *OC, const KValueColumn *HC, const KCharColumn *TC,
                          TQChar SC, TQChar UC, const TQString &CN,
                          TQWidget *Source, const char *Name )
  :TQDragObject( Source, Name ),
   CoordRange( Range ),
   NoOfCol( 0 ),
   SubstituteChar( SC ),
   UndefinedChar( UC ),
   CodecName( CN )
{
  setData( D );

  // print column wise?
  if( HC || TC )
  {
    if( OC )
    {
      Columns[NoOfCol++] = new KOffsetColTextExport( OC );
      Columns[NoOfCol++] = new KBorderColTextExport();
    }
    if( HC )
      Columns[NoOfCol++] = new KValueColTextExport( HC, Data.data(), CoordRange );
    if( TC )
    {
      if( HC ) Columns[NoOfCol++] = new KBorderColTextExport();
      Columns[NoOfCol++] = new KCharColTextExport( TC, Data.data(), CoordRange, CodecName );
    }
  }
}


KBufferDrag::~KBufferDrag()
{
  for( uint i=0; i<NoOfCol; ++i )
    delete Columns[i];
}



void KBufferDrag::setData( const TQByteArray &D )
{
  Data = D;
}


const char *KBufferDrag::format( int i ) const
{
  return( i == 0 ? OctetStream :
          i == 1 ? TextPlainUTF8 :
          i == 2 ? TextPlain :
          i == 3 ? localTextPlain() :
                   0 );
}


TQByteArray KBufferDrag::encodedData( const char *Format ) const
{
  if( Format != 0 )
  {
    // octet stream wanted?
    if( qstrcmp(Format,OctetStream) == 0 )
      return( Data );

    // plain text wanted?
    if( tqstrncmp(Format,TextPlain,10) == 0 )
    {
      TQCString Output;
      TQTextCodec *TextCodec = codecForCharset( TQCString(Format).lower() );
      if( TextCodec == 0 )
        return Output;

      TQString Text;
      // plain copy?
      if( NoOfCol == 0 )
      {
        // duplicate the data and substitute all non-printable items with a space
        KCharCodec *CharCodec = KCharCodec::createCodec( CodecName );
        static const TQChar Tab('\t');
        static const TQChar Return('\n');
        uint Size = Data.size();
        Text.setLength( Size );

        for( uint i=0; i<Size; ++i )
        { 
          KHEChar B = CharCodec->decode( Data[i] );

          Text.at(i) = B.isUndefined() ? KHEChar(UndefinedChar) :
              (!B.isPrint() && B != Tab && B != Return ) ? KHEChar(SubstituteChar) : B;
        }
        // clean up
        delete CharCodec;
      }
      // formatted copy
      else
      {
        // initialize: one for the line's newline \n
        uint NeededChars = 1;
        for( uint i=0; i<NoOfCol; ++i )
          NeededChars += Columns[i]->charsPerLine();
        // scale with the number of lines
        NeededChars *= CoordRange.lines();
        // find out needed size
        Text.reserve( NeededChars );

        // now fill
        int l = CoordRange.start().line();
        for( uint i=0; i<NoOfCol; ++i )
          Columns[i]->printFirstLine( Text, l );
        Text.append('\n');
        for( ++l; l<=CoordRange.end().line(); ++l )
        {
          for( uint i=0; i<NoOfCol; ++i )
            Columns[i]->printNextLine( Text );
          Text.append('\n');
        }
      }
      // generate the ouput
      Output = TextCodec->fromUnicode( Text );
      // fix end
      //if( TextCodec->mibEnum() != 1000 )
      //{
        // Don't include NUL in size (TQCString::resize() adds NUL)
      //  ((TQByteArray&)Output).resize( Output.length() );
      //}
      return Output;
    }
  }

  // return empty dummy
  return TQByteArray();
}



bool KBufferDrag::canDecode( const TQMimeSource* Source )
{
  bool c =( Source->provides(OctetStream) /*|| Source->provides(TextPlain)*/ );
  return c;
//  return( Source->provides(OctetStream) /*|| Source->provides(TextPlain)*/ );
}


bool KBufferDrag::decode( const TQMimeSource* Source, TQByteArray &Dest )
{
//   Dest = Source->encodedData( MediaString );
//   return Dest.size() != 0;

  bool CanDecode = Source->provides( OctetStream );
  if( CanDecode )
    Dest = Source->encodedData( OctetStream );

  return CanDecode;
}

#include "kbufferdrag.moc"