Diffstat (limited to 'noncore/apps/opie-console/filetransfer.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/apps/opie-console/filetransfer.cpp | 143 |
1 files changed, 123 insertions, 20 deletions
diff --git a/noncore/apps/opie-console/filetransfer.cpp b/noncore/apps/opie-console/filetransfer.cpp index 78982bd..7b75d35 100644 --- a/noncore/apps/opie-console/filetransfer.cpp +++ b/noncore/apps/opie-console/filetransfer.cpp | |||
@@ -11,12 +11,26 @@ | |||
11 | 11 | ||
12 | #include "filetransfer.h" | 12 | #include "filetransfer.h" |
13 | 13 | ||
14 | /** | ||
15 | * | ||
16 | * | ||
17 | class FileTransferControl { | ||
18 | public: | ||
19 | FileTransferControl(); | ||
20 | ~FileTransferControl(); | ||
21 | |||
22 | |||
23 | }; | ||
24 | */ | ||
25 | |||
14 | bool FileTransfer::terminate = false; | 26 | bool FileTransfer::terminate = false; |
15 | pid_t FileTransfer::m_pid; | 27 | pid_t FileTransfer::m_pid; |
16 | 28 | ||
17 | FileTransfer::FileTransfer( Type t, IOLayer* lay ) | 29 | FileTransfer::FileTransfer( Type t, IOLayer* lay ) |
18 | : FileTransferLayer( lay ), m_type( t ) { | 30 | : FileTransferLayer( lay ), m_type( t ) { |
31 | signal(SIGPIPE, SIG_IGN ); | ||
19 | signal( SIGCHLD, signal_handler ); | 32 | signal( SIGCHLD, signal_handler ); |
33 | m_not = 0l; | ||
20 | } | 34 | } |
21 | FileTransfer::~FileTransfer() { | 35 | FileTransfer::~FileTransfer() { |
22 | } | 36 | } |
@@ -30,21 +44,26 @@ FileTransfer::~FileTransfer() { | |||
30 | * to do direct IO from and to the fd | 44 | * to do direct IO from and to the fd |
31 | */ | 45 | */ |
32 | void FileTransfer::sendFile( const QString& file ) { | 46 | void FileTransfer::sendFile( const QString& file ) { |
47 | m_prog =-1; | ||
33 | m_fd = layer()->rawIO(); | 48 | m_fd = layer()->rawIO(); |
34 | // | 49 | // |
35 | // m_fd = ::open("/dev/ttyS0", O_RDWR); | 50 | // m_fd = ::open("/dev/ttyS0", O_RDWR); |
36 | 51 | ||
37 | 52 | m_file = file; | |
38 | if ( pipe( m_comm ) < 0 ) | 53 | if ( pipe( m_comm ) < 0 ) |
39 | m_comm[0] = m_comm[1] = 0; | 54 | m_comm[0] = m_comm[1] = 0; |
40 | if ( pipe( m_info ) < 0 ) | 55 | if ( pipe( m_info ) < 0 ) |
41 | m_info[0] = m_info[1] = 0; | 56 | m_info[0] = m_info[1] = 0; |
42 | 57 | ||
43 | qWarning("output:"+file ); | 58 | |
44 | m_pid = fork(); | 59 | m_pid = fork(); |
45 | switch( m_pid ) { | 60 | switch( m_pid ) { |
61 | case -1: | ||
62 | emit error( StartError, tr("Was not able to fork") ); | ||
63 | break; | ||
46 | case 0:{ | 64 | case 0:{ |
47 | setupChild(); | 65 | setupChild(); |
66 | qWarning("output:"+file ); | ||
48 | /* exec */ | 67 | /* exec */ |
49 | char* verbose = "-vv"; | 68 | char* verbose = "-vv"; |
50 | char* binray = "-b"; | 69 | char* binray = "-b"; |
@@ -67,8 +86,10 @@ void FileTransfer::sendFile( const QString& file ) { | |||
67 | char resultByte; int len; | 86 | char resultByte; int len; |
68 | len = read(m_info[0], &resultByte, 1 ); | 87 | len = read(m_info[0], &resultByte, 1 ); |
69 | /* len == 1 start up failed */ | 88 | /* len == 1 start up failed */ |
70 | if ( len == 1 ) | 89 | if ( len == 1 ) { |
90 | emit error( StartError, tr("Could not start") ); | ||
71 | return; | 91 | return; |
92 | } | ||
72 | if ( len == -1 ) | 93 | if ( len == -1 ) |
73 | if ( (errno == ECHILD ) || (errno == EINTR ) ) | 94 | if ( (errno == ECHILD ) || (errno == EINTR ) ) |
74 | continue; | 95 | continue; |
@@ -85,35 +106,35 @@ void FileTransfer::sendFile( const QString& file ) { | |||
85 | int sel; | 106 | int sel; |
86 | 107 | ||
87 | /* replace by QSocketNotifier!!! */ | 108 | /* replace by QSocketNotifier!!! */ |
88 | while (!terminate) { | 109 | m_not = new QSocketNotifier(m_comm[0], QSocketNotifier::Read ); |
89 | FD_ZERO( &fds ); | 110 | connect(m_not, SIGNAL(activated(int) ), |
90 | FD_SET( m_comm[0], &fds ); | 111 | this, SLOT(slotRead() ) ); |
91 | sel = select( m_comm[0]+1, &fds, NULL, NULL, &timeout ); | ||
92 | if (sel ) { | ||
93 | if ( FD_ISSET(m_comm[0], &fds ) ) { | ||
94 | printf("out:"); | ||
95 | QByteArray ar(4096); | ||
96 | int len = read(m_comm[0], ar.data(), 4096 ); | ||
97 | for (int i = 0; i < len; i++ ) { | ||
98 | // printf("%c", ar[i] ); | ||
99 | } | ||
100 | printf("\n"); | ||
101 | } | ||
102 | } | ||
103 | } | ||
104 | break; | ||
105 | } | 112 | } |
113 | break; | ||
106 | } | 114 | } |
107 | } | 115 | } |
116 | /* | ||
117 | * let's call the one with the filename | ||
118 | */ | ||
108 | void FileTransfer::sendFile( const QFile& file ) { | 119 | void FileTransfer::sendFile( const QFile& file ) { |
109 | sendFile( file.name() ); | 120 | sendFile( file.name() ); |
110 | } | 121 | } |
122 | /* | ||
123 | * our signal handler to be replaced by | ||
124 | * a procctl thingie | ||
125 | */ | ||
111 | void FileTransfer::signal_handler(int ) { | 126 | void FileTransfer::signal_handler(int ) { |
127 | qWarning("Terminated"); | ||
112 | int status; | 128 | int status; |
113 | signal( SIGCHLD, signal_handler ); | 129 | signal( SIGCHLD, signal_handler ); |
114 | waitpid( m_pid, &status, WNOHANG ); | 130 | waitpid( m_pid, &status, WNOHANG ); |
115 | terminate = true; | 131 | terminate = true; |
116 | } | 132 | } |
133 | |||
134 | /* | ||
135 | * setting up communication | ||
136 | * between parent child and ioLayer | ||
137 | */ | ||
117 | void FileTransfer::setupChild() { | 138 | void FileTransfer::setupChild() { |
118 | /* | 139 | /* |
119 | * we do not want to read from our | 140 | * we do not want to read from our |
@@ -140,3 +161,85 @@ void FileTransfer::setupChild() { | |||
140 | dup2( m_fd, STDOUT_FILENO ); | 161 | dup2( m_fd, STDOUT_FILENO ); |
141 | dup2( m_comm[1], STDERR_FILENO ); | 162 | dup2( m_comm[1], STDERR_FILENO ); |
142 | } | 163 | } |
164 | |||
165 | /* | ||
166 | * read from the stderr of the child | ||
167 | * process | ||
168 | */ | ||
169 | void FileTransfer::slotRead() { | ||
170 | QByteArray ar(4096); | ||
171 | int len = read(m_comm[0], ar.data(), 4096 ); | ||
172 | qWarning("slot read %d", len); | ||
173 | for (int i = 0; i < len; i++ ) { | ||
174 | // printf("%c", ar[i] ); | ||
175 | } | ||
176 | ar.resize( len ); | ||
177 | QString str( ar ); | ||
178 | QStringList lis = QStringList::split(' ', str ); | ||
179 | /* | ||
180 | * Transfer finished.. either complete or incomplete | ||
181 | */ | ||
182 | if ( lis[0].simplifyWhiteSpace() == "Transfer" ) { | ||
183 | qWarning("sent!!!!"); | ||
184 | emit sent(); | ||
185 | return; | ||
186 | } | ||
187 | /* | ||
188 | * do progress reading | ||
189 | */ | ||
190 | slotProgress( lis ); | ||
191 | |||
192 | |||
193 | } | ||
194 | /* | ||
195 | * find the progress | ||
196 | */ | ||
197 | void FileTransfer::slotProgress( const QStringList& list ) { | ||
198 | bool complete = true; | ||
199 | int min, sec; | ||
200 | int bps; | ||
201 | unsigned long sent, total; | ||
202 | |||
203 | min = sec = bps = -1; | ||
204 | sent = total = 0; | ||
205 | |||
206 | // Data looks like this | ||
207 | // 0 1 2 3 4 5 | ||
208 | // Bytes Sent 65536/11534336 BPS:7784 ETA 24:33 | ||
209 | QStringList progi = QStringList::split('/', list[2].simplifyWhiteSpace() ); | ||
210 | sent = progi[0].toULong(&complete ); | ||
211 | if (!complete ) return; | ||
212 | |||
213 | total = progi[1].toULong(&complete ); | ||
214 | if (!complete || total == 0) { | ||
215 | qWarning("returning!!"); | ||
216 | return; | ||
217 | } | ||
218 | |||
219 | qWarning("%s, %d, %d", progi.join("/").latin1(), sent, total ); | ||
220 | |||
221 | double pro = (double)sent/total; | ||
222 | int prog = pro * 100; | ||
223 | |||
224 | // speed | ||
225 | progi = QStringList::split(':', list[3].simplifyWhiteSpace() ); | ||
226 | bps = progi[1].toInt(); | ||
227 | |||
228 | // time | ||
229 | progi = QStringList::split(':', list[5].simplifyWhiteSpace() ); | ||
230 | min = progi[0].toInt(); | ||
231 | sec = progi[1].toInt(); | ||
232 | |||
233 | |||
234 | qWarning("Prog!:%d", prog ); | ||
235 | if ( prog > m_prog ) { | ||
236 | m_prog = prog; | ||
237 | emit progress(m_file, m_prog, bps, -1, min , sec ); | ||
238 | qWarning("Progress: %s, %d\%, %d, %d:%d", m_file.latin1(), m_prog, bps, min, sec ); | ||
239 | } | ||
240 | |||
241 | } | ||
242 | void FileTransfer::cancel() { | ||
243 | ::kill(m_pid,9 ); | ||
244 | delete m_not; | ||
245 | } | ||