summaryrefslogtreecommitdiffstats
path: root/konq-plugins/rsync/rsyncplugin.cpp
blob: 434ceab6d65be2c9bacfe3e635a8838c2722b329 (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
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
/*
   Copyright (C) 2000, 2001, 2002 Dawit Alemayehu <adawit@kde.org>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License version 2 as published by the Free Software Foundation.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/
#include <unistd.h>
#include <sys/types.h>
#include <signal.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <errno.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <fcntl.h>

#define HAVE_TERMIOS_H 1
#define HAVE_GRANTPT 1

#include <stdlib.h>
#ifdef HAVE_PTY_H
#include <pty.h>
#endif
#ifdef HAVE_TERMIOS_H
#include <termios.h>
#endif
#ifdef HAVE_STROPTS
#include <stropts.h>
#endif
#ifdef HAVE_SYS_IOCTL_H
#include <sys/ioctl.h>
#endif
#ifdef HAVE_LIBUTIL_H
#include <libutil.h>
#endif
#ifdef HAVE_UTIL_H
#include <util.h>
#endif

#include <qfile.h>
#include <qtimer.h>
#include <qapplication.h>
#include <qlabel.h>
#include <qpushbutton.h>
#include <qhbox.h>
#include <qwhatsthis.h>
#include <qiconview.h>
#include <qpainter.h>
#include <qpixmap.h>
#include <qlabel.h>
#include <qlayout.h>
#include <qpushbutton.h>
#include <qstring.h>
#include <qregexp.h>
#include <qstyle.h>
#include <qtimer.h>

#include <kdebug.h>
#include <klocale.h>
#include <kinstance.h>

#include <kwin.h>
#include <kaction.h>
#include <kpopupmenu.h>
#include <kmessagebox.h>
#include <kiconloader.h>
#include <kprogressbox.h>
#include <kpassdlg.h>
#include <klistview.h>
#include <kapplication.h>
#include <kconfigdialog.h>

#include <kdirlister.h>
#include <kstandarddirs.h>
#include <klistviewsearchline.h>
#include <kiconviewsearchline.h>
#include <konq_dirpart.h>
#include <konq_propsview.h>
#include <kstaticdeleter.h>
#include <kgenericfactory.h>
#include <kparts/browserextension.h>

#include <kio/global.h>
#include <kio/slavebase.h>
#include <kio/authinfo.h>

#include "rsyncplugin.h"
#include "rsyncconfigdialog.h"

#define myDebug(x) kdDebug(7127) << __LINE__ << ": " x
// #define myDebug(x) cout << __LINE__ << ": " x
#define infoMessage(x) printf("INFO: %s\n\r", x);

static int open_pty_pair(int fd[2])
{
#if defined(HAVE_TERMIOS_H) && defined(HAVE_GRANTPT) && !defined(HAVE_OPENPTY)
/** with kind regards to The GNU C Library
Reference Manual for Version 2.2.x of the GNU C Library */
    int master, slave;
    char *name;
    struct ::termios ti;
    memset(&ti,0,sizeof(ti));

    ti.c_cflag = CLOCAL|CREAD|CS8;
    ti.c_cc[VMIN] = 1;

#ifdef HAVE_GETPT
    master = getpt();
#else
    master = open("/dev/ptmx", O_RDWR);
#endif
    if (master < 0) return 0;

    if (grantpt(master) < 0 || unlockpt(master) < 0) goto close_master;

    name = ptsname(master);
    if (name == NULL) goto close_master;

    slave = open(name, O_RDWR);
    if (slave == -1) goto close_master;

#if (defined(HAVE_ISASTREAM) || defined(isastream)) && defined(I_PUSH)
    if (isastream(slave) &&
        (ioctl(slave, I_PUSH, "ptem") < 0 ||
         ioctl(slave, I_PUSH, "ldterm") < 0))
            goto close_slave;
#endif

    tcsetattr(slave, TCSANOW, &ti);
    fd[0] = master;
    fd[1] = slave;
    return 0;

#if (defined(HAVE_ISASTREAM) || defined(isastream)) && defined(I_PUSH)
close_slave:
#endif
    close(slave);

close_master:
    close(master);
    return -1;
#else
#ifdef HAVE_OPENPTY
    struct ::termios ti;
    memset(&ti,0,sizeof(ti));

    ti.c_cflag = CLOCAL|CREAD|CS8;
    ti.c_cc[VMIN] = 1;

    return openpty(fd,fd+1,NULL,&ti,NULL);
#else
#ifdef __GNUC__
#warning "No tty support available. Password dialog won't work."
#endif
    return socketpair(PF_UNIX,SOCK_STREAM,0,fd);
#endif
#endif
}
/**
creates the unidirectional sync subprocess
*/
bool RsyncPlugin::syncUnidirectional(QString synccommand, QString syncflags, int parameter_order, QString localfolder, QString remotepath) {
    int fd[2];
    int rc, flags;
    thisFn = QString::null;

    rc = open_pty_pair(fd);
    if (rc == -1) {
        myDebug( << "socketpair failed, error: " << strerror(errno) << endl);
        return true;
    }

    childPid = fork();
    if (childPid == -1) {
        myDebug( << "fork failed, error: " << strerror(errno) << endl);
        close(fd[0]);
        close(fd[1]);
        childPid = 0;
        return true;
    }
    if (childPid == 0) {
        // Create the rsync command to run
        QString execstring;
        if (parameter_order == 0) {
            execstring = synccommand + syncflags + localfolder + QString("/ ") + remotepath;
        }
        else {
            execstring = synccommand + syncflags + remotepath + QString("/ ") + localfolder;
        }

        // taken from konsole, see TEPty.C for details
        // note: if we're running on socket pairs,
        // this will fail, but thats what we expect

        for (int sig = 1; sig < NSIG; sig++) signal(sig,SIG_DFL);

        struct rlimit rlp;
        getrlimit(RLIMIT_NOFILE, &rlp);
        for (int i = 0; i < (int)rlp.rlim_cur; i++)
            if (i != fd[1]) close(i);

        dup2(fd[1],0);
        dup2(fd[1],1);
        dup2(fd[1],2);
        if (fd[1] > 2) close(fd[1]);

        setsid();

#if defined(TIOCSCTTY)
        ioctl(0, TIOCSCTTY, 0);
#endif

        int pgrp = getpid();
#if defined( _AIX) || defined( __hpux)
        tcsetpgrp(0, pgrp);
#else
        ioctl(0, TIOCSPGRP, (char *)&pgrp);
#endif

        const char *dev = ttyname(0);
        setpgid(0,0);
        if (dev) close(open(dev, O_WRONLY, 0));
        setpgid(0,0);

        system(execstring.ascii());
        #undef common_args
        myDebug( << "could not exec! " << strerror(errno) << endl);
        ::exit(-1);
    }
    close(fd[1]);
    rc = fcntl(fd[0],F_GETFL,&flags);
    rc = fcntl(fd[0],F_SETFL,flags|O_NONBLOCK);
    childFd = fd[0];

    fd_set rfds, wfds;
    FD_ZERO(&rfds);
    FD_ZERO(&wfds);
    char buf[32768];
    int offset = 0;
    while (!isLoggedIn) {
        FD_SET(childFd,&rfds);
        FD_ZERO(&wfds);
        if (outBufPos >= 0) FD_SET(childFd,&wfds);
        struct timeval timeout;
        timeout.tv_sec = 0;
        timeout.tv_usec = 1000;
        rc = select(childFd+1, &rfds, &wfds, NULL, &timeout);
        if (rc < 0) {
            if (errno == EINTR)
                continue;
            myDebug( << "select failed, rc: " << rc << ", error: " << strerror(errno) << endl);
            return true;
        }
        if (FD_ISSET(childFd,&wfds) && outBufPos >= 0) {
            if (outBuf) {
                rc = write(childFd,outBuf+outBufPos,outBufLen-outBufPos);
                fflush(stdout);
            }
            else {
                rc = 0;
            }
            if (rc >= 0) outBufPos += rc;
            else {
                if (errno == EINTR)
                    continue;
                myDebug( << "write failed, rc: " << rc << ", error: " << strerror(errno) << endl);
                outBufPos = -1;
                //return true;
            }
            if (outBufPos >= outBufLen) {
                outBufPos = -1;
                outBuf = NULL;
                outBufLen = 0;
            }
        }
        if (FD_ISSET(childFd,&rfds)) {
            rc = read(childFd,buf+offset,32768-offset);
            if (rc > 0) {
                int noff = establishConnectionRsync(buf,rc+offset);
                if (noff < 0) return false;
                if (noff > 0) memmove(buf,buf+offset+rc-noff,noff);
                offset = noff;
            } else {
                if (errno == EINTR)
                    continue;
                //if (errno == EAGAIN)
                //    continue;
                myDebug( << "read failed, rc: " << rc << ", error: " << strerror(errno) << endl);
                return true;
            }
        }
    }
    return false;
}

