-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDropBoxDownloadDialog.cpp
94 lines (79 loc) · 2.55 KB
/
DropBoxDownloadDialog.cpp
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
#include "DropBoxDownloadDialog.h"
#include "ui_ProgressDialog.h"
#include <QDir>
#include <QDirIterator>
#include "dropbox/DropboxClient.h"
#include "dropbox/files/FilesRoutes.h"
#include "dropbox/endpoint/DropboxAppInfo.h"
#include "dropbox/endpoint/DropboxAuthInfo.h"
using namespace dropboxQt;
DropBoxDownloadDialog::DropBoxDownloadDialog(QWidget *parent, QString token, QString workingPath) :
ProgressDialog(parent),
m_token(token),
m_workingPath(workingPath)
{
setTitle( "Sync Dropbox..." );
setActionText(QString( "tracks downloaded" ));
}
DropBoxDownloadDialog::~DropBoxDownloadDialog()
{
}
bool DropBoxDownloadDialog::createDownloadList()
{
//Load token
DropboxClient c( m_token );
//What are the new files?
m_downloadList.clear();
QDirIterator it( m_workingPath, QStringList() << "*.fit", QDir::Files, QDirIterator::Subdirectories);
QStringList localList;
while( it.hasNext() ) localList << QFileInfo( it.next() ).fileName();
foreach( QString fitFile, c.listFolder( "/Apps/WahooFitness/" ) )
{
if( !localList.contains( fitFile ) ) m_downloadList << fitFile;
}
//qDebug() << downloadList;
ui->labelInfo->setText( QString( "%1 / %2 %3..." ).arg( 0 ).arg( m_downloadList.size() ).arg( m_actionText ) );
return !m_downloadList.isEmpty();
}
void DropBoxDownloadDialog::downloadFiles()
{
//Download all files
DropboxClient c( m_token );
int todo = m_downloadList.size();
int jobs = todo;
//Create Directory
if( !QDir( m_workingPath + "New/" ).exists() )
QDir().mkdir( m_workingPath + "New/" );
foreach( QString fitFile, m_downloadList )
{
QFile out( QString( m_workingPath + "New/" + fitFile ) );
if(!out.open(QFile::WriteOnly|QIODevice::Truncate)){
m_retVal = RetError;
close();
return;
}
try
{
files::DownloadArg arg( QString( "/Apps/WahooFitness/" + fitFile ) );
std::unique_ptr<files::Metadata> res = c.getFiles()->download(arg, &out);
}
catch(DropboxException& e)
{
m_retVal = RetError;
return;
}
out.close();
if( m_retVal == RetReject )
{
reject();
return;
}
todo--;
ui->progressBar->setValue( 100 * ( (double)jobs - (double)todo ) / (double)jobs );
ui->labelInfo->setText( QString( "%1 / %2 %3..." ).arg( jobs - todo ).arg( jobs ).arg( m_actionText ) );
update();
}
accept();
m_retVal = RetAccept;
return;
}