summaryrefslogtreecommitdiffstats
path: root/kftpgrabber/src/kftpbookmarks.cpp
blob: 424dd85080698f50f8a9c9bc2a859f6f28efb20a (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
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
/*
 * 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 "kftpbookmarks.h"
#include "kftpbookmarkaction.h"
#include "kftpqueue.h"
#include "kftpapi.h"
#include "kftpwalletconnection.h"
#include "misc.h"
#include "desencryptor.h"
#include "browser/view.h"
#include "kftpsession.h"
#include "bookmarks/listview.h"

#include "misc/config.h"
#include "engine/thread.h"
#include "engine/ftpsocket.h"

#include <ntqfile.h>

#include <kdebug.h>
#include <tdelocale.h>
#include <kiconloader.h>
#include <kpassdlg.h>
#include <tdemessagebox.h>
#include <tdeversion.h>
#include <tdeapplication.h>
#include <tdeio/passdlg.h>
#include <kstaticdeleter.h>

/* KSSL includes */
#include <ksslpkcs12.h>

using namespace KFTPGrabberBase;

namespace KFTPBookmarks {

Site::Site(TQDomNode node)
  : m_element(node.toElement())
{
  // generate id if it is not present!
  if (m_element.tagName() == "category") {
    m_type = ST_CATEGORY;

    if (getAttribute("id").isEmpty())
      setAttribute("id", TQString("cat-%1").arg(TDEApplication::randomString(7)));
  } else if (m_element.tagName() == "server") {
    m_type = ST_SITE;

    if (getAttribute("id").isEmpty())
      setAttribute("id", TQString("site-%1").arg(TDEApplication::randomString(7)));
  }

  // Set the id
  m_id = getAttribute("id");
}

Site::~Site()
{
}

Site *Site::duplicate()
{
  Site *site = new Site(m_element.cloneNode());
  site->setAttribute("name", i18n("Copy of") + " " + getAttribute("name"));

  // Assign a new id
  site->setAttribute("id", TQString("site-%1").arg(TDEApplication::randomString(7)));
  site->m_id = site->getAttribute("id");

  m_element.parentNode().appendChild(site->m_element);

  return site;
}

void Site::reparentSite(Site *site)
{
  // Move site's parent
  m_element.appendChild(site->m_element);
}

Site *Site::addSite(TQDomNode node)
{
  if (node.isNull()) {
    // If there was no node specified, create a new one
    node = m_element.ownerDocument().createElement("server");
  }

  Site *site = new Site(node);
  m_element.appendChild(site->m_element);

  return site;
}

void Site::addCategory(const TQString &name)
{
  TQDomElement cat = m_element.ownerDocument().createElement("category");

  // Assign a new id and name
  cat.setAttribute("id", TQString("cat-%1").arg(TDEApplication::randomString(7)));
  cat.setAttribute("name", name);

  m_element.appendChild(cat);
}

KURL Site::getUrl()
{
  KURL url;

  url.setProtocol(getProperty("protocol"));
  url.setHost(getProperty("host"));
  url.setPort(getIntProperty("port"));
  url.setUser(getProperty("username"));
  url.setPass(getProperty("password"));

  return url;
}

Site *Site::getParentSite()
{
  // Get parent's site id, then search for it
  TQDomElement parent = m_element.parentNode().toElement();

  if (parent.isNull())
    return NULL;
  else
    return KFTPBookmarks::Manager::self()->findCategory(parent.attribute("id"));
}

TQString Site::getProperty(const TQString &name)
{
  TQDomNodeList nodes = m_element.elementsByTagName(name);

  if (nodes.length() > 0) {
    TQString property = nodes.item(0).toElement().text();
    property.stripWhiteSpace();

    // Automagicly decode passwords from BASE64
    if (name.contains("pass") == 1)
      property = decodePassword(property);

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

int Site::getIntProperty(const TQString &name)
{
  return getProperty(name).toInt();
}

void Site::setProperty(const TQString &name, const TQString &value)
{
  // First delete the old property if it exists
  TQDomNodeList nodes = m_element.elementsByTagName(name);

  if (nodes.length() > 0)
    m_element.removeChild(nodes.item(0));

  // Now add a new one
  TQDomElement property = m_element.ownerDocument().createElement(name);
  m_element.appendChild(property);

  TQDomText text = m_element.ownerDocument().createTextNode(value);
  property.appendChild(text);
}

void Site::setProperty(const TQString &name, int value)
{
  setProperty(name, TQString::number(value));
}

void Site::setAttribute(const TQString &name, const TQString &value)
{
  m_element.setAttribute(name, value);
}

TQString Site::getAttribute(const TQString &name)
{
  return m_element.attribute(name);
}

////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////

Manager *Manager::m_self = 0;
static KStaticDeleter<Manager> staticBookmarkManagerDeleter;

Manager *Manager::self()
{
  if (!m_self) {
    staticBookmarkManagerDeleter.setObject(m_self, new Manager());
  }
  
  return m_self;
}

Manager::Manager()
  : TQObject()
{
  // Init the cache
  m_siteCache.setAutoDelete(true);

  // Init the DOM document
  m_document = TQDomDocument("KFTPGrabber");
}

Manager::Manager(const Manager &bookmarks)
  : TQObject()
{
  // Init the cache
  m_siteCache.setAutoDelete(true);

  // Init the DOM document
  m_document = TQDomDocument("KFTPGrabber");

  // Copy the bookmarks
  TQDomNode tmp = m_document.importNode(bookmarks.m_document.documentElement(), true);
  m_document.appendChild(tmp);
}

Manager::~Manager()
{
  if (m_self == this)
    staticBookmarkManagerDeleter.setObject(m_self, 0, false);
}

void Manager::setBookmarks(KFTPBookmarks::Manager *bookmarks)
{
  // Init the DOM document
  m_document = TQDomDocument("KFTPGrabber");

  TQDomNode tmp = m_document.importNode(bookmarks->m_document.documentElement(), true);
  m_document.appendChild(tmp);

  // Clear the cache
  m_siteCache.clear();

  emit update();
}

void Manager::importSites(TQDomNode node)
{
  TQDomNode import = m_document.importNode(node, true);
  m_document.documentElement().appendChild(import);
  
  // Run sanity checks to generate missing ids
  Manager::validate();
}

void Manager::load(const TQString &filename)
{
  m_filename = filename;

  TQFile file(filename);
  if (!file.open(IO_ReadOnly)) {
    // Create a new empty XML
    m_document.setContent(TQString("<sites version=\"%1\"></sites>").arg(KFTP_BOOKMARKS_VERSION));

    return;
  }

  // Check if the file is encrpyted
  TQCString content(file.readAll());
  
  if (KFTPCore::Config::encryptBookmarks()) {
    // File is encrypted
pwd_entry:
    int saveToWallet = 1;

    TQCString p_pass(KFTPAPI::getInstance()->walletConnection()->getPassword("bookmarkDecryptPwd").ascii());
    if (TQString(p_pass).isNull()) {
      // Ask the user for a password
      int ret = KPasswordDialog::getPassword(p_pass, i18n("This bookmark file is encrypted. Please enter key for decryption."), &saveToWallet);

      if (ret != KPasswordDialog::Accepted) {
        // User pressed cancel
        p_pass = "";
      }
    }

    // Try to decrypt
    DESEncryptor des;
    des.setKey(p_pass);
    des.decrypt(content);

    if (des.output().left(6) != "<sites" && des.output().left(9) != "<!DOCTYPE") {
      // Clear any saved passwords
      KFTPAPI::getInstance()->walletConnection()->setPassword("bookmarkDecryptPwd", TQString::null);

      if (KMessageBox::warningContinueCancel(
            KFTPAPI::getInstance()->mainWindow(),
            i18n("<qt>Bookmark file decryption has failed with provided key. Do you want to <b>overwrite</b> bookmarks with an empty file ?<br><br><font color=\"red\"><b>Warning:</b> If you overwrite, all current bookmarks will be lost.</font></qt>"),
            i18n("Decryption Failed"),
            KGuiItem(i18n("&Overwrite Bookmarks"))
          ) != KMessageBox::Continue)
      {
        // Request the password again
        goto pwd_entry;
      }

      // Create new empty XML
      m_document.setContent(TQString("<sites version=\"%1\"></sites>").arg(KFTP_BOOKMARKS_VERSION));

      file.close();
      return;
    }

    // Save the password for later encryption
    m_decryptKey = p_pass;
    content = des.output().ascii();

    if (saveToWallet) {
      // Save the password to KWallet
      KFTPAPI::getInstance()->walletConnection()->setPassword("bookmarkDecryptPwd", p_pass);
    }
  }

  m_document.setContent(TQString::fromLocal8Bit(content));
  file.close();

  // Check for XML document version updates
  versionUpdate();

  // Document validation
  Manager::validate();
  
  // We have just loaded the bookmarks, so update all the menus
  emit update();
}

void Manager::save()
{
  // Save the new XML file
  if (m_filename.isEmpty()) {
    tqDebug("WARNING: No open XML file, will NOT save.");
    return;
  }

  TQFile file(m_filename);
  if (!file.open(IO_WriteOnly)) {
    tqDebug("WARNING: Unable to open xml for writing!");
    return;
  }

  // Should we encrypt the data ?
  TQString content = m_document.toString();
  if (KFTPCore::Config::encryptBookmarks()) {
    DESEncryptor des;

    if (m_decryptKey.isEmpty()) {
      // Ask the user for the password
      KPasswordDialog::getPassword(m_decryptKey, i18n("Enter key for bookmark file encryption."));
    }

    des.setKey(m_decryptKey);
    des.encrypt(content);

    content = des.output();
  }

  // Write the XML data to the stream
  TQTextStream fileStream(&file);
  fileStream << content;
  file.flush();
  file.close();
}

void Manager::validate(TQDomNode node)
{
  if (node.isNull())
    node = m_document.documentElement();
    
  TQDomNode n = node.firstChild();

  while (!n.isNull()) {
    if (n.toElement().tagName() == "category") {
      if (!n.toElement().hasAttribute("id"))
        n.toElement().setAttribute("id", TQString("cat-%1").arg(TDEApplication::randomString(7)));
      
      Manager::validate(n);
    } else if (n.toElement().tagName() == "server") {
      if (!n.toElement().hasAttribute("id"))
        n.toElement().setAttribute("id", TQString("site-%1").arg(TDEApplication::randomString(7)));
    }
    
    n = n.nextSibling();
  }
}

void Manager::versionUpdate()
{
  int version = m_document.documentElement().attribute("version").toInt();

  if (version < KFTP_BOOKMARKS_VERSION) {
    // Conversion from an old bookmark file
    tqDebug("Detected an old (version %d, new version %d) bookmarks file. Starting conversion process...", version, KFTP_BOOKMARKS_VERSION);

    // NOTE: There are no breaks here, since every update method updates to a specific
    // version. So in order to convert to the most current from the oldest version, all
    // methods need to be called!
    switch (version) {
      case 0:
      case 1: versionFrom1Update();
    }

    // Fix the version
    m_document.documentElement().setAttribute("version", KFTP_BOOKMARKS_VERSION);
  }
}

void Manager::versionFrom1Update(TQDomNode parent)
{
  // The original format had no site ids, so we have to generate them. Also the old
  // format used something wierd called "options", we have to convert them as well.
  // The username/password fields now have differend names.

  if (parent.isNull())
    parent = m_document.documentElement();

  TQDomNode n = parent.firstChild();

  while (!n.isNull()) {
    if (n.toElement().tagName() == "category") {
      // Update the category id and all children
      n.toElement().setAttribute("id", TQString("cat-%1").arg(TDEApplication::randomString(7)));

      versionFrom1Update(n);
    } else if (n.toElement().tagName() == "server") {
      // Update the server id
      n.toElement().setAttribute("id", TQString("site-%1").arg(TDEApplication::randomString(7)));

      // Convert the "options"
      TQDomNodeList nodes = n.toElement().elementsByTagName("option");

      if (nodes.length() > 0) {
        for (unsigned int i = 0; i < nodes.count(); i++) {
          TQDomNode node = nodes.item(i);

          // Add a new standard property
          TQDomElement property = m_document.createElement(node.toElement().attribute("name"));
          n.appendChild(property);

          TQDomText text = m_document.createTextNode(node.toElement().attribute("value"));
          property.appendChild(text);

          // And remove the option :>
          node.parentNode().removeChild(node);
          i--;
        }
      }

      // Rename the username/password fields
      nodes = n.toElement().elementsByTagName("downuser");
      if (nodes.length() > 0) {
        for (unsigned int i = 0; i < nodes.count(); i++) {
          TQDomNode node = nodes.item(i);
          node.toElement().setTagName("username");
        }
      }

      nodes = n.toElement().elementsByTagName("downpass");
      if (nodes.length() > 0) {
        for (unsigned int i = 0; i < nodes.count(); i++) {
          TQDomNode node = nodes.item(i);
          node.toElement().setTagName("password");
        }
      }
    }

    n = n.nextSibling();
  }
}

Site *Manager::findSite(const KURL &url)
{
  // Find the node, then see if it is already present in the cache
  TQDomNode siteElement = findSiteElementByUrl(url);

  if (!siteElement.isNull()) {
    // Try to get a cached version
    Site *site = m_siteCache.find(siteElement.toElement().attribute("id"));

    if (!site) {
      site = new Site(siteElement);
      m_siteCache.insert(siteElement.toElement().attribute("id"), site);
    }

    return site;
  } else {
    return NULL;
  }
}

Site *Manager::findSite(const TQString &id)
{
  if (id.isNull())
    return NULL;

  // Try the cache first
  Site *site = m_siteCache.find(id);

  if (!site) {
    // The site was not found, search in the DOM tree and add it to the
    // cache if found.
    TQDomNode siteElement = findSiteElementById(id);

    if (siteElement.isNull()) {
      tqDebug("WARNING: Unable to find site with id '%s'!", id.ascii());

      return NULL;
    }

    site = new Site(siteElement);
    m_siteCache.insert(id, site);
  }

  return site;
}

TQDomNode Manager::findSiteElementByUrl(const KURL &url, TQDomNode parent)
{
  if (parent.isNull())
    parent = m_document.documentElement();

  TQDomNode n = parent.firstChild();

  while (!n.isNull()) {
    if (n.toElement().tagName() == "category") {
      // Check the category
      TQDomNode site = findSiteElementByUrl(url, n);

      if (!site.isNull())
        return site;
    } else if (n.toElement().tagName() == "server") {
      // Check if the server matches
      Site *tmp = new Site(n);

      if (tmp->getProperty("host") == url.host() &&
          tmp->getIntProperty("port") == url.port() &&
          tmp->getProperty("username") == url.user() &&
          tmp->getProperty("password") == url.pass())
      {
        delete tmp;
        return n;
      }

      delete tmp;
    }

    n = n.nextSibling();
  }

  return TQDomNode();
}

TQDomNode Manager::findSiteElementById(const TQString &id, TQDomNode parent)
{
  if (parent.isNull())
    parent = m_document.documentElement();

  TQDomNode n = parent.firstChild();

  while (!n.isNull()) {
    if (n.toElement().tagName() == "category") {
      // Check the category
      TQDomNode site = findSiteElementById(id, n);

      if (!site.isNull())
        return site;
    } else if (n.toElement().tagName() == "server") {
      // Check if the server matches
      if (n.toElement().attribute("id") == id)
        return n;
    }

    n = n.nextSibling();
  }

  return TQDomNode();
}

TQDomNode Manager::findCategoryElementById(const TQString &id, TQDomNode parent)
{
  if (id == "root")
    return m_document.documentElement();

  if (parent.isNull())
    parent = m_document.documentElement();

  TQDomNode n = parent.firstChild();

  while (!n.isNull()) {
    if (n.toElement().tagName() == "category") {
      if (n.toElement().attribute("id") == id)
        return n;

      // Check the category
      TQDomNode site = findCategoryElementById(id, n);

      if (!site.isNull())
        return site;
    }

    n = n.nextSibling();
  }

  return TQDomNode();
}

Site *Manager::findCategory(const TQString &id)
{
  // Try the cache first
  Site *site = m_siteCache.find(id);

  if (!site) {
    // The site was not found, search in the DOM tree and add it to the
    // cache if found.
    TQDomNode siteElement = findCategoryElementById(id);

    if (siteElement.isNull()) {
      tqDebug("WARNING: Unable to find category with id '%s'!", id.ascii());

      return NULL;
    }

    site = new Site(siteElement);
    m_siteCache.insert(id, site);
  }

  return site;
}

Site *Manager::addSite(Site *category, TQDomNode node)
{
  if (category)
    return category->addSite(node);

  return NULL;
}

void Manager::delSite(Site *site)
{
  // Remove the node from the DOM tree
  site->m_element.parentNode().removeChild(site->m_element);

  // Remove the site from cache and it will be automaticly deleted
  m_siteCache.remove(site->id());

  emit update();
}

void Manager::setupClient(Site *site, KFTPEngine::Thread *client)
{
  if (site) {
    // First activate the correct socket and reset the old flags
    client->selectSocketForProtocol(KURL(TQString("%1://test/").arg(site->getProperty("protocol"))));
    client->socket()->initConfig();

    int retryTime = site->getIntProperty("retrytime");
    int retryCnt = site->getIntProperty("retrycount");

    if (retryTime != 0) {
      client->socket()->setConfig("retry", 1);
      client->socket()->setConfig("max_retries", retryCnt);
      client->socket()->setConfig("retry_delay", retryTime);
    } else {
      client->socket()->setConfig("retry", 0);
    }
    
    client->socket()->setConfig("keepalive.enabled", site->getIntProperty("doKeepalive"));
    client->socket()->setConfig("keepalive.timeout", site->getIntProperty("keepaliveTimeout"));

    client->socket()->setConfig("ssl.use_tls", site->getIntProperty("use_tls"));
    client->socket()->setConfig("ssl.use_implicit", site->getIntProperty("use_implicit"));
    client->socket()->setConfig("ssl.prot_mode", site->getProperty("tls_data_mode"));
    client->socket()->setConfig("feat.pasv", site->getIntProperty("disablePASV") == 1 ? 0 : 1);
    client->socket()->setConfig("feat.epsv", site->getIntProperty("disableEPSV") == 1 ? 0 : 1);
    client->socket()->setConfig("pasv.use_site_ip", site->getIntProperty("pasvSiteIp"));
    client->socket()->setConfig("active.no_force_ip", site->getIntProperty("disableForceIp"));
    client->socket()->setConfig("stat_listings", site->getIntProperty("statListings"));
    client->socket()->setConfig("encoding", site->getProperty("encoding"));

    // Should we use a X509 certificate ?
    if (site->getIntProperty("use_cert") && site->getProperty("protocol") == "ftp") {
      // Ask the user for the decryption password
      TQCString certPass;
      KPasswordDialog::getPassword(certPass, i18n("Please provide your X509 certificate decryption password."));

      static_cast<KFTPEngine::FtpSocket*>(client->socket())->setSslClientCertificate(KSSLPKCS12::loadCertFile(site->getProperty("tls_cert_path"), certPass));
    }
  } else {
    // Just reset the client, since we don't know the config
    client->socket()->initConfig();
  }
}

void Manager::guiPopulateBookmarksTree(KFTPWidgets::Bookmarks::ListView *tree, TQDomNode parent, KFTPWidgets::Bookmarks::ListViewItem *item)
{
  if (parent.isNull()) {
    // Clear the tree and set the parent
    tree->clear();
    parent = m_document.documentElement();
  }

  TQDomNode n = parent.firstChild();

  while (!n.isNull()) {
    if (n.toElement().tagName() == "category") {
      // Add a submenu
      KFTPWidgets::Bookmarks::ListViewItem *cat;
      Site *site = findCategory(n.toElement().attribute("id"));

      if (!item)
        cat = new KFTPWidgets::Bookmarks::ListViewItem(tree, site->getAttribute("name"));
      else
        cat = new KFTPWidgets::Bookmarks::ListViewItem(item, site->getAttribute("name"));

      cat->setType(0);
      cat->setSite(site);

      cat->setPixmap(0, loadSmallPixmap("bookmark_folder"));
      guiPopulateBookmarksTree(tree, n, cat);
    } else if (n.toElement().tagName() == "server") {
      KFTPWidgets::Bookmarks::ListViewItem *serv;
      Site *site = findSite(n.toElement().attribute("id"));

      if (!item)
        serv = new KFTPWidgets::Bookmarks::ListViewItem(tree, site->getAttribute("name"));
      else
        serv = new KFTPWidgets::Bookmarks::ListViewItem(item, site->getAttribute("name"));

      serv->setType(1);
      serv->setSite(site);

      serv->setPixmap(0, loadSmallPixmap("ftp"));
    }

    n = n.nextSibling();
  }
}

void Manager::guiPopulateBookmarksMenu(TDEActionMenu *parentMenu, TQDomNode parentNode, bool base, TQObject *data)
{
  if (parentNode.isNull())
    parentNode = m_document.documentElement();

  TQDomNode n = parentNode.firstChild();
  TDEActionMenu *menu = 0L;
  KFTPBookmarkAction *action = 0L;

  while (!n.isNull()) {
    TQString name = n.toElement().attribute("name");

    if (n.toElement().tagName() == "category") {
      menu = new TDEActionMenu(name, "bookmark_folder", parentMenu);
      parentMenu->insert(menu, base ? 5 : 0);

      // Fill the menu
      guiPopulateBookmarksMenu(menu, n, false, data);
    } else if (n.toElement().tagName() == "server") {
      action = new KFTPBookmarkAction(name, "ftp", TDEShortcut(), this, SLOT(slotBookmarkExecuted()), KFTPAPI::getInstance()->mainWindow()->actionCollection());
      action->setSiteId(n.toElement().attribute("id"));
      action->setData(data);

      parentMenu->insert(action);
    }

    n = n.nextSibling();
  }
}

void Manager::guiPopulateZeroconfMenu(TDEActionMenu *parentMenu)
{
  // Clear the menu
  parentMenu->popupMenu()->clear();

#if KDE_IS_VERSION(3,4,0)
  // Populate
  TQValueList<DNSSD::RemoteService::Ptr> list = KFTPAPI::getInstance()->zeroConfInterface()->getServiceList();

  if (!list.empty()) {
    TQValueList<DNSSD::RemoteService::Ptr>::iterator end(list.end());

    for (TQValueList<DNSSD::RemoteService::Ptr>::iterator i(list.begin()); i != end; ++i) {
      KFTPZeroconfAction *newService = new KFTPZeroconfAction((*i)->serviceName(), "lan", TDEShortcut(), this, SLOT(slotZeroconfExecuted()), KFTPAPI::getInstance()->mainWindow()->actionCollection());
      newService->setSite(*i);

      parentMenu->insert(newService);
    }
  } else {
    TDEAction *disabledAction = new TDEAction(i18n("<No Services Published>"));
    disabledAction->setEnabled(false);
    parentMenu->insert(disabledAction);
  }
#else
  TDEAction *disabledAction = new TDEAction(i18n("<DNSSD Not Available>"));
  disabledAction->setEnabled(false);
  parentMenu->insert(disabledAction);
#endif
}

void Manager::guiPopulateWalletMenu(TDEActionMenu *parentMenu)
{
  // Clear the menu
  parentMenu->popupMenu()->clear();
  
  // Populate
  TQValueList<KURL> list = KFTPAPI::getInstance()->walletConnection()->getSiteList();

  if (!list.empty()) {
    TQValueList<KURL>::iterator end(list.end());

    for (TQValueList<KURL>::iterator i(list.begin()); i != end; ++i) {
      TQString desc;
      
      if ((*i).port() != 21)
        desc = TQString("%1@%2:%3").arg((*i).user()).arg((*i).host()).arg((*i).port());
      else
        desc = TQString("%1@%2").arg((*i).user()).arg((*i).host());
        
      KFTPWalletAction *newSite = new KFTPWalletAction(desc, "ftp", TDEShortcut(), this, SLOT(slotWalletExecuted()), KFTPAPI::getInstance()->mainWindow()->actionCollection());
      newSite->setSite(*i);

      parentMenu->insert(newSite);
    }
  } else {
    TDEAction *disabledAction = new TDEAction(i18n("<No Sites In KWallet>"));
    disabledAction->setEnabled(false);
    parentMenu->insert(disabledAction);
  }
}

void Manager::slotBookmarkExecuted()
{
  // Get the sender
  KFTPBookmarkAction *action = (KFTPBookmarkAction*) TQObject::sender();
  Site *site = findSite(action->siteId());

  // Get the node data from bookmarks
  KURL siteUrl = site->getUrl();

  // Handle empty usernames and passwords for non-anonymous sites
  if (!siteUrl.hasUser() || !siteUrl.hasPass() && siteUrl.user() != "anonymous") {
    TDEIO::PasswordDialog *dlg = new TDEIO::PasswordDialog(i18n("Please provide your username and password for connecting to this site."), siteUrl.user(), true);
    dlg->addCommentLine(i18n("Site:"), TQString("%1:%2").arg(siteUrl.host()).arg(siteUrl.port()));
    
    if (dlg->exec() == KDialogBase::Accepted) {
      siteUrl.setUser(dlg->username());
      siteUrl.setPass(dlg->password());
      
      if (dlg->keepPassword()) {
        // Save password to the bookmarked site
        site->setProperty("username", dlg->username());
        site->setProperty("password", encodePassword(dlg->password()));
      }
      
      delete dlg;
    } else {
      // Abort connection attempt
      delete dlg;
      return;
    }
  }

  if (action->data()) {
    // A specific session was passed on to us
    KFTPSession::Session *session = static_cast<KFTPSession::Session*>(action->data());
    
    // Set the correct client for connection
    KFTPEngine::Thread *client = session->getClient();

    // Now, connect to the server
    if (client->socket()->isConnected()) {
      if (KFTPCore::Config::confirmDisconnects() && KMessageBox::warningYesNo(0, i18n("Do you want to drop current connection?")) == KMessageBox::No)
        return;
    }

    client->socket()->setCurrentUrl(siteUrl);

    // Set the session's site and connect
    session->setSite(site);
    session->reconnect(siteUrl);
  } else {
    // Just spawn a new session
    KFTPSession::Session *session = KFTPSession::Manager::self()->spawnRemoteSession(KFTPSession::IgnoreSide, siteUrl, site);
    KFTPSession::Manager::self()->setActive(session);
  }
}

void Manager::slotWalletExecuted()
{
  // Get the sender
  KFTPWalletAction *action = (KFTPWalletAction*) TQObject::sender();
  KURL siteUrl = action->getSite();

  // Just spawn a new session
  KFTPSession::Manager::self()->spawnRemoteSession(KFTPSession::IgnoreSide, siteUrl);
}

void Manager::slotZeroconfExecuted()
{
#if KDE_IS_VERSION(3,4,0)
  // Get the sender
  KFTPZeroconfAction *action = (KFTPZeroconfAction*) TQObject::sender();
  DNSSD::RemoteService::Ptr service = action->getSite();

  KFTPAPI::getInstance()->mainWindow()->slotQuickConnect(service->serviceName(), service->hostName(), service->port());
#endif
}

}


#include "kftpbookmarks.moc"