/**
creates the bidirectional sync subprocess
*/
bool RsyncPlugin::syncBidirectional(QString synccommand, QString syncflags, int parameter_order, QString localfolder, QString remotepath) {
    int fd[2];
    int rc, flags;
    thisFn = QString::null;

    // Check for and remove the trailing slash in localfolder
    if (localfolder.endsWith("/")) {
        localfolder.remove(localfolder.length()-1, 1);
    }

    rc = open_pty_pair(fd);
    if (rc == -1) {
        myDebug( << "socketpair failed, error: " << strerror(errno) << endl);
        return true;
    }

    childPid = fork();
    if (childPid == -1) {
        myDebug( << "fork failed, error: " << strerror(errno) << endl);
        close(fd[0]);
        close(fd[1]);
        childPid = 0;
        return true;
    }
    if (childPid == 0) {
        // Create the rsync command to run
        QString execstring;
        execstring = synccommand + syncflags + localfolder + QString(" ") + remotepath;

        // taken from konsole, see TEPty.C for details
        // note: if we're running on socket pairs,
        // this will fail, but thats what we expect

        for (int sig = 1; sig < NSIG; sig++) signal(sig,SIG_DFL);

        struct rlimit rlp;
        getrlimit(RLIMIT_NOFILE, &rlp);
        for (int i = 0; i < (int)rlp.rlim_cur; i++)
            if (i != fd[1]) close(i);

        dup2(fd[1],0);
        dup2(fd[1],1);
        dup2(fd[1],2);
        if (fd[1] > 2) close(fd[1]);

        setsid();

#if defined(TIOCSCTTY)
        ioctl(0, TIOCSCTTY, 0);
#endif

        int pgrp = getpid();
#if defined( _AIX) || defined( __hpux)
        tcsetpgrp(0, pgrp);
#else
        ioctl(0, TIOCSPGRP, (char *)&pgrp);
#endif

        const char *dev = ttyname(0);
        setpgid(0,0);
        if (dev) close(open(dev, O_WRONLY, 0));
        setpgid(0,0);

        system(execstring.ascii());
        #undef common_args
        myDebug( << "could not exec! " << strerror(errno) << endl);
        ::exit(-1);
    }
    close(fd[1]);
    rc = fcntl(fd[0],F_GETFL,&flags);
    rc = fcntl(fd[0],F_SETFL,flags|O_NONBLOCK);
    childFd = fd[0];

    fd_set rfds, wfds;
    FD_ZERO(&rfds);
    FD_ZERO(&wfds);
    char buf[32768];
    int offset = 0;
    while (!isLoggedIn) {
        FD_SET(childFd,&rfds);
        FD_ZERO(&wfds);
        if (outBufPos >= 0) FD_SET(childFd,&wfds);
        struct timeval timeout;
        timeout.tv_sec = 0;
        timeout.tv_usec = 1000;
        rc = select(childFd+1, &rfds, &wfds, NULL, &timeout);
        if (rc < 0) {
            if (errno == EINTR)
                continue;
            myDebug( << "select failed, rc: " << rc << ", error: " << strerror(errno) << endl);
            return true;
        }
        if (FD_ISSET(childFd,&wfds) && outBufPos >= 0) {
            if (outBuf) {
                rc = write(childFd,outBuf+outBufPos,outBufLen-outBufPos);
                fflush(stdout);
            }
            else {
                rc = 0;
            }
            if (rc >= 0) outBufPos += rc;
            else {
                if (errno == EINTR)
                    continue;
                myDebug( << "write failed, rc: " << rc << ", error: " << strerror(errno) << endl);
                outBufPos = -1;
                //return true;
            }
            if (outBufPos >= outBufLen) {
                outBufPos = -1;
                outBuf = NULL;
                outBufLen = 0;
            }
        }
        if (FD_ISSET(childFd,&rfds)) {
            rc = read(childFd,buf+offset,32768-offset);
            if (rc > 0) {
                int noff = establishConnectionUnison(buf,rc+offset, localfolder, remotepath);
                if (noff < 0) return false;
                if (noff > 0) memmove(buf,buf+offset+rc-noff,noff);
                offset = noff;
            } else {
                if (errno == EINTR)
                    continue;
                //if (errno == EAGAIN)
                //    continue;
                myDebug( << "read failed, rc: " << rc << ", error: " << strerror(errno) << endl);
                return true;
            }
        }
    }
    return false;
}

