void HeadlessApplication::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);
        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(getTagValue(postIDRegExp.cap(1) + "@" + catIDRefExp.cap(1)));
    } else {
        item->setColor(0);
    }

    //qDebug() << item->getTitle() << postIDRegExp.cap(1) + "@" + catIDRefExp.cap(1) << item->getColor();

    if(item->getColor() == 0) {
        item->deleteLater();
        return;
    }


    QSettings settings("Amonchakai", "HFR10");

    /*
    qDebug() << item->getColor()
             << settings.value("NotifGreen",false).toBool()
             << settings.value("NotifBlue",false).toBool()
             << settings.value("NotifOrange",false).toBool()
             << settings.value("NotifPink",false).toBool()
             << settings.value("NotifPurple",false).toBool();
     */
    switch(item->getColor()) {
        case 1:
            if(!settings.value("NotifGreen",false).toBool()){
                item->deleteLater();
                return;
            }
            break;

        case 2:
            if(!settings.value("NotifBlue",false).toBool()){
                item->deleteLater();
                return;
            }
            break;


        case 3:
            if(!settings.value("NotifOrange",false).toBool()){
                item->deleteLater();
                return;
            }
            break;

        case 4:
            if(!settings.value("NotifPink",false).toBool()){
                item->deleteLater();
                return;
            }
            break;

        case 5:
            if(!settings.value("NotifPurple",false).toBool()){
                item->deleteLater();
                return;
            }
            break;
    }



    QString SubjectNumber = postIDRegExp.cap(1) + QString::number(catIDRefExp.cap(1).toInt()*10000);
    qint64 timestamp = QDateTime::fromString(item->getDetailedTimestamp().mid(0,10) + " " + item->getDetailedTimestamp().mid(23), "dd-MM-yyyy hh:mm").toMSecsSinceEpoch();

    // -----------------------------------------------------------
    // add item into the Hub

    QVariantList hubItems = m_Hub->items();

    bool existing = false;
    QVariantMap itemMap;
    for(int i = 0 ; i < hubItems.length() ; ++i) {
        itemMap = hubItems.at(i).toMap();

        if(itemMap["syncId"].toString() == SubjectNumber) {
            existing = true;
            break;
        }
    }




    if(existing) {

        if(itemMap["timestamp"].toULongLong() == timestamp) {
            item->deleteLater();
            return;
        }

        itemMap["description"] = item->getLastAuthor();
        itemMap["timestamp"] = timestamp;
        itemMap["readCount"] = item->isRead();

        if(!item->getUrlLastPostRead().isEmpty())
            itemMap["url"] = item->getUrlLastPostRead();
        else {
            if(!item->getUrlLastPage().isEmpty())
                itemMap["url"] = item->getUrlFirstPage();
            else {
                itemMap["url"] = item->getUrlFirstPage();
            }
        }

        qint64 itemId;
        if (itemMap["sourceId"].toString().length() > 0) {
            itemId = itemMap["sourceId"].toLongLong();
        } else if (itemMap["messageid"].toString().length() > 0) {
            itemId = itemMap["messageid"].toLongLong();
        }

        qDebug() << "New favorite. update post --> notif.";
        m_Hub->updateHubItem(m_Hub->categoryId(), itemId, itemMap, true);

    } else {

        QVariantMap entry;
        if(!item->getUrlLastPostRead().isEmpty())
            entry["url"] = item->getUrlLastPostRead();
        else {
            if(!item->getUrlFirstPage().isEmpty())
                entry["url"] = item->getUrlFirstPage();
            else {
                item->deleteLater();
                return;
            }
        }

        qDebug() << "New favorite. add new entry --> notif.";
        m_Hub->addHubItem(m_Hub->categoryId(), entry, item->getTitle(), item->getLastAuthor(), timestamp, SubjectNumber, SubjectNumber, "",  true);

    }

    item->deleteLater();


}
Ejemplo n.º 2
0
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);

}