summaryrefslogtreecommitdiffstats
path: root/ktnef/gui/attachpropertydialog.cpp
blob: 73b20871521bd8e906c73cf9ffb0d8580a5bbf1e (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
/*
    attachpropertydialog.cpp

    Copyright (C) 2002 Michael Goffioul <tdeprint@swing.be>

    This file is part of KTNEF, the KDE TNEF support library/program.

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software Foundation,
    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 */

#include "attachpropertydialog.h"
#include <ktnef/ktnefattach.h>
#include <ktnef/ktnefproperty.h>
#include <ktnef/ktnefpropertyset.h>
#include <ktnef/ktnefdefs.h>
#include "qwmf.h"

#include <tqlabel.h>
#include <tdelistview.h>
#include <kmimetype.h>
#include <kdebug.h>
#include <tdelocale.h>
#include <tdemessagebox.h>
#include <tdefiledialog.h>
#include <tqbuffer.h>
#include <tqdatastream.h>
#include <tqpicture.h>

AttachPropertyDialog::AttachPropertyDialog(TQWidget *parent, const char *name)
	: AttachPropertyDialogBase(parent, name, true)
{
}

AttachPropertyDialog::~AttachPropertyDialog()
{
}

void AttachPropertyDialog::setAttachment(KTNEFAttach *attach)
{
	TQString	s = (attach->fileName().isEmpty() ? attach->name() : attach->fileName());
	filename_->setText("<b>"+s+"</b>");
	setCaption(s);
	display_->setText(attach->displayName());
	mime_->setText(attach->mimeTag());
	s.setNum(attach->size());
	s.append(" bytes");
	size_->setText(s);
	KMimeType::Ptr	mimetype = KMimeType::mimeType(attach->mimeTag());
	TQPixmap pix = loadRenderingPixmap( attach, colorGroup().background() );
	if ( !pix.isNull() )
		icon_->setPixmap( pix );
	else
		icon_->setPixmap(mimetype->pixmap(TDEIcon::Small));
	description_->setText(mimetype->comment());
	s.setNum(attach->index());
	index_->setText(s);

	formatPropertySet( attach, properties_ );
	m_attach = attach;
}

void AttachPropertyDialog::saveClicked()
{
	saveProperty( properties_, m_attach, this );
}

void formatProperties( const TQMap<int,KTNEFProperty*>& props, TQListView *lv, TQListViewItem *item, const TQString& prefix )
{
	for ( TQMap<int,KTNEFProperty*>::ConstIterator it=props.begin(); it!=props.end(); ++it )
	{
		TQListViewItem *newItem = 0;
		if ( lv )
			newItem = new TQListViewItem( lv, ( *it )->keyString() );
		else if ( item )
			newItem = new TQListViewItem( item, ( *it )->keyString() );
		else
		{
			kdWarning() << "formatProperties() called with no listview and no item" << endl;
			return;
		}

		TQVariant value = ( *it )->value();
		if ( value.type() == TQVariant::List )
		{
			newItem->setOpen( true );
			newItem->setText( 0, newItem->text( 0 ) + " [" + TQString::number( value.asList().count() ) + "]" );
			int i = 0;
			for ( TQValueList<TQVariant>::ConstIterator lit=value.listBegin(); lit!=value.listEnd(); ++lit, i++ )
				new TQListViewItem( newItem, "[" + TQString::number( i ) + "]", KTNEFProperty::formatValue( *lit ) );
		}
		else if ( value.type() == TQVariant::DateTime )
			newItem->setText( 1, value.asDateTime().toString() );
		else
		{
			newItem->setText( 1, ( *it )->valueString() );
			newItem->setText( 2, prefix + "_" + TQString::number( it.key() ) );
		}
	}
}

void formatPropertySet( KTNEFPropertySet *pSet, TQListView *lv )
{
	formatProperties( pSet->properties(), lv, 0, "prop" );
	TQListViewItem *item = new TQListViewItem( lv, i18n( "TNEF Attributes" ) );
	item->setOpen( true );
	formatProperties( pSet->attributes(), 0, item, "attr" );
}

void saveProperty( TQListView *lv, KTNEFPropertySet *pSet, TQWidget *parent )
{
	TQListViewItem *item = lv->selectedItem();
	if ( !item )
		KMessageBox::error( parent, i18n( "Select an item." ) );
	else if ( item->text( 2 ).isEmpty() )
		KMessageBox::error( parent, i18n( "The selected item cannot be saved." ) );
	else
	{
		TQString tag = item->text( 2 );
		int key = tag.mid( 5 ).toInt();
		TQVariant prop = ( tag.startsWith( "attr_" ) ? pSet->attribute( key ) : pSet->property( key ) );
		TQString filename = KFileDialog::getSaveFileName( tag, TQString(), parent );
		if ( !filename.isEmpty() )
		{
			TQFile f( filename );
			if ( f.open( IO_WriteOnly ) )
			{
				switch ( prop.type() )
				{
					case TQVariant::ByteArray:
						f.writeBlock( prop.asByteArray().data(), prop.asByteArray().size() );
						break;
					default:
						{
							TQTextStream t( &f );
							t << prop.toString();
							break;
						}
				}
				f.close();
			}
			else
				KMessageBox::error( parent, i18n( "Unable to open file for writing, check file permissions." ) );
		}
	}
}

TQPixmap loadRenderingPixmap( KTNEFPropertySet *pSet, const TQColor& bgColor )
{
	TQPixmap pix;
	TQVariant rendData = pSet->attribute( attATTACHRENDDATA ), wmf = pSet->attribute( attATTACHMETAFILE );
	if ( !rendData.isNull() && !wmf.isNull() )
	{
		// Get rendering size
		TQBuffer rendBuffer( rendData.asByteArray() );
		rendBuffer.open( IO_ReadOnly );
		TQDataStream rendStream( &rendBuffer );
		rendStream.setByteOrder( TQDataStream::LittleEndian );
		TQ_UINT16 type, w, h;
		rendStream >> type >> w >> w; // read type and skip 4 bytes
		rendStream >> w >> h;
		rendBuffer.close();

		if ( type == 1 && w > 0 && h > 0 )
		{
			// Load WMF data
			TQWinMetaFile wmfLoader;
			TQBuffer wmfBuffer( wmf.asByteArray() );
			wmfBuffer.open( IO_ReadOnly );
			wmfLoader.setBbox( TQRect( 0, 0, w, h ) );
			if ( wmfLoader.load( wmfBuffer ) )
			{
				pix.resize( w, h );
				pix.fill( bgColor );
				wmfLoader.paint( &TQT_TQPAINTDEVICE_OBJECT(pix) );
			}
			wmfBuffer.close();
		}
	}
	return pix;
}