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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
| /* Pour générer le projet:
* qmake -project "QT += network"
* qmake && make (ou nmake pour les utilisateurs de VC++)
*/
#include <QtCore>
#include <QtGui>
#include <QtNetwork>
class PADAlbum : public QObject
{
Q_OBJECT
public:
PADAlbum(QString albumUrl, QObject *parent = 0)
:QObject(parent), expectedNetReply(0)
{
netAccessMgr = new QNetworkAccessManager(this);
connect(netAccessMgr, SIGNAL(finished(QNetworkReply*)), this, SLOT(requestFinished(QNetworkReply*)));
albumPageDownload = true;
launchDownload(albumUrl);
}
void downloadAll(QString targetFolder)
{
currentPictureDownload = 0;
albumPageDownload = false;
destFolder = targetFolder;
downloadNextPicture();
}
int pictureQuantity()
{
return pictureQty;
}
QString albumName()
{
return name;
}
signals:
void albumNameFound(QString albumName);
void pictureQuantityFound(int pictureQty);
void currentPictureDownloaded(int currentPicture);
void finishedDownloadingAlbum();
private:
void launchDownload(QString urlStr)
{
QUrl url(urlStr);
expectedNetReply = netAccessMgr->get(QNetworkRequest(url));
connect(expectedNetReply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(requestError(QNetworkReply::NetworkError)));
}
void albumPageDownloaded()
{
QString albumPage(expectedNetReply->readAll());
// Search album title.
int titlePos = albumPage.indexOf("\"title\":\"");
if (titlePos == -1)
{
name = "";
pictureQty = 0;
picturesURLs.clear();
QMessageBox::information(0, tr("PicasaAlbumDownloader"), tr("Pas d'album à cette adresse !"));
emit pictureQuantityFound(pictureQty);
emit finishedDownloadingAlbum();
return;
}
titlePos += QString("\"title\":\"").length();
int endtitlePos = albumPage.indexOf("\",\"sub", titlePos + 1);
name = albumPage.mid(titlePos, endtitlePos - titlePos);
name.replace("\\\"", "\"");
// Search pictures count.
int pictureCountPos = albumPage.indexOf("_album.photoCount");
pictureCountPos += QString("_album.photoCount = ").length();
int endPictureCountPos = albumPage.indexOf(";", pictureCountPos);
QString pictureQtyStr = albumPage.mid(pictureCountPos, endPictureCountPos - pictureCountPos);
pictureQty = pictureQtyStr.toInt();
// Gather pictures URLs.
picturesURLs.reserve(pictureQty);
int pictureUrlPos = 0;
while((pictureUrlPos = albumPage.indexOf("content$src", pictureUrlPos)) != -1)
{
pictureUrlPos += QString("content$src\":\"").length();
int endPictureUrlPos = albumPage.indexOf("\"", pictureUrlPos + 1);
QString pictureUrl = albumPage.mid(pictureUrlPos, endPictureUrlPos - pictureUrlPos);
picturesURLs.append(pictureUrl);
}
emit albumNameFound(name);
emit pictureQuantityFound(pictureQty);
}
void downloadNextPicture()
{
if (currentPictureDownload == pictureQty)
{
emit finishedDownloadingAlbum();
return;
}
launchDownload(picturesURLs[currentPictureDownload]);
}
void photoDownloaded()
{
QUrl url(picturesURLs[currentPictureDownload]);
QFileInfo fileInfo(url.path());
QFile pictFile(destFolder + "/" + QUrl::fromPercentEncoding(fileInfo.fileName().toUtf8()), this);
pictFile.open(QIODevice::WriteOnly);
const qint64 expectedImgSize = expectedNetReply->size();
const qint64 bytesWritten = pictFile.write(expectedNetReply->readAll());
if (bytesWritten != expectedImgSize)
qDebug(qPrintable(QString("Can't save %1").arg(pictFile.fileName())));
emit currentPictureDownloaded(currentPictureDownload + 1);
currentPictureDownload++;
downloadNextPicture();
}
private slots:
void requestFinished(QNetworkReply *reply)
{
if (reply != expectedNetReply)
return;
if (albumPageDownload)
albumPageDownloaded();
else
photoDownloaded();
}
void requestError(QNetworkReply::NetworkError /*code*/)
{
QMessageBox::information(0, tr("PicasaAlbumDownloader"),
tr("Téléchargement échoué: %1.").arg(expectedNetReply->errorString()));
}
private:
QNetworkAccessManager * netAccessMgr;
QNetworkReply * expectedNetReply;
bool albumPageDownload;
QString name;
int pictureQty;
QVector<QString> picturesURLs;
int currentPictureDownload;
QString destFolder;
};
class PADWindow : public QWidget
{
Q_OBJECT
public:
PADWindow()
:album(0)
{
setWindowTitle("PicasaAlbumDownloader");
// Widgets
albumUrlLabel = new QLabel(tr("Adresse de l'album Picasa:"));
albumUrlLE = new QLineEdit;
destFolderLabel = new QLabel(tr("Destination:"));
destFolderLE = new QLineEdit;
openFolderButton = new QPushButton("...");
subfolderAlbumName = new QCheckBox(tr("Créer un sous-dossier portant le nom de l'album"));
albumNameLabel = new QLabel;
pictureDownloadedOnQuantity = new QLabel;
quitButton = new QPushButton(tr("Quitter"));
downloadAlbumButton = new QPushButton(tr("Télécharger l'album"));
// Layout
QGridLayout *layout = new QGridLayout;
layout->addWidget(albumUrlLabel, 0, 0);
layout->addWidget(albumUrlLE, 0, 1);
layout->addWidget(destFolderLabel, 1, 0);
layout->addWidget(destFolderLE, 1, 1);
layout->addWidget(openFolderButton, 1, 2);
layout->addWidget(subfolderAlbumName, 2, 1, 1, 2);
layout->addWidget(albumNameLabel, 3, 0);
layout->addWidget(pictureDownloadedOnQuantity, 3, 1, 1, 2);
layout->addWidget(quitButton, 4, 0);
layout->addWidget(downloadAlbumButton, 4, 2);
layout->setColumnMinimumWidth(1, 250);
setLayout(layout);
// Signals/Slots connection
connect(openFolderButton, SIGNAL(clicked()), this, SLOT(openFolder()));
connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
connect(downloadAlbumButton, SIGNAL(clicked()), this, SLOT(downloadAlbum()));
}
private slots:
void openFolder()
{
QString dest = QFileDialog::getExistingDirectory(this);
if (!dest.isEmpty())
destFolderLE->setText(dest);
}
void downloadAlbum()
{
delete album;
album = new PADAlbum(albumUrlLE->text(), this);
connect(album, SIGNAL(albumNameFound(QString)), this, SLOT(albumNameFound(QString)));
connect(album, SIGNAL(pictureQuantityFound(int)), this, SLOT(pictureQuantityFound(int)));
connect(album, SIGNAL(currentPictureDownloaded(int)), this, SLOT(currentPictureDownloaded(int)));
connect(album, SIGNAL(finishedDownloadingAlbum()), this, SLOT(finishedAlbumDownload()));
downloadAlbumButton->setEnabled(false);
}
void albumNameFound(QString albumName)
{
albumNameLabel->setText(tr("Album: %1").arg(albumName));
}
void pictureQuantityFound(int pictureQty)
{
pictureDownloadedOnQuantity->setText(tr("0 / %1 photos téléchargées").arg(pictureQty));
if(pictureQty <= 0)
return;
QString destFolder = destFolderLE->text();
if (subfolderAlbumName->checkState() == Qt::Checked)
{
QDir target(destFolderLE->text());
QString cleanedAlbumName = album->albumName().replace(QRegExp("[^a-zA-Z0-9\\s_]"), "_");
target.mkdir(cleanedAlbumName);
destFolder += QString("/" + cleanedAlbumName);
}
album->downloadAll(destFolder);
}
void currentPictureDownloaded(int currentPicture)
{
pictureDownloadedOnQuantity->setText(tr("%1 / %2 photos téléchargées").arg(currentPicture).arg(album->pictureQuantity()));
}
void finishedAlbumDownload()
{
downloadAlbumButton->setEnabled(true);
if (album->pictureQuantity() > 0)
QMessageBox::information(this, "PicasaAlbumDownloader", tr("L'album a été téléchargé intégralement!"));
album->deleteLater();
album = 0;
}
private:
QLabel * albumUrlLabel;
QLineEdit * albumUrlLE;
QLabel * destFolderLabel;
QLineEdit * destFolderLE;
QPushButton * openFolderButton;
QCheckBox * subfolderAlbumName;
QLabel * albumNameLabel;
QLabel * pictureDownloadedOnQuantity;
QPushButton * quitButton;
QPushButton * downloadAlbumButton;
PADAlbum * album;
};
#include "main.moc"
int main(int argc, char **argv)
{
QApplication app(argc, argv);
PADWindow w;
w.show();
return app.exec();
} |
Partager