Esempio n. 1
0
bool ManagerApp::CheckManagerDB() {
    int version_groups = DB_MANAGER_VERSION_GROUPS;
    int version_packages = DB_MANAGER_VERSION_PACKAGES;
    int version_authorlist = DB_MANAGER_VERSION_AUTHORLIST;
    int version_packagelist = DB_MANAGER_VERSION_PACKAGELIST;
    int version_packagelist_authors = DB_MANAGER_VERSION_PACKAGELIST_AUTHORS;
    int version_packagelist_warnings = DB_MANAGER_VERSION_PACKAGELIST_WARNINGS;
    wxSQLite3StatementBuffer temp;

    m_managerdb.Open(m_appdir.GetPath(wxPATH_GET_SEPARATOR | wxPATH_GET_VOLUME) + wxT("manager.db"));
    if (!m_managerdb.TableExists(wxT("versions"))) {
        m_managerdb.ExecuteUpdate(wxT("create table versions(tablename char[30], version int);"));
        temp.Format("insert into versions values ('groups', %d);", version_groups);
        m_managerdb.ExecuteUpdate(temp);
        temp.Format("insert into versions values ('packages', %d);", version_packages);
        m_managerdb.ExecuteUpdate(temp);
        temp.Format("insert into versions values ('authorlist', %d);", version_authorlist);
        m_managerdb.ExecuteUpdate(temp);
        temp.Format("insert into versions values ('packagelist', %d);", version_packagelist);
        m_managerdb.ExecuteUpdate(temp);
        temp.Format("insert into versions values ('packagelist_authors', %d);", version_packagelist_authors);
        m_managerdb.ExecuteUpdate(temp);
        temp.Format("insert into versions values ('packagelist_warnings', %d);", version_packagelist_warnings);
        m_managerdb.ExecuteUpdate(temp);
    } else {
        version_groups = m_managerdb.ExecuteScalar(wxT("select version from versions where tablename = 'groups';"));
        version_packages = m_managerdb.ExecuteScalar(wxT("select version from versions where tablename = 'packages';"));
        version_authorlist = m_managerdb.ExecuteScalar(wxT("select version from versions where tablename = 'authorlist';"));
        version_packagelist = m_managerdb.ExecuteScalar(wxT("select version from versions where tablename = 'packagelist';"));
        version_packagelist_authors = m_managerdb.ExecuteScalar(wxT("select version from versions where tablename = 'packagelist_authors';"));
        version_packagelist_warnings = m_managerdb.ExecuteScalar(wxT("select version from versions where tablename = 'packagelist_warnings';"));
    }
    if ((version_groups > DB_MANAGER_VERSION_GROUPS) ||
        (version_packages > DB_MANAGER_VERSION_PACKAGES) ||
        (version_authorlist > DB_MANAGER_VERSION_AUTHORLIST) ||
        (version_packagelist > DB_MANAGER_VERSION_PACKAGELIST) ||
        (version_packagelist_authors > DB_MANAGER_VERSION_PACKAGELIST_AUTHORS) ||
        (version_packagelist_warnings > DB_MANAGER_VERSION_PACKAGELIST_WARNINGS)) {
        wxLogError(_("Manager database modified by a newer version of this application."));
        return false;
    }
    if (!m_managerdb.TableExists(wxT("groups"))) {
        m_managerdb.ExecuteUpdate(wxT("create table groups(name text, guid text);"));
    }
    if (!m_managerdb.TableExists(wxT("packages"))) {
        m_managerdb.ExecuteUpdate(wxT("create table packages(name text, guid text, file text, version text);"));
    }
    if (!m_managerdb.TableExists(wxT("authorlist"))) {
        m_managerdb.ExecuteUpdate(wxT("create table authorlist(name text, guid text, official int);"));
        wxFileSystem fs;

        m_managerdb.Begin();

        try {
            xmlcpp::cXmlDoc xdoc(wxString(MEMORY_PREFIX RES_XML_AUTHORS).mb_str(wxConvUTF8), NULL);
            if (xdoc.ok()) {
                xmlcpp::cXmlNode child(xdoc.getRoot().children());
                while (child.ok()) {
                    if (child.name() == "author") {
                        wxString name(child.getPropVal("name").c_str(), wxConvUTF8);
                        name.Trim();
                        name.Trim(true);
                        wxString guid(child.getPropVal("guid").c_str(), wxConvUTF8);
                        guid.Trim();
                        guid.Trim(true);
                        if (name.IsEmpty() || guid.IsEmpty()) {
                            m_managerdb.Rollback();
                            wxLogError(_("Error in author list xml."));
                            return false;
                        }
                        temp.Format("insert into authorlist values ('%q', '%q', 1);", name.mb_str(wxConvUTF8).data(), guid.mb_str(wxConvUTF8).data());
                        m_managerdb.ExecuteUpdate(temp);
                    }
                    child.go_next();
                }
            }
        } catch (std::exception& e) {
            wxLogError(wxString(e.what(), wxConvUTF8));
        }
/*
        std::auto_ptr<wxFSFile> inputfsfile(fs.OpenFile(MEMORY_PREFIX RES_XML_AUTHORS, wxFS_READ));

        wxXmlDocument doc(*inputfsfile->GetStream());

        wxXmlNode* child = doc.GetRoot()->GetChildren();
        while (child) {
            if (child->GetName() == wxT("author")) {
                wxString name = child->GetPropVal(wxT("name"), wxT(""));
                name.Trim();
                name.Trim(true);
                wxString guid = child->GetPropVal(wxT("guid"), wxT(""));
                guid.Trim();
                guid.Trim(true);
                if (name.IsEmpty() || guid.IsEmpty()) {
                    m_managerdb.Rollback();
                    wxLogError(_("Error in author list xml."));
                    return false;
                }
                temp.Format("insert into authorlist values ('%q', '%q', 1);", name.mb_str(wxConvUTF8).data(), guid.mb_str(wxConvUTF8).data());
                m_managerdb.ExecuteUpdate(temp);
            }
            child = child->GetNext();
        }
*/
        m_managerdb.Commit();
    }
    if (!m_managerdb.TableExists(wxT("packagelist"))) {
        m_managerdb.ExecuteUpdate(wxT("create table packagelist(name text, shortname text, guid text, version text, official int);"));
    }
    if (!m_managerdb.TableExists(wxT("packagelist_authors"))) {
        m_managerdb.ExecuteUpdate(wxT("create table packagelist_authors(guid text, authorguid text);"));
    }
    if (!m_managerdb.TableExists(wxT("packagelist_warnings"))) {
        m_managerdb.ExecuteUpdate(wxT("create table packagelist_warnings(guid text, type int, descrition text);"));
    }
    if (!m_managerdb.ExecuteScalar(wxT("select count(*) from packagelist;"))) {
        wxFileSystem fs;

        m_managerdb.Begin();

        try {
            xmlcpp::cXmlDoc xdoc(wxString(MEMORY_PREFIX RES_XML_PACKAGES).mb_str(wxConvUTF8), NULL);
            if (xdoc.ok()) {
                xmlcpp::cXmlNode child(xdoc.getRoot().children());
                while (child.ok()) {
                    if (child.name() == "package") {
                        wxString name(child.getPropVal("name").c_str(), wxConvUTF8);
                        name.Trim();
                        name.Trim(true);
                        wxString shortname(child.getPropVal("shortname").c_str(), wxConvUTF8);
                        shortname.Trim();
                        shortname.Trim(true);
                        wxString guid(child.getPropVal("guid").c_str(), wxConvUTF8);
                        guid.Trim();
                        guid.Trim(true);
                        wxString version(child.getPropVal("version").c_str(), wxConvUTF8);
                        version.Trim();
                        version.Trim(true);
                        if (name.IsEmpty() || guid.IsEmpty()) {
                            m_managerdb.Rollback();
                            wxLogError(_("Error in package list xml (name and/or guid missing)."));
                            return false;
                        }
                        temp.Format("insert into packagelist values ('%q', '%q', '%q', '%q', 1);", name.mb_str(wxConvUTF8).data(),
                                    shortname.mb_str(wxConvUTF8).data(), guid.mb_str(wxConvUTF8).data(), version.mb_str(wxConvUTF8).data());
                        m_managerdb.ExecuteUpdate(temp);

                        bool haveauthor = false;
                        if (child.hasProp("authorguid")) {
                            wxString authorguid(child.getPropVal("authorguid").c_str(), wxConvUTF8);
                            authorguid.Trim();
                            authorguid.Trim(true);
                            if (authorguid.IsEmpty()) {
                                m_managerdb.Rollback();
                                wxLogError(_("Error in package list xml (authorguid empty)."));
                                return false;
                            }
                            temp.Format("insert into packagelist_authors values ('%q', '%q');", guid.mb_str(wxConvUTF8).data(), authorguid.mb_str(wxConvUTF8).data());
                            m_managerdb.ExecuteUpdate(temp);
                            haveauthor  = true;
                        }

                        xmlcpp::cXmlNode subchild(child.children());
                        while(subchild.ok()) {
                            if (subchild.name() == "author") {
                                wxString authorguid(subchild.getPropVal("guid").c_str(), wxConvUTF8);
                                authorguid.Trim();
                                authorguid.Trim(true);
                                if (authorguid.IsEmpty()) {
                                    m_managerdb.Rollback();
                                    wxLogError(_("Error in package list xml (author[guid] empty)."));
                                    return false;
                                }
                                temp.Format("insert into packagelist_authors values ('%q', '%q');", guid.mb_str(wxConvUTF8).data(), authorguid.mb_str(wxConvUTF8).data());
                                m_managerdb.ExecuteUpdate(temp);
                                haveauthor  = true;
                            } else if (subchild.name() == "warning") {
                                wxString wtype(subchild.getPropVal("type").c_str(), wxConvUTF8);
                                wtype.Trim();
                                wtype.Trim(true);
                                if (wtype.IsEmpty()) {
                                    m_managerdb.Rollback();
                                    wxLogError(_("Error in package list xml (warning[type] empty)."));
                                    return false;
                                }
                                wxString desc(subchild.content().c_str(), wxConvUTF8);
                                desc.Trim();
                                desc.Trim(true);
                                temp.Format("insert into packagelist_warnings values ('%q', '%q', '%q');", guid.mb_str(wxConvUTF8).data(), wtype.mb_str(wxConvUTF8).data(), desc.mb_str(wxConvUTF8).data());
                                m_managerdb.ExecuteUpdate(temp);
                            }
                            subchild.go_next();
                        }
                        if (!haveauthor) {
                            m_managerdb.Rollback();
                            wxLogError(_("Error in package list xml (author missing)."));
                            return false;
                        }
                    }
                    child.go_next();
                }
            } else {
                wxLogError(_("Failed to open package list xml."));
                for (std::vector<xmlcpp::cXmlStructuredError>::const_iterator it = xdoc.getStructuredErrors().begin(); it != xdoc.getStructuredErrors().end(); ++it)
                    wxLogError(wxString(it->message.c_str(), wxConvUTF8));
            }
        } catch (std::exception& e) {
            wxLogError(wxString(e.what(), wxConvUTF8));
        }

/*
        std::auto_ptr<wxFSFile> inputfsfile(fs.OpenFile(MEMORY_PREFIX RES_XML_PACKAGES, wxFS_READ));

        wxXmlDocument doc(*inputfsfile->GetStream());

        wxXmlNode* child = doc.GetRoot()->GetChildren();
        while (child) {
            if (child->GetName() == wxT("package")) {
                wxString name = child->GetPropVal(wxT("name"), wxT(""));
                name.Trim();
                name.Trim(true);
                wxString shortname = child->GetPropVal(wxT("shortname"), wxT(""));
                shortname.Trim();
                shortname.Trim(true);
                wxString guid = child->GetPropVal(wxT("guid"), wxT(""));
                guid.Trim();
                guid.Trim(true);
                wxString version = child->GetPropVal(wxT("version"), wxT("0"));
                version.Trim();
                version.Trim(true);
                if (name.IsEmpty() || guid.IsEmpty()) {
                    m_managerdb.Rollback();
                    wxLogError(_("Error in package list xml (name and/or guid missing)."));
                    return false;
                }
                temp.Format("insert into packagelist values ('%q', '%q', '%q', '%q', 1);", name.mb_str(wxConvUTF8).data(),
                            shortname.mb_str(wxConvUTF8).data(), guid.mb_str(wxConvUTF8).data(), version.mb_str(wxConvUTF8).data());
                m_managerdb.ExecuteUpdate(temp);

                bool haveauthor = false;
                if (child->HasProp(wxT("authorguid"))) {
                    wxString authorguid = child->GetPropVal(wxT("authorguid"), wxT(""));
                    authorguid.Trim();
                    authorguid.Trim(true);
                    if (authorguid.IsEmpty()) {
                        m_managerdb.Rollback();
                        wxLogError(_("Error in package list xml (authorguid empty)."));
                        return false;
                    }
                    temp.Format("insert into packagelist_authors values ('%q', '%q');", guid.mb_str(wxConvUTF8).data(), authorguid.mb_str(wxConvUTF8).data());
                    m_managerdb.ExecuteUpdate(temp);
                    haveauthor  = true;
                }

                wxXmlNode* subchild = child->GetChildren();
                while(subchild) {
                    if (subchild->GetName() == wxT("author")) {
                        wxString authorguid = subchild->GetPropVal(wxT("guid"), wxT(""));
                        authorguid.Trim();
                        authorguid.Trim(true);
                        if (authorguid.IsEmpty()) {
                            m_managerdb.Rollback();
                            wxLogError(_("Error in package list xml (author[guid] empty)."));
                            return false;
                        }
                        temp.Format("insert into packagelist_authors values ('%q', '%q');", guid.mb_str(wxConvUTF8).data(), authorguid.mb_str(wxConvUTF8).data());
                        m_managerdb.ExecuteUpdate(temp);
                        haveauthor  = true;
                    } else if (subchild->GetName() == wxT("warning")) {
                        wxString wtype = subchild->GetPropVal(wxT("type"), wxT(""));
                        wtype.Trim();
                        wtype.Trim(true);
                        if (wtype.IsEmpty()) {
                            m_managerdb.Rollback();
                            wxLogError(_("Error in package list xml (warning[type] empty)."));
                            return false;
                        }
                        wxString desc = subchild->GetNodeContent();
                        desc.Trim();
                        desc.Trim(true);
                        temp.Format("insert into packagelist_warnings values ('%q', '%q', '%q');", guid.mb_str(wxConvUTF8).data(), wtype.mb_str(wxConvUTF8).data(), desc.mb_str(wxConvUTF8).data());
                        m_managerdb.ExecuteUpdate(temp);
                    }
                    subchild = subchild->GetNext();
                }
                if (!haveauthor) {
                    m_managerdb.Rollback();
                    wxLogError(_("Error in package list xml (author missing)."));
                    return false;
                }
            }
            child = child->GetNext();
        }
*/
        m_managerdb.Commit();
    }
    return true;
}
Esempio n. 2
0
int extract_images_from_pdf(char* filename,
                            char* target,
                            char* owner_password,
                            char* user_password,
                            char* range,
                            char* format,
                            int jpg_quality,
                            GBool dump_jpg,
                            GBool tiff_jpg)
{
    if (user_cancelled)
        return gpret_user_cancelled;

    // load config
    xpdf_rc xrc;

    // open file
    xpdf_doc xdoc(filename, owner_password, user_password);
    PDFDoc* doc = xdoc.get_doc();
    if (!doc->isOk())
        return doc->getErrorCode() == errEncrypted ?
               gpret_pdf_encrypted :
               gpret_cant_open_pdf;

    // check for copy permission
    // if (!doc->okToCopy())
    //	 return gpret_dont_allow_copy;

    // get page range
    page_range range_list(range);
    if (*range == '\0')
    {
        range_list.add_item(range_item(1, doc->getNumPages()));
    }

    if (user_cancelled)
        return gpret_user_cancelled;

    // write image files
    fi_loader fi;
    int progress = 0;
    image_extractor img_out(target, dump_jpg, format, jpg_quality, tiff_jpg);
    for (int i = 0; i < range_list.item_count(); i++)
    {
        range_item& item = range_list.get_item(i);
        for (int pg = item.first;
                pg <= min(item.last, doc->getNumPages());
                pg++)
        {
            if (user_cancelled)
                return gpret_user_cancelled;
            doc->displayPage(&img_out, pg, 72, 72, 0, gFalse, gTrue, gFalse);
            printf("progress: %d\n",
                   ++progress * 100 / range_list.page_count());
        }
    }

    printf("image count: %d\n", img_out.get_image_number());

    return gpret_success;
}