#include <QtCore/QtGlobal>
#include <QtCore/QDir>
#include <QtCore/QSettings>
#include <QtCore/QString>
#include <QtCore/QStringList>
#include <QtGui/QApplication>
#include <QtGui/QDesktopServices>
#include <QtGui/QFont>
#include <QtGui/QRawFont>
#include <iostream>

// Shut up Qt error messages.  (Get a lot from QRawFont.)
void handler(QtMsgType type, const char *msg)
{
}

int main(int argc, char *argv[])
{
    qInstallMsgHandler(handler); // Quiet you!

    QApplication a(argc, argv);
    QFont font = a.font();

    #ifdef Q_OS_WIN32
        QString name("MS Shell Dlg");
    #else
        QString name("Helvetica");
    #endif

    std::cout << "Font asked to be used is '" << qPrintable(name) << "'." << std::endl;

    QString substitute;
    if(!font.exactMatch())
    {
        substitute = font.substitute(name);
        if(substitute == name)
            substitute = font.lastResortFamily();
        if(substitute == name)
            substitute = font.lastResortFont();
    }
    if(substitute == name)
    {
        substitute = QString();
        std::cout << "Font '" << qPrintable(name) << "' was NOT found by Qt and could NOT be substituted." << std::endl;
    }
    else if(!substitute.isEmpty())
    {
        std::cout << "Font '" << qPrintable(name) << "' was NOT found by Qt, so '" << qPrintable(substitute) << "' was substituted." << std::endl;
    }
    else
    {
        std::cout << "Font '" << qPrintable(name) << "' WAS found by Qt." << std::endl;
    }
    name = substitute.isEmpty() ? name : substitute;

    QString mapped(name);
    #ifdef Q_OS_WIN32
        QSettings key("HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\FontSubstitutes", QSettings::NativeFormat);
        QString sub;
        QVariant val;
        foreach(sub, key.childKeys())
        {

            val = key.value(sub);
            if(!val.isNull())
            {
                if(name == sub)
                {
                    mapped = val.toString();
                    break;
                }
            }
        }
    #endif

    if(name == mapped)
    {
        std::cout << "Font '" << qPrintable(name) << "' is not mapped." << std::endl;
    }
    else
    {
        std::cout << "Font '" << qPrintable(name) << "' is mapped to '" << qPrintable(mapped) << "'." << std::endl;
    }

    QString filename;
    QDir fontDir(QDesktopServices::storageLocation(QDesktopServices::FontsLocation));
    if(fontDir.exists())
    {
        QStringList types;
        types << "*.ttf" << "*.odf" << "*.fon";
        QString fn;
        foreach(fn, fontDir.entryList(types, QDir::Files | QDir::Readable, QDir::Name))
        {
            QRawFont r(fontDir.absoluteFilePath(fn), font.pixelSize());
            if(r.familyName() == mapped)
            {
                filename = fontDir.absoluteFilePath(fn);
                break;
            }
        }
    }

    if(filename.isEmpty())
    {
        std::cout << "Font '" << qPrintable(mapped) << "' could not be found in '" << qPrintable(fontDir.absolutePath()) << "'." << std::endl;
    }
    else
    {
        std::cout << "Font '" << qPrintable(mapped) << "' is file '" << qPrintable(filename) << "'." << std::endl;
    }

    //return a.exec();
}
