Commit 97d7cf75 authored by Daniel Kreuter's avatar Daniel Kreuter

Added support for pictures in Twitter posts.

Choqok will now be able to display images of tweets if available.

CCBUG: 299433
parents 79415518 3ac39f19
......@@ -220,6 +220,9 @@ QList< Choqok::Post* > TwitterApiMicroBlog::loadTimeline( Choqok::Account *accou
st->repeatedFromUsername = grp.readEntry("repeatedFrom", QString());
st->repeatedPostId = grp.readEntry("repeatedPostId", QString());
st->conversationId = grp.readEntry("conversationId", QString());
st->media = grp.readEntry("mediaUrl", QString());
st->mediaSizeWidth = grp.readEntry("mediaWidth", 0);
st->mediaSizeHeight = grp.readEntry("mediaHeight", 0);
list.append( st );
}
......@@ -270,6 +273,9 @@ void TwitterApiMicroBlog::saveTimeline(Choqok::Account *account,
grp.writeEntry( "repeatedFrom", post->repeatedFromUsername);
grp.writeEntry( "repeatedPostId", post->repeatedPostId.toString());
grp.writeEntry( "conversationId", post->conversationId.toString() );
grp.writeEntry( "mediaUrl", post->media);
grp.writeEntry("mediaWidth", post->mediaSizeWidth);
grp.writeEntry("mediaHeight", post->mediaSizeHeight);
}
postsBackup.sync();
}
......@@ -1188,6 +1194,24 @@ Choqok::Post* TwitterApiMicroBlog::readPost(Choqok::Account* theAccount,
post->author.userName = userMap["screen_name"].toString();
post->author.profileImageUrl = userMap["profile_image_url"].toString();
post->author.homePageUrl = userMap["statusnet_profile_url"].toString();
QVariantMap entities = var["entities"].toMap();
QVariantMap mediaMap;
QVariantList media = entities["media"].toList();
if(media.size() > 0) {
mediaMap = media.at(0).toMap();
post->media = mediaMap["media_url"].toString() + ":small";
QVariantMap sizes = mediaMap["sizes"].toMap();
QVariantMap w = sizes["small"].toMap();
kError() << "size: " << w;
post->mediaSizeWidth = w["w"].toInt() != 0L ? w["w"].toInt() : 0;
post->mediaSizeHeight = w["h"].toInt() != 0L ? w["h"].toInt() : 0;
}
else {
post->media = "";
post->mediaSizeHeight = 0;
post->mediaSizeWidth = 0;
}
Choqok::Post* repeatedPost = 0;
QVariantMap retweetedMap = var["retweeted_status"].toMap();
if( !retweetedMap.isEmpty() ){
......
......@@ -77,6 +77,9 @@ public:
QString repeatedFromUsername;
ChoqokId repeatedPostId;
ChoqokId conversationId;
QString media; // first Image of Post, if available
int mediaSizeWidth;
int mediaSizeHeight;
unsigned int owners; // number of associated PostWidgets
};
/**
......
......@@ -66,8 +66,11 @@ class PostWidget::Private
//BEGIN UI contents:
QString mSign;
QString mContent;
QString mProfileImage;
QString mImage;
QString imageUrl;
QString dir;
QPixmap originalImage;
//END UI contents;
QStringList detectedUrls;
......@@ -75,11 +78,9 @@ class PostWidget::Private
TimelineWidget *timeline;
};
const QString PostWidget::ownText ("<table width=\"100%\" ><tr><td width=\"90%\" dir=\"%4\">%2</td><td rowspan=\"2\" align=\"right\">%1</td></tr><tr>%5</tr><tr><td colspan=\"2\" style=\"font-size:small;\" dir=\"ltr\" align=\"right\" width=\"100%\" valign=\"bottom\">%3</td></tr></table>");
const QString PostWidget::ownText ("<table width=\"100%\" ><tr><td dir=\"%4\"><p>%2</p></td><td width=\"5\"><!-- empty --></td><td width=\"48\" rowspan=\"2\" align=\"right\">%1</td></tr><tr><td style=\"font-size:small;\" dir=\"ltr\" align=\"right\" valign=\"bottom\">%3</td></tr></table>");
const QString PostWidget::otherText ( "<table height=\"100%\" width=\"100%\"><tr><td rowspan=\"2\"\
width=\"48\">%1</td><td width=\"5\"><!-- EMPTY HAHA --></td><td dir=\"%4\"><p>%2</p></td></tr><tr><td><!-- EMPTY HAHA --></td><td style=\"font-size:small;\" dir=\"ltr\" align=\"right\" width=\"100%\" valign=\"bottom\">%3</td></tr></table>");
const QString PostWidget::otherText ( "<table height=\"100%\" width=\"100%\"><tr><td rowspan=\"2\" width=\"48\">%1</td><td width=\"5\"><!-- EMPTY HAHA --></td><td colspan=\"2\" dir=\"%4\"><p>%2</p></td></tr><tr><td></td>%5</tr><tr><td ><!-- empty --></td><td></td><td colspan=\"2\" style=\"font-size:small;\" dir=\"ltr\" align=\"right\" width=\"100%\" valign=\"bottom\">%3</td></tr></table>");
const QString PostWidget::baseStyle ("KTextBrowser {border: 1px solid rgb(150,150,150);\
border-radius:5px; color:%1; background-color:%2; %3}\
......@@ -129,6 +130,12 @@ PostWidget::PostWidget( Account* account, Choqok::Post* post, QWidget* parent/*
d->timeline = qobject_cast<TimelineWidget*>(parent);
d->mCurrentPost->owners++;
if(!d->mCurrentPost->media.isEmpty()) {
d->imageUrl = d->mCurrentPost->media;
}
setHeight();
}
void PostWidget::checkAnchor(const QUrl & url)
......@@ -232,26 +239,33 @@ void PostWidget::initUi()
connect(btnResend, SIGNAL(clicked(bool)), SLOT(slotResendPost()));
baseText = &otherText;
}*/
d->mImage = "<img src=\"img://profileImage\" title=\""+ d->mCurrentPost->author.realName +"\" width=\"48\" height=\"48\" />";
d->mProfileImage = "<img src=\"img://profileImage\" title=\""+ d->mCurrentPost->author.realName +"\" width=\"48\" height=\"48\" />";
if(!d->imageUrl.isEmpty()) {
d->mImage = QString("<td width=\"%1\" height=\"%2\" style=\"padding-left: 5px; padding-left: 5px;\"><img src=\"img://postImage\" /></td>").arg(d->mCurrentPost->mediaSizeWidth, d->mCurrentPost->mediaSizeHeight);
}
d->mContent = prepareStatus(d->mCurrentPost->content);
d->mSign = generateSign();
setupAvatar();
fetchImage();
setDirection();
setUiStyle();
d->mContent.replace("<a href","<a style=\"text-decoration:none\" href",Qt::CaseInsensitive);
d->mContent.replace("\n", "<br/>");
d->mSign.replace("<a href","<a style=\"text-decoration:none\" href",Qt::CaseInsensitive);
updateUi();
}
void PostWidget::updateUi()
void PostWidget::updateUi()
{
_mainWidget->setHtml(baseText->arg( d->mImage, d->mContent,
_mainWidget->setHtml(baseText->arg( d->mProfileImage, d->mContent,
d->mSign.arg(formatDateTime( d->mCurrentPost->creationDateTime )),
d->dir ));
d->dir,
d->mImage
));
}
void PostWidget::setStyle(const QColor& color, const QColor& back, const QColor& read, const QColor& readBack, const QColor& own, const QColor& ownBack, const QFont& font)
......@@ -365,7 +379,30 @@ void PostWidget::mousePressEvent(QMouseEvent* ev)
void PostWidget::resizeEvent ( QResizeEvent * event )
{
// only scale if image is present
if(!d->originalImage.isNull()) {
QPixmap *newPixmap;
// TODO: Find a way to calculate the difference we need to subtract.
int w = event->size().width() - 76;
newPixmap = new QPixmap(d->originalImage.scaledToWidth(w, Qt::SmoothTransformation));
int newW = newPixmap->width();
int newH = newPixmap->height();
// only use scaled image if it's smaller than the original one
if(newW <= d->originalImage.width() && newH <= d->originalImage.height()) { // never scale up
d->mImage = QString("<td width=\"%1\" height=\"%2\"><img src=\"img://postImage\" /></td>").arg(newW, newH );
QString url = "img://postImage";
_mainWidget->document()->addResource( QTextDocument::ImageResource, url, *newPixmap);
} else {
d->mImage = QString("<td width=\"%1\" height=\"%2\"><img src=\"img://postImage\" /></td>").arg(d->mCurrentPost->mediaSizeWidth, d->mCurrentPost->mediaSizeHeight);
QString url = "img://postImage";
_mainWidget->document()->addResource( QTextDocument::ImageResource, url, d->originalImage);
}
}
setHeight();
updateUi();
QWidget::resizeEvent(event);
}
......@@ -533,6 +570,20 @@ QString PostWidget::generateResendText()
}
}
void PostWidget::fetchImage() {
if(d->imageUrl.isEmpty())
return;
QPixmap *pix = MediaManager::self()->fetchImage(d->imageUrl, MediaManager::Async);
if(pix)
slotImageFetched(d->imageUrl, *pix);
else {
connect( MediaManager::self(), SIGNAL(imageFetched(QString,QPixmap)),
this, SLOT(slotImageFetched(QString, QPixmap)));
}
}
void PostWidget::setupAvatar()
{
QPixmap *pix = MediaManager::self()->fetchImage( d->mCurrentPost->author.profileImageUrl,
......@@ -572,6 +623,18 @@ void PostWidget::avatarFetchError(const QString& remoteUrl, const QString& errMs
}
}
void PostWidget::slotImageFetched(const QString& remoteUrl, const QPixmap& pixmap) {
if(remoteUrl == d->imageUrl) {
QString url = "img://postImage";
QPixmap *newPixmap = new QPixmap(pixmap.scaled(d->mCurrentPost->mediaSizeWidth, d->mCurrentPost->mediaSizeHeight));
_mainWidget->document()->addResource( QTextDocument::ImageResource, url, *newPixmap);
d->originalImage = pixmap;
updateUi();
disconnect( MediaManager::self(), SIGNAL(imageFetched(QString, QPixmap)), this, SLOT(slotImageFetched(QString, QPixmap)));
}
}
QMap<QString, KPushButton* >& PostWidget::buttons()
{
return d->mUiButtons;
......@@ -592,12 +655,12 @@ void PostWidget::slotPostError(Account* theAccount, Choqok::Post* post,
QString PostWidget::avatarText() const
{
return d->mImage;
return d->mProfileImage;
}
void PostWidget::setAvatarText(const QString& text)
{
d->mImage = text;
d->mProfileImage = text;
updateUi();
}
......
......@@ -187,11 +187,14 @@ protected Q_SLOTS:
void avatarFetchError( const QString &remoteUrl, const QString &errMsg );
void avatarFetched( const QString &remoteUrl, const QPixmap &pixmap );
void slotImageFetched( const QString &remoteUrl, const QPixmap &pixmap );
virtual void mousePressEvent(QMouseEvent* ev);
protected:
virtual void setupUi();
virtual void closeEvent(QCloseEvent* event);
virtual void setupAvatar();
virtual void fetchImage();
virtual void wheelEvent(QWheelEvent* );
virtual void resizeEvent ( QResizeEvent * event );
virtual void enterEvent ( QEvent * event );
......@@ -245,6 +248,8 @@ public:
PostWidget *postWidget();
void setPostWidget(PostWidget *widget);
private:
class Private;
Private * const d;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment