00001 #include <qlayout.h>
00002 #include <qcombobox.h>
00003 #include <qlabel.h>
00004 #include <qlineedit.h>
00005 #include <qpushbutton.h>
00006 #include <qmessagebox.h>
00007 #include <qprogressbar.h>
00008 #include <qradiobutton.h>
00009 #include <qbuttongroup.h>
00010
00011 #include <opie2/ofiledialog.h>
00012
00013 #include "metafactory.h"
00014 #include "mainwindow.h"
00015
00016 #include "transferdialog.h"
00017
00018 using namespace Opie::Ui;
00019 TransferDialog::TransferDialog(QWidget *parent, MainWindow *mainwindow, const char *)
00020 : QDialog(parent, 0l, false), m_win(mainwindow)
00021 {
00022 m_lay = 0l;
00023 m_recvlay = 0l;
00024 QVBoxLayout *vbox, *vbox2;
00025 QHBoxLayout *hbox, *hbox2, *hbox3;
00026 QLabel *file, *mode, *progress, *status;
00027 QButtonGroup *group;
00028 QRadioButton *mode_send, *mode_receive;
00029
00030 m_autocleanup = 0;
00031 m_running = true;
00032
00033 group = new QButtonGroup(QObject::tr("Transfer mode"), this);
00034 mode_send = new QRadioButton(QObject::tr("Send"), group);
00035 mode_receive = new QRadioButton(QObject::tr("Receive"), group);
00036 group->insert(mode_send, id_send);
00037 group->insert(mode_receive, id_receive);
00038 vbox2 = new QVBoxLayout(group, 2);
00039 vbox2->addSpacing(10);
00040 hbox3 = new QHBoxLayout(vbox2, 2);
00041 hbox3->add(mode_send);
00042 hbox3->add(mode_receive);
00043 mode_send->setChecked(true);
00044 m_transfermode = id_send;
00045
00046 file = new QLabel(QObject::tr("Send file"), this);
00047 mode = new QLabel(QObject::tr("Transfer protocol"), this);
00048 progress = new QLabel(QObject::tr("Progress"), this);
00049 status = new QLabel(QObject::tr("Status"), this);
00050
00051 statusbar = new QLabel(QObject::tr("Ready"), this);
00052 statusbar->setFrameStyle(QFrame::Panel | QFrame::Sunken);
00053
00054 protocol = new QComboBox(this);
00055 QStringList list = m_win->factory()->fileTransferLayers();
00056 for (QStringList::Iterator it = list.begin(); it != list.end(); ++it)
00057 protocol->insertItem((*it));
00058
00059 filename = new QLineEdit(this);
00060
00061 progressbar = new QProgressBar(this);
00062 progressbar->setProgress(0);
00063
00064 selector = new QPushButton("...", this);
00065 ok = new QPushButton(QObject::tr("Start transfer"), this);
00066 cancel = new QPushButton(QObject::tr("Cancel"), this);
00067
00068 vbox = new QVBoxLayout(this, 2);
00069 vbox->add(group);
00070 vbox->add(file);
00071 hbox = new QHBoxLayout(vbox, 0);
00072 hbox->add(filename);
00073 hbox->add(selector);
00074 vbox->add(mode);
00075 vbox->add(protocol);
00076 vbox->add(progress);
00077 vbox->add(progressbar);
00078 vbox->add(status);
00079 vbox->add(statusbar);
00080 vbox->addStretch(1);
00081 hbox2 = new QHBoxLayout(vbox, 2);
00082 hbox2->add(ok);
00083 hbox2->add(cancel);
00084
00085 setCaption(QObject::tr("File transfer"));
00086 show();
00087
00088 connect(selector, SIGNAL(clicked()), SLOT(slotFilename()));
00089 connect(ok, SIGNAL(clicked()), SLOT(slotTransfer()));
00090 connect(cancel, SIGNAL(clicked()), SLOT(slotCancel()));
00091 connect(group, SIGNAL(clicked(int)), SLOT(slotMode(int)));
00092 }
00093
00094 TransferDialog::~TransferDialog()
00095 {
00096 }
00097
00098 void TransferDialog::slotFilename()
00099 {
00100 QString f;
00101
00102 f = OFileDialog::getOpenFileName(0);
00103 if(!f.isNull()) filename->setText(f);
00104 }
00105
00106 void TransferDialog::slotTransfer()
00107 {
00108 if((m_transfermode == id_send) && (filename->text().isEmpty()))
00109 {
00110 QMessageBox::information(this,
00111 QObject::tr("Attention"),
00112 QObject::tr("No file has been specified."));
00113 return;
00114 }
00115
00116 ok->setEnabled(false);
00117
00118 cleanup();
00119 m_autocleanup = 0;
00120
00121 if(m_transfermode == id_send) statusbar->setText(QObject::tr("Sending..."));
00122 else statusbar->setText(QObject::tr("Receiving..."));
00123
00124 if(m_transfermode == id_send)
00125 {
00126 m_lay = m_win->factory()->newFileTransfer(protocol->currentText(), m_win->currentSession()->layer());
00127 m_lay->sendFile(filename->text());
00128
00129 connect(m_lay, SIGNAL(progress(const QString&,int,int,int,int,int)),
00130 SLOT(slotProgress(const QString&,int,int,int,int,int)));
00131 connect(m_lay, SIGNAL(error(int,const QString&)), SLOT(slotError(int,const QString&)));
00132 connect(m_lay, SIGNAL(sent()), SLOT(slotSent()));
00133 }
00134 else
00135 {
00136 m_recvlay = m_win->factory()->newReceive(protocol->currentText(), m_win->currentSession()->layer());
00137 m_recvlay->receive();
00138
00139 connect(m_recvlay, SIGNAL(progress(const QString&,int,int,int,int,int)),
00140 SLOT(slotProgress(const QString&,int,int,int,int,int)));
00141 connect(m_recvlay, SIGNAL(error(int,const QString&)), SLOT(slotError(int,const QString&)));
00142 connect(m_recvlay, SIGNAL(received(const QString&)), SLOT(slotReceived(const QString&)));
00143 }
00144 }
00145
00146 void TransferDialog::cleanup()
00147 {
00148 if(m_lay)
00149 {
00150 m_lay->cancel();
00151 delete m_lay;
00152 m_lay = 0l;
00153 }
00154 if(m_recvlay)
00155 {
00156 m_recvlay->cancel();
00157 delete m_recvlay;
00158 m_recvlay = 0l;
00159 }
00160 }
00161
00162 void TransferDialog::slotCancel()
00163 {
00164 ok->setEnabled(true);
00165 statusbar->setText(QObject::tr("Ready"));
00166
00167 if((m_lay) || (m_recvlay))
00168 {
00169 cleanup();
00170 if(m_autocleanup)
00171 {
00172 m_running = false;
00173 close();
00174 }
00175 else
00176 {
00177 QMessageBox::information(this,
00178 QObject::tr("Cancelled"),
00179 QObject::tr("The file transfer has been cancelled."));
00180 }
00181 }
00182 else
00183 {
00184 m_running = false;
00185 close();
00186 }
00187 }
00188
00189 void TransferDialog::slotProgress(const QString& , int progress, int , int , int, int )
00190 {
00191 progressbar->setProgress(progress);
00192 }
00193
00194 void TransferDialog::slotError(int error, const QString& )
00195 {
00196 statusbar->setText(QObject::tr("Ready"));
00197
00198 switch(error)
00199 {
00200 case FileTransferLayer::NotSupported:
00201 QMessageBox::critical(this,
00202 QObject::tr("Error"),
00203 QObject::tr("Operation not supported."));
00204 break;
00205 case FileTransferLayer::StartError:
00206 QMessageBox::critical(this,
00207 QObject::tr("Error"),
00208 QObject::tr("Transfer could not be started."));
00209 break;
00210 case FileTransferLayer::NoError:
00211 QMessageBox::critical(this,
00212 QObject::tr("Error"),
00213 QObject::tr("No error."));
00214 break;
00215 case FileTransferLayer::Undefined:
00216 QMessageBox::critical(this,
00217 QObject::tr("Error"),
00218 QObject::tr("Undefined error occured."));
00219 break;
00220 case FileTransferLayer::Incomplete:
00221 QMessageBox::critical(this,
00222 QObject::tr("Error"),
00223 QObject::tr("Incomplete transfer."));
00224 break;
00225 case FileTransferLayer::Unknown:
00226 default:
00227 QMessageBox::critical(this,
00228 QObject::tr("Error"),
00229 QObject::tr("Unknown error occured."));
00230 break;
00231 }
00232
00233 m_autocleanup = 1;
00234 }
00235
00236 void TransferDialog::slotSent()
00237 {
00238 progressbar->setProgress(100);
00239 QMessageBox::information(this, QObject::tr("Sent"), QObject::tr("File has been sent."));
00240 ok->setEnabled(true);
00241 progressbar->setProgress(0);
00242 statusbar->setText(QObject::tr("Ready"));
00243 m_autocleanup = 1;
00244 }
00245
00246 void TransferDialog::slotReceived(const QString& )
00247 {
00248 progressbar->setProgress(100);
00249 QMessageBox::information(this, QObject::tr("Received"), QObject::tr("File has been received."));
00250
00251 ok->setEnabled(true);
00252 progressbar->setProgress(0);
00253 statusbar->setText(QObject::tr("Ready"));
00254 m_autocleanup = 1;
00255 }
00256
00257 void TransferDialog::slotMode(int id)
00258 {
00259 if(id == id_send)
00260 {
00261 selector->setEnabled(true);
00262 filename->setEnabled(true);
00263 }
00264 else
00265 {
00266 selector->setEnabled(false);
00267 filename->setEnabled(false);
00268 }
00269 m_transfermode = id;
00270 }
00271
00272 bool TransferDialog::isRunning()
00273 {
00274 return m_running;
00275 }
00276