~vcs-imports/bibletime/trunk

« back to all changes in this revision

Viewing changes to bibletime/frontend/kio_ftptransport.cpp

  • Committer: mgruner
  • Date: 2007-05-08 15:51:07 UTC
  • Revision ID: vcs-imports@canonical.com-20070508155107-0rj7jdmm5ivf8685
-imported source and data files to new svn module
-this is where KDE4-based development will take place

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*********
 
2
*
 
3
* This file is part of BibleTime's source code, http://www.bibletime.info/.
 
4
*
 
5
* Copyright 1999-2006 by the BibleTime developers.
 
6
* The BibleTime source code is licensed under the GNU General Public License version 2.0.
 
7
*
 
8
**********/
 
9
 
 
10
 
 
11
 
 
12
//BibleTime includes
 
13
#include "kio_ftptransport.h"
 
14
 
 
15
//KDE includes
 
16
#include <kapplication.h>
 
17
#include <kdirlister.h>
 
18
#include <kfileitem.h>
 
19
#include <kio/jobclasses.h>
 
20
#include <kio/job.h>
 
21
 
 
22
namespace BookshelfManager {
 
23
        bool finishedDownload = false;
 
24
 
 
25
        KIO_FTPTransport::KIO_FTPTransport(const char *host, sword::StatusReporter *statusReporter )
 
26
: QObject(),
 
27
        sword::FTPTransport(host, statusReporter),
 
28
        m_totalSize(-1) {}
 
29
 
 
30
 
 
31
        KIO_FTPTransport::~KIO_FTPTransport() {}
 
32
 
 
33
        char KIO_FTPTransport::getURL(const char *destPath, const char *sourceURL) {
 
34
                qWarning("FTP: Copy %s -> %s", sourceURL, destPath);
 
35
 
 
36
                KIO::file_delete(
 
37
                        KURL(QString::fromLocal8Bit(destPath)),
 
38
                        false
 
39
                );
 
40
 
 
41
                KIO::Job* job = KIO::copy(
 
42
                                                        KURL(QString::fromLocal8Bit(sourceURL)),
 
43
                                                        KURL(QString::fromLocal8Bit(destPath)),
 
44
                                                        false
 
45
                                                );
 
46
 
 
47
                //make sure to wait as long as the job is working
 
48
                finishedDownload = false;
 
49
                const int progressID = job->progressId();
 
50
                connect(
 
51
                        job, SIGNAL(result(KIO::Job*)),
 
52
                        this, SLOT(slotCopyResult(KIO::Job*))
 
53
                );
 
54
                connect(
 
55
                        job, SIGNAL(totalSize(KIO::Job*, KIO::filesize_t)),
 
56
                        this, SLOT(slotTotalSize(KIO::Job*, KIO::filesize_t))
 
57
                );
 
58
                connect(
 
59
                        job, SIGNAL(processedSize(KIO::Job*, KIO::filesize_t)),
 
60
                        this, SLOT(slotCopyProgress(KIO::Job*, KIO::filesize_t))
 
61
                );
 
62
 
 
63
                while (!finishedDownload) {
 
64
                        KApplication::kApplication()->processEvents(1);
 
65
                        //   qWarning("FTP: Copy not yet finished");
 
66
                        if (term) {
 
67
                                if (job) {
 
68
                                        job->kill(false); //kill emits the result signal
 
69
                                }
 
70
                        }
 
71
                }
 
72
 
 
73
                statusReporter->statusUpdate(m_totalSize, m_totalSize); //completed
 
74
 
 
75
                if (!m_copyResults.contains(progressID)) {
 
76
                        return 1; //Error
 
77
                }
 
78
                else if (m_copyResults[progressID] > 0) { //an error occurred
 
79
                        return 1; //an error occured
 
80
                }
 
81
                return 0;
 
82
        }
 
83
 
 
84
        void KIO_FTPTransport::slotTotalSize(KIO::Job* /*job*/, KIO::filesize_t size) {
 
85
                if (size > 0) {
 
86
                        m_totalSize = size;
 
87
                        statusReporter->statusUpdate(m_totalSize, 0); //emit that we just started
 
88
                }
 
89
        }
 
90
 
 
91
        void KIO_FTPTransport::slotCopyResult(KIO::Job *job) {
 
92
                m_copyResults.insert(job->progressId(),job->error());
 
93
                finishedDownload = true;
 
94
 
 
95
                if ( job->error() ) {}
 
96
        }
 
97
 
 
98
        void KIO_FTPTransport::slotCopyProgress(KIO::Job* /*job*/, KIO::filesize_t processedSize) {
 
99
                if (m_totalSize > 0) {
 
100
                        statusReporter->statusUpdate(m_totalSize, processedSize);
 
101
                }
 
102
        }
 
103
 
 
104
        void KIO_FTPTransport::slotDirListingCanceled() {
 
105
                m_listingCancelled = true;
 
106
        }
 
107
 
 
108
        std::vector<struct ftpparse> KIO_FTPTransport::getDirList(const char *dirURL) {
 
109
                std::vector< struct ftpparse > ret;
 
110
 
 
111
                //   char* dirURL = const_cast<char*>(myDir);
 
112
                //  if (dirURL[strlen(dirURL)-1] == '/') {
 
113
                //   qWarning("setting end to 0");
 
114
                //   dirURL[strlen(dirURL)-1] = 0;
 
115
                //  }
 
116
 
 
117
                qWarning("listing %s", dirURL);
 
118
 
 
119
                Q_ASSERT(!term);
 
120
                if (term) {
 
121
                        return ret;
 
122
                }
 
123
 
 
124
                m_listingCancelled = false;
 
125
                KDirLister lister;
 
126
                connect(&lister, SIGNAL(canceled()), SLOT(slotDirListingCanceled()));
 
127
                lister.openURL(KURL(dirURL));
 
128
 
 
129
                while (!lister.isFinished() && !m_listingCancelled) {
 
130
                        if (term) {
 
131
                                lister.stop();
 
132
                                break;
 
133
                        }
 
134
 
 
135
                        KApplication::kApplication()->processEvents(1);
 
136
                }
 
137
 
 
138
 
 
139
                if (term) {
 
140
                        return ret;
 
141
                }
 
142
 
 
143
                KFileItemList items = lister.itemsForDir(KURL(dirURL));
 
144
                KFileItem* i = 0;
 
145
                for ( i = items.first(); i; i = items.next() ) {
 
146
                        int length = i->name().length();
 
147
                        const char* t = i->name().latin1();
 
148
 
 
149
                        struct ftpparse s;
 
150
                        s.name = new char[length+1];//i->name().latin1();
 
151
                        bzero(s.name, length+1);
 
152
                        strcpy(s.name, t);
 
153
                        s.namelen = length+1;
 
154
 
 
155
                        s.size = i->size();
 
156
                        s.flagtrycwd = i->isDir(); //== 1 means a dir
 
157
 
 
158
                        s.id = 0;
 
159
                        s.idlen = 0;
 
160
 
 
161
                        //   qWarning("push_back item");
 
162
                        ret.push_back(s);
 
163
                }
 
164
 
 
165
                return ret;
 
166
        }
 
167
 
 
168
};
 
169