Fixed download progress bar
[icord-viewer:icord-viewer.git] / src / humaxftpviewer.cpp
1 #include "humaxftpviewer.h"
2
3 #include <QtGui/QMenu>
4 #include <QtGui/QMenuBar>
5 #include <QtGui/QAction>
6 #include <QtGui/QVBoxLayout>
7 #include <QtGui/QToolBar>
8 #include <QtGui/QMessageBox>
9 #include <QtNetwork/QUrlInfo>
10 #include <QtNetwork/QFtp>
11
12 #include "ftp.h"
13 #include "mainview.h"
14 #include "recording.h"
15 #include "recordinglistview.h"
16
17 #include <qdebug.h>
18 #include <qprogressdialog.h>
19 #include <qfile.h>
20 #include <QFileDialog>
21 #include <QDebug>
22
23 HumaxFtpViewer::HumaxFtpViewer()
24     : view(0), lvRecordings(0),
25       ftp(new Ftp()),
26       downloadProgress(0),
27       downloadFile(0)
28 {
29     setMinimumSize(QSize(640,480));
30     setupWidgets();
31     setupMenusAndToolbars();
32
33     connect(view, SIGNAL(hostnameChanged(QString)), ftp, SLOT(setHostname(QString)));
34     connect(view, SIGNAL(pinChanged(QString)), ftp, SLOT(setPin(QString)));
35     connect(ftp, SIGNAL(done(bool)), this, SLOT(slotFtpDone(bool)));
36     connect(ftp, SIGNAL(done(bool)), view, SLOT(slotCommunicationDone()));
37     connect(ftp, SIGNAL(recordingListInfo(QUrlInfo)),
38             this, SLOT(slotRecordingListInfo(QUrlInfo)));
39     connect(ftp, SIGNAL(previewListInfo(QString,QUrlInfo)),
40             this, SLOT(slotPreviewListInfo(QString,QUrlInfo)));
41     connect(ftp, SIGNAL(dataTransferProgress(QString,qint64,qint64)),
42             this, SLOT(slotUpdateDownloadProgress(QString,qint64,qint64)));
43     connect(ftp, SIGNAL(fileTransferCompleted(QString)),
44             this, SLOT(slotDownloadDone(QString)));
45 }
46
47 HumaxFtpViewer::~HumaxFtpViewer()
48 {
49     if (ftp->isConnected()) ftp->disconnect();
50     clearRecordings();
51 }
52
53 void HumaxFtpViewer::slotConnectClicked()
54 {
55     clearRecordings();
56     view->clearResults();
57     if (!ftp->isConnected()) ftp->connectToHost();
58     ftp->listRecordings();
59 }
60
61 void HumaxFtpViewer::slotDeleteSelectedRecording()
62 {
63     Recording * recording = lvRecordings->selectedRecording();
64     QMessageBox::StandardButton answer = QMessageBox::question(
65             this, tr("Delete Recording", "@title"),
66             tr("Really delete the following recording:\n%1", "@message")
67                     .arg(recording->toString()),
68             QMessageBox::Yes | QMessageBox::No, QMessageBox::No
69             );
70     if (answer != QMessageBox::Yes) return;
71
72     ftp->removeFile(recording->recordingUrlInfo().name());
73     ftp->removeFile(recording->metaDataUrlInfo().name());
74     ftp->removeFile(recording->ntfFileUrlInfo().name());
75     ftp->removePreviews(recording->previewsDirUrlInfo().name(), recording->previewFiles());
76
77     QProgressDialog * dlg = new QProgressDialog(this);
78     dlg->setLabelText(tr("Deleting %1", "@message").arg(recording->toString()));
79     dlg->setCancelButton(0);
80     dlg->setMinimumDuration(0);
81     dlg->setValue(0);
82     connect(ftp, SIGNAL(done(bool)), dlg, SLOT(close()));
83
84     recordings.erase(recordings.find(recording->hash()));
85     lvRecordings->setRecordings(recordings);
86     delete recording;
87 }
88
89 void HumaxFtpViewer::slotDownloadRecording()
90 {
91     qDebug() << __PRETTY_FUNCTION__;
92     Recording * recording = lvRecordings->selectedRecording();
93     if (recording == 0) return;
94     if (downloadFile != 0) return;
95
96     QString filename = QFileDialog::getSaveFileName(this,
97             tr("Select location to save %1, %2 MB", "@window")
98                     .arg(recording->toString()).arg(recording->size() / (1024 * 1024)),
99             QString("%1--%2.ts").arg(recording->name()).arg(recording->dateTime().toString(Qt::ISODate)),
100             tr("Video files (*.ts)"));
101     if (filename.isEmpty()) return;
102     qDebug() << "Downloading to: " << filename;
103
104     downloadFile = new QFile(filename);
105     downloadFile->open(QIODevice::WriteOnly);
106     ftp->getFile(recording->recordingUrlInfo().name(), downloadFile);
107
108     if (downloadProgress != 0) {
109         delete downloadProgress;
110     }
111     downloadProgress = new QProgressDialog(this);
112     downloadProgress->setLabelText(tr("Downloading %1", "@message").arg(recording->toString()));
113     downloadProgress->setMinimum(0);
114     downloadProgress->setMaximum(recording->size());
115     downloadProgress->setValue(0);
116     connect(downloadProgress, SIGNAL(canceled()), ftp, SLOT(abort()));
117 }
118
119
120 void HumaxFtpViewer::slotUpdateDownloadProgress(const QString& filename, qint64 done, qint64 total)
121 {
122     if (done % (1024*1024) == 0) {
123         qDebug() << "Downloading: " << filename << " -- " << done << " / " << total << "%";
124     }
125     if (downloadProgress == 0) return;
126     downloadProgress->setValue(done);
127 }
128
129 void HumaxFtpViewer::slotDownloadDone(const QString& filename)
130 {
131     qDebug() << "Downloading " << filename << " finished";
132     if (downloadProgress != 0) {
133         delete downloadProgress;
134         downloadProgress = 0;
135     }
136
137     if (downloadFile != 0) {
138         downloadFile->close();
139         delete downloadFile;
140         downloadFile = 0;
141     }
142     qDebug() << "Download completed.";
143 }
144
145
146 void HumaxFtpViewer::slotRecordingListInfo(const QUrlInfo& urlInfo)
147 {
148     if (!urlInfo.isValid()) return;
149
150     QString hash(Recording::hash(urlInfo));
151     if (hash == "-") return;
152
153     Recording * recording = 0;
154     RecordingList::iterator itRecording = recordings.find(hash);
155     if (itRecording == recordings.end()) {
156         recording = new Recording();
157         recordings.insert(hash, recording);
158     }
159     else {
160         recording = itRecording.value();
161     }
162
163     recording->setUrlInfo(urlInfo);
164
165     if (Recording::isPreviewDir(urlInfo)) {
166         ftp->listPreviews(urlInfo.name());
167     }
168 }
169
170 void HumaxFtpViewer::slotPreviewListInfo(const QString& previewsDir, const QUrlInfo& urlInfo)
171 {
172     RecordingList::iterator itRecording = recordings.find(Recording::hash(previewsDir));
173     if (itRecording == recordings.end()) return;
174
175     itRecording.value()->addPreviewUrlInfo(urlInfo);
176 }
177
178
179 void HumaxFtpViewer::slotFtpDone ( bool result )
180 {
181     QString info;
182     if(result) {
183         info = tr("Done without error.");
184     }
185     else {
186         info = tr("Done with errors: %1").arg(ftp->errorString());
187     }
188     view->addToResults(info);
189     lvRecordings->setRecordings(recordings);
190 }
191
192 void HumaxFtpViewer::clearRecordings()
193 {
194     lvRecordings->clearRecordings();
195     qDeleteAll(recordings);
196     recordings.clear();
197 }
198
199 void HumaxFtpViewer::setupWidgets()
200 {
201     view = new MainView();
202     connect(view, SIGNAL(hostnameChanged(QString)), ftp, SLOT(setHostname(QString)));
203     connect(view, SIGNAL(pinChanged(QString)), ftp, SLOT(setPin(QString)));
204
205     lvRecordings = new RecordingListView();
206
207     QWidget * main = new QWidget();
208     setCentralWidget(main);
209     QVBoxLayout * mainLayout = new QVBoxLayout(main);
210     mainLayout->addWidget(view);
211     mainLayout->addWidget(lvRecordings);
212
213 }
214
215 void HumaxFtpViewer::setupMenusAndToolbars()
216 {
217     QAction * quitAction = new QAction(this);
218     quitAction->setText(tr("Quit", "@action"));
219     connect(quitAction, SIGNAL(triggered()), this, SLOT(close()) );
220
221     QAction * readRecordingsAction = new QAction(this);
222     readRecordingsAction->setText(tr("Read recordings", "@action"));
223     readRecordingsAction->setEnabled(false);
224     connect(readRecordingsAction, SIGNAL(triggered()),
225             this, SLOT(slotConnectClicked()));
226     connect(view, SIGNAL(connectClicked()),
227             readRecordingsAction, SLOT(trigger()));
228     connect(ftp, SIGNAL(credentialsChanged(bool)),
229             readRecordingsAction, SLOT(setEnabled(bool)));
230
231     QAction * deleteRecordingAction = new QAction(this);
232     deleteRecordingAction->setText(tr("Delete recording", "@action"));
233     deleteRecordingAction->setEnabled(false);
234     connect(deleteRecordingAction, SIGNAL(triggered(bool)),
235             this, SLOT(slotDeleteSelectedRecording()));
236     connect(lvRecordings, SIGNAL(selectionChanged(bool)),
237             deleteRecordingAction, SLOT(setEnabled(bool)));
238
239     QAction * downloadRecordingAction = new QAction(this);
240     downloadRecordingAction->setText(tr("Download", "@action"));
241     downloadRecordingAction->setEnabled(false);
242     connect(downloadRecordingAction, SIGNAL(triggered(bool)),
243             this, SLOT(slotDownloadRecording()));
244     connect(lvRecordings, SIGNAL(selectionChanged(bool)),
245             downloadRecordingAction, SLOT(setEnabled(bool)));
246
247     QMenu * fileMenu = new QMenu(tr("File", "@menu"));
248     menuBar()->addMenu(fileMenu);
249     fileMenu->addAction(readRecordingsAction);
250     fileMenu->addAction(quitAction);
251
252     QToolBar * toolbar = addToolBar(tr("Default", "@label"));
253     toolbar->addAction(quitAction);
254     toolbar->addSeparator();
255     toolbar->addAction(readRecordingsAction);
256     toolbar->addAction(downloadRecordingAction);
257     toolbar->addAction(deleteRecordingAction);
258 }
259
260
261
262
263 #include "humaxftpviewer.moc"