/** * Load last 100 tweets mentions from database * @reimp */ void MentionsQmlListModel::loadTweetsFromDatabase() { QSqlQuery query; query.prepare("SELECT id, text, screenName, profileImageUrl, userId, created " "FROM status " "WHERE mention = 1 " "ORDER BY id DESC " "LIMIT 100 "); query.exec(); //remove/clear all statuses beginResetModel(); m_statuses.clear(); m_numUnreadTweets = 0; endResetModel(); QList<QTweetStatus> newStatuses; while (query.next()) { QTweetStatus st; st.setId(query.value(0).toLongLong()); st.setText(query.value(1).toString()); //Datetime is stored in UTC QDateTime tempTime = query.value(5).toDateTime(); QDateTime utcTime(tempTime.date(), tempTime.time(), Qt::UTC); st.setCreatedAt(utcTime); QTweetUser userinfo; userinfo.setScreenName(query.value(2).toString()); userinfo.setprofileImageUrl(query.value(3).toString()); userinfo.setId(query.value(4).toInt()); st.setUser(userinfo); newStatuses.append(st); } if (newStatuses.count()) { beginInsertRows(QModelIndex(), 0, newStatuses.count() - 1); m_statuses.append(newStatuses); endInsertRows(); } }
QTweetStatus QTweetConvert::jsonObjectToStatus(const QJsonObject& json) { QTweetStatus status; status.setCreatedAt(json["created_at"].toString()); status.setText(json["text"].toString()); status.setId(static_cast<qint64>(json["id"].toDouble())); status.setInReplyToUserId(static_cast<qint64>(json["in_reply_to_user_id"].toDouble())); status.setInReplyToScreenName(json["in_reply_to_screen_name"].toString()); status.setFavorited(json["favorited"].toBool()); QJsonObject userObject = json["user"].toObject(); QTweetUser user = jsonObjectToUser(userObject); status.setUser(user); status.setSource(json["source"].toString()); status.setInReplyToStatusId(static_cast<qint64>(json["in_reply_to_status_id"].toDouble())); //check if contains native retweet if (json.contains("retweeted_status")) { QJsonObject retweetObject = json["retweeted_status"].toObject(); QTweetStatus rtStatus = jsonObjectToStatus(retweetObject); status.setRetweetedStatus(rtStatus); } //parse place id if it's not null QJsonValue placeValue = json["place"]; if (!placeValue.isNull()) { QTweetPlace place = jsonObjectToPlace(placeValue.toObject()); status.setPlace(place); } //check if contains entities if (json.contains("entities")) { QJsonObject entitiesObject = json["entities"].toObject(); //url entities QJsonArray urlEntitiesList = entitiesObject["urls"].toArray(); for (int i = 0; i < urlEntitiesList.size(); ++i) { QTweetEntityUrl urlEntity = jsonObjectToEntityUrl(urlEntitiesList[i].toObject()); status.addUrlEntity(urlEntity); } //hashtag entities QJsonArray hashtagEntitiesList = entitiesObject["hashtags"].toArray(); for (int i = 0; i < hashtagEntitiesList.size(); ++i) { QTweetEntityHashtag hashtagEntity = jsonObjectToEntityHashtag(hashtagEntitiesList[i].toObject()); status.addHashtagEntity(hashtagEntity); } //user mentions QJsonArray userMentionsEntitiesList = entitiesObject["user_mentions"].toArray(); for (int i = 0; i < userMentionsEntitiesList.count(); ++i) { QTweetEntityUserMentions userMentionsEntity = jsonObjectToEntityUserMentions(userMentionsEntitiesList[i].toObject()); status.addUserMentionsEntity(userMentionsEntity); } //media QJsonArray mediaEntitiesList = entitiesObject["media"].toArray(); for (int i = 0; i < mediaEntitiesList.count(); ++i) { QTweetEntityMedia mediaEntity = jsonObjectToEntityMedia(mediaEntitiesList[i].toObject()); status.addMediaEntity(mediaEntity); } } return status; }