summaryrefslogtreecommitdiffstats
path: root/kbarcode/mimesources.cpp
blob: 23278bb7b325ce3c0a360ef05cbcc9afb28a2be3 (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
/***************************************************************************
                          mimesources.cpp  -  description
                             -------------------
    begin                : Son Sep 14 2003
    copyright            : (C) 2003 by Dominik Seichter
    email                : domseichter@web.de
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

#include "mimesources.h"
#include "mycanvasview.h"
#include "documentitem.h"
#include "commands.h"
#include "xmlutils.h"

#include <kcommand.h>

#include <tqbuffer.h>
#include <tqcstring.h>
#include <tqdom.h>

class DragCommand : public NewItemCommand {
    public:
        DragCommand( MyCanvasView* view, DocumentItem* doc_item )
            : NewItemCommand( view, i18n("Pasted Object") )
        {
            m_doc_item = doc_item;
        }
        
        void create()
        {
            m_object = m_doc_item;
        }
        
    private:
        DocumentItem* m_doc_item;
};



DocumentItemDrag::DocumentItemDrag( TQWidget* dragSource, const char* name )
    : TQStoredDrag( DocumentItemDrag::mimeType(), dragSource, name )
{
}

TQString DocumentItemDrag::mimeType()
{
    return "application/x-kbarcode-document-item";
}

void DocumentItemDrag::setDocumentItem( DocumentItemList* list )
{
    TQByteArray data;
    TQBuffer buffer( data );
    if( buffer.open( IO_WriteOnly ) )
    {
        TQDomDocument doc("KBarcodeClipboard");
        TQDomElement root = doc.createElement( "root" );
        doc.appendChild( root );
        
        XMLUtils xml;
        for( unsigned int i=0;i<list->count();i++)
        {
            DocumentItem* item = list->at( i );
            xml.writeXMLDocumentItem( &root, &item );
        }
                
        TQTextStream t( &buffer );
        doc.save( t, 0 );
        
        buffer.close();
        setEncodedData( data );    
    }
}

bool DocumentItemDrag::canDecode( TQMimeSource* e )
{
    return e->provides( DocumentItemDrag::mimeType() );
}

bool DocumentItemDrag::decode( TQMimeSource* mime, MyCanvasView* cv, TokenProvider* token, KCommandHistory* history )
{
    TQByteArray data = mime->encodedData( DocumentItemDrag::mimeType() );
    TQDomDocument doc( "KBarcodeClipboard" );
    if( !doc.setContent( data ) )
        return false;
    
    TQDomNode n = doc.documentElement();
    TQDomNodeList list = n.childNodes();
    KMacroCommand* commands = new KMacroCommand( i18n("Paste") );
    
    for( unsigned int i=0;i<list.length();i++)
    {
        TQDomNode n = list.item(i);
        
        TQDomElement e = n.toElement();
        if( !e.isNull() )
        {
            XMLUtils xml;
            DocumentItem* item = NULL;
            if( xml.readXMLDocumentItem( &e, &item, token ) )
            {            
                DragCommand* dc = new DragCommand( cv, item );
                dc->execute();
                commands->addCommand( dc );
            }
            else
            {
                delete commands;
                return false;
            }
        }
    }
    
    history->addCommand( commands, false );
        
    return true;
}


#include "mimesources.moc"