/*
-----BEGIN QCMOD-----
name: webkit
arg: with-webkit=[type],type of webkit QtWebKit/QtWebEngine
-----END QCMOD-----
*/

QString qc_webkit_type;

//----------------------------------------------------------------------------
// qc_webkit
//----------------------------------------------------------------------------
class qc_webkit : public ConfObj
{
    QString webkitType;
public:
    qc_webkit(Conf *c) : ConfObj(c) {}
    QString name() const { return "QtWebKit"; }
    QString shortname() const { return "webkit"; }
    bool exec()
    {
    	if (!conf->getenv("QC_DISABLE_webkit").isEmpty()) {
    		webkitType = "disabled";
    		return false;
    	}

    	QStringList tryList;
    	QString wt = conf->getenv("QC_WITH_WEBKIT").toLower();
    	if (wt.isEmpty() || !(wt == "qtwebkit" || wt == "qtwebengine")) {
#if QT_VERSION >= QT_VERSION_CHECK(5,6,0)
    		tryList << "qtwebengine" << "qtwebkit";
#else
    		tryList << "qtwebkit";
#endif
    	} else {
#if QT_VERSION < QT_VERSION_CHECK(5,6,0)
    		if (wt == "qtwebengine") {
    			webkitType = "unsupported";
    			return false;
    		}
#endif
    		tryList << wt;
    	}

    	QString qt;
    	foreach (const QString &wt, tryList) {
    		if (wt == "qtwebengine") {
    			webkitType = "QtWebEngine";
    			qt = "webenginewidgets webchannel";
    		} else {
    			webkitType = "QtWebKit";
    			qt = "webkit webkitwidgets";
    		}

    		QString proextra = "CONFIG += qt webkit\n";
    		proextra += "QT += " + qt + "\n";

    		QString str =
    		"\n"
    		"int main()\n"
    		"{\n"
    		"	return 0;\n"
    		"}\n";

    		int ret;
    		if(!conf->doCompileAndLink(str, QStringList(), QString(), proextra, &ret))
    			continue;
    		if(ret != 0)
    			continue;

    		conf->addExtra("CONFIG += " + wt);
    		qc_webkit_type = wt;
    		return true;
    	}

    	webkitType = "not found";

    	return false;
    }

    QString resultString() const
    {
    	return webkitType;
    }
};
