示例#1
0
void SearchController::parseThreadListing(const QString &caption, const QString &urlFirstPage, bool read, const QString &threadListing) {
	ThreadListItem *item = new ThreadListItem();
	QRegExp andAmp("&");
	QRegExp nbsp(" ");


	{
		bool inf = false;
		bool slash = false;
		int idx = 0;
		for(int i = 0 ; i < caption.length() ; ++i) {
			if(caption[i] == '<') {
				inf = true;
				continue;
			}
			if(inf && caption[i] == '/') {
				slash = true;
				continue;
			} else {
				inf = false;
			}

			if(slash && caption[i] == 'a') {
				idx = i-2;
				break;
			}
		}

		if(idx != 0)
			item->setTitle(caption.mid(0,idx));
		else
			item->setTitle(caption);
	}

	item->setRead(read);
	item->setUrlFirstPage(urlFirstPage);

	QRegExp lastPostReadRegexp("</td><td class=\"sujetCase5\"><a href=\"(.*#t[0-9bas]+)\"><img src=");
	lastPostReadRegexp.setCaseSensitivity(Qt::CaseSensitive);
	lastPostReadRegexp.setMinimal(true);

	if(lastPostReadRegexp.indexIn(threadListing, 0) != -1) {
		QString s = lastPostReadRegexp.cap(1);
		s.replace(andAmp, "&");
		item->setUrlLastPostRead(s);
	}

	QRegExp pageNumberRegExp("<td class=\"sujetCase4\"><a href=\"(.*)\" class=\"cCatTopic\">([0-9]+)</a></td><td class=\"sujetCase5\">");
	pageNumberRegExp.setCaseSensitivity(Qt::CaseSensitive);
	pageNumberRegExp.setMinimal(true);

	if(pageNumberRegExp.indexIn(threadListing, 0) != -1) {
		item->setPages(pageNumberRegExp.cap(2));

		if(item->getUrlLastPostRead() == "") {
			QString s = pageNumberRegExp.cap(1);
			s.replace(andAmp, "&");
			item->setUrlLastPostRead(s+"#bas");
		}
	} else {
		item->setPages("1");
	}

	QRegExp lastPageRegExp("<td class=\"sujetCase9 cBackCouleurTab[0-9] \"><a href=\"(.*)\" class=\"Tableau\">(.*)<br /><b>(.*)</b>");
	lastPageRegExp.setCaseSensitivity(Qt::CaseSensitive);
	lastPageRegExp.setMinimal(true);

	if(lastPageRegExp.indexIn(threadListing, 0) != -1) {
		QString s = lastPageRegExp.cap(2);
		s.replace(nbsp, " ");
		item->setTimestamp(s);

		s = lastPageRegExp.cap(1);
		s.replace(andAmp, "&");
		item->setUrlLastPage(s);

		item->setLastAuthor(lastPageRegExp.cap(3));
	}

	QRegExp flagTypeRegexp("<img src=\"http://forum-images.hardware.fr/themes_static/images_forum/1/favoris.gif\"");
	if(flagTypeRegexp.indexIn(threadListing, 0) != -1)
		item->setFlagType(Flag::FAVORITE);

	flagTypeRegexp = QRegExp("<img src=\"http://forum-images.hardware.fr/themes_static/images_forum/1/flag([0-1]).gif\"");
	if(flagTypeRegexp.indexIn(threadListing, 0) != -1) {
		switch(flagTypeRegexp.cap(1).toInt()) {
			case 0:
				item->setFlagType(Flag::READ);
				break;

			case 1:
				item->setFlagType(Flag::PARTICIPATE);
				break;
		}
	}


	if(!item->getLastAuthor().isEmpty())
		m_Datas->append(item);
	else
		item->deleteLater();

}
void ListFavoriteController::parseThreadListing(const QString &category, const QString &caption, const QString &urlFirstPage, const QString &threadListing, const QString &today, int groupKey) {
	//						   + ".*<td class=\"sujetCase4\"><a href=\"(.+)\" class=\"cCatTopic\">"		// link to first post
	//						   + "([0-9]+)</a></td>"													// overall number of pages
	//						   + ".*<td class=\"sujetCase5\"><a href=\"(.+)\"><img src"					// index to last read post
	//						   + ".*p.([0-9]+)"										// last page read number
	//						   + ".*<td class=\"sujetCase9.*class=\"Tableau\">(.+)" 					// time stamp
	//						   + "<br /><b>(.+)</b></a></td><td class=\"sujetCase10\"><input type");	// last contributor

	QRegExp andAmp("&amp;");

	if(caption.isEmpty())
	    return;

	ThreadListItem *item = new ThreadListItem();
	item->setCategory(category);
	item->setTitle(caption);
	item->setUrlFirstPage(urlFirstPage);

	QRegExp firstPostAndPage(QString("<td class=\"sujetCase4\"><a href=\".+\" class=\"cCatTopic\">")		// link to first post
								 + "([0-9]+)</a></td>");													// overall number of pages

	firstPostAndPage.setCaseSensitivity(Qt::CaseSensitive);
	firstPostAndPage.setMinimal(true);

	if(firstPostAndPage.indexIn(threadListing, 0) != -1) {
		item->setPages(firstPostAndPage.cap(1));
	}



	QRegExp lastReadAndPage(QString("<td class=\"sujetCase5\"><a href=\"(.+)\"><img src.*p.([0-9]+).*\" alt=\"flag\"")); // index to last read post + last page read number
	lastReadAndPage.setCaseSensitivity(Qt::CaseSensitive);
	lastReadAndPage.setMinimal(true);

	if(lastReadAndPage.indexIn(threadListing, 0) != -1) {
		QString s = lastReadAndPage.cap(1); s.replace(andAmp, "&");
		item->setUrlLastPostRead(s);

		item->setPages(lastReadAndPage.cap(2)  + "/" + item->getPages());
	}


	QRegExp lastContribution("<td class=\"sujetCase9 cBackCouleurTab[0-9] \"><a href=\"(.+)\" class=\"Tableau\">(.+)<br /><b>(.+)</b></a></td><td class=\"sujetCase10\"><input type"); // timestamp + last contributor
	lastContribution.setCaseSensitivity(Qt::CaseSensitive);
	lastContribution.setMinimal(true);

	if(lastContribution.indexIn(threadListing, 0) != -1) {
		QString s = lastContribution.cap(2);
		if(s.mid(0,10).compare(today) == 0)
			item->setTimestamp(s.mid(23,5));
		else
			item->setTimestamp(s.mid(0,10));
		item->setDetailedTimestamp(s);


		item->setLastAuthor(lastContribution.cap(3));

		s = lastContribution.cap(1);
		s.replace(andAmp, "&");
		item->setUrlLastPage(s);

	}

	QRegExp postIDRegExp("post=([0-9]+)");
	QRegExp catIDRefExp("cat=([0-9]+)");
	if(postIDRegExp.indexIn(item->getUrlLastPage()) != -1 && catIDRefExp.indexIn(item->getUrlLastPage()) != -1) {
	    item->setColor(Settings::getTagValue(postIDRegExp.cap(1) + "@" + catIDRefExp.cap(1)));
	} else {
	    item->setColor(0);
	}

	item->setGroupKey(groupKey);

	m_Datas->append(item);

}