/**
writes one chunk of data to stdin of child process
*/
void RsyncPlugin::writeChild(const char *buf, KIO::fileoffset_t len) {
    if (outBufPos >= 0 && outBuf) {
#if 0
        QString debug;
        debug.setLatin1(outBuf,outBufLen);
        if (len > 0) myDebug( << "write request while old one is pending, throwing away input (" << outBufLen << "," << outBufPos << "," << debug.left(10) << "...)" << endl);
#endif
        return;
    }
    outBuf = buf;
    outBufPos = 0;
    outBufLen = len;
}

/**
manages initial communication setup including password queries
*/
int RsyncPlugin::establishConnectionRsync(char *buffer, KIO::fileoffset_t len) {
    QString buf;
    buf.setLatin1(buffer,len);
    int pos;
    // Strip trailing whitespace
    while (buf.length() && (buf[buf.length()-1] == ' '))
       buf.truncate(buf.length()-1);

    myDebug( << "establishing: got " << buf << endl);
    while (childPid && ((pos = buf.find('\n')) >= 0 || buf.endsWith(":") || buf.endsWith("?"))) {
        if (m_progressDialogExists == true) {
            qApp->processEvents();
        }
        pos++;
        QString str = buf.left(pos);
        buf = buf.mid(pos);
        if (str == "\n")
            continue;
        //if (str.contains("rsync error:")) {
        if (str.contains("rsync:") || str.contains("failed.") || (str.contains("Could not") && str.endsWith("."))) {
            KMessageBox::error(NULL, str);
        }
        else if (!str.isEmpty()) {
            thisFn += str;
            if ((buf.endsWith(":") == false) && (buf.endsWith("?") == false)) {
                // Display a nice little progress bar with text box
                if (m_progressDialogExists == false) {
                    m_progressDialog = new KProgressBoxDialog(0, "rsyncProgress", i18n("Synchronizing Folder..."), i18n("Synchronizing Folder..."), true);
                    m_progressDialog->progressBar()->setFormat("%v / %m");
                    m_progressDialog->setAutoClose(true);
                    m_progressDialog->progressBar()->setTotalSteps(2);
                    m_progressDialog->progressBar()->setValue(1);
                    connect (m_progressDialog, SIGNAL(cancelClicked()), SLOT(slotRsyncCancelled()));
                    m_progressDialog->show();
                    m_progressDialogExists = true;
                }
            }
        }
        else if (buf.endsWith(":")) {
            if (!redirectUser.isEmpty() && connectionUser != redirectUser) {
               // FIXME: Possibly do something here; is this the success response?
                return -1;
            } else if (!connectionPassword.isEmpty()) {
                myDebug( << "sending cpass" << endl);
                connectionAuth.password = connectionPassword+"\n";
                connectionPassword = QString::null;
                writeChild(connectionAuth.password.latin1(),connectionAuth.password.length());
            } else {
                myDebug( << "sending mpass" << endl);
                connectionAuth.prompt = thisFn+buf;
                connectionAuth.password = QString::null; // don't prefill
                QCString thispass;
                if (KPasswordDialog::getPassword (thispass, i18n("Remote authorization required") + QString("\n") + i18n("Please input") + QString(" ") + QString(buf), NULL) != 1) {
                    shutdownConnection(true, false);
                    return -1;
                }
                else {
                    connectionAuth.password = QString(thispass);
                }
                connectionAuth.password += "\n";
                myDebug( << "sending pass" << endl);
                writeChild(connectionAuth.password.latin1(),connectionAuth.password.length());
            }
            thisFn = QString::null;
            return 0;
        }
        else if (buf.endsWith("?")) {
            int rc = KMessageBox::questionYesNo(NULL, thisFn+buf);
            if (rc == KMessageBox::Yes) {
                writeChild("yes\n",4);
            } else {
                writeChild("no\n",3);
            }
            thisFn = QString::null;
            return 0;
        }

        if (m_progressDialogExists == true) {
            if (str.contains("exit()") && str.contains("ICE default IO")) {
                if (m_progressDialogExists == true) {
                    m_progressDialog->progressBar()->setValue(m_progressDialog->progressBar()->totalSteps());
                }
            }
            else {
                if (str.contains(", to-check=")) {
                    // Parse the to-check output
                    QString tocheck_out_cur;
                    QString tocheck_out_tot;
                    tocheck_out_cur = str.mid(str.find(", to-check=") + 11, str.length());
                    tocheck_out_tot = tocheck_out_cur.mid(tocheck_out_cur.find("/") + 1, tocheck_out_cur.length());
                    tocheck_out_cur = tocheck_out_cur.left(tocheck_out_cur.find("/"));
                    tocheck_out_tot = tocheck_out_tot.left(tocheck_out_tot.find(")"));
                    m_progressDialog->progressBar()->setTotalSteps(tocheck_out_tot.toInt()-1);
                    m_progressDialog->progressBar()->setValue(tocheck_out_tot.toInt()-tocheck_out_cur.toInt()-2);
                }
                else {
                    m_progressDialog->textEdit()->append(str);
                    m_progressDialog->textEdit()->scrollToBottom();
                }
            }
        }
    }
    return buf.length();
}

