summaryrefslogtreecommitdiffstats
path: root/kftpgrabber/src/kftpqueueconverter.cpp
blob: 24f7941fe43a601b54b932d255595bf35e0bf4fb (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
/*
 * This file is part of the KFTPGrabber project
 *
 * Copyright (C) 2003-2004 by the KFTPGrabber developers
 * Copyright (C) 2003-2004 Jernej Kos <kostko@jweb-network.net>
 *
 * 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.
 *
 * This program is distributed in the hope that it will be useful, but
 * is provided AS IS, WITHOUT ANY WARRANTY; without even the implied
 * warranty of MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, and
 * NON-INFRINGEMENT.  See the GNU General Public License for more details.
 *
 * 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 Steet, Fifth Floor, Boston,
 * MA 02110-1301, USA.
 *
 * In addition, as a special exception, the copyright holders give
 * permission to link the code of portions of this program with the
 * OpenSSL library under certain conditions as described in each
 * individual source file, and distribute linked combinations
 * including the two.
 * You must obey the GNU General Public License in all respects
 * for all of the code used other than OpenSSL.  If you modify
 * file(s) with this exception, you may extend this exception to your
 * version of the file(s), but you are not obligated to do so.  If you
 * do not wish to do so, delete this exception statement from your
 * version.  If you delete this exception statement from all source
 * files in the program, then also delete it here.
 */

#include "kftpqueueconverter.h"
#include "kftpqueue.h"

#include <ntqfile.h>
#include <ntqobjectlist.h>

#include <kfilterdev.h>

KFTPQueueConverter::KFTPQueueConverter(TQObject *parent, const char *name)
 : TQObject(parent, name)
{
}

void KFTPQueueConverter::importQueue(const TQString &filename)
{
  m_xml = TQDomDocument("KFTPGrabberQueue");
  KFTPQueue::Manager::self()->clearQueue();
  
  // Load from file
  TQIODevice *file = KFilterDev::deviceForFile(filename);
  m_xml.setContent(file);
  file->close();
  delete file;
  
  // Parse XML and create KFTPQueueTransfers
  TQDomNode n = m_xml.documentElement().firstChild();
  while (!n.isNull()) {
    importNode(n);
    
    n = n.nextSibling();
  }
  
  KFTPQueue::Manager::self()->doEmitUpdate();
}

void KFTPQueueConverter::exportQueue(const TQString &filename)
{
  m_xml = TQDomDocument("KFTPGrabberQueue");
  m_xml.setContent(TQString("<queue></queue>"));
  
  // Go trough all KFTPQueueTransfers and generate XML
  KFTPQueue::QueueObject *i;
  TQPtrList<KFTPQueue::QueueObject> sites = KFTPQueue::Manager::self()->topLevelObject()->getChildrenList();
  
  for (i = sites.first(); i; i = sites.next()) {
    KFTPQueue::QueueObject *t;
    TQPtrList<KFTPQueue::QueueObject> list = i->getChildrenList();
    
    for (t = list.first(); t; t = list.next())
      generateXML(static_cast<KFTPQueue::Transfer*>(t), m_xml.documentElement());
  }
  
  // Save to file
  TQIODevice *file = KFilterDev::deviceForFile(filename, "application/x-gzip");
  if (!file->open(IO_WriteOnly)) {
    tqDebug("WARNING: Unable to open xml for writing!");
    return;
  }
  
  TQTextStream fileStream(file);
  m_xml.save(fileStream, 2);
  file->flush();
  file->close();
  delete file;
}

void KFTPQueueConverter::generateXML(KFTPQueue::Transfer *transfer, TQDomNode parent)
{
  // Create the item
  TQDomElement item = m_xml.createElement("item");
  parent.appendChild(item);

  // Create text nodes
  createTextNode("source", transfer->getSourceUrl().url(), item);
  createTextNode("dest", transfer->getDestUrl().url(), item);
  createTextNode("size", TQString::number(transfer->getSize()), item);
  createTextNode("type", transfer->isDir() ? "directory" : "file", item);
  
  if (transfer->isDir() && transfer->children()) {
    // Transfer has children, add them as well
    TQDomElement tag = m_xml.createElement("children");
    item.appendChild(tag);
    
    KFTPQueue::QueueObject *i;
    TQPtrList<KFTPQueue::QueueObject> list = transfer->getChildrenList();
    for (i = list.first(); i; i = list.next()) {
      generateXML(static_cast<KFTPQueue::Transfer*>(i), tag);
    }
  }
}

void KFTPQueueConverter::importNode(TQDomNode node, TQObject *parent)
{
  // Get node data
  KURL srcUrl = KURL(getTextNode("source", node));
  KURL dstUrl = KURL(getTextNode("dest", node));
  filesize_t size = getTextNode("size", node).toULongLong();
  bool dir = getTextNode("type", node) == "directory";
  
  KFTPQueue::TransferType transType = KFTPQueue::Download;
  
  if (srcUrl.isLocalFile() && !dstUrl.isLocalFile()) {
    transType = KFTPQueue::Upload;
  } else if (!srcUrl.isLocalFile() && dstUrl.isLocalFile()) {
    transType = KFTPQueue::Download;
  } else if (!srcUrl.isLocalFile() && !dstUrl.isLocalFile()) {
    transType = KFTPQueue::FXP;
  }
  
  // Create new transfer
  if (!parent)
    parent = KFTPQueue::Manager::self()->topLevelObject();
    
  KFTPQueue::Transfer *transfer = 0L;
  if (dir)
    transfer = new KFTPQueue::TransferDir(parent);
  else
    transfer = new KFTPQueue::TransferFile(parent);
  
  transfer->setSourceUrl(srcUrl);
  transfer->setDestUrl(dstUrl);
  transfer->addSize(dir ? 0 : size);
  transfer->setTransferType(transType);
  
  if (parent == KFTPQueue::Manager::self()->topLevelObject()) {
    KFTPQueue::Manager::self()->insertTransfer(transfer);
  } else {
    transfer->setId(KFTPQueue::Manager::self()->m_lastTQID++);
    emit KFTPQueue::Manager::self()->newTransfer(transfer);
  }
  
  TQDomNodeList tagNodes = node.toElement().elementsByTagName("children");
  if (dir && tagNodes.length() > 0) {
    // Import all child nodes
    TQDomNode n = node.firstChild();
    while (!n.isNull()) {
      if (n.toElement().tagName() == "children") {
        n = n.firstChild();
        break;
      }
      
      n = n.nextSibling();
    }
    
    while (!n.isNull()) {
      importNode(n, transfer);
      
      n = n.nextSibling();
    }
  }
}

void KFTPQueueConverter::createTextNode(const TQString &name, const TQString &value, TQDomNode parent)
{
  TQDomElement tag = m_xml.createElement(name);
  parent.appendChild(tag);
  
  TQDomText textNode = m_xml.createTextNode(value);
  tag.appendChild(textNode);
}

TQString KFTPQueueConverter::getTextNode(const TQString &name, TQDomNode parent)
{
  TQDomNodeList tagNodes = parent.toElement().elementsByTagName(name);
  
  if (tagNodes.length() > 0) {
    TQString prop = tagNodes.item(0).toElement().text();
    prop.stripWhiteSpace();

    return prop;
  } else {
    return TQString::null;
  }
}

#include "kftpqueueconverter.moc"