-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy paththumbmanager.cpp
executable file
·98 lines (91 loc) · 3.71 KB
/
thumbmanager.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
95
96
97
98
#include "thumbmanager.h"
#include "qweenapplication.h"
ThumbManager::ThumbManager(QObject *parent) :
QObject(parent)
{
m_manager = new QNetworkAccessManager( this );
connect(m_manager,SIGNAL(finished(QNetworkReply*)),
this, SLOT(replyFinished(QNetworkReply*)));
}
void ThumbManager::fetchThumb(const QString& uri){
if(m_connections.contains(uri)) return;
QString fetchUri(getFetchUri(uri));
if(fetchUri.isEmpty()) return;
QNetworkReply *rep = m_manager->get(QNetworkRequest(fetchUri));
m_connections.insert(uri, rep);
}
bool ThumbManager::isThumbAvailable(const QString& uri){
return QFile(getThumbFilePath(uri)).exists();
}
QString ThumbManager::getThumbFilePath(const QString& uri){
QDir thumbdir(QweenApplication::profileDir() + "/thumb");
if(!thumbdir.exists())
thumbdir.mkpath(thumbdir.path());
QString fileName;
QRegExp tprx("http://twitpic.com/([0-9a-z]+)$");
QRegExp mprx("http://movapic.com/pic/([0-9a-z]+)$");
QRegExp ytrx("http://www.youtube.com/watch\\?v=([0-9a-zA-Z_]+)");
QRegExp htrx("http://f.hatena.ne.jp/([0-9a-zA-Z]+)/([0-9]+)$");
if(tprx.indexIn(uri) != -1){
fileName = QString("twitpic_%1").arg(tprx.cap(1));
}else if(mprx.indexIn(uri) != -1){
fileName = QString("movapic_%1").arg(mprx.cap(1));
}else if(ytrx.indexIn(uri) != -1){
fileName = QString("youtube_%1").arg(ytrx.cap(1));
}else if(htrx.indexIn(uri) != -1){
fileName = QString("fotolife_%1_%2").arg(htrx.cap(1), htrx.cap(2));
}else{
return "";
}
return thumbdir.path() + "/" + fileName;
}
QString ThumbManager::getFetchUri(const QString& uri){
QString fetchUri;
QRegExp tprx("http://twitpic.com/([0-9a-z]+)$");
QRegExp mprx("http://movapic.com/pic/([0-9a-z]+)$");
QRegExp ytrx("http://www.youtube.com/watch\\?v=([0-9a-zA-Z_]+)");
QRegExp htrx("http://f.hatena.ne.jp/([0-9a-zA-Z]+)/([0-9]+)$");
if(tprx.indexIn(uri) != -1){
fetchUri = QString("http://twitpic.com/show/mini/%1").arg(tprx.cap(1));
}else if(mprx.indexIn(uri) != -1){
fetchUri = QString("http://image.movapic.com/pic/t_%1.jpeg").arg(mprx.cap(1));
}else if(ytrx.indexIn(uri) != -1){
fetchUri = QString("http://i.ytimg.com/vi/%1/default.jpg").arg(ytrx.cap(1));
}else if(htrx.indexIn(uri) != -1){
/*
Page: http://f.hatena.ne.jp/<user-id>/YYYYMMDD<image-id>
Thumb:http://img.f.hatena.ne.jp/images/fotolife/<user-id>[0]/<user-id>/YYYYMMDD/YYYYMMDD<image-id>_m.jpg
*/
fetchUri = QString("http://img.f.hatena.ne.jp/images/fotolife/%1/%2/%3/%4_m.jpg")
.arg(QString(htrx.cap(1)[0]),
htrx.cap(1),
htrx.cap(2).left(8), //Y10K :-P
htrx.cap(2));
}else{
return "";
}
return fetchUri;
}
void ThumbManager::replyFinished(QNetworkReply* reply){
QString uri = m_connections.key(reply);
m_connections.remove(uri);
if(uri.isEmpty()) {
return;
}
if(reply->error() != QNetworkReply::NoError){
reply->deleteLater();
return;
}else if(!reply->attribute(QNetworkRequest::RedirectionTargetAttribute).isNull()){
QUrl newuri(reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl());
QNetworkReply *rep = m_manager->get(QNetworkRequest(newuri));
m_connections.insert(uri, rep);
reply->deleteLater();
return;
}
QFile thumbFile(getThumbFilePath(uri));
thumbFile.open(QFile::WriteOnly);
thumbFile.write(reply->readAll());
thumbFile.close();
emit thumbDownloaded(uri);
reply->deleteLater();
}