/**
manages initial communication setup including password queries
*/
int RsyncPlugin::establishConnectionUnison(char *buffer, KIO::fileoffset_t len, QString localfolder, QString remotepath) {
    QString buf;
    buf.setLatin1(buffer,len);
    int pos;
    // Strip trailing whitespace
    while (buf.length() && (buf[buf.length()-1] == ' '))
       buf.truncate(buf.length()-1);

    myDebug( << "establishing: got " << buf << endl);
    while (childPid && (((pos = buf.find('\n')) >= 0) || buf.endsWith(":") || buf.endsWith("?") || buf.endsWith("]"))) {
        if (m_progressDialogExists == true) {
            qApp->processEvents();
        }
        pos++;
        QString str = buf.left(pos);
        buf = buf.mid(pos);
        if (str == "\n")
            continue;
        //if (str.contains("rsync error:")) {
        if (str.contains("rsync:") || str.contains("failed.") || (str.contains("Could not") && str.endsWith("."))) {
            KMessageBox::error(NULL, str);
        }
        else if (!str.isEmpty()) {
            thisFn += str;
            if ((buf.endsWith(":") == false) && (buf.endsWith("?") == false)) {
                // Display a nice little progress bar with text box
                if (m_progressDialogExists == false) {
                    m_progressDialog = new KProgressBoxDialog(0, "rsyncProgress", i18n("Synchronizing Folder..."), i18n("Synchronizing Folder..."), true);
                    m_progressDialog->progressBar()->setFormat("%v / %m");
                    m_progressDialog->progressBar()->setTotalSteps(0);
                    m_progressDialog->setAutoClose(true);
                    connect (m_progressDialog, SIGNAL(cancelClicked()), SLOT(slotUnisonCancelled()));
                    m_progressDialog->show();
                    m_progressDialogExists = true;
                }
            }
        }
        else if (buf.endsWith(":")) {
            if (!redirectUser.isEmpty() && connectionUser != redirectUser) {
               // FIXME: Possibly do something here; is this the success response?
                return -1;
            } else if (!connectionPassword.isEmpty()) {
                myDebug( << "sending cpass" << endl);
                connectionAuth.password = connectionPassword+"\n";
                connectionPassword = QString::null;
                writeChild(connectionAuth.password.latin1(),connectionAuth.password.length());
            } else {
                myDebug( << "sending mpass" << endl);
                connectionAuth.prompt = thisFn+buf;
                connectionAuth.password = QString::null; // don't prefill
                QCString thispass;
                if (KPasswordDialog::getPassword (thispass, i18n("Remote authorization required") + QString("\n") + i18n("Please input") + QString(" ") + QString(buf), NULL) != 1) {
                    slotUnisonCancelled();
                    return -1;
                }
                else {
                    connectionAuth.password = QString(thispass);
                }
                connectionAuth.password += "\n";
                myDebug( << "sending pass" << endl);
                writeChild(connectionAuth.password.latin1(),connectionAuth.password.length());
            }
            thisFn = QString::null;
            return 0;
        }
        else if (buf.endsWith("?") || buf.endsWith("? []")) {
            buf.replace("[]", "");
            if (buf.endsWith("? []")) {
                int rc = KMessageBox::questionYesNo(NULL, buf);
                if (rc == KMessageBox::Yes) {
                    writeChild("y\n",3);
                } else {
                    writeChild("n\n",3);
                }
            }
            else {
                int rc = KMessageBox::questionYesNo(NULL, buf);
                if (rc == KMessageBox::Yes) {
                    writeChild("yes\n",4);
                } else {
                    writeChild("no\n",3);
                }
            }
            thisFn = QString::null;
            buf = "";
            return 0;
        }
        else if (buf.endsWith("]")) {
            if (m_progressDialogExists == true) {
                m_progressDialog->textEdit()->append(buf);
                m_progressDialog->textEdit()->scrollToBottom();
                int currentPos;
                currentPos = m_progressDialog->progressBar()->progress();
                m_progressDialog->progressBar()->setProgress(++currentPos);
            }
            QString file_name;
            file_name = buf;
            file_name.replace("[]", "");
            file_name.replace(QString("changed "), "");
            //file_name = file_name.simplifyWhiteSpace();
            KDialogBase *dialog= new KDialogBase(i18n("User Intervention Required"), KDialogBase::Yes | KDialogBase::No | KDialogBase::Cancel, KDialogBase::Yes, KDialogBase::Cancel, NULL, "warningYesNoCancel", true, true, i18n("Use &Local File"), i18n("Use &Remote File"), i18n("&Ignore"));
            int rc = KMessageBox::createKMessageBox(dialog, QMessageBox::Warning, QString("<b>") + i18n("WARNING: Both the local and remote file have been modified") + QString("</b><p>") + i18n("Local") + QString(": ") + localfolder + QString("/") + file_name + QString("<br>") + i18n("Remote") + QString(": ") + remotepath + QString("/") + file_name + QString("<p>") + i18n("Please select the file to duplicate (the other will be overwritten)") + QString("<br>") + i18n("Or, select Ignore to skip synchronization of this file for now"), QStringList(), QString::null, NULL, 1);
            if (rc == KDialogBase::Yes) {
                writeChild(">\n",3);
            }
            else if (rc == KDialogBase::No) {
                writeChild("<\n",3);
            }
            else {
                writeChild("/\n",3);
            }
            return 0;
        }

        if (m_progressDialogExists == true) {
            if (str.contains("exit()") && str.contains("ICE default IO")) {
                if (m_progressDialogExists == true) {
                    m_progressDialog->progressBar()->setFormat("%v / %m");
                    m_progressDialog->progressBar()->setTotalSteps(2);
                    m_progressDialog->progressBar()->setValue(m_progressDialog->progressBar()->totalSteps());
                }
            }
            else {
                m_progressDialog->textEdit()->append(str);
                m_progressDialog->textEdit()->scrollToBottom();
                int currentPos;
                currentPos = m_progressDialog->progressBar()->progress();
                m_progressDialog->progressBar()->setProgress(++currentPos);
            }
        }
    }
    return buf.length();
}

