summaryrefslogtreecommitdiffstats
path: root/quanta/project/projectupload.cpp
blob: 6d07512769971cd8f9166ae5b513bcd64e175c7f (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
/***************************************************************************
                          projectupload.cpp  -  description
                             -------------------
    begin                : Wed Nov 15 2000
    copyright            : (C) 2000 by Dmitry Poplavsky & Alexander Yakovlev & Eric Laffoon <pdima@users.sourceforge.net,yshurik@penguinpowered.com,sequitur@easystreet.com>
                           (C) 2001-2003 by Andras Mantia
 ***************************************************************************/

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



//qt includes
#include <tqlistview.h>
#include <tqeventloop.h>
#include <tqfileinfo.h>
#include <tqlineedit.h>
#include <tqpushbutton.h>
#include <tqstringlist.h>
#include <tqregexp.h>
#include <tqlabel.h>
#include <tqcombobox.h>
#include <tqurl.h>
#include <tqcheckbox.h>
#include <tqtimer.h>
#include <tqframe.h>
#include <tqtooltip.h>

//kde includes
#include <kapplication.h>
#include <kurl.h>
#include <tdeio/job.h>
#include <kmessagebox.h>
#include <kprotocolinfo.h>
#include <kdebug.h>
#include <kinputdialog.h>
#include <tdeio/netaccess.h>
#include <klocale.h>
#include <klineedit.h>
#include <kcombobox.h>
#include <kprogress.h>
#include <kpassdlg.h>
#include <kpushbutton.h>
#include <ktempfile.h>
#include <kuser.h>

//standard includes
#include <time.h>

//own includes
#include "uploadprofiledlgs.h"
#include "projectupload.h"
#include "project.h"
#include "quantacommon.h"
#include "qextfileinfo.h"
#include "resource.h"

ProjectUpload::ProjectUpload(const KURL& url, const TQString& profileName, bool showOnlyProfiles, bool quickUpload, bool markOnly, const char* name)
  :ProjectUploadS( 0L, name, false, TQt::WDestructiveClose)
{
    m_quickUpload = quickUpload;
    m_profilesOnly = showOnlyProfiles;
    list->hide();
    m_project = Project::ref();
    initProjectInfo(profileName);
    startUrl = url;
    if (m_profilesOnly)
    {
      clearWFlags(TQt::WDestructiveClose);
      uploadFrame->hide();
      buttonCancel->hide();
      adjustSize();
      buttonUpload->setText(i18n("&Close"));
      setCaption(i18n("Upload Profiles"));
    } else
    {
      if (markOnly)
        markAsUploaded->setChecked(true);
      TQTimer::singleShot(10, this, TQT_SLOT(slotBuildTree()));
      currentItem = 0L;
    }
}


ProjectUpload::~ProjectUpload()
{
  m_project->setModified(true);
  delete baseUrl;
}

void  ProjectUpload::initProjectInfo(const TQString& defaultProfile)
{
  baseUrl = new KURL();

//  list->setMultiSelection(true);
  TQDomDocument *dom = m_project->sessionDom();
  m_profilesNode = dom->firstChild().firstChild().namedItem("uploadprofiles");
  if (m_profilesNode.isNull()) //compat code, remove when upgrade from 3.2 is not supported
  {
     m_currentProfileElement = dom->firstChild().firstChild().namedItem("upload").toElement();
     m_defaultProfile = m_currentProfileElement.attribute("user","") + "@" + m_currentProfileElement.attribute("remote_host","");
     TQDomElement e = dom->createElement("uploadprofiles");
     e.setAttribute("defaultProfile", m_defaultProfile);
     TQDomElement el = dom->createElement("profile");
     el.setAttribute("remote_host", m_currentProfileElement.attribute("remote_host",""));
     el.setAttribute("user", m_currentProfileElement.attribute("user",""));
     el.setAttribute("remote_path", m_currentProfileElement.attribute("remote_path",""));
     el.setAttribute("remote_port", m_currentProfileElement.attribute("remote_port",""));
     el.setAttribute("remote_protocol", m_currentProfileElement.attribute("remote_protocol","ftp"));
     el.setAttribute("name", m_defaultProfile);
     e.appendChild(el);
//     m_project->dom.firstChild().firstChild().removeChild(m_currentProfileElement);
     m_currentProfileElement = el;
     dom->firstChild().firstChild().appendChild(e);
     m_profilesNode = e;
     comboProfile->insertItem(m_defaultProfile);
     m_project->setModified(true);
  } else
  {
      if (defaultProfile.isEmpty())
        m_defaultProfile = m_profilesNode.toElement().attribute("defaultProfile");
      else
        m_defaultProfile = defaultProfile;
      TQDomNodeList profileList = m_profilesNode.toElement().elementsByTagName("profile");
      TQDomElement e;
      m_currentProfileElement = profileList.item(0).toElement();
      TQString s;
      int defaultIdx = 0;
      for (uint i = 0; i < profileList.count(); i++)
      {
          e = profileList.item(i).toElement();
          s = e.attribute("name");
          comboProfile->insertItem(s);
          if (s == m_defaultProfile)
          {
            defaultIdx = i;
            m_currentProfileElement = e;
          }
      }
      comboProfile->setCurrentItem(defaultIdx);
  }
  buttonRemoveProfile->setEnabled(comboProfile->count() > 1);
  keepPasswords->setChecked(m_project->keepPasswd);
  uploadInProgress = false;
  connect( this, TQT_SIGNAL( uploadNext() ), TQT_SLOT( slotUploadNext() ) );
  setProfileTooltip();
}

/** No descriptions */
void ProjectUpload::slotBuildTree()
{
 emit eventHappened("upload_requested", m_project->projectBaseURL().url(), "");
 loadRemoteUploadInfo();
 TDEIO::UDSEntry entry;
 TQString strUrl = QuantaCommon::qUrl(startUrl);
 bool isDirectory = strUrl.endsWith("/");
 bool forceUpload = !startUrl.isEmpty();
 TQString s;
 TQDomElement el;
 TQDomNodeList nl = m_project->dom()->elementsByTagName("item");
 totalProgress->setTotalSteps(nl.count() - 1 );
 totalProgress->setValue(0);
 totalText->setText(i18n("Scanning project files..."));

 KURL u = m_project->projectBaseURL();
 if (!startUrl.isEmpty())
 {
   u = QExtFileInfo::toAbsolute(startUrl, u);
 }
 TQDict<KFileItem> projectDirFiles;

 if (startUrl.isEmpty() || strUrl.endsWith("/")) //upload a folder
 {
   projectDirFiles = QExtFileInfo::allFilesDetailed(u, "*", this);
 } else
 {
   projectDirFiles.insert(u.url(), new KFileItem(KFileItem::Unknown, KFileItem::Unknown, u, true));
 }

 TQTime t;
 t.start();

 u = m_project->projectBaseURL();
 KURL absUrl = u;
 for (uint i = 0; i < nl.count(); i++)
 {
   el = nl.item(i).toElement();
   s = el.attribute("url");
   if (startUrl.isEmpty() || (s.startsWith(strUrl) && isDirectory) || s == strUrl)
   {
     QuantaCommon::setUrl(u, s);
     absUrl.setPath(m_project->projectBaseURL().path(1)+u.path(-1));
/*     if (!QExtFileInfo::exists(absUrl))
       continue; */
     KFileItem *p_item = projectDirFiles.find(absUrl.url());
     if (!p_item)
       continue;
     KFileItem item(*p_item);
     UploadTreeFile *it = list->addItem(u, item);
     if ( it != 0 )
     {
       int uploadedTime = -1;
       if (m_uploadTimeList.contains(s))
         uploadedTime = m_uploadTimeList[s];
       int modifiedTime = item.time(TDEIO::UDS_MODIFICATION_TIME);
       el.setAttribute("modified_time", modifiedTime);
       int uploadStatus = el.attribute("uploadstatus", "1").toInt();
       if (m_quickUpload || (forceUpload && uploadStatus == 0))
        uploadStatus = 1;
       if (m_quickUpload || (uploadedTime != modifiedTime && uploadStatus != 0))
       {
         modified.append( u );
         it->setSelected(true);
       }
       if (uploadStatus == 2)
         it->setConfirmUpload(true);
       totalProgress->setValue(i);
     }
   }
 }
 projectDirFiles.setAutoDelete(true);
 projectDirFiles.clear();
 totalText->setText(i18n("Building the tree..."));
 list->checkboxTree();
 if (!startUrl.isEmpty())
      expandAll();
 list->show();
 totalText->setText(i18n("Total:"));
 totalProgress->setTotalSteps(1);
 totalProgress->setValue(0);
 //hack to force repainting of the treeview
 resize(width() + 1, height());
 resize(width() - 1, height());
 if (m_quickUpload)
   startUpload();
}


void ProjectUpload::buildSelectedItemList()
{
  TQListViewItem *item;
  TQListViewItemIterator it(list);
  toUpload.clear();
  needsConfirmation.clear();
  for ( ; it.current(); ++it )
  {
   item = it.current();
   if ( list->isSelected( item ))
   {
     KURL u;
     if (dynamic_cast<UploadTreeFolder*>(item))
     {
      u = dynamic_cast<UploadTreeFolder*>(item)->url();
     } else
     {
       UploadTreeFile* fileItem = dynamic_cast<UploadTreeFile*>(item);
       u = fileItem->url();
       if (fileItem->confirmUpload() && !u.isEmpty())
         needsConfirmation.append(item);
     }

     if (!u.isEmpty())
         toUpload.append(item);
   }
  }
}

void ProjectUpload::initBaseUrl()
{
  TQString path = m_currentProfileElement.attribute("remote_path","");
  if (!path.startsWith("/"))
    path.prepend("/");

  baseUrl->setProtocol(m_currentProfileElement.attribute("remote_protocol","ftp"));
  baseUrl->setPort(m_currentProfileElement.attribute("remote_port","").toInt());
  baseUrl->setHost(m_currentProfileElement.attribute("remote_host",""));
  baseUrl->setPath(path);
  baseUrl->setUser(m_currentProfileElement.attribute("user",""));
  TQString password;
  if (keepPasswords->isChecked())
  {
     m_project->keepPasswd = true;
     password = m_project->password(m_currentProfileElement.attribute("remote_protocol") + "://" + m_currentProfileElement.attribute("user") + "@" + m_currentProfileElement.attribute("remote_host"));
  } else
  {
     m_project->keepPasswd = false;
     if (m_currentProfileElement != m_lastEditedProfileElement)
     {
         m_lastPassword = "";
     }
     password = m_lastPassword;
  }
  baseUrl->setPass(password);
}

void ProjectUpload::startUpload()
{
  if (m_profilesOnly)
  {
    TQDialog::accept();
    return;
   }
  stopUpload = false;
  initBaseUrl();
  if (markAsUploaded->isChecked())
  {
      TQStringList selectedList;
      TQListViewItem *item;
      TQListViewItemIterator it(list);
      for ( ; it.current(); ++it )
      {
          item = it.current();
          if ( list->isSelected( item ))
          {
            KURL u;
            if (dynamic_cast<UploadTreeFolder*>(item))
            {
              u = dynamic_cast<UploadTreeFolder*>(item)->url();
            } else
            {
              u = dynamic_cast<UploadTreeFile*>(item)->url();
            }

            if (!u.isEmpty())
                selectedList.append(QuantaCommon::qUrl(u));
          }
     }
    //update upload time
    TQDomNodeList nl = m_project->dom()->elementsByTagName("item");
    TQDomElement el;
    for ( uint i = 0; i < nl.count(); i++ )
    {
      el = nl.item(i).toElement();
      if ( selectedList.contains(el.attribute("url")))
      {
        m_uploadTimeList[el.attribute("url")] = el.attribute("modified_time").toInt();
      }
    }
    saveRemoteUploadInfo();
    accept();
  } else
  {
    buildSelectedItemList();
    int confirmCount = needsConfirmation.count();
    if (confirmCount > 0)
    {
        TQValueList<TQListViewItem*>::Iterator it;
        TQStringList confirmList;
        for (it = needsConfirmation.begin(); it != needsConfirmation.end(); ++it)
        {
            confirmList.append(((UploadTreeFile*)(*it))->url().prettyURL(0, KURL::StripFileProtocol));
        }
        bool ok;
        TQStringList confirmedList = KInputDialog::getItemList(i18n("Confirm Upload"), i18n("Confirm that you want to upload the following files (unselect the files you do not want to upload):"), confirmList, confirmList, true, &ok, this);
        if (!ok)  return;
        for (it = needsConfirmation.begin(); it != needsConfirmation.end(); ++it)
        {
            if (!confirmedList.contains(((UploadTreeFile*)(*it))->url().prettyURL(0, KURL::StripFileProtocol)))
              toUpload.remove(*it);
        }

    }
    int selectedNum = toUpload.count();
    totalProgress->setProgress(0);
    totalProgress->setTotalSteps(selectedNum);
    uploadInProgress = true;
    suspendUpload = false;
    buttonUpload->setEnabled(false);
    KURL u = *baseUrl;
    u.setPath(u.protocol() == "file" ? "/" : "");
    if (QExtFileInfo::exists(u, false, this) || (u.protocol() == "webdav" && QExtFileInfo::exists(*baseUrl, false, this)))
    {
      upload();
      return;
    } else
    {
      if (KMessageBox::warningContinueCancel(this, i18n("<qt><b>%1</b> seems to be unaccessible.<br>Do you want to proceed with upload?</qt>")
          .arg(u.prettyURL(0, KURL::StripFileProtocol)),TQString(),KStdGuiItem::cont()) == KMessageBox::Continue)
      {
        upload();
        return;
      }
    }
  }
  buttonUpload->setEnabled(true);
  uploadInProgress = false;
}

void ProjectUpload::upload()
{
  if (stopUpload)
  {
    saveRemoteUploadInfo();
    return;
  }
  KURL dir;
  KURL to;
  UploadTreeFile *fileItem;
  UploadTreeFolder *folderItem;

  if (toUpload.isEmpty())
  {
    saveRemoteUploadInfo();
    accept();
    return;
  }
  currentItem = toUpload.at(0);
  if (dynamic_cast<UploadTreeFile*>(currentItem))
  {
    fileItem = static_cast<UploadTreeFile*>(currentItem);
    folderItem = 0L;
    currentURL = fileItem->url();
  } else
  {
    fileItem = 0L;
    folderItem = static_cast<UploadTreeFolder*>(currentItem);
    currentURL = folderItem->url();
  }


  KURL from = QExtFileInfo::toAbsolute(currentURL, m_project->projectBaseURL());
  to = *baseUrl;
  to.addPath( currentURL.path() );
  if (to.fileName(false).isEmpty())
  {
    dir = to;
  }
  else
  {
    dir = to.upURL() ;
  }

  if ( !madeDirs.contains(dir) )
  {
    madeDirs.append( dir );
    if (!QExtFileInfo::createDir(dir, this))
    {
      QuantaCommon::dirCreationError(this, KURL( dir.prettyURL(0, KURL::StripFileProtocol) ));
      buttonUpload->setEnabled(true);
      uploadInProgress = false;
      saveRemoteUploadInfo();
      return;
    }
  }

  // tqDebug("%s -> %s", from.url().data(), to.url().data() );
  if (!from.fileName(false).isEmpty() && fileItem)
  {
    emit eventHappened("before_upload", from.url(), to.url());
    TDEIO::FileCopyJob *job = TDEIO::file_copy( from, to, fileItem->permissions(), true, false, false );

    connect( job, TQT_SIGNAL( result( TDEIO::Job * ) ),this,
                      TQT_SLOT( uploadFinished( TDEIO::Job * ) ) );
    connect( job, TQT_SIGNAL( percent( TDEIO::Job *,unsigned long ) ),
                this, TQT_SLOT( uploadProgress( TDEIO::Job *,unsigned long ) ) );
    connect( job, TQT_SIGNAL( infoMessage( TDEIO::Job *,const TQString& ) ),
                this, TQT_SLOT( uploadMessage( TDEIO::Job *,const TQString& ) ) );

    labelCurFile->setText(i18n("Current: %1").arg(currentURL.fileName()));
    currentProgress->setProgress( 0 );
    return;
  } else  //it is a dir, so just go to the next item
  {
    emit uploadNext();
    return;
  }
  saveRemoteUploadInfo();
  buttonUpload->setEnabled(true);
  uploadInProgress = false;
  reject();
}

void ProjectUpload::uploadFinished( TDEIO::Job *job )
{
   if ( !job ) return;

   if ( job->error() )
   {
      job->showErrorDialog( this  );
      uploadInProgress = false;
      buttonUpload->setEnabled(true);
      saveRemoteUploadInfo();
      return;
   }
   TDEIO::FileCopyJob *fJob = dynamic_cast<TDEIO::FileCopyJob *>(job);
   if (fJob)
    emit eventHappened("after_upload", fJob->srcURL().url(), fJob->destURL().url());
   slotUploadNext();
}

void ProjectUpload::uploadProgress ( TDEIO::Job *, unsigned long percent  )
{
  currentProgress->setProgress( percent );
}

void ProjectUpload::uploadMessage ( TDEIO::Job *, const TQString & msg )
{
    labelCurFile->setText( currentURL.fileName() + " : " + msg );
}

void ProjectUpload::selectAll()
{
  list->selectAll(true);
  list->checkboxTree();
}

void ProjectUpload::selectModified()
{
  for ( KURL::List::Iterator file = modified.begin(); file != modified.end(); ++file )
  {
    TQListViewItem *it = list->findItem( (*file).path() );
    it->setSelected(true);
    it->repaint();
  }
  list->checkboxTree();
}

void ProjectUpload::clearSelection()
{
  list->selectAll(false);
  list->checkboxTree();
}

void ProjectUpload::invertSelection()
{
  list->invertAll();
  list->checkboxTree();
}

void ProjectUpload::expandAll()
{
  list->expandAll();
}

void ProjectUpload::collapseAll()
{
  list->collapseAll();
}

void ProjectUpload::resizeEvent ( TQResizeEvent *t )
{
  ProjectUploadS::resizeEvent(t);
  list->setColumnWidth(0,list->width()-list->columnWidth(1)-list->columnWidth(2)-20);
}

/** No descriptions */
void ProjectUpload::slotUploadNext()
{
  if (!suspendUpload)
  {
    totalProgress->setProgress(totalProgress->progress()+1);
   // TQListViewItem *it = list->findItem( currentURL.path() );
    TQListViewItem *it = currentItem;
    if (it)
    {
     it->setSelected(false);
     UploadTreeFile *itf = dynamic_cast<UploadTreeFile*>(it);
     if (itf)
      itf->setWhichPixmap( "check_clear" );
     it->repaint();
    }
    toUpload.remove( it );

    //update upload time
    TQDomNodeList nl = m_project->dom()->elementsByTagName("item");
    TQDomElement el;
    for ( uint i = 0; i < nl.count(); i++ )
    {
      el = nl.item(i).toElement();
      if ( el.attribute("url") == QuantaCommon::qUrl(currentURL) )
      {
        m_uploadTimeList[el.attribute("url")] = el.attribute("modified_time").toInt();
        break;
      }
    }

    upload();
  }
}

void ProjectUpload::clearProjectModified()
{
  TQDomNodeList nl = m_project->dom()->elementsByTagName("item");
  for ( unsigned int i=0; i<nl.count(); i++ )
  {
    TQDomElement el = nl.item(i).toElement();
    m_uploadTimeList[el.attribute("url")] = el.attribute("modified_time").toInt();
  }
  modified.clear();
  list->clearSelection();
  list->checkboxTree();
}

void ProjectUpload::slotNewProfile()
{
  UploadProfileDlgS *profileDlg = new UploadProfileDlgS(this);
  TQDomElement el = m_currentProfileElement;
  m_currentProfileElement = m_project->dom()->createElement("profile");
  fillProfileDlg(profileDlg);
  if (profileDlg->exec())
  {
     readProfileDlg(profileDlg);
     m_profilesNode.appendChild(m_currentProfileElement);
     m_project->setModified(true);
     comboProfile->insertItem(m_currentProfileElement.attribute("name"), 0);
     setProfileTooltip();
  } else
    m_currentProfileElement = el;
  delete profileDlg;
  buttonRemoveProfile->setEnabled(comboProfile->count() > 1);
}

void ProjectUpload::slotEditProfile()
{
  UploadProfileDlgS *profileDlg = new UploadProfileDlgS(this);
  fillProfileDlg(profileDlg);
  if (profileDlg->exec())
  {
    readProfileDlg(profileDlg);
    m_project->setModified(true);
    comboProfile->changeItem(profileDlg->lineProfileName->text(), comboProfile->currentItem());
    setProfileTooltip();
  }
  delete profileDlg;
}

void ProjectUpload::slotRemoveProfile()
{
   if (comboProfile->count() == 1)
   {
       KMessageBox::error(this, i18n("You cannot remove the last profile."), i18n("Profile Removal Error") );
   } else
   {
      TQString profileName = comboProfile->currentText();
      if (KMessageBox::warningContinueCancel(this, i18n("<qt>Do you really want to remove the <b>%1</b> upload profile?</qt>").arg(profileName),
                                   i18n("Profile Removal"), KStdGuiItem::del()) == KMessageBox::Continue)
       {
          m_profilesNode.removeChild(m_currentProfileElement);
          int idx = comboProfile->currentItem();
          int newIdx = idx + 1;
          if (newIdx >= comboProfile->count())
            newIdx = idx - 1;
          comboProfile->setCurrentItem(newIdx);
          TQString currentProfile = comboProfile->currentText();
          slotNewProfileSelected(currentProfile);
          if (profileName == defaultProfile())
          {
              KMessageBox::information(this, i18n("<qt>You have removed your default profile.<br>The new default profile will be <b>%1</b>.</qt>").arg(currentProfile), i18n("Profile Removal"));
              m_profilesNode.toElement().setAttribute("defaultProfile", currentProfile);
          }
          comboProfile->removeItem(idx);
          m_project->setModified(true);
       }
   }
  buttonRemoveProfile->setEnabled(comboProfile->count() > 1);
}

void ProjectUpload::fillProfileDlg(UploadProfileDlgS *profileDlg)
{
  profileDlg->lineProfileName->setText(m_currentProfileElement.attribute("name",""));
  profileDlg->lineHost->setText(m_currentProfileElement.attribute("remote_host",""));
  profileDlg->lineUser->setText(m_currentProfileElement.attribute("user",""));
  profileDlg->linePath->setText(m_currentProfileElement.attribute("remote_path",""));
  profileDlg->port->setText( m_currentProfileElement.attribute("remote_port","") );
  TQString def_p = m_currentProfileElement.attribute("remote_protocol","ftp");

  TQStringList protocols = KProtocolInfo::protocols();
  protocols.sort();
  for ( uint i=0; i<protocols.count(); i++ )
  {
    TQString protocol = protocols[i];
    KURL p;
    p.setProtocol(protocol);
    if ( KProtocolInfo::supportsWriting(p) &&
         KProtocolInfo::supportsMakeDir(p) &&
         KProtocolInfo::supportsDeleting(p) )
    {
      profileDlg->comboProtocol->insertItem(protocol);
      if ( protocol == def_p )
        profileDlg->comboProtocol->setCurrentItem(profileDlg->comboProtocol->count()-1 );
    }
  }
  TQString entry = profileDlg->comboProtocol->currentText() + "://" + profileDlg->lineUser->text() + "@" + profileDlg->lineHost->text();
  if (m_project->keepPasswd || m_project->passwordSaved(entry))
  {
    profileDlg->linePasswd->insert(m_project->password(entry));
    profileDlg->keepPasswd->setChecked(m_project->passwordSaved(entry));
 } else
  {
    profileDlg->linePasswd->clear();
    profileDlg->keepPasswd->setChecked(false);
  }
  if (m_profilesNode.toElement().attribute("defaultProfile") ==  profileDlg->lineProfileName->text())
    profileDlg->defaultProfile->setChecked(true);
}

void ProjectUpload::readProfileDlg(UploadProfileDlgS *profileDlg)
{
  TQString path = profileDlg->linePath->text();
  if (path.startsWith("~/"))
  {
    KUser user;
    path = user.homeDir() + path.mid(1);
  }
  m_currentProfileElement.setAttribute("name", profileDlg->lineProfileName->text());
  m_currentProfileElement.setAttribute("remote_host", profileDlg->lineHost->text());
  m_currentProfileElement.setAttribute("user", profileDlg->lineUser->text());
  m_currentProfileElement.setAttribute("remote_path", path);
  m_currentProfileElement.setAttribute("remote_port", profileDlg->port->text());
  m_currentProfileElement.setAttribute("remote_protocol", profileDlg->comboProtocol->currentText());
  TQString passwd = TQString(profileDlg->linePasswd->password());
  m_project->savePassword(profileDlg->comboProtocol->currentText() + "://" + profileDlg->lineUser->text() + "@" + profileDlg->lineHost->text(), passwd,  profileDlg->keepPasswd->isChecked());
  m_lastEditedProfileElement = m_currentProfileElement;
  m_lastPassword = passwd;
  if (profileDlg->defaultProfile->isChecked())
    m_profilesNode.toElement().setAttribute("defaultProfile", profileDlg->lineProfileName->text());
}

void ProjectUpload::slotNewProfileSelected(const TQString& profileName)
{
  TQDomNodeList profileList = m_profilesNode.toElement().elementsByTagName("profile");
  TQDomElement e;
  TQString s;
  for (uint i = 0; i < profileList.count(); i++)
  {
      e = profileList.item(i).toElement();
      s = e.attribute("name");
      if (s == profileName)
      {
        m_currentProfileElement = e;
        break;
      }
  }
  m_project->setModified(true);
  setProfileTooltip();
  if (!m_profilesOnly)
  {
    list->clear();
    slotBuildTree();
  }
}

TQString ProjectUpload::defaultProfile()
{
  return m_profilesNode.toElement().attribute("defaultProfile");
}

void ProjectUpload::reject()
{
  if (uploadInProgress && !m_profilesOnly)
  {
    suspendUpload = true;
    //TODO when message freeze if lift: i18n("Upload") -> i18n("Continue") or "Continue upload"
    if (KMessageBox::questionYesNo(this,i18n("Do you really want to abort the upload?"),
                                   i18n("Abort Upload"), i18n("Abort the uploading", "Abort"), i18n("Upload")) == KMessageBox::No)
    {
      suspendUpload = false;
      emit uploadNext();
      return;
    }
  }

  TQDialog::reject();
}


void ProjectUpload::setProfileTooltip()
{
  TQString tip = m_currentProfileElement.attribute("remote_protocol","ftp") + "://";
  TQString user = m_currentProfileElement.attribute("user","");
  if (! user.isEmpty()) {
    tip += user + "@";
  }
  tip += m_currentProfileElement.attribute("remote_host","");
  TQString port = m_currentProfileElement.attribute("remote_port","");
  if (! port.isEmpty()) {
    tip += ":" + port;
  }
  tip += m_currentProfileElement.attribute("remote_path","");

  TQToolTip::add(comboProfile, tip);
}

void ProjectUpload::loadRemoteUploadInfo()
{
    TQDomNodeList nl = m_currentProfileElement.elementsByTagName("uploadeditem");
    for (uint i = 0; i < nl.count(); i++)
    {
      TQDomElement el = nl.item(i).toElement();
      m_uploadTimeList[el.attribute("url")] = el.attribute("upload_time").toInt();
    }
}

void ProjectUpload::saveRemoteUploadInfo()
{
    TQDomNode parent = m_currentProfileElement.parentNode();
    TQDomNode profileNode = m_currentProfileElement.cloneNode(false);
    parent.removeChild(m_currentProfileElement);
    parent.appendChild(profileNode);
    TQMap<TQString, int>::ConstIterator it;
    for (it = m_uploadTimeList.constBegin(); it != m_uploadTimeList.constEnd(); ++it)
    {
      TQDomElement el = m_uploadStatusDom.createElement("uploadeditem");
      el.setAttribute("url", it.key());
      el.setAttribute("upload_time", it.data());
      profileNode.appendChild(el);
    }
    m_project->setModified(true);
}


#include "projectupload.moc"