const mozilla::Module*
nsNativeModuleLoader::LoadModule(nsILocalFile* aFile)
{
    nsresult rv;

    if (!NS_IsMainThread()) {
        // If this call is off the main thread, synchronously proxy it
        // to the main thread.
        nsRefPtr<LoadModuleMainThreadRunnable> r = new LoadModuleMainThreadRunnable(this, aFile);
        NS_DispatchToMainThread(r, NS_DISPATCH_SYNC);
        return r->mResult;
    }

    nsCOMPtr<nsIHashable> hashedFile(do_QueryInterface(aFile));
    if (!hashedFile) {
        NS_ERROR("nsIFile is not nsIHashable");
        return NULL;
    }

    nsCAutoString filePath;
    aFile->GetNativePath(filePath);

    NativeLoadData data;

    if (mLibraries.Get(hashedFile, &data)) {
        NS_ASSERTION(data.module, "Corrupt mLibraries hash");
        LOG(PR_LOG_DEBUG,
            ("nsNativeModuleLoader::LoadModule(\"%s\") - found in cache",
             filePath.get()));
        return data.module;
    }

    // We haven't loaded this module before

    rv = aFile->Load(&data.library);

    if (NS_FAILED(rv)) {
        char errorMsg[1024] = "<unknown; can't get error from NSPR>";

        if (PR_GetErrorTextLength() < (int) sizeof(errorMsg))
            PR_GetErrorText(errorMsg);

        LogMessage("Failed to load native module at path '%s': (%lx) %s",
                   filePath.get(), rv, errorMsg);

        return NULL;
    }

#ifdef IMPLEMENT_BREAK_AFTER_LOAD
    nsCAutoString leafName;
    aFile->GetNativeLeafName(leafName);

    char *env = getenv("XPCOM_BREAK_ON_LOAD");
    char *blist;
    if (env && *env && (blist = strdup(env))) {
        char *nextTok = blist;
        while (char *token = NS_strtok(":", &nextTok)) {
            if (leafName.Find(token, true) != kNotFound) {
                NS_BREAK();
            }
        }

        free(blist);
    }
#endif

    void *module = PR_FindSymbol(data.library, "NSModule");
    if (!module) {
        LogMessage("Native module at path '%s' doesn't export symbol `NSModule`.",
                   filePath.get());
        PR_UnloadLibrary(data.library);
        return NULL;
    }

    data.module = *(mozilla::Module const *const *) module;
    if (mozilla::Module::kVersion != data.module->mVersion) {
        LogMessage("Native module at path '%s' is incompatible with this version of Firefox, has version %i, expected %i.",
                   filePath.get(), data.module->mVersion,
                   mozilla::Module::kVersion);
        PR_UnloadLibrary(data.library);
        return NULL;
    }
        
    mLibraries.Put(hashedFile, data); // infallible
    return data.module;
}
const mozilla::Module*
nsNativeModuleLoader::LoadModule(nsILocalFile* aFile)
{
    nsresult rv;

    if (!NS_IsMainThread()) {
        // If this call is off the main thread, synchronously proxy it
        // to the main thread.
        nsRefPtr<LoadModuleMainThreadRunnable> r = new LoadModuleMainThreadRunnable(this, aFile);
        NS_DispatchToMainThread(r, NS_DISPATCH_SYNC);
        return r->mResult;
    }

    nsCOMPtr<nsIHashable> hashedFile(do_QueryInterface(aFile));
    if (!hashedFile) {
        NS_ERROR("nsIFile is not nsIHashable");
        return NULL;
    }

    nsCAutoString filePath;
    aFile->GetNativePath(filePath);

    NativeLoadData data;

    if (mLibraries.Get(hashedFile, &data)) {
        NS_ASSERTION(data.module, "Corrupt mLibraries hash");
        LOG(PR_LOG_DEBUG,
            ("nsNativeModuleLoader::LoadModule(\"%s\") - found in cache",
             filePath.get()));
        return data.module;
    }

    // We haven't loaded this module before

    rv = aFile->Load(&data.library);

    if (NS_FAILED(rv)) {
        char errorMsg[1024] = "<unknown; can't get error from NSPR>";

        if (PR_GetErrorTextLength() < (int) sizeof(errorMsg))
            PR_GetErrorText(errorMsg);

        LOG(PR_LOG_ERROR,
            ("nsNativeModuleLoader::LoadModule(\"%s\") - load FAILED, "
             "rv: %lx, error:\n\t%s\n",
             filePath.get(), rv, errorMsg));

#ifdef DEBUG
        fprintf(stderr,
                "nsNativeModuleLoader::LoadModule(\"%s\") - load FAILED, "
                "rv: %lx, error:\n\t%s\n",
                filePath.get(), (unsigned long)rv, errorMsg);
#endif

        return NULL;
    }

#ifdef IMPLEMENT_BREAK_AFTER_LOAD
    nsCAutoString leafName;
    aFile->GetNativeLeafName(leafName);

    char *env = getenv("XPCOM_BREAK_ON_LOAD");
    char *blist;
    if (env && *env && (blist = strdup(env))) {
        char *nextTok = blist;
        while (char *token = NS_strtok(":", &nextTok)) {
            if (leafName.Find(token, PR_TRUE) != kNotFound) {
                NS_BREAK();
            }
        }

        free(blist);
    }
#endif

    void *module = PR_FindSymbol(data.library, "NSModule");
    if (module) {
        data.module = *(mozilla::Module const *const *) module;
        if (mLibraries.Put(hashedFile, data))
            return data.module;
    }
    else {
        LOG(PR_LOG_ERROR,
            ("nsNativeModuleLoader::LoadModule(\"%s\") - "
             "Symbol NSModule not found", filePath.get()));
    }

    // at some point we failed, clean up
    data.module = nsnull;
    PR_UnloadLibrary(data.library);

    return NULL;
}
void guFolderInspector::run()
{
    isbnMethods isbnExtractor;
    QDirIterator::IteratorFlag flag;
    if (toSearchInSubFolders)
        flag = QDirIterator::Subdirectories;
    else
        flag = QDirIterator::NoIteratorFlags;

    QDirIterator it(searchPath,nameFilters, QDir::Files, flag);
    while (it.hasNext())
    {
        if(terminate)
        {
            emit inspectFinished();
            terminate = false;
            return;
        }
        it.next();
        QString filePath = it.filePath();
        QFile hashedFile(filePath);
        if (!hashedFile.open(QIODevice::ReadOnly))
            continue;
        QByteArray hashedData = hashedFile.readAll();
        hashedFile.close();
        QByteArray hashData;
        hashData = QCryptographicHash::hash( hashedData, QCryptographicHash::Md5);
        QString hashString(hashData.toHex());
        hashedData.clear();
        hashData.clear();
        ebook newEbook;
        newEbook.inLib = false;
        newEbook.inLibChecked = false;

        newEbook.fileName = it.fileName();

        if(toExtractIdentifiersFromFileNames) //если указоно, то
        {    //ищем ISBNы в имени файла
            isbnExtractor.findIsbns(newEbook.fileName, newEbook.ISBNs);
        }
        //убираем точку в конце файла и точки внутри замен¤ем пробелами
        newEbook.fileExtension = it.fileInfo().suffix();
        //убираем расширение и точку
        newEbook.fileName = newEbook.fileName.left(newEbook.fileName.length() - newEbook.fileExtension.length() - 1);

        //по умолчанию заголовок - это им¤ файла
        newEbook.title = newEbook.fileName.replace(".", " ");
        newEbook.title = newEbook.title.replace("_"," ");

        newEbook.filePath = filePath;

        if (toExtractIdentifiersFromFiles) //если указано, то
        {//ищем »—ЅЌы из самих файлов
            extractIsbnsFromFile(newEbook.filePath, newEbook.ISBNs, newEbook.fileExtension);
        }

        newEbook.fileSize = it.fileInfo().size();
        newEbook.inLibCheckSended = false;
        newEbook.categoryManualSetted = false;
        newEbook.inLibChecked = false;
        newEbook.inLib = false;
        newEbook.biblioSetted = false;
        newEbook.uploaded = false;
        newEbook.catogorySubjectSetted = false;

        newEbook.pubYear = 0;
        newEbook.pageNum = 0;
        newEbook.orientation = 0;
        newEbook.scanDpi = 0;
        newEbook.scanColor = 0;
        newEbook.scanCleaned = 0;

        newEbook.categoryTreeSetted = false;
        newEbook.MD5 = hashString;
        emit addNewEbook(hashString, newEbook);

    }
    emit inspectFinished();
    return;
    //exec();
}