summaryrefslogtreecommitdiffstats
path: root/kftpgrabber/src/widgets/browser/treeview.cpp
blob: f101dc705b015e96e09690735d5afa56ac9cfe44 (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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
/*
 * 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 "browser/treeview.h"
#include "browser/view.h"

#include "engine/thread.h"

#include "misc.h"

#include <ntqheader.h>
#include <ntqtimer.h>

#include <tdeio/job.h>
#include <kurl.h>
#include <kurldrag.h>

using namespace KFTPGrabberBase;

namespace KFTPWidgets {

namespace Browser {

/*
 *                         TreeViewItem class
 */

TreeViewItem::TreeViewItem(TreeView *parent, const KURL &url)
  : TQListViewItem(parent),
    m_tree(parent),
    m_url(url),
    m_dirty(false)
{
  // Set the name
  setText(0, path2Name(url.path(-1)));
}

TreeViewItem::TreeViewItem(TreeView *tree, TQListViewItem *parent, const KURL &url)
  : TQListViewItem(parent),
    m_tree(tree),
    m_url(url),
    m_dirty(false)
{
  // Set the name
  setText(0, path2Name(url.path(-1)));
}

TreeViewItem::~TreeViewItem()
{
  // The item should be removed from the list as well
  m_tree->m_treeItems.remove(m_url.path());
}

int TreeViewItem::compare(TQListViewItem *i, int col, bool) const
{
  // Hidden files must be on top
  if (m_url.fileName().at(0) == '.')
    return -1;

  return TQListViewItem::compare(i, col, false);
}

/*
 *                          TreeView class
 */

TreeView::TreeView(TQWidget *parent)
 : TDEListView(parent)
{
  // Create the columns
  addColumn(i18n("Directory"));

  // General tree settings
  setDragEnabled(true);
  setAcceptDrops(true);
  setMinimumWidth(150);
  setRootIsDecorated(false);
  header()->hide();

  // Reset the view
  resetView(KURL("/"));
  m_noItemOpen = false;

  // Connect signals
  connect(this, SIGNAL(clicked(TQListViewItem*)), this, SLOT(slotClicked(TQListViewItem*)));
  connect(this, SIGNAL(doubleClicked(TQListViewItem*)), this, SLOT(slotDoubleClicked(TQListViewItem*)));
  
  // Drag and drop
  m_dropItem = 0L;
}


TreeView::~TreeView()
{
  // Free the item index
  m_treeItems.clear();
}

void TreeView::resetView(const KURL &url)
{
  // Free the item index
  m_treeItems.clear();
  
  // Clear the view
  clear();
  header()->resizeSection(0, 0);

  // Create the root item
  TreeViewItem *rootItem = new TreeViewItem(this, remoteUrl("/", url));
  rootItem->setText(0, url.isLocalFile() ? i18n("Root directory") : url.host());
  rootItem->setPixmap(0, loadSmallPixmap("folder_red"));
  setOpen(rootItem, true);
}

int TreeView::openUrl(const KURL &url, TQListViewItem *parent)
{
  // Root item should always be open
  setOpen(firstChild(), true);

  // Go trough all items in the list and try to find the url
  TQListViewItem *item;
  if (!parent)
    item = firstChild();
  else
    item = parent;

  while (item) {
    // Check if the item is correct
    if (static_cast<TreeViewItem*>(item)->m_url.path(-1) == url.path(-1)) {
      // We have found it
      ensureItemVisible(item);
      setCurrentItem(item);
      setSelected(item, true);

      // Change viewport
      TQRect r = itemRect(item);
      if (r.isValid()) {
        int x, y;
        viewportToContents(contentsX(), r.y(), x, y);
        setContentsPos(x, y);
      }

      return 1;
    }

    if (item->firstChild()) {
      // If item has children, go after them
      if (openUrl(url, item->firstChild()) == 1)
        return 1;
    }

    item = item->nextSibling();
  }

  return 0;
}

TQListViewItem *TreeView::findItem(TQListViewItem *parent, const TQString &name)
{
  TQListViewItem *item = parent->firstChild();

  while (item) {
    if (item->text(0) == name)
      return item;

    item = item->nextSibling();
  }

  // If nothing is found, parent should be returned
  return parent;
}

void TreeView::createFolder(const KURL &url, TQPixmap icon)
{
  int numDirs = url.path(1).contains('/', false);
  TQListViewItem *item = firstChild();
  
  for (int i = 1; i < numDirs; i++) {
    TQString itemUrl = url.path().section('/', 0, i);
    
    if (m_treeItems[itemUrl]) {
      // Item exists for this URL
      item = m_treeItems[itemUrl];
    } else {
      // Item not yet created
      KURL tmp = url;
      tmp.setPath(itemUrl);
      
      item = new TreeViewItem(this, item, tmp);
      if (i == numDirs - 1)
        item->setPixmap(0, icon);
      else
        item->setPixmap(0, loadSmallPixmap("folder"));
      
      m_treeItems.insert(itemUrl, static_cast<TreeViewItem*>(item));
    }
    
    // Mark it as dirty
    static_cast<TreeViewItem*>(item)->m_dirty = true;

    // Open it
    if (!m_noItemOpen)
      setOpen(item, true);
  }

  // Root item should always be open
  setOpen(firstChild(), true);
}

void TreeView::endUpdate(const KURL &url)
{
  // Remove all items in the selected dir, not marked as dirty
  TreeViewItem *top = static_cast<TreeViewItem*>(firstChild());
  TreeViewItem *tmp = 0L;
  int numDirs = url.path(1).contains('/', false);
  
  if (url.path() == "/") {
    TreeViewItem *i = static_cast<TreeViewItem*>(top->firstChild());
    
    while (i) {
      tmp = static_cast<TreeViewItem*>(i->nextSibling());
      
      if (!i->m_dirty) {
        // Remove item from the index
        delete i;
      } else {
        i->m_dirty = false;
      }
      
      i = tmp;
    }
    
    return;
  }

  // URL for items
  KURL itemURL = url;
  itemURL.setPath("/");
  
  for (int i = 1; i < numDirs; i++) {
    TQString sect = url.path().section('/', i, i);
    itemURL.setPath(url.path().section('/', 0, i));
    
    if (!m_treeItems[itemURL.path()]) {
      // Item not yet created
      return;
    } else {
      // Item is already present
      top = m_treeItems[itemURL.path()];

      // Check for URL match
      if (itemURL.path(-1) == url.path(-1)) {
        // URL match, delete the item's children
        TreeViewItem *i = static_cast<TreeViewItem*>(top->firstChild());
        
        while (i) {
          tmp = static_cast<TreeViewItem*>(i->nextSibling());
          
          if (!i->m_dirty) {
            // Remove item from the index
            delete i;
          } else {
            i->m_dirty = false;
          }
            
          i = tmp;
        }
        return;
      }
    }
  }
}

void TreeView::clearFolder(const KURL &url)
{
  // Remove url's children
  TQListViewItem *top = firstChild();
  int numDirs = url.path(1).contains('/', false);
  
  if (url.path() == "/") {
    TQListViewItem *i = top->firstChild();
    
    while (i) {
      // Remove item from the index
      delete i;
      
      i = top->firstChild();
    }
    
    return;
  }

  // URL for items
  KURL itemURL = url;
  itemURL.setPath("/");

  for (int i = 1; i < numDirs; i++) {
    TQString sect = url.path().section('/', i, i);
    itemURL.setPath(url.path().section('/', 0, i));

    if (!m_treeItems[itemURL.path()]) {
      // Item not yet created
      return;
    } else {
      // Item is already present
      top = m_treeItems[itemURL.path()];

      // Check for URL match
      if (itemURL.path(-1) == url.path(-1)) {
        // URL match, delete the item's children
        TQListViewItem *i = top->firstChild();
        
        while (i) {
          // Remove item from the index
          delete i;
          
          i = top->firstChild();
        }
        return;
      }
    }
  }
}

void TreeView::removeFolder(const KURL &url)
{
  // Removes a folder at url
  TQListViewItem *top = firstChild();
  int numDirs = url.path(1).contains('/', false);

  // URL for items
  KURL itemURL = url;
  itemURL.setPath("/");

  for (int i = 1; i < numDirs; i++) {
    TQString sect = url.path().section('/', i, i);
    itemURL.setPath(url.path().section('/', 0, i));

    if (!m_treeItems[itemURL.path()]) {
      // Item not yet created
      return;
    } else {
      // Item is already present
      top = m_treeItems[itemURL.path()];

      // Check for URL match
      if (itemURL.path(-1) == url.path(-1)) {
        // Remove item from the index
        delete top;
        
        return;
      }
    }
  }
}

void TreeView::slotClicked(TQListViewItem *item)
{
  if (!item)
    return;
    
  m_noItemOpen = true;
  emit pathChanged(static_cast<TreeViewItem*>(item)->m_url);
  m_noItemOpen = false;
}

void TreeView::slotDoubleClicked(TQListViewItem *item)
{
  if (!item)
    return;
    
  setOpen(item, !item->isOpen());
}

void TreeView::startDrag()
{
  dragObject()->drag();
}

bool TreeView::acceptDrag(TQDropEvent *e)
{
  return KURLDrag::canDecode(e) &&
         ( e->action() == TQDropEvent::Copy
           || e->action() == TQDropEvent::Move
           || e->action() == TQDropEvent::Link )
         && acceptDrops()
         && dragEnabled();
}

TQDragObject *TreeView::dragObject()
{
  TreeViewItem *item = static_cast<TreeViewItem*>(selectedItem());

  // Set the correct pixmap
  TQPixmap pix = *item->pixmap(0);
  
  KURL::List urls;
  urls.append(item->m_url);
  
  TDEIO::MetaData meta;
  meta.insert(item->m_url.htmlURL().local8Bit(), "D:0");
  
  m_dragObject = new KURLDrag(urls, meta, this, name());
  m_dragObject->setPixmap(pix, TQPoint(pix.width() / 2, pix.height() / 2));
  
  return m_dragObject;
}

void TreeView::contentsDragEnterEvent(TQDragEnterEvent *e)
{
  if (!acceptDrag(e)) {
    e->accept(false);
    return;
  }

  e->acceptAction();
  TQListViewItem *i = itemAt(contentsToViewport(e->pos()));
  if (i)
    m_dropItem = i;
}

void TreeView::contentsDragMoveEvent(TQDragMoveEvent *e)
{
  if (!acceptDrag(e)) {
    e->accept(false);
    return;
  }

  e->acceptAction();
  TQListViewItem *i = itemAt(contentsToViewport(e->pos()));
  
  if (i && i != m_dropItem)
    m_dropItem = i;
}

void TreeView::contentsDragLeaveEvent(TQDragLeaveEvent*)
{
  m_dropItem = 0L;
}

void TreeView::contentsDropEvent(TQDropEvent *e)
{
  if (!m_dropItem)
    return;
    
  if (!acceptDrag(e)) {
    e->acceptAction(false);
    return;
  }
  e->acceptAction();

  // Decode the data and try to init transfer
  TDEIO::MetaData meta;
  KURL::List urls;
  KURLDrag::decode(e, urls, meta);

  // Move the specified file(s)
  for (KURL::List::iterator i = urls.begin(); i != urls.end(); ++i) {
    KURL destUrl = static_cast<TreeViewItem*>(m_dropItem)->m_url;
    
    if (destUrl != (*i)) {
      if (destUrl.isLocalFile() != (*i).isLocalFile()) {
        // TODO Queue up a transfer
      } else {
        // Rename/move the file
        destUrl.addPath((*i).fileName());
        
        if ((*i).isLocalFile()) {
          TDEIO::move((*i), destUrl, false);
          
          // Reload the listing
          static_cast<View*>(parent()->parent())->reload();
        } else {
          static_cast<View*>(parent()->parent())->m_ftpClient->rename((*i), destUrl);
        }
      }
    }
  }
  
  // Invalidate the drop item
  m_dropItem = 0L;
}

}

}

#include "treeview.moc"