/**
Forced close of the connection

This function gets called from the application side of the universe,
it shouldn't send any response.
 */
void RsyncPlugin::closeConnection(){
    myDebug( << "closeConnection()" << endl);
    shutdownConnection(true, false);
}

/**
Closes the connection
 */
void RsyncPlugin::shutdownConnection(bool forced, bool wait){
    if (childPid) {
        kill(childPid,SIGTERM); // We may not have permission...
        childPid = 0;
        if (wait == false) {
            close(childFd); // ...in which case this should do the trick
            childFd = -1;
        }
    }
    outBufPos = -1;
    outBuf = NULL;
    outBufLen = 0;
    isLoggedIn = false;
}

// --------------------------------------------------------------------------------------------
//
// Here begins the standard load/save/search/Konqy stuff
//
// --------------------------------------------------------------------------------------------

void RsyncPlugin::saveSettings()
{
  KConfig cfg ("rsyncrc", false, false);
  cfg.setGroup ("General");

  QString longstring = QString("");
  for (QStringList::Iterator i(cfgfolderlist.begin()); i != cfgfolderlist.end();) {
	longstring = longstring + (*i);
	i++;
	longstring = longstring + QString(";");
  }

  cfg.writeEntry("LocalFolders", longstring);
  cfg.sync();
}

void RsyncPlugin::loadSettings()
{
  if (m_bSettingsLoaded)
    return;

  KConfig cfg ("rsyncrc", false, false);
  cfg.setGroup ("General");

  cfgfolderlist = cfg.readListEntry("LocalFolders", ';');

  m_bSettingsLoaded = true;
}

QString RsyncPlugin::findLocalFolderByName(QString folderurl)
{
  QString folderurl_stripped;
  folderurl_stripped = folderurl;
  folderurl_stripped.replace(QString("file://"), QString(""));
  for (QStringList::Iterator i(cfgfolderlist.begin()); i != cfgfolderlist.end(); ++i) {
	if (QString::compare((*i), folderurl_stripped) == 0) {
		i++;
		return (*i);
		i++;
		i++;
	}
  }
  return NULL;
}

QString RsyncPlugin::findSyncMethodByName(QString folderurl)
{
  QString folderurl_stripped;
  folderurl_stripped = folderurl;
  folderurl_stripped.replace(QString("file://"), QString(""));
  for (QStringList::Iterator i(cfgfolderlist.begin()); i != cfgfolderlist.end(); ++i) {
	if (QString::compare((*i), folderurl_stripped) == 0) {
		i++;
		i++;
		return (*i);
		i++;
	}
  }
  return NULL;
}

int RsyncPlugin::deleteLocalFolderByName(QString folderurl)
{
  QString folderurl_stripped;
  folderurl_stripped = folderurl;
  folderurl_stripped.replace(QString("file://"), QString(""));
  for (QStringList::Iterator i(cfgfolderlist.begin()); i != cfgfolderlist.end(); ++i) {
	if (QString::compare((*i), folderurl_stripped) == 0) {
		i=cfgfolderlist.remove(i);
		i=cfgfolderlist.remove(i);
		i=cfgfolderlist.remove(i);
		cfgfolderlist.remove(i);
		return 0;
	}
  }
  return 1;
}

int RsyncPlugin::addLocalFolderByName(QString folderurl, QString remoteurl, QString syncmethod, QString excludelist)
{
  QString folderurl_stripped;
  folderurl_stripped = folderurl;
  folderurl_stripped.replace(QString("file://"), QString(""));
  cfgfolderlist.append(folderurl);
  cfgfolderlist.append(remoteurl);
  cfgfolderlist.append(syncmethod);
  cfgfolderlist.append(excludelist);
  return 1;
}

RsyncPlugin::RsyncPlugin (QObject* parent, const char* name,
                                  const QStringList&)
                :KParts::Plugin (parent, name),
                 m_pSyncMenu(0)
{
  m_part = ::qt_cast<KonqDirPart*>(parent);

  if ( !m_part || !m_part->scrollWidget() )
    return;

  m_pSyncNow = new KAction(i18n("Synchronize F&older"), "syncnow",
                               actionCollection(), "syncnow");
  m_pSyncSetup = new KAction (i18n("Setup Syn&chronization"), "setupsync",
                               actionCollection(), "setupsync");
  m_pSyncNow->setIcon("remotesync");
  m_pSyncSetup->setIcon("remotesyncconfig");
  m_pSyncNow->setEnabled (false);

  connect (m_part, SIGNAL(aboutToOpenURL()), SLOT(slotOpenURL()));

  connect(m_pSyncNow, SIGNAL(activated()), this, SLOT(slotSync()));
  connect(m_pSyncSetup, SIGNAL(activated()), this, SLOT(slotSetup()));

  loadSettings();

    // Initialize the rsync backend variables
    childPid = 0;
    isLoggedIn = false;
    firstLogin = true;
    connectionAuth.keepPassword = true;
    outBufPos = -1;
    outBuf = NULL;
    outBufLen = 0;
    isStat = false; // FIXME: just a workaround for konq deficiencies
    redirectUser = ""; // FIXME: just a workaround for konq deficiencies
    redirectPass = ""; // FIXME: just a workaround for konq deficiencies
}

RsyncPlugin::~RsyncPlugin()
{
  delete m_pSyncMenu;
}

void RsyncPlugin::slotOpenURL ()
{
  KURL url = m_part->url();

  if (m_pURL != url)
  {
	// See if this URL is in the list of rsync-able directories
	if (findLocalFolderByName(url.directory(true, true) + QString("/") + url.fileName(true)) != NULL) {
		m_pSyncNow->setEnabled (true);
	}
	else {
		m_pSyncNow->setEnabled (false);
	}
  }
  m_pURL = url;
}

// void RsyncPlugin::slotShowPopup()
// {
//   if (!m_part)
//   {
//     m_pSyncMenu->setEnabled (false);
//     return;
//   }
//
//   int id = 0;
//   uint enableReset = 0;
//
//   QString label;
//   QStringList inodes;
//
//   m_pSyncMenu->popupMenu()->clear();
//   m_pSyncMenu->popupMenu()->insertTitle (i18n("Remote Folder Synchronization"));
//   id = m_pSyncMenu->popupMenu()->insertItem (i18n("Synchronize Now"), this, SLOT(slotSync()));
// }

void RsyncPlugin::slotSetup()
{
	KURL url = m_part->url();

	m_pSyncSetup->setEnabled (false);

	// Look up settings
	QString localfolder = url.directory(true, true) + QString("/") + url.fileName(true);
	QString remotefolder = findLocalFolderByName(url.directory(true, true) + QString("/") + url.fileName(true));
	QString syncmethod = findSyncMethodByName(url.directory(true, true) + QString("/") + url.fileName(true));
	int syncint;
	if (syncmethod == NULL) {
		syncint = 1;
	}
	else if (syncmethod == "rsync_upload") {
		syncint = 1;
	}
	else if (syncmethod == "rsync_download") {
		syncint = 2;
	}
	else if (syncmethod == "rsync_bidirectional") {
		syncint = 3;
	}

	m_configDialog = new RsyncConfigDialog(0, "rsyncConfig", i18n("Remote Folder Synchronization"), i18n("Configuring Remote Folder Synchronization"), localfolder, remotefolder, syncint, true);
	m_configDialog->show();

	connect (m_configDialog, SIGNAL(okClicked()), SLOT(slotSetupOK()));
	connect (m_configDialog, SIGNAL(cancelClicked()), SLOT(slotSetupCancelled()));
}

void RsyncPlugin::slotSetupOK()
{
	if (!m_part)
		return;

	KURL url = m_part->url();

	// Look up settings
	QString localfolder = url.directory(true, true) + QString("/") + url.fileName(true);
	QString remotefolder = findLocalFolderByName(localfolder);
	QString remotefolder_new = m_configDialog->lineEdit()->text().ascii();
	int syncmethod = m_configDialog->getSyncMode();
	QString syncmethod_new = "";
	if (syncmethod == 1) {
		syncmethod_new = "rsync_upload";
	}
	else if (syncmethod == 2) {
		syncmethod_new = "rsync_download";
	}
	else if (syncmethod == 3) {
		syncmethod_new = "rsync_bidirectional";
	}

	// See if an old entry has to be deleted
	if (remotefolder.isEmpty() == false) {
		deleteLocalFolderByName(localfolder);
	}
	if (remotefolder_new.isEmpty() == false) {
		addLocalFolderByName(localfolder, remotefolder_new, syncmethod_new, "");
	}
	saveSettings();

	if (remotefolder_new.isEmpty() == false) {
		m_pSyncNow->setEnabled (true);
	}
	else {
		m_pSyncNow->setEnabled (false);
	}
	m_pSyncSetup->setEnabled (true);
}

void RsyncPlugin::slotSetupCancelled()
{
	m_pSyncSetup->setEnabled (true);
}

void RsyncPlugin::slotRsyncCancelled()
{
        shutdownConnection(true, true);
        if (m_progressDialogExists == true) {
            m_progressDialog->progressBar()->setValue(m_progressDialog->progressBar()->totalSteps());
        }
	m_pSyncNow->setEnabled (true);
}

void RsyncPlugin::slotUnisonCancelled()
{
        shutdownConnection(true, true);
        if (m_progressDialogExists == true) {
            m_progressDialog->progressBar()->setFormat("%v / %m");
            m_progressDialog->progressBar()->setTotalSteps(2);
            m_progressDialog->progressBar()->setValue(m_progressDialog->progressBar()->totalSteps());
        }
	m_pSyncNow->setEnabled (true);
}

void RsyncPlugin::slotSync()
{
	if (!m_part)
		return;

	KURL url = m_part->url();

	m_pSyncNow->setEnabled (false);

	QString syncmethod = findSyncMethodByName(url.directory(true, true) + QString("/") + url.fileName(true));
	if (syncmethod == NULL) {
		// Do nothing
	}
	else if (syncmethod == "rsync_upload") {
		// Initiate rsync
		syncUnidirectional(QString("rsync"), QString(" -avtzAXE --delete --progress "), 0, url.directory(true, true) + QString("/") + url.fileName(true), findLocalFolderByName(url.directory(true, true) + QString("/") + url.fileName(true)));
	}
 	else if (syncmethod == "rsync_download") {
 		syncUnidirectional(QString("rsync"), QString(" -avtzAXE --delete --progress "), 1, url.directory(true, true) + QString("/") + url.fileName(true), findLocalFolderByName(url.directory(true, true) + QString("/") + url.fileName(true)));
 	}
 	else if (syncmethod == "rsync_bidirectional") {
 		syncBidirectional(QString("unison"), QString(" -ui text -auto "), 1, url.directory(true, true) + QString("/") + url.fileName(true), findLocalFolderByName(url.directory(true, true) + QString("/") + url.fileName(true)));
 	}

	m_progressDialogExists = false;
	m_pSyncNow->setEnabled (true);
}

typedef KGenericFactory<RsyncPlugin> RsyncFactory;
K_EXPORT_COMPONENT_FACTORY (librsyncplugin, RsyncFactory("rsyncplugin"))

#include "rsyncplugin